From 3139dff80016c139f7da472d13ddc2746e64be24 Mon Sep 17 00:00:00 2001 From: Speiger Date: Mon, 10 Jun 2024 23:24:06 +0200 Subject: [PATCH] Started work on the GuiContainer implementation --- .../src/coreengine/math/misc/ColorUtils.java | 73 +----------------- .../gui/components/base/GuiComponent.java | 1 + .../gui/components/base/IComponentScreen.java | 6 ++ .../rendering/gui/screen/GuiContainer.java | 77 +++++++++++++++++++ .../guiOld/components/PieComponent.java | 4 +- .../color/ColorPickerWindowComponent.java | 13 ++-- .../rendering/input/window/VideoMode.java | 1 - 7 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java diff --git a/src/main/java/speiger/src/coreengine/math/misc/ColorUtils.java b/src/main/java/speiger/src/coreengine/math/misc/ColorUtils.java index c2932c7..a0350d8 100644 --- a/src/main/java/speiger/src/coreengine/math/misc/ColorUtils.java +++ b/src/main/java/speiger/src/coreengine/math/misc/ColorUtils.java @@ -4,7 +4,6 @@ import java.nio.ByteBuffer; import java.nio.FloatBuffer; import speiger.src.collections.floats.lists.FloatList; -import speiger.src.coreengine.math.MathUtils; public class ColorUtils { static final float DEVIDER = 1F / 255F; @@ -71,7 +70,7 @@ public class ColorUtils { list.add(((color >> 16) & 0xFF) * DEVIDER); list.add(((color >> 8) & 0xFF) * DEVIDER); list.add((color & 0xFF) * DEVIDER); - if(alpha) { list.add(((color >> 24) & 0xFF) * DEVIDER); } + if(alpha) list.add(((color >> 24) & 0xFF) * DEVIDER); } public static int read(ByteBuffer buffer, boolean alpha) { return alpha ? rgb(buffer.get(), buffer.get(), buffer.get()) : rgb(buffer.get(), buffer.get(), buffer.get(), buffer.get()); } @@ -92,76 +91,6 @@ public class ColorUtils { return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | b & 0xFF; } - public static int darker(int color) { return darker(color, 0.7F); } - public static int darker(int color, float factor) { - int r = Math.max(0, (int)(((color >> 16) & 0xFF) * factor)); - int g = Math.max(0, (int)(((color >> 8) & 0xFF) * factor)); - int b = Math.max(0, (int)((color & 0xFF) * factor)); - return (color & A) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF); - } - - public static int brighter(int color) { return brighter(color, 0.7F); } - public static int brighter(int color, float factor) { - int r = (color >> 16) & 0xFF; - int g = (color >> 8) & 0xFF; - int b = color & 0xFF; - int i = (int)(1.0 / (1.0 - factor)); - if(r == 0 && g == 0 && b == 0) { return (color & A) | ((i & 0xFF) << 16) | ((i & 0xFF) << 8) | (i & 0xFF); } - if(r > 0 && r < i) r = i; - if(g > 0 && g < i) g = i; - if(b > 0 && b < i) b = i; - return (color & A) | Math.min(255, (int)(r / factor)) << 16 | Math.min(255, (int)(g / factor)) << 8 | Math.min(255, (int)(b / factor)); - } - - public static int toRGB(float hue, float saturation, float brightness) { - if(saturation == 0) { - int result = (int)(brightness * 255F + 0.5F); - return rgb(result, result, result); - } - float h = (hue - MathUtils.floor(hue)) * 6F; - float f = h - MathUtils.floor(h); - float p = brightness * (1F - saturation); - float q = brightness * (1F - saturation * f); - float t = brightness * (1F - (saturation * (1F - f))); - switch((int)h) { - case 0: return rgb(brightness, t, p); - case 1: return rgb(q, brightness, p); - case 2: return rgb(p, brightness, t); - case 3: return rgb(p, q, brightness); - case 4: return rgb(t, p, brightness); - case 5: return rgb(brightness, p, q); - default: return BLACK; - } - } - - public static float[] toHue(int rgba) { - int r = getR(rgba); - int g = getG(rgba); - int b = getB(rgba); - int cmax = (r > g) ? r : g; - if(b > cmax) cmax = b; - int cmin = (r < g) ? r : g; - if(b < cmin) cmin = b; - float length = cmax - cmin; - - float[] result = new float[3]; - result[1] = cmax == 0 ? 0F : length / cmax; - result[2] = cmax * DEVIDER; - float hue = 0F; - if(result[1] != 0F) { - float redc = (cmax - r) / length; - float greenc = (cmax - g) / length; - float bluec = (cmax - b) / length; - if(r == cmax) hue = bluec - greenc; - else if(g == cmax) hue = 2F + redc - bluec; - else hue = 4F + greenc - redc; - hue /= 6F; - if(hue < 0) hue += 1F; - } - result[0] = hue; - return result; - } - public static int rgb(int rgb) { return rgb | (255 << 24); } public static int rgb(int r, int g, int b) { return A | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF); } public static int rgb(float r, float g, float b) { return rgb((int)(r * 255F + 0.5F), (int)(g * 255F + 0.5F), (int)(b * 255F + 0.5F)); } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/components/base/GuiComponent.java b/src/main/java/speiger/src/coreengine/rendering/gui/components/base/GuiComponent.java index b55ae47..dc6df0c 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/components/base/GuiComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/components/base/GuiComponent.java @@ -166,6 +166,7 @@ public abstract non-sealed class GuiComponent extends FlagObject implements ICas public IComponentScreen screen() { return screen; } @Override public IGuiBox getBox() { return box; } + public IInteractable interactContainer() { return interactions; } @Override public GuiComponent set(float x, float y) { diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/components/base/IComponentScreen.java b/src/main/java/speiger/src/coreengine/rendering/gui/components/base/IComponentScreen.java index 074867c..e88fe54 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/components/base/IComponentScreen.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/components/base/IComponentScreen.java @@ -1,8 +1,14 @@ package speiger.src.coreengine.rendering.gui.components.base; +import speiger.src.coreengine.rendering.gui.layout.constraints.ConstraintContainer; import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox; public interface IComponentScreen { public IGuiBox getBox(); public long clock(); + + public void pushLayer(); + public void popLayer(); + public GuiComponent addComponent(GuiComponent component, ConstraintContainer constraints); + public boolean removeComponent(GuiComponent component); } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java b/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java new file mode 100644 index 0000000..bad7c2b --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java @@ -0,0 +1,77 @@ +package speiger.src.coreengine.rendering.gui.screen; + +import java.util.List; + +import speiger.src.collections.ints.sets.IntOpenHashSet; +import speiger.src.collections.ints.sets.IntSet; +import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.collections.objects.utils.ObjectIterables; +import speiger.src.collections.objects.utils.ObjectIterators; +import speiger.src.coreengine.rendering.gui.components.base.GuiComponent; +import speiger.src.coreengine.rendering.gui.components.base.IComponentScreen; +import speiger.src.coreengine.rendering.gui.components.base.IInteractable; +import speiger.src.coreengine.rendering.gui.components.base.IInteractableContainer; +import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox; + +public class GuiContainer implements IComponentScreen, IInteractableContainer { + LayeredContainer container = new LayeredContainer(); + IntSet activeButtons = new IntOpenHashSet(); + IInteractable focused; + IGuiBox box = IGuiBox.of(0F, 0F, 0F, 0F); + long tick; + + @Override + public IGuiBox getBox() { return box; } + @Override + public long clock() { return tick; } + @Override + public boolean isFocused() { return focused != null; } + @Override + public void setFocused(boolean value) { if(!value) setFocusedChild(null); } + @Override + public List getChildren() { return container.interactables(); } + @Override + public void setFocusedChild(IInteractable child) { + if(focused != null) focused.setFocused(false); + this.focused = child; + if(focused != null) focused.setFocused(true); + } + @Override + public IInteractable getFocusedChild() { return focused; } + @Override + public IntSet getActiveButtons() { return activeButtons; } + + public static class LayeredContainer { + List> components = new ObjectArrayList<>(); + List> interactables = new ObjectArrayList<>(); + int layer = -1; + + public LayeredContainer() { pushLayer(); } + + public void pushLayer() { + layer++; + components.add(new ObjectArrayList<>()); + interactables.add(new ObjectArrayList<>()); + } + + public void popLayer() { + if(layer == 0) return; + layer--; + components.removeLast(); + interactables.removeLast(); + } + + public void add(GuiComponent component) { + children().add(component); + interactables().add(component.interactContainer()); + } + + public List children() { + return components.get(layer); + } + + public List interactables() { + return interactables.get(layer); + } + } +} diff --git a/src/main/java/speiger/src/coreengine/rendering/guiOld/components/PieComponent.java b/src/main/java/speiger/src/coreengine/rendering/guiOld/components/PieComponent.java index 577de0d..df10023 100644 --- a/src/main/java/speiger/src/coreengine/rendering/guiOld/components/PieComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/guiOld/components/PieComponent.java @@ -6,7 +6,7 @@ import java.util.function.Supplier; import org.lwjgl.opengl.GL11; import speiger.src.coreengine.math.MathUtils; -import speiger.src.coreengine.math.misc.ColorUtils; +import speiger.src.coreengine.math.misc.ColorSpaces; import speiger.src.coreengine.rendering.guiOld.GuiComponent; import speiger.src.coreengine.rendering.guiOld.renderer.buffer.RenderBuffer; import speiger.src.coreengine.rendering.tesselationOld.Tesselator; @@ -106,7 +106,7 @@ public class PieComponent extends GuiComponent IPieIndex pieIndex = indexes.get(j); int steps = j == 0 ? maxSteps - stepsDone : pieIndex.getSteps(); int color = pieIndex.getColor(); - int darker = ColorUtils.darker(color); + int darker = ColorSpaces.ABGR.darker(color); tes.offset(0F, 0F, 0.01F); for(int i = 0;i