Debian Codegraph @

scripts/menus/patch.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 -- (c) Copyright 2009-2010 by Jimmy Salmon
11 --
12 -- This program is free software; you can redistribute it and/or modify
13 -- it under the terms of the GNU General Public License as published by
14 -- the Free Software Foundation; only version 2 of the License.
15 --
16 -- This program is distributed in the hope that it will be useful,
17 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
18 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 -- GNU General Public License for more details.
20 --
21 -- You should have received a copy of the GNU General Public License
22 -- along with this program; if not, write to the Free Software
23 -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 -- 02111-1307, USA.
25
26 -- Called when the user presses an unhandled key while the patch editor
27 -- is active and no menu is open. Returns true if it handled the key.
28 -- See the HTML documentation on HandleCommandKey for details.
29 function HandlePatchEditorIngameCommandKey(key, ctrl, alt, shift)
30 return false
31 end
32
33 function RunNewPatchMenu()
34 local menu
35 local buttonsY = Video.Height - 100
36 local labelX
37 local inputX
38 local y
39 local smallX = Video.Width / 20
40 local smallY = Video.Height / 20
41 local name = ""
42 local image = ""
43 local width = 1
44 local height = 1
45 local returnToMainMenu = false
46
47 local function listDirsAndGraphics(path)
48 local list = {}
49 for pos,name in ipairs(ListDirsInDirectory(path)) do
50 table.insert(list, name .. "/")
51 end
52 for pos,name in ipairs(ListFilesInDirectory(path)) do
53 if string.find(name, "%.png$") then
54 table.insert(list, name)
55 end
56 end
57 return list
58 end
59
60 menu = BosMenu(_("Create New Patch"))
61
62 labelX = smallX
63 inputX = labelX + 30
64 y = 2 * smallY + 20
65 local browser = menu:addBrowser("patches/", listDirsAndGraphics,
66 smallX * 10, y,
67 smallX * 8, buttonsY - 2 * smallY - y)
68 menu:addLabel(_("Name:"), labelX, y)
69 local nameInput = menu:addTextInputField(name,
70 inputX, y, smallX * 9 - inputX)
71
72 y = y + 30
73
74 menu:addLabel(_("Image:"), labelX, y)
75 local imageInput = menu:addTextInputField(image,
76 inputX, y, smallX * 9 - inputX)
77
78 y = y + 30
79
80 menu:addLabel(_("Size:"), labelX, y)
81 local widthInput = menu:addTextInputField(tostring(width),
82 inputX, y, 34)
83 menu:addLabel(_("x"), inputX + 44, y)
84 local heightInput = menu:addTextInputField(tostring(height),
85 inputX + 54, y, 34)
86
87 y = y + 30
88
89 local usedLabel = menu:addMultiLineLabel("", labelX, y,
90 Fonts["game"], false, smallX * 9 - labelX)
91
92 menu:addButton(_("Cancel (~<Esc~>)"), Video.Width / 2 - 250, buttonsY,
93 function() menu:stop() end)
94 menu:addButton(_("Create ~!Patch"), Video.Width / 2 + 50, buttonsY,
95 function()
96 name = nameInput:getText()
97 image = imageInput:getText()
98 width = tonumber(widthInput:getText())
99 height = tonumber(heightInput:getText())
100
101 local flags = {}
102 for i=1,width * height do
103 table.insert(flags, 0x13)
104 end
105
106 patchType(name, image, width, height, flags)
107 HandleCommandKey = HandlePatchEditorIngameCommandKey
108 StartPatchEditor(name)
109 HandleCommandKey = HandleIngameCommandKey
110 menu:stop()
111 returnToMainMenu = true
112 end)
113
114 local function browserCallback(event)
115 local imageFile = browser:getSelectedItem()
116 local imageDirAndFile = browser.path .. imageFile
117 imageInput:setText(imageDirAndFile)
118
119 local patchName = string.gsub(imageDirAndFile, "^patches/", "", 1)
120 patchName = string.gsub(patchName, "%.png$", "", 1)
121 patchName = string.gsub(patchName, "/", "-")
122 nameInput:setText(patchName)
123
124 local ok,width,height = Map.PatchManager:computePatchSize(imageDirAndFile)
125 if ok then
126 widthInput:setText(tostring(width))
127 heightInput:setText(tostring(height))
128 else
129 widthInput:setText("")
130 heightInput:setText("")
131 end
132
133 local usedIn = Map.PatchManager:getPatchTypeNamesUsingGraphic(imageDirAndFile)
134 if usedIn:empty() then
135 usedLabel:setCaption("")
136 else
137 -- Include the name of the image in this label so the claim will
138 -- remain correct even if the user types a different file name
139 -- in imageInput after selecting an image from the browser.
140 usedLabel:setCaption(string.format(_("The image %q is already used in the patch type %q."),
141 imageFile, usedIn[0]))
142 end
143 usedLabel:adjustSize()
144 end
145 browser:setActionCallback(browserCallback)
146
147 menu:run()
148 return returnToMainMenu
149 end
150
151
152 function RunLoadPatchMenu()
153 local menu
154 local buttonsY = Video.Height - 100
155 local y
156 local smallX = Video.Width / 20
157 local smallY = Video.Height / 20
158 local typeNames
159 local names = {}
160
161 menu = BosMenu(_("Select Patch to Edit"))
162
163 typeNames = Map.PatchManager:getPatchTypeNames()
164 -- Convert vector to lua table
165 for i = 0, typeNames:size() - 1 do
166 table.insert(names, typeNames[i])
167 end
168
169 y = 2 * smallY + 20
170 local nameListBox = menu:addListBox(smallX * 10, y,
171 smallX * 8, buttonsY - 2 * smallY - y,
172 names)
173
174 menu:addButton(_("Cancel (~<Esc~>)"), Video.Width / 2 - 250, buttonsY,
175 function() menu:stop() end)
176 menu:addButton(_("Load ~!Patch"), Video.Width / 2 + 50, buttonsY,
177 function()
178 local name = names[nameListBox:getSelected() + 1]
179 HandleCommandKey = HandlePatchEditorIngameCommandKey
180 StartPatchEditor(name)
181 HandleCommandKey = HandleIngameCommandKey
182
183 -- Make it easy for the user to edit another patch type in the
184 -- same series.
185 nameListBox:getContent():requestFocus()
186
187 -- Should the list of patch types be refreshed here?
188 -- Typically, there's a new one whose name begins with "user-".
189 end)
190
191 menu:run() -- loops until menu:stop is called
192 return false -- i.e. return only one level up, not to main menu
193 end