Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClickToSwitch.lua
More file actions
Latest commit
304 lines (257 loc) · 13.6 KB
/
Copy pathClickToSwitch.lua
File metadata and controls
304 lines (257 loc) · 13.6 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
--[[
This mod enables the possibility to enter vehicles by clicking with the mouse onto them.
Interface for other mods:
- vehicle:isClickToSwitchToggleMouseAllowed() :
This function can be overwritten to disable the mouse visibility action event.
- vehicle:isClickToSwitchAllowed() :
This function can be overwritten to enable the click to switch raycast, if the mouse is active.
]]
---@classClickToSwitch
ClickToSwitch= {}
ClickToSwitch.MOD_NAME=g_currentModName
ClickToSwitch.DEFAULT_ASSIGNMENT=false
ClickToSwitch.ADVANCED_ASSIGNMENT=true
ClickToSwitch.KEY="."..ClickToSwitch.MOD_NAME..".clickToSwitch#assignment"
functionClickToSwitch.initSpecialization()
localschema=Vehicle.xmlSchemaSavegame
schema:register(XMLValueType.BOOL, "vehicles.vehicle(?)"..ClickToSwitch.KEY,false)
end
functionClickToSwitch.prerequisitesPresent(specializations)
returnSpecializationUtil.hasSpecialization(Drivable, specializations)
end
functionClickToSwitch.registerEventListeners(vehicleType)
SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ClickToSwitch)
SpecializationUtil.registerEventListener(vehicleType, "onLoad", ClickToSwitch)
SpecializationUtil.registerEventListener(vehicleType, "onReadStream", ClickToSwitch)
SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", ClickToSwitch)
SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", ClickToSwitch)
end
functionClickToSwitch.registerFunctions(vehicleType)
SpecializationUtil.registerFunction(vehicleType, "isClickToSwitchMouseActive", ClickToSwitch.isClickToSwitchMouseActive)
SpecializationUtil.registerFunction(vehicleType, "getClickToSwitchLastMousePosition", ClickToSwitch.getClickToSwitchLastMousePosition)
SpecializationUtil.registerFunction(vehicleType, "enterVehicleRaycastClickToSwitch", ClickToSwitch.enterVehicleRaycastClickToSwitch)
SpecializationUtil.registerFunction(vehicleType, "enterVehicleRaycastCallbackClickToSwitch", ClickToSwitch.enterVehicleRaycastCallbackClickToSwitch)
end
functionClickToSwitch:onLoad(savegame)
--- Register the spec: spec_clickToSwitch
localspecName=ClickToSwitch.MOD_NAME..".clickToSwitch"
self.spec_clickToSwitch=self["spec_" ..specName]
localspec=self.spec_clickToSwitch
spec.texts= {}
spec.texts.toggleMouse=g_i18n:getText("input_CLICK_TO_SWITCH_TOGGLE_MOUSE")
spec.texts.toggleMouseAlternative=g_i18n:getText("input_CLICK_TO_SWITCH_TOGGLE_MOUSE_ALTERNATIVE")
spec.texts.changesAssignments=g_i18n:getText("input_CLICK_TO_SWITCH_CHANGES_ASSIGNMENTS")
spec.texts.enterVehicle=g_i18n:getText("input_CLICK_TO_SWITCH_ENTER_VEHICLE")
spec.assignmentMode=ClickToSwitch.DEFAULT_ASSIGNMENT
--- Creating a backup table of all camera and if they are rotatable
spec.camerasBackup= {}
forcamIndex, camerainpairs(self.spec_enterable.cameras) do
ifcamera.isRotatablethen
spec.camerasBackup[camIndex] =camera.isRotatable
end
end
ifsavegame==nilorsavegame.resetVehiclesthenreturnend
spec.assignmentMode=savegame.xmlFile:getValue(savegame.key..ClickToSwitch.KEY,false)
spec.changedCamera=false
end
functionClickToSwitch:saveToXMLFile(xmlFile, key, usedModNames)
localspec=self.spec_clickToSwitch
xmlFile:setValue(key.."#assignment", spec.assignmentMode)
end
--- Register toggle mouse state and clickToSwitch action events
---@paramisActiveForInputboolean
---@paramisActiveForInputIgnoreSelectionboolean
functionClickToSwitch:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
ifself.isClientthen
localspec=self.spec_clickToSwitch
self:clearActionEventsTable(spec.actionEvents)
ifself.isActiveForInputIgnoreSelectionIgnoreAIthen
ifnotg_modIsLoaded["FS25_Courseplay"] andnotg_modIsLoaded["FS25_AutoDrive"] then
--- Toggle mouse action event
local_, actionEventId=self:addActionEvent(spec.actionEvents, InputAction.CLICK_TO_SWITCH_TOGGLE_MOUSE, self, ClickToSwitch.actionEventToggleMouse, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
g_inputBinding:setActionEventText(actionEventId, spec.texts.toggleMouse)
--- ClickToSwitch (enter vehicle by mouse button) action event
_, actionEventId=self:addActionEvent(spec.actionEvents, InputAction.CLICK_TO_SWITCH_TOGGLE_MOUSE_ALTERNATIVE, self, ClickToSwitch.actionEventToggleMouse, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
g_inputBinding:setActionEventText(actionEventId, spec.texts.toggleMouseAlternative)
--- ClickToSwitch (enter vehicle by mouse button) action event
_, actionEventId=self:addActionEvent(spec.actionEvents, InputAction.CLICK_TO_SWITCH_CHANGES_ASSIGNMENTS, self, ClickToSwitch.actionEventChangeAssignments, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
g_inputBinding:setActionEventText(actionEventId, spec.texts.changesAssignments)
end
--- ClickToSwitch (enter vehicle by mouse button) action event
local_, actionEventId=self:addActionEvent(spec.actionEvents, InputAction.CLICK_TO_SWITCH_ENTER_VEHICLE, self, ClickToSwitch.actionEventEnterVehicle, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
g_inputBinding:setActionEventText(actionEventId, spec.texts.enterVehicle)
ClickToSwitch.updateActionEventState(self)
end
end
end
--- Updates toggle mouse state and clickToSwitch action events visibility and usability
---@paramselftable vehicle
functionClickToSwitch.updateActionEventState(self)
--- Activate/deactivate the clickToSwitch action event
localspec=self.spec_clickToSwitch
ifspec.actionEvents==nilornext(spec.actionEvents) ==nilthen
return
end
localactionEvent=spec.actionEvents[InputAction.CLICK_TO_SWITCH_ENTER_VEHICLE]
g_inputBinding:setActionEventActive(actionEvent.actionEventId, self:isClickToSwitchMouseActive())
actionEvent=spec.actionEvents[InputAction.CLICK_TO_SWITCH_CHANGES_ASSIGNMENTS]
ifactionEventthen
g_inputBinding:setActionEventActive(actionEvent.actionEventId, notself:isClickToSwitchMouseActive())
end
actionEvent=spec.actionEvents[InputAction.CLICK_TO_SWITCH_TOGGLE_MOUSE]
ifactionEventthen
g_inputBinding:setActionEventActive(actionEvent.actionEventId, spec.assignmentMode==ClickToSwitch.DEFAULT_ASSIGNMENT)
end
ifactionEventthen
actionEvent=spec.actionEvents[InputAction.CLICK_TO_SWITCH_TOGGLE_MOUSE_ALTERNATIVE]
g_inputBinding:setActionEventActive(actionEvent.actionEventId, spec.assignmentMode==ClickToSwitch.ADVANCED_ASSIGNMENT)
end
end
functionClickToSwitch:onUpdateTick()
ClickToSwitch.updateActionEventState(self)
end
--- Action event for turning the mouse on/off
---@paramselftable vehicle
---@paramactionNamestring
---@paraminputValuenumber
---@paramcallbackStatenumber
---@paramisAnalogboolean
functionClickToSwitch.actionEventToggleMouse(self, actionName, inputValue, callbackState, isAnalog)
localspec=self.spec_clickToSwitch
localshowCursor=notself:isClickToSwitchMouseActive()
spec.changedCamera=showCursor
g_inputBinding:setShowMouseCursor(showCursor)
---While mouse cursor is active, disable the camera rotations
forcamIndex,_inpairs(spec.camerasBackup) do
self.spec_enterable.cameras[camIndex].isRotatable=notshowCursorandspec.camerasBackup[camIndex]
end
end
--- Action event for entering a vehicle by mouse click
---@paramselftable vehicle
---@paramactionNamestring
---@paraminputValuenumber
---@paramcallbackStatenumber
---@paramisAnalogboolean
functionClickToSwitch.actionEventEnterVehicle(self, actionName, inputValue, callbackState, isAnalog)
ifself:isClickToSwitchMouseActive() then
localx,y=self:getClickToSwitchLastMousePosition()
self:enterVehicleRaycastClickToSwitch(x,y)
end
end
functionClickToSwitch.actionEventChangeAssignments(self, actionName, inputValue, callbackState, isAnalog)
ClickToSwitch.changeAssignments(self)
ClickToSwitchChangedAssignmentEvent.sendEvent(self)
end
functionClickToSwitch:onReadStream(streamId, connection)
localspec=self.spec_clickToSwitch
spec.assignmentMode=streamReadBool(streamId)
end
functionClickToSwitch:onWriteStream(streamId, connection)
localspec=self.spec_clickToSwitch
streamWriteBool(streamId, spec.assignmentMode)
end
functionClickToSwitch:changeAssignments()
localspec=self.spec_clickToSwitch
spec.assignmentMode=spec.assignmentMode==ClickToSwitch.DEFAULT_ASSIGNMENTandClickToSwitch.ADVANCED_ASSIGNMENT
orClickToSwitch.DEFAULT_ASSIGNMENT
end
--- Is the mouse visible/active
functionClickToSwitch:isClickToSwitchMouseActive()
returng_inputBinding:getShowMouseCursor()
end
--- Gets the last mouse cursor screen positions
---@returnnumber posX
---@returnnumber posY
functionClickToSwitch:getClickToSwitchLastMousePosition()
returng_inputBinding.mousePosXLast,g_inputBinding.mousePosYLast
end
--- Creates a raycast relative to the current camera and the mouse click
---@paramposXnumber
---@paramposYnumber
functionClickToSwitch:enterVehicleRaycastClickToSwitch(posX, posY)
localactiveCam=getCamera()
ifactiveCam~=nilthen
localhx, hy, hz, px, py, pz=RaycastUtil.getCameraPickingRay(posX, posY, activeCam)
raycastClosest(hx, hy, hz, px, py, pz, 1000, "enterVehicleRaycastCallbackClickToSwitch", self, CollisionFlag.VEHICLE)
end
end
--- Check and enters a vehicle.
---@paramhitObjectIdnumber
---@paramxnumber world x hit position
---@paramynumber world y hit position
---@paramznumber world z hit position
---@paramdistancenumber distance at which the cast hit the object
---@returnbool was the correct object hit?
functionClickToSwitch:enterVehicleRaycastCallbackClickToSwitch(hitObjectId, x, y, z, distance)
ifhitObjectId~=nilthen
localobject=g_currentMission.nodeToObject[hitObjectId]
ifobject~=nilthen
-- check if the object is a implement or trailer then get the rootVehicle
localrootVehicle=object.rootVehicle
localtargetObject=object.spec_enterableandobjectorrootVehicle~=nilandrootVehicle.spec_enterableandrootVehicle
iftargetObjectthen
iftargetObject~=g_currentMission.playerSystem:getLocalPlayer():getCurrentVehicle() then
-- this is a valid vehicle, so enter it
g_currentMission.playerSystem:getLocalPlayer():requestToEnterVehicle(targetObject)
ifself~=g_currentMission.playerSystem:getLocalPlayer():getCurrentVehicle() then
localspec=self.spec_clickToSwitch
ifspec.changedCamerathen
g_inputBinding:setShowMouseCursor(false)
spec.changedCamera=false
forcamIndex,_inpairs(spec.camerasBackup) do
self.spec_enterable.cameras[camIndex].isRotatable=spec.camerasBackup[camIndex]
end
end
end
end
returnfalse
end
end
end
returntrue
end
ClickToSwitchChangedAssignmentEvent= {}
localClickToSwitchChangedAssignmentEvent_mt=Class(ClickToSwitchChangedAssignmentEvent, Event)
InitEventClass(ClickToSwitchChangedAssignmentEvent, "ClickToSwitchChangedAssignmentEvent")
functionClickToSwitchChangedAssignmentEvent.emptyNew()
returnEvent.new(ClickToSwitchChangedAssignmentEvent_mt)
end
--- Creates a new Event
functionClickToSwitchChangedAssignmentEvent.new(vehicle)
localself=ClickToSwitchChangedAssignmentEvent.emptyNew()
self.vehicle=vehicle
returnself
end
--- Reads the serialized data on the receiving end of the event.
functionClickToSwitchChangedAssignmentEvent:readStream(streamId, connection) -- wird aufgerufen wenn mich ein Event erreicht
self.vehicle=NetworkUtil.readNodeObject(streamId)
self:run(connection);
end
--- Writes the serialized data from the sender.ClickToSwitchChangedAssignmentEvent
functionClickToSwitchChangedAssignmentEvent:writeStream(streamId, connection) -- Wird aufgrufen wenn ich ein event verschicke (merke: reihenfolge der Daten muss mit der bei readStream uebereinstimmen
NetworkUtil.writeNodeObject(streamId,self.vehicle)
end
--- Runs the event on the receiving end of the event.
functionClickToSwitchChangedAssignmentEvent:run(connection) -- wir fuehren das empfangene event aus
ifself.vehiclethen
localspec=self.vehicle.spec_clickToSwitch
ifspecthen
ClickToSwitch.changeAssignments(self.vehicle)
end
end
--- If the receiver was the client make sure every clients gets also updated.
ifnotconnection:getIsServer() then
g_server:broadcastEvent(ClickToSwitchChangedAssignmentEvent.new(self.vehicle), nil, connection, self.vehicle)
end
end
functionClickToSwitchChangedAssignmentEvent.sendEvent(vehicle)
ifg_server~=nilthen
g_server:broadcastEvent(ClickToSwitchChangedAssignmentEvent.new(vehicle), nil, nil, vehicle)
else
g_client:getServerConnection():sendEvent(ClickToSwitchChangedAssignmentEvent.new(vehicle))
end
end