Debian Codegraph @

scripts/menus/ingame/editor.lua

raw
1 -- ____ _ __
2 -- / __ )____ _____ | | / /___ ___________
3 -- / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 -- / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 -- /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 --
7 -- A futuristic real-time strategy game.
8 -- This file is part of Bos Wars.
9 --
10 -- editor.lua - In-game menus.
11 --
12 -- (c) Copyright 2006-2010 by Francois Beerten and Jimmy Salmon
13 --
14 -- This program is free software; you can redistribute it and/or modify
15 -- it under the terms of the GNU General Public License as published by
16 -- the Free Software Foundation; only version 2 of the License.
17 --
18 -- This program is distributed in the hope that it will be useful,
19 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
20 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 -- GNU General Public License for more details.
22 --
23 -- You should have received a copy of the GNU General Public License
24 -- along with this program; if not, write to the Free Software
25 -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 -- 02111-1307, USA.
27 --
28 -- $Id: game.lua 453 2006-06-26 18:36:19Z feb $
29
30 function ErrorEditorMenu(errmsg)
31 local menu = BosGameMenu()
32 menu:setBaseColor(darkNoAlpha)
33 menu:setDrawMenusUnder(true)
34
35 menu:addLabel(_("Error"), menu:getWidth() / 2, 11)
36
37 local l = MultiLineLabel(errmsg)
38 l:setFont(Fonts["large"])
39 l:setAlignment(MultiLineLabel.CENTER)
40 l:setVerticalAlignment(MultiLineLabel.CENTER)
41 l:setLineWidth(240)
42 l:setWidth(240)
43 l:setHeight(200)
44 l:setBackgroundColor(dark)
45 menu:add(l, menu:getWidth() / 2 - l:getWidth() / 2, 50)
46
47 menu:addSmallButton(_("~!OK"), menu:getWidth() / 2 - 50, 248,
48 function() menu:stop() end)
49
50 menu:run(false)
51 end
52
53 -- Called when the user presses an unhandled key while the map editor
54 -- is active and no menu is open. Returns true if it handled the key.
55 -- See the HTML documentation on HandleCommandKey for details.
56 function HandleEditorIngameCommandKey(key, ctrl, alt, shift)
57 if ((key == "m" and alt) or key == "f10") then
58 RunEditorIngameMenu()
59 elseif ((key == "s" and alt) or key == "f11") then
60 RunEditorSaveMenu()
61 elseif (key == "f5") then
62 RunEditorMapPropertiesMenu()
63 elseif (key == "f6") then
64 RunEditorPlayerPropertiesMenu()
65 elseif (key == "t" and not (ctrl or alt)) then
66 Editor.ShowTerrainFlags = not Editor.ShowTerrainFlags
67 elseif (key == "t" and (ctrl and shift)) then
68 Editor:TileSelectedPatch()
69 else
70 return false
71 end
72 return true
73 end
74
75
76 function RunEditorIngameMenu(s)
77 local menu = BosGameMenu()
78 local showTerrain
79
80 menu:setSize(256, 324)
81 menu:addLabel(_("Editor Menu"), 128, 11)
82 menu:addButton(_("Save (~<F11~>)"), 16, 40,
83 function() RunEditorSaveMenu() end)
84 menu:addButton(_("Map Properties (~<F5~>)"), 16, 40 + (36 * 1),
85 function() RunEditorMapPropertiesMenu() end)
86 menu:addButton(_("Player Properties (~<F6~>)"), 16, 40 + (36 * 2),
87 function() RunEditorPlayerPropertiesMenu() end)
88 showTerrain = menu:addCheckBox(_("Show terrain attributes"), 16, 40 + (36 * 3),
89 function() Editor.ShowTerrainFlags = showTerrain:isMarked() end)
90 showTerrain:setFont(Fonts["large"])
91 showTerrain:setMarked(Editor.ShowTerrainFlags)
92
93 menu:addButton(_("~!Exit to menu"), 16, 40 + (36 * 5),
94 function() Editor.Running = EditorNotRunning; menu:stop() end)
95 menu:addButton(_("Return to the Editor (~<Esc~>)"), 16, 284,
96 function() menu:stop() end)
97
98 menu:run(false)
99 end
100
101 function RunEditorSaveMenu()
102 local menu = BosGameMenu()
103
104 menu:addLabel(_("Save Map"), 128, 11)
105
106 local t = menu:addTextInputField("new.map", 16, 40, 224)
107
108 local browser = menu:addMapBrowser("maps/", 16, 70, 224, 166)
109 local function cb(s)
110 t:setText(browser:getSelectedItem())
111 end
112 browser:setActionCallback(cb)
113
114 menu:addSmallButton(_("~!Save"), 16, 248,
115 -- FIXME: use a confirm menu if the file exists already
116 function()
117 local mapname = browser.path .. t:getText()
118 EditorSaveMap(mapname)
119 UI.StatusLine:Set("Saved map to: " .. mapname)
120 menu:stop()
121 end)
122
123 menu:addSmallButton(_("Cancel (~<Esc~>)"), 16 + 12 + 106, 248,
124 function() menu:stop() end)
125
126 menu:run(false)
127 end
128
129 function RunEditorMapPropertiesMenu()
130 local menu = BosGameMenu()
131 menu:setSize(288, 256)
132 menu:setPosition((Video.Width - menu:getWidth()) / 2,
133 (Video.Height - menu:getHeight()) / 2)
134
135 menu:addLabel(_("Map Properties"), 288 / 2, 11)
136
137 local l = Label(_("Map Description:"))
138 l:setFont(Fonts["game"])
139 l:adjustSize()
140 menu:add(l, (288 - 260) / 2, 11 + 36)
141
142 local desc = menu:addTextInputField(Map.Info.Description,
143 (288 - 260) / 2, 11 + 36 + 22, 260)
144
145 local l = Label(_("Size:"))
146 l:setFont(Fonts["game"])
147 l:adjustSize()
148 menu:add(l, (288 - 260) / 2, 11 + (36 * 2) + 22)
149
150 local sizeLabel = Label("" .. Map.Info.MapWidth .. " x " .. Map.Info.MapHeight)
151 sizeLabel:setFont(Fonts["game"])
152 menu:add(sizeLabel, 288 - ((288 - 260) / 2) - 152, 11 + (36 * 2) + 22)
153
154 menu:addSmallButton(_("~!OK"), (288 - (106 * 2)) / 4, 256 - 11 - 27,
155 function()
156 Map.Info.Description = desc:getText()
157 menu:stop()
158 end
159 )
160
161 menu:addSmallButton(_("Cancel (~<Esc~>)"), (288 - (288 - (106 * 2)) / 4) - 106, 256 - 11 - 27,
162 function()
163 menu:stop()
164 end
165 )
166
167 menu:run(false)
168 end
169
170 function RunEditorPlayerPropertiesMenu()
171 local menu = BosGameMenu()
172 menu:setSize(500, 310)
173 menu:setPosition((Video.Width - menu:getWidth()) / 2,
174 (Video.Height - menu:getHeight()) / 2)
175
176 menu:addLabel(_("Player Properties"), 500 / 2, 11)
177
178 local LastEditablePlayer = 7
179
180 for i=0,LastEditablePlayer do
181 local l = Label(tostring(i))
182 l:setFont(Fonts["game"])
183 l:adjustSize()
184 menu:add(l, 12, 40 + (22 * (i + 1)))
185 end
186
187 local l = Label(_("Type"))
188 l:setFont(Fonts["game"])
189 l:adjustSize()
190 menu:add(l, 50, 40 + (22 * 0))
191
192 local types = {_("Person"), _("Computer"), _("Rescue (Passive)"),
193 _("Rescue (Active)"), _("Neutral"), _("Nobody")}
194 local pt = { 5, 4, 5, 1, 0, 2, 3 }; pt[0] = 5
195 local pt2 = { 4, 6, 7, 2, 3 }; pt2[0] = 5
196 local typeWidgets = {}
197 for i=0,LastEditablePlayer do
198 local d = menu:addDropDown(types, 50, 40 + (22 * (i + 1)),
199 function() end)
200 d:getListBox():setWidth(150)
201 d:setWidth(150)
202 d:setSelected(pt[Map.Info.PlayerType[i]])
203 typeWidgets[i] = d
204 end
205
206 local l = Label(_("AI"))
207 l:setFont(Fonts["game"])
208 l:adjustSize()
209 menu:add(l, 210, 40 + (22 * 0))
210
211 local types = {} -- key >= 1; value = .Name
212 local itypes = {} -- key >= 0; value = .Ident
213 local ainametonum = {} -- key = .Ident; value >= 0
214 local i = 1
215 for ai_ident,ai_type in pairs(AiTypes) do
216 types[i] = ai_type.Name
217 ainametonum[ai_ident] = i - 1
218 itypes[i - 1] = ai_ident
219 i = i + 1
220 end
221
222 -- If any players have AI types that this version does not support,
223 -- then add them to the list, so that the editor will at least
224 -- display them and save them back as they were.
225 for i=0,LastEditablePlayer do
226 local ident = Players[i].AiName
227 -- Check ainametonum[ident] rather than AiTypes[ident]
228 -- so we add each ident only once even if multiple players have it.
229 if ainametonum[ident] == nil then
230 local name = ident .. _(" (unsupported)")
231 types[#types + 1] = name
232 itypes[#itypes + 1] = ident
233 ainametonum[ident] = #itypes
234 end
235 end
236
237 local aiWidgets = {}
238 for i=0,LastEditablePlayer do
239 local d = menu:addDropDown(types, 210, 40 + (22 * (i + 1)),
240 function(dd) end)
241 d:setSelected(ainametonum[Players[i].AiName])
242 d:getListBox():setWidth(120)
243 d:setWidth(80)
244 aiWidgets[i] = d
245 end
246
247 local l = Label(_("Energy"))
248 l:setFont(Fonts["game"])
249 l:adjustSize()
250 menu:add(l, 340, 40 + (22 * 0))
251
252 local energyWidgets = {}
253 for i=0,LastEditablePlayer do
254 local d = menu:addTextInputField("" .. Players[i].EnergyStored,
255 340, 40 + (22 * (i + 1)), 60)
256 energyWidgets[i] = d
257 end
258
259 local l = Label(_("Magma"))
260 l:setFont(Fonts["game"])
261 l:adjustSize()
262 menu:add(l, 410, 40 + (22 * 0))
263
264 local magmaWidgets = {}
265 for i=0,LastEditablePlayer do
266 local d = menu:addTextInputField("" .. Players[i].MagmaStored,
267 410, 40 + (22 * (i + 1)), 60)
268 magmaWidgets[i] = d
269 end
270
271 menu:addSmallButton(_("~!OK"), (500 - (106 * 2)) / 4, 270,
272 function()
273 -- Check for at least 1 Person player type
274 local foundPlayer = false
275 for i=0,LastEditablePlayer do
276 if (pt2[typeWidgets[i]:getSelected()] == 5) then
277 foundPlayer = true
278 break
279 end
280 end
281
282 if (foundPlayer == false) then
283 ErrorEditorMenu(_("Must have at least one Type set to Person"))
284 return
285 end
286
287 for i=0,LastEditablePlayer do
288 Map.Info.PlayerType[i] = pt2[typeWidgets[i]:getSelected()]
289 Players[i].AiName = itypes[aiWidgets[i]:getSelected()]
290 Players[i].EnergyStored = tonumber(energyWidgets[i]:getText())
291 Players[i].MagmaStored = tonumber(magmaWidgets[i]:getText())
292 end
293 menu:stop()
294 end)
295
296 menu:addSmallButton(_("Cancel (~<Esc~>)"), (500 - (500 - (106 * 2)) / 4) - 106, 270,
297 function() menu:stop() end)
298
299 menu:run(false)
300 end
301
302
303