~kalon33/corsix-th/master

« back to all changes in this revision

Viewing changes to CorsixTH/Lua/dialogs/map_editor.lua

  • Committer: corsixth.bot at gmail
  • Date: 2014-03-31 23:30:23 UTC
  • Revision ID: svn-v4:c39591fa-788f-11de-a72b-d90af8dea425:trunk:2687
Remove trailing whitespaces in .h, .cpp, .c and .lua files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
  self.width = math.huge
39
39
  self.height = math.huge
40
40
  self.ui = ui
41
 
  
 
41
 
42
42
  self.command_stack = CommandStack()
43
43
 
44
44
 
45
 
  
 
45
 
46
46
  -- For when there are multiple things which could be sampled from a tile,
47
47
  -- keep track of the index of which one was most recently sampled, so that
48
48
  -- next time a different one is sampled.
49
49
  self.sample_i = 1
50
 
  
 
50
 
51
51
  -- The block to put on the UI layer as a preview for what will be placed
52
52
  -- by the current drawing operation.
53
53
  self.block_brush_preview = 0
54
 
  
 
54
 
55
55
  self:classifyBlocks()
56
 
  
 
56
 
57
57
  -- A sprite table containing a "cell outline" sprite
58
58
  self.cell_outline = TheApp.gfx:loadSpriteTable("Bitmap", "aux_ui", true)
59
 
  
 
59
 
60
60
  -- Coordinates in Lua tile space of the mouse cursor.
61
61
  self.mouse_cell_x = 0
62
62
  self.mouse_cell_y = 0
63
63
end
64
 
  
 
64
 
65
65
function UIMapEditor:classifyBlocks()
66
66
  -- Classify each block / tile with a type, subtype, and category.
67
67
  -- Type and subtype are used by this file, and by the UI code to determine
139
139
    block_info[i] = {"floor", "simple", "Outside"}
140
140
  end
141
141
  block_info[208].base = 3
 
142
-- adds street lights, could do with mirrors of these to have lamps facing different directions
142
143
  for i = 209, 210 do
143
144
  local pair
144
145
  local category = "External"
145
 
  local dir = i % 2 == 0 and "north" or "west" 
146
 
  if i ~= 209 then 
 
146
  local dir = i % 2 == 0 and "north" or "west"
 
147
  if i ~= 209 then
147
148
    pair = i - 1
148
 
  end   
 
149
  end
149
150
  block_info[i] = {"wall", dir, category, pair = pair}
150
151
  end
151
 
 
 
152
 
152
153
  MapEditorSetBlocks(self.ui.app.map.blocks, block_info) -- pass data to UI
153
154
  self.block_info = block_info
154
155
end
158
158
  local ui = self.ui
159
159
  local x, y = ui:WorldToScreen(self.mouse_cell_x, self.mouse_cell_y)
160
160
  self.cell_outline:draw(canvas, 2, x - 32, y)
161
 
  
 
161
 
162
162
  Window.draw(self, canvas, ...)
163
163
end
164
164
 
165
165
 
166
166
function UIMapEditor:onMouseMove(x, y, dx, dy)
167
167
  local repaint = Window.onMouseMove(self, x, y, dx, dy)
168
 
  
 
168
 
169
169
  local ui = self.ui
170
170
  local wxr, wyr = ui:ScreenToWorld(self.x + x, self.y + y)
171
171
  local wx = math_floor(wxr)
172
172
  local wy = math_floor(wyr)
173
173
  local map = self.ui.app.map
174
 
  
 
174
 
175
175
  -- Update the stored state of cursor position, and trigger a repaint as the
176
176
  -- cell outline sprite should track the cursor position.
177
177
  if wx ~= self.mouse_cell_x or wy ~= self.mouse_cell_y then
227
227
      repaint = true
228
228
    end
229
229
  end
230
 
  
 
230
 
231
231
  return repaint
232
232
end
233
233
 
262
262
    self:setPaintRect(0, 0, 0, 0)
263
263
    repaint = true
264
264
  end
265
 
  
 
265
 
266
266
  return Window.onMouseDown(self, button, x, y) or repaint
267
267
end
268
268
 
279
279
    self.command_stack:add(self.current_command)
280
280
    repaint = true
281
281
  end
282
 
  
 
282
 
283
283
  return Window.onMouseUp(self, button, x, y) or repaint
284
284
end
285
285
 
318
318
function UIMapEditor:doLargePaint(x, y)
319
319
  -- Perform a "large" paint. At the moment, this is triggered by a double
320
320
  -- left click, and results in a floor tile "flood fill" operation.
321
 
  
 
321
 
322
322
  -- Get the Lua tile coordinate of the tile to start filling from.
323
323
  x, y = self.ui:ScreenToWorld(x, y)
324
324
  x = math_floor(x)
326
326
  if x <= 0 or y <= 0 then
327
327
    return
328
328
  end
329
 
  
 
329
 
330
330
  -- The click which preceeded the double click should have set "old_floors"
331
331
  -- with the contents of the tile prior to the single click.
332
332
  if not self.old_floors then
349
349
  if match_f == brush_f then
350
350
    return
351
351
  end
352
 
  
 
352
 
353
353
  -- Reset the starting tile as to simplify the upcoming loop.
354
354
  self.current_command_cell:addTile(x, y, 1, match_f)
355
355
  map:setCell(x, y, 1, match_f)
356
 
  
357
 
  
 
356
 
 
357
 
358
358
  local to_visit = {[key] = {x, y}}
359
359
  local visited = {[key] = true}
360
360
  -- Mark the tiles beyond the edge of the map as visited, as to prevent the
412
412
  local step_base_y = math.floor(self.paint_start_wy)
413
413
  local xstep = self.paint_step_x
414
414
  local ystep = self.paint_step_y
415
 
  
 
415
 
416
416
  -- Determine what kind of thing is being painted.
417
417
  local is_wall = self.block_info[self.block_brush_preview]
418
418
  is_wall = is_wall and is_wall[1] == "wall" and is_wall[2]
419
419
  local is_simple_floor = self.block_info[self.block_brush_preview]
420
420
  is_simple_floor = is_simple_floor and is_simple_floor[1] == "floor" and is_simple_floor[2] == "simple"
421
 
  
 
421
 
422
422
  -- To allow the double click handler to know what was present before the
423
423
  -- single click which preceeds it, the prior contents of the floor layer is
424
424
  -- saved.
500
500
  local step_base_y = math.floor(self.paint_start_wy)
501
501
  xstep = xstep or old_xstep
502
502
  ystep = ystep or old_ystep
503
 
  
 
503
 
504
504
  -- Create a rectangle which contains both the old and new rectangles, as
505
505
  -- this contains all tiles which may need to change.
506
506
  local left, right, top, bottom = x, x + w - 1, y, y + h - 1
518
518
      bottom = rect.y + rect.h - 1
519
519
    end
520
520
  end
521
 
  
 
521
 
522
522
  -- Determine what kind of thing is being painted
523
523
  local is_wall = self.block_info[self.block_brush_preview]
524
524
  local block_brush_preview_pair = is_wall and is_wall.pair
610
610
      end
611
611
    end
612
612
  end
613
 
  
 
613
 
614
614
  if wx == self.recent_sample_x and wy == self.recent_sample_y then
615
615
    self.sample_i = self.sample_i + 1
616
616
  else