Skip to content

Commit

Permalink
More UI refactoring.
Browse files Browse the repository at this point in the history
Added in a ItemsComponent, which allows dynamic creation of UI elements based off of a List of items. Can be used to show repeating components that represent some form of data (ie alts, macros, friends, etc...)
Added in more components on the Macro screen.
Component's parent is now assumed when it is added to the parent, reducing the potential for the develop to mess up UI.
Other small tweaks to make order of operations more concise.
  • Loading branch information
coltonk9043 committed Nov 30, 2024
1 parent 3cc97c3 commit 1d84c5d
Show file tree
Hide file tree
Showing 37 changed files with 527 additions and 286 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modCompileOnly("maven.modrinth:sodium:${project.sodium_version}") { transitive = false }
modCompileOnly("maven.modrinth:lithium:${project.lithium_version}") { transitive = false }
modCompileOnly("maven.modrinth:lithium:${project.lithium_version}") { transitive = false }w
modCompileOnly("maven.modrinth:iris:${project.iris_version}") { transitive = false }

// Fabric API. This is technically optional, but you probably want it anyway.
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/aoba/AobaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,15 @@ public void loadAssets() {
fontManager.Initialize();
LogUtils.getLogger().info("[Aoba] Initializing Combat Manager");
combatManager = new CombatManager();

LogUtils.getLogger().info("[Aoba] Initializing Macro Manager");
macroManager = new MacroManager();
LogUtils.getLogger().info("[Aoba] Initializing GUI");
guiManager = new GuiManager();
guiManager.Initialize();
LogUtils.getLogger().info("[Aoba] Loading Alts");
altManager = new AltManager();
proxyManager = new ProxyManager();

macroManager = new MacroManager();

LogUtils.getLogger().info("[Aoba] Starting Discord RPC");
rpcManager = new RPCManager();
rpcManager.startRpc();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/aoba/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,31 +185,31 @@ public void Initialize() {

for (Category category : categories.values()) {
Window tab = new Window(category.getName(), xOffset, 75.0f);
StackPanelComponent stackPanel = new StackPanelComponent(tab);
StackPanelComponent stackPanel = new StackPanelComponent();
stackPanel.setMargin(new Margin(null, 30f, null, null));

GridComponent gridComponent = new GridComponent(stackPanel);
GridComponent gridComponent = new GridComponent();
gridComponent.addColumnDefinition(new GridDefinition(30, RelativeUnit.Absolute)); // Fill 30px
gridComponent.addColumnDefinition(new GridDefinition(1, RelativeUnit.Relative)); // Fill all remaining space

ImageComponent img = new ImageComponent(gridComponent, category.getIcon());
ImageComponent img = new ImageComponent(category.getIcon());
img.setMargin(new Margin(4f, 0f, 4f, 0f));
gridComponent.addChild(img);

StringComponent title = new StringComponent(gridComponent, category.getName());
StringComponent title = new StringComponent(category.getName());
title.setIsHitTestVisible(false);
gridComponent.addChild(title);

stackPanel.addChild(gridComponent);

SeparatorComponent separator = new SeparatorComponent(stackPanel);
SeparatorComponent separator = new SeparatorComponent();
separator.setIsHitTestVisible(false);
stackPanel.addChild(separator);

// Loop through modules and add them to the correct category
for (Module module : Aoba.getInstance().moduleManager.modules) {
if (module.getCategory().equals(category)) {
ModuleComponent button = new ModuleComponent(stackPanel, module.getName(), module);
ModuleComponent button = new ModuleComponent(module.getName(), module);
stackPanel.addChild(button);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aoba/gui/Size.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Size {
public static final Size INFINITE = new Size(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);

public static final Size ZERO = new Size(0f, 0f);
private Float width = null;
private Float height = null;

Expand Down
100 changes: 79 additions & 21 deletions src/main/java/net/aoba/gui/UIElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package net.aoba.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import net.aoba.Aoba;
import net.aoba.AobaClient;
Expand All @@ -37,7 +40,7 @@ public abstract class UIElement {
protected static MinecraftClient MC = MinecraftClient.getInstance();
protected static AobaClient AOBA = Aoba.getInstance();

protected ArrayList<UIElement> children = new ArrayList<UIElement>();
private ArrayList<UIElement> children = new ArrayList<UIElement>();
protected UIElement parent;

// Constraints (Dimensions will always adhere to these if it is not null)
Expand All @@ -60,22 +63,31 @@ public abstract class UIElement {
protected boolean hovered = false;
protected boolean isHitTestVisible = true;

public UIElement(UIElement parent) {
this.parent = parent;
public UIElement() {
preferredSize = new Size(0.0f, 0.0f);
actualSize = new Rectangle(0.0f, 0.0f, 0.0f, 0.0f);
}

public void initialize() {
this.initialized = true;
boolean wasInitialized = initialized;
if (!wasInitialized) {
this.initialized = true;
}

for (UIElement child : children) {
if (child == null)
continue;
child.initialize();
}

invalidate();
if (!wasInitialized) {
onInitialized();
invalidateMeasure();
}
}

protected void onInitialized() {

}

public void update() {
Expand Down Expand Up @@ -123,7 +135,7 @@ public Margin getMargin() {
public void setMargin(Margin margin) {
if (!this.margin.equals(margin)) {
this.margin = margin;
invalidate();
invalidateMeasure();
}
}

Expand All @@ -138,22 +150,22 @@ public void setSize(Size size) {
if (this.width != newWidth || this.height != newHeight) {
this.width = size.getWidth();
this.height = size.getHeight();
invalidate();
invalidateMeasure();
}
}

public void setSize(Float width, Float height) {
if (this.width != width || this.height != height) {
this.width = width;
this.height = height;
invalidate();
invalidateMeasure();
}
}

public void setWidth(Float width) {
if (this.width != width) {
this.width = width;
invalidate();
invalidateMeasure();
}
}

Expand All @@ -168,7 +180,7 @@ public void setHeight(Float height) {
}

this.height = height;
invalidate();
invalidateMeasure();
}
}

Expand Down Expand Up @@ -254,13 +266,23 @@ public UIElement getParent() {
return parent;
}

public void invalidate() {
public void setParent(UIElement parent) {
this.parent = parent;
invalidateMeasure();
}

public void invalidateMeasure() {
if (initialized) {
if (parent != null)
parent.invalidate();
else {
if (parent != null) {
parent.invalidateMeasure();
} else {

// Construct new bounds based off of width/height constraints.
Size size = new Size(0f, 0f);
Size size;
if (parent == null)
size = new Size(0f, 0f);
else
size = parent.getPreferredSize();

if (width != null)
size.setWidth(width);
Expand All @@ -287,6 +309,13 @@ public void invalidate() {
}
}

public void invalidateArrange() {
if (initialized) {
Rectangle rect = new Rectangle(0f, 0f, preferredSize.getWidth(), preferredSize.getHeight());
arrange(rect);
}
}

/**
* Measures the UI element accounting for all of the children. This method spans
* all of the children, starting from the bottom up.
Expand All @@ -296,6 +325,11 @@ public void invalidate() {
* @return The new preferred size of the UI Element.
*/
public void measure(Size availableSize) {
if (!isVisible()) {
preferredSize = Size.ZERO;
return;
}

if (initialized) {
float finalWidth = availableSize.getWidth();
float finalHeight = availableSize.getHeight();
Expand Down Expand Up @@ -384,18 +418,29 @@ public void arrange(Rectangle finalSize) {
newFinalSize = finalSize;
}

Rectangle oldActualSize = actualSize;
setActualSize(newFinalSize);

for (UIElement element : children) {
element.arrange(this.getActualSize());
if (!oldActualSize.equals(actualSize)) {
for (UIElement element : children) {
element.arrange(this.getActualSize());
}
}
}
}

public List<UIElement> getChildren() {
return Collections.unmodifiableList(children);
}

public void addChild(UIElement child) {
if (child == null)
return;

if (this.initialized && !child.initialized)
child.initialize();

child.setParent(this);
children.add(child);
onChildAdded(child);
}
Expand All @@ -408,24 +453,37 @@ public void removeChild(UIElement child) {
onChildRemoved(child);
}

public void clearChildren() {
children.clear();
// TODO: Implement children cleared as list.
}

public void onChildAdded(UIElement child) {
invalidate();
invalidateMeasure();
}

public void onChildChanged(UIElement child) {
invalidate();
invalidateMeasure();
}

public void onChildRemoved(UIElement child) {
invalidate();
invalidateMeasure();
}

public void onVisibilityChanged() {
invalidate();
invalidateMeasure();
}

/**
* Dispose method to release resources.
*/
public void dispose() {
Iterator<UIElement> children = getChildren().iterator();
while (children.hasNext()) {
children.next().dispose();
}

this.clearChildren();
}

public void onMouseMove(MouseMoveEvent event) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/aoba/gui/components/BlocksComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.aoba.gui.Margin;
import net.aoba.gui.Rectangle;
import net.aoba.gui.Size;
import net.aoba.gui.UIElement;
import net.aoba.gui.colors.Color;
import net.aoba.settings.types.BlocksSetting;
import net.aoba.utils.render.Render2D;
Expand Down Expand Up @@ -59,8 +58,8 @@ public class BlocksComponent extends Component implements MouseScrollListener {
*
* @param parent Parent Tab that this Component resides in.
*/
public BlocksComponent(UIElement parent, BlocksSetting setting) {
super(parent);
public BlocksComponent(BlocksSetting setting) {
super();
this.text = setting.displayName;
blocks = setting;

Expand Down
Loading

0 comments on commit 1d84c5d

Please sign in to comment.