forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clicktoswitch.lua
144 lines (119 loc) · 6.65 KB
/
clicktoswitch.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
--[[
AUTHOR
Russ Beuker, January 2020
CLICK TO SWITCH
This was developed as a way to quickly jump from your current vehicle to another vehicle by mouse clicking on the vehicle on-screen.
This can provide a quicker way of getting into a vehicle which you can see from your current position rather that cycling through your vehicles with a hotkey.
You'll need to enable this in the hud advanced settings page.
CAVEATS
1) This has not been tested on multiplayer.
2) Works with courseplay .00358Dev (didn't try other versions)
DEV NOTES
This file is intended to be included with CoursePlay. See the bottom of this file for instructions on what to modify in other files.
Most of this will be handled in a merge, but the instructions contain explanations on why we are making the changes.
NOTES
1) The vehicle must be visible and within range (1000 meters, which is pretty well a pixel on the horizon) to be able to switch to it.
2) You must click on the power unit. Ex. For a tractor with a trailer, you must click on the tractor. Clicking on the trailer won't do anything.
3) The only input used is the 'Courseplay: mouse action' hotkey. No other configuration or hotkeys are required.
TEST SCRIPT
1) Enter a vehicle and bring up the Courseplay HUD advanced settings page. Turn off the Click to switch setting.
2) While in a vehicle, turn to face another vehicle that you have permission to enter.
3) Click on the other vehicle. You should switch to that vehicle.
4) Now click on the original vehicle. You should switch back to that vehicle.
5) Try clicking on other things (other vehicles, trailers, trees, ground etc). Nothing should happen.
6) Save the game and quit.
7) Load the game and try clicking to switch. It should work.
8) Disable the Click to switch option.
9) Save the game and quit.
10) Load the game and try clicking to switch. It should not work.
]]--
clickToSwitch = {}
-- let's find out if a vehicle is under the cursor by casting a ray in that direction
function clickToSwitch:updateMouseState(vehicle, posX, posY, isDown, isUp, mouseButton)
local activeCam = getCamera()
if activeCam ~= nil then
local hx, hy, hz, px, py, pz = RaycastUtil.getCameraPickingRay(posX, posY, activeCam)
raycastClosest(hx, hy, hz, px, py, pz, "vehicleClickToSwitchRaycastCallback", 1000, self, 371)
end
end
-- this is called when the ray hits something
function clickToSwitch:vehicleClickToSwitchRaycastCallback(hitObjectId, x, y, z, distance)
if hitObjectId ~= nil then
local objectType = getRigidBodyType(hitObjectId)
if objectType ~= "Kinematic" then
local object = g_currentMission:getNodeObject(hitObjectId)
if object ~= nil then
-- this is a valid vehicle, so enter it
g_currentMission:requestToEnterVehicle(object);
end
return false
end
end
return true
end
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-- CODE MERGING NOTES
-----------------------------------------------------------------------------------------
--[[
-- make the following change to input.lua (to
function courseplay:onMouseEvent(posX, posY, isDown, isUp, mouseButton)
--RIGHT CLICK
-- Input binding debug
local vehicle = g_currentMission.controlledVehicle
if not vehicle or not vehicle.hasCourseplaySpec then return end
courseEditor:updateMouseState(vehicle, posX, posY, isDown, isUp, mouseButton)
--print(string.format('courseplay:mouseEvent(posX(%s), posY(%s), isDown(%s), isUp(%s), mouseButton(%s))', tostring(posX), tostring(posY), tostring(isDown), tostring(isUp), tostring(mouseButton) ))
--print(string.format("if isUp(%s) and mouseButton(%s) == courseplay.inputBindings.mouse.secondaryButtonId(%s) and Enterable.getIsEntered(self)(%s) then"
--,tostring(isUp),tostring(mouseButton),tostring(courseplay.inputBindings.mouse.secondaryButtonId),tostring(Enterable.getIsEntered(self))))
if isUp and mouseButton == courseplay.inputBindings.mouse.secondaryButtonId and vehicle:getIsEntered() then
if vehicle.cp.hud.show then
courseplay:setMouseCursor(vehicle, not vehicle.cp.mouseCursorActive);
elseif not vehicle.cp.hud.show and vehicle.cp.hud.openWithMouse then
courseplay:openCloseHud(vehicle, true)
end;
end;
local hudGfx = courseplay.hud.visibleArea;
local mouseIsInHudArea = vehicle.cp.mouseCursorActive and courseplay:mouseIsInArea(posX, posY, hudGfx.x1, hudGfx.x2, hudGfx.y1, vehicle.cp.suc.active and courseplay.hud.suc.visibleArea.y2 or hudGfx.y2);
-- if not mouseIsInHudArea then return; end;
-- should we switch vehicles? -- added for clickToSwitch
if courseplay.globalSettings.clickToSwitch:is(true) and vehicle.cp.mouseCursorActive and vehicle.cp.hud.show and vehicle:getIsEntered() and not mouseIsInHudArea and -- added for clickToSwitch
mouseButton == courseplay.inputBindings.mouse.primaryButtonId then -- added for clickToSwitch
clickToSwitch:updateMouseState(vehicle, posX, posY, isDown, isUp, mouseButton) -- added for clickToSwitch
end -- added for clickToSwitch
.
.
----------------------------
In settings.lua add the following just after the 'AutoFieldScanSetting' block (to define the global setting to turn this on or off)
---@class ClickToSwitchSetting : BooleanSetting
ClickToSwitchSetting = CpObject(BooleanSetting)
function ClickToSwitchSetting:init()
BooleanSetting.init(self, 'clickToSwitch', 'COURSEPLAY_CLICK_TO_SWITCH',
'COURSEPLAY_YES_NO_CLICK_TO_SWITCH', nil)
-- set default while we are transitioning from the the old setting to this new one
self:set(false)
end
----------------------------
In GlobalSettingsPage.xml add the following just after the 'workerWages' block (to define the on/off gui switch for this feature)
.
.
<GuiElement type="multiTextOption" profile="multiTextOptionSettings" onCreate="onCreateGlobalSettingsPage" name="clickToSwitch" toolTipElementId="ingameMenuHelpBoxText">
<GuiElement type="button" profile="multiTextOptionSettingsLeft" />
<GuiElement type="button" profile="multiTextOptionSettingsRight" />
<GuiElement type="text" profile="multiTextOptionSettingsText" />
<GuiElement type="text" profile="multiTextOptionSettingsTitle" />
<GuiElement type="bitmap" profile="multiTextOptionSettingsBg" />
</GuiElement>
.
.
----------------------------
In translation_en.xml add
.
.
<text name="COURSEPLAY_EARN_WAGES" text="Courseplay workers earn wages" />
<text name="COURSEPLAY_CLICK_TO_SWITCH" text="Click to switch" /> -- add for clicktoswitch
<text name="COURSEPLAY_YES_NO_CLICK_TO_SWITCH" text="Click on a vehicle to switch to it" /> -- add for clicktoswitch
.
.
do this for the other translation languages too.
]]--