Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathGlobalInfoTextHandler.lua
More file actions
Latest commit
410 lines (362 loc) · 14.4 KB
/
Copy pathGlobalInfoTextHandler.lua
File metadata and controls
410 lines (362 loc) · 14.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
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2021 courseplay dev team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
The global info texts handler controls all info texts at the left side of the screen relative to the vehicles.
It enables quick vehicle swapping by pressing the button which has an info texts by the vehicle.
]]--
---@classGlobalInfoTextHandler
GlobalInfoTextHandler=CpObject()
GlobalInfoTextHandler.attributes= {
{name='level', getXmlFunction=getXMLInt},
{name='text', getXmlFunction=getXMLString},
}
functionGlobalInfoTextHandler:init()
self.playerOnFootMouseEnabled=false;
self.wasPlayerFrozen=false;
self.infoTexts= {}
self.vehicles= {}
end
--- Load the info text xml File.
functionGlobalInfoTextHandler:loadFromXml()
self.xmlFileName=Utils.getFilename('config/InfoTexts.xml', courseplay.path)
self.xmlFile=self:loadXmlFile(self.xmlFileName)
end
--- Loads the info texts.
---@paramfileNamestring
functionGlobalInfoTextHandler:loadXmlFile(fileName)
courseplay.info('Loading Info texts from %s ...', fileName)
iffileExists(fileName) then
localxmlFile=loadXMLFile('infoTexts', fileName);
localrootElement='InfoTexts'
ifxmlFileandhasXMLProperty(xmlFile, rootElement) then
locali=0
whiletruedo
localinfoTextElement=string.format('%s.InfoText(%d)', rootElement, i)
ifhasXMLProperty(xmlFile, infoTextElement) then
self:loadInfoText(xmlFile, infoTextElement)
else
break
end
i=i+1
end
returnxmlFile
end
else
courseplay.info('Info text file %s does not exist.', fileName)
end
end
--- Load a single info text.
---@paramxmlFilenumber id of the loaded xml file.
---@paraminfoTextElementstring xml key to the info text element.
functionGlobalInfoTextHandler:loadInfoText(xmlFile, infoTextElement)
localinfoText= {}
localname=getXMLString(xmlFile,string.format("%s#name",infoTextElement))
for_,attributeinpairs(self.attributes) do
infoText[attribute.name] =attribute.getXmlFunction(xmlFile,string.format("%s#%s",infoTextElement,attribute.name))
end
self.infoTexts[name] =infoText
end
--- Setup all the basic global info text information.
--- Needs to be done after the hud is initialized.
functionGlobalInfoTextHandler:setup()
--- Load info texts
self:loadFromXml()
--- All visual/button variables
self.posY=0.01238; -- = ingameMap posY
self.posYAboveMap=self.posY+0.027777777777778+0.20833333333333;
self.fontSize=courseplay.hud:pxToNormal(18, 'y');
self.lineHeight=self.fontSize*1.2;
self.lineMargin=self.lineHeight*0.2;
self.buttonHeight=self.lineHeight;
self.buttonWidth=self.buttonHeight/g_screenAspectRatio;
self.buttonPosX=0.015625; -- = ingameMap posX
self.buttonMargin=self.buttonWidth*0.4;
self.backgroundPadding=self.buttonWidth*0.2;
self.backgroundImg='dataS2/menu/white.png';
self.backgroundPosX=self.buttonPosX+self.buttonWidth+self.buttonMargin;
self.backgroundPosY=self.posY;
self.textPosX=self.backgroundPosX+self.backgroundPadding;
self.content= {};
self.vehicleHasText= {};
self.levelColors= {
[-2] =courseplay.hud.colors.closeRed;
[-1] =courseplay.hud.colors.activeRed;
[0] =courseplay.hud.colors.hover;
[1] =courseplay.hud.colors.activeGreen;
};
self.maxNum=20;
self.overlays= {};
self.buttons= {};
fori=1, self.maxNumdo
localposY=self.backgroundPosY+ (i-1) *self.lineHeight;
self.overlays[i] =Overlay:new(self.backgroundImg, self.backgroundPosX, posY, 0.1, self.buttonHeight);
courseplay.button:new(self, 'globalInfoText', 'iconSprite.png', 'goToVehicle', i, self.buttonPosX, posY, self.buttonWidth, self.buttonHeight);
end;
self.buttonsClickArea= {
x1=self.buttonPosX;
x2=self.buttonPosX+self.buttonWidth;
y1=self.backgroundPosY,
y2=self.backgroundPosY+ (self.maxNum* (self.lineHeight+self.lineMargin));
};
self.hasContent=false;
end
--- TODO: Find a better solution then to saves these in the vehicle.
--- Adds a new vehicle.
---@paramvehicletable
functionGlobalInfoTextHandler:addVehicle(vehicle)
self.vehicles[vehicle] = {
currentInfoTexts= {},
lastInfoTexts= {}
}
end
--- Removes a vehicle.
---@paramvehicletable
functionGlobalInfoTextHandler:removeVehicle(vehicle)
self:resetAllInfoTextsForVehicle(vehicle)
self.vehicles[vehicle] =nil
end
--- Reset all not used info texts at the end of the vehicle update loop.
---@paramvehicletable
functionGlobalInfoTextHandler:resetInactiveInfoTextsForVehicle(vehicle)
forrefIdx,_inpairs(self.infoTexts) do
ifnotself.vehicles[vehicle].currentInfoTexts[refIdx] then
ifself.vehicles[vehicle].lastInfoTexts[refIdx] then
self:resetInfoText(vehicle,refIdx)
end
end
end
self.vehicles[vehicle].lastInfoTexts=self.vehicles[vehicle].currentInfoTexts
self.vehicles[vehicle].currentInfoTexts= {}
end
--- Completely resets all info texts related to the vehicle.
---@paramvehicletable
functionGlobalInfoTextHandler:resetAllInfoTextsForVehicle(vehicle)
forrefIdx,_inpairs(self.infoTexts) do
ifself.vehicles[vehicle].currentInfoTexts[refIdx]
orself.vehicles[vehicle].lastInfoTexts[refIdx] then
self:resetInfoText(vehicle,refIdx)
end
end
end
--- Sets a global info text for the given vehicle.
---@paramvehicletable
---@paramrefIdxstring index for the info text.
functionGlobalInfoTextHandler:setInfoText(vehicle, refIdx)
localdata=self.infoTexts[refIdx]
ifself.vehicles[vehicle].currentInfoTexts[refIdx] ==nilorself.vehicles[vehicle].currentInfoTexts[refIdx] ~=data.levelthen
ifg_server~=nilthen
InfoTextEvent.sendEvent(vehicle,refIdx,false)
end
localtext=nameNum(vehicle) .."" ..courseplay:loc(data.text);
self.vehicles[vehicle].currentInfoTexts[refIdx] =data.level;
ifself.content[vehicle.rootNode] ==nilthen
self.content[vehicle.rootNode] = {};
end;
self.content[vehicle.rootNode][refIdx] = {
level=data.level,
text=text,
backgroundWidth=getTextWidth(self.fontSize, text) +self.backgroundPadding*2.5,
vehicle=vehicle
};
end;
end
--- Resets a global info text for the given vehicle.
---@paramvehicletable
---@paramrefIdxstring index for the info text.
functionGlobalInfoTextHandler:resetInfoText(vehicle, refIdx)
ifg_server~=nilthen
InfoTextEvent.sendEvent(vehicle,refIdx,true)
end
ifself.content[vehicle.rootNode] andself.content[vehicle.rootNode][refIdx] then
self.content[vehicle.rootNode][refIdx] =nil;
end;
self.vehicles[vehicle][refIdx] =nil
if#self.vehicles[vehicle].currentInfoTexts==0then
self.content[vehicle.rootNode] =nil;
end;
end
--- Renders the info texts buttons.
functionGlobalInfoTextHandler:render()
self.hasContent=false;
localnumLinesRendered=0;
localbasePosY=self.posY;
ifnot (g_currentMission.hud.ingameMap.isVisibleandg_currentMission.hud.ingameMap:getIsFullSize()) andnext(self.content) ~=nilthen
self.hasContent=true;
ifg_currentMission.hud.ingameMap.isVisiblethen
basePosY=self.posYAboveMap;
end;
end;
localline=0;
courseplay:setFontSettings('white', false, 'left');
for_,refIndexesinpairs(self.content) do
ifline>=self.maxNumthen
break;
end;
forrefIdx,datainpairs(refIndexes) do
line=line+1;
-- background
localbg=self.overlays[line];
bg:setColor(unpack(self.levelColors[data.level]));
localgfxPosY=basePosY+ (line-1) * (self.lineHeight+self.lineMargin);
bg:setPosition(bg.x, gfxPosY);
bg:setDimension(data.backgroundWidth, bg.height);
bg:render();
-- text
localtextPosY=gfxPosY+ (self.lineHeight-self.fontSize) *1.2; -- should be (lineHeight-fontSize)*0.5, but there seems to be some pixel/sub-pixel rendering error
renderText(self.textPosX, textPosY, self.fontSize, data.text);
-- button
localbutton=self.buttons[line];
ifbutton~=nilthen
button:setPosition(button.overlay.x, gfxPosY)
localcurrentColor=button.curColor;
localtargetColor=currentColor;
button:setCanBeClicked(true);
button:setDisabled(data.vehicle.isBrokenordata.vehicle.isControlled);
button:setParameter(data.vehicle);
ifg_currentMission.controlledVehicleandg_currentMission.controlledVehicle==data.vehiclethen
targetColor='activeGreen';
button:setCanBeClicked(false);
elseifbutton.isDisabledthen
targetColor='whiteDisabled';
elseifbutton.isClickedthen
targetColor='activeRed';
elseifbutton.isHoveredthen
targetColor='hover';
else
targetColor='white';
end;
-- set color
ifcurrentColor~=targetColorthen
button:setColor(targetColor);
end;
-- NOTE: do not use button:render() here, as we neither need the button.show check, nor the hoveredButton var, nor the color setting. Simply rendering the overlay suffices
button.overlay:render();
end;
end;
end;
self.buttonsClickArea.y1=basePosY;
self.buttonsClickArea.y2=basePosY+ (line* (self.lineHeight+self.lineMargin));
end;
--- Resets all buttons.
functionGlobalInfoTextHandler:resetButtons()
for_,buttoninpairs(self.buttons) do
button:setClicked(false);
button:setHovered(false);
end;
end
--- Delete all button overlays.
functionGlobalInfoTextHandler:delete()
--delete globalInfoText overlays
fori,buttoninpairs(self.buttons) do
button:deleteOverlay();
ifself.overlays[i] then
localovl=self.overlays[i];
ifovl.overlayId~=nilandovl.delete~=nilthen
ovl:delete();
end;
end;
end;
end
--- Mouse event to interact with the info text buttons.
---@paramposXnumber mouse x position
---@paramposYnumber mouse y position
---@paramisDownboolean the mouse button down ?
---@paramisUpboolean the mouse button up ?
---@parammouseKeynumber mouse button was pressed ?
functionGlobalInfoTextHandler:mouseEvent(posX, posY, isDown, isUp, mouseKey)
localarea=self.buttonsClickArea;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- LEFT CLICK to click the button shown in globalInfoText
if (isDownorisUp) andmouseKey==courseplay.inputBindings.mouse.primaryButtonIdandcourseplay:mouseIsInArea(posX, posY, area.x1, area.x2, area.y1, area.y2) then
self:onPrimaryMouseClick(posX, posY, isDown, isUp, mouseKey)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- RIGHT CLICK to activate the mouse cursor when I'm not in a vehicle and a globalInfoText is shown
elseifisUpandmouseKey==courseplay.inputBindings.mouse.secondaryButtonIdandg_currentMission.controlledVehicle==nilthen
self:onSecondaryMouseClick(posX, posY, isDown, isUp, mouseKey)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- HOVER
elseifnotisDownandnotisUpandself.hasContentthen
for_,buttoninpairs(self.buttons) do
button:setClicked(false)
ifbutton.showandnotbutton.isHiddenthen
button:setHovered(button:getHasMouse(posX, posY))
end
end
end
end
---Secondary mouse button pressed
---@paramposXnumber mouse x position
---@paramposYnumber mouse y position
---@paramisDownboolean the mouse button down ?
---@paramisUpboolean the mouse button up ?
---@parammouseKeynumber mouse button was pressed ?
functionGlobalInfoTextHandler:onSecondaryMouseClick(posX, posY, isDown, isUp, mouseKey)
ifself.hasContentandnotself.playerOnFootMouseEnabledandnotg_currentMission.player.currentToolthen
self.playerOnFootMouseEnabled=true
self.wasPlayerFrozen=g_currentMission.isPlayerFrozen
g_currentMission.isPlayerFrozen=true
elseifself.playerOnFootMouseEnabledthen
self.playerOnFootMouseEnabled=false
ifself.hasContentthen--if a button was hovered when deactivating the cursor, deactivate hover state
self:resetButtons()
end;
ifnotself.wasPlayerFrozenthen
g_currentMission.isPlayerFrozen=false
end;
end;
g_inputBinding:setShowMouseCursor(self.playerOnFootMouseEnabled)
end
---Primary mouse button pressed
---@paramposXnumber mouse x position
---@paramposYnumber mouse y position
---@paramisDownboolean the mouse button down ?
---@paramisUpboolean the mouse button up ?
---@parammouseKeynumber mouse button was pressed ?
functionGlobalInfoTextHandler:onPrimaryMouseClick(posX, posY, isDown, isUp, mouseKey)
ifself.hasContentthen
fori,buttoninpairs(self.buttons) do
ifbutton.showandbutton:getHasMouse(posX, posY) then
button:setClicked(isDown)
ifisUpthen
localsourceVehicle=g_currentMission.controlledVehicleorbutton.parameter
button:handleMouseClick(sourceVehicle)
end
break
end
end
end
end
---Is the second mouse button allowed, when the player isn't in a vehicle ?
---@returnboolean allowed?
functionGlobalInfoTextHandler:isSecondaryMouseClickAllowed()
returnself.playerOnFootMouseEnabledorself.hasContentandnotself.playerOnFootMouseEnabledandnotg_currentMission.player.currentTool
end
---Is the first mouse button allowed, when the player isn't in a vehicle ?
---@returnboolean allowed?
functionGlobalInfoTextHandler:isPrimaryMouseClickAllowed()
returnself.hasContentandself.playerOnFootMouseEnabled
end
--- Gets all info texts
functionGlobalInfoTextHandler:getInfoTexts()
returnself.infoTexts
end
--- Switch to a vehicle, which info text button was pressed.
functionGlobalInfoTextHandler:goToVehicle(vehicle)
g_client:getServerConnection():sendEvent(VehicleEnterRequestEvent:new(vehicle, g_currentMission.missionInfo.playerStyle, g_currentMission.player.farmId));
g_currentMission.isPlayerFrozen=false;
self.playerOnFootMouseEnabled=false;
g_inputBinding:setShowMouseCursor(vehicle.cp.mouseCursorActive);
end
g_globalInfoTextHandler=GlobalInfoTextHandler()