forked from zxbc/BAR_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_capslock_camera_pan.lua
201 lines (173 loc) · 6.42 KB
/
gui_capslock_camera_pan.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
function widget:GetInfo()
return {
name = "Capslock SmoothScroll",
desc = "Use capslock to pan camera in the direction of mouse cursor. Can set param in file to override speed.",
author = "Errrrrrr, [DE]LSR, original implementation by trepan",
date = "June 2023",
version = "1.0",
license = "GNU GPL, v2 or later",
layer = 1,
enabled = true,
handler = true,
}
end
-------------------------------------------------------------------------------------------------------------
-- New implementation by Errrrrrr
-- Updated features:
-- 1) Capslock is now the default key to activate the camera pan
-- 2) Hold down capslock to pan the camera in the direction of mouse cursor
-- 3) Camera pan speed is now customizable
-- 4) Camera pan speed is now affected by camera height (so that camera pan speed is the same at all heights)
-------------------------------------------------------------------------------------------------------------
local override_speed_factor = 0 -- set to 0 to disable override
local spGetCameraState = Spring.GetCameraState
local spGetCameraVectors = Spring.GetCameraVectors
local spGetModKeyState = Spring.GetModKeyState
local spGetMouseState = Spring.GetMouseState
local spIsAboveMiniMap = Spring.IsAboveMiniMap
local spSendCommands = Spring.SendCommands
local spSetCameraState = Spring.SetCameraState
local spSetMouseCursor = Spring.SetMouseCursor
local spWarpMouse = Spring.WarpMouse
local blockModeSwitching = true
local vsx, vsy = widgetHandler:GetViewSizes()
function widget:ViewResize(viewSizeX, viewSizeY)
vsx = viewSizeX
vsy = viewSizeY
end
local mx, my
local active = false
local drawing = false
function widget:Update(dt)
if (active) then
local speedFactor = Spring.GetConfigInt('OverheadScrollSpeed', 10)
if override_speed_factor > 0 then
speedFactor = override_speed_factor
end
local x, y, lmb, mmb, rmb = spGetMouseState()
local cs = spGetCameraState()
local speed = dt * speedFactor
local curHeight = minHeight
if (cs.name == "ta") then
curHeight = cs.height
elseif (cs.name == "spring") then
curHeight = cs.dist
end
speed = speed * (curHeight / 10000)
if (cs.name == 'free') then
local a, c, m, s = spGetModKeyState()
if (c) then
return
end
-- clear the velocities
cs.vx = 0; cs.vy = 0; cs.vz = 0
cs.avx = 0; cs.avy = 0; cs.avz = 0
end
if (cs.name == 'ta') then
local flip = -cs.flipped
-- simple, forward and right are locked
cs.px = cs.px + (speed * flip * (x - mx))
if (false) then
cs.py = cs.py + (speed * flip * (my - y))
else
cs.pz = cs.pz + (speed * flip * (my - y))
end
else
-- forward, up, right, top, bottom, left, right
local camVecs = spGetCameraVectors()
local cf = camVecs.forward
local len = math.sqrt((cf[1] * cf[1]) + (cf[3] * cf[3]))
local dfx = cf[1] / len
local dfz = cf[3] / len
local cr = camVecs.right
local len = math.sqrt((cr[1] * cr[1]) + (cr[3] * cr[3]))
local drx = cr[1] / len
local drz = cr[3] / len
local mxm = (speed * (x - mx))
local mym = (speed * (y - my))
cs.px = cs.px + (mxm * drx) + (mym * dfx)
cs.pz = cs.pz + (mxm * drz) + (mym * dfz)
end
spSetCameraState(cs, 0)
if (mmb) then
spSetMouseCursor('none')
end
end
end
function widget:KeyPress(key, mods, isRepeat)
if key ~= 301 or isRepeat then return end
local cs = spGetCameraState()
if (blockModeSwitching and (cs.name ~= 'ta') and (cs.name ~= 'spring')) then
local a, c, m, s = spGetModKeyState()
return (c or s) -- block the mode toggling
end
if (cs.name == 'free') then
local a, c, m, s = spGetModKeyState()
if (m and (not (c or s))) then
return false
end
end
active = not active
if (active) then
mx = vsx * 0.5
my = vsy * 0.5
spWarpMouse(mx, my)
spSendCommands({ 'trackoff' })
end
return true
end
function widget:KeyRelease(key, mods, isRepeat)
if key ~= 301 then return end
active = false
return -1
end
-- Adjust the camera position when the user scrolls the mouse wheel
function widget:MouseWheel(up, value)
-- Get the current camera state and mod key state
local cameraState = Spring.GetCameraState()
local altDown, ctrlDown, metaDown, shiftDown = Spring.GetModKeyState()
-- If the Alt key is down, adjust the camera height
if altDown then
local absCameraY = math.abs(cameraState.py)
local cameraYDelta = (absCameraY / 10) * (up and -1 or 1)
local newCameraState = {
py = absCameraY + cameraYDelta
}
Spring.SetCameraState(newCameraState, Spring.GetConfigFloat("CameraTransitionTime",1.0))
return true
end
-- If the camera mode is not "free", do nothing
if cameraState.name ~= 'free' then
return false
end
-- Get the mouse position and position on ground
local mouseX, mouseY = Spring.GetMouseState()
local _, groundPos = Spring.TraceScreenRay(mouseX, mouseY, true)
-- If there is no ground position, adjust the camera vertically
if not groundPos then
local cameraYDelta = value * 10
Spring.SetCameraState({
vy = cameraState.vy + cameraYDelta
}, 0)
else
-- Otherwise, adjust the camera position based on the ground position
local dx = groundPos[1] - cameraState.px
local dy = groundPos[2] - cameraState.py
local dz = groundPos[3] - cameraState.pz
-- local distance = math.sqrt((dx * dx) + (dy * dy) + (dz * dz))
local speed = (up and 1 or -1) * (1 / 8)
dx = dx * speed
dy = dy * speed
dz = dz * speed
local newCameraState = {
px = cameraState.px + dx,
py = cameraState.py + dy,
pz = cameraState.pz + dz,
vx = 0,
vy = 0,
vz = 0
}
Spring.SetCameraState(newCameraState, Spring.GetConfigFloat("CameraTransitionTime",1.0))
end
return true
end