11package io .github .techstreet .dfscript .screen .script ;
22
33import io .github .techstreet .dfscript .DFScript ;
4+ import io .github .techstreet .dfscript .screen .CReloadableScreen ;
45import io .github .techstreet .dfscript .screen .CScreen ;
6+ import io .github .techstreet .dfscript .screen .ContextMenuButton ;
57import io .github .techstreet .dfscript .screen .widget .*;
68import io .github .techstreet .dfscript .script .Script ;
79import io .github .techstreet .dfscript .script .ScriptParametrizedPart ;
8- import io .github .techstreet .dfscript .script .ScriptPart ;
910import io .github .techstreet .dfscript .script .argument .*;
1011
1112import java .awt .Rectangle ;
1213import java .math .RoundingMode ;
1314import java .text .DecimalFormat ;
1415import java .util .ArrayList ;
1516import java .util .List ;
17+
18+ import net .minecraft .client .font .TextRenderer ;
1619import net .minecraft .client .gui .DrawableHelper ;
1720import net .minecraft .client .sound .PositionedSoundInstance ;
1821import net .minecraft .client .util .math .MatrixStack ;
2124import net .minecraft .sound .SoundEvents ;
2225import net .minecraft .text .Text ;
2326
24- public class ScriptEditPartScreen extends CScreen {
27+ public class ScriptEditPartScreen extends CReloadableScreen {
2528
2629 private final Script script ;
30+ private final ScriptParametrizedPart action ;
2731 private final CScrollPanel panel ;
2832 private final List <CWidget > contextMenu = new ArrayList <>();
2933
@@ -34,39 +38,66 @@ public ScriptEditPartScreen(ScriptParametrizedPart action, Script script) {
3438 widgets .add (panel );
3539
3640 this .script = script ;
41+ this .action = action ;
42+
43+ reload ();
44+ }
45+
46+ @ Override
47+ public void close () {
48+ DFScript .MC .setScreen (new ScriptEditScreen (script ));
49+ }
50+
51+ @ Override
52+ public boolean mouseClicked (double mouseX , double mouseY , int button ) {
53+ boolean b = super .mouseClicked (mouseX , mouseY , button );
54+ clearContextMenu ();
55+ return b ;
56+ }
57+
58+ private void clearContextMenu () {
59+ for (CWidget w : contextMenu ) {
60+ panel .remove (w );
61+ }
62+ contextMenu .clear ();
63+ }
64+
65+ public void contextMenu (int x , int y , List <ContextMenuButton > contextMenuButtons ) {
66+ clearContextMenu ();
67+
68+ int maxWidth = 0 ;
69+
70+ for (ContextMenuButton w : contextMenuButtons )
71+ {
72+ TextRenderer t = DFScript .MC .textRenderer ;
73+ int width = t .getWidth (w .getName ())/2 + 4 ;
74+
75+ if (width > maxWidth ) maxWidth = width ;
76+ }
77+
78+ for (ContextMenuButton w : contextMenuButtons )
79+ {
80+ CButton button = new CButton (x , y , maxWidth , 8 , w .getName (), w .getOnClick ());
81+ y += 8 ;
82+
83+ panel .add (button );
84+ contextMenu .add (button );
85+ }
86+ }
87+
88+ @ Override
89+ public void reload () {
90+ clearContextMenu ();
91+ panel .clear ();
3792
3893 panel .add (new CItem (5 , 3 , action .getIcon ()));
3994 panel .add (new CText (15 , 5 , Text .of (action .getName ())));
4095
4196 int y = 15 ;
4297 int index = 0 ;
4398 for (ScriptArgument arg : action .getArguments ()) {
44- ItemStack icon ;
45- String text ;
46- if (arg instanceof ScriptTextArgument ta ) {
47- icon = new ItemStack (Items .BOOK );
48- text = ta .value ();
49- } else if (arg instanceof ScriptNumberArgument na ) {
50- icon = new ItemStack (Items .SLIME_BALL );
51- if (na .value () % 1 == 0 ) {
52- DecimalFormat df = new DecimalFormat ("#" );
53- df .setRoundingMode (RoundingMode .UNNECESSARY );
54- text = df .format (na .value ());
55- } else {
56- text = String .valueOf (na .value ());
57- }
58- } else if (arg instanceof ScriptVariableArgument va ) {
59- icon = new ItemStack (Items .MAGMA_CREAM );
60- text = va .name ();
61- } else if (arg instanceof ScriptClientValueArgument cva ) {
62- icon = new ItemStack (Items .NAME_TAG );
63- text = cva .getName ();
64- } else if (arg instanceof ScriptConfigArgument ca ) {
65- icon = new ItemStack (Items .INK_SAC );
66- text = ca .getOption ().getFullName ();
67- } else {
68- throw new IllegalArgumentException ("Invalid argument type" );
69- }
99+ ItemStack icon = arg .getArgIcon ();
100+ String text = arg .getArgText ();
70101
71102 panel .add (new CItem (5 , y , icon ));
72103 panel .add (new CText (15 , y + 2 , Text .literal (text )));
@@ -100,23 +131,21 @@ public boolean mouseClicked(double x, double y, int button) {
100131 }
101132
102133 if (button != 0 ) {
103- CButton insertBefore = new CButton ((int ) x , (int ) y , 40 , 8 , "Insert Before" , () -> {
134+ List <ContextMenuButton > contextMenuButtons = new ArrayList <>();
135+ contextMenuButtons .add (new ContextMenuButton ("Insert Before" , () -> {
104136 DFScript .MC .setScreen (new ScriptAddArgumentScreen (script , action , currentIndex ));
105- });
106- CButton insertAfter = new CButton (( int ) x , ( int ) y + 8 , 40 , 8 , "Insert After" , () -> {
137+ }, false ) );
138+ contextMenuButtons . add ( new ContextMenuButton ( "Insert After" , () -> {
107139 DFScript .MC .setScreen (new ScriptAddArgumentScreen (script , action , currentIndex +1 ));
108- });
109- CButton delete = new CButton (( int ) x , ( int ) y + 16 , 40 , 8 , "Delete" , () -> {
140+ }, false ) );
141+ contextMenuButtons . add ( new ContextMenuButton ( "Delete" , () -> {
110142 action .getArguments ().remove (currentIndex );
111- DFScript . MC . setScreen ( new ScriptEditPartScreen ( action , script ));
112- } );
143+ } ));
144+ contextMenuButtons . addAll ( action . getArguments (). get ( currentIndex ). getContextMenu () );
113145 DFScript .MC .send (() -> {
114- panel .add (insertBefore );
115- panel .add (insertAfter );
116- panel .add (delete );
117- contextMenu .add (insertBefore );
118- contextMenu .add (insertAfter );
119- contextMenu .add (delete );
146+ if (DFScript .MC .currentScreen instanceof ScriptEditPartScreen screen ) {
147+ screen .contextMenu ((int ) x , (int ) y , contextMenuButtons );
148+ }
120149 });
121150 }
122151 return true ;
@@ -135,23 +164,4 @@ public boolean mouseClicked(double x, double y, int button) {
135164 });
136165 panel .add (add );
137166 }
138-
139- @ Override
140- public void close () {
141- DFScript .MC .setScreen (new ScriptEditScreen (script ));
142- }
143-
144- @ Override
145- public boolean mouseClicked (double mouseX , double mouseY , int button ) {
146- boolean b = super .mouseClicked (mouseX , mouseY , button );
147- clearContextMenu ();
148- return b ;
149- }
150-
151- private void clearContextMenu () {
152- for (CWidget w : contextMenu ) {
153- panel .remove (w );
154- }
155- contextMenu .clear ();
156- }
157167}
0 commit comments