forked from Funwayguy/BetterQuesting
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathGuiPrerequisiteEditor.java
More file actions
354 lines (300 loc) · 15.9 KB
/
GuiPrerequisiteEditor.java
File metadata and controls
354 lines (300 loc) · 15.9 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package betterquesting.client.gui2.editors;
import betterquesting.api.client.gui.misc.INeedsRefresh;
import betterquesting.api.client.gui.misc.IVolatileScreen;
import betterquesting.api.enums.EnumLogic;
import betterquesting.api.properties.NativeProps;
import betterquesting.api.questing.IQuest;
import betterquesting.api2.client.gui.GuiScreenCanvas;
import betterquesting.api2.client.gui.controls.IPanelButton;
import betterquesting.api2.client.gui.controls.PanelButton;
import betterquesting.api2.client.gui.controls.PanelButtonStorage;
import betterquesting.api2.client.gui.controls.PanelTextField;
import betterquesting.api2.client.gui.controls.filters.FieldFilterString;
import betterquesting.api2.client.gui.events.IPEventListener;
import betterquesting.api2.client.gui.events.PEventBroadcaster;
import betterquesting.api2.client.gui.events.PanelEvent;
import betterquesting.api2.client.gui.events.types.PEventButton;
import betterquesting.api2.client.gui.misc.GuiAlign;
import betterquesting.api2.client.gui.misc.GuiPadding;
import betterquesting.api2.client.gui.misc.GuiRectangle;
import betterquesting.api2.client.gui.misc.GuiTransform;
import betterquesting.api2.client.gui.misc.IGuiRect;
import betterquesting.api2.client.gui.panels.CanvasEmpty;
import betterquesting.api2.client.gui.panels.CanvasTextured;
import betterquesting.api2.client.gui.panels.bars.PanelVScrollBar;
import betterquesting.api2.client.gui.panels.content.PanelLine;
import betterquesting.api2.client.gui.panels.content.PanelTextBox;
import betterquesting.api2.client.gui.panels.lists.CanvasQuestDatabase;
import betterquesting.api2.client.gui.panels.lists.CanvasScrolling;
import betterquesting.api2.client.gui.themes.presets.PresetColor;
import betterquesting.api2.client.gui.themes.presets.PresetIcon;
import betterquesting.api2.client.gui.themes.presets.PresetLine;
import betterquesting.api2.client.gui.themes.presets.PresetTexture;
import betterquesting.api2.storage.DBEntry;
import betterquesting.api2.utils.QuestTranslation;
import betterquesting.client.gui2.GuiQuest;
import betterquesting.network.handlers.NetQuestEdit;
import betterquesting.questing.QuestDatabase;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import org.lwjgl.input.Keyboard;
public class GuiPrerequisiteEditor extends GuiScreenCanvas implements IPEventListener, IVolatileScreen, INeedsRefresh {
private IQuest quest;
private final int questID;
private CanvasQuestDatabase canvasDB;
private CanvasScrolling canvasPreReq;
private PanelButton btnLogic;
public GuiPrerequisiteEditor(GuiScreen parent, IQuest quest) {
super(parent);
this.quest = quest;
this.questID = QuestDatabase.INSTANCE.getID(quest);
}
@Override
public void refreshGui() {
quest = QuestDatabase.INSTANCE.getValue(questID);
if (quest == null) {
mc.displayGuiScreen(parent);
return;
}
canvasDB.refreshSearch();
refreshReqCanvas();
btnLogic.setText(QuestTranslation.translate("betterquesting.btn.logic") + ": " + quest.getProperty(NativeProps.LOGIC_QUEST));
}
@Override
public void initPanel() {
super.initPanel();
PEventBroadcaster.INSTANCE.register(this, PEventButton.class);
Keyboard.enableRepeatEvents(true);
// Background panel
CanvasTextured cvBackground = new CanvasTextured(new GuiTransform(GuiAlign.FULL_BOX, new GuiPadding(0, 0, 0, 0), 0),
PresetTexture.PANEL_MAIN.getTexture());
this.addPanel(cvBackground);
PanelTextBox panTxt = new PanelTextBox(new GuiTransform(GuiAlign.TOP_EDGE, new GuiPadding(0, 16, 0, -32), 0),
QuestTranslation.translate("betterquesting.title.pre_requisites")).setAlignment(1);
panTxt.setColor(PresetColor.TEXT_HEADER.getColor());
cvBackground.addPanel(panTxt);
btnLogic = new PanelButton(new GuiTransform(GuiAlign.TOP_RIGHT, -116, 8, 100, 16, 0),
9,
QuestTranslation.translate("betterquesting.btn.logic") + ": " + quest.getProperty(NativeProps.LOGIC_QUEST));
cvBackground.addPanel(btnLogic);
cvBackground.addPanel(new PanelButton(new GuiTransform(GuiAlign.BOTTOM_CENTER, -100, -16, 200, 16, 0), 0, QuestTranslation.translate("gui.back")));
// === RIGHT SIDE ===
CanvasEmpty cvRight = new CanvasEmpty(new GuiTransform(GuiAlign.HALF_RIGHT, new GuiPadding(8, 32, 16, 24), 0));
cvBackground.addPanel(cvRight);
PanelTextBox txtDb = new PanelTextBox(new GuiTransform(GuiAlign.TOP_EDGE, new GuiPadding(0, 0, 0, -16), 0),
QuestTranslation.translate("betterquesting.gui.database")).setAlignment(1)
.setColor(PresetColor.TEXT_MAIN.getColor());
cvRight.addPanel(txtDb);
PanelTextField<String> searchBox = new PanelTextField<>(new GuiTransform(GuiAlign.TOP_EDGE, new GuiPadding(0, 16, 8, -32), 0),
"",
FieldFilterString.INSTANCE);
searchBox.setWatermark("Search...");
cvRight.addPanel(searchBox);
canvasDB = new CanvasQuestDatabase(new GuiTransform(GuiAlign.FULL_BOX, new GuiPadding(0, 32, 8, 24), 0)) {
@Override
protected boolean addResult(DBEntry<IQuest> entry, int index, int width) {
PanelButtonStorage<DBEntry<IQuest>> btnAdd = new PanelButtonStorage<>(new GuiRectangle(0, index * 16, 16, 16, 0), 2, "", entry);
btnAdd.setIcon(PresetIcon.ICON_POSITIVE.getTexture());
btnAdd.setActive(!containsReq(quest, entry.getID()));
this.addPanel(btnAdd);
PanelButtonStorage<DBEntry<IQuest>> btnEdit = new PanelButtonStorage<>(new GuiRectangle(16, index * 16, width - 32, 16, 0),
1,
QuestTranslation.translate(entry.getValue()
.getProperty(NativeProps.NAME)),
entry);
this.addPanel(btnEdit);
PanelButtonStorage<DBEntry<IQuest>> btnDel = new PanelButtonStorage<>(new GuiRectangle(width - 16, index * 16, 16, 16, 0), 4, "", entry);
btnDel.setIcon(PresetIcon.ICON_TRASH.getTexture());
this.addPanel(btnDel);
return true;
}
};
cvRight.addPanel(canvasDB);
searchBox.setCallback(canvasDB::setSearchFilter);
PanelVScrollBar scDb = new PanelVScrollBar(new GuiTransform(GuiAlign.RIGHT_EDGE, new GuiPadding(-8, 32, 0, 24), 0));
cvRight.addPanel(scDb);
canvasDB.setScrollDriverY(scDb);
PanelButton btnNew = new PanelButton(new GuiTransform(GuiAlign.BOTTOM_EDGE, new GuiPadding(0, -16, 0, 0), 0),
5,
QuestTranslation.translate("betterquesting.btn.new"));
cvRight.addPanel(btnNew);
// === LEFT SIDE ===
CanvasEmpty cvLeft = new CanvasEmpty(new GuiTransform(GuiAlign.HALF_LEFT, new GuiPadding(16, 32, 8, 24), 0));
cvBackground.addPanel(cvLeft);
PanelTextBox txtQuest = new PanelTextBox(new GuiTransform(GuiAlign.TOP_EDGE, new GuiPadding(0, 0, 0, -16), 0),
QuestTranslation.translate(quest.getProperty(NativeProps.NAME))).setAlignment(1)
.setColor(PresetColor.TEXT_MAIN.getColor());
cvLeft.addPanel(txtQuest);
canvasPreReq = new CanvasScrolling(new GuiTransform(GuiAlign.FULL_BOX, new GuiPadding(0, 16, 8, 0), 0));
cvLeft.addPanel(canvasPreReq);
PanelVScrollBar scReq = new PanelVScrollBar(new GuiTransform(GuiAlign.RIGHT_EDGE, new GuiPadding(-8, 16, 0, 0), 0));
cvLeft.addPanel(scReq);
canvasPreReq.setScrollDriverY(scReq);
// === DIVIDERS ===
IGuiRect ls0 = new GuiTransform(GuiAlign.TOP_CENTER, 0, 32, 0, 0, 0);
ls0.setParent(cvBackground.getTransform());
IGuiRect le0 = new GuiTransform(GuiAlign.BOTTOM_CENTER, 0, -24, 0, 0, 0);
le0.setParent(cvBackground.getTransform());
PanelLine paLine0 = new PanelLine(ls0, le0, PresetLine.GUI_DIVIDER.getLine(), 1, PresetColor.GUI_DIVIDER.getColor(), 1);
cvBackground.addPanel(paLine0);
refreshReqCanvas();
}
private void refreshReqCanvas() {
canvasPreReq.resetCanvas();
int width = canvasPreReq.getTransform().getWidth();
List<DBEntry<IQuest>> arrReq = QuestDatabase.INSTANCE.bulkLookup(quest.getRequirements());
for (int i = 0; i < arrReq.size(); i++) {
PanelButtonStorage<DBEntry<IQuest>> btnEdit = new PanelButtonStorage<>(new GuiRectangle(0, i * 16, width - 64, 16, 0),
1,
QuestTranslation.translate(arrReq.get(i)
.getValue()
.getProperty(NativeProps.NAME)),
arrReq.get(i));
canvasPreReq.addPanel(btnEdit);
PanelButtonStorage<DBEntry<IQuest>> btnType = new PanelButtonStorage<>(new GuiRectangle(width - 64, i * 16, 16, 16, 0), 6, "", arrReq.get(i));
int arrReqID = arrReq.get(i).getID();
btnType.setIcon(quest.getRequirementType(arrReqID).getIcon().getTexture());
if (quest.getRequirementType(arrReqID) == IQuest.RequirementType.NORMAL)
btnType.setTooltip(Collections.singletonList(QuestTranslation.translate("betterquesting.btn.visible_always")));
else if (quest.getRequirementType(arrReqID) == IQuest.RequirementType.IMPLICIT)
btnType.setTooltip(Collections.singletonList(QuestTranslation.translate("betterquesting.btn.visible_implicit")));
else
btnType.setTooltip(Collections.singletonList(QuestTranslation.translate("betterquesting.btn.visible_hidden")));
canvasPreReq.addPanel(btnType);
PanelButtonStorage<DBEntry<IQuest>> btnUp = new PanelButtonStorage<>(new GuiRectangle(width - 48, i * 16, 16, 16, 0), 7, "", arrReq.get(i));
btnUp.setIcon(PresetIcon.ICON_UP.getTexture());
btnUp.setActive(arrReq.size() > 1);
canvasPreReq.addPanel(btnUp);
PanelButtonStorage<DBEntry<IQuest>> btnDown = new PanelButtonStorage<>(new GuiRectangle(width - 32, i * 16, 16, 16, 0), 8, "", arrReq.get(i));
btnDown.setIcon(PresetIcon.ICON_DOWN.getTexture());
btnDown.setActive(arrReq.size() > 1);
canvasPreReq.addPanel(btnDown);
PanelButtonStorage<DBEntry<IQuest>> btnRem = new PanelButtonStorage<>(new GuiRectangle(width - 16, i * 16, 16, 16, 0), 3, "", arrReq.get(i));
btnRem.setIcon(PresetIcon.ICON_NEGATIVE.getTexture());
canvasPreReq.addPanel(btnRem);
}
}
@Override
public void onPanelEvent(PanelEvent event) {
if (event instanceof PEventButton) {
onButtonPress((PEventButton) event);
}
}
@SuppressWarnings("unchecked")
private void onButtonPress(PEventButton event) {
IPanelButton btn = event.getButton();
if (btn.getButtonID() == 0) { // Exit
mc.displayGuiScreen(this.parent);
} else if (btn.getButtonID() == 1 && btn instanceof PanelButtonStorage) { // Edit Quest
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
mc.displayGuiScreen(new GuiQuest(this, entry.getID()));
} else if (btn.getButtonID() == 2 && btn instanceof PanelButtonStorage) { // Add
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
addReq(quest, entry.getID());
SendChanges();
} else if (btn.getButtonID() == 3 && btn instanceof PanelButtonStorage) { // Remove
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
removeReq(quest, entry.getID());
SendChanges();
} else if (btn.getButtonID() == 4 && btn instanceof PanelButtonStorage) { // Delete
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
NBTTagCompound payload = new NBTTagCompound();
payload.setIntArray("questIDs", new int[]{
entry.getID()
});
payload.setInteger("action", 1);
NetQuestEdit.sendEdit(payload);
} else if (btn.getButtonID() == 5) { // New
NBTTagCompound payload = new NBTTagCompound();
NBTTagList dataList = new NBTTagList();
NBTTagCompound entry = new NBTTagCompound();
entry.setInteger("questID", -1);
dataList.appendTag(entry);
payload.setTag("data", dataList);
NetQuestEdit.sendEdit(payload);
} else if (btn.getButtonID() == 6) { // set type
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
quest.setRequirementType(entry.getID(), quest.getRequirementType(entry.getID()).next());
SendChanges();
} else if (btn.getButtonID() == 7) { // Up
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
reorderReq(quest, entry.getID(), false);
SendChanges();
} else if (btn.getButtonID() == 8) { // Down
DBEntry<IQuest> entry = ((PanelButtonStorage<DBEntry<IQuest>>) btn).getStoredValue();
reorderReq(quest, entry.getID(), true);
SendChanges();
} else if (btn.getButtonID() == 9) { // Logic
EnumLogic[] logicList = EnumLogic.values();
EnumLogic logic = quest.getProperty(NativeProps.LOGIC_QUEST);
logic = logicList[(logic.ordinal() + 1) % logicList.length];
quest.setProperty(NativeProps.LOGIC_QUEST, logic);
((PanelButton) btn).setText(QuestTranslation.translate("betterquesting.btn.logic") + ": " + logic);
SendChanges();
}
}
private boolean containsReq(IQuest quest, int id) {
for (int reqID : quest.getRequirements()) {
if (id == reqID)
return true;
}
return false;
}
private void reorderReq(IQuest quest, int id, boolean down) {
int[] orig = quest.getRequirements();
int indexToShift = -1;
for (int i = 0; i < orig.length; i++) {
if (orig[i] == id) {
indexToShift = i;
break;
}
}
if (indexToShift < 0)
return;
int indexFrom = (indexToShift + (down ? 1 : -1) + orig.length) % orig.length;
int tmp = orig[indexToShift];
orig[indexToShift] = orig[indexFrom];
orig[indexFrom] = tmp;
quest.setRequirements(orig);
}
private void removeReq(IQuest quest, int id) {
int[] orig = quest.getRequirements();
if (orig.length <= 0)
return;
boolean hasRemoved = false;
int[] rem = new int[orig.length - 1];
for (int i = 0; i < orig.length; i++) {
if (!hasRemoved && orig[i] == id) {
hasRemoved = true;
continue;
} else if (!hasRemoved && i >= rem.length)
break;
rem[!hasRemoved ? i : (i - 1)] = orig[i];
}
if (hasRemoved)
quest.setRequirements(rem);
}
private void addReq(IQuest quest, int id) {
if (containsReq(quest, id))
return;
int[] orig = quest.getRequirements();
int[] added = Arrays.copyOf(orig, orig.length + 1);
added[orig.length] = id;
quest.setRequirements(added);
}
private void SendChanges() {
NBTTagCompound payload = new NBTTagCompound();
NBTTagList dataList = new NBTTagList();
NBTTagCompound entry = new NBTTagCompound();
entry.setInteger("questID", questID);
entry.setTag("config", quest.writeToNBT(new NBTTagCompound()));
dataList.appendTag(entry);
payload.setTag("data", dataList);
payload.setInteger("action", 0);
NetQuestEdit.sendEdit(payload);
}
}