From: Daniel Kirschten Date: Sun, 30 Jun 2019 16:14:24 +0000 (+0200) Subject: TextComponent no longer overrides getBounds but rather calls setSize() X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=35df5a4186f12073d5cdc956b645b882b43b858a;p=Mograsim.git TextComponent no longer overrides getBounds but rather calls setSize() --- diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java index a80c15c4..655422a5 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java @@ -203,7 +203,7 @@ public abstract class GUIComponent * * @author Daniel Kirschten */ - public Rectangle getBounds() + public final Rectangle getBounds() { return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height); } diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/TextComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/TextComponent.java index e5a6347a..b0138aa2 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/TextComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/TextComponent.java @@ -10,35 +10,32 @@ import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter; import net.mograsim.logic.ui.modeladapter.componentadapters.NoLogicAdapter; import net.mograsim.preferences.Preferences; +//TODO clean size calculation mess public class TextComponent extends GUIComponent { private final String text; - private Point textExtent; public TextComponent(ViewModelModifiable model, String text) { super(model); this.text = text; + // If size is unset, it defaults to 0, which could prohibit this component from being rendered, which would prohibit the size being + // set to a better guess + setSize(1, 1); } @Override public void render(GeneralGC gc, Rectangle visibleRegion) { - textExtent = gc.textExtent(text); + Point textExtent = gc.textExtent(text); + setSize(textExtent.x, textExtent.y); + Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text"); if (textColor != null) gc.setForeground(textColor); gc.drawText(text, getPosX(), getPosY(), true); } - @Override - public Rectangle getBounds() - { - if (textExtent == null) - return super.getBounds(); - return new Rectangle(getPosX(), getPosY(), textExtent.x, textExtent.y); - } - static { ViewLogicModelAdapter.addComponentAdapter(new NoLogicAdapter<>(TextComponent.class));