forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.lua
390 lines (344 loc) · 15.4 KB
/
input.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
function courseplay:mouseEvent(posX, posY, isDown, isUp, mouseButton)
--RIGHT CLICK
if isUp and mouseButton == courseplay.inputBindings.mouse.secondaryButtonId and self.isEntered then
if self.cp.hud.show then
courseplay:setMouseCursor(self, not self.cp.mouseCursorActive);
elseif not self.cp.hud.show and self.cp.hud.openWithMouse then
courseplay:openCloseHud(self, true)
end;
end;
local hudGfx = courseplay.hud.visibleArea;
local mouseIsInHudArea = self.cp.mouseCursorActive and courseplay:mouseIsInArea(posX, posY, hudGfx.x1, hudGfx.x2, hudGfx.y1, self.cp.suc.active and courseplay.hud.suc.visibleArea.y2 or hudGfx.y2);
-- if not mouseIsInHudArea then return; end;
--LEFT CLICK
if (isDown or isUp) and mouseButton == courseplay.inputBindings.mouse.primaryButtonId and self.cp.mouseCursorActive and self.cp.hud.show and self.isEntered and mouseIsInHudArea then
local buttonToHandle;
if self.cp.suc.active then
for _,button in pairs(self.cp.buttons.suc) do
if button.show and button:getHasMouse(posX, posY) then
buttonToHandle = button;
break;
end;
end;
end;
if buttonToHandle == nil then
for _,button in pairs(self.cp.buttons.global) do
if button.show and button:getHasMouse(posX, posY) then
buttonToHandle = button;
break;
end;
end;
end;
if buttonToHandle == nil then
for _,button in pairs(self.cp.buttons[self.cp.hud.currentPage]) do
if button.canBeClicked and button.show and button:getHasMouse(posX, posY) then
buttonToHandle = button;
break;
end;
end;
end;
if buttonToHandle == nil then
if self.cp.hud.currentPage == 2 then
for _,button in pairs(self.cp.buttons[-2]) do
if button.show and button:getHasMouse(posX, posY) then
buttonToHandle = button;
break;
end;
end;
end;
end;
if buttonToHandle then
buttonToHandle:setClicked(isDown);
if not buttonToHandle.isDisabled and buttonToHandle.hoverText and buttonToHandle.functionToCall ~= nil then
self.cp.hud.content.pages[buttonToHandle.page][buttonToHandle.row][1].isClicked = isDown;
end;
if isUp then
buttonToHandle:handleMouseClick();
end;
return;
end;
--HOVER
elseif self.cp.mouseCursorActive and not isDown and self.cp.hud.show and self.isEntered then
-- local currentHoveredButton;
if self.cp.suc.active then
for _,button in pairs(self.cp.buttons.suc) do
if button.show and not button.isHidden then
button:setClicked(false);
button:setHovered(button:getHasMouse(posX, posY));
end;
end;
end;
for _,button in pairs(self.cp.buttons.global) do
button:setClicked(false);
if button.show and not button.isHidden then
button:setClicked(false);
button:setHovered(button:getHasMouse(posX, posY));
end;
end;
self.cp.hud.mouseWheel.render = false;
for _,button in pairs(self.cp.buttons[self.cp.hud.currentPage]) do
button:setClicked(false);
if button.show and not button.isHidden then
button:setHovered(button:getHasMouse(posX, posY));
if button.isHovered then
if button.isMouseWheelArea and (button.canScrollUp or button.canScrollDown) then
--Mouse wheel icon
self.cp.hud.mouseWheel.render = true;
self.cp.hud.mouseWheel.icon:setPosition(posX + 3/g_screenWidth, posY - 16/g_screenHeight);
--action
local parameter = button.parameter;
if InputBinding.isPressed(InputBinding.COURSEPLAY_MODIFIER) 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(self), tostring(button.functionToCall), tostring(upParameter)), 18);
self:setCourseplayFunc(button.functionToCall, upParameter, false, button.page);
elseif Input.isMouseButtonPressed(Input.MOUSE_BUTTON_WHEEL_DOWN) and button.canScrollDown then
courseplay:debug(string.format("%s: MOUSE_BUTTON_WHEEL_DOWN: %s(%s)", nameNum(self), tostring(button.functionToCall), tostring(downParameter)), 18);
self:setCourseplayFunc(button.functionToCall, downParameter, false, button.page);
end;
end;
end;
if button.hoverText and not button.isDisabled then
self.cp.hud.content.pages[button.page][button.row][1].isHovered = button.isHovered;
end;
end;
end;
if self.cp.hud.currentPage == 2 then
for _,button in pairs(self.cp.buttons[-2]) do
button:setClicked(false);
if button.show and not button.isHidden then
button:setHovered(button:getHasMouse(posX, posY));
if button.hoverText then
self.cp.hud.content.pages[2][button.row][1].isHovered = button.isHovered;
end;
end;
end;
end;
end;
-- ##################################################
-- 2D COURSE WINDOW: DRAG + DROP MOVE
if self.cp.course2dDrawData and (self.cp.drawCourseMode == courseplay.COURSE_2D_DISPLAY_2DONLY or self.cp.drawCourseMode == courseplay.COURSE_2D_DISPLAY_BOTH) then
local plot = CpManager.course2dPlotField;
if isDown and mouseButton == courseplay.inputBindings.mouse.primaryButtonId and self.cp.mouseCursorActive and self.isEntered and courseplay:mouseIsInArea(posX, posY, plot.x, plot.x + plot.width, plot.y, plot.y + plot.height) then
CpManager.course2dDragDropMouseDown = { posX, posY };
if self.cp.course2dPdaMapOverlay then
self.cp.course2dPdaMapOverlay.origPos = { self.cp.course2dPdaMapOverlay.x, self.cp.course2dPdaMapOverlay.y };
else
self.cp.course2dBackground.origPos = { self.cp.course2dBackground.x, self.cp.course2dBackground.y };
end;
elseif isUp and CpManager.course2dDragDropMouseDown ~= nil then
courseplay.utils:move2dCoursePlotField(self, posX, posY);
elseif not isUp and not isDown and CpManager.course2dDragDropMouseDown ~= nil then
courseplay.utils:update2dCourseBackgroundPos(self, posX, posY);
end;
end;
end; --END mouseEvent()
function courseplay:mouseIsInArea(mouseX, mouseY, areaX1, areaX2, areaY1, areaY2)
return mouseX >= areaX1 and mouseX <= areaX2 and mouseY >= areaY1 and mouseY <= areaY2;
end;
function courseplay.button:handleMouseClick(vehicle)
vehicle = vehicle or self.vehicle;
local parameter = self.parameter;
if InputBinding.isPressed(InputBinding.COURSEPLAY_MODIFIER) and self.modifiedParameter ~= nil then --for some reason InputBinding works in :mouseEvent
courseplay:debug("self.modifiedParameter = " .. tostring(self.modifiedParameter), 18);
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
if self.functionToCall == "goToVehicle" then
courseplay:executeFunction(vehicle, "goToVehicle", parameter)
else
vehicle:setCourseplayFunc(self.functionToCall, parameter, false, self.page);
end
-- self:setClicked(false);
end;
end;
function courseplay:setCourseplayFunc(func, value, noEventSend, page)
--print(string.format("courseplay:setCourseplayFunc( %s, %s, %s, %s)",tostring(func), tostring(value), tostring(noEventSend), tostring(page)))
if noEventSend ~= true then
CourseplayEvent.sendEvent(self, func, value,noEventSend,page); -- Die Funktion ruft sendEvent auf und übergibt 3 Werte (self "also mein ID", action, "Ist eine Zahl an der ich festmache welches Fenster ich aufmachen will", state "Ist der eigentliche Wert also true oder false"
end;
if value == "nil" then
value = nil
end
courseplay:executeFunction(self, func, value, page);
if page and self.cp.hud.reloadPage[page] ~= nil then
courseplay.hud:setReloadPageOrder(self, page, true);
end;
end
function courseplay:executeFunction(self, func, value, page)
if func == "setMPGlobalInfoText" then
CpManager:setGlobalInfoText(self, value, page)
courseplay:debug(" setting infoText: "..value..", force remove: "..tostring(page),5)
return
elseif Utils.startsWith(func,"self") or Utils.startsWith(func,"courseplay") then
courseplay:debug(" setting value",5)
courseplay:setVarValueFromString(self, func, value)
--courseplay:debug(" "..tostring(func)..": "..tostring(value),5)
return
end
if self.isEntered then
playSample(courseplay.hud.clickSound, 1, 1, 0);
end
courseplay:debug(('%s: calling function "%s(%s)"'):format(nameNum(self), tostring(func), tostring(value)), 18);
if func ~= "rowButton" then
--@source: http://stackoverflow.com/questions/1791234/lua-call-function-from-a-string-with-function-name
assert(loadstring('courseplay:' .. func .. '(...)'))(self, value);
else
local page = Utils.getNoNil(page, self.cp.hud.currentPage);
local line = value;
if page == 0 then
local combine = self;
if self.cp.attachedCombine ~= nil then
combine = self.cp.attachedCombine;
end;
if not combine.cp.isChopper then
if line == 4 then
courseplay:toggleDriverPriority(combine);
elseif line == 5 and self.cp.mode == courseplay.MODE_FIELDWORK then
courseplay:toggleStopWhenUnloading(combine);
end;
end;
if combine.courseplayers == nil or #(combine.courseplayers) == 0 then
if line == 1 then
courseplay:toggleWantsCourseplayer(combine);
end;
else
if line == 2 then
courseplay:startStopCourseplayer(combine);
elseif line == 3 then
courseplay:sendCourseplayerHome(combine);
elseif line == 4 and combine.cp.isChopper then
courseplay:switchCourseplayerSide(combine);
elseif line == 5 and combine.cp.isChopper and not self:getIsCourseplayDriving() and not self.aiIsStarted then --manual chopping: initiate/end turning maneuver
if self.cp.turnStage == 0 then
self.cp.turnStage = 1;
elseif self.cp.turnStage == 1 then
self.cp.turnStage = 0;
end;
end;
end;
elseif page == 1 then
if self.cp.canDrive then
if not self:getIsCourseplayDriving() then
if line == 1 then
courseplay:start(self);
elseif line == 3 and self.cp.mode ~= 9 then
courseplay:changeStartAtPoint(self);
elseif line == 4 then
courseplay:getPipesRotation(self);
elseif line == 5 then
courseplay:toggleFertilizeOption(self);
end;
else -- driving
if line == 1 then
courseplay:stop(self);
elseif line == 2 and self.cp.HUD1wait then
if self.cp.stopAtEnd and (self.cp.waypointIndex == self.cp.numWaypoints or self.cp.currentTipTrigger ~= nil) then
courseplay:setStopAtEnd(self, false);
else
courseplay:cancelWait(self);
end;
elseif line == 3 and not self.cp.isLoaded then
courseplay:setIsLoaded(self, true);
elseif line == 4 then
courseplay:setStopAtEnd(self, not self.cp.stopAtEnd);
elseif line == 5 then
if self.cp.mode == courseplay.MODE_SEED_FERTILIZE and self.cp.hasSowingMachine then
self.cp.ridgeMarkersAutomatic = not self.cp.ridgeMarkersAutomatic;
elseif self.cp.mode == courseplay.MODE_FIELDWORK and self.cp.hasBaleLoader and not self.hasUnloadingRefillingCourse then
self.cp.automaticUnloadingOnField = not self.cp.automaticUnloadingOnField;
end;
elseif line == 6 then
if self.cp.tipperHasCover and (self.cp.mode == courseplay.MODE_GRAIN_TRANSPORT or self.cp.mode == courseplay.MODE_COMBI or self.cp.mode == courseplay.MODE_TRANSPORT or self.cp.mode == courseplay.MODE_FIELDWORK) then
self.cp.automaticCoverHandling = not self.cp.automaticCoverHandling;
end;
elseif line == 7 then
self.cp.turnOnField = not self.cp.turnOnField;
elseif line == 8 then
self.cp.oppositeTurnMode = not self.cp.oppositeTurnMode;
end;
end; -- end driving
if line == 5 then
courseplay:toggleConvoyActive(self)
end
elseif not self:getIsCourseplayDriving() then
if not self.cp.isRecording and not self.cp.recordingIsPaused and not self.cp.canDrive and self.cp.numWaypoints == 0 then
if line == 1 then
courseplay:start_record(self);
elseif line == 3 then
courseplay:setCustomSingleFieldEdge(self);
elseif line == 5 and self.cp.fieldEdge.customField.fieldNum > 0 then
courseplay:addCustomSingleFieldEdgeToList(self);
end;
end;
end; --END if not self:getIsCourseplayDriving()
elseif page == 10 then
if line == 1 and not self:getIsCourseplayDriving() then
courseplay:toggleMode10Mode(self)
elseif line == 2 then
courseplay:toggleMode10SearchMode(self)
elseif line == 5 then
courseplay:toggleMode10automaticSpeed(self)
elseif line == 6 then
if self.cp.mode10.leveling then
courseplay:toggleMode10AutomaticHeight(self)
end
elseif line == 7 then
courseplay:toggleMode10drivingThroughtLoading(self)
end
end; --END is page 0 or 1 or 3 or 10
end; --END isRowFunction
end;
function courseplay:keyEvent(unicode, sym, modifier, isDown) end;
courseplay.inputBindings = {};
courseplay.inputBindings.mouse = {};
courseplay.inputBindings.mouse.mouseButtonOverlays = {
MOUSE_BUTTON_NONE = 'mouseNMB.png',
MOUSE_BUTTON_LEFT = 'mouseLMB.png',
MOUSE_BUTTON_RIGHT = 'mouseRMB.png',
MOUSE_BUTTON_MIDDLE = 'mouseMMB.png',
MOUSE_BUTTON_LEFTRIGHT = 'mouseBMB.png'
};
courseplay.inputBindings.keyboard = {};
function courseplay.inputBindings.updateInputButtonData()
-- print('updateInputButtonData()')
-- MOUSE
for _,type in ipairs( { 'primary', 'secondary' } ) do
local inputName = 'COURSEPLAY_MOUSEACTION_' .. type:upper();
local action = InputBinding.actions[ InputBinding[inputName] ];
local mouseButtonId = action.mouseButtons[1]; -- can there be more than 1 mouseButton for 1 action?
-- print(('\t%s: inputName=%q'):format(type, inputName));
local txt = ('%s %s'):format(g_i18n:getText('ui_mouse'), MouseHelper.getButtonNames(action.mouseButtons)); -- TODO (Jakob): getButtonNames returns English, not i18n text
courseplay.inputBindings.mouse[type .. 'TextI18n'] = txt;
courseplay.inputBindings.mouse[type .. 'ButtonId'] = mouseButtonId;
-- print(('\t\t%sTextI18n=%q, mouseButtonId=%d'):format(type, txt, mouseButtonId));
if type == 'secondary' then
local mouseButtonIdName = Input.mouseButtonIdToIdName[mouseButtonId];
local fileName = courseplay.inputBindings.mouse.mouseButtonOverlays[mouseButtonIdName] or 'mouseRMB.png';
--print(('\t\tmouseButtonIdName=%q, fileName=%q'):format(tostring(mouseButtonIdName), tostring(fileName)));
if courseplay.inputBindings.mouse.overlaySecondary then
courseplay.inputBindings.mouse.overlaySecondary:delete();
end;
courseplay.inputBindings.mouse.overlaySecondary = Overlay:new('cpMouseIPB', courseplay.path .. 'img/mouseIcons/' .. fileName, 0, 0, 0.0, 0.0);
end;
end;
-- KEYBOARD
-- open/close hud (combined with modifier): get i18n text
local modifierAction = InputBinding.actions[InputBinding.COURSEPLAY_MODIFIER];
local modifierTextI18n = KeyboardHelper.getKeyNames(modifierAction.keys1);
local openCloseHudAction = InputBinding.actions[InputBinding.COURSEPLAY_HUD];
local openCloseHudTextI18n = KeyboardHelper.getKeyNames(openCloseHudAction.keys1);
courseplay.inputBindings.keyboard.openCloseHudTextI18n = ('%s + %s'):format(modifierTextI18n, openCloseHudTextI18n);
-- print(('\topenCloseHudTextI18n=%q'):format(courseplay.inputBindings.keyboard.openCloseHudTextI18n));
end;
InputBinding.storeBindings = Utils.appendedFunction(InputBinding.storeBindings, courseplay.inputBindings.updateInputButtonData);