From bf454215ea0442831ef46466c425447e095cf3d4 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 3 Dec 2018 02:04:43 +0100 Subject: Add widget anchors to place widgets in relative positions --- src/subconscious/graphics/Scene.java | 28 +++++++++++++++++++++++++++- src/subconscious/graphics/widget/Widget.java | 9 +++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 01b8fe9..711a60f 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -12,6 +12,7 @@ import java.lang.Thread; import java.util.ArrayList; import java.util.Map; +import java.util.EnumMap; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.Lock; @@ -28,6 +29,7 @@ import java.awt.Panel; import java.awt.RenderingHints; import java.awt.Font; import java.awt.FontFormatException; +import java.awt.Point; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; @@ -72,6 +74,7 @@ public abstract class Scene extends Panel RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED ); + protected EnumMap widgetAnchors = new EnumMap(Widget.Anchor.class); protected ArrayList widgets = new ArrayList<>(); protected ArrayList dynamicWidgetsCache = new ArrayList<>(); protected ArrayList clickableWidgetsCache = new ArrayList<>(); @@ -118,6 +121,17 @@ public abstract class Scene extends Panel this.canvas.addMouseWheelListener(this); // TODO: solve canvas + + // create widget anchors + this.widgetAnchors.put(Widget.Anchor.SO, new Point(0, this.canvasSize.height)); + this.widgetAnchors.put(Widget.Anchor.S , new Point(this.canvasSize.width / 2, this.canvasSize.height)); + this.widgetAnchors.put(Widget.Anchor.SE, new Point(this.canvasSize.width, this.canvasSize.height)); + this.widgetAnchors.put(Widget.Anchor.E , new Point(this.canvasSize.width, this.canvasSize.width / 2)); + this.widgetAnchors.put(Widget.Anchor.NE, new Point(this.canvasSize.width, 0)); + this.widgetAnchors.put(Widget.Anchor.N , new Point(this.canvasSize.width / 2, 0)); + this.widgetAnchors.put(Widget.Anchor.NO, new Point(0, 0)); + this.widgetAnchors.put(Widget.Anchor.O , new Point(0, this.canvasSize.height / 2)); + // this.add(this.canvas, BorderLayout.CENTER); this.build(); @@ -146,8 +160,11 @@ public abstract class Scene extends Panel protected void renderWidgets(Graphics2D g) { for (Widget w : this.widgets) { + Point absPos = this.widgetAnchors.get(w.getAnchor()); + absPos.translate(w.getX(), w.getY()); + Graphics2D widgetGraphics = (Graphics2D) g.create( - w.getX(), w.getY(), w.getWidth(), w.getHeight() + absPos.x, absPos.y, w.getWidth(), w.getHeight() ); w.render(widgetGraphics); @@ -315,6 +332,15 @@ public abstract class Scene extends Panel // this one always works public synchronized void updateCanvasSize(Dimension newSize) { this.canvasSize = newSize; + + this.widgetAnchors.get(Widget.Anchor.SO).move(0, this.canvasSize.height); + this.widgetAnchors.get(Widget.Anchor.S ).move(this.canvasSize.width / 2, this.canvasSize.height); + this.widgetAnchors.get(Widget.Anchor.SE).move(this.canvasSize.width, this.canvasSize.height); + this.widgetAnchors.get(Widget.Anchor.E ).move(this.canvasSize.width, this.canvasSize.width); + this.widgetAnchors.get(Widget.Anchor.NE).move(this.canvasSize.width, 0); + this.widgetAnchors.get(Widget.Anchor.N ).move(this.canvasSize.width / 2, 0); + this.widgetAnchors.get(Widget.Anchor.NO).move(0, 0); + this.widgetAnchors.get(Widget.Anchor.O ).move(0, this.canvasSize.height / 2); } /* key listener */ diff --git a/src/subconscious/graphics/widget/Widget.java b/src/subconscious/graphics/widget/Widget.java index 0a9e5f6..0da1b40 100644 --- a/src/subconscious/graphics/widget/Widget.java +++ b/src/subconscious/graphics/widget/Widget.java @@ -2,14 +2,20 @@ package subconscious.graphics.widget; import java.awt.Graphics2D; import java.awt.Rectangle; +import java.awt.Point; public abstract class Widget { + public enum Anchor { + SO, S, SE, E, NE, N, NO, O + } public final String UNIQUE_NAME; protected int x, y; protected int width, height; + // default anchor is relative to (0, 0) + protected Anchor anchor = Anchor.NO; public Widget(String uniqueName, int x, int y, int width, int height) { this.UNIQUE_NAME = uniqueName; @@ -30,6 +36,9 @@ public abstract class Widget { public int getX() { return this.x; } public int getY() { return this.y; } + public Point getPoint() { return new Point(this.x, this.y); } public int getWidth() { return this.width; } public int getHeight() { return this.height; } + + public Anchor getAnchor() { return this.anchor; } } \ No newline at end of file -- cgit v1.2.1