-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTaskDashboard.java
More file actions
433 lines (369 loc) · 17.8 KB
/
TaskDashboard.java
File metadata and controls
433 lines (369 loc) · 17.8 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.logmaster.ui.component;
import com.logmaster.LogMasterConfig;
import com.logmaster.LogMasterPlugin;
import com.logmaster.domain.Task;
import com.logmaster.domain.TaskTier;
import com.logmaster.synchronization.SyncService;
import com.logmaster.task.TaskService;
import com.logmaster.ui.generic.UIButton;
import com.logmaster.ui.generic.UIGraphic;
import com.logmaster.ui.generic.UILabel;
import com.logmaster.ui.generic.UIPage;
import lombok.Getter;
import net.runelite.api.Client;
import net.runelite.api.FontID;
import net.runelite.api.SoundEffectID;
import net.runelite.api.widgets.ItemQuantityMode;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetType;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Map;
import static com.logmaster.ui.InterfaceConstants.COLLECTION_LOG_WINDOW_HEIGHT;
import static com.logmaster.ui.InterfaceConstants.COLLECTION_LOG_WINDOW_WIDTH;
public class TaskDashboard extends UIPage {
private final static int DEFAULT_BUTTON_WIDTH = 140;
private final static int DEFAULT_BUTTON_HEIGHT = 30;
private final static int SMALL_BUTTON_WIDTH = 68;
private final static int DEFAULT_TASK_DETAILS_WIDTH = 300;
private final static int DEFAULT_TASK_DETAILS_HEIGHT = 75;
private final static int GENERATE_TASK_SPRITE_ID = -20001;
private final static int COMPLETE_TASK_SPRITE_ID = -20000;
private final static int GENERATE_TASK_HOVER_SPRITE_ID = -20003;
private final static int COMPLETE_TASK_HOVER_SPRITE_ID = -20002;
private final static int GENERATE_TASK_DISABLED_SPRITE_ID = -20005;
private final static int COMPLETE_TASK_DISABLED_SPRITE_ID = -20004;
private final static int TASK_BACKGROUND_SPRITE_ID = -20006;
private final static int FAQ_BUTTON_SPRITE_ID = -20027;
private final static int FAQ_BUTTON_HOVER_SPRITE_ID = -20028;
private final static int SYNC_BUTTON_SPRITE_ID = -20034;
private final static int SYNC_BUTTON_HOVER_SPRITE_ID = -20035;
private final static int SYNC_BUTTON_DISABLED_SPRITE_ID = -20036;
@Getter
private Widget window;
private LogMasterPlugin plugin;
private LogMasterConfig config;
private final SyncService syncService;
private final TaskService taskService;
private final Client client;
private final TaskInfo taskInfo;
private UILabel title;
private UILabel taskLabel;
private UILabel percentCompletion;
private UILabel rerollLabel;
private UIGraphic taskImage;
private UIGraphic taskBg;
private UIButton completeTaskBtn;
private UIButton generateTaskBtn;
private UIButton faqBtn;
private UIButton syncBtn;
public TaskDashboard(LogMasterPlugin plugin, LogMasterConfig config, Widget window, SyncService syncService, TaskService taskService, Client client, TaskInfo taskInfo) {
this.window = window;
this.plugin = plugin;
this.config = config;
this.syncService = syncService;
this.taskService = taskService;
this.client = client;
this.taskInfo = taskInfo;
createTaskDetails();
Widget titleWidget = window.createChild(-1, WidgetType.TEXT);
this.title = new UILabel(titleWidget);
this.title.setFont(FontID.QUILL_CAPS_LARGE);
this.title.setSize(COLLECTION_LOG_WINDOW_WIDTH, DEFAULT_TASK_DETAILS_HEIGHT);
this.title.setPosition(getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH), 24);
this.title.setText("Current Task");
Widget percentWidget = window.createChild(-1, WidgetType.TEXT);
this.percentCompletion = new UILabel(percentWidget);
this.percentCompletion.setFont(FontID.BOLD_12);
this.percentCompletion.setSize(COLLECTION_LOG_WINDOW_WIDTH, 25);
this.percentCompletion.setPosition(getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH), COLLECTION_LOG_WINDOW_HEIGHT - 97);
updatePercentages();
Widget rerollWidget = window.createChild(-1, WidgetType.TEXT);
this.rerollLabel = new UILabel(rerollWidget);
this.rerollLabel.setFont(FontID.BOLD_12);
this.rerollLabel.setSize(COLLECTION_LOG_WINDOW_WIDTH, 25);
this.rerollLabel.setPosition(getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH), COLLECTION_LOG_WINDOW_HEIGHT - 75);
updateRerolls();
Widget completeTaskWidget = window.createChild(-1, WidgetType.GRAPHIC);
this.completeTaskBtn = new UIButton(completeTaskWidget);
this.completeTaskBtn.setSize(DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT);
this.completeTaskBtn.setPosition(getCenterX(window, DEFAULT_BUTTON_WIDTH) + (DEFAULT_BUTTON_WIDTH / 2 + 15), getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 62);
this.completeTaskBtn.setSprites(COMPLETE_TASK_SPRITE_ID, COMPLETE_TASK_HOVER_SPRITE_ID);
Widget generateTaskWidget = window.createChild(-1, WidgetType.GRAPHIC);
this.generateTaskBtn = new UIButton(generateTaskWidget);
this.generateTaskBtn.setSize(DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT);
this.generateTaskBtn.setPosition(getCenterX(window, DEFAULT_BUTTON_WIDTH) - (DEFAULT_BUTTON_WIDTH / 2 + 15), getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 62);
this.generateTaskBtn.setSprites(GENERATE_TASK_SPRITE_ID, GENERATE_TASK_HOVER_SPRITE_ID);
Widget faqWidget = window.createChild(-1, WidgetType.GRAPHIC);
this.faqBtn = new UIButton(faqWidget);
this.faqBtn.setSize(SMALL_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT);
this.faqBtn.setPosition(getCenterX(window, SMALL_BUTTON_WIDTH) + 190, getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 112);
this.faqBtn.setSprites(FAQ_BUTTON_SPRITE_ID, FAQ_BUTTON_HOVER_SPRITE_ID);
Widget syncWidget = window.createChild(-1, WidgetType.GRAPHIC);
this.syncBtn = new UIButton(syncWidget);
this.syncBtn.setSize(SMALL_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT);
this.syncBtn.setPosition(getCenterX(window, SMALL_BUTTON_WIDTH) - 190, getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 112);
this.syncBtn.setSprites(SYNC_BUTTON_SPRITE_ID, SYNC_BUTTON_DISABLED_SPRITE_ID);
this.add(this.title);
this.add(this.taskBg);
this.add(this.taskLabel);
this.add(this.taskImage);
this.add(this.completeTaskBtn);
this.add(this.generateTaskBtn);
this.add(this.percentCompletion);
this.add(this.rerollLabel);
this.add(faqBtn);
this.add(syncBtn);
}
@Override
public void setVisibility(boolean visible) {
super.setVisibility(visible);
if (visible) {
if (taskService.getActiveTask() == null) {
clearTask();
} else {
Task activeTask = taskService.getActiveTask();
setTask(activeTask, null);
}
}
}
private void createTaskDetails() {
final int POS_X = getCenterX(window, DEFAULT_TASK_DETAILS_WIDTH);
final int POS_Y = getCenterY(window, DEFAULT_TASK_DETAILS_HEIGHT)-3;
Widget taskBgWidget = window.createChild(-1, WidgetType.GRAPHIC);
this.taskBg = new UIGraphic(taskBgWidget);
this.taskBg.setSize(DEFAULT_TASK_DETAILS_WIDTH, DEFAULT_TASK_DETAILS_HEIGHT);
this.taskBg.setPosition(POS_X, POS_Y);
this.taskBg.setSprite(TASK_BACKGROUND_SPRITE_ID);
Widget label = window.createChild(-1, WidgetType.TEXT);
label.setTextColor(Color.WHITE.getRGB());
label.setTextShadowed(true);
label.setName("Task Label");
this.taskLabel = new UILabel(label);
this.taskLabel.setFont(496);
this.taskLabel.setPosition(POS_X+60, POS_Y);
this.taskLabel.setSize(DEFAULT_TASK_DETAILS_WIDTH-60, DEFAULT_TASK_DETAILS_HEIGHT);
Widget taskImageWidget = window.createChild(-1, WidgetType.GRAPHIC);
this.taskImage = new UIGraphic(taskImageWidget);
this.taskImage.setPosition(POS_X+12, POS_Y+21);
this.taskImage.setSize(42, 36);
this.taskImage.getWidget().setBorderType(1);
}
public void clearTask() {
this.taskBg.getWidget().clearActions();
this.taskBg.clearActions();
this.taskLabel.setText("No active task.");
this.taskImage.setItem(7542);
this.disableCompleteTask();
this.enableGenerateTask();
this.enableFaqButton();
}
public void setTask(Task task, List<Task> cyclingTasks) {
this.disableGenerateTask();
this.disableCompleteTask();
this.taskBg.getWidget().clearActions();
this.taskBg.clearActions();
if (cyclingTasks != null) {
for (int i = 0; i < 250; i++) {
Task displayTask = cyclingTasks.get((int) Math.floor(Math.random() * cyclingTasks.size()));
// Seems the most natural timing
double decay = 450.0 / ((double) config.rollTime());
int delay = (int) ((config.rollTime() * 0.925) * Math.exp(-decay * i));
Timer fakeTaskTimer = new Timer(delay, ae -> {
this.taskLabel.setText(displayTask.getName());
this.taskImage.setItem(displayTask.getDisplayItemId());
});
fakeTaskTimer.setRepeats(false);
fakeTaskTimer.setCoalesce(true);
fakeTaskTimer.start();
}
Timer realTaskTimer = new Timer(config.rollTime(), ae -> {
setTask(task, null);
});
realTaskTimer.setRepeats(false);
realTaskTimer.setCoalesce(true);
realTaskTimer.start();
return;
}
this.taskLabel.setText(task.getName());
this.taskImage.setItem(task.getDisplayItemId());
this.taskBg.addAction("View task info", () -> taskInfo.showTask(task.getId()));
if (taskService.getRerolls() > 0) {
this.enableGenerateTask();
}
this.enableCompleteTask();
this.enableFaqButton();
}
private void generateTask() {
client.playSoundEffect(SoundEffectID.UI_BOOP);
Task generatedTask = taskService.generate();
List<Task> rollTaskList = config.rollPastCompleted() ? taskService.getTierTasks() : taskService.getIncompleteTierTasks();
disableGenerateTask();
disableCompleteTask();
updatePercentages();
setTask(generatedTask, rollTaskList);
updateRerolls();
}
public void updatePercentages() {
Map<TaskTier, Float> progress = taskService.getProgress();
TaskTier currentTier = taskService.getCurrentTier();
float tierPercentage = progress.get(currentTier);
String text = String.format(
"<col=%s>%d%%</col> %s Completed",
getCompletionColor(tierPercentage),
(int) tierPercentage,
currentTier.displayName
);
percentCompletion.setText(text);
}
public void updateRerolls() {
String color;
switch (taskService.getRerolls()) {
case 0:
color = "e74c3c";
break;
case 1:
color = "e67e22";
break;
case 2:
color = "f1c40f";
break;
default:
color = "2ecc71";
break;
}
String rerollsText = config.rerollsEnabled() ? "Re-rolls available: <col=" + color + ">" + taskService.getRerolls() + "</col>" : "";
this.rerollLabel.setText(rerollsText);
}
private String getCompletionColor(double percent) {
int max = 255;
int amount = (int) Math.round(((percent % 50) / 50) * max);
if(percent == 100) {
return "00ff00";
}
else if(percent > 50) {
int redValue = max - amount;
return String.format("%02x", redValue)+"ff00";
}
else if(percent == 50) {
return "ffff00";
}
else {
return "ff"+String.format("%02x", amount)+"00";
}
}
public void disableGenerateTask() {
this.generateTaskBtn.setSprites(GENERATE_TASK_DISABLED_SPRITE_ID);
this.generateTaskBtn.getWidget().clearActions();
this.generateTaskBtn.clearActions();
this.generateTaskBtn.addAction("Disabled", this::playFailSound);
updateRerolls();
}
public void enableGenerateTask() {
this.generateTaskBtn.getWidget().clearActions();
this.generateTaskBtn.clearActions();
this.generateTaskBtn.setSprites(GENERATE_TASK_SPRITE_ID, GENERATE_TASK_HOVER_SPRITE_ID);
if (taskService.getRerolls() > 0 && taskService.getActiveTask() != null) {
this.generateTaskBtn.addAction("Right click to re-roll", this::playFailSound);
this.generateTaskBtn.addAction("<col=2ecc71>Re-roll task</col>", this::generateTask);
} else {
this.generateTaskBtn.addAction("Generate task", this::generateTask);
}
this.disableCompleteTask();
updateRerolls();
}
public void disableCompleteTask() {
this.completeTaskBtn.setSprites(COMPLETE_TASK_DISABLED_SPRITE_ID);
this.completeTaskBtn.clearActions();
this.completeTaskBtn.addAction("Disabled", this::playFailSound);
updateRerolls();
}
public void enableCompleteTask() {
this.completeTaskBtn.clearActions();
this.completeTaskBtn.setSprites(COMPLETE_TASK_SPRITE_ID, COMPLETE_TASK_HOVER_SPRITE_ID);
this.completeTaskBtn.addAction("Complete", plugin::completeTask);
updateRerolls();
}
public void enableFaqButton() {
this.faqBtn.clearActions();
this.faqBtn.setSprites(FAQ_BUTTON_SPRITE_ID, FAQ_BUTTON_HOVER_SPRITE_ID);
this.faqBtn.addAction("FAQ", plugin::visitFaq);
this.enableSyncButton();
}
public void enableSyncButton() {
this.syncBtn.clearActions();
this.syncBtn.setSprites(SYNC_BUTTON_SPRITE_ID, SYNC_BUTTON_HOVER_SPRITE_ID);
this.syncBtn.addAction("Auto sync completed tasks", syncService::sync);
}
public void updateBounds() {
int windowWidth = window.getWidth();
// Update title position - force widget position update
int titleX = getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH);
this.title.setPosition(titleX, 24);
this.title.getWidget().setPos(titleX, 24);
// Update task details (background, label, image)
final int taskPosX = getCenterX(window, DEFAULT_TASK_DETAILS_WIDTH);
final int taskPosY = getCenterY(window, DEFAULT_TASK_DETAILS_HEIGHT) - 3;
this.taskBg.setPosition(taskPosX, taskPosY);
this.taskBg.getWidget().setPos(taskPosX, taskPosY);
this.taskLabel.setPosition(taskPosX + 60, taskPosY);
this.taskLabel.getWidget().setPos(taskPosX + 60, taskPosY);
this.taskImage.setPosition(taskPosX + 12, taskPosY + 21);
this.taskImage.getWidget().setPos(taskPosX + 12, taskPosY + 21);
// Update button positions - force widget position updates
int generateBtnX = getCenterX(window, DEFAULT_BUTTON_WIDTH) - (DEFAULT_BUTTON_WIDTH / 2 + 15);
int generateBtnY = getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 62;
this.generateTaskBtn.setPosition(generateBtnX, generateBtnY);
this.generateTaskBtn.getWidget().setPos(generateBtnX, generateBtnY);
int completeBtnX = getCenterX(window, DEFAULT_BUTTON_WIDTH) + (DEFAULT_BUTTON_WIDTH / 2 + 15);
int completeBtnY = getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 62;
this.completeTaskBtn.setPosition(completeBtnX, completeBtnY);
this.completeTaskBtn.getWidget().setPos(completeBtnX, completeBtnY);
// Update FAQ button position with boundary checking
int faqBtnX = getCenterX(window, SMALL_BUTTON_WIDTH) + 238;
int faqBtnY = getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 112;
// Check if FAQ button would go outside the window and align with edge if needed
int faqBtnWidth = SMALL_BUTTON_WIDTH;
if (faqBtnX + faqBtnWidth + 10 > windowWidth) {
faqBtnX = windowWidth - faqBtnWidth - 10; // 10px margin from edge
}
this.faqBtn.setPosition(faqBtnX, faqBtnY);
this.faqBtn.getWidget().setPos(faqBtnX, faqBtnY);
// Update Sync button position with boundary checking
int syncBtnX = getCenterX(window, SMALL_BUTTON_WIDTH) - 238;
int syncBtnY = getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 112;
if (syncBtnX < 10) {
syncBtnX = 10; // 10px margin from left edge
}
this.syncBtn.setPosition(syncBtnX, syncBtnY);
this.syncBtn.getWidget().setPos(syncBtnX, syncBtnY);
// Update percentage completion position - force widget position update
int percentX = getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH);
int percentY = getCenterY(window, DEFAULT_BUTTON_HEIGHT) + 106;
this.percentCompletion.setPosition(percentX, percentY);
this.percentCompletion.getWidget().setPos(percentX, percentY);
this.rerollLabel.setPosition(getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH), percentY + 16);
this.rerollLabel.getWidget().setPos(getCenterX(window, COLLECTION_LOG_WINDOW_WIDTH), percentY + 16);
// Force revalidation of all widgets
this.title.getWidget().revalidate();
this.taskBg.getWidget().revalidate();
this.taskLabel.getWidget().revalidate();
this.taskImage.getWidget().revalidate();
this.generateTaskBtn.getWidget().revalidate();
this.completeTaskBtn.getWidget().revalidate();
this.faqBtn.getWidget().revalidate();
this.syncBtn.getWidget().revalidate();
this.percentCompletion.getWidget().revalidate();
this.rerollLabel.getWidget().revalidate();
}
private int getCenterX(Widget window, int width) {
return (window.getWidth() / 2) - (width / 2);
}
private int getCenterY(Widget window, int height) {
return (window.getHeight() / 2) - (height / 2);
}
private void playFailSound() {
client.playSoundEffect(2277);
}
}