Class: SpriteKit::Canvas
- Inherits:
-
Object
- Object
- SpriteKit::Canvas
- Defined in:
- mygame/lib/sprite_kit/canvas.rb
Instance Attribute Summary collapse
-
#hover_rect ⇒ Object
Returns the value of attribute hover_rect.
-
#max_width ⇒ Object
Returns the value of attribute max_width.
-
#rect_size ⇒ Object
Returns the value of attribute rect_size.
-
#spritesheets ⇒ Object
Returns the value of attribute spritesheets.
-
#state ⇒ Object
Returns the value of attribute state.
-
#viewport_boundary ⇒ Object
Returns the value of attribute viewport_boundary.
Instance Method Summary collapse
- #calc(args) ⇒ Object
- #calc_camera(_args) ⇒ Object
- #camera_render_target(args) ⇒ Object
- #camera_speed ⇒ Object
-
#initialize(state:, sprite_directory: "sprites") ⇒ Canvas
constructor
A new instance of Canvas.
- #input(args) ⇒ Object
- #move_camera(args) ⇒ Object
- #render(args) ⇒ Object
- #render_camera(args) ⇒ Object
- #render_current_sprite(args) ⇒ Object
- #render_sprite_canvas(args) ⇒ Object
-
#sprite_out_of_bounds?(sprite, rect) ⇒ Boolean
@state.draw_buffer.sprites.concat(@grid_boxes.map do |rect| @state.camera.to_screen_space(rect) end) end.
- #tick(args) ⇒ Object
Constructor Details
#initialize(state:, sprite_directory: "sprites") ⇒ Canvas
Returns a new instance of Canvas.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 12 def initialize(state:, sprite_directory: "sprites") @spritesheet_loader = SpriteKit::SpritesheetLoader.new @spritesheets = @spritesheet_loader.load_directory(sprite_directory) # max_width of elements to load. Wraps downward if greater than 2000px @max_width = 2000 @hover_rect = nil @hover_rect_screen = nil # @show_grid = false # used to calculate where clicks are registered. @viewport_boundary = { x: 0, y: 0, h: Grid.h, w: Grid.w } @state = state end |
Instance Attribute Details
#hover_rect ⇒ Object
Returns the value of attribute hover_rect.
10 11 12 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 10 def hover_rect @hover_rect end |
#max_width ⇒ Object
Returns the value of attribute max_width.
10 11 12 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 10 def max_width @max_width end |
#rect_size ⇒ Object
Returns the value of attribute rect_size.
10 11 12 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 10 def rect_size @rect_size end |
#spritesheets ⇒ Object
Returns the value of attribute spritesheets.
10 11 12 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 10 def spritesheets @spritesheets end |
#state ⇒ Object
Returns the value of attribute state.
10 11 12 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 10 def state @state end |
#viewport_boundary ⇒ Object
Returns the value of attribute viewport_boundary.
10 11 12 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 10 def @viewport_boundary end |
Instance Method Details
#calc(args) ⇒ Object
64 65 66 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 64 def calc(args) calc_camera(args) end |
#calc_camera(_args) ⇒ Object
108 109 110 111 112 113 114 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 108 def calc_camera(_args) ease = 0.1 @state.camera.scale += (@state.camera.target_scale - @state.camera.scale) * ease @state.camera.x += (@state.camera.target_x - @state.camera.x) * ease @state.camera.y += (@state.camera.target_y - @state.camera.y) * ease end |
#camera_render_target(args) ⇒ Object
34 35 36 37 38 39 40 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 34 def camera_render_target(args) rt = args.outputs[@state.camera_path] = @state.camera. args.outputs[@state.camera_path].w = .w args.outputs[@state.camera_path].h = .h rt end |
#camera_speed ⇒ Object
42 43 44 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 42 def camera_speed 3 + (12 / @state.camera.scale) end |
#input(args) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 52 def input(args) move_camera(args) if args.inputs.keyboard.key_down.escape @state.current_sprite = nil end # if args.inputs.keyboard.key_down.g # @show_grid = !@show_grid # end end |
#move_camera(args) ⇒ Object
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 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 79 def move_camera(args) inputs = args.inputs speed = camera_speed # Movement if inputs.keyboard.left_arrow @state.camera.target_x -= speed elsif inputs.keyboard.right_arrow @state.camera.target_x += speed end if inputs.keyboard.down_arrow @state.camera.target_y -= speed elsif inputs.keyboard.up_arrow @state.camera.target_y += speed end # Zoom if args.inputs.keyboard.key_down.equal_sign || args.inputs.keyboard.key_down.plus @state.camera.target_scale += 0.25 elsif args.inputs.keyboard.key_down.minus @state.camera.target_scale -= 0.25 @state.camera.target_scale = 0.25 if @state.camera.target_scale < 0.25 elsif args.inputs.keyboard.zero @state.camera.target_scale = 1 end end |
#render(args) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 68 def render(args) render_camera(args) render_sprite_canvas(args) # render_grid_lines(args) render_current_sprite(args) if @hover_rect_screen @state.draw_buffer[@state.camera_path] << @hover_rect_screen end end |
#render_camera(args) ⇒ Object
116 117 118 119 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 116 def render_camera(args) camera_render_target(args) @state.draw_buffer.primitives << { **@state.camera., path: @state.camera_path } end |
#render_current_sprite(args) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 363 def render_current_sprite(args) current_sprite = @state.current_sprite if current_sprite current_sprite.w = current_sprite.source_w current_sprite.h = current_sprite.source_h current_sprite.x = @state.world_mouse.x - (@state.current_sprite.w / 2) current_sprite.y = @state.world_mouse.y - (@state.current_sprite.h / 2) # args.outputs.debug << { x: @state.camera.x, y: @state.camera.y }.to_s # prefab stuff. if @state.current_sprite.prefab && @state.current_sprite.prefab.length > 0 prefab = SpriteMethods.render_prefab(@state.current_sprite).map do |sprite| @state.camera.to_screen_space(sprite) end @state.draw_buffer[@state.camera_path].sprites.concat(prefab) else @state.draw_buffer[@state.camera_path].sprites << @state.camera.to_screen_space(@state.current_sprite) end # @state.draw_buffer[@state.camera_path].sprites << @state.camera.to_screen_space(@state.current_sprite) end end |
#render_sprite_canvas(args) ⇒ Object
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 121 def render_sprite_canvas(args) x = 0 y = 0 gap = 80 current_width = 0 current_row = [] @hover_rect = nil @hover_rect_screen = nil @spritesheets.each_with_index do |spritesheet, index| current_width += spritesheet.file_width if index > 0 prev_spritesheet = @spritesheets[index - 1] if current_width + gap + spritesheet.file_width > @max_width # move down a row current_width = spritesheet.file_width y -= current_row.max_by(&:h).h + gap x = 0 current_row = [] else x += prev_spritesheet.file_width + gap end end spritesheet.merge!({ x: x, y: y - spritesheet.file_height, w: spritesheet.file_width, h: spritesheet.file_height, path: spritesheet.path }) current_row << spritesheet end visible_spritesheets = @state.camera.(@spritesheets) visible_spritesheets.each do |spritesheet| spritesheet.spritesheet_screen = @state.camera.to_screen_space(spritesheet) @state.draw_buffer[@state.camera_path] << spritesheet.spritesheet_screen end if !args.inputs.mouse.intersect_rect?(@viewport_boundary) return end Geometry.find_all_intersect_rect(@state.world_mouse, visible_spritesheets).each do |spritesheet| spritesheet_screen = spritesheet.spritesheet_screen rect_size = @state.tile_selection relative_x = (@state.world_mouse.x - spritesheet.x).clamp(@state.tile_selection.offset_x, spritesheet.w) relative_y = (@state.world_mouse.y - spritesheet.y).clamp(@state.tile_selection.offset_y, spritesheet.h) column_gap = @state.tile_selection.column_gap row_gap = @state.tile_selection.row_gap rect_x = (relative_x).ifloor(rect_size.w + column_gap) rect_y = (relative_y).ifloor(rect_size.h + row_gap) hover_rect_x = rect_x + spritesheet.x + @state.tile_selection.offset_x hover_rect_y = rect_y + spritesheet.y + @state.tile_selection.offset_y @hover_rect = rect_size.merge({ x: hover_rect_x, y: hover_rect_y, path: :pixel, primitive_marker: :sprite, r: 255, g: 0, b: 0, a: 128 }) unclamped_source_x = (@hover_rect.x - spritesheet.x)# .clamp(0, spritesheet.w - rect_size.w) unclamped_source_y = (@hover_rect.y - spritesheet.y)# .clamp(0, spritesheet.h - rect_size.h) source_x = unclamped_source_x.clamp(@state.tile_selection.offset_x, spritesheet.w - rect_size.w) source_y = unclamped_source_y.clamp(@state.tile_selection.offset_y, spritesheet.h - rect_size.h) # source_w and source_h need to be "clamped" because otherwise you get weird scaling. source_w = (rect_size.w).clamp(@state.tile_selection.offset_x, spritesheet.w - source_x) if unclamped_source_x < 0 source_w += unclamped_source_x @hover_rect.w += unclamped_source_x @hover_rect.x -= unclamped_source_x elsif unclamped_source_x > spritesheet.w - rect_size.w source_x_diff = unclamped_source_x - source_x source_w -= source_x_diff source_x += source_x_diff @hover_rect.w -= source_x_diff # @hover_rect.x -= source_x_diff end # w = 16, source_x = 72 = 88px, but file max is 80. need to chop 8px. # w = 16, source_x = 0 = 16px, file max is 80. use 16px. source_h = (rect_size.h).clamp(@state.tile_selection.offset_y, spritesheet.h - source_y) if unclamped_source_y < 0 source_h += unclamped_source_y @hover_rect.h += unclamped_source_y @hover_rect.y -= unclamped_source_y elsif unclamped_source_y > spritesheet.h - rect_size.h source_y_diff = unclamped_source_y - source_y source_h -= source_y_diff source_y += source_y_diff @hover_rect.h -= source_y_diff # @hover_rect.x -= source_x_diff end # h = 16, source_y = 72 = 88px, but file max is 80px. need to chop 8px. # h = 16, source_x = 0 = 16px, file max is 80. use 16px. new_sprite = { spritesheet: spritesheet, w: @hover_rect.w, h: @hover_rect.h, source_x: source_x, source_y: source_y, source_w: source_w, source_h: source_h, path: spritesheet.path } if @state.current_sprite && @state.current_sprite.path == new_sprite.path && args.inputs.keyboard.key_down_or_held?(:shift) current_sprite = @state.current_sprite if current_sprite.source_x == new_sprite.source_x # no-op elsif current_sprite.source_x > new_sprite.source_x source_x = [current_sprite.source_x, new_sprite.source_x].min source_w = [current_sprite.source_x - new_sprite.source_x + @hover_rect.w, current_sprite.source_w].max else source_x = [current_sprite.source_x, new_sprite.source_x].min source_w = [new_sprite.source_x - current_sprite.source_x + @hover_rect.w, spritesheet.w - source_x].min end if current_sprite.source_y == new_sprite.source_y # no-op elsif current_sprite.source_y > new_sprite.source_y source_y = new_sprite.source_y source_h = current_sprite.source_y - new_sprite.source_y + [new_sprite.source_h, current_sprite.source_h].min else source_y = current_sprite.source_y source_h = new_sprite.source_y - current_sprite.source_y + [new_sprite.source_h, current_sprite.source_h].min end # rect_x = (relative_x).ifloor(rect_size.w + column_gap) # rect_y = (relative_y).ifloor(rect_size.h + row_gap) # puts "Rows: #{rows}, Columns: #{columns}" new_sprite = new_sprite.merge({ source_x: source_x, source_y: source_y, source_w: source_w, source_h: source_h, }) if args.inputs.mouse.click @virtual_sprite_selection = nil else @virtual_sprite_selection = { x: spritesheet.x + new_sprite.source_x, y: spritesheet.y + new_sprite.source_y, w: new_sprite.source_w, h: new_sprite.source_h, path: :pixel, r: 255, g: 255, b: 255, a: 64, # blendmode_enum: 0, } end else @virtual_sprite_selection = nil end if args.inputs.mouse.click @state.current_sprite = new_sprite @state.current_sprite.spritesheet = spritesheet end @hover_rect_screen = @state.camera.to_screen_space(@hover_rect) label_size = 20 label = { x: spritesheet_screen.x + (spritesheet_screen.w / 2), y: spritesheet_screen.y + spritesheet_screen.h + label_size, text: "#{spritesheet.path}", primitive_marker: :label, size_px: label_size, r: 255, b: 255, g: 255, a: 255, anchor_x: 0.5, anchor_y: 0.5, scale_quality_enum: 2 } label_w, label_h = GTK.calcstringbox(label.text, size_px: label_size) label_background = label.merge({ w: label_w + 16, h: label_h + 8, anchor_x: 0.5, anchor_y: 0.5, primitive_marker: :solid, r: 0, b: 0, g: 0, a: 255, }) @state.draw_buffer[@state.camera_path].concat([ label_background, label, ]) end if @virtual_sprite_selection @state.draw_buffer[@state.camera_path] << @state.camera.to_screen_space(@virtual_sprite_selection) end if @state.current_sprite @current_sprite_selection = { x: @state.current_sprite.spritesheet.x + @state.current_sprite.source_x, y: @state.current_sprite.spritesheet.y + @state.current_sprite.source_y, w: @state.current_sprite.source_w, h: @state.current_sprite.source_h, path: :pixel, r: 50, g: 50, b: 50, a: 128, } @state.draw_buffer[@state.camera_path] << @state.camera.to_screen_space(@current_sprite_selection) end end |
#sprite_out_of_bounds?(sprite, rect) ⇒ Boolean
@state.draw_buffer.sprites.concat(@grid_boxes.map do |rect|
@state.camera.to_screen_space(rect)
end)
end
448 449 450 451 452 453 454 455 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 448 def sprite_out_of_bounds?(sprite, rect) return true if sprite.source_x < 0 return true if sprite.source_y < 0 return true if sprite.source_x + sprite.source_w > rect.w return true if sprite.source_y + sprite.source_h > rect.h false end |
#tick(args) ⇒ Object
46 47 48 49 50 |
# File 'mygame/lib/sprite_kit/canvas.rb', line 46 def tick(args) input(args) calc(args) render(args) end |