Skip to content

Commit

Permalink
expand input support for changing enum/string array options
Browse files Browse the repository at this point in the history
  • Loading branch information
moehreag committed Jan 17, 2025
1 parent 4ea6d46 commit e2ab1b0
Show file tree
Hide file tree
Showing 60 changed files with 1,743 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright © 2021-2025 moehreag <[email protected]> & Contributors
*
* This file is part of AxolotlClient.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* For more information, see the LICENSE file.
*/

@SuppressWarnings("unused")
package io.github.axolotlclient.AxolotlClientConfig.impl;
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ protected int getScrollbarPositionX() {
return super.getScrollbarPositionX() + 38;
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollY) {
return getHoveredEntry() != null && getHoveredEntry().mouseScrolled(mouseX, mouseY, scrollY) || super.mouseScrolled(mouseX, mouseY, scrollY);
}

protected static class Entry extends ElementListWidget.Entry<Entry> {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
package io.github.axolotlclient.AxolotlClientConfig.impl.ui.rounded.widgets;

import io.github.axolotlclient.AxolotlClientConfig.impl.options.EnumOption;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.TranslatableText;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.glfw.GLFW;

public class EnumWidget<T extends Enum<T>> extends RoundedButtonWidget {
private final EnumOption<T> option;
Expand All @@ -35,17 +39,65 @@ public EnumWidget(int x, int y, int width, int height, EnumOption<T> option) {
}

@Override
public void onPress() {
T[] values = option.getClazz().getEnumConstants();
int i = 0;
while (!values[i].equals(option.get())) {
i += 1;
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (this.active && this.visible) {
if (this.isValidClickButton(button)) {
boolean bl = this.isMouseOver(mouseX, mouseY);
if (bl) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
this.cycle(button == 0 ? 1 : -1, true);
return true;
}
}
}
i += 1;
if (i >= values.length) {
i = 0;
return false;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!this.active || !this.visible) {
return false;
} else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_SPACE || keyCode == GLFW.GLFW_KEY_KP_ENTER || keyCode == GLFW.GLFW_KEY_RIGHT) {
if (this.cycle(1, false)) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
return true;
}
} else if(keyCode == GLFW.GLFW_KEY_LEFT) {
if (this.cycle(-1, false)) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
return true;
}
}
option.set(values[i]);
return false;
}

@Override
protected boolean isValidClickButton(int button) {
return button == 0 || button == 1;
}

private boolean cycle(int step, boolean wrap) {
if (Screen.hasShiftDown()) {
step *= -1;
}
T[] values = option.getClazz().getEnumConstants();
int i = ArrayUtils.indexOf(values, option.get());
if (!wrap && (i == 0 || i == values.length-1)) {
return false;
}
option.set(values[Math.floorMod(i + step, values.length)]);
setMessage(new TranslatableText(String.valueOf(option.get())));
return true;
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollY) {
if (scrollY > 0.0) {
this.cycle(-1, true);
} else if (scrollY < 0.0) {
this.cycle(1, true);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected Color getWidgetColor() {
}

public boolean isHovered() {
return hovered;
return hovered || isFocused();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
package io.github.axolotlclient.AxolotlClientConfig.impl.ui.rounded.widgets;

import io.github.axolotlclient.AxolotlClientConfig.impl.options.StringArrayOption;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.TranslatableText;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.glfw.GLFW;

public class StringArrayButtonWidget extends RoundedButtonWidget {

Expand All @@ -46,17 +50,65 @@ public void renderButton(MatrixStack graphics, int mouseX, int mouseY, float del
}

@Override
public void onPress() {
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (this.active && this.visible) {
if (this.isValidClickButton(button)) {
boolean bl = this.isMouseOver(mouseX, mouseY);
if (bl) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
this.cycle(button == 0 ? 1 : -1, true);
return true;
}
}
}
return false;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!this.active || !this.visible) {
return false;
} else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_SPACE || keyCode == GLFW.GLFW_KEY_KP_ENTER || keyCode == GLFW.GLFW_KEY_RIGHT) {
if (this.cycle(1, false)) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
return true;
}
} else if (keyCode == GLFW.GLFW_KEY_LEFT) {
if (this.cycle(-1, false)) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
return true;
}
}
return false;
}

