Skip to content

Commit

Permalink
feat: Animated navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
cvs0 committed Jul 7, 2024
1 parent 0c25431 commit 209406b
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions src/main/java/net/aoba/gui/NavigationBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public class NavigationBar implements MouseClickListener {

private List<Page> options;
private int selectedIndex;
private float currentSelectionX;
private float targetSelectionX;
private final float animationSpeed = 0.1f;

public NavigationBar() {
options = new ArrayList<Page>();
Aoba.getInstance().eventManager.AddListener(MouseClickListener.class, this);
}
public NavigationBar() {
options = new ArrayList<>();
Aoba.getInstance().eventManager.AddListener(MouseClickListener.class, this);
currentSelectionX = 0;
targetSelectionX = 0;
}

public void addPane(Page pane) {
options.add(pane);
Expand All @@ -62,42 +67,45 @@ public Page getSelectedPage() {
return options.get(selectedIndex);
}

public void setSelectedIndex(int index) {
if (index <= this.options.size()) {
this.options.get(selectedIndex).setVisible(false);
this.selectedIndex = index;
this.options.get(selectedIndex).setVisible(true);
}
}
public void setSelectedIndex(int index) {
if (index < this.options.size()) {
this.options.get(selectedIndex).setVisible(false);
this.selectedIndex = index;
this.options.get(selectedIndex).setVisible(true);
targetSelectionX = index * 100; // Update target position for animation
}
}

public void update() {
if (options.size() > 0) {
options.get(selectedIndex).update();
}
}

public void draw(DrawContext drawContext, float partialTicks) {
Window window = mc.getWindow();

int centerX = (window.getWidth() / 2);

MatrixStack matrixStack = drawContext.getMatrices();
Matrix4f matrix = matrixStack.peek().getPositionMatrix();
int width = 100 * options.size();

RenderUtils.drawRoundedBox(matrix, centerX - (width / 2), 25, width, 25, 6, GuiManager.backgroundColor.getValue());
RenderUtils.drawRoundedOutline(matrix, centerX - (width / 2), 25, width, 25, 6, GuiManager.borderColor.getValue());

RenderUtils.drawRoundedBox(matrix, centerX - (width / 2) + (100 * this.selectedIndex), 25, 100, 25, 5, new Color(150, 150, 150, 100));

for (int i = 0; i < options.size(); i++) {
Page pane = options.get(i);
if (i == selectedIndex) {
pane.render(drawContext, partialTicks);
}
RenderUtils.drawString(drawContext, pane.title, centerX - (width / 2) + 50 + (100 * i) - mc.textRenderer.getWidth(pane.title), 30, GuiManager.foregroundColor.getValue());
}
}
public void draw(DrawContext drawContext, float partialTicks) {
Window window = mc.getWindow();
int centerX = (window.getWidth() / 2);
MatrixStack matrixStack = drawContext.getMatrices();
Matrix4f matrix = matrixStack.peek().getPositionMatrix();
int width = 100 * options.size();

// Animate selection box movement
currentSelectionX += (targetSelectionX - currentSelectionX) * animationSpeed * partialTicks;

RenderUtils.drawRoundedBox(matrix, centerX - (width / 2), 25, width, 25, 6, GuiManager.backgroundColor.getValue());
RenderUtils.drawRoundedOutline(matrix, centerX - (width / 2), 25, width, 25, 6, GuiManager.borderColor.getValue());

// Use currentSelectionX for animated position
RenderUtils.drawRoundedBox(matrix, centerX - (width / 2) + currentSelectionX, 25, 100, 25, 5, new Color(150, 150, 150, 100));

for (int i = 0; i < options.size(); i++) {
Page pane = options.get(i);
if (i == selectedIndex) {
pane.render(drawContext, partialTicks);
}
RenderUtils.drawString(drawContext, pane.title, centerX - (width / 2) + 50 + (100 * i) - mc.textRenderer.getWidth(pane.title), 30, GuiManager.foregroundColor.getValue());
}
}

@Override
public void OnMouseClick(MouseClickEvent event) {
Expand Down

0 comments on commit 209406b

Please sign in to comment.