| 1 |
--[[ Copyright (c) 2010 Peter "Corsix" Cawley |
| 2 |
Copyright (c) 2014 Stephen Baker |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. --]] |
| 21 |
|
| 22 |
class "UIMapEditor" (UIResizable) |
| 23 |
|
| 24 |
---@type UIMapEditor |
| 25 |
local UIMapEditor = _G["UIMapEditor"] |
| 26 |
|
| 27 |
local col_bg = {red = 154, green = 146, blue = 198} |
| 28 |
|
| 29 |
-- {{{ Editor sprites. |
| 30 |
|
| 31 |
-- Unfortunately, current map file format does not support flags like FLIP_H. |
| 32 |
-- Commented this out until flags can be stored in the file created by the map editor. |
| 33 |
-- -- High byte sprite constants. |
| 34 |
-- local FLIP_H = DrawFlags.FlipHorizontal * 256 |
| 35 |
|
| 36 |
-- Each variable below is an array of multi-tile sprites, which is translated |
| 37 |
-- to a list of buttons at a page. |
| 38 |
-- The generic form of a multi-tile sprite (see the helipad for an example) is |
| 39 |
-- a table {sprites = .., height = .., objects = ..}. The 'height' defines the |
| 40 |
-- height of the displayed button (between 1 and MAX_HEIGHT). The 'sprites' is |
| 41 |
-- an array of single sprites, a table of {sprite = ..., xpos = ..., ypos = |
| 42 |
-- ..., type = ...}. It defines which sprite to display at which relative |
| 43 |
-- position. Positions run from 1 upward, sprites are numbers 0..255 (the low |
| 44 |
-- byte). |
| 45 |
-- The high byte is reserved for adding DrawFlags flags in the future, eg FLIP_H. |
| 46 |
-- |
| 47 |
-- The type defines where the sprite is stored. 'north' and 'west' mean north |
| 48 |
-- respectively west wall. 'floor' means it is a floor sprite. 'hospital' is |
| 49 |
-- also a floor sprite but it also states that the tile is part of the |
| 50 |
-- hospital. Similarly, 'road' is a floor sprite outside the hospital that can |
| 51 |
-- be walked on. Finally, the 'objects' defines the position of objects in the |
| 52 |
-- shape. At this time, only an entrance door can be placed. |
| 53 |
-- |
| 54 |
-- As there are a lot of single tile sprite buttons, there is a simplified |
| 55 |
-- form to specify those (and they get expanded to the generic multi-tile |
| 56 |
-- sprite form automagically). The short form for a single sprite button is a |
| 57 |
-- table {sprite = ..., height = ..., type = ...}, where all the fields have |
| 58 |
-- the same meaning as described above for the generic form. |
| 59 |
-- |
| 60 |
-- {{{ Foliage sprites. |
| 61 |
local foliage = { |
| 62 |
{sprite=192, height=3, type="floor"}, -- Regular European shrub 1 |
| 63 |
{sprite=193, height=2, type="floor"}, -- Ground plant, green 1 |
| 64 |
{sprite=194, height=2, type="floor"}, -- Bush |
| 65 |
{sprite=195, height=3, type="floor"}, -- Ground plant, red flowers |
| 66 |
{sprite=196, height=3, type="floor"}, -- Shrub |
| 67 |
{sprite=197, height=4, type="floor"}, -- Dead tree (very high) |
| 68 |
{sprite=198, height=2, type="floor"}, -- Low ground plant |
| 69 |
{sprite=199, height=3, type="floor"}, -- Regular European shrub 2 |
| 70 |
{sprite=200, height=2, type="floor"}, -- Ground plant, flowers |
| 71 |
{sprite=201, height=1, type="floor"}, -- Flowerbed East/South |
| 72 |
{sprite=202, height=1, type="floor"}, -- Flowerbed West/South |
| 73 |
{sprite=203, height=1, type="floor"}, -- Flowerbed West/North |
| 74 |
{sprite=204, height=1, type="floor"}, -- Flowerbed North/South |
| 75 |
} |
| 76 |
-- }}} |
| 77 |
-- {{{ Hedge row sprites. |
| 78 |
local hedgerow={ |
| 79 |
{sprite=176, height=2, type="floor"}, -- Hedge West-North |
| 80 |
{sprite=177, height=2, type="floor"}, -- Hedge West-South |
| 81 |
{sprite=178, height=2, type="floor"}, -- Hedge East-South |
| 82 |
{sprite=179, height=2, type="floor"}, -- Hedge East-North |
| 83 |
{sprite=180, height=2, type="floor"}, -- Hedge West-East |
| 84 |
{sprite=181, height=2, type="floor"}, -- Hedge North-South |
| 85 |
{sprite=182, height=2, type="floor"}, -- Hedge East-West-South |
| 86 |
{sprite=183, height=2, type="floor"}, -- Hedge East-West-North |
| 87 |
{sprite=184, height=2, type="floor"}, -- Hedge West-North-South |
| 88 |
{sprite=185, height=2, type="floor"}, -- Hedge East-North-South |
| 89 |
{sprite=186, height=2, type="floor"}, -- Hedge East-West-North-South |
| 90 |
{sprite=187, height=2, type="floor"}, -- Hedge North-South with shrub |
| 91 |
{sprite=188, height=2, type="floor"}, -- Hedge North-South with holes |
| 92 |
{sprite=189, height=2, type="floor"}, -- Hedge East-West with shrub 1 |
| 93 |
{sprite=190, height=2, type="floor"}, -- Hedge East-West with holes |
| 94 |
{sprite=191, height=2, type="floor"}, -- Hedge East-West with shrub 2 |
| 95 |
} |
| 96 |
-- }}} |
| 97 |
-- {{{ Pond sprites. |
| 98 |
local pond={ |
| 99 |
{sprite= 60, height=1, type="floor"}, -- South edge of a pond |
| 100 |
{sprite= 65, height=1, type="floor"}, -- West edge of a pond |
| 101 |
{sprite= 68, height=1, type="floor"}, -- North edge of a pond |
| 102 |
{sprite= 78, height=1, type="floor"}, -- East edge of a pond |
| 103 |
{sprite= 59, height=1, type="floor"}, -- South-West corner of a pond |
| 104 |
{sprite= 77, height=1, type="floor"}, -- North-East corner of a pond |
| 105 |
{sprite= 79, height=1, type="floor"}, -- South-East corner of a pond |
| 106 |
{sprite= 80, height=1, type="floor"}, -- North-West corner of a pond |
| 107 |
{sprite= 69, height=1, type="floor"}, -- Water tile of a pond |
| 108 |
{sprite= 71, height=1, type="floor"}, -- Water tile of a pond with water lilies |
| 109 |
{sprite= 72, height=2, type="floor"}, -- Water tile of a pond with water plant 1 |
| 110 |
{sprite= 73, height=2, type="floor"} -- Water tile of a pond with water plant 2 |
| 111 |
} |
| 112 |
-- }}} |
| 113 |
-- {{{ Inside floor sprites. |
| 114 |
local inside={ |
| 115 |
{sprite= 17, height=1, type="hospital"}, -- Dark blue/purple carpet tile |
| 116 |
{sprite= 70, height=1, type="hospital"}, -- Duplicate of 017 |
| 117 |
{sprite= 18, height=1, type="hospital"}, -- Red-Blue floor tile 1 |
| 118 |
{sprite= 19, height=1, type="hospital"}, -- Red-Blue floor tile 2 |
| 119 |
{sprite= 23, height=1, type="hospital"}, -- Red-Blue floor tile 3 |
| 120 |
{sprite= 16, height=1, type="hospital"}, -- Dark big checker pattern tile |
| 121 |
{sprite= 21, height=1, type="hospital"}, -- Small checker pattern tile |
| 122 |
{sprite= 22, height=1, type="hospital"}, -- Big checker pattern tile |
| 123 |
{sprite= 66, height=1, type="hospital"}, -- Floor tile with light center |
| 124 |
{sprite= 76, height=1, type="hospital"}, -- Floor tile with light center and corners |
| 125 |
{sprite= 20, height=1, type="hospital"} -- Wooden floor tile |
| 126 |
} |
| 127 |
-- }}} |
| 128 |
-- {{{ Outside floor sprites. |
| 129 |
local outside={ |
| 130 |
{sprite= 1, height=1, type="floor"}, -- Grass tile 1 |
| 131 |
{sprite= 2, height=1, type="floor"}, -- Grass tile 2 |
| 132 |
{sprite= 3, height=1, type="floor"}, -- Grass tile 3 |
| 133 |
{sprite= 4, height=1, type="road" }, -- Light concrete tile |
| 134 |
{sprite= 15, height=1, type="road" }, -- Concrete tile |
| 135 |
{sprite= 5, height=1, type="road" }, -- Dark concrete tile |
| 136 |
{sprite= 6, height=1, type="floor"}, -- Grass tile with South-East concrete corner |
| 137 |
{sprite= 8, height=1, type="floor"}, -- Grass tile with South-West concrete corner |
| 138 |
{sprite= 10, height=1, type="floor"}, -- Grass tile with North-West concrete corner |
| 139 |
{sprite= 12, height=1, type="floor"}, -- Grass tile with North-East concrete corner |
| 140 |
{sprite= 7, height=1, type="floor"}, -- Grass tile with South concrete edge |
| 141 |
{sprite= 9, height=1, type="floor"}, -- Grass tile with West concrete edge |
| 142 |
{sprite= 11, height=1, type="floor"}, -- Grass tile with North concrete edge |
| 143 |
{sprite= 13, height=1, type="floor"}, -- Grass tile with East concrete edge |
| 144 |
{sprite= 14, height=1, type="floor"}, -- Concrete tile with North-East grass corner |
| 145 |
{sprite= 61, height=1, type="floor"}, -- Concrete tile with South-West grass corner |
| 146 |
{sprite= 62, height=1, type="floor"}, -- Concrete tile with South-East grass corner |
| 147 |
{sprite= 63, height=1, type="floor"}, -- Concrete tile with North-West grass corner |
| 148 |
{sprite= 64, height=1, type="floor"}, -- Grass tile with rocks |
| 149 |
{sprite=205, height=1, type="floor"}, -- Fully cracked garden marble tile |
| 150 |
{sprite=206, height=1, type="floor"}, -- Broken garden marble tile |
| 151 |
{sprite=207, height=1, type="floor"}, -- Partially cracked garden marble tile |
| 152 |
{sprite=208, height=1, type="floor"}, -- Garden marble tile |
| 153 |
} |
| 154 |
-- }}} |
| 155 |
-- {{{ Road floor sprites. |
| 156 |
local road_spr = { |
| 157 |
{sprite= 41, height=1, type="road"}, -- Road with white discontinuous line North-South |
| 158 |
{sprite= 45, height=1, type="road"}, -- Road with double yellow lines at West edge merging at South |
| 159 |
--{sprite= 46, height=1, type="road"}, -- Duplicate of 45 |
| 160 |
{sprite= 42, height=1, type="road"}, -- Road with double yellow lines at West with black orthogonal lines |
| 161 |
{sprite= 43, height=1, type="road"}, -- Road with double yellow lines at West edge |
| 162 |
{sprite= 44, height=1, type="road"}, -- Road with double yellow lines at West edge merging at North |
| 163 |
{sprite= 47, height=1, type="road"}, -- Road with red line at East linked to yellow discontinuous line at South |
| 164 |
{sprite= 49, height=1, type="road"}, -- Road with red braking line at the East pointing to the West |
| 165 |
{sprite= 48, height=1, type="road"}, -- Road with red line at East linked to yellow discontinuous line at North |
| 166 |
{sprite= 53, height=1, type="road"}, -- Road with double yellow lines at East edge merging at the south |
| 167 |
{sprite= 52, height=1, type="road"}, -- Road with double yellow lines at East with black orthogonal lines |
| 168 |
--{sprite= 54, height=1, type="road"}, -- Duplicate of 52 |
| 169 |
{sprite= 51, height=1, type="road"}, -- Road with double yellow lines at East edge |
| 170 |
{sprite= 57, height=1, type="road"}, -- Road with red line at West linked to yellow discontinuous line at South |
| 171 |
{sprite= 55, height=1, type="road"}, -- Road with red braking line at the West pointing to the East |
| 172 |
{sprite= 56, height=1, type="road"}, -- Road with red line at West linked to yellow discontinuous line at North |
| 173 |
{sprite= 50, height=1, type="road"}, -- Road with grey edge at the East |
| 174 |
{sprite= 58, height=1, type="road"}, -- Road with grey edge at the West |
| 175 |
} |
| 176 |
local road = {} -- All sprites get horizontally flipped as well, for roads running north-south. |
| 177 |
for _, spr in ipairs(road_spr) do |
| 178 |
road[#road + 1] = spr |
| 179 |
|
| 180 |
-- Unfortunately, current map file format does not support flags like FLIP_H. |
| 181 |
-- Commented this out until flags can be stored in the file created by the map editor. |
| 182 |
-- road[#road + 1] = {sprite = spr.sprite + FLIP_H, height = spr.height, type = spr.type} |
| 183 |
end |
| 184 |
-- }}} |
| 185 |
-- {{{ North wall layout and floor sprites. |
| 186 |
local north_wall = { |
| 187 |
{sprites = { |
| 188 |
{sprite=159, xpos=1, ypos=1, type="north"}, -- External doorway North outside left part |
| 189 |
{sprite=157, xpos=3, ypos=1, type="north"}, -- External doorway North outside right part |
| 190 |
}, |
| 191 |
height = 3, |
| 192 |
objects = {{type="entrance_door", xpos=2, ypos=1, direction="north"}}, |
| 193 |
}, |
| 194 |
|
| 195 |
{sprites = { |
| 196 |
{sprite=163, xpos=1, ypos=1, type="north"}, -- External doorway North inside left part |
| 197 |
{sprite=161, xpos=3, ypos=1, type="north"}, -- External doorway North inside right part |
| 198 |
}, |
| 199 |
height = 3, |
| 200 |
objects = {{type="entrance_door", xpos=2, ypos=1, direction="north"}}, |
| 201 |
}, |
| 202 |
|
| 203 |
{sprite=114, height=3, type="north"}, -- External North wall outside |
| 204 |
{sprite=116, height=3, type="north"}, -- External North wall outside left part of window |
| 205 |
{sprite=120, height=3, type="north"}, -- External North wall with window |
| 206 |
{sprite=118, height=3, type="north"}, -- External North wall outside right part of window |
| 207 |
{sprite=122, height=3, type="north"}, -- External North wall inside |
| 208 |
{sprite=124, height=3, type="north"}, -- External North wall inside left part of window |
| 209 |
{sprite=126, height=3, type="north"}, -- External North wall inside right part of window |
| 210 |
-- Unfortunately, current map file format does not support flags like FLIP_H. |
| 211 |
-- Commented this out until flags can be stored in the file created by the map editor. |
| 212 |
-- {sprite=209 + FLIP_H, height=3, type="north"}, -- Lamp post pointing East |
| 213 |
-- {sprite=210 + FLIP_H, height=3, type="north"}, -- Lamp post pointing West |
| 214 |
} |
| 215 |
-- }}} |
| 216 |
-- {{{ West wall layout and floor sprites. |
| 217 |
local west_wall = { |
| 218 |
{sprites = { |
| 219 |
{sprite=158, xpos=1, ypos=3, type="west"}, -- External doorway West outside left part |
| 220 |
{sprite=160, xpos=1, ypos=1, type="west"}, -- External doorway West outside right part |
| 221 |
}, |
| 222 |
height = 3, |
| 223 |
objects = {{type="entrance_door", xpos=1, ypos=2, direction="west"}}, |
| 224 |
}, |
| 225 |
|
| 226 |
{sprites = { |
| 227 |
{sprite=162, xpos=1, ypos=3, type="west"}, -- External doorway West inside left part |
| 228 |
{sprite=164, xpos=1, ypos=1, type="west"}, -- External doorway West inside right part |
| 229 |
}, |
| 230 |
height = 3, |
| 231 |
objects = {{type="entrance_door", xpos=1, ypos=2, direction="west"}}, |
| 232 |
}, |
| 233 |
|
| 234 |
{sprite=115, height=3, type="west"}, -- External West wall outside |
| 235 |
{sprite=119, height=3, type="west"}, -- External West wall outside left part of window |
| 236 |
-- No external west-wall with just glass. 121 (below) isn't finished, 120 (north above) has different lighting |
| 237 |
-- {sprite=121, height=3, type="west"}, -- External West wall with window glass (UNFINISHED?) |
| 238 |
{sprite=117, height=3, type="west"}, -- External West wall outside right part of window |
| 239 |
{sprite=123, height=3, type="west"}, -- External West wall inside |
| 240 |
{sprite=127, height=3, type="west"}, -- External West wall inside left part of window |
| 241 |
{sprite=125, height=3, type="west"}, -- External West wall inside right part of window |
| 242 |
{sprite=210, height=3, type="west"}, -- Lamp post pointing South |
| 243 |
{sprite=209, height=3, type="west"}, -- Lamp post pointing North |
| 244 |
} |
| 245 |
-- }}} |
| 246 |
-- {{{ Helipad layout. |
| 247 |
local helipad = { |
| 248 |
{sprites = { |
| 249 |
-- Dark tiles around the edges. |
| 250 |
{sprite=5, xpos=1, ypos=1, type="road"}, |
| 251 |
{sprite=5, xpos=2, ypos=1, type="road"}, |
| 252 |
{sprite=5, xpos=3, ypos=1, type="road"}, |
| 253 |
{sprite=5, xpos=4, ypos=1, type="road"}, |
| 254 |
{sprite=5, xpos=5, ypos=1, type="road"}, |
| 255 |
|
| 256 |
{sprite=5, xpos=1, ypos=2, type="road"}, |
| 257 |
{sprite=5, xpos=1, ypos=3, type="road"}, |
| 258 |
{sprite=5, xpos=1, ypos=4, type="road"}, |
| 259 |
{sprite=5, xpos=1, ypos=5, type="road"}, |
| 260 |
|
| 261 |
{sprite=5, xpos=2, ypos=5, type="road"}, |
| 262 |
{sprite=5, xpos=3, ypos=5, type="road"}, |
| 263 |
{sprite=5, xpos=4, ypos=5, type="road"}, |
| 264 |
|
| 265 |
{sprite=5, xpos=5, ypos=5, type="road"}, |
| 266 |
{sprite=5, xpos=5, ypos=2, type="road"}, |
| 267 |
{sprite=5, xpos=5, ypos=3, type="road"}, |
| 268 |
{sprite=5, xpos=5, ypos=4, type="road"}, |
| 269 |
-- Dark tiles in the 'H' |
| 270 |
{sprite=5, xpos=3, ypos=2, type="road"}, |
| 271 |
{sprite=5, xpos=3, ypos=4, type="road"}, |
| 272 |
-- Light tiles in the 'H' |
| 273 |
{sprite=4, xpos=2, ypos=2, type="road"}, |
| 274 |
{sprite=4, xpos=2, ypos=3, type="road"}, |
| 275 |
{sprite=4, xpos=2, ypos=4, type="road"}, |
| 276 |
{sprite=4, xpos=4, ypos=2, type="road"}, |
| 277 |
{sprite=4, xpos=4, ypos=3, type="road"}, |
| 278 |
{sprite=4, xpos=4, ypos=4, type="road"}, |
| 279 |
{sprite=4, xpos=3, ypos=3, type="road"} |
| 280 |
}, |
| 281 |
height=5 |
| 282 |
} |
| 283 |
} |
| 284 |
-- }}} |
| 285 |
|
| 286 |
local MAX_HEIGHT = 5 -- Biggest height in above sprites |
| 287 |
local PAGES = { |
| 288 |
{name = _S.map_editor_window.pages.inside, spr_data = inside}, |
| 289 |
{name = _S.map_editor_window.pages.outside, spr_data = outside}, |
| 290 |
{name = _S.map_editor_window.pages.foliage, spr_data = foliage}, |
| 291 |
{name = _S.map_editor_window.pages.hedgerow, spr_data = hedgerow}, |
| 292 |
{name = _S.map_editor_window.pages.pond, spr_data = pond}, |
| 293 |
{name = _S.map_editor_window.pages.road, spr_data = road}, |
| 294 |
{name = _S.map_editor_window.pages.north_wall, spr_data = north_wall}, |
| 295 |
{name = _S.map_editor_window.pages.west_wall, spr_data = west_wall}, |
| 296 |
{name = _S.map_editor_window.pages.helipad, spr_data = helipad} |
| 297 |
} |
| 298 |
-- {{{ Functions |
| 299 |
--! Normalize the editor sprite in the table to always have a 'sprites' field, as well as have sizes and a column width (for the display). |
| 300 |
--!param (table) Sprite from the 'PAGES[#].spr_data' table. |
| 301 |
--!return (table 'sprites', 'xsize', 'ysize', 'width', and 'height') |
| 302 |
local function normalizeEditSprite(spr) |
| 303 |
assert(MAX_HEIGHT >= spr.height) -- Verify that sprite fits in the maximum height. |
| 304 |
|
| 305 |
if spr.sprites == nil then -- {sprite = xxx, height = y} case |
| 306 |
return {sprites = {{sprite = spr.sprite, xpos = 1, ypos = 1, type=spr.type}}, |
| 307 |
xsize = 1, |
| 308 |
ysize = 1, |
| 309 |
type = spr.type, |
| 310 |
xorigin = 0, |
| 311 |
yorigin = 0, |
| 312 |
width = 2, |
| 313 |
height = spr.height} |
| 314 |
else |
| 315 |
-- {sprites={...}, height=y} case, compute sizes, and width |
| 316 |
local xsize = 1 |
| 317 |
local ysize = 1 |
| 318 |
local spr_type = nil |
| 319 |
for _, sp in ipairs(spr.sprites) do |
| 320 |
if sp.xpos > xsize then xsize = sp.xpos end |
| 321 |
if sp.ypos > ysize then ysize = sp.ypos end |
| 322 |
|
| 323 |
assert(not spr_type or spr_type == sp.type) -- Ensure all sprites have the same type. |
| 324 |
spr_type = sp.type |
| 325 |
end |
| 326 |
-- Position to draw (1,1) sprite, by default (0,0) |
| 327 |
-- Since sprites are drawn top to bottom, yorigin never changes. |
| 328 |
local xorigin = (ysize - 1) * 32 |
| 329 |
local yorigin = 0 |
| 330 |
|
| 331 |
local width = ysize - 1 + xsize - 1 + 2 |
| 332 |
if width < ysize then width = ysize end |
| 333 |
|
| 334 |
return {sprites = spr.sprites, |
| 335 |
xsize = xsize, |
| 336 |
ysize = ysize, |
| 337 |
type = spr_type, |
| 338 |
xorigin = xorigin, |
| 339 |
yorigin = yorigin, |
| 340 |
width = width, |
| 341 |
height = spr.height, |
| 342 |
objects = spr.objects} |
| 343 |
end |
| 344 |
end |
| 345 |
|
| 346 |
--!Decide the highest possible placement for 'width' columns. |
| 347 |
--!param cols (list int) First available position in each column, higher number is lower. |
| 348 |
--!param width (int) Required width as number of columns. |
| 349 |
--!return (int, int) Starting column and placement height. |
| 350 |
local function getHighestRowcol(cols, width) |
| 351 |
local best = cols[1] + 100000 |
| 352 |
local best_col = 0 |
| 353 |
|
| 354 |
for left = 1, #cols - width + 1 do -- Try starting in each column. |
| 355 |
if cols[left] < best then -- First column is potentially better. |
| 356 |
local top = cols[left] |
| 357 |
for i = 1, width - 1 do |
| 358 |
if top < cols[left + i] then |
| 359 |
top = cols[left + i] |
| 360 |
if top >= best then break end -- i-th column breaks improvement. |
| 361 |
end |
| 362 |
end |
| 363 |
if top < best then |
| 364 |
best = top |
| 365 |
best_col = left |
| 366 |
end |
| 367 |
end |
| 368 |
end |
| 369 |
return best_col, best |
| 370 |
end |
| 371 |
|
| 372 |
--! Layout buttons from a tab of editor sprites. |
| 373 |
--!param esprs (list) Editor sprite tab to layout. |
| 374 |
--!param num_cols (int) Number of columns available in the layout. |
| 375 |
--!return (list) Sprites with button positions in ('column', 'row'). |
| 376 |
local function layoutButtons(esprs, num_cols) |
| 377 |
local buttons = {} |
| 378 |
|
| 379 |
local cols = {} |
| 380 |
for i = 1, num_cols do |
| 381 |
cols[i] = 1 |
| 382 |
end |
| 383 |
|
| 384 |
for hgt = MAX_HEIGHT, 1, -1 do -- Fit highest sprites first. |
| 385 |
for _, espr in ipairs(esprs) do |
| 386 |
espr = normalizeEditSprite(espr) |
| 387 |
if espr.height == hgt then |
| 388 |
local spr_col, spr_height = getHighestRowcol(cols, espr.width) |
| 389 |
buttons[#buttons + 1] = espr |
| 390 |
buttons[#buttons].column = spr_col |
| 391 |
buttons[#buttons].row = spr_height |
| 392 |
|
| 393 |
for i = spr_col, spr_col + espr.width - 1 do -- Update height in the affected columns |
| 394 |
assert(cols[i] <= spr_height) |
| 395 |
cols[i] = spr_height + hgt |
| 396 |
end |
| 397 |
|
| 398 |
end |
| 399 |
end |
| 400 |
end |
| 401 |
return buttons |
| 402 |
end |
| 403 |
-- }}} |
| 404 |
-- }}} |
| 405 |
|
| 406 |
local EDITOR_WINDOW_XSIZE = 368 |
| 407 |
local EDITOR_WINDOW_YSIZE = 586 |
| 408 |
local EDITOR_COLUMNS = 10 -- Number of columns for the sprite buttons |
| 409 |
local EDITOR_COLUMN_SIZE = 32 + 3 -- Width of a sprite button in pixels |
| 410 |
local EDITOR_ROW_SIZE = 32 + 4 -- Height of a sprite button in pixels |
| 411 |
|
| 412 |
function UIMapEditor:UIMapEditor(ui) |
| 413 |
self:UIResizable(ui, EDITOR_WINDOW_XSIZE, EDITOR_WINDOW_YSIZE, col_bg) |
| 414 |
self.resizable = false |
| 415 |
|
| 416 |
self.ui = ui |
| 417 |
self.panel_sprites = self.ui.app.map.blocks |
| 418 |
|
| 419 |
-- For when there are multiple things which could be sampled from a tile, |
| 420 |
-- keep track of the index of which one was most recently sampled, so that |
| 421 |
-- next time a different one is sampled. |
| 422 |
self.sample_i = 1 |
| 423 |
|
| 424 |
-- Cursor data in the main world view. |
| 425 |
-- States: |
| 426 |
-- - disabled: Just show the cursor. |
| 427 |
-- - grid: Show an area with red rectangles (requires 'sprite' to exist). |
| 428 |
-- - left: Dragging with left mouse button is down, (leftx, lefty) defines the tile. |
| 429 |
-- - right: Dragging with right mouse button down, (rightx, righty) defines the tile. |
| 430 |
-- - both: Dragging with both left and right mouse buttons down (initiated with left |
| 431 |
-- button), (rightx, righty) defines the tile where right button got pressed. |
| 432 |
-- - delete: Like grid, with a 1x1 cursor for selecting tiles to remove walls from. |
| 433 |
-- - delete-left: Left mouse button drag of 'delete' mode. |
| 434 |
-- - parcel: Like grid, with a 1x1 cursor for selecting tiles to set the parcel. |
| 435 |
-- - parcel-left: Left mouse button drag of 'parcel' mode. |
| 436 |
-- - paste: Like grid, with a copy_xsize, copy_ysize cursor to show where to paste. |
| 437 |
-- - paste-left: Left mouse button drag of 'paste' mode. Pasting is limited to dragged |
| 438 |
-- area (which can be smaller than (copy_xsize, copy_ysize) area). |
| 439 |
-- - camera: Like grid, with a 1x1 cursor for selecting tile to set the camera |
| 440 |
-- - heliport: Like grid, with a 1x1 cursor for selecting tile to set the heliport |
| 441 |
-- |
| 442 |
-- Notes: |
| 443 |
-- - The code switches back to 'grid' or 'disabled' without waiting for all |
| 444 |
-- buttons getting released. |
| 445 |
-- - Nothing gets added in the world until you let go of a mouse button. |
| 446 |
-- - Drag detection ('is_drag') is used to allow cancel if user moves back |
| 447 |
-- to the (leftx, lefty) position. |
| 448 |
-- - The sprite decides how you can drag (none, north-south, east-west, area) |
| 449 |
-- |
| 450 |
self.cursor = { |
| 451 |
state = "disabled", -- State of the cursor. |
| 452 |
sprite = nil, -- Selected sprite from the menu. |
| 453 |
is_drag = false, -- Whether a true drag (at least 2 cells covered) has been detected. |
| 454 |
parcel = nil, -- Parcel number to set in 'parcel' / 'parcel-left' mode. |
| 455 |
camera = nil, -- Camera player to set in 'camera' mode. |
| 456 |
heliport = nil, -- Heliport player to set in 'heliport' mode. |
| 457 |
|
| 458 |
xpos = 0, -- Horizontal tile position of the mouse cursor. |
| 459 |
ypos = 0, -- Vertical tile position of the mouse cursor. |
| 460 |
|
| 461 |
leftx = 0, -- Horizontal tile position where the left-click was first detected. |
| 462 |
lefty = 0, -- Vertical tile position where the left-click was first detected. |
| 463 |
rightx = 0, -- Horizontal tile position where the right-click was first detected. |
| 464 |
righty = 0, -- Vertical tile position where the right-click was first detected. |
| 465 |
|
| 466 |
copy_data = nil, -- Data of the copied area, 'nil' if no data available. |
| 467 |
copy_xsize = 0, -- Horizontal size of the copy area. |
| 468 |
copy_ysize = 0, -- Vertical size of the copy area. |
| 469 |
} |
| 470 |
|
| 471 |
-- {{{ Creation of buttons. |
| 472 |
-- A sprite table containing a "cell outline" sprite |
| 473 |
self.cell_outline = TheApp.gfx:loadSpriteTable("Bitmap", "aux_ui", true) |
| 474 |
|
| 475 |
self:addBevelPanel(0, 0, EDITOR_WINDOW_XSIZE, EDITOR_WINDOW_YSIZE, col_bg) -- Background of the window. |
| 476 |
|
| 477 |
self.block_buttons = {} -- List of table {button = .., panel = ..} |
| 478 |
self.selected_block = nil -- Page of of block buttons currently displayed/selected. |
| 479 |
|
| 480 |
-- Make the page selection buttons |
| 481 |
local XSTART = 10 |
| 482 |
local XSIZE = 109 |
| 483 |
local YSIZE = 20 |
| 484 |
|
| 485 |
local ypos = 10 |
| 486 |
local xpos = XSTART |
| 487 |
self.page_selectbuttons = {} |
| 488 |
for _, page in ipairs(PAGES) do |
| 489 |
local name = page.name |
| 490 |
if xpos + XSIZE >= EDITOR_WINDOW_XSIZE then -- Update ypos (and xpos) if necessary. |
| 491 |
xpos = XSTART |
| 492 |
ypos = ypos + YSIZE + 2 |
| 493 |
end |
| 494 |
|
| 495 |
local p = self:addBevelPanel(xpos, ypos, XSIZE, YSIZE, col_bg) |
| 496 |
:setLabel(name):makeToggleButton(0, 0, XSIZE, YSIZE, nil, |
| 497 |
--[[persistable:map_editor_spritepage_clicked]] function() self:pageClicked(name) end) |
| 498 |
local spr_buttons = layoutButtons(page.spr_data, EDITOR_COLUMNS) |
| 499 |
local page_data = {button = p, name = name, sprite_buttons = spr_buttons} |
| 500 |
|
| 501 |
self.page_selectbuttons[#self.page_selectbuttons + 1] = page_data |
| 502 |
xpos = xpos + XSIZE + 10 |
| 503 |
end |
| 504 |
-- Make the bottom text buttons |
| 505 |
xpos = XSTART |
| 506 |
ypos = 420 |
| 507 |
|
| 508 |
local text_pages = { |
| 509 |
{name = "paste", text = _S.map_editor_window.pages.paste}, |
| 510 |
{name = "delete_wall", text = _S.map_editor_window.pages.delete_wall}, |
| 511 |
{name = "camera_1", text = _S.map_editor_window.pages.camera_1, camera = 1}, |
| 512 |
{name = "camera_2", text = _S.map_editor_window.pages.camera_2, camera = 2}, |
| 513 |
{name = "camera_3", text = _S.map_editor_window.pages.camera_3, camera = 3}, |
| 514 |
{name = "camera_4", text = _S.map_editor_window.pages.camera_4, camera = 4}, |
| 515 |
{name = "heliport_1", text = _S.map_editor_window.pages.heliport_1, heliport = 1}, |
| 516 |
{name = "heliport_2", text = _S.map_editor_window.pages.heliport_2, heliport = 2}, |
| 517 |
{name = "heliport_3", text = _S.map_editor_window.pages.heliport_3, heliport = 3}, |
| 518 |
{name = "heliport_4", text = _S.map_editor_window.pages.heliport_4, heliport = 4}} |
| 519 |
for i = 0, 9 do |
| 520 |
table.insert(text_pages, { name = "parcel_" .. i, text = _S.map_editor_window.pages.parcel:format(i), parcel = i}) |
| 521 |
end |
| 522 |
|
| 523 |
for _, page in ipairs(text_pages) do |
| 524 |
if xpos + XSIZE >= EDITOR_WINDOW_XSIZE then -- Update ypos (and xpos) if necessary. |
| 525 |
xpos = XSTART |
| 526 |
ypos = ypos + YSIZE + 2 |
| 527 |
end |
| 528 |
|
| 529 |
local name = page.name |
| 530 |
local p = self:addBevelPanel(xpos, ypos, XSIZE, YSIZE, col_bg) |
| 531 |
:setLabel(page.text):makeToggleButton(0, 0, XSIZE, YSIZE, nil, |
| 532 |
--[[persistable:map_editor_textpage_clicked]] function() self:pageClicked(name) end) |
| 533 |
local page_data = {button = p, name = name, data = page} |
| 534 |
self.page_selectbuttons[#self.page_selectbuttons + 1] = page_data |
| 535 |
xpos = xpos + XSIZE + 10 |
| 536 |
end |
| 537 |
|
| 538 |
-- Custom parcel number input |
| 539 |
local col_textbox = { |
| 540 |
red = 0, |
| 541 |
green = 0, |
| 542 |
blue = 0, |
| 543 |
} |
| 544 |
local function parcel_num() |
| 545 |
self.cursor.state = "parcel" |
| 546 |
self.cursor.sprite = nil |
| 547 |
self.cursor.is_drag = false |
| 548 |
self.cursor.parcel = self.parcel_input.text |
| 549 |
TheApp.map:loadDebugText("parcel") |
| 550 |
-- Raise all numbered parcel buttons |
| 551 |
for _, table in pairs(self.page_selectbuttons) do |
| 552 |
if table.name:find("parcel") and table.button.toggled then |
| 553 |
table.button:toggle() |
| 554 |
end |
| 555 |
end |
| 556 |
end |
| 557 |
self.parcel_input = self:addBevelPanel(xpos, ypos, XSIZE, YSIZE, col_textbox) |
| 558 |
:setLabel(_S.map_editor_window.pages.set_parcel) |
| 559 |
:setTooltip(_S.map_editor_window.pages.set_parcel_tooltip) |
| 560 |
:makeTextbox(--[[persistable:mapeditor_set_parcel_number_callback]] function() parcel_num() end) |
| 561 |
:allowedInput("numbers"):characterLimit(2):setText("10") |
| 562 |
|
| 563 |
self:pageClicked("") -- Initialize all above 'page_selectbuttons'. |
| 564 |
-- }}} |
| 565 |
|
| 566 |
self:setPosition(0.1, 0.1) |
| 567 |
|
| 568 |
ui:addKeyHandler("ingame_loadMenu", self, self.openLoad) -- load saved maps menu |
| 569 |
ui:addKeyHandler("ingame_saveMenu", self, self.openSave) -- load save map menu |
| 570 |
ui:addKeyHandler("ingame_restartLevel", self, self.restart) -- restart the map editor |
| 571 |
ui:addKeyHandler("ingame_quitLevel", self, self.quit) -- quit the map editor and return to main menu |
| 572 |
ui:addKeyHandler("ingame_jukebox", self, self.openJukebox) -- open jukebox |
| 573 |
end |
| 574 |
|
| 575 |
function UIMapEditor:openLoad() |
| 576 |
self.ui:addWindow(UILoadMap(self.ui, "map")) |
| 577 |
end |
| 578 |
|
| 579 |
function UIMapEditor:openSave() |
| 580 |
self.ui:addWindow(UISaveMap(self.ui)) |
| 581 |
end |
| 582 |
|
| 583 |
function UIMapEditor:restart() |
| 584 |
self.ui:restartMapEditor() |
| 585 |
end |
| 586 |
|
| 587 |
function UIMapEditor:quit() |
| 588 |
self.ui:quit(true) |
| 589 |
end |
| 590 |
|
| 591 |
function UIMapEditor:openJukebox() |
| 592 |
if self.ui.app.config.audio then |
| 593 |
self.ui:addWindow(UIJukebox(self.ui.app)) |
| 594 |
end |
| 595 |
end |
| 596 |
|
| 597 |
function UIMapEditor:setPlayerCount(count) |
| 598 |
local map = self.ui.app.map |
| 599 |
map:setPlayerCount(count) |
| 600 |
if self.cursor.state == "camera" or self.cursor.state == "heliport" then |
| 601 |
map:updateDebugOverlay() |
| 602 |
end |
| 603 |
end |
| 604 |
|
| 605 |
-- {{{ function UIMapEditor:pageClicked(name) |
| 606 |
|
| 607 |
--! Update how the button is displayed based on the provided new state. |
| 608 |
--!param button (Panel) to update. |
| 609 |
--!param action (str) New state of the button. |
| 610 |
local function updateToggleButton(button, action) |
| 611 |
if action == "raised" then |
| 612 |
button:enable(true) |
| 613 |
button:setVisible(true) |
| 614 |
button:setToggleState(false) |
| 615 |
|
| 616 |
elseif action == "lowered" then |
| 617 |
button:enable(true) |
| 618 |
button:setVisible(true) |
| 619 |
button:setToggleState(true) |
| 620 |
|
| 621 |
elseif action == "invisible" then |
| 622 |
button:enable(false) |
| 623 |
button:setVisible(false) |
| 624 |
|
| 625 |
elseif action == "disabled" then |
| 626 |
button:enable(false) |
| 627 |
button:setVisible(true) |
| 628 |
button:setToggleState(false) |
| 629 |
|
| 630 |
else |
| 631 |
assert(false) -- Should never arrive here |
| 632 |
end |
| 633 |
end |
| 634 |
|
| 635 |
--! Callback function of the page select buttons. |
| 636 |
--!param name (string) Name of the clicked page. |
| 637 |
function UIMapEditor:pageClicked(name) |
| 638 |
local map = self.ui.app.map |
| 639 |
if name == "paste" then |
| 640 |
-- Make 'paste' button non-selectable if there is no data to paste. |
| 641 |
name = self.cursor.copy_data and name or "" |
| 642 |
else |
| 643 |
self.cursor.copy_data = nil -- Delete copied area when selecting another item. |
| 644 |
end |
| 645 |
|
| 646 |
for _, pb in ipairs(self.page_selectbuttons) do |
| 647 |
if pb.name == name then |
| 648 |
map:clearDebugText() |
| 649 |
self.cursor.state = name == "paste" and "paste" or "disabled" |
| 650 |
updateToggleButton(pb.button, "lowered") |
| 651 |
if pb.sprite_buttons then |
| 652 |
-- 'sprite' button, display sprites to select from, by the user. |
| 653 |
self:buildSpriteButtons(pb.sprite_buttons) |
| 654 |
else |
| 655 |
-- 'text' button, switch cursor to the right mode directly. |
| 656 |
self:buildSpriteButtons({}) |
| 657 |
if pb.name == "delete_wall" then |
| 658 |
self.cursor.state = "delete" |
| 659 |
self.cursor.sprite = nil |
| 660 |
self.cursor.is_drag = false |
| 661 |
|
| 662 |
elseif pb.name == "paste" then |
| 663 |
self.cursor.state = "paste" |
| 664 |
self.cursor.sprite = nil |
| 665 |
self.cursor.is_drag = false |
| 666 |
|
| 667 |
elseif pb.data.camera then |
| 668 |
self.cursor.state = "camera" |
| 669 |
self.cursor.spirte = nil |
| 670 |
self.cursor.is_drag = false |
| 671 |
self.cursor.camera = pb.data.camera |
| 672 |
map:loadDebugText("camera") |
| 673 |
|
| 674 |
elseif pb.data.heliport then |
| 675 |
self.cursor.state = "heliport" |
| 676 |
self.cursor.spirte = nil |
| 677 |
self.cursor.is_drag = false |
| 678 |
self.cursor.heliport = pb.data.heliport |
| 679 |
map:loadDebugText("heliport") |
| 680 |
|
| 681 |
else -- Should be a parcel button. |
| 682 |
assert(pb.data.parcel) |
| 683 |
|
| 684 |
self.cursor.state = "parcel" |
| 685 |
self.cursor.sprite = nil |
| 686 |
self.cursor.is_drag = false |
| 687 |
self.cursor.parcel = pb.data.parcel |
| 688 |
map:loadDebugText("parcel") |
| 689 |
end |
| 690 |
end |
| 691 |
|
| 692 |
elseif pb.name == "paste" and not self.cursor.copy_data then |
| 693 |
updateToggleButton(pb.button, "disabled") |
| 694 |
else |
| 695 |
updateToggleButton(pb.button, "raised") |
| 696 |
end |
| 697 |
end |
| 698 |
end |
| 699 |
|
| 700 |
--! Do layout of the sprite buttons of the page. |
| 701 |
function UIMapEditor:buildSpriteButtons(buttons) |
| 702 |
self.selected_block = buttons |
| 703 |
|
| 704 |
local XBASE = 10 |
| 705 |
local YBASE = 83 |
| 706 |
local number = 1 |
| 707 |
for _, button in ipairs(buttons) do |
| 708 |
local xpos = XBASE + (button.column - 1) * EDITOR_COLUMN_SIZE |
| 709 |
local ypos = YBASE + (button.row - 1) * EDITOR_ROW_SIZE |
| 710 |
local width = EDITOR_COLUMN_SIZE * button.width - 2 |
| 711 |
local height = EDITOR_ROW_SIZE * button.height - 2 |
| 712 |
|
| 713 |
local pb = self.block_buttons[number] |
| 714 |
if pb == nil then -- New sprite button needed |
| 715 |
local bbutton_number = number |
| 716 |
local bbutton = self:addBevelPanel(xpos, ypos, width, height, col_bg) |
| 717 |
bbutton = bbutton:makeToggleButton(0, 0, width, height, nil, |
| 718 |
--[[persistable:map_editor_block_clicked]] function() |
| 719 |
self:blockClicked(bbutton_number) |
| 720 |
end) |
| 721 |
updateToggleButton(bbutton, "raised") |
| 722 |
|
| 723 |
local bpanel = self:addPanel(number, xpos+1, ypos+1, width-2, height-2) |
| 724 |
bpanel.visible = true |
| 725 |
bpanel.editor_button = button |
| 726 |
|
| 727 |
bpanel.custom_draw = --[[persistable:map_editor_draw_block_sprite]] function(panel, canvas, x, y) |
| 728 |
local s = TheApp.config.ui_scale |
| 729 |
x = x + panel.x * s + panel.editor_button.xorigin * s |
| 730 |
y = y + panel.y * s + panel.editor_button.yorigin * s |
| 731 |
for _, spr in ipairs(panel.editor_button.sprites) do |
| 732 |
local xspr = x + (spr.xpos - spr.ypos) * 32 * s |
| 733 |
local yspr = y + (spr.xpos + spr.ypos) * 16 * s - 32 * s |
| 734 |
panel.window.panel_sprites:draw(canvas, spr.sprite % 256, xspr, yspr, { flags = math.floor(spr.sprite / 256), scaleFactor = s }) |
| 735 |
end |
| 736 |
end |
| 737 |
|
| 738 |
self.block_buttons[#self.block_buttons + 1] = {button=bbutton, panel=bpanel} |
| 739 |
|
| 740 |
else -- Reposition & resize existing sprite button |
| 741 |
local bbutton = pb.button |
| 742 |
bbutton:setPosition(xpos, ypos) |
| 743 |
bbutton:setSize(width, height) |
| 744 |
updateToggleButton(bbutton, "raised") |
| 745 |
|
| 746 |
local bpanel = pb.panel |
| 747 |
bpanel.editor_button = button |
| 748 |
bpanel:setPosition(xpos+1, ypos+1) |
| 749 |
bpanel:setSize(width-2, height-2) |
| 750 |
bpanel:setVisible(true) |
| 751 |
end |
| 752 |
|
| 753 |
number = number + 1 |
| 754 |
end |
| 755 |
|
| 756 |
-- Make remaining buttons and panels invisible. |
| 757 |
while true do |
| 758 |
local pb = self.block_buttons[number] |
| 759 |
if pb == nil then break end |
| 760 |
|
| 761 |
updateToggleButton(pb.button, "invisible") |
| 762 |
pb.panel.visible = false |
| 763 |
number = number + 1 |
| 764 |
end |
| 765 |
end |
| 766 |
-- }}} |
| 767 |
|
| 768 |
--! Should the given type of sprite be considered a floor sprite? |
| 769 |
--!param sprite_type (string) Type of sprite. |
| 770 |
--!return The (boolean) type is a floor sprite type. |
| 771 |
local function isFloorSpriteType(sprite_type) |
| 772 |
return sprite_type == "floor" or sprite_type == "hospital" or sprite_type == "road" |
| 773 |
end |
| 774 |
|
| 775 |
--! Construct cell flags for a given kind of floor sprite. |
| 776 |
--!param sprite_type (string) Type of sprite. |
| 777 |
local function makeCellFlags(sprite_type) |
| 778 |
if sprite_type == "floor" then |
| 779 |
return {buildable=false, passable=false, hospital=false} |
| 780 |
|
| 781 |
elseif sprite_type == "road" then |
| 782 |
return {buildable=false, passable=true, hospital=false} |
| 783 |
|
| 784 |
elseif sprite_type == "hospital" then |
| 785 |
return {buildable=true, passable=true, hospital=true} |
| 786 |
end |
| 787 |
assert(false) -- Should never get here |
| 788 |
end |
| 789 |
|
| 790 |
--! Get the tile area covered by two points. |
| 791 |
--!param x1 (jnt) Horizontal coordinate of the first point. |
| 792 |
--!param y1 (int) Vertical coordinate of the first point. |
| 793 |
--!param x2 (jnt) Horizontal coordinate of the second point. |
| 794 |
--!param y2 (int) Vertical coordinate of the second point. |
| 795 |
--!return (4 int) Smallest horizontal, smallest vertical, largest horizontal, |
| 796 |
-- and largest vertical coordinate. |
| 797 |
local function getCoveredArea(x1, y1, x2, y2) |
| 798 |
local minx, maxx, miny, maxy |
| 799 |
if x1 < x2 then minx, maxx = x1, x2 else minx, maxx = x2, x1 end |
| 800 |
if y1 < y2 then miny, maxy = y1, y2 else miny, maxy = y2, y1 end |
| 801 |
return minx, miny, maxx, maxy |
| 802 |
end |
| 803 |
|
| 804 |
--! Get the size of an area covered by two points. |
| 805 |
--!param x1 (jnt) Horizontal coordinate of the first point. |
| 806 |
--!param y1 (int) Vertical coordinate of the first point. |
| 807 |
--!param x2 (jnt) Horizontal coordinate of the second point. |
| 808 |
--!param y2 (int) Vertical coordinate of the second point. |
| 809 |
--!return (2 int) Horizontal and vertical size of the area. |
| 810 |
local function getAreaSize(x1, y1, x2, y2) |
| 811 |
local minx, miny, maxx, maxy = getCoveredArea(x1, y1, x2, y2) |
| 812 |
return maxx - minx + 1, maxy - miny + 1 |
| 813 |
end |
| 814 |
|
| 815 |
--! Test whether (px, py) is inside the given area. |
| 816 |
--!param px (int) Horizontal coordinate of the point to test. |
| 817 |
--!param py (int) Vertical coordinate of the point to test. |
| 818 |
--!param minx (jnt) Smallest horizontal coordinate of the area. |
| 819 |
--!param miny (int) Smallest vertical coordinate of the area. |
| 820 |
--!param maxx (jnt) Largest horizontal coordinate of the area. |
| 821 |
--!param maxy (int) Largest vertical coordinate of the area. |
| 822 |
--!return (bool) Whether the point is inside the given area. |
| 823 |
local function isPointInside(px, py, minx, miny, maxx, maxy) |
| 824 |
return px >= minx and px <= maxx and py >= miny and py <= maxy |
| 825 |
end |
| 826 |
|
| 827 |
|
| 828 |
--! Compute x/y pairs to draw the cursor sprite over the area. |
| 829 |
--!param minx (int) First horizontal position to draw the sprite. |
| 830 |
--!param miny (int) First vertical position to draw the sprite. |
| 831 |
--!param maxx (int) Last horizontal position to draw the sprite. |
| 832 |
--!param maxy (int) Last vertical position to draw the sprite. |
| 833 |
--!param dx (nil or int) Horizontal step size of drawing, usually same size as |
| 834 |
-- the width of the sprite being drawn, default is 1. |
| 835 |
--!param dy (nil or int) Vertical step size of drawing, usually same size as |
| 836 |
-- the height of the sprite being drawn, default is 1. |
| 837 |
--!return (array of (xpos, ypos) pairs) Points to draw the sprite cursor. |
| 838 |
local function computeCursorSpriteAtArea(minx, miny, maxx, maxy, dx, dy) |
| 839 |
local coords = {} |
| 840 |
|
| 841 |
if not dx or not dy then dx, dy = 1, 1 end |
| 842 |
|
| 843 |
assert(dx > 0 and dy > 0) |
| 844 |
local xbase, ybase |
| 845 |
xbase = minx |
| 846 |
while xbase <= maxx do |
| 847 |
ybase = miny |
| 848 |
while ybase <= maxy do |
| 849 |
coords[#coords + 1] = {xpos = xbase, ypos = ybase} |
| 850 |
|
| 851 |
ybase = ybase + dy |
| 852 |
end |
| 853 |
xbase = xbase + dx |
| 854 |
end |
| 855 |
|
| 856 |
return coords |
| 857 |
end |
| 858 |
|
| 859 |
--! Compute the positions to draw the selected sprite in the world. |
| 860 |
--!return (array of {xpos, ypos} tables) Coordinates to draw the selected sprite. |
| 861 |
function UIMapEditor:getDrawPoints() |
| 862 |
if self.cursor.state == "disabled" then |
| 863 |
return {} -- Nothing to compute |
| 864 |
|
| 865 |
elseif self.cursor.state == "grid" then |
| 866 |
local bx, by = self:areaOnWorld(self.cursor.xpos, self.cursor.ypos, |
| 867 |
self.cursor.sprite.xsize, self.cursor.sprite.ysize) |
| 868 |
return {{xpos = bx, ypos = by}} |
| 869 |
|
| 870 |
elseif self.cursor.state == "delete" or self.cursor.state == "parcel" or |
| 871 |
self.cursor.state == "camera" or self.cursor.state == "heliport" or |
| 872 |
self.cursor.state == "paste" then |
| 873 |
return {{xpos = self.cursor.xpos, ypos = self.cursor.ypos}} |
| 874 |
|
| 875 |
elseif self.cursor.state == "left" then |
| 876 |
-- Simple drag (left button only). |
| 877 |
local minx, miny, maxx, maxy = getCoveredArea(self.cursor.leftx, self.cursor.lefty, |
| 878 |
self.cursor.xpos, self.cursor.ypos) |
| 879 |
if minx ~= maxx or miny ~= maxy or not self.cursor.is_drag then |
| 880 |
-- Just 1 tile without starting a drag, or an area of at least two tiles. |
| 881 |
return computeCursorSpriteAtArea(minx, miny, maxx, maxy, |
| 882 |
self.cursor.sprite.xsize, self.cursor.sprite.ysize) |
| 883 |
end |
| 884 |
|
| 885 |
elseif self.cursor.state == "delete-left" or self.cursor.state == "parcel-left" or |
| 886 |
self.cursor.state == "paste-left" then |
| 887 |
local minx, miny, maxx, maxy = getCoveredArea(self.cursor.leftx, self.cursor.lefty, |
| 888 |
self.cursor.xpos, self.cursor.ypos) |
| 889 |
if minx ~= maxx or miny ~= maxy or not self.cursor.is_drag then |
| 890 |
-- Just 1 tile without starting a drag, or an area of at least two tiles. |
| 891 |
return computeCursorSpriteAtArea(minx, miny, maxx, maxy, 1, 1) |
| 892 |
end |
| 893 |
|
| 894 |
elseif self.cursor.state == "right" then |
| 895 |
local minx, miny, maxx, maxy = getCoveredArea(self.cursor.rightx, self.cursor.righty, |
| 896 |
self.cursor.xpos, self.cursor.ypos) |
| 897 |
if minx ~= maxx or miny ~= maxy or not self.cursor.is_drag then |
| 898 |
-- Just 1 tile without starting a drag, or an area of at least two tiles. |
| 899 |
return computeCursorSpriteAtArea(minx, miny, maxx, maxy, 1, 1) |
| 900 |
end |
| 901 |
|
| 902 |
elseif self.cursor.state == "both" then |
| 903 |
-- left+right drag (left initiated). |
| 904 |
-- Area is defined from first to last point, 'right' point must be inside the area. |
| 905 |
local minx, miny, maxx, maxy = getCoveredArea(self.cursor.leftx, self.cursor.lefty, |
| 906 |
self.cursor.xpos, self.cursor.ypos) |
| 907 |
if minx ~= maxx or miny ~= maxy then -- Otherwise area is 1x1, either due to 'cancel', or never enlarged. |
| 908 |
if isPointInside(self.cursor.rightx, self.cursor.righty, minx, miny, maxx, maxy) then |
| 909 |
-- Get block size between left and right click. |
| 910 |
local dx, dy = getAreaSize(self.cursor.leftx, self.cursor.lefty, |
| 911 |
self.cursor.rightx, self.cursor.righty) |
| 912 |
if dx > 1 or dy > 1 then -- Otherwise, left and right click is in the same tile. |
| 913 |
return computeCursorSpriteAtArea(minx, miny, maxx, maxy, dx, dy) |
| 914 |
end |
| 915 |
end |
| 916 |
end |
| 917 |
end |
| 918 |
|
| 919 |
return {} |
| 920 |
end |
| 921 |
|
| 922 |
--! Fill an area of tiles with red cursor rectangles. Caller must make sure that |
| 923 |
-- the area is completely inside the world boundaries. |
| 924 |
--!param canvas Canvas to draw at. |
| 925 |
--!param xpos (int) Horizontal base tile position (of the top corner). |
| 926 |
--!param ypos (int) Vertical base tile position (of the top corner). |
| 927 |
--!param xsize (int) Horizontal size in tiles. |
| 928 |
--!param ysize (int) Vertical size in tiles. |
| 929 |
function UIMapEditor:fillCursorArea(canvas, xpos, ypos, xsize, ysize) |
| 930 |
local ui = self.ui |
| 931 |
local zoom = ui.zoom_factor |
| 932 |
|
| 933 |
for x = 0, xsize - 1 do |
| 934 |
for y = 0, ysize - 1 do |
| 935 |
local xcoord, ycoord = ui:WorldToScreen(xpos + x, ypos + y) |
| 936 |
self.cell_outline:draw(canvas, 2, math.floor(xcoord / zoom) - 32, math.floor(ycoord / zoom)) |
| 937 |
end |
| 938 |
end |
| 939 |
end |
| 940 |
|
| 941 |
--! Draw the display (map editor window, and main world display) |
| 942 |
--!param canvas (draw object) Canvas to draw on. |
| 943 |
function UIMapEditor:draw(canvas, ...) |
| 944 |
local ui = self.ui |
| 945 |
|
| 946 |
-- Draw the red grid for the selected tile. |
| 947 |
local coords = self:getDrawPoints() |
| 948 |
if #coords ~= 0 then |
| 949 |
-- Get size of the cursor. |
| 950 |
local xsize, ysize |
| 951 |
if self.cursor.state == "delete" or self.cursor.state == "delete-left" or |
| 952 |
self.cursor.state == "parcel" or self.cursor.state == "parcel-left" or |
| 953 |
self.cursor.state == "heliport" or self.cursor.state == "camera" or |
| 954 |
self.cursor.state == "paste-left" or self.cursor.state == "right" then |
| 955 |
xsize, ysize = 1, 1 |
| 956 |
elseif self.cursor.state == "paste" then |
| 957 |
xsize, ysize = self.cursor.copy_xsize, self.cursor.copy_ysize |
| 958 |
else |
| 959 |
xsize, ysize = self.cursor.sprite.xsize, self.cursor.sprite.ysize |
| 960 |
end |
| 961 |
-- Draw cursors. |
| 962 |
local scaled = canvas:scale(ui.zoom_factor) |
| 963 |
for _, coord in ipairs(coords) do |
| 964 |
local xpos, ypos = coord.xpos, coord.ypos |
| 965 |
self:fillCursorArea(canvas, xpos, ypos, xsize, ysize) |
| 966 |
end |
| 967 |
if scaled then |
| 968 |
canvas:scale(1) |
| 969 |
end |
| 970 |
end |
| 971 |
|
| 972 |
UIResizable.draw(self, canvas, ...) |
| 973 |
end |
| 974 |
|
| 975 |
-- {{{ several useful functions |
| 976 |
--! User clicked at a block (a button with a sprite). |
| 977 |
--!param num Index block number. |
| 978 |
function UIMapEditor:blockClicked(num) |
| 979 |
-- Reset toggle of other block buttons. |
| 980 |
for bnum = 1, #self.block_buttons do |
| 981 |
if bnum ~= num then self.block_buttons[bnum].button:setToggleState(false) end |
| 982 |
end |
| 983 |
|
| 984 |
if self.block_buttons[num].button.toggled then |
| 985 |
local sprite = self.selected_block[num] |
| 986 |
self.cursor.state = "grid" |
| 987 |
self.cursor.sprite = {xsize = sprite.xsize, |
| 988 |
ysize = sprite.ysize, |
| 989 |
sprites = sprite.sprites, |
| 990 |
objects = sprite.objects, |
| 991 |
type = sprite.type} |
| 992 |
else |
| 993 |
self.cursor.state = "disabled" |
| 994 |
self.cursor.sprite = nil |
| 995 |
end |
| 996 |
end |
| 997 |
|
| 998 |
--! Convert mouse coordinates to tile coordinates in the world. |
| 999 |
--!param mx (int) Mouse X screen coordinate. |
| 1000 |
--!param my (int) Mouse y screen coordinate. |
| 1001 |
--!return (int, int) Tile x,y coordinates, limited to the map. |
| 1002 |
function UIMapEditor:mouseToWorld(mx, my) |
| 1003 |
local ui = self.ui |
| 1004 |
local s = TheApp.config.ui_scale |
| 1005 |
|
| 1006 |
local wxr, wyr = ui:ScreenToWorld(self.x * s + mx, self.y * s + my) |
| 1007 |
local wx = math.floor(wxr) |
| 1008 |
local wy = math.floor(wyr) |
| 1009 |
return self:areaOnWorld(wx, wy, 1, 1) |
| 1010 |
end |
| 1011 |
|
| 1012 |
--! Stay on world with the entire area, by moving the base position (if needed). |
| 1013 |
--!param xpos (int) Horizontal base position. |
| 1014 |
--!param ypos (int) Vertical base position. |
| 1015 |
--!param xsize (int) Horizontal size. |
| 1016 |
--!param ysize (int) Vertical size. |
| 1017 |
--!return (int, int) Allowed base position |
| 1018 |
function UIMapEditor:areaOnWorld(xpos, ypos, xsize, ysize) |
| 1019 |
local map = self.ui.app.map |
| 1020 |
|
| 1021 |
xpos = math.min(math.max(xpos, 1), map.width - xsize + 1) |
| 1022 |
ypos = math.min(math.max(ypos, 1), map.height - ysize + 1) |
| 1023 |
return xpos, ypos |
| 1024 |
end |
| 1025 |
-- }}} |
| 1026 |
|
| 1027 |
--! Retrieve what drag capabilities are allowed by the currently selected world cursor sprite. |
| 1028 |
--!return (string) "none"=not draggable, "east-west"=dragging only in east-west direction, |
| 1029 |
-- "north-south"=dragging only in north-south direction, "area"=dragging in both directions. |
| 1030 |
function UIMapEditor:getCursorDragCapabilities() |
| 1031 |
-- Parcel and delete modes have unrestricted movement. |
| 1032 |
if self.cursor.state == "delete" or self.cursor.state == "delete-left" or |
| 1033 |
self.cursor.state == "parcel" or self.cursor.state == "parcel-left" or |
| 1034 |
self.cursor.state == "paste" or self.cursor.state == "paste-left" or |
| 1035 |
self.cursor.state == "right" then |
| 1036 |
return "area" |
| 1037 |
end |
| 1038 |
|
| 1039 |
-- No sprite, or not a 1x1 size -> not draggable. |
| 1040 |
if not self.cursor.sprite then return "none" end |
| 1041 |
if self.cursor.sprite.xsize ~= 1 or self.cursor.sprite.ysize ~= 1 then |
| 1042 |
return "none" |
| 1043 |
end |
| 1044 |
|
| 1045 |
if isFloorSpriteType(self.cursor.sprite.type) then return "area" end |
| 1046 |
if self.cursor.sprite.type == "north" then return "east-west" end |
| 1047 |
if self.cursor.sprite.type == "west" then return "north-south" end |
| 1048 |
assert(false) -- Should never get here |
| 1049 |
end |
| 1050 |
|
| 1051 |
--! The user moved the mouse! |
| 1052 |
--!param x (int) New horizontal position of the mouse at the screen. |
| 1053 |
--!param y (int) New vertical position of the mouse at the screen. |
| 1054 |
--!param dx (int) Horizontal shift in position of the mouse at the screen. |
| 1055 |
--!param dy (int) Vertical shift in position of the mouse at the screen. |
| 1056 |
function UIMapEditor:onMouseMove(x, y, dx, dy) |
| 1057 |
local repaint = UIResizable.onMouseMove(self, x, y, dx, dy) |
| 1058 |
|
| 1059 |
-- Update the stored state of cursor position, and trigger a repaint as the |
| 1060 |
-- cell outline sprite should track the cursor position. |
| 1061 |
local wx, wy = self:mouseToWorld(x, y) |
| 1062 |
if wx ~= self.cursor.xpos or wy ~= self.cursor.ypos then |
| 1063 |
|
| 1064 |
if self.cursor.state == "disabled" then |
| 1065 |
self.cursor.xpos = wx -- Nothing is displayed, just keep track of the position. |
| 1066 |
self.cursor.ypos = wy |
| 1067 |
return repaint |
| 1068 |
|
| 1069 |
elseif self.cursor.state == "grid" or self.cursor.state == "delete" or |
| 1070 |
self.cursor.state == "parcel" or self.cursor.state == "paste" or |
| 1071 |
self.cursor.state == "heliport" or self.cursor.state == "camera" or |
| 1072 |
self.cursor.state == "right" then |
| 1073 |
self.cursor.xpos = wx -- Allow arbitrary movement. |
| 1074 |
self.cursor.ypos = wy |
| 1075 |
return true |
| 1076 |
|
| 1077 |
else -- Dragging in some mode. |
| 1078 |
self.cursor.is_drag = true -- Crossing a tile boundary implies 'real' dragging. |
| 1079 |
|
| 1080 |
-- Update wx and wy according to drag capabilities. |
| 1081 |
local cap = self:getCursorDragCapabilities() |
| 1082 |
if cap == "north-south" then wx = self.cursor.leftx |
| 1083 |
elseif cap == "east-west" then wy = self.cursor.lefty |
| 1084 |
elseif cap == "none" then wx, wy = self.cursor.leftx, self.cursor.lefty -- Block all movement. |
| 1085 |
end |
| 1086 |
|
| 1087 |
repaint = repaint or self.cursor.xpos ~= wx or self.cursor.ypos ~= wy |
| 1088 |
self.cursor.xpos = wx |
| 1089 |
self.cursor.ypos = wy |
| 1090 |
return repaint |
| 1091 |
end |
| 1092 |
end |
| 1093 |
end |
| 1094 |
|
| 1095 |
--! Mouse button got pressed. |
| 1096 |
--!param button (string) Mouse button being pressed. |
| 1097 |
--!param xpos (int) Horizontal position of the mouse at the time of the mouse button press. |
| 1098 |
--!param ypos (int) Vertical position of the mouse at the time of the mouse button press. |
| 1099 |
--!return (bool) Whether to repaint the display. |
| 1100 |
function UIMapEditor:onMouseDown(button, xpos, ypos) |
| 1101 |
if UIResizable.onMouseDown(self, button, xpos, ypos) then -- Button in this window. |
| 1102 |
return true |
| 1103 |
end |
| 1104 |
|
| 1105 |
if self:hitTest(xpos, ypos) then -- Clicked elsewhere in the window. |
| 1106 |
return true |
| 1107 |
end |
| 1108 |
|
| 1109 |
local repaint = false |
| 1110 |
if self.cursor.state == "disabled" then |
| 1111 |
if button == "right" then |
| 1112 |
self.cursor.state = "right" |
| 1113 |
self.cursor.rightx = self.cursor.xpos |
| 1114 |
self.cursor.righty = self.cursor.ypos |
| 1115 |
end |
| 1116 |
|
| 1117 |
elseif self.cursor.state == "grid" then |
| 1118 |
-- LMB down switches to 'left' |
| 1119 |
if button == "left" then |
| 1120 |
self.cursor.state = "left" |
| 1121 |
self.cursor.leftx = self.cursor.xpos |
| 1122 |
self.cursor.lefty = self.cursor.ypos |
| 1123 |
-- RMB down switches to 'right' |
| 1124 |
elseif button == "right" then |
| 1125 |
self.cursor.state = "right" |
| 1126 |
self.cursor.rightx = self.cursor.xpos |
| 1127 |
self.cursor.righty = self.cursor.ypos |
| 1128 |
end |
| 1129 |
-- Since 'grid' already shows the red rectangles, nothing changes visually. |
| 1130 |
|
| 1131 |
elseif self.cursor.state == "delete" then |
| 1132 |
if button == "left" then |
| 1133 |
self.cursor.state = "delete-left" |
| 1134 |
self.cursor.leftx = self.cursor.xpos |
| 1135 |
self.cursor.lefty = self.cursor.ypos |
| 1136 |
elseif button == "right" then |
| 1137 |
self.cursor.state = "right" |
| 1138 |
self.cursor.rightx = self.cursor.xpos |
| 1139 |
self.cursor.righty = self.cursor.ypos |
| 1140 |
end |
| 1141 |
|
| 1142 |
elseif self.cursor.state == "parcel" then |
| 1143 |
if button == "left" then |
| 1144 |
self.cursor.state = "parcel-left" |
| 1145 |
self.cursor.leftx = self.cursor.xpos |
| 1146 |
self.cursor.lefty = self.cursor.ypos |
| 1147 |
elseif button == "right" then |
| 1148 |
self.cursor.state = "right" |
| 1149 |
self.cursor.rightx = self.cursor.xpos |
| 1150 |
self.cursor.righty = self.cursor.ypos |
| 1151 |
end |
| 1152 |
|
| 1153 |
elseif self.cursor.state == "left" then |
| 1154 |
-- If RMB is down, switch to 'both'. |
| 1155 |
if button == "right" then |
| 1156 |
self.cursor.state = "both" |
| 1157 |
self.cursor.rightx = self.cursor.xpos |
| 1158 |
self.cursor.righty = self.cursor.ypos |
| 1159 |
repaint = true |
| 1160 |
end |
| 1161 |
|
| 1162 |
elseif self.cursor.state == "paste" then |
| 1163 |
if button == "left" then |
| 1164 |
self.cursor.state = "paste-left" |
| 1165 |
self.cursor.leftx = self.cursor.xpos |
| 1166 |
self.cursor.lefty = self.cursor.ypos |
| 1167 |
elseif button == "right" then |
| 1168 |
self.cursor.state = "right" |
| 1169 |
self.cursor.rightx = self.cursor.xpos |
| 1170 |
self.cursor.righty = self.cursor.ypos |
| 1171 |
end |
| 1172 |
|
| 1173 |
-- "both", "delete-left", "parcel-left", "paste-left" do not handle buttons. |
| 1174 |
-- Ignore all down buttons, until RMB is released. |
| 1175 |
|
| 1176 |
end |
| 1177 |
|
| 1178 |
return repaint |
| 1179 |
end |
| 1180 |
|
| 1181 |
--! Add an object to the map. Currently, only "entrance_door" is supported. |
| 1182 |
--!param obj_type (str) Type of object ("entrance_door") |
| 1183 |
--!param xpos (int) Desired x position of the new object. |
| 1184 |
--!param ypos (int) Desired y position of the new object. |
| 1185 |
--!param direction (str) Direction of the new object ("north" or "west"). |
| 1186 |
function UIMapEditor:drawObject(obj_type, xpos, ypos, direction) |
| 1187 |
local world = self.ui.app.world |
| 1188 |
|
| 1189 |
-- TheApp.objects[name].thob |
| 1190 |
-- name = world.object_id_by_thob[thob] |
| 1191 |
-- generic object = world.object_types[object_id] |
| 1192 |
-- instance: world:getObject(x, y, name) |
| 1193 |
if obj_type == "entrance_door" then |
| 1194 |
world:newObject("entrance_right_door", xpos, ypos, direction) |
| 1195 |
if direction == "north" then |
| 1196 |
world:newObject("entrance_left_door", xpos - 1, ypos, direction) |
| 1197 |
else |
| 1198 |
world:newObject("entrance_left_door", xpos, ypos - 1, direction) |
| 1199 |
end |
| 1200 |
end |
| 1201 |
end |
| 1202 |
|
| 1203 |
--! Remove an entrance door from the world. |
| 1204 |
--!param door Entrance door to remove. |
| 1205 |
function UIMapEditor:removeDoor(door) |
| 1206 |
local world = self.ui.app.world |
| 1207 |
|
| 1208 |
world:destroyEntity(door) |
| 1209 |
if door.slave then |
| 1210 |
world:destroyEntity(door.slave) |
| 1211 |
end |
| 1212 |
end |
| 1213 |
|
| 1214 |
--! Collect other objects that use the space needed for the specified new object. |
| 1215 |
-- If they exist, return them or delete them. |
| 1216 |
--!param obj_type (str) Type of object ("entrance_door") |
| 1217 |
--!param xpos (int) Desired x position of the new object. |
| 1218 |
--!param ypos (int) Desired y position of the new object. |
| 1219 |
--!param direction (str) Direction of the new object ("north" or "west"). |
| 1220 |
--!param remove (bool) If set, remove the found objects. |
| 1221 |
--!return (list) The objects that use the space, if they are not removed. |
| 1222 |
function UIMapEditor:checkObjectSpace(obj_type, xpos, ypos, direction, remove) |
| 1223 |
local world = self.ui.app.world |
| 1224 |
local right_door = world.object_types["entrance_right_door"] |
| 1225 |
local left_door = world.object_types["entrance_left_door"] |
| 1226 |
local th = self.ui.app.map.th |
| 1227 |
|
| 1228 |
--! Check single tile for conflicts with other doors. |
| 1229 |
--!param x X position of the tile to check. |
| 1230 |
--!param y Y position of the tile to check. |
| 1231 |
--!return (int, int) position of the conflicting door, or (nil, nil) if no conflict. |
| 1232 |
local function checkTile(x, y) |
| 1233 |
local all_flags = th:getCellFlags(x, y) |
| 1234 |
if not all_flags.thob then |
| 1235 |
return nil, nil |
| 1236 |
elseif all_flags.thob == right_door.thob then |
| 1237 |
return x, y |
| 1238 |
elseif all_flags.thob == left_door.thob then |
| 1239 |
if all_flags.tallWest then |
| 1240 |
return x, y + 1 |
| 1241 |
else |
| 1242 |
return x + 1, y |
| 1243 |
end |
| 1244 |
end |
| 1245 |
return nil, nil |
| 1246 |
end |
| 1247 |
|
| 1248 |
-- While the general intention is 'objects', the only object that can exist |
| 1249 |
-- and is handled here is the entrance door. |
| 1250 |
assert(obj_type == "entrance_door") |
| 1251 |
local doors = {} |
| 1252 |
|
| 1253 |
local x, y = checkTile(xpos, ypos) |
| 1254 |
if x then |
| 1255 |
doors[#doors + 1] = world:getObject(x, y, "entrance_right_door") |
| 1256 |
end |
| 1257 |
|
| 1258 |
local x2, y2 |
| 1259 |
if direction == "north" then |
| 1260 |
x2, y2 = checkTile(xpos - 1, ypos) |
| 1261 |
else |
| 1262 |
x2, y2 = checkTile(xpos, ypos - 1) |
| 1263 |
end |
| 1264 |
|
| 1265 |
if x2 and (x2 ~= x or y2 ~= y) then |
| 1266 |
doors[#doors + 1] = world:getObject(x2, y2, "entrance_right_door") |
| 1267 |
end |
| 1268 |
|
| 1269 |
if remove then |
| 1270 |
for _, door in ipairs(doors) do self:removeDoor(door) end |
| 1271 |
return |
| 1272 |
end |
| 1273 |
return doors |
| 1274 |
end |
| 1275 |
|
| 1276 |
--! Recognize objects in the collection of thob+tallWest entries |
| 1277 |
--!param minx (int) Base horizontal position (objects should be put relative to it). |
| 1278 |
--!param miny (int) Base vertical position (objects should be put relative to it). |
| 1279 |
--!param thobdir_positions (table xy to {thob, tallWest}) Found thobs. |
| 1280 |
--!return (array of {type, xpos, ypos, direction}) Found objects. |
| 1281 |
function UIMapEditor:findObjects(minx, miny, thobdir_positions) |
| 1282 |
local world = self.ui.app.world |
| 1283 |
local right_door = world.object_types["entrance_right_door"] |
| 1284 |
local left_door = world.object_types["entrance_left_door"] |
| 1285 |
|
| 1286 |
local objects = {} -- Found objects ordered by position. |
| 1287 |
|
| 1288 |
-- Look for right entrance door. |
| 1289 |
for xy_right, thobdir_right in pairs(thobdir_positions) do |
| 1290 |
if right_door.thob == thobdir_right.thob then |
| 1291 |
-- Found right door, is there a matching left door? |
| 1292 |
local xy_left = thobdir_right.tallWest and xy_right - 256 or xy_right - 1 |
| 1293 |
local thobdir_left = thobdir_positions[xy_left] |
| 1294 |
if thobdir_left and thobdir_left.thob == left_door.thob and |
| 1295 |
thobdir_left.tallWest == thobdir_right.tallWest then |
| 1296 |
local obj = {type="entrance_door", |
| 1297 |
xpos = xy_right % 256 - minx + 1, |
| 1298 |
ypos = math.floor(xy_right / 256) - miny + 1, |
| 1299 |
direction=thobdir_right.tallWest and "west" or "north"} |
| 1300 |
objects[#objects + 1] = obj |
| 1301 |
end |
| 1302 |
end |
| 1303 |
end |
| 1304 |
|
| 1305 |
return objects |
| 1306 |
end |
| 1307 |
|
| 1308 |
--! Draw the selected sprite at the given coordinates. |
| 1309 |
--!param coords (array or {xpos, ypos} tables) Coordinates to draw the selected sprite. |
| 1310 |
function UIMapEditor:drawCursorSpriteAtArea(coords) |
| 1311 |
local th = self.ui.app.map.th |
| 1312 |
|
| 1313 |
if self.cursor.sprite then |
| 1314 |
for _, coord in ipairs(coords) do |
| 1315 |
local xbase, ybase = coord.xpos, coord.ypos |
| 1316 |
|
| 1317 |
-- Draw the selected sprite. |
| 1318 |
xbase, ybase = self:areaOnWorld(xbase, ybase, self.cursor.sprite.xsize, self.cursor.sprite.ysize) |
| 1319 |
for _, spr in ipairs(self.cursor.sprite.sprites) do |
| 1320 |
local tx, ty = xbase + spr.xpos - 1, ybase + spr.ypos - 1 |
| 1321 |
local f, nw, ww = th:getCell(tx, ty) -- floor, north-wall, west-wall (, ui) |
| 1322 |
if isFloorSpriteType(spr.type) then |
| 1323 |
f = spr.sprite |
| 1324 |
-- Floor sprite gets changed, also modify the cell flags. |
| 1325 |
th:setCellFlags(tx, ty, makeCellFlags(spr.type)) |
| 1326 |
|
| 1327 |
elseif spr.type == "north" then |
| 1328 |
nw = spr.sprite |
| 1329 |
|
| 1330 |
elseif spr.type == "west" then |
| 1331 |
ww = spr.sprite |
| 1332 |
end |
| 1333 |
|
| 1334 |
th:setCell(tx, ty, f, nw, ww, 0) |
| 1335 |
end |
| 1336 |
|
| 1337 |
-- Draw the objects |
| 1338 |
if self.cursor.sprite.objects then |
| 1339 |
for _, obj in ipairs(self.cursor.sprite.objects) do |
| 1340 |
local tx, ty = xbase + obj.xpos - 1, ybase + obj.ypos - 1 |
| 1341 |
self:drawObject(obj.type, tx, ty, obj.direction) |
| 1342 |
end |
| 1343 |
end |
| 1344 |
end |
| 1345 |
end |
| 1346 |
end |
| 1347 |
|
| 1348 |
--! Copy area from the game. |
| 1349 |
--!return (bool) Whether copying succeeded. |
| 1350 |
function UIMapEditor:copyArea() |
| 1351 |
local th = self.ui.app.map.th |
| 1352 |
|
| 1353 |
local minx, miny, maxx, maxy = getCoveredArea(self.cursor.rightx, self.cursor.righty, |
| 1354 |
self.cursor.xpos, self.cursor.ypos) |
| 1355 |
if minx == maxx and miny == maxy and self.is_drag then |
| 1356 |
return false -- Canceled drag |
| 1357 |
end |
| 1358 |
|
| 1359 |
local thobdir_positions = {} |
| 1360 |
|
| 1361 |
-- Copy data. |
| 1362 |
self.cursor.copy_data = {} |
| 1363 |
self.cursor.copy_xsize = maxx - minx + 1 |
| 1364 |
self.cursor.copy_ysize = maxy - miny + 1 |
| 1365 |
|
| 1366 |
local tx, ty |
| 1367 |
tx = minx |
| 1368 |
while tx <= maxx do |
| 1369 |
ty = miny |
| 1370 |
while ty <= maxy do |
| 1371 |
local f, nw, ww = th:getCell(tx, ty) |
| 1372 |
local all_flags = th:getCellFlags(tx, ty) |
| 1373 |
self.cursor.copy_data[#self.cursor.copy_data + 1] = { |
| 1374 |
xpos = tx - minx, |
| 1375 |
ypos = ty - miny, |
| 1376 |
floor = f, |
| 1377 |
north_wall = nw, |
| 1378 |
west_wall = ww, |
| 1379 |
flags = {buildable = all_flags.buildable, |
| 1380 |
passable = all_flags.passable, |
| 1381 |
hospital = all_flags.hospital}, |
| 1382 |
} |
| 1383 |
if all_flags.thob and all_flags.thob ~= 0 then |
| 1384 |
thobdir_positions[tx + 256 * ty] = {thob = all_flags.thob, |
| 1385 |
tallWest = all_flags.tallWest} |
| 1386 |
end |
| 1387 |
|
| 1388 |
ty = ty + 1 |
| 1389 |
end |
| 1390 |
tx = tx + 1 |
| 1391 |
end |
| 1392 |
self.cursor.copy_objects = self:findObjects(minx, miny, thobdir_positions) |
| 1393 |
|
| 1394 |
return true |
| 1395 |
end |
| 1396 |
|
| 1397 |
--! Paste copied area into the destination area (one or more times). |
| 1398 |
function UIMapEditor:pasteArea() |
| 1399 |
local th = self.ui.app.map.th |
| 1400 |
|
| 1401 |
local minx, miny, maxx, maxy |
| 1402 |
-- Fill 'minx', 'miny' with the non-cursor corner. |
| 1403 |
if self.cursor.leftx == self.cursor.xpos and self.cursor.lefty == self.cursor.ypos then |
| 1404 |
if self.is_drag then |
| 1405 |
return -- Canceled paste area. |
| 1406 |
end |
| 1407 |
|
| 1408 |
minx = self.cursor.xpos + self.cursor.copy_xsize - 1 |
| 1409 |
miny = self.cursor.ypos + self.cursor.copy_ysize - 1 |
| 1410 |
minx, miny = self:areaOnWorld(minx, miny, 1, 1) |
| 1411 |
else |
| 1412 |
minx = self.cursor.leftx |
| 1413 |
miny = self.cursor.lefty |
| 1414 |
end |
| 1415 |
|
| 1416 |
minx, miny, maxx, maxy = getCoveredArea(minx, miny, self.cursor.xpos, self.cursor.ypos) |
| 1417 |
-- Copy area in 'sub-areas' of (self.cursor.copy_xsize, self.cursor.copy_ysize). |
| 1418 |
local tx, ty |
| 1419 |
tx = minx |
| 1420 |
while tx <= maxx do |
| 1421 |
ty = miny |
| 1422 |
while ty <= maxy do |
| 1423 |
-- Copy floor and wall sprites, and set the passable/buildable/hospital flag. |
| 1424 |
for _, elm in ipairs(self.cursor.copy_data) do |
| 1425 |
local x, y = tx + elm.xpos, ty + elm.ypos |
| 1426 |
if x <= maxx and y <= maxy then |
| 1427 |
th:setCell(x, y, elm.floor, elm.north_wall, elm.west_wall, 0) |
| 1428 |
th:setCellFlags(x, y, elm.flags) |
| 1429 |
end |
| 1430 |
end |
| 1431 |
-- Make room for the new objects. |
| 1432 |
for _, obj in ipairs(self.cursor.copy_objects) do |
| 1433 |
local x, y = tx + obj.xpos - 1, ty + obj.ypos - 1 |
| 1434 |
if x <= maxx and y <= maxy then |
| 1435 |
self:checkObjectSpace(obj.type, x, y, obj.direction, true) |
| 1436 |
end |
| 1437 |
end |
| 1438 |
-- Paste the objects of the copied area. |
| 1439 |
for _, obj in ipairs(self.cursor.copy_objects) do |
| 1440 |
local x, y = tx + obj.xpos - 1, ty + obj.ypos - 1 |
| 1441 |
if x <= maxx and y <= maxy then |
| 1442 |
self:drawObject(obj.type, x, y, obj.direction) |
| 1443 |
end |
| 1444 |
end |
| 1445 |
|
| 1446 |
ty = ty + self.cursor.copy_ysize |
| 1447 |
end |
| 1448 |
tx = tx + self.cursor.copy_xsize |
| 1449 |
end |
| 1450 |
end |
| 1451 |
|
| 1452 |
--! Delete the walls at the given coordinates. |
| 1453 |
--!param coords (array or {xpos, ypos} tables) Coordinates to remove the walls. |
| 1454 |
function UIMapEditor:deleteWallsAtArea(coords) |
| 1455 |
local th = self.ui.app.map.th |
| 1456 |
|
| 1457 |
local thobdir_positions = {} -- Storage for found objects. |
| 1458 |
|
| 1459 |
for _, coord in ipairs(coords) do |
| 1460 |
local tx, ty = coord.xpos, coord.ypos |
| 1461 |
-- Remove west and north wall. |
| 1462 |
local f = th:getCell(tx, ty) -- floor (, north-wall, west-wall , ui) |
| 1463 |
th:setCell(tx, ty, f, 0, 0, 0) |
| 1464 |
-- No need for map:setCellFlags, as 'passable', 'buildable', and 'hospital' are floor properties. |
| 1465 |
|
| 1466 |
-- Collect thobs, to remove next. |
| 1467 |
local all_flags = th:getCellFlags(tx, ty) |
| 1468 |
if all_flags.thob ~= 0 then |
| 1469 |
thobdir_positions[tx + ty * 256] = {thob=all_flags.thob, |
| 1470 |
tallWest=all_flags.tallWest} |
| 1471 |
end |
| 1472 |
end |
| 1473 |
|
| 1474 |
-- Remove objects from the area. |
| 1475 |
local objects = self:findObjects(1, 1, thobdir_positions) -- Uses absolute position for the objects |
| 1476 |
for _, obj in ipairs(objects) do |
| 1477 |
self:checkObjectSpace(obj.type, obj.xpos, obj.ypos, obj.direction, true) |
| 1478 |
end |
| 1479 |
end |
| 1480 |
|
| 1481 |
--! Set parcel number at the given coordinates. |
| 1482 |
--!param coords (array or {xpos, ypos} tables) Coordinates to set parcel. |
| 1483 |
--!param parcel_num (int) Parcel number to set. |
| 1484 |
function UIMapEditor:setParcelAtArea(coords, parcel_num) |
| 1485 |
local th = self.ui.app.map.th |
| 1486 |
|
| 1487 |
for _, coord in ipairs(coords) do |
| 1488 |
local tx, ty = coord.xpos, coord.ypos |
| 1489 |
th:setCellFlags(tx, ty, {parcelId = parcel_num}) |
| 1490 |
end |
| 1491 |
end |
| 1492 |
|
| 1493 |
--! Mouse button was released. |
| 1494 |
--!param button (string) Mouse button being released. |
| 1495 |
--!param x (int) Horizontal position of the mouse at the time of the mouse button release. |
| 1496 |
--!param y (int) Vertical position of the mouse at the time of the mouse button release. |
| 1497 |
--!return (bool) Whether to repaint the display. |
| 1498 |
function UIMapEditor:onMouseUp(button, x, y) |
| 1499 |
local map = self.ui.app.map |
| 1500 |
|
| 1501 |
if UIResizable.onMouseUp(self, button, x, y) then |
| 1502 |
return true |
| 1503 |
end |
| 1504 |
|
| 1505 |
if self:hitTest(x, y) then |
| 1506 |
return true |
| 1507 |
end |
| 1508 |
|
| 1509 |
--! Get the cursor mode to jump to after ending the drag mode. |
| 1510 |
--!return (string) New cursor mode. |
| 1511 |
local function newState() |
| 1512 |
if self.cursor.sprite then |
| 1513 |
return "grid" |
| 1514 |
else |
| 1515 |
return "disabled" |
| 1516 |
end |
| 1517 |
end |
| 1518 |
|
| 1519 |
|
| 1520 |
local repaint = false |
| 1521 |
if self.cursor.state == "disabled" then |
| 1522 |
return repaint -- Don't care about buttons |
| 1523 |
|
| 1524 |
elseif self.cursor.state == "left" then |
| 1525 |
if button == "left" then |
| 1526 |
-- Simple drag (left button only). |
| 1527 |
self:drawCursorSpriteAtArea(self:getDrawPoints()) |
| 1528 |
|
| 1529 |
self.cursor.state = newState() |
| 1530 |
self.cursor.is_drag = false |
| 1531 |
repaint = true |
| 1532 |
end |
| 1533 |
|
| 1534 |
elseif self.cursor.state == "delete-left" then |
| 1535 |
if button == "left" then |
| 1536 |
self:deleteWallsAtArea(self:getDrawPoints()) |
| 1537 |
|
| 1538 |
self.cursor.state = "delete" |
| 1539 |
self.cursor.is_drag = false |
| 1540 |
repaint = true |
| 1541 |
end |
| 1542 |
|
| 1543 |
elseif self.cursor.state == "parcel-left" then |
| 1544 |
if button == "left" then |
| 1545 |
self:setParcelAtArea(self:getDrawPoints(), self.cursor.parcel) |
| 1546 |
map:updateDebugOverlay() |
| 1547 |
|
| 1548 |
self.cursor.state = "parcel" |
| 1549 |
self.cursor.is_drag = false |
| 1550 |
repaint = true |
| 1551 |
end |
| 1552 |
|
| 1553 |
elseif self.cursor.state == "camera" then |
| 1554 |
if button == "left" then |
| 1555 |
local dp = self:getDrawPoints() |
| 1556 |
map:setCameraTile(dp[1].xpos, dp[1].ypos, self.cursor.camera) |
| 1557 |
map:updateDebugOverlay() |
| 1558 |
repaint = true |
| 1559 |
end |
| 1560 |
|
| 1561 |
elseif self.cursor.state == "heliport" then |
| 1562 |
if button == "left" then |
| 1563 |
local dp = self:getDrawPoints() |
| 1564 |
map:setHeliportTile(dp[1].xpos, dp[1].ypos, self.cursor.heliport) |
| 1565 |
map:updateDebugOverlay() |
| 1566 |
repaint = true |
| 1567 |
end |
| 1568 |
|
| 1569 |
elseif self.cursor.state == "right" then |
| 1570 |
if button == "right" then |
| 1571 |
if self:copyArea() then |
| 1572 |
self.cursor.state = "paste" |
| 1573 |
self:pageClicked("paste") |
| 1574 |
self.cursor.is_drag = false |
| 1575 |
repaint = true |
| 1576 |
else |
| 1577 |
self.cursor.state = "disabled" -- Copy area was canceled. |
| 1578 |
self.cursor.is_drag = false |
| 1579 |
repaint = true |
| 1580 |
end |
| 1581 |
end |
| 1582 |
|
| 1583 |
elseif self.cursor.state == "paste-left" then |
| 1584 |
if button == "left" then |
| 1585 |
self.cursor.state = "paste" |
| 1586 |
self.cursor.is_drag = false |
| 1587 |
repaint = true |
| 1588 |
self:pasteArea() |
| 1589 |
end |
| 1590 |
|
| 1591 |
elseif self.cursor.state == "both" then |
| 1592 |
if button == "left" then -- Only care about left button. |
| 1593 |
-- left+right drag ends. |
| 1594 |
self:drawCursorSpriteAtArea(self:getDrawPoints()) |
| 1595 |
|
| 1596 |
self.cursor.state = newState() |
| 1597 |
self.cursor.is_drag = false |
| 1598 |
repaint = true |
| 1599 |
end |
| 1600 |
-- "grid", "delete", "parcel", and "paste" already assumed no buttons pushed, |
| 1601 |
-- ignore event. |
| 1602 |
|
| 1603 |
end |
| 1604 |
|
| 1605 |
return repaint |
| 1606 |
end |
| 1607 |
|