2929import com .fox2code .foxloader .loader .ModContainer ;
3030import com .fox2code .foxloader .loader .ModInfo ;
3131import com .fox2code .foxloader .loader .ModLoader ;
32+ import com .fox2code .foxloader .loader .ModLoaderInit ;
3233import com .fox2code .foxloader .updater .UpdateManager ;
34+ import com .fox2code .foxloader .utils .io .URLUtils ;
3335import net .minecraft .client .Minecraft ;
34- import net .minecraft .client .gui .FontRenderer ;
35- import net .minecraft .client .gui .GuiButton ;
36- import net .minecraft .client .gui .GuiScreen ;
37- import net .minecraft .client .gui .GuiSmallButton ;
36+ import net .minecraft .client .gui .*;
37+ import net .minecraft .common .util .Utils ;
3838import net .minecraft .common .util .i18n .StringTranslate ;
39- import org .lwjgl .Sys ;
39+
40+ import java .util .Arrays ;
4041
4142public class GuiModMenu extends GuiScreen {
43+ private static final int BUTTON_MARGIN = 4 ;
4244 private GuiModMenuContainer modListContainer ;
4345 GuiModMenuDescription modListDescription ;
44- private GuiSmallButton guiUpdateAll , guiConfigureMod ;
45- private Object guiScreen ;
46+ private GuiSmallButton guiUpdateAll ;
47+ private final GuiButton [] modActionButtons = new GuiButton [4 ];
48+ private final ModActionButtonType [] modActionButtonsTypes = new ModActionButtonType [4 ];
4649 private int selectedBackup = 0 ;
50+ private String screenTitle ;
4751
4852 public GuiModMenu (GuiScreen parent ) {
4953 this .parentScreen = parent ;
54+ this .screenTitle = "Mod Menu" ;
5055 }
5156
5257 @ Override
@@ -57,42 +62,54 @@ public void initGui() {
5762 this .modListDescription = new GuiModMenuDescription (this );
5863 this .controlList .add (this .modListDescription );
5964 StringTranslate st = StringTranslate .getInstance ();
65+ this .screenTitle = st .translateKeyFormat (
66+ "mods.title" , ModLoaderInit .getModContainers ().size ());
67+ int widthOneSixth = Math .min (this .width / 6 , 50 + (BUTTON_MARGIN * 6 ));
68+ int midWidth = this .width / 2 ;
69+ int buttonWidth = Math .max (Math .min (150 , (widthOneSixth - BUTTON_MARGIN ) * 2 ), 50 );
6070 this .controlList .add (new GuiSmallButton (0 ,
61- this .width / 2 - 154 , this .height - 48 ,
71+ midWidth - (widthOneSixth * 3 ) + BUTTON_MARGIN ,
72+ this .height - 24 , buttonWidth , 20 ,
6273 st .translateKey ("mods.openFolder" )));
6374 this .controlList .add (new GuiSmallButton (1 ,
64- this .width / 2 + 4 , this .height - 48 ,
75+ midWidth - widthOneSixth + BUTTON_MARGIN ,
76+ this .height - 24 , buttonWidth , 20 ,
6577 st .translateKey ("gui.done" )));
6678 this .controlList .add (this .guiUpdateAll = new GuiSmallButton (2 ,
67- this .width / 2 - 154 , this .height - 24 ,
79+ midWidth + widthOneSixth + BUTTON_MARGIN ,
80+ this .height - 24 , buttonWidth , 20 ,
6881 st .translateKey ("mods.updateAllMods" )));
69- this .controlList .add (this .guiConfigureMod = new GuiSmallButton (3 ,
70- this .width / 2 + 4 , this .height - 24 ,
71- st .translateKey ("mods.configureMod" )));
82+ // Mod Actions buttons
83+ for (int i = 0 ; i < this .modActionButtons .length ; i ++) {
84+ GuiSmallButton guiSmallButton = new GuiSmallButton (4 + i , 240 , 32 , "#" + i );
85+ this .modActionButtons [i ] = guiSmallButton ;
86+ this .controlList .add (guiSmallButton );
87+ guiSmallButton .visible = false ;
88+ }
89+ Arrays .fill (this .modActionButtonsTypes , null );
7290 // Update GUI state after init
7391 this .modListContainer .elementClicked (this .selectedBackup , false );
7492 }
7593
7694 @ Override
7795 public void drawScreen (float var1 , float var2 , float deltaTicks ) {
7896 this .drawDefaultBackground ();
97+ this .drawCenteredString (this .fontRenderer , this .screenTitle , (int ) (this .width / 2 ), 13 , 0xffffff );
7998 super .drawScreen (var1 , var2 , deltaTicks );
8099 }
81100
82101 @ Override
83102 protected void actionPerformed (GuiButton var1 ) {
84103 if (var1 .id == 0 ) {
85- Sys .openURL ("file://" + ModLoader .getModsFolder ().getPath ());
104+ Utils .openURL ("file://" + ModLoader .getModsFolder ().getPath ());
86105 } else if (var1 .id == 1 ) {
87106 this .mc .displayGuiScreen (this .parentScreen );
88107 } else if (var1 .id == 2 ) {
89108 UpdateManager .getInstance ().doUpdates ();
90- } else if (var1 .id == 3 ) {
91- if (this .guiScreen instanceof GuiConfigProvider ) {
92- Minecraft .getInstance ().displayGuiScreen (
93- ((GuiConfigProvider ) this .guiScreen ).provideConfigScreen (this ));
94- } else {
95- this .openModConfigScreen (this .modListContainer .getSelectedModContainer ());
109+ } else if (var1 .id >= 4 && var1 .id < 8 ) {
110+ ModActionButtonType type = this .modActionButtonsTypes [var1 .id - 4 ];
111+ if (type != null ) {
112+ type .doAction (this .modListContainer .getSelectedModContainer (), this );
96113 }
97114 } else {
98115 this .modListContainer .actionPerformed (var1 );
@@ -110,20 +127,10 @@ public FontRenderer getFontRenderer() {
110127
111128 void updateGuiState () {
112129 this .selectedBackup = this .modListContainer .getSelectedIndex ();
113- final StringTranslate st = StringTranslate .getInstance ();
114130 final ModContainer modContainer = this .modListContainer .getSelectedModContainer ();
115131 this .guiUpdateAll .enabled = UpdateManager .getInstance ().canUpdate ();
116- this .guiScreen = null ;
117- this .guiConfigureMod .displayString = st .translateKey ("mods.configureMod" );
118- Object configObject = modContainer .getConfigObject ();
119- if (configObject instanceof GuiConfigProvider ) {
120- this .guiScreen = modContainer .getConfigObject ();
121- this .guiConfigureMod .enabled = true ;
122- } else if (configObject == null ) {
123- this .guiConfigureMod .enabled = false ;
124- } else {
125- this .guiConfigureMod .enabled = !(configObject instanceof NoConfigObject );
126- }
132+ // Update mod action buttons
133+ this .updateActionButtons ();
127134 // Update mod description
128135 ModInfo modInfo = modContainer .getModInfo ();
129136 String fileName = modInfo .fileName ;
@@ -138,4 +145,123 @@ void updateGuiState() {
138145 this .modListDescription .addText ("Authors: " + modInfo .authors );
139146 this .modListDescription .addText ("Description: " + modInfo .description );
140147 }
148+
149+ void updateActionButtons () {
150+ final ModContainer modContainer = this .modListContainer .getSelectedModContainer ();
151+ this .resetModButtonAction (0 );
152+ Object configObject = modContainer .getConfigObject ();
153+ ModInfo modInfo = modContainer .getModInfo ();
154+ if (UpdateManager .getInstance ().canUpdate (modContainer .getModId ())) {
155+ this .addModButtonAction (ModActionButtonType .UPDATE );
156+ }
157+ if (configObject instanceof GuiConfigProvider ||
158+ (configObject != null && !(configObject instanceof NoConfigObject ))) {
159+ this .addModButtonAction (ModActionButtonType .CONFIGURE );
160+ }
161+ if (URLUtils .isValidHttpURL (modInfo .website )) {
162+ this .addModButtonAction (ModActionButtonType .WEBSITE );
163+ }
164+ int buttonCount = this .countButtonActions ();
165+ if (buttonCount == 0 ) {
166+ this .addModButtonAction (ModActionButtonType .CONFIGURE );
167+ this .modActionButtons [0 ].enabled = false ;
168+ buttonCount = 1 ;
169+ }
170+ if (buttonCount == 1 && this .modActionButtonsTypes [0 ] == ModActionButtonType .CONFIGURE ) {
171+ this .modActionButtons [0 ].displayString =
172+ StringTranslate .getInstance ().translateKey ("mods.configureMod" );
173+ }
174+ int leftMost = 240 - BUTTON_MARGIN ;
175+ int rightMost = this .width + BUTTON_MARGIN - 10 ;
176+ int mostPossibleButtons = (rightMost - leftMost ) / (50 + (BUTTON_MARGIN * 2 ));
177+ if (mostPossibleButtons <= 0 ) {
178+ this .resetModButtonAction (0 );
179+ return ;
180+ } else if (mostPossibleButtons < buttonCount ) {
181+ this .resetModButtonAction (mostPossibleButtons );
182+ buttonCount = mostPossibleButtons ;
183+ }
184+ int totalWidth = rightMost - leftMost ;
185+ int buttonSpace = totalWidth / buttonCount ;
186+ int leakingWidth = totalWidth % buttonCount ;
187+ int buttonWidth = buttonSpace - (BUTTON_MARGIN * 2 );
188+ for (int i = 0 ; i < buttonCount ; i ++) {
189+ GuiButton guiButton = this .modActionButtons [i ];
190+ guiButton .width = buttonWidth ;
191+ guiButton .xPosition = leftMost + BUTTON_MARGIN + (buttonSpace * i );
192+ if (i != 0 ) {
193+ guiButton .xPosition += ((leakingWidth * i ) / (buttonCount - 1 ));
194+ }
195+ }
196+ }
197+
198+ private void resetModButtonAction (int fromId ) {
199+ for (int i = fromId ; i < this .modActionButtons .length ; i ++) {
200+ this .modActionButtonsTypes [i ] = null ;
201+ GuiButton guiButton = this .modActionButtons [i ];
202+ if (guiButton != null ) {
203+ guiButton .visible = false ;
204+ guiButton .enabled = true ;
205+ }
206+ }
207+ }
208+
209+ private void addModButtonAction (ModActionButtonType modActionButtonType ) {
210+ for (int i = 0 ; i < this .modActionButtons .length ; i ++) {
211+ if (this .modActionButtonsTypes [i ] == null ) {
212+ this .modActionButtonsTypes [i ] = modActionButtonType ;
213+ GuiButton guiButton = this .modActionButtons [i ];
214+ guiButton .displayString = StringTranslate .getInstance ()
215+ .translateKey (modActionButtonType .titleTranslate );
216+ guiButton .visible = true ;
217+ break ;
218+ }
219+ }
220+ }
221+
222+ private int countButtonActions () {
223+ for (int i = 0 ; i < this .modActionButtons .length ; i ++) {
224+ if (this .modActionButtonsTypes [i ] == null ) {
225+ return i ;
226+ }
227+ }
228+ return this .modActionButtons .length ;
229+ }
230+
231+ private enum ModActionButtonType {
232+ UPDATE ("mods.update" ) {
233+ @ Override
234+ void doAction (ModContainer modContainer , GuiModMenu guiModMenu ) {
235+ UpdateManager .getInstance ().doUpdate (modContainer .getModId ());
236+ guiModMenu .updateActionButtons ();
237+ }
238+ }, CONFIGURE ("mods.configure" ) {
239+ @ Override
240+ void doAction (ModContainer modContainer , GuiModMenu guiModMenu ) {
241+ Object configObject = modContainer .getConfigObject ();
242+ if (configObject instanceof GuiConfigProvider ) {
243+ Minecraft .getInstance ().displayGuiScreen (
244+ ((GuiConfigProvider ) configObject ).provideConfigScreen (guiModMenu ));
245+ } else if (!(configObject instanceof NoConfigObject )) {
246+ guiModMenu .openModConfigScreen (modContainer );
247+ }
248+ }
249+ }, WEBSITE ("mods.website" ) {
250+ @ Override
251+ void doAction (ModContainer modContainer , GuiModMenu guiModMenu ) {
252+ String website = modContainer .getModInfo ().website ;
253+ if (URLUtils .isValidHttpURL (website )) {
254+ Utils .openURL (website );
255+ }
256+ }
257+ };
258+
259+ final String titleTranslate ;
260+
261+ ModActionButtonType (String titleTranslate ) {
262+ this .titleTranslate = titleTranslate ;
263+ }
264+
265+ abstract void doAction (ModContainer modContainer , GuiModMenu guiModMenu );
266+ }
141267}
0 commit comments