forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.lua
More file actions
349 lines (310 loc) · 13.8 KB
/
input.lua
File metadata and controls
349 lines (310 loc) · 13.8 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
function courseplay:mouseEvent(posX, posY, isDown, isUp, button)
local mouseKey = button;
--RIGHT CLICK
if isDown and mouseKey == Input[courseplay.inputBindings.mouse.COURSEPLAY_MOUSEACTION_SECONDARY.keyName] 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)
courseplay:buttonsActiveEnabled(self, "all");
end;
end;
local hudGfx = courseplay.hud.visibleArea;
local mouseIsInHudArea = self.cp.mouseCursorActive and posX > hudGfx.x1 and posX < hudGfx.x2 and posY > hudGfx.y1 and posY < hudGfx.y2;
--LEFT CLICK
if isDown and mouseKey == Input[courseplay.inputBindings.mouse.COURSEPLAY_MOUSEACTION.keyName] and self.cp.mouseCursorActive and self.cp.hud.show and self.isEntered and mouseIsInHudArea then
local continueSearchingButton = true;
for _,button in pairs(self.cp.buttons.global) do
if button.show and courseplay:mouseIsInButtonArea(posX, posY, button) then
continueSearchingButton = false;
courseplay:handleMouseClickForButton(self, button);
break;
end;
end;
if continueSearchingButton then
for _,button in pairs(self.cp.buttons[tostring(self.cp.hud.currentPage)]) do
if button.canBeClicked and button.show and courseplay:mouseIsInButtonArea(posX, posY, button) then
continueSearchingButton = false;
courseplay:handleMouseClickForButton(self, button);
break;
end;
end;
end;
if continueSearchingButton then
if self.cp.hud.currentPage == 2 then
for _,button in pairs(self.cp.buttons["-2"]) do
if button.show and courseplay:mouseIsInButtonArea(posX, posY, button) then
courseplay:handleMouseClickForButton(self, button);
break;
end;
end;
end;
end;
--HOVER
elseif self.cp.mouseCursorActive and not isDown and self.cp.hud.show and self.isEntered then
for _,button in pairs(self.cp.buttons.global) do
button.isClicked = false;
if button.show and not button.isHidden then
button.isHovered = false;
if courseplay:mouseIsInButtonArea(posX, posY, button) then
button.isClicked = false;
button.isHovered = true;
end;
end;
end;
self.cp.hud.mouseWheel.render = false;
for _,button in pairs(self.cp.buttons[tostring(self.cp.hud.currentPage)]) do
button.isClicked = false;
if button.show and not button.isHidden then
button.isHovered = false;
if courseplay:mouseIsInButtonArea(posX, posY, button) then
button.isHovered = true;
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/1920, posY - 16/1080);
--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.function_to_call), tostring(upParameter)), 12);
self:setCourseplayFunc(button.function_to_call, 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.function_to_call), tostring(downParameter)), 12);
self:setCourseplayFunc(button.function_to_call, downParameter, false, button.page);
end;
end;
end;
if button.hoverText 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.isClicked = false;
if button.show and not button.isHidden then
button.isHovered = false;
if courseplay:mouseIsInButtonArea(posX, posY, button) then
button.isClicked = false;
button.isHovered = true;
end;
if button.hoverText then
self.cp.hud.content.pages[2][button.row][1].isHovered = button.isHovered;
end;
end;
end;
end;
end;
end; --END mouseEvent()
function courseplay:mouseIsInButtonArea(x, y, button)
return x > button.x and x < button.x2 and y > button.y and y < button.y2;
end;
function courseplay:handleMouseClickForButton(self, button)
local parameter = button.parameter;
if InputBinding.isPressed(InputBinding.COURSEPLAY_MODIFIER) and button.modifiedParameter ~= nil then --for some reason InputBinding works in :mouseEvent
courseplay:debug("button.modifiedParameter = " .. tostring(button.modifiedParameter), 12);
parameter = button.modifiedParameter;
end;
if button.show and not button.isHidden and button.canBeClicked and not button.isDisabled then
if button.function_to_call == "rowButton" and self.cp.hud.content.pages[self.cp.hud.currentPage][button.parameter][1].text == nil then
return;
end;
button.isClicked = true;
if button.function_to_call == "showSaveCourseForm" then
self.cp.imWriting = true
end
if button.function_to_call == "goToVehicle" then
courseplay:executeFunction(self, "goToVehicle", parameter)
else
self:setCourseplayFunc(button.function_to_call, parameter, false, button.page);
end
button.isClicked = 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
courseplay: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
playSample(courseplay.hud.clickSound, 1, 1, 0);
courseplay:debug(nameNum(self) .. ": calling function \"" .. tostring(func) .. "(" .. tostring(value) .. ")\"", 12);
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.attachedCombineIdx ~= nil and self.tippers ~= nil and self.tippers[self.cp.attachedCombineIdx] ~= nil then
combine = self.tippers[self.cp.attachedCombineIdx];
end;
if combine.cp.isCombine then
if line == 4 then
courseplay:toggleDriverPriority(combine);
end;
end;
if combine.courseplayers == nil or #(combine.courseplayers) == 0 then
if line == 1 then
courseplay:call_player(combine);
end;
else
if line == 2 then
courseplay:start_stop_player(combine);
elseif line == 3 then
courseplay:send_player_home(combine);
elseif line == 4 and combine.cp.isChopper then
courseplay:switch_player_side(combine);
elseif line == 5 and combine.cp.isChopper and not self.drive and not self.isAIThreshing 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.drive then
if line == 1 then
courseplay:start(self);
elseif line == 3 and self.cp.mode ~= 9 then
courseplay:setStartAtFirstPoint(self);
elseif line == 4 then
courseplay:reset_course(self);
end;
else -- driving
if line == 1 then
courseplay:stop(self);
elseif line == 2 and self.cp.last_recordnumber ~= nil and self.Waypoints[self.cp.last_recordnumber].wait and self.wait then
courseplay:driveOn(self);
elseif line == 2 and self.cp.stopAtEnd and (self.recordnumber == self.maxnumber or self.cp.currentTipTrigger ~= nil) then
courseplay:setStopAtEnd(self, false);
elseif line == 3 and not self.cp.isLoaded then
courseplay:setIsLoaded(self, true);
elseif line == 4 and not self.cp.stopAtEnd then
self.cp.stopAtEnd = true
elseif line == 5 then
if self.cp.mode == 1 or self.cp.mode == 2 then
self.cp.unloadAtSiloStart = not self.cp.unloadAtSiloStart;
elseif self.cp.mode == 4 and self.cp.hasSowingMachine then
self.cp.ridgeMarkersAutomatic = not self.cp.ridgeMarkersAutomatic;
elseif self.cp.mode == 6 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 == 1 or self.cp.mode == 2 or self.cp.mode == 5 or self.cp.mode == 6) then
self.cp.automaticCoverHandling = not self.cp.automaticCoverHandling;
end;
end;
end; -- end driving
elseif not self.drive then
if not self.cp.isRecording and not self.cp.recordingIsPaused and not self.cp.canDrive and #(self.Waypoints) == 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.drive
end; --END is page 0 or 1
end; --END isRowFunction
end;
function courseplay:keyEvent(unicode, sym, modifier, isDown)
end;
function courseplay:setInputBindings()
courseplay.inputBindings = {
keyboard = {};
mouse = {};
};
--MODIFIER
local modifierAction = InputBinding.actions[InputBinding.COURSEPLAY_MODIFIER];
courseplay.inputBindings.modifier = {
inputBinding = InputBinding.COURSEPLAY_MODIFIER;
actionIndex = modifierAction.actionIndex;
keyId = modifierAction.keys1[1];
isRealModifier = Input.keyIdIsModifier[modifierAction.keys1[1]];
keyName = Input.keyIdToIdName[modifierAction.keys1[1]];
displayName = KeyboardHelper.getKeyNames(modifierAction.keys1)
};
if not courseplay.inputBindings.modifier.isRealModifier then
print(string.format("Warning: Courseplay InputBinding modifier \"%s\" (%s) is not a real modifier. Conflicts with other mods may arise.", courseplay.inputBindings.modifier.keyName, courseplay.inputBindings.modifier.displayName));
end;
--KEYBOARD
local courseplayKeyboardInputs = { "COURSEPLAY_HUD", "COURSEPLAY_START_STOP", "COURSEPLAY_DRIVEON", "COURSEPLAY_DRIVENOW" };
for i,name in pairs(courseplayKeyboardInputs) do
courseplay:createNewCombinedInputBinding(name);
end;
--print(tableShow(courseplay.inputBindings.keyboard, "courseplay.inputBindings.keyboard"));
--MOUSE
local courseplayMouseInputs = { "COURSEPLAY_MOUSEACTION", "COURSEPLAY_MOUSEACTION_SECONDARY" };
for i,name in pairs(courseplayMouseInputs) do
local action = InputBinding.actions[InputBinding[name]];
local mouseButtonId = action.mouseButtons[1]; --can there be more than 1 mouseButton for 1 action?
if mouseButtonId == nil then
print(string.format("Warning: Courseplay InputBinding \"%s\" has no mouse button attached to it. Functionality will not be guaranteed.", name));
end;
courseplay.inputBindings.mouse[name] = {
name = name;
buttonId = mouseButtonId;
keyName = Input.mouseButtonIdToIdName[mouseButtonId];
displayName = g_i18n:getText("mouse") .. " " .. MouseHelper.getButtonNames(action.mouseButtons);
actionIndex = action.actionIndex;
inputBinding = InputBinding[name];
};
end;
--print(tableShow(courseplay.inputBindings.mouse, "courseplay.inputBindings.mouse"));
end;
function courseplay:createNewCombinedInputBinding(name)
local originalAction = InputBinding.actions[InputBinding[name]];
if #(originalAction.keys1) == 1 then
local action = courseplay.utils.table.copy(originalAction);
local actionIndex = #(InputBinding.actions) + 1;
action.name = name .. "_COMBINED";
local keyNameList = courseplay.inputBindings.modifier.keyName .. " " .. Input.keyIdToIdName[originalAction.keys1[1]];
action.keys1 = InputBinding.loadKeyList(keyNameList, action.name);
action.keys1Set = Utils.listToSet(action.keys1);
action.actionIndex = actionIndex;
InputBinding[action.name] = actionIndex;
table.insert(InputBinding.actions, action);
courseplay.inputBindings.keyboard[action.name] = {
originalKeyActionName = name;
originalKeyInputBinding = InputBinding[name];
originalModifierInputBinding = courseplay.inputBindings.modifier.inputBinding;
actionIndex = actionIndex;
displayName = courseplay.inputBindings.modifier.displayName .. " + " .. KeyboardHelper.getKeyNames(originalAction.keys1);
};
if g_i18n:hasText(name) then
local text = g_i18n:getText(name) .. " (COMBINED)";
g_i18n:setText(action.name, text);
g_i18n.globalI18N.texts[action.name] = text;
-- print(string.format("CP: set newly created inputbinding text for \"%s\" to \"%s\"", tostring(action.name), tostring(text)));
end;
end;
end;