forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.lua
More file actions
428 lines (388 loc) · 16.4 KB
/
button.lua
File metadata and controls
428 lines (388 loc) · 16.4 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
function courseplay:register_button(self, hudPage, img, function_to_call, parameter, x, y, width, height, hudRow, modifiedParameter, hoverText, isMouseWheelArea, isToggleButton)
local overlay = nil;
if img and img ~= "blank.dds" then
overlay = Overlay:new(img, Utils.getFilename("img/" .. img, courseplay.path), x, y, width, height);
end;
if hoverText == nil then
hoverText = false;
end;
if isMouseWheelArea == nil then
isMouseWheelArea = false;
end;
if isToggleButton == nil then
isToggleButton = false;
end;
local button = {
page = hudPage,
overlay = overlay,
overlays = { overlay },
function_to_call = function_to_call,
parameter = parameter,
x_init = x,
x = x,
x2 = (x + width),
y_init = y,
y = y,
y2 = (y + height),
row = hudRow,
hoverText = hoverText,
color = courseplay.hud.colors.white,
isMouseWheelArea = isMouseWheelArea and function_to_call ~= nil,
isToggleButton = isToggleButton,
canBeClicked = not isMouseWheelArea and function_to_call ~= nil,
show = true,
isClicked = false,
isActive = false,
isDisabled = false,
isHovered = false,
isHidden = false
};
if modifiedParameter then
button.modifiedParameter = modifiedParameter;
end
if isMouseWheelArea then
button.canScrollUp = true;
button.canScrollDown = true;
end;
if function_to_call == "toggleDebugChannel" then
local col = ((button.parameter-1) % courseplay.numDebugChannelButtonsPerLine) + 1;
local line = math.ceil(button.parameter / courseplay.numDebugChannelButtonsPerLine);
--space in dds: 16 x, 2 y
local uvX1,uvX2 = (col-1)/16, col/16;
local uvY1 = 1 - (line * (courseplay.numDebugChannelButtonsPerLine/courseplay.numAvailableDebugChannels));
local uvY2 = uvY1 + (courseplay.numDebugChannelButtonsPerLine/courseplay.numAvailableDebugChannels);
setOverlayUVs(button.overlay.overlayId, uvX1,uvY1, uvX1,uvY2, uvX2,uvY1, uvX2,uvY2);
end;
table.insert(self.cp.buttons[tostring(hudPage)], button);
return #(self.cp.buttons[tostring(hudPage)]);
end
function courseplay:renderButtons(self, page)
for _,button in pairs(self.cp.buttons.global) do
courseplay:renderButton(self, button);
end;
for _,button in pairs(self.cp.buttons[tostring(page)]) do
courseplay:renderButton(self, button);
end;
if page == 2 then
for _,button in pairs(self.cp.buttons["-2"]) do
courseplay:renderButton(self, button);
end;
end;
end;
function courseplay:renderButton(self, button)
local pg, fn, prm = button.page, button.function_to_call, button.parameter;
--mouseWheelAreas conditionals
if button.isMouseWheelArea then
if pg == 1 then
if fn == "setCustomFieldEdgePathNumber" then
button.canScrollUp = self.cp.fieldEdge.customField.isCreated and self.cp.fieldEdge.customField.fieldNum < courseplay.fields.customFieldMaxNum;
button.canScrollDown = self.cp.fieldEdge.customField.isCreated and self.cp.fieldEdge.customField.fieldNum > 0;
end;
elseif pg == 2 then
if fn == "shiftHudCourses" then
button.canScrollUp = self.cp.hud.courseListPrev == true;
button.canScrollDown = self.cp.hud.courseListNext == true;
end;
elseif pg == 3 then
if fn == "changeTurnRadius" then
button.canScrollUp = true;
button.canScrollDown = self.cp.turnRadius > 0;
elseif fn == "change_required_fill_level" then
button.canScrollUp = self.cp.followAtFillLevel < 100;
button.canScrollDown = self.cp.followAtFillLevel > 0;
elseif fn == "change_required_fill_level_for_drive_on" then
button.canScrollUp = self.cp.driveOnAtFillLevel < 100;
button.canScrollDown = self.cp.driveOnAtFillLevel > 0;
end;
elseif pg == 4 then
if fn == 'setSearchCombineOnField' then
button.canScrollUp = courseplay.fields.numAvailableFields > 0 and self.cp.searchCombineAutomatically and self.cp.searchCombineOnField > 0;
button.canScrollDown = courseplay.fields.numAvailableFields > 0 and self.cp.searchCombineAutomatically and self.cp.searchCombineOnField < courseplay.fields.numAvailableFields;
end;
elseif pg == 5 then
if fn == "change_turn_speed" then
button.canScrollUp = self.cp.speeds.turn < 60/3600;
button.canScrollDown = self.cp.speeds.turn > 5/3600;
elseif fn == "change_field_speed" then
button.canScrollUp = self.cp.speeds.field < 60/3600;
button.canScrollDown = self.cp.speeds.field > 5/3600;
elseif fn == "change_max_speed" then
button.canScrollUp = self.cp.speeds.useRecordingSpeed == false and self.cp.speeds.max < 60/3600;
button.canScrollDown = self.cp.speeds.useRecordingSpeed == false and self.cp.speeds.max > 5/3600;
elseif fn == "change_unload_speed" then
button.canScrollUp = self.cp.speeds.unload < 60/3600;
button.canScrollDown = self.cp.speeds.unload > 3/3600;
end;
elseif pg == 6 then
if fn == "changeWaitTime" then
button.canScrollUp = not (self.cp.mode == 3 or self.cp.mode == 4 or self.cp.mode == 6 or self.cp.mode == 7);
button.canScrollDown = button.canScrollUp and self.cp.waitTime > 0;
end;
elseif pg == 7 then
if fn == "changeLaneOffset" then
button.canScrollUp = self.cp.mode == 4 or self.cp.mode == 6;
button.canScrollDown = button.canScrollUp;
elseif fn == "changeToolOffsetX" or fn == "changeToolOffsetZ" then
button.canScrollUp = self.cp.mode == 3 or self.cp.mode == 4 or self.cp.mode == 6 or self.cp.mode == 7 or self.cp.mode == 8;
button.canScrollDown = button.canScrollUp;
end;
elseif pg == 8 then
if fn == "setFieldEdgePath" then
button.canScrollUp = courseplay.fields.numAvailableFields > 0 and self.cp.fieldEdge.selectedField.fieldNum > 0;
button.canScrollDown = courseplay.fields.numAvailableFields > 0 and self.cp.fieldEdge.selectedField.fieldNum < courseplay.fields.numAvailableFields;
elseif fn == "changeWorkWidth" then
button.canScrollUp = true;
button.canScrollDown = self.cp.workWidth > 0.1;
end;
end;
elseif button.overlay ~= nil then
button.show = true;
--CONDITIONAL DISPLAY
--Global
if pg == "global" then
if fn == "showSaveCourseForm" and prm == "course" then
button.show = self.cp.canDrive and not self.cp.isRecording and not self.cp.recordingIsPaused and self.Waypoints ~= nil and #(self.Waypoints) ~= 0;
end;
--Page 1
elseif pg == 1 then
if fn == "setAiMode" then
button.show = self.cp.canSwitchMode;
elseif fn == "clearCustomFieldEdge" or fn == "toggleCustomFieldEdgePathShow" then
button.show = not self.cp.canDrive and self.cp.fieldEdge.customField.isCreated;
elseif fn == "setCustomFieldEdgePathNumber" then
if prm < 0 then
button.show = not self.cp.canDrive and self.cp.fieldEdge.customField.isCreated and self.cp.fieldEdge.customField.fieldNum > 0;
elseif prm > 0 then
button.show = not self.cp.canDrive and self.cp.fieldEdge.customField.isCreated and self.cp.fieldEdge.customField.fieldNum < courseplay.fields.customFieldMaxNum;
end;
elseif fn == 'stop_record' or fn == 'setRecordingPause' or fn == 'delete_waypoint' or fn == 'set_waitpoint' or fn == 'set_crossing' or fn == 'setRecordingTurnManeuver' or fn == 'change_DriveDirection' then
button.show = self.cp.isRecording or self.cp.recordingIsPaused;
end;
--Page 2
elseif pg == 2 then
if fn == "reloadCoursesFromXML" then
button.show = g_server ~= nil;
elseif fn == "showSaveCourseForm" and prm == "filter" then
button.show = not self.cp.hud.choose_parent;
elseif fn == "shiftHudCourses" then
if prm < 0 then
button.show = self.cp.hud.courseListPrev;
elseif prm > 0 then
button.show = self.cp.hud.courseListNext;
end;
end;
elseif pg == -2 then
button.show = self.cp.hud.content.pages[2][prm][1].text ~= nil;
--Page 3
elseif pg == 3 then
if fn == "changeTurnRadius" and prm < 0 then
button.show = self.cp.turnRadius > 0;
elseif fn == "change_required_fill_level" then
if prm < 0 then
button.show = self.cp.followAtFillLevel > 0;
elseif prm > 0 then
button.show = self.cp.followAtFillLevel < 100;
end;
elseif fn == "change_required_fill_level_for_drive_on" then
if prm < 0 then
button.show = self.cp.driveOnAtFillLevel > 0;
elseif prm > 0 then
button.show = self.cp.driveOnAtFillLevel < 100;
end;
end;
--Page 4
elseif pg == 4 then
if fn == 'selectAssignedCombine' then
button.show = not self.cp.searchCombineAutomatically;
if button.show and prm < 0 then
button.show = self.cp.selectedCombineNumber > 0;
end;
elseif fn == 'setSearchCombineOnField' then
button.show = courseplay.fields.numAvailableFields > 0 and self.cp.searchCombineAutomatically;
if button.show then
if prm < 0 then
button.show = self.cp.searchCombineOnField > 0;
else
button.show = self.cp.searchCombineOnField < courseplay.fields.numAvailableFields;
end;
end;
elseif fn == 'removeActiveCombineFromTractor' then
button.show = self.cp.activeCombine ~= nil;
end;
--Page 5
elseif pg == 5 then
if fn == "change_turn_speed" then
if prm < 0 then
button.show = self.cp.speeds.turn > 5/3600;
elseif prm > 0 then
button.show = self.cp.speeds.turn < 60/3600;
end;
elseif fn == "change_field_speed" then
if prm < 0 then
button.show = self.cp.speeds.field > 5/3600;
elseif prm > 0 then
button.show = self.cp.speeds.field < 60/3600;
end;
elseif fn == "change_max_speed" then
if prm < 0 then
button.show = not self.cp.speeds.useRecordingSpeed and self.cp.speeds.max > 5/3600;
elseif prm > 0 then
button.show = not self.cp.speeds.useRecordingSpeed and self.cp.speeds.max < 60/3600;
end;
elseif fn == "change_unload_speed" then
if prm < 0 then
button.show = self.cp.speeds.unload > 3/3600;
elseif prm > 0 then
button.show = self.cp.speeds.unload < 60/3600;
end;
end;
--Page 6
elseif pg == 6 then
if fn == "changeWaitTime" then
button.show = not (self.cp.mode == 3 or self.cp.mode == 4 or self.cp.mode == 6 or self.cp.mode == 7);
if prm < 0 and button.show then
button.show = self.cp.waitTime > 0;
end;
elseif fn == "toggleDebugChannel" then
button.show = prm >= courseplay.debugChannelSectionStart and prm <= courseplay.debugChannelSectionEnd;
elseif fn == "changeDebugChannelSection" then
if prm < 0 then
button.show = courseplay.debugChannelSection > 1;
elseif prm > 0 then
button.show = courseplay.debugChannelSection < math.ceil(courseplay.numAvailableDebugChannels / courseplay.numDebugChannelButtonsPerLine);
end;
end;
--Page 7
elseif pg == 7 then
if fn == "changeLaneOffset" then
button.show = self.cp.mode == 4 or self.cp.mode == 6;
elseif fn == "toggleSymmetricLaneChange" then
button.show = self.cp.mode == 4 or self.cp.mode == 6 and self.cp.laneOffset ~= 0;
elseif fn == "changeToolOffsetX" or fn == "changeToolOffsetZ" then
button.show = self.cp.mode == 3 or self.cp.mode == 4 or self.cp.mode == 6 or self.cp.mode == 7;
elseif fn == "switchDriverCopy" and prm < 0 then
button.show = self.cp.selectedDriverNumber > 0;
elseif fn == "copyCourse" then
button.show = self.cp.hasFoundCopyDriver;
end;
--Page 8
elseif pg == 8 then
if fn == "toggleSelectedFieldEdgePathShow" then
button.show = courseplay.fields.numAvailableFields > 0 and self.cp.fieldEdge.selectedField.fieldNum > 0;
elseif fn == "setFieldEdgePath" then
button.show = courseplay.fields.numAvailableFields > 0;
if button.show then
if prm < 0 then
button.show = self.cp.fieldEdge.selectedField.fieldNum > 0;
elseif prm > 0 then
button.show = self.cp.fieldEdge.selectedField.fieldNum < courseplay.fields.numAvailableFields;
end;
end;
elseif fn == "changeWorkWidth" and prm < 0 then
button.show = self.cp.workWidth > 0.1;
elseif fn == "switchStartingDirection" then
button.show = self.cp.hasStartingCorner;
elseif fn == 'setHeadlandDir' or fn == 'setHeadlandOrder' then
button.show = self.cp.headland.numLanes > 0;
elseif fn == 'setHeadlandNumLanes' then
if prm < 0 then
button.show = self.cp.headland.numLanes > 0;
elseif prm > 0 then
button.show = self.cp.headland.numLanes < self.cp.headland.maxNumLanes;
end;
elseif fn == "generateCourse" then
button.show = self.cp.hasValidCourseGenerationData;
end;
end;
if button.show and not button.isHidden then
local colors = courseplay.hud.colors;
local currentColor = courseplay:getButtonColor(button);
local targetColor = currentColor;
local hoverColor = colors.hover;
if fn == "openCloseHud" then
hoverColor = colors.closeRed;
end;
if not button.isDisabled and not button.isActive and not button.isHovered and button.canBeClicked and not button.isClicked and not courseplay:colorsMatch(currentColor, colors.white) then
targetColor = colors.white;
elseif button.isDisabled and not courseplay:colorsMatch(currentColor, colors.whiteDisabled) then
targetColor = colors.whiteDisabled;
elseif not button.isDisabled and button.canBeClicked and button.isClicked and not fn == "openCloseHud" then
targetColor = colors.activeRed;
elseif button.isHovered and ((not button.isDisabled and button.isToggleButton and button.isActive and button.canBeClicked and not button.isClicked) or (not button.isDisabled and not button.isActive and button.canBeClicked and not button.isClicked)) and not courseplay:colorsMatch(currentColor, hoverColor) then
targetColor = hoverColor;
if button.isToggleButton then
--print(string.format('button %q (loop %d): isHovered=%s, isActive=%s, isDisabled=%s, canBeClicked=%s -> hoverColor', fn, g_updateLoopIndex, tostring(button.isHovered), tostring(button.isActive), tostring(button.isDisabled), tostring(button.canBeClicked)));
end;
elseif button.isActive and (not button.isToggleButton or (button.isToggleButton and not button.isHovered)) and not courseplay:colorsMatch(currentColor, colors.activeGreen) then
targetColor = colors.activeGreen;
if button.isToggleButton then
--print(string.format('button %q (loop %d): isHovered=%s, isActive=%s, isDisabled=%s, canBeClicked=%s -> activeGreen', fn, g_updateLoopIndex, tostring(button.isHovered), tostring(button.isActive), tostring(button.isDisabled), tostring(button.canBeClicked)));
end;
end;
-- set colors
if not courseplay:colorsMatch(currentColor, targetColor) then
courseplay:setButtonColor(button, targetColor)
end;
button.overlay:render();
end;
end; --elseif button.overlay ~= nil
end;
function courseplay:setButtonColor(button, color)
if button == nil or button.overlay == nil or color == nil or table.getn(color) ~= 4 then
return;
end;
button.overlay:setColor(unpack(color));
end;
function courseplay:getButtonColor(button)
if button == nil or button.overlay == nil or button.overlay.r == nil or button.overlay.g == nil or button.overlay.b == nil or button.overlay.a == nil then
return nil;
end;
local r,g,b,a = button.overlay.r, button.overlay.g, button.overlay.b, button.overlay.a;
if r == nil or g == nil or b == nil or a == nil then
return nil;
end;
return { r,g,b,a };
end;
function courseplay:colorsMatch(color1, color2)
if color1 == nil or color2 == nil then
return nil;
end;
return Utils.areListsEqual(color1, color2, false);
end;
function courseplay.button.setOffset(button, x_off, y_off)
x_off = x_off or 0
y_off = y_off or 0
local width = button.x2 - button.x
local height = button.y2 - button.y
button.x = button.x_init + x_off
button.y = button.y_init + y_off
button.x2 = button.x + width
button.y2 = button.y + height
button.overlay.x = button.x_init + x_off
button.overlay.y = button.y_init + y_off
end
function courseplay.button.addOverlay(button, index, img)
local width = button.x2 - button.x
local height = button.y2 - button.y
button.overlays[index] = Overlay:new(img, Utils.getFilename("img/" .. img, courseplay.path), button.x, button.y, width, height);
end
function courseplay.button.setOverlay(button, index)
button.overlay = button.overlays[index]
-- the offset of the button might have changed...
button.overlay.x = button.x
button.overlay.y = button.y
end
function courseplay.button.deleteButtonOverlays(vehicle)
for k,buttonSection in pairs(vehicle.cp.buttons) do
for i,button in pairs(buttonSection) do
if button.overlays ~= nil then
for j,overlay in pairs(button.overlays) do
if overlay.overlayId ~= nil and overlay.delete ~= nil then
overlay:delete();
end;
end;
end;
--NOTE: deleting single overlays not necessary since all overlay in button.overlays have already been deleted.
end;
end;
end;