aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNickolay Ilyushin <nickolay02@inbox.ru>2019-11-15 12:07:35 +0200
committerNickolay Ilyushin <nickolay02@inbox.ru>2019-11-15 12:07:35 +0200
commitb657b581d9e780c1a43c43bba9a2edbf14e67836 (patch)
tree6538d49ae181054f88764d9085dda87a62d8c674
parent6b40020b11fb6568c69699885f2836b552c40dc1 (diff)
downloadLibGui-b657b581d9e780c1a43c43bba9a2edbf14e67836.tar.gz
LibGui-b657b581d9e780c1a43c43bba9a2edbf14e67836.tar.bz2
LibGui-b657b581d9e780c1a43c43bba9a2edbf14e67836.zip
Some skeleton code for anchor layouts. Do not touch this please until it works or i am completely awayhandi/anchor-layout
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WAnchorLayout.java108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAnchorLayout.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAnchorLayout.java
new file mode 100644
index 0000000..bd72836
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAnchorLayout.java
@@ -0,0 +1,108 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+public class WAnchorLayout {
+ public class AnchorException extends Exception {
+ private static final long serialVersionUID = -4344321727684528775L;
+
+ public AnchorException(AnchorSide side1, String what1, AnchorSide side2, String what2, String why) {
+ super("Unable to link " + side1.toString() + " of " + what1 + " to " + side2.toString() + " of " + what2 + ": " + why);
+ }
+ }
+
+ public enum AnchorSide {
+ TOP,
+ RIGHT,
+ BOTTOM,
+ LEFT,
+ }
+
+ public class Anchor {
+ protected final String anchorName;
+ protected final AnchorSide anchorSide;
+ protected String anchorTarget = null;
+ protected AnchorSide anchorTargetSide = null;
+ protected AnchoredWidget linkedAnchorTarget = null;
+
+ public Anchor(String _anchorName, AnchorSide _anchorSide) {
+ anchorName = _anchorName;
+ anchorSide = _anchorSide;
+ }
+
+ public void bind(String _anchorTarget, AnchorSide _anchorTargetSide) {
+ anchorTarget = _anchorTarget;
+ anchorTargetSide = _anchorTargetSide;
+ }
+
+ public void link(WAnchorLayout layout) throws AnchorException {
+ if (anchorTarget == null) {
+ return;
+ }
+ AnchoredWidget w = layout.getWidgetByName(anchorTarget);
+ if (w == null) {
+ throw new AnchorException(anchorSide, anchorName, anchorTargetSide, anchorTarget, "No such anchor: " + anchorTarget);
+ }
+ linkedAnchorTarget = w;
+ }
+ }
+
+ public class AnchoredWidget {
+ protected WWidget widget;
+ protected final String name;
+ protected Anchor top, right, bottom, left;
+
+ public AnchoredWidget(String _name, WWidget _widget) {
+ widget = _widget;
+ name = _name;
+ top = new Anchor(name, AnchorSide.TOP);
+ right = new Anchor(name, AnchorSide.RIGHT);
+ bottom = new Anchor(name, AnchorSide.BOTTOM);
+ left = new Anchor(name, AnchorSide.LEFT);
+ }
+
+ public void link(WAnchorLayout layout) throws AnchorException {
+ if (top != null) top.link(layout);
+ if (right != null) right.link(layout);
+ if (bottom != null) bottom.link(layout);
+ if (left != null) left.link(layout);
+ }
+
+ public AnchoredWidget bind(AnchorSide side, AnchorSide toSide, String ofWhat) {
+ Anchor a = null;
+ if (side == null) {
+ return this;
+ } else if (side == AnchorSide.TOP) {
+ a = top;
+ } else if (side == AnchorSide.RIGHT) {
+ a = right;
+ } else if (side == AnchorSide.BOTTOM) {
+ a = bottom;
+ } else if (side == AnchorSide.LEFT) {
+ a = left;
+ }
+ // a.anchorSide
+ return this;
+ }
+ }
+
+ protected ConcurrentHashMap<String, AnchoredWidget> children;
+
+ void add(String name, AnchoredWidget widget) {
+ children.put(name, widget);
+ }
+
+ AnchoredWidget getWidgetByName(String name) {
+ return children.get(name);
+ }
+
+ void link() {
+ children.forEach((k, w) -> {
+ try {
+ w.link(this);
+ } catch (AnchorException e) {
+ e.printStackTrace();
+ }
+ });
+ }
+} \ No newline at end of file