@Override
protected boolean isValidClickButton(int button) {
return button == 0 || button == 1;
}

private boolean cycle(int step, boolean wrap) {
if (Screen.hasShiftDown()) {
step *= -1;
}
String[] values = option.getValues();
int i = 0;
while (!values[i].equals(option.get())) {
i += 1;
int i = ArrayUtils.indexOf(values, option.get());
if (!wrap && (i == 0 || i == values.length-1)) {
return false;
}
i += 1;
if (i >= values.length) {
i = 0;
option.set(values[Math.floorMod(i + step, values.length)]);
setMessage(new TranslatableText(String.valueOf(option.get())));
return true;
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollY) {
if (scrollY > 0.0) {
this.cycle(-1, true);
} else if (scrollY < 0.0) {
this.cycle(1, true);
}
option.set(values[i]);
setMessage(new TranslatableText(option.get()));

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright © 2021-2024 moehreag <[email protected]> & Contributors
*
* This file is part of AxolotlClient.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* For more information, see the LICENSE file.
*/

package io.github.axolotlclient.AxolotlClientConfig.impl.ui.rounded.widgets;

import io.github.axolotlclient.AxolotlClientConfig.impl.ui.rounded.NVGHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ protected void renderList(MatrixStack matrices, int mouseX, int mouseY, float de
DrawUtil.popScissor();
}

protected static class Entry extends ElementListWidget.Entry<Entry> {

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollY) {
return getHoveredEntry() != null && getHoveredEntry().mouseScrolled(mouseX, mouseY, scrollY) || super.mouseScrolled(mouseX, mouseY, scrollY);
}

protected static class Entry extends ElementListWidget.Entry<Entry> {
private final List<AbstractButtonWidget> children = new ArrayList<>();

public Entry(Collection<AbstractButtonWidget> widgets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
package io.github.axolotlclient.AxolotlClientConfig.impl.ui.vanilla.widgets;

import io.github.axolotlclient.AxolotlClientConfig.impl.options.EnumOption;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.TranslatableText;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.glfw.GLFW;

public class EnumWidget<T extends Enum<T>> extends VanillaButtonWidget {
private final EnumOption<T> option;
Expand All @@ -35,17 +39,65 @@ public EnumWidget(int x, int y, int width, int height, EnumOption<T> option) {
}

@Override
public void onClick(double mouseX, double mouseY) {
T[] values = option.getClazz().getEnumConstants();
int i = 0;
while (!values[i].equals(option.get())) {
i += 1;
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (this.active && this.visible) {
if (this.isValidClickButton(button)) {
boolean bl = this.isMouseOver(mouseX, mouseY);
if (bl) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
this.cycle(button == 0 ? 1 : -1, true);
return true;
}
}
}
i += 1;
if (i >= values.length) {
i = 0;
return false;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!this.active || !this.visible) {
return false;
} else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_SPACE || keyCode == GLFW.GLFW_KEY_KP_ENTER || keyCode == GLFW.GLFW_KEY_RIGHT) {
if (this.cycle(1, false)) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
return true;
}
} else if(keyCode == GLFW.GLFW_KEY_LEFT) {
if (this.cycle(-1, false)) {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
return true;
}
}
option.set(values[i]);
return false;
}

@Override
protected boolean isValidClickButton(int button) {
return button == 0 || button == 1;
}

private boolean cycle(int step, boolean wrap) {
if (Screen.hasShiftDown()) {
step *= -1;
}
T[] values = option.getClazz().getEnumConstants();
int i = ArrayUtils.indexOf(values, option.get());
if (!wrap && (i == 0 || i == values.length-1)) {
return false;
}
option.set(values[Math.floorMod(i + step, values.length)]);
setMessage(new TranslatableText(String.valueOf(option.get())));
return true;
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollY) {
if (scrollY > 0.0) {
this.cycle(-1, true);
} else if (scrollY < 0.0) {
this.cycle(1, true);
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright © 2021-2024 moehreag <[email protected]> & Contributors
*
* This file is part of AxolotlClient.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* For more information, see the LICENSE file.
*/

package io.github.axolotlclient.AxolotlClientConfig.impl.ui.vanilla.widgets;

import net.minecraft.client.font.TextRenderer;
Expand Down
Loading

0 comments on commit e2ab1b0

Please sign in to comment.