This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputCourseNameDialogue.lua
305 lines (253 loc) · 12.2 KB
/
inputCourseNameDialogue.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
--[[
@name: inputCourseNameDialogue
@desc: Dialogue settings for the Courseplay course saving form
@author: Jakob Tischler
@version: 1.5
@date: 31 Oct 2014
--]]
local modDir = g_currentModDirectory;
inputCourseNameDialogue = {};
inputCourseNameDialogue.firstTimeRun = true;
local inputCourseNameDialogue_mt = Class(inputCourseNameDialogue)
inputCourseNameDialogue.types = { "course", "folder", "filter" };
function inputCourseNameDialogue:new()
local instance = {};
instance = setmetatable(instance, inputCourseNameDialogue_mt);
return instance;
end; --END new()
local elementOverlayExists = function(element)
return element.overlay ~= nil and element.overlay.overlay ~= nil and element.overlay.overlay ~= 0;
end;
function inputCourseNameDialogue:setImageOverlay(element, filePath, type)
-- print(('\t\tsetImageOverlay(): element=%q, filePath=%q, type=%q'):format(tostring(element), tostring(filePath), tostring(type)));
if type == nil then
if elementOverlayExists(element) then
delete(element.overlay.overlay);
end;
element.overlay.overlay = createImageOverlay(filePath);
element.overlay.filePath = filePath;
else
type = type:sub(2);
if element.courseplayTypes == nil then
element.courseplayTypes = {};
end;
if element.courseplayTypes[type] == nil then
element.courseplayTypes[type] = {};
end;
element.courseplayTypes[type].overlayId = createImageOverlay(filePath);
element.courseplayTypes[type].filePath = filePath;
if type == 'course' then -- set the default overlay - ONLY DO THIS ONCE (for the course)
if elementOverlayExists(element) then
delete(element.overlay.overlay);
end;
element.overlay.overlay = element.courseplayTypes[type].overlayId;
element.overlay.filePath = element.courseplayTypes[type].filePath;
end;
-- print(tableShow(element, 'element "' .. filePath .. '"'));
end;
end;
function inputCourseNameDialogue.setModImages(element, xmlFile, key)
element.modImgDir = modDir .. (getXMLString(xmlFile, key .. "#MOD_imageDir") or "");
local fileNames = getXMLString(xmlFile, key .. '#MOD_imageFilename');
if fileNames ~= nil then
local split = Utils.splitString(",", fileNames);
if #split == 1 then
inputCourseNameDialogue:setImageOverlay(element, element.modImgDir .. fileNames);
elseif #split == #inputCourseNameDialogue.types then
for _,data in pairs(split) do
local kv = Utils.splitString(":", data);
local type, filePath = unpack(kv);
local realFilePath = filePath;
if not Utils.startsWith(filePath, "$") then
realFilePath = element.modImgDir .. filePath;
end;
inputCourseNameDialogue:setImageOverlay(element, realFilePath, type);
end;
else
--ERROR
end;
end;
end; --END setModImages()
BitmapElement.loadFromXML = Utils.appendedFunction(BitmapElement.loadFromXML, inputCourseNameDialogue.setModImages);
-- TextInputElement.loadFromXML = Utils.appendedFunction(TextInputElement.loadFromXML, inputCourseNameDialogue.setModImages);
ButtonElement.loadFromXML = Utils.appendedFunction(ButtonElement.loadFromXML, inputCourseNameDialogue.setModImages);
function inputCourseNameDialogue:onCreateTitleText(element)
self.titleTextElement = element;
end; --END onCreateTitleText()
function inputCourseNameDialogue:onCreateSaveText(element)
self.saveTextElement = element;
end; --END onCreateSaveButton()
function inputCourseNameDialogue:onCreateCancelText(element)
self.cancelTextElement = element;
end; --END onCreateCancelButton()
function inputCourseNameDialogue:onCreateTextInput(element)
self.textInputElement = element;
end; --END onCreateTextInput()
function inputCourseNameDialogue:onOpen(element)
g_currentMission.isPlayerFrozen = true;
InputBinding.setShowMouseCursor(true);
if self.firstTimeRun then
--SET GET TITLE TEXT
if self.titleTextElement.courseText == nil or self.titleTextElement.folderText == nil or self.titleTextElement.filterText == nil then
local cpTitleParts = Utils.splitString(",", self.titleTextElement.text);
local courseTitle = string.sub(cpTitleParts[1], 5);
local folderTitle = string.sub(cpTitleParts[2], 5);
local filterTitle = string.sub(cpTitleParts[3], 5);
self.titleTextElement.courseText = courseplay.locales[courseTitle] or "Course name:";
self.titleTextElement.folderText = courseplay.locales[folderTitle] or "Folder name:";
self.titleTextElement.filterText = courseplay.locales[filterTitle] or "Filter courses:";
end;
--SET CANCLE TEXT
local cancelText = string.sub(self.cancelTextElement.text, 5);
self.cancelTextElement.text = courseplay.locales[cancelText]:format(courseplay.locales["COURSEPLAY_BUTTON_ESC"]) or "Cancel with Escape";
if self.saveTextElement.courseText == nil or self.saveTextElement.folderText == nil or self.saveTextElement.filterText == nil then
local cpSaveTextParts = Utils.splitString(",", self.saveTextElement.text);
local courseSaveText = string.sub(cpSaveTextParts[1], 5);
local folderSaveText = string.sub(cpSaveTextParts[2], 5);
local filterSaveText = string.sub(cpSaveTextParts[3], 5);
local saveButton = courseplay.locales["COURSEPLAY_BUTTON_RETURN"];
self.saveTextElement.courseSaveText = courseplay.locales[courseSaveText]:format(saveButton) or "Return to Save Course";
self.saveTextElement.folderSaveText = courseplay.locales[folderSaveText]:format(saveButton) or "Return to Save Folder";
self.saveTextElement.filterSaveText = courseplay.locales[filterSaveText]:format(saveButton) or "Return to Filter Courses";
end;
self.firstTimeRun = false;
end;
local saveWhat = courseplay.vehicleToSaveCourseIn.cp.saveWhat;
--SET TITLE TEXT
self.titleTextElement.text = self.titleTextElement[saveWhat .. "Text"];
--SET SAVE TEXT
self.saveTextElement.text = self.saveTextElement[saveWhat .. "SaveText"];
self:validateCourseName();
--SET FOCUS
FocusManager:setFocus(self.textInputElement);
self.textInputElement.mouseDown = false;
self.textInputElement.state = TextInputElement.STATE_PRESSED;
self.textInputElement:setForcePressed(true);
end; --END onOpen()
function inputCourseNameDialogue:onClose(element)
InputBinding.setShowMouseCursor(false);
g_currentMission.isPlayerFrozen = false;
end; --END onClose()
function inputCourseNameDialogue:onIsUnicodeAllowed(unicode)
return courseplay.allowedCharacters[unicode] == true;
end; --END onIsUnicodeAllowed()
function inputCourseNameDialogue:onSaveClick()
--print("inputCourseNameDialogue:onSaveClick()");
local vehicle = courseplay.vehicleToSaveCourseIn
if vehicle.cp.saveWhat == 'course' then
if self.textInputElement ~= nil then
--print("self.textInputElement.text= "..tostring(self.textInputElement.text).." courseplay.vehicleToSaveCourseIn.cp.currentCourseName= "..tostring(courseplay.vehicleToSaveCourseIn.cp.currentCourseName));
CourseplayEvent.sendEvent(vehicle, "self.cp.saveWhat", vehicle.cp.saveWhat)
vehicle:setCpVar('currentCourseName',self.textInputElement.text);
--CourseplayEvent.sendEvent(vehicle, "self.cp.currentCourseName", self.textInputElement.text)
vehicle.cp.doNotOnSaveClick = true
else
--print("self.textInputElement= "..tostring(self.textInputElement).." courseplay.vehicleToSaveCourseIn.cp.currentCourseName= "..tostring(courseplay.vehicleToSaveCourseIn.cp.currentCourseName));
end
local maxID = courseplay.courses:getMaxCourseID() -- horoman: made maxID local, should not make a difference as it is used nowhere (at least Eclipse file search doesn't find it in any of the courseplay files)
if maxID == nil then
g_currentMission.cp_courses = {};
maxID = 0
end
vehicle.cp.currentCourseId = maxID + 1;
vehicle.cp.numCourses = 1;
local course = { id = vehicle.cp.currentCourseId, uid = 'c'..vehicle.cp.currentCourseId, type = 'course', name = vehicle.cp.currentCourseName, nameClean = courseplay:normalizeUTF8(vehicle.cp.currentCourseName), waypoints = vehicle.Waypoints, parent = 0 }
if vehicle.cp.courseWorkWidth then -- data for turn maneuver
course.workWidth = vehicle.cp.courseWorkWidth;
end;
if vehicle.cp.courseNumHeadlandLanes then
course.numHeadlandLanes = vehicle.cp.courseNumHeadlandLanes;
end;
if vehicle.cp.courseHeadlandDirectionCW ~= nil then
course.headlandDirectionCW = vehicle.cp.courseHeadlandDirectionCW;
end;
if vehicle.cp.multiTools ~= 1 then
course.multiTools = vehicle.cp.multiTools
end;
g_currentMission.cp_courses[vehicle.cp.currentCourseId] = course
--courseplay:dopairs(g_currentMission.cp_courses,1) replace it by tableshow
g_currentMission.cp_sorted = courseplay.courses:sort()
if not courseplay.isClient then
courseplay.courses:saveCourseToXml(vehicle.cp.currentCourseId, nil, true)
end
courseplay.settings.setReloadCourseItems()
courseplay.signs:updateWaypointSigns(vehicle);
elseif vehicle.cp.saveWhat == 'folder' then
if self.textInputElement ~= nil then
vehicle.cp.saveFolderName = self.textInputElement.text
CourseplayEvent.sendEvent(vehicle, "self.cp.saveWhat", vehicle.cp.saveWhat)
CourseplayEvent.sendEvent(vehicle, "self.cp.saveFolderName", self.textInputElement.text)
end
local maxID = courseplay.courses:getMaxFolderID()
if maxID == nil then
g_currentMission.cp_folders = {}
maxID = 0
end
local folderID = maxID+1
local folder = { id = folderID, uid = 'f'..folderID, type = 'folder', name = vehicle.cp.saveFolderName, nameClean = courseplay:normalizeUTF8(vehicle.cp.saveFolderName), parent = 0 }
g_currentMission.cp_folders[folderID] = folder
--courseplay:dopairs(g_currentMission.cp_folders,1)replace it by tableshow
g_currentMission.cp_sorted = courseplay.courses:sort(g_currentMission.cp_courses, g_currentMission.cp_folders, 0, 0)
if not courseplay.isClient then
courseplay.courses:saveFolderToXml(folderID, nil, true)
end
courseplay.settings.add_folder(folderID)
courseplay.settings.setReloadCourseItems()
courseplay.signs:updateWaypointSigns(vehicle);
elseif vehicle.cp.saveWhat == 'filter' then
if self.textInputElement ~= nil then
vehicle.cp.hud.filter = self.textInputElement.text;
CourseplayEvent.sendEvent(vehicle, "self.cp.saveWhat", vehicle.cp.saveWhat)
CourseplayEvent.sendEvent(vehicle, "self.cp.saveFolderName", self.textInputElement.text)
end
vehicle.cp.hud.filterButton:setSpriteSectionUVs('cancel');
vehicle.cp.hud.filterButton:setToolTip(courseplay:loc('COURSEPLAY_DEACTIVATE_FILTER'));
courseplay.settings.setReloadCourseItems(vehicle);
end
if self.textInputElement ~= nil then
CourseplayEvent.sendEvent(courseplay.vehicleToSaveCourseIn, "self.cp.onSaveClick",true)
self:onCancelClick();
else
vehicle.cp.saveFolderName = nil
end
end; --END onSaveClick()
function inputCourseNameDialogue:onCancelClick()
self.textInputElement.text = "";
self.textInputElement.visibleTextPart1 = "";
self.textInputElement.cursorPosition = 1;
self.textInputElement.cursorBlinkTime = 0;
self.saveTextElement.text = "";
g_gui:showGui("");
courseplay.vehicleToSaveCourseIn.cp.saveFolderName = nil
courseplay.vehicleToSaveCourseIn = nil;
self:onClose();
end; --END onCancelClick()
function inputCourseNameDialogue:onTextChanged()
self:validateCourseName();
end; --END onTextChanged()
function inputCourseNameDialogue:onEnterPressed()
if self:validateCourseName() then
self:onSaveClick();
end;
end; --END onEnterPressed()
function inputCourseNameDialogue:validateCourseName()
self.saveTextElement.disabled = self.textInputElement.text == nil or self.textInputElement.text:len() < 1;
return not self.saveTextElement.disabled;
end; --END validateCourseName()
function inputCourseNameDialogue:setTextInputFocus(element)
end;
function inputCourseNameDialogue:setCallbacks(onCourseNameEntered, target)
self.target = target;
end; --END setCallbacks()
function inputCourseNameDialogue:update(dt)
if InputBinding.hasEvent(InputBinding.MENU_ACCEPT) or InputBinding.hasEvent(InputBinding.COURSEPLAY_MENU_ACCEPT_SECONDARY) then
-- InputBinding.hasEvent(InputBinding.MENU_ACCEPT);
self:onEnterPressed();
elseif InputBinding.hasEvent(InputBinding.MENU, true) or InputBinding.hasEvent(InputBinding.MENU_CANCEL, true) then
-- InputBinding.hasEvent(InputBinding.MENU_CANCEL);
-- InputBinding.hasEvent(InputBinding.MENU);
self:onCancelClick();
end;
end; --END update()
g_inputCourseNameDialogue = inputCourseNameDialogue:new();
g_gui:loadGui(courseplay.path .. 'inputCourseNameDialogue.xml', 'inputCourseNameDialogue', g_inputCourseNameDialogue);