11package tools .redstone .redstonetools .malilib ;
22
3- import com .google .common .collect .ImmutableList ;
4- import fi .dy .masa .malilib .config .IConfigBase ;
3+ import fi .dy .masa .malilib .config .IConfigBoolean ;
54import fi .dy .masa .malilib .config .options .ConfigBoolean ;
6- import fi .dy .masa .malilib .config .options .ConfigString ;
7- import fi .dy .masa .malilib .config .options .ConfigStringList ;
85import fi .dy .masa .malilib .gui .GuiBase ;
9- import fi .dy .masa .malilib .gui .GuiConfigsBase ;
10- import fi .dy .masa .malilib .gui .button .ButtonGeneric ;
6+ import fi .dy .masa .malilib .gui .button .ConfigButtonBoolean ;
7+ import fi .dy .masa .malilib .gui .button .ConfigButtonKeybind ;
8+ import net .minecraft .client .MinecraftClient ;
119import net .minecraft .client .gui .DrawContext ;
12- import tools .redstone .redstonetools .RedstoneTools ;
13- import tools .redstone .redstonetools .macros .actions .CommandAction ;
10+ import net .minecraft .client .gui .screen .Screen ;
11+ import net .minecraft .client .gui .widget .ButtonWidget ;
12+ import net .minecraft .client .gui .widget .TextFieldWidget ;
13+ import net .minecraft .text .Text ;
1414import tools .redstone .redstonetools .malilib .config .MacroManager ;
15- import tools .redstone .redstonetools .malilib .widget .MacroBase ;
16- import tools .redstone .redstonetools .malilib .widget .WidgetListMacros ;
15+ import tools .redstone .redstonetools .malilib .widget .action .CommandListWidget ;
16+ import tools .redstone .redstonetools .malilib .widget .macro .MacroBase ;
17+ import tools .redstone .redstonetools .utils .GuiUtils ;
1718
18- import java .util .List ;
19+ import java .lang .reflect .InvocationTargetException ;
20+ import java .lang .reflect .Method ;
1921
20- public class GuiMacroEditor extends GuiConfigsBase {
22+ public class GuiMacroEditor extends Screen {
2123 private final MacroBase macro ;
22- private final ConfigStringList commands ;
23- private final WidgetListMacros parent ;
24+ private final GuiMacroManager parent ;
25+ private CommandListWidget commandList ;
26+ private ConfigButtonKeybind buttonKeybind ;
27+ private ConfigButtonBoolean buttonEnabled ;
28+ private IConfigBoolean configBoolean ;
29+ private TextFieldWidget nameWidget ;
2430 private float errorCountDown ;
2531
26- public GuiMacroEditor (MacroBase macro , WidgetListMacros parent ) {
27- super (10 , 50 , RedstoneTools . MOD_ID , null , macro . getName (), "" );
32+ public GuiMacroEditor (Text title , MacroBase macro , GuiMacroManager parent ) {
33+ super (title );
2834 this .parent = parent ;
35+ this .client = MinecraftClient .getInstance ();
2936 this .macro = macro ;
30- this .title = macro .getName ();
31- this .commands = new ConfigStringList ("Commands" , ImmutableList .of (), "List of commands (prefixed with \" /\" ) or messages that should be sent in chat upon running the macro" );
32- this .configEnabled = new ConfigBoolean ("Enabled" , this .macro .isEnabled (), "Whether or not to enable the macro" );
33- this .configName = new ConfigString ("Name" , this .macro .getName (), "Name of the macro" );
34- this .commands .setStrings (macro .actionsAsStringList );
3537 }
3638
37- @ Override
38- public void initGui () {
39- super .initGui ();
39+ private static Method bkRenderMethod ;
40+ private static Method beRenderMethod ;
4041
41- int x = 10 ;
4242
43- ButtonGeneric button = new ButtonGeneric (x , this .height - 24 , -1 , 20 , GuiConfigs .ConfigGuiTab .MACROS .getDisplayName ());
44- this .addButton (button , (a , b ) -> updateConfigsAndClose ());
43+ @ Override
44+ public void render (DrawContext context , int mouseX , int mouseY , float deltaTicks ) {
45+ super .render (context , mouseX , mouseY , deltaTicks );
46+ buttonKeybind .updateDisplayString ();
47+ try {
48+ buttonKeybind .render (context , mouseX , mouseY , buttonKeybind .isMouseOver (mouseX , mouseY ));
49+ buttonEnabled .render (context , mouseX , mouseY , buttonEnabled .isMouseOver (mouseX , mouseY ));
50+ } catch (NoSuchMethodError ignored ) {
51+ if (bkRenderMethod == null ) {
52+ try {
53+ bkRenderMethod = ConfigButtonKeybind .class .getMethod ("render" , int .class , int .class , boolean .class , DrawContext .class );
54+ beRenderMethod = ConfigButtonBoolean .class .getMethod ("render" , int .class , int .class , boolean .class , DrawContext .class );
55+ } catch (Exception e ) {
56+ throw new RuntimeException ("Something went wrong. Contact a redstonetools developer" , e );
57+ }
58+ }
59+ try {
60+ bkRenderMethod .invoke (buttonKeybind , mouseX , mouseY , buttonKeybind .isMouseOver (mouseX , mouseY ), context );
61+ beRenderMethod .invoke (buttonKeybind , mouseX , mouseY , buttonKeybind .isMouseOver (mouseX , mouseY ), context );
62+ } catch (IllegalAccessException | InvocationTargetException e ) {
63+ throw new RuntimeException ("Something went wrong. Contact a redstonetools developer" , e );
64+ }
65+ }
66+ if (errorCountDown > 0.0f ) {
67+ context .drawText (this .textRenderer , "Name already exists!" , mouseX , mouseY - 10 , 0xFFFFFFFF , true );
68+ errorCountDown -= deltaTicks ;
69+ }
4570 }
4671
4772 @ Override
48- public void render (DrawContext drawContext , int mouseX , int mouseY , float partialTicks ) {
49- super .render (drawContext , mouseX , mouseY , partialTicks );
50- if (errorCountDown > 0.0f ) {
51- drawContext .drawText (this .textRenderer , "Name already exists!" , mouseX , mouseY - 10 , 0xFFFFFFFF , true );
52- errorCountDown -= partialTicks ;
73+ public boolean keyPressed (int keyCode , int scanCode , int modifiers ) {
74+ buttonEnabled .onKeyTyped (keyCode , scanCode , modifiers );
75+ buttonKeybind .onKeyPressed (keyCode );
76+ if (buttonKeybind .isSelected () && keyCode == 256 ) {
77+ this .macro .hotkey .getKeybind ().clearKeys ();
78+ buttonKeybind .onClearSelection ();
79+ return true ;
5380 }
81+ if (this .commandList .keyPressed (keyCode , scanCode , modifiers ))
82+ return true ;
83+ else
84+ return super .keyPressed (keyCode , scanCode , modifiers );
5485 }
5586
5687 @ Override
57- public void closeGui (boolean showParent ) {
58- if (updateConfigsAndClose ()) return ;
59- super .closeGui (showParent );
88+ protected void init () {
89+ GuiUtils .Layout ncLayout = GuiUtils .getWidgetLayout (4 , 10 , this .width , 0 , true , 50 , this .height - 52 , 20 );
90+ GuiUtils .Layout bkLayout = GuiUtils .getWidgetLayout (4 , 10 , this .width , 1 , true , 50 , this .height - 52 , 20 );
91+ GuiUtils .Layout beLayout = GuiUtils .getWidgetLayout (4 , 10 , this .width , 2 , true , 50 , this .height - 52 , 20 );
92+ GuiUtils .Layout nwLayout = GuiUtils .getWidgetLayout (4 , 10 , this .width , 3 , true , 50 , this .height - 52 , 20 );
93+ this .commandList = this .addDrawableChild (
94+ new CommandListWidget (this , this .client , this .width , this .height - 75 , 0 , 36 , this .macro )
95+ );
96+ this .addDrawableChild (ButtonWidget .builder (Text .of ("Add command" ), button ->
97+ this .commandList .addEntry ())
98+ .dimensions (ncLayout .x , ncLayout .y , ncLayout .width , ncLayout .height )
99+ .build ());
100+ this .buttonKeybind = new ConfigButtonKeybind (bkLayout .x , bkLayout .y , bkLayout .width , bkLayout .height , macro .hotkey .getKeybind (), null ) {
101+ @ Override
102+ public boolean onMouseClicked (int mx , int my , int mb ) {
103+ if (!this .isMouseOver (mx , my )) {
104+ this .selected = false ;
105+ return false ;
106+ } else {
107+ return super .onMouseClicked (mx , my , mb );
108+ }
109+ }
110+ @ Override
111+ public void onClearSelection () {
112+ this .firstKey = true ;
113+ super .onClearSelection ();
114+ }
115+ };
116+ this .configBoolean = new ConfigBoolean ("" , true , "" );
117+ this .configBoolean .setBooleanValue (this .macro .isEnabled ());
118+ this .buttonEnabled = new ConfigButtonBoolean (beLayout .x , beLayout .y , beLayout .width , beLayout .height , this .configBoolean );
119+ this .nameWidget = addDrawableChild (new TextFieldWidget (this .textRenderer , nwLayout .width , nwLayout .height , Text .of ("" )));
120+ this .nameWidget .setText (macro .getName ());
121+ this .nameWidget .setPosition (nwLayout .x , nwLayout .y );
60122 }
61123
62- private boolean updateConfigsAndClose () {
63- if (MacroManager .nameExists (this .configName .getStringValue (), this .macro )) {
64- errorCountDown = 50.0f ;
124+ @ Override
125+ public void mouseMoved (double mouseX , double mouseY ) {
126+ commandList .mouseMoved (mouseX , mouseY );
127+ }
128+
129+ @ Override
130+ public boolean mouseClicked (double mouseX , double mouseY , int button ) {
131+ if (buttonKeybind .onMouseClicked ((int ) mouseX , (int ) mouseY , button )) {
132+ if (this .getFocused () != null ) {
133+ this .getFocused ().setFocused (false );
134+ }
65135 return true ;
66136 }
67- this . macro . setName ( this . configName . getStringValue ());
68- this . macro . setEnabled (this .configEnabled . getBooleanValue ());
69- this .macro . actions . clear ( );
70- for ( String s : this . commands . getStrings ()) {
71- this . macro . actions . add ( new CommandAction ( s )) ;
137+ else if ( buttonEnabled . onMouseClicked (( int ) mouseX , ( int ) mouseY , button )) {
138+ if (this .getFocused () != null ) {
139+ this .getFocused (). setFocused ( false );
140+ }
141+ return true ;
72142 }
73- MacroManager .saveChanges ();
74- this .parent .refreshEntries ();
75- GuiBase .openGui (new GuiMacroManager ());
76- return false ;
143+ else if (super .mouseClicked (mouseX , mouseY , button )) return true ;
144+ else return commandList .mouseClicked (mouseX , mouseY , button );
77145 }
78146
79- private final ConfigBoolean configEnabled ;
80- private final ConfigString configName ;
147+ @ Override
148+ public boolean mouseReleased (double mouseX , double mouseY , int button ) {
149+ buttonKeybind .onMouseReleased ((int ) mouseX , (int ) mouseY , button );
150+ buttonEnabled .onMouseReleased ((int ) mouseX , (int ) mouseY , button );
151+ if (commandList .mouseReleased (mouseX , mouseY , button )) return true ;
152+ else return super .mouseReleased (mouseX , mouseY , button );
153+ }
81154
82155 @ Override
83- public List <ConfigOptionWrapper > getConfigs () {
84- List <? extends IConfigBase > configs = List .of (
85- this .configEnabled ,
86- this .macro .hotkey ,
87- this .commands ,
88- this .configName
89- );
90- return ConfigOptionWrapper .createFor (configs );
156+ public boolean mouseDragged (double mouseX , double mouseY , int button , double deltaX , double deltaY ) {
157+ if (commandList .mouseDragged (mouseX , mouseY , button , deltaX , deltaY )) return true ;
158+ else return super .mouseDragged (mouseX , mouseY , button , deltaX , deltaY );
159+ }
160+
161+ @ Override
162+ public boolean mouseScrolled (double mouseX , double mouseY , double horizontalAmount , double verticalAmount ) {
163+ if (commandList .mouseScrolled (mouseX , mouseY , horizontalAmount , verticalAmount )) return true ;
164+ else if (buttonKeybind .onMouseScrolled ((int ) mouseX , (int ) mouseY , horizontalAmount , verticalAmount ))
165+ return true ;
166+ else if (buttonEnabled .onMouseScrolled ((int ) mouseX , (int ) mouseY , horizontalAmount , verticalAmount ))
167+ return true ;
168+ else return super .mouseScrolled (mouseX , mouseY , horizontalAmount , verticalAmount );
169+ }
170+
171+ @ Override
172+ public boolean keyReleased (int keyCode , int scanCode , int modifiers ) {
173+ if (commandList .keyReleased (keyCode , scanCode , modifiers )) return true ;
174+ else return super .keyReleased (keyCode , scanCode , modifiers );
175+ }
176+
177+ @ Override
178+ public boolean charTyped (char chr , int modifiers ) {
179+ if (commandList .charTyped (chr , modifiers )) return true ;
180+ else if (buttonKeybind .onCharTyped (chr , modifiers )) return true ;
181+ else if (buttonEnabled .onCharTyped (chr , modifiers )) return true ;
182+ else return super .charTyped (chr , modifiers );
183+ }
184+
185+ @ Override
186+ public boolean isMouseOver (double mouseX , double mouseY ) {
187+ if (commandList .isMouseOver (mouseX , mouseY )) return true ;
188+ else if (buttonKeybind .isMouseOver ((int ) mouseX , (int ) mouseY )) return true ;
189+ else if (buttonEnabled .isMouseOver ((int ) mouseX , (int ) mouseY )) return true ;
190+ else return super .isMouseOver (mouseX , mouseY );
191+ }
192+
193+ @ Override
194+ public void close () {
195+ if (MacroManager .nameExists (this .nameWidget .getText (), this .macro )) {
196+ errorCountDown = 50.0f ;
197+ return ;
198+ }
199+ this .macro .actions .clear ();
200+ this .commandList .children ().forEach (t -> this .macro .actions .add (t .command ));
201+ this .macro .setEnabled (this .configBoolean .getBooleanValue ());
202+ this .macro .setName (this .nameWidget .getText ());
203+ MacroManager .saveChanges ();
204+ assert client != null ;
205+ parent .initGui ();
206+ GuiBase .openGui (parent );
91207 }
92- }
208+ }
0 commit comments