This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathbutton.lua
418 lines (350 loc) · 13.1 KB
/
button.lua
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
-- #################################################################
-- courseplay.button class
courseplay.button = {};
cpButton_mt = Class(courseplay.button);
function courseplay.button:new(vehicle, hudPage, img, functionToCall, parameter, x, y, width, height, hudRow,
modifiedParameter, hoverText, isMouseWheelArea, isToggleButton, toolTip, onlyCallLocal)
local self = setmetatable({}, cpButton_mt);
if img then
if type(img) == 'table' then
if img[1] == 'iconSprite.png' then
self.overlay = Overlay:new( courseplay.hud.iconSpritePath, x, y, width, height);
self.spriteSection = img[2];
end;
else
self.overlay = Overlay:new(Utils.getFilename('img/' .. img, courseplay.path), x, y, width, height);
end;
end;
if hoverText == nil then
hoverText = false;
end;
if isMouseWheelArea == nil then
isMouseWheelArea = false;
end;
if isToggleButton == nil then
isToggleButton = false;
end;
if onlyCallLocal == nil then
onlyCallLocal = false;
end;
if not vehicle.isCourseplayManager then
self.vehicle = vehicle;
end;
self.page = hudPage;
self.settingCall = nil
self.functionToCall = functionToCall;
self:setParameter(parameter);
self.width = width;
self.height = height;
self.x_init = x;
self.x = x;
self.x2 = (x + width);
self.y_init = y;
self.y = y;
self.y2 = (y + height);
self.row = hudRow;
self.hoverText = hoverText;
self:setColor('white')
self:setToolTip(toolTip);
self.isMouseWheelArea = isMouseWheelArea and functionToCall ~= nil;
self.isToggleButton = isToggleButton;
self.onlyCallLocal = onlyCallLocal;
self:setCanBeClicked(not isMouseWheelArea and functionToCall ~= nil);
self:setShow(true);
self:setClicked(false);
self:setActive(false);
self:setDisabled(false);
self:setHovered(false);
if modifiedParameter then
self.modifiedParameter = modifiedParameter;
end
if isMouseWheelArea then
self.canScrollUp = true;
self.canScrollDown = true;
end;
if self.spriteSection then
self:setSpriteSectionUVs(self.spriteSection);
else
self:setSpecialButtonUVs();
end;
if not vehicle.cp then
table.insert(vehicle.buttons, self);
else
table.insert(vehicle.cp.buttons[hudPage], self);
end;
return self;
end;
function courseplay.button:setOnlyCallLocal()
self.onlyCallLocal = true
return self;
end;
function courseplay.button:setSetting(setting)
self.settingCall = setting
--- ugly hack to allow the drive mode button overlays,
--- as the settingCall only gets passed after the button was created
self:setSpecialButtonUVs()
return self;
end;
function courseplay.button:setSpriteSectionUVs(spriteSection)
if not spriteSection or courseplay.hud.buttonUVsPx[spriteSection] == nil then return; end;
self.spriteSection = spriteSection;
courseplay.utils:setOverlayUVsPx(self.overlay, courseplay.hud.buttonUVsPx[spriteSection], courseplay.hud.iconSpriteSize.x, courseplay.hud.iconSpriteSize.y);
end;
function courseplay.button:setSpecialButtonUVs()
if not self.overlay then return; end;
local fn = self.functionToCall;
local prm = self.parameter;
local txtSizeX, txtSizeY = courseplay.hud.iconSpriteSize.x, courseplay.hud.iconSpriteSize.y;
if self.settingCall and self.settingCall == self.vehicle.cp.settings.driverMode then
courseplay.utils:setOverlayUVsPx(self.overlay, courseplay.hud.modeButtonsUVsPx[prm], txtSizeX, txtSizeY);
elseif fn == 'setHudPage' then
courseplay.utils:setOverlayUVsPx(self.overlay, courseplay.hud.pageButtonsUVsPx[prm], txtSizeX, txtSizeY);
elseif fn == 'generateCourse' then
courseplay.utils:setOverlayUVsPx(self.overlay, courseplay.hud.pageButtonsUVsPx[courseplay.hud.PAGE_COURSE_GENERATION], txtSizeX, txtSizeY);
-- CpManager buttons
elseif fn == 'goToVehicle' then
courseplay.utils:setOverlayUVsPx(self.overlay, courseplay.hud.pageButtonsUVsPx[courseplay.hud.PAGE_DRIVING_SETTINGS], txtSizeX, txtSizeY);
end;
end;
function courseplay.button:render()
-- self = courseplay.button
local vehicle, pg, fn, prm = self.vehicle, self.page, self.functionToCall, self.parameter;
local hoveredButton = false;
if self.overlay ~= nil then
if self.show then
-- set color
local currentColor = self.curColor;
local targetColor = currentColor;
local hoverColor = 'hover';
if fn == 'openCloseHud' then
hoverColor = 'closeRed';
end;
if not self.isDisabled and not self.isActive and not self.isHovered and (self.canBeClicked or self.functionToCall == nil) and not self.isClicked then
targetColor = 'white';
elseif self.isDisabled then
targetColor = 'whiteDisabled';
elseif not self.isDisabled and self.canBeClicked and self.isClicked and fn ~= 'openCloseHud' then
targetColor = 'activeRed';
elseif self.isHovered and ((not self.isDisabled and self.isToggleButton and self.isActive and self.canBeClicked and not self.isClicked) or (not self.isDisabled and not self.isActive and self.canBeClicked and not self.isClicked)) then
targetColor = hoverColor;
hoveredButton = true;
if self.isToggleButton then
--print(string.format('self %q (loop %d): isHovered=%s, isActive=%s, isDisabled=%s, canBeClicked=%s -> hoverColor', fn, g_updateLoopIndex, tostring(self.isHovered), tostring(self.isActive), tostring(self.isDisabled), tostring(self.canBeClicked)));
end;
elseif self.isActive and (not self.isToggleButton or (self.isToggleButton and not self.isHovered)) then
targetColor = 'activeGreen';
if self.isToggleButton then
--print(string.format('button %q (loop %d): isHovered=%s, isActive=%s, isDisabled=%s, canBeClicked=%s -> activeGreen', fn, g_updateLoopIndex, tostring(self.isHovered), tostring(self.isActive), tostring(self.isDisabled), tostring(self.canBeClicked)));
end;
end;
if currentColor ~= targetColor then
self:setColor(targetColor);
end;
-- render
self.overlay:render();
end;
end; --elseif button.overlay ~= nil
return hoveredButton;
end;
function courseplay.button:setColor(colorName)
if self.overlay and colorName and (self.curColor == nil or self.curColor ~= colorName) and courseplay.hud.colors[colorName] then
self.overlay:setColor(unpack(courseplay.hud.colors[colorName]));
self.curColor = colorName;
end;
end;
function courseplay.button:setPosition(posX, posY)
self.x = posX;
self.x_init = posX;
self.x2 = posX + self.width;
self.y = posY;
self.y_init = posY;
self.y2 = posY + self.height;
if not self.overlay then return; end;
self.overlay:setPosition(self.x, self.y);
end;
function courseplay.button:handleHoverAction(vehicle, posX, posY)
local button = self;
if button.isMouseWheelArea and (button.canScrollUp or button.canScrollDown) then
--Mouse wheel icon
vehicle.cp.hud.mouseWheel.render = true;
vehicle.cp.hud.mouseWheel.icon:setPosition(posX + 3/g_screenWidth, posY - 16/g_screenHeight);
--action
local parameter = button.parameter;
--print(string.format("if courseplay.inputModifierIsPressed(%s) and button.modifiedParameter(%s) ~= nil then",tostring(courseplay.inputModifierIsPressed),tostring(button.modifiedParameter)))
if courseplay.inputModifierIsPressed and button.modifiedParameter ~= nil then
parameter = button.modifiedParameter;
end;
local upParameter = parameter;
local downParameter = upParameter * -1;
if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_WHEEL_UP) and button.canScrollUp then
courseplay:debug(string.format("%s: MOUSE_BUTTON_WHEEL_UP: %s(%s)", nameNum(vehicle), tostring(button.functionToCall), tostring(upParameter)), courseplay.DBG_HUD);
self:handleInput(vehicle,upParameter)
elseif Input.isMouseButtonPressed(Input.MOUSE_BUTTON_WHEEL_DOWN) and button.canScrollDown then
courseplay:debug(string.format("%s: MOUSE_BUTTON_WHEEL_DOWN: %s(%s)", nameNum(vehicle), tostring(button.functionToCall), tostring(downParameter)), courseplay.DBG_HUD);
self:handleInput(vehicle,downParameter)
end;
end;
end
function courseplay.button:handleMouseClick(vehicle)
vehicle = vehicle or self.vehicle;
local parameter = self.parameter;
if courseplay.inputModifierIsPressed and self.modifiedParameter ~= nil then
courseplay:debug("self.modifiedParameter = " .. tostring(self.modifiedParameter), courseplay.DBG_HUD);
parameter = self.modifiedParameter;
end;
if self.show and not self.isHidden and self.canBeClicked and not self.isDisabled then
--[[if self.functionToCall == "rowButton" and vehicle.cp.hud.content.pages[vehicle.cp.hud.currentPage][self.parameter][1].text == nil then
return;
end;]]
-- self:setClicked(true);
if self.functionToCall == "showSaveCourseForm" then
vehicle.cp.imWriting = true
end
--- Global info text button was pressed, switch to vehicle.
if self.functionToCall == "goToVehicle" then
g_globalInfoTextHandler:goToVehicle(parameter)
else
courseplay:debug(string.format("%s: MOUSE_BUTTON_ClICKED: %s(%s)", nameNum(vehicle), tostring(self.functionToCall), tostring(parameter)), courseplay.DBG_HUD);
self:handleInput(vehicle,parameter)
end
-- self:setClicked(false);
end;
end;
function courseplay.button:handleInput(vehicle,parameter)
if self.settingCall then --settingButton
courseplay:debug(string.format("%s: handleSettingInput: %s:%s(%s)", nameNum(vehicle),tostring(self.settingCall.name), tostring(self.functionToCall), tostring(parameter)), courseplay.DBG_HUD);
self.settingCall[self.functionToCall](self.settingCall, parameter)
if vehicle:getIsEntered() then
g_currentMission.hud.guiSoundPlayer:playSample(GuiSoundPlayer.SOUND_SAMPLES.CLICK)
end
courseplay.hud:setReloadPageOrder(vehicle, vehicle.cp.hud.currentPage, true);
else
if self.functionToCall then
vehicle:setCourseplayFunc(self.functionToCall, parameter, self.onlyCallLocal or false, self.page);
end
end
end
function courseplay.button:setOffset(offsetX, offsetY)
offsetX = offsetX or 0
offsetY = offsetY or 0
self.x = self.x_init + offsetX;
self.y = self.y_init + offsetY;
self.x2 = self.x + self.width;
self.y2 = self.y + self.height;
if not self.overlay then return; end;
self.overlay:setPosition(self.x, self.y);
end
function courseplay.button:setParameter(parameter)
if self.parameter ~= parameter then
self.parameter = parameter;
end;
end;
function courseplay.button:setToolTip(text)
if self.toolTip ~= text then
self.toolTip = text;
end;
end;
function courseplay.button:setActive(active)
if self.isActive ~= active then
self.isActive = active;
end;
end;
function courseplay.button:setCanBeClicked(canBeClicked)
if self.canBeClicked ~= canBeClicked then
self.canBeClicked = canBeClicked;
end;
end;
function courseplay.button:setClicked(clicked)
if self.isClicked ~= clicked then
self.isClicked = clicked;
end;
end;
function courseplay.button:setDisabled(disabled)
if self.isDisabled ~= disabled then
self.isDisabled = disabled;
end;
end;
function courseplay.button:setHovered(hovered)
if self.isHovered ~= hovered then
self.isHovered = hovered;
end;
end;
function courseplay.button:setCanScrollUp(canScrollUp)
if self.canScrollUp ~= canScrollUp then
self.canScrollUp = canScrollUp;
end;
end;
function courseplay.button:setCanScrollDown(canScrollDown)
if self.canScrollDown ~= canScrollDown then
self.canScrollDown = canScrollDown;
end;
end;
function courseplay.button:setShow(show)
if self.show ~= show then
self.show = show;
end;
end;
function courseplay.button:setAttribute(attribute, value)
if self[attribute] ~= value then
self[attribute] = value;
end;
end;
function courseplay.button:deleteOverlay()
if self.overlay ~= nil and self.overlay.overlayId ~= nil and self.overlay.delete ~= nil then
self.overlay:delete();
end;
end;
function courseplay.button:getHasMouse(mouseX, mouseY)
-- return mouseX > self.x and mouseX < self.x2 and mouseY > self.y and mouseY < self.y2;
return courseplay:mouseIsInArea(mouseX, mouseY, self.x, self.x2, self.y, self.y2);
end;
function courseplay.button:getIsDisabled()
return self.isDisabled
end
-- #################################################################
-- courseplay.buttons
function courseplay.buttons:renderButtons(vehicle, page)
-- self = courseplay.buttons
local hoveredButton;
for _,button in pairs(vehicle.cp.buttons.global) do
if button:render() then
hoveredButton = button;
end;
end;
for _,button in pairs(vehicle.cp.buttons[page]) do
if button:render() then
hoveredButton = button;
end;
end;
if page == courseplay.hud.PAGE_MANAGE_COURSES then
for _,button in pairs(vehicle.cp.buttons[-courseplay.hud.PAGE_MANAGE_COURSES]) do
if button:render() then
hoveredButton = button;
end;
end;
end;
-- set currently hovered button in vehicle
self:setHoveredButton(vehicle, hoveredButton);
end;
function courseplay.buttons:setHoveredButton(vehicle, button)
if vehicle.cp.buttonHovered == button then
return;
end;
vehicle.cp.buttonHovered = button;
self:onHoveredButtonChanged(vehicle);
end;
function courseplay.buttons:onHoveredButtonChanged(vehicle)
-- set toolTip in vehicle
if vehicle.cp.buttonHovered ~= nil and vehicle.cp.buttonHovered.toolTip ~= nil then
courseplay:setToolTip(vehicle, vehicle.cp.buttonHovered.toolTip);
elseif vehicle.cp.buttonHovered == nil then
courseplay:setToolTip(vehicle, nil);
end;
end;
function courseplay.buttons:deleteButtonOverlays(vehicle)
for k,buttonSection in pairs(vehicle.cp.buttons) do
for i,button in pairs(buttonSection) do
button:deleteOverlay();
end;
end;
end;