-
Notifications
You must be signed in to change notification settings - Fork 16
/
ClickActionScreen.java
300 lines (263 loc) · 12.4 KB
/
ClickActionScreen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package dmillerw.menu.gui.menu;
import dmillerw.menu.data.click.*;
import dmillerw.menu.data.session.EditSessionData;
import dmillerw.menu.gui.ScreenStack;
import dmillerw.menu.gui.menu.button.ItemButton;
import dmillerw.menu.helper.GuiRenderHelper;
import net.minecraft.block.Blocks;
import net.minecraft.client.gui.IGuiEventListener;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.fml.client.gui.widget.ExtendedButton;
import org.lwjgl.glfw.GLFW;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
public class ClickActionScreen extends Screen {
@Nonnull
public static ItemStack item;
public static KeyBinding keyBinding;
private static boolean toggle = false;
private static boolean clipboard = false;
private TextFieldWidget textCommand;
private TextFieldWidget textCategory;
private ExtendedButton modeCommand;
private ExtendedButton modeKeybinding;
private ExtendedButton modeUseItem;
private ExtendedButton modeCategory;
private Button commandClipboardButton;
private Button keybindButton;
private Button keybindToggleButton;
private Button selectItemButton;
private Button buttonCancel;
private Button buttonConfirm;
private int mode = 0;
public ClickActionScreen() {
super(new TranslationTextComponent("minemenu.actionScreen.title"));
ClickActionScreen.keyBinding = null;
ClickActionScreen.item = ItemStack.EMPTY;
}
@Override
public void tick() {
this.textCommand.tick();
this.textCategory.tick();
}
@Override
@Nullable
public IGuiEventListener getFocused() {
if (mode == 0) {
return this.textCommand;
} else if (mode == 3) {
return this.textCategory;
} else {
return super.getFocused();
}
}
@Override
public void init() {
if (ClickActionScreen.keyBinding != null) {
mode = ClickAction.KEYBIND.ordinal();
} else if (!ClickActionScreen.item.isEmpty()) {
mode = ClickAction.ITEM_USE.ordinal();
} else {
mode = EditSessionData.clickAction != null ? EditSessionData.clickAction.getClickAction().ordinal() : 0;
}
this.getMinecraft().keyboardListener.enableRepeatEvents(true);
addButton(this.buttonConfirm = new Button(this.width / 2 - 4 - 150, this.height - 60, 150, 20, I18n.format("gui.done"), (screen) -> {
if (mode == 0) {
EditSessionData.clickAction = !textCommand.getText().trim().isEmpty() ? new ClickActionCommand(textCommand.getText().trim(), clipboard) : null;
} else if (mode == 1 && ClickActionScreen.keyBinding != null) {
EditSessionData.clickAction = new ClickActionKey(keyBinding.getKeyDescription(), toggle);
} else if (mode == 2 && !ClickActionScreen.item.isEmpty()) {
EditSessionData.clickAction = new ClickActionUseItem(item);
} else if (mode == 3) {
EditSessionData.clickAction = !(textCategory.getText().trim().isEmpty()) ? new ClickActionCategory(textCategory.getText().trim()) : null;
}
ScreenStack.pop();
}));
addButton(this.buttonCancel = new Button(this.width / 2 + 4, this.height - 60, 150, 20, I18n.format("gui.cancel"), (screen) -> ScreenStack.pop()));
String commandString;
if (EditSessionData.clickAction instanceof ClickActionCommand) {
commandString = ((ClickActionCommand) EditSessionData.clickAction).clipboard ? "Clipboard" : "Send";
} else {
commandString = clipboard ? "Clipboard" : "Send";
}
addButton(this.commandClipboardButton = new Button(this.width / 2 - 75, 80, 150, 20, commandString, (screen) -> {
clipboard = !clipboard;
commandClipboardButton.setMessage(clipboard ? "Clipboard" : "Send");
}));
String keyString;
if (ClickActionScreen.keyBinding != null) {
keyString = I18n.format(keyBinding.getKeyDescription());
} else {
if (EditSessionData.clickAction instanceof ClickActionKey) {
keyString = I18n.format(((ClickActionKey) EditSessionData.clickAction).key);
} else {
keyString = "Select a key";
}
}
addButton(this.keybindButton = new Button(this.width / 2 - 75, 50, 150, 20, keyString, (screen) -> ScreenStack.push(new PickKeyScreen())));
String keyToggleString;
if (EditSessionData.clickAction instanceof ClickActionKey) {
keyToggleString = ((ClickActionKey) EditSessionData.clickAction).toggle ? "Toggle" : "Press";
} else {
keyToggleString = toggle ? "Toggle" : "Press";
}
addButton(this.keybindToggleButton = new Button(this.width / 2 - 75, 80, 150, 20, keyToggleString, (screen) -> {
toggle = !toggle;
keybindToggleButton.setMessage(toggle ? "Toggle" : "Press");
}));
String itemString;
if (!ClickActionScreen.item.isEmpty()) {
itemString = "Item: " + item.getDisplayName().getString();
} else {
if (EditSessionData.clickAction != null && EditSessionData.clickAction.getClickAction() == ClickAction.ITEM_USE) {
itemString = "Item: " + ((ClickActionUseItem) EditSessionData.clickAction).stack.getItem().getName().getString();
} else {
itemString = "Select a Slot";
}
}
addButton(this.selectItemButton = new Button(this.width / 2 - 75, 50, 150, 20, itemString, (screen) -> ScreenStack.push(new PickItemScreen())));
addButton(this.modeCommand = new ItemButton(this.width / 2 - 55, this.height - 90, 20, 20, new ItemStack(Items.PAPER), (screen) -> {
// Command
mode = 0;
modeCategory.active = true;
modeUseItem.active = true;
modeKeybinding.active = true;
modeCommand.active = false;
textCategory.setVisible(false);
selectItemButton.visible = false;
textCommand.setVisible(true);
commandClipboardButton.visible = true;
keybindButton.visible = false;
keybindToggleButton.visible = false;
}));
addButton(this.modeKeybinding = new ItemButton(this.width / 2 - 25, this.height - 90, 20, 20, new ItemStack(Blocks.OAK_BUTTON), (screen) -> {
// Keybinding
mode = 1;
modeCategory.active = true;
modeUseItem.active = true;
modeKeybinding.active = false;
modeCommand.active = true;
textCategory.setVisible(false);
selectItemButton.visible = false;
textCommand.setVisible(false);
commandClipboardButton.visible = false;
keybindButton.visible = true;
keybindToggleButton.visible = true;
}));
addButton(this.modeUseItem = new ItemButton(this.width / 2 + 5, this.height - 90, 20, 20, new ItemStack(Items.DIAMOND_SWORD), (screen) -> {
// Select item
mode = 2;
modeCategory.active = true;
modeUseItem.active = false;
modeKeybinding.active = true;
modeCommand.active = true;
textCategory.setVisible(false);
selectItemButton.visible = true;
textCommand.setVisible(false);
commandClipboardButton.visible = false;
keybindButton.visible = false;
keybindToggleButton.visible = false;
}));
addButton(this.modeCategory = new ItemButton(this.width / 2 + 35, this.height - 90, 20, 20, new ItemStack(Blocks.CHEST), (screen) -> {
// Category
mode = 3;
modeCategory.active = false;
modeUseItem.active = true;
modeKeybinding.active = true;
modeCommand.active = true;
textCategory.setVisible(true);
selectItemButton.visible = false;
textCommand.setVisible(false);
commandClipboardButton.visible = false;
keybindButton.visible = false;
keybindToggleButton.visible = false;
}));
this.textCommand = new TextFieldWidget(this.font, this.width / 2 - 150, 50, 300, 20, "minemenu.action.command");
this.textCommand.setMaxStringLength(32767);
this.textCommand.changeFocus(true);
this.textCommand.setText((EditSessionData.clickAction instanceof ClickActionCommand) ? ((ClickActionCommand) EditSessionData.clickAction).command : "");
this.textCategory = new TextFieldWidget(this.font, this.width / 2 - 150, 50, 300, 20, "minemenu.action.category");
this.textCategory.setMaxStringLength(32767);
this.textCategory.changeFocus(true);
this.textCategory.setText((EditSessionData.clickAction instanceof ClickActionCategory) ? ((ClickActionCategory) EditSessionData.clickAction).category : "");
this.modeCommand.active = mode != 0;
this.modeKeybinding.active = mode != 1;
this.modeUseItem.active = mode != 2;
this.modeCategory.active = mode != 3;
textCommand.setVisible(mode == 0);
commandClipboardButton.visible = mode == 0;
keybindButton.visible = mode == 1;
keybindToggleButton.visible = mode == 1;
selectItemButton.visible = mode == 2;
textCategory.setVisible(mode == 3);
}
@Override
public void removed() {
this.getMinecraft().keyboardListener.enableRepeatEvents(false);
}
@Override
public boolean isPauseScreen() {
return false;
}
@Override
public boolean charTyped(char key, int keycode) {
this.textCommand.charTyped(key, keycode);
this.textCategory.charTyped(key, keycode);
return true;
}
@Override
public boolean mouseClicked(double mx, double my, int button) {
super.mouseClicked(mx, my, button);
this.textCommand.mouseClicked(mx, my, button);
return true;
}
@Override
public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
if (p_keyPressed_1_ == GLFW.GLFW_KEY_ESCAPE) {
ScreenStack.pop();
return true;
} else {
return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
}
}
@Override
public void render(int mouseX, int mouseY, float partial) {
this.renderBackground();
this.textCommand.render(mouseX, mouseY, partial);
this.textCategory.render(mouseX, mouseY, partial);
super.render(mouseX, mouseY, partial);
String header = "";
switch (mode) {
case 0:
header = "Enter a command";
break;
case 1:
header = "Select a key";
break;
case 2:
header = "Pick an item";
break;
case 3:
header = "Enter a category";
break;
}
GuiRenderHelper.renderHeaderAndFooter(this, 25, 20, 5, header);
if (mouseX > modeCommand.x && mouseX < modeCommand.x + modeCommand.getWidth() && mouseY > modeCommand.y && mouseY < modeCommand.y + modeCommand.getWidth()) {
this.renderTooltip(Collections.singletonList("Click Action: Command"), mouseX, mouseY);
} else if (mouseX > modeKeybinding.x && mouseX < modeKeybinding.x + modeKeybinding.getWidth() && mouseY > modeKeybinding.y && mouseY < modeKeybinding.y + modeKeybinding.getWidth()) {
this.renderTooltip(Collections.singletonList("Click Action: KeyBinding"), mouseX, mouseY);
} else if (mouseX > modeUseItem.x && mouseX < modeUseItem.x + modeUseItem.getWidth() && mouseY > modeUseItem.y && mouseY < modeUseItem.y + modeUseItem.getWidth()) {
this.renderTooltip(Collections.singletonList("Click Action: Use Item"), mouseX, mouseY);
} else if (mouseX > modeCategory.x && mouseX < modeCategory.x + modeCategory.getWidth() && mouseY > modeCategory.y && mouseY < modeCategory.y + modeCategory.getWidth()) {
this.renderTooltip(Collections.singletonList("Click Action: Category"), mouseX, mouseY);
}
}
}