Skip to content

Commit

Permalink
Additional comments on Components and some logic change.
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonk9043 committed Sep 3, 2023
1 parent 5cecfc1 commit 93385f7
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 91 deletions.
48 changes: 31 additions & 17 deletions src/main/java/net/aoba/gui/tabs/components/ButtonComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ public class ButtonComponent extends Component implements MouseLeftClickListener

private String text;
private ClickGuiTab parent;
private boolean wasClicked = false;
private boolean hovered = false;
private Runnable onClick;

private Color hoverColor = new Color(90, 90, 90);
private Color color = new Color(128, 128, 128);

private Color backgroundColor = color;
private boolean focused = false;


/**
* Constructor for button component.
* @param parent Parent Tab that this Component resides in.
* @param text Text contained in this button element.
* @param onClick OnClick delegate that will run when the button is pressed.
*/
public ButtonComponent(ClickGuiTab parent, String text, Runnable onClick) {
super();
this.text = text;
Expand All @@ -32,14 +34,29 @@ public ButtonComponent(ClickGuiTab parent, String text, Runnable onClick) {
Aoba.getInstance().eventManager.AddListener(MouseLeftClickListener.class, this);
}

/**
* Sets the text of the button.
* @param text Text to set.
*/
public void setText(String text) {
this.text = text;
}

/**
* Sets the OnClick delegate of the button.
* @param onClick Delegate to set.
*/
public void setOnClick(Runnable onClick) {
this.onClick = onClick;
}

/**
* Draws the button to the screen.
* @param offset The offset (Y location relative to parent) of the Component.
* @param drawContext The current draw context of the game.
* @param partialTicks The partial ticks used for interpolation.
* @param color The current Color of the UI.
*/
@Override
public void draw(int offset, DrawContext drawContext, float partialTicks, Color color) {
float parentX = parent.getX();
Expand All @@ -52,6 +69,10 @@ public void draw(int offset, DrawContext drawContext, float partialTicks, Color
renderUtils.drawString(drawContext, this.text, parentX + 8, parentY + 8 + offset, 0xFFFFFF);
}

/**
* Triggered when the user clicks the Left Mouse Button (LMB)
* @param event Event fired.
*/
@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
float parentX = parent.getX();
Expand All @@ -62,19 +83,12 @@ public void OnMouseLeftClick(MouseLeftClickEvent event) {
int mouseY = event.GetMouseY();

if (HudManager.currentGrabbed == null) {
// Enable Module
if ((mouseX >= ((parentX + 2)) && mouseX <= (((parentX + 2)) + parentWidth - 34))
&& (mouseY >= parentY + offset && mouseY <= (parentY + offset + 28))) {
backgroundColor = hoverColor;
hovered = true;
if (!this.wasClicked) {
focused = true;
// If our delegate exists and we are inside the bounds of the button, run it.
if(this.onClick != null) {
if ((mouseX >= ((parentX + 2)) && mouseX <= (((parentX + 2)) + parentWidth - 34))
&& (mouseY >= parentY + offset && mouseY <= (parentY + offset + 28))) {
this.onClick.run();
}
} else {
this.backgroundColor = color;
this.hovered = false;
this.wasClicked = false;
focused = true;
}
}
}
Expand Down
61 changes: 30 additions & 31 deletions src/main/java/net/aoba/gui/tabs/components/CheckboxComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,60 @@

public class CheckboxComponent extends Component implements MouseLeftClickListener {
private String text;
private boolean wasClicked = false;

BooleanSetting checkbox;
private BooleanSetting checkbox;
private Runnable onClick;

public CheckboxComponent(ClickGuiTab parent, BooleanSetting checkbox) {
super();
this.text = checkbox.displayName;
this.parent = parent;
this.checkbox = checkbox;

Aoba.getInstance().eventManager.AddListener(MouseLeftClickListener.class, this);
}

/**
* Draws the checkbox to the screen.
* @param offset The offset (Y location relative to parent) of the Component.
* @param drawContext The current draw context of the game.
* @param partialTicks The partial ticks used for interpolation.
* @param color The current Color of the UI.
*/
@Override
public void draw(int offset, DrawContext drawContext, float partialTicks, Color color) {
float parentX = parent.getX();
float parentY = parent.getY();
float parentWidth = parent.getWidth();
MatrixStack matrixStack = drawContext.getMatrices();
renderUtils.drawString(drawContext, this.text, parentX + 10,
parentY + offset + 8, 0xFFFFFF);
if(this.checkbox.getValue()) {
renderUtils.drawOutlinedBox(matrixStack, parentX + parentWidth - 24, parentY + 1 + offset, 20, 20, new Color(0,154,0), 0.8f);
}else {
renderUtils.drawOutlinedBox(matrixStack, parentX + parentWidth - 24, parentY + 1 + offset, 20, 20, new Color(154,0,0), 0.8f);
renderUtils.drawString(drawContext, this.text, parentX + 10, parentY + offset + 8, 0xFFFFFF);
if (this.checkbox.getValue()) {
renderUtils.drawOutlinedBox(matrixStack, parentX + parentWidth - 24, parentY + 1 + offset, 20, 20,
new Color(0, 154, 0), 0.8f);
} else {
renderUtils.drawOutlinedBox(matrixStack, parentX + parentWidth - 24, parentY + 1 + offset, 20, 20,
new Color(154, 0, 0), 0.8f);
}
}

/**
* Handles updating the Checkbox component.
* @param offset The offset (Y position relative to parent) of the Checkbox.
*/
@Override
public void update(int offset) {
// TODO Auto-generated method stub

}

/**
* Triggered when the user clicks the Left Mouse Button (LMB)
*
* @param event Event fired.
*/
@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
float parentX = parent.getX();
float parentY = parent.getY();
float parentWidth = parent.getWidth();

int mouseX = event.GetMouseX();
int mouseY = event.GetMouseY();

if (HudManager.currentGrabbed == null) {
if (!this.wasClicked) {
if (mouseX >= ((parentX + parent.getWidth() - 28))
&& mouseX <= ((parentX + parentWidth - 8))) {
if (mouseY >= (((parentY + offset + 2)))
&& mouseY <= (parentY + offset + 22)) {
if (!this.wasClicked) {
checkbox.toggle();
this.wasClicked = true;
}
}
}
if (HudManager.currentGrabbed == null && hovered) {
checkbox.toggle();
if(onClick != null) {
onClick.run();
}
}
}
Expand Down
49 changes: 43 additions & 6 deletions src/main/java/net/aoba/gui/tabs/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.aoba.event.events.MouseMoveEvent;
import net.aoba.event.listeners.MouseMoveListener;
import net.aoba.gui.Color;
import net.aoba.gui.HudManager;
import net.aoba.gui.tabs.ClickGuiTab;
import net.aoba.misc.RenderUtils;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -21,48 +22,84 @@ public Component() {
Aoba.getInstance().eventManager.AddListener(MouseMoveListener.class, this);
}

/**
* Sets whether the component is visible or not
* @param bool State to set visibility to.
*/
public void setVisible(boolean bool) {
this.visible = bool;

// Register and Unregister event listener according to state.
if(bool) {
Aoba.getInstance().eventManager.AddListener(MouseMoveListener.class, this);
}else {
Aoba.getInstance().eventManager.RemoveListener(MouseMoveListener.class, this);
}
}

/**
* Whether or not the component is currently visible.
* @return Visibility state as a boolean.
*/
public boolean isVisible() {
return this.visible;
}

/**
* Gets the height of the component.
* @return Height of the component as an integer.
*/
public int getHeight()
{
return height;
}

/**
* Sets the height of the component.
* @param height The height to set.
*/
public void setHeight(int height)
{
this.height = height;
}

/**
* Returns the parent of the Component.
* @return Parent of the component as a ClickGuiTab.
*/
public ClickGuiTab getParent()
{
return parent;
}

public void setParent(ClickGuiTab parent)
{
this.parent = parent;
}

/**
* Updates the offset (y position relative to parent) of the component.
* @param offset Offset
*/
public void update(int offset) {
this.offset = offset;
}

/**
* Abstract method for drawing components onto the screen.
* @param offset Offset of the module.
* @param drawContext DrawContext of the game.
* @param partialTicks Partial Ticks of the game.
* @param color Color of the UI.
*/
public abstract void draw(int offset, DrawContext drawContext, float partialTicks, Color color);

/**
* Triggers when the mouse is moved.
* @param mouseMoveEvent Event fired.
*/
@Override
public void OnMouseMove(MouseMoveEvent mouseMoveEvent) {
System.out.println("Mouse Move Component");
if (HudManager.currentGrabbed != null) {
this.hovered = false;
return;
}

float parentX = parent.getX();
float parentY = parent.getY();
float parentWidth = parent.getWidth();
Expand Down
51 changes: 22 additions & 29 deletions src/main/java/net/aoba/gui/tabs/components/ListComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
public class ListComponent extends Component implements MouseLeftClickListener {

private String text;
private boolean wasClicked = false;
private IndexedStringListSetting list;
private List<Component> settingsList = new ArrayList<Component>();

public ListComponent(String text, ClickGuiTab parent) {
super();
this.text = text;
this.parent = parent;

Aoba.getInstance().eventManager.AddListener(MouseLeftClickListener.class, this);
}

Expand All @@ -34,7 +33,7 @@ public ListComponent(ClickGuiTab parent, IndexedStringListSetting list) {
this.text = list.displayName;
this.parent = parent;
this.list = list;

Aoba.getInstance().eventManager.AddListener(MouseLeftClickListener.class, this);
}

Expand All @@ -45,43 +44,37 @@ public void draw(int offset, DrawContext drawContext, float partialTicks, Color
float parentWidth = parent.getWidth();
int length = MinecraftClient.getInstance().textRenderer.getWidth(list.getIndexValue());
MatrixStack matrixStack = drawContext.getMatrices();
renderUtils.drawOutlinedBox(matrixStack, parentX + 4, parentY + offset, parentWidth - 8, 22, new Color(25,25,25),
0.3f);
renderUtils.drawString(drawContext, list.getIndexValue(), (parentX + (parentWidth / 2)) - length , parentY + offset + 4, 0xFFFFFF);
renderUtils.drawString(drawContext,"<<", parentX + 8, parentY + offset + 4, 0xFFFFFF);
renderUtils.drawString(drawContext,">>", parentX + 8 + (parentWidth - 34), parentY + offset + 4, 0xFFFFFF);
renderUtils.drawOutlinedBox(matrixStack, parentX + 4, parentY + offset, parentWidth - 8, 22,
new Color(25, 25, 25), 0.3f);
renderUtils.drawString(drawContext, list.getIndexValue(), (parentX + (parentWidth / 2)) - length,
parentY + offset + 4, 0xFFFFFF);
renderUtils.drawString(drawContext, "<<", parentX + 8, parentY + offset + 4, 0xFFFFFF);
renderUtils.drawString(drawContext, ">>", parentX + 8 + (parentWidth - 34), parentY + offset + 4, 0xFFFFFF);
}

@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
float parentX = parent.getX();
float parentY = parent.getY();
float parentWidth = parent.getWidth();

int mouseX = event.GetMouseX();
int mouseY = event.GetMouseY();

if (HudManager.currentGrabbed == null) {
if (!wasClicked) {
if (mouseY >= (((parentY + offset + 4))) && mouseY <= (parentY + offset + 22)) {
// If Left arrow clicked.
if (mouseX >= ((parentX + 4)) && mouseX <= ((parentX + 64))) {
if (!this.wasClicked) {
list.decrement();
this.wasClicked = true;
return;
}
}
// If Right arrow clicked.
if (mouseX >= ((parentX + parentWidth - 64)) && mouseX <= ((parentX + parentWidth - 4))) {
if (!this.wasClicked) {
list.increment();
this.wasClicked = true;
return;
}
}
if (mouseY >= (((parentY + offset + 4))) && mouseY <= (parentY + offset + 22)) {
// If Left arrow clicked.
if (mouseX >= ((parentX + 4)) && mouseX <= ((parentX + 64))) {
list.decrement();
return;
}
// If Right arrow clicked.
if (mouseX >= ((parentX + parentWidth - 64)) && mouseX <= ((parentX + parentWidth - 4))) {
list.increment();
return;
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public class ModuleComponent extends Component implements MouseLeftClickListener
private String text;
private Module module;
private ClickGuiTab parent;
private boolean wasClicked = false;
private boolean popped = false;


private int expandedHeight = 30;

Expand Down Expand Up @@ -112,6 +110,7 @@ public void RecalculateExpandedHeight() {
expandedHeight = height;
}


@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
float parentX = parent.getX();
Expand Down
Loading

0 comments on commit 93385f7

Please sign in to comment.