aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java8
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java6
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java118
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java7
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java42
5 files changed, 176 insertions, 5 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java
index f19cce7..16c0484 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java
@@ -167,8 +167,8 @@ public class ClientCottonScreen extends Screen {
return true;
}
- @Override
- public Element getFocused() {
- return this;
- }
+ //@Override
+ //public Element getFocused() {
+ //return this;
+ //}
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java
new file mode 100644
index 0000000..fd97f20
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java
@@ -0,0 +1,6 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+public enum Axis {
+ HORIZONTAL,
+ VERTICAL;
+}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java
new file mode 100644
index 0000000..a98857e
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java
@@ -0,0 +1,118 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.function.BiConsumer;
+import java.util.function.Supplier;
+
+import io.github.cottonmc.cotton.gui.GuiDescription;
+import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
+
+/**
+ * Similar to the RecyclerView in Android, this widget represents a scrollable list of items.
+ *
+ * <p> D is the type of data represented. The data must reside in some ordered backing {@code List<D>}.
+ * D's *must* have working equals and hashCode methods to distinguish them from each other!
+ * <p> W is the WWidget class that will represent a single D of data.
+ */
+public class WListPanel<D, W extends WWidget> extends WPanel {
+ protected List<D> data;
+ protected Class<W> listItemClass;
+ protected Supplier<W> supplier;
+ protected BiConsumer<D, W> configurator;
+
+ protected HashMap<D, W> configured = new HashMap<>();
+ protected List<W> unconfigured = new ArrayList<>();
+ protected int cellHeight = 20;
+ protected boolean fixedHeight = false;
+
+ protected int margin = 4;
+
+ protected int scrollOffset;
+
+ public WListPanel(List<D> data, Class<W> listItemClass, Supplier<W> supplier, BiConsumer<D, W> configurator) {
+ this.data = data;
+ this.listItemClass = listItemClass;
+ this.supplier = supplier;
+ this.configurator = configurator;
+ }
+
+ @Override
+ public void paintBackground(int x, int y) {
+ if (getBackgroundPainter()!=null) {
+ getBackgroundPainter().paintBackground(x, y, this);
+ } else {
+ ScreenDrawing.drawBeveledPanel(x, y, width, height);
+ }
+
+ for(WWidget child : children) {
+ child.paintBackground(x + child.getX(), y + child.getY());
+ }
+ }
+
+ @Override
+ public void layout() {
+ super.layout();
+
+ System.out.println("Validating");
+
+ //Recompute cellHeight if needed
+ if (!fixedHeight) {
+ if (unconfigured.isEmpty()) {
+ if (configured.isEmpty()) {
+ W exemplar = supplier.get();
+ unconfigured.add(exemplar);
+ if (!exemplar.canResize()) cellHeight = exemplar.getHeight();
+ } else {
+ W exemplar = configured.values().iterator().next();
+ if (!exemplar.canResize()) cellHeight = exemplar.getHeight();
+ }
+ } else {
+ W exemplar = unconfigured.get(0);
+ if (!exemplar.canResize()) cellHeight = exemplar.getHeight();
+ }
+ }
+ if (cellHeight<4) cellHeight=4;
+
+ int layoutHeight = this.getHeight()-(margin*2);
+ int cellsHigh = layoutHeight / cellHeight;
+ int presentCells = Math.min(data.size()-scrollOffset, cellsHigh);
+
+ System.out.println("Adding children...");
+
+ this.children.clear();
+ if (presentCells>0) {
+ for(int i=0; i<presentCells; i++) {
+ int index = i+scrollOffset;
+ D d = data.get(index);
+ W w = configured.get(d);
+ if (w==null) {
+ if (unconfigured.isEmpty()) {
+ w = supplier.get();
+ } else {
+ w = unconfigured.remove(0);
+ }
+ configurator.accept(d, w);
+ }
+
+ //At this point, w is nonnull and configured by d
+ if (w.canResize()) {
+ w.setSize(this.width-(margin*2), cellHeight);
+ }
+ w.x = margin;
+ w.y = margin + (cellHeight * i);
+ this.children.add(w);
+ }
+ }
+
+ System.out.println("Children: "+children.size());
+ }
+
+ public WListPanel<D, W> setListItemHeight(int height) {
+ cellHeight = height;
+ fixedHeight = true;
+ return this;
+ }
+}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java
index 630494f..055e188 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java
@@ -9,7 +9,7 @@ import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
-public class WPanel extends WWidget {
+public abstract class WPanel extends WWidget {
protected final List<WWidget> children = Lists.newArrayList();
@Environment(EnvType.CLIENT)
private BackgroundPainter backgroundPainter = null;
@@ -37,6 +37,11 @@ public class WPanel extends WWidget {
return this;
}
+ @Environment(EnvType.CLIENT)
+ public BackgroundPainter getBackgroundPainter() {
+ return this.backgroundPainter;
+ }
+
/**
* Uses this Panel's layout rules to reposition and resize components to fit nicely in the panel.
*/
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java
new file mode 100644
index 0000000..289a2cd
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java
@@ -0,0 +1,42 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
+
+public class WScrollBar extends WWidget {
+ protected Axis axis = Axis.HORIZONTAL;
+ protected int value;
+ protected int maxValue = 100;
+ protected int window = 16;
+
+ public WScrollBar() {
+ }
+
+ public WScrollBar(Axis axis) {
+ this.axis = axis;
+ }
+
+ @Override
+ public void paintBackground(int x, int y) {
+ ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFFFFFFFF, 0xFF8b8b8b, 0xFF373737);
+
+ float barHeight = (window>maxValue) ? 1f : window / maxValue;
+ int scrollDistance = maxValue - window;
+
+
+ super.paintBackground(x, y);
+ }
+
+ @Override
+ public void setSize(int x, int y) {
+ switch(axis) {
+ case HORIZONTAL:
+ this.width = x;
+ this.height = 8;
+ break;
+ case VERTICAL:
+ this.width = 8;
+ this.height = y;
+ break;
+ }
+ }
+}