Class: SpriteKit::MapEditor

Inherits:
Object
  • Object
show all
Defined in:
mygame/lib/sprite_kit/map_editor.rb

Overview

A tile map editor. Paints @state.current_sprite (picked from SpriteKit::Canvas) onto layers of a SpriteKit::Map. The grid size is the same as the brush size (@state.tile_selection.w / @state.tile_selection.h).

See SpriteKit::Scenes::MapEditorScene for wiring it up with a canvas + tool drawer.

Constant Summary collapse

IGNORED_KEYS =

These keys dont really map to anything.

[:char, :raw_key]
MODIFIER_KEYS =

We dont really expect modifier keys to be used as actual keys.

[
  :control,
  :control_left,
  :control_right,

  :shift,
  :shift_right,
  :shift_left,

  :meta,
  :meta_left,
  :meta_right,

  :alt,
  :alt_left,
  :alt_right,
]
HELP =
[
  "LMB: use tool   RMB: erase",
  "MMB / Space+drag: pan",
  "Wheel / +/-/0: zoom",
  "Ctrl+Z: undo  Ctrl+Shift+Z: redo",
  "Ctrl+S: save   G: grid",
  "N: new layer   V: hide layer",
  "[ / ]: change layer",
  "Tab: pick tiles / switch view",
]
PANEL_W =
260
STATUS_H =
28
ROW_H =
26
PADDING =
12
MIN_SCALE =
0.25
MAX_SCALE =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state:, map_path: "data/map.json", camera: ::SpriteKit::Camera.new(path: :map_camera)) ⇒ MapEditor

Returns a new instance of MapEditor.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'mygame/lib/sprite_kit/map_editor.rb', line 59

def initialize(state:, map_path: "data/map.json", camera: ::SpriteKit::Camera.new(path: :map_camera))
  @state = state
  @camera = camera
  @camera_path = camera.path || :map_camera
  @palette = UI::SemanticPalette.new

  @map = ::SpriteKit::Map.new(path: map_path, tile_w: @state.grid_size.w, tile_h: @state.grid_size.h)

  # Keep the brush in sync with the grid the map was made with.
  if @map.load
    @state.grid_size.w = @map.tile_w
    @state.grid_size.h = @map.tile_h
  end

  @state.grid_size.w = 48
  @state.grid_size.h = 48

  # @type [:paint, :erase, :rect, :pick]
  @tool = :paint
  @show_grid = true

  # The in-progress brush stroke / rectangle.
  @stroke = nil
  @panning = false
  @saved_at = nil

  # used to calculate where clicks are registered.
  @viewport_boundary = {
    x: 0,
    y: 0,
    h: Grid.h,
    w: Grid.w
  }

  @panel_buttons = []

  @tools = {
    keyboard: {
      paint: {
        keybindings: [
          [:b]
        ],
        label: "Paint (B)",
        action: -> { select_tool(:paint) }
      },
      erase: {
        keybindings: [
          [:e]
        ],
        label: "Erase (E)",
        action: -> { select_tool(:erase) }
      },
      rect: {
        keybindings: [
          [:r]
        ],
        label: "Rect fill (R)",
        action: -> { select_tool(:rect) }
      },
      pick: {
        keybindings: [
          [:p]
        ],
        label: "Pick tile (P)",
        action: -> { select_tool(:pick) }
      },
    }
  }

  @hotkeys = {
    keyboard: {
      undo_change: {
        keybindings: [
          [:ctrl, :z],
          [:meta, :z]
        ],
        action: -> { undo_change }
      },
      redo_change: {
        keybindings: [
          [:ctrl, :shift, :z],
          [:meta, :shift, :z]
        ],
        action: -> { redo_change }
      },
      save: {
        keybindings: [
          [:ctrl, :s],
          [:meta, :s]
        ],
        action: -> { save }
      },
      show_grid: {
        keybindings: [
          [:g]
        ],
        action: -> { @show_grid = !@show_grid }
      },
      add_layer: {
        keybindings: [
          [:n]
        ],
        action: -> { add_layer }
      },
      toggle_layer_visibility: {
        keybindings: [
          [:v]
        ],
        action: -> { toggle_layer_visibility(@map.current_layer_index) }
      },
      select_previous_layer: {
        keybindings: [
          [:open_square_brace]
        ],
        action: -> { select_layer(@map.current_layer_index - 1) }
      },
      select_next_layer: {
        keybindings: [
          [:close_square_brace]
        ],
        action: -> { select_layer(@map.current_layer_index + 1) }
      },
      clear: {
        keybindings: [
          [:escape]
        ],
        action: -> {
          if @stroke
            @stroke = nil
          else
            @state.current_sprite = nil
          end
        }
      }
    }
  }.merge(@tools)

  @background_color = nil
  @background_color = { r: 40, g: 42, b: 54, a: 255 }
  @hotkey_handler = HotkeyHandler.new(@hotkeys)
end

Instance Attribute Details

#camera ⇒ Object

Returns the value of attribute camera.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def camera
  @camera
end

#camera_path ⇒ Object

Returns the value of attribute camera_path.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def camera_path
  @camera_path
end

#map ⇒ Object

Returns the value of attribute map.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def map
  @map
end

#show_grid ⇒ Object

Returns the value of attribute show_grid.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def show_grid
  @show_grid
end

#state ⇒ Object

Returns the value of attribute state.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def state
  @state
end

#tool ⇒ Object

Returns the value of attribute tool.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def tool
  @tool
end

#viewport_boundary ⇒ Object

Returns the value of attribute viewport_boundary.



57
58
59
# File 'mygame/lib/sprite_kit/map_editor.rb', line 57

def viewport_boundary
  @viewport_boundary
end

Instance Method Details

#brush_tiles(x, y) ⇒ Object

Tiles that the current sprite (or prefab) would place with its bottom-left at x, y.



266
267
268
269
270
271
272
273
274
275
276
277
# File 'mygame/lib/sprite_kit/map_editor.rb', line 266

def brush_tiles(x, y)
  sprite = @state.current_sprite
  return [] if !sprite

  if sprite.prefab && sprite.prefab.length > 0
    SpriteMethods.render_prefab(sprite.merge({ x: x, y: y })).map do |prefab_sprite|
      tile_from_sprite(prefab_sprite, prefab_sprite.x, prefab_sprite.y)
    end
  else
    [tile_from_sprite(sprite, x, y)]
  end
end

#calc(args) ⇒ Object



235
236
237
238
239
240
# File 'mygame/lib/sprite_kit/map_editor.rb', line 235

def calc(args)
  calc_camera(args)

  @map.tile_w = @state.grid_size.w
  @map.tile_h = @state.grid_size.h
end

#grid_h ⇒ Object



256
257
258
# File 'mygame/lib/sprite_kit/map_editor.rb', line 256

def grid_h
  [@state.grid_size.h.to_i, 1].max
end

#grid_w ⇒ Object



252
253
254
# File 'mygame/lib/sprite_kit/map_editor.rb', line 252

def grid_w
  [@state.grid_size.w.to_i, 1].max
end

#input(args) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'mygame/lib/sprite_kit/map_editor.rb', line 215

def input(args)
  calc_layout(args)

  mouse = args.inputs.mouse
  @world_mouse = @camera.to_world_space({ x: mouse.x, y: mouse.y })
  @hovered_cell = cell_at(@world_mouse)
  @mouse_over_map = mouse.intersect_rect?(@map_area)

  move_camera(args)
  handle_pan(args)

  if !@state.text_input_focused
    @hotkey_handler.tick(args)
  end

  build_panel(args)
  handle_panel_click(args)
  handle_mouse(args) if !@panning
end

#render(args) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'mygame/lib/sprite_kit/map_editor.rb', line 242

def render(args)
  render_camera(args)
  render_tiles(args)
  render_grid(args) if @show_grid
  render_cursor(args)
  render_panel(args)
  render_status_bar(args)
  render_hint(args)
end

#save ⇒ Object



260
261
262
263
# File 'mygame/lib/sprite_kit/map_editor.rb', line 260

def save
  @map.save
  @saved_at = Kernel.tick_count
end

#tick(args) ⇒ Object



201
202
203
204
205
# File 'mygame/lib/sprite_kit/map_editor.rb', line 201

def tick(args)
  input(args)
  calc(args)
  render(args)
end

#tick_panel(args) ⇒ Object

Only the side panel, for when another view (canvas / file tree) is showing so the brush + layers stay visible.



208
209
210
211
212
213
# File 'mygame/lib/sprite_kit/map_editor.rb', line 208

def tick_panel(args)
  calc_layout(args)
  build_panel(args)
  handle_panel_click(args)
  render_panel(args)
end