From 580fd1d5c4ec5625c813f6d593928a401a500869 Mon Sep 17 00:00:00 2001 From: nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:51:01 +0100 Subject: Buttons but with a key twist (#70) * config checker 9000 * apiDump * rename * put in package and remove javadocs * put in package and remove javadocs * button methods --- .../gui/elements/config/ConfigButton.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/main/java/cc/polyfrost/oneconfig/gui') diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java index 9879ac2..1480ff8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java @@ -9,6 +9,8 @@ import cc.polyfrost.oneconfig.renderer.font.Fonts; import cc.polyfrost.oneconfig.utils.color.ColorPalette; import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; public class ConfigButton extends BasicOption { private final BasicButton button; @@ -25,11 +27,31 @@ public class ConfigButton extends BasicOption { this.button.setClickAction(getRunnableFromField(field, parent)); } + public ConfigButton(Method method, Object parent, String name, String category, String subcategory, int size, String text) { + super(null, parent, name, category, subcategory, size); + this.button = new BasicButton(size == 1 ? 128 : 256, 32, text, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY); + this.button.setClickAction(() -> { + try { + method.invoke(parent); + } catch (Exception e) { + if (e.getMessage() != null && e.getMessage().contains("wrong number of arguments")) { + throw new IllegalArgumentException("Button method " + method.getDeclaringClass().getName() + "." + method.getName() + "(" + Arrays.toString(method.getGenericParameterTypes()) + ") must take no arguments!"); + } else e.printStackTrace(); + } + }); + } + public static ConfigButton create(Field field, Object parent) { Button button = field.getAnnotation(Button.class); return new ConfigButton(field, parent, button.name(), button.category(), button.subcategory(), button.size(), button.text()); } + public static ConfigButton create(Method method, Object parent) { + method.setAccessible(true); + Button button = method.getAnnotation(Button.class); + return new ConfigButton(method, parent, button.name(), button.category(), button.subcategory(), button.size(), button.text()); + } + private static Runnable getRunnableFromField(Field field, Object parent) { Runnable runnable = () -> { }; -- cgit