Class: SpriteKit::ToolDrawer
- Inherits:
-
Object
- Object
- SpriteKit::ToolDrawer
- Defined in:
- mygame/lib/sprite_kit/tool_drawer.rb
Defined Under Namespace
Classes: Counter
Instance Attribute Summary collapse
-
#focus ⇒ Object
readonly
Returns the value of attribute focus.
-
#h ⇒ Object
Returns the value of attribute h.
-
#render_path ⇒ Object
Returns the value of attribute render_path.
-
#state ⇒ Object
Returns the value of attribute state.
-
#w ⇒ Object
Returns the value of attribute w.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #attr(obj, key) ⇒ Object
-
#build_clickables(controls) ⇒ Object
Order matters: find_intersect_rect returns the first hit, so the most specific targets go first.
- #calc(args) ⇒ Object
- #calc_size(c) ⇒ Object
- #can_respond?(obj, key) ⇒ Boolean
- #dig_assign(hash, path, value) ⇒ Object
-
#focusable_for(item) ⇒ Object
Only text inputs can take focus; everything else clears it.
- #handle_click(args) ⇒ Object
-
#initialize(state:) ⇒ ToolDrawer
constructor
A new instance of ToolDrawer.
- #input(args) ⇒ Object
- #render(args) ⇒ Object
- #render_primitives(c, ary = []) ⇒ Object
- #serialize ⇒ Object
- #set_focus(target) ⇒ Object
- #tick(args) ⇒ Object
Constructor Details
#initialize(state:) ⇒ ToolDrawer
Returns a new instance of ToolDrawer.
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 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 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 101 def initialize(state:) @tools = [ :sprite, ] @render_path = :tool_drawer @state = state @x = 0 @y = 0 @h = Grid.h @w = (Grid.w / 5).ceil @focus = nil @buttons = { view_file_tree: proc { hash = { id: :view_file_tree, on_click: proc { @state.next_view = :file_tree }, padding: 8, text: "View File Tree", } hash.run_tick = proc { @primitives.concat(SpriteKit::Primitives.borders(hash, padding: hash.padding).values) click_box = hash.merge({ x: hash.x - hash.padding, y: hash.y - hash.padding, w: hash.w + (hash.padding * 2), h: hash.h + (hash.padding * 2), }) # @clickables << click_box hash.y = hash.y + (hash.padding / 2) + 2 hash.x = hash.x + (hash.padding / 2) + 2 } hash }, gap_button: proc { hash = { id: :gap_button, text: (@state.current_sprite.prefab ? "Add gap" : "Remove Gap"), padding: 4, on_click: proc { current_sprite = @state.current_sprite if current_sprite.prefab current_sprite.prefab = nil else current_sprite.prefab = SpriteKit::SpriteMethods.generate_prefab(current_sprite, @state) end } } hash.run_tick = proc { @primitives.concat(SpriteKit::Primitives.borders(hash, padding: hash.padding).values) click_box = hash.merge({ x: hash.x - hash.padding, y: hash.y - hash.padding, w: hash.w + (hash.padding * 2), h: hash.h + (hash.padding * 2), }) # @clickables << click_box hash.y = hash.y + (hash.padding / 2) + 2 hash.x = hash.x + (hash.padding / 2) + 2 } hash }, copy_to_clipboard: proc { hash = { id: :copy_to_clipboard, text: "Copy to clipboard", padding: 4, on_click: proc { str = SpriteMethods.serialize_sprite(@state.current_sprite, :ruby) str = str.slice(2..-4) + "," GTK.exec("echo \"#{str}\" | pbcopy") # DR.misc_ffi.set_clipboard(str) } } hash.run_tick = proc { @primitives.concat(SpriteKit::Primitives.borders(hash, padding: hash.padding).values) click_box = hash.merge({ x: hash.x - hash.padding, y: hash.y - hash.padding, w: hash.w + (hash.padding * 2), h: hash.h + (hash.padding * 2), }) # @clickables << click_box hash.y = hash.y + (hash.padding / 2) + 2 hash.x = hash.x + (hash.padding / 2) + 2 } hash }, # save_format: { # label: proc { { id: :save_format, text: "Copy to clipboard" } }, # } } counters = [ { id: :tile_selection_w, label: "w: ", path: [:tile_selection, :w] }, { id: :tile_selection_h, label: "h: ", path: [:tile_selection, :h] }, { id: :row_gap, label: "row_gap: ", path: [:tile_selection, :row_gap] }, { id: :column_gap, label: "col_gap: ", path: [:tile_selection, :column_gap] }, { id: :offset_x, label: "offset_x: ", path: [:tile_selection, :offset_x] }, { id: :offset_y, label: "offset_y: ", path: [:tile_selection, :offset_y] }, { id: :source_x, label: "source_x: ", path: [:current_sprite, :source_x] }, { id: :source_y, label: "source_y: ", path: [:current_sprite, :source_y] }, { id: :source_w, label: "source_w: ", path: [:current_sprite, :source_w] }, { id: :source_h, label: "source_h: ", path: [:current_sprite, :source_h] }, ] @counters = {} counters.each do |c| @counters[c.id] = Counter.new( label: c.label, value: @state.dig(*c.path), get_value: proc { @state.dig(*c.path).to_i }, set_value: proc { |value| dig_assign(@state, c.path, value.to_i) }, on_increment: proc { val = @state.dig(*c.path) dig_assign(@state, c.path, val + 1) }, on_decrement: proc { val = @state.dig(*c.path) dig_assign(@state, c.path, val - 1) } ) end @tick_count = 0 end |
Instance Attribute Details
#focus ⇒ Object (readonly)
Returns the value of attribute focus.
8 9 10 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 8 def focus @focus end |
#h ⇒ Object
Returns the value of attribute h.
7 8 9 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7 def h @h end |
#render_path ⇒ Object
Returns the value of attribute render_path.
7 8 9 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7 def render_path @render_path end |
#state ⇒ Object
Returns the value of attribute state.
7 8 9 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7 def state @state end |
#w ⇒ Object
Returns the value of attribute w.
7 8 9 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
7 8 9 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
7 8 9 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7 def y @y end |
Instance Method Details
#attr(obj, key) ⇒ Object
455 456 457 458 459 460 461 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 455 def attr(obj, key) if obj.respond_to?(:[]) obj[key] elsif obj.respond_to?(key) obj.send(key) end end |
#build_clickables(controls) ⇒ Object
Order matters: find_intersect_rect returns the first hit, so the most specific targets go first.
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 406 def build_clickables(controls) @clickables = [] @counters.each_value do |c| prims = c.primitives @clickables << prims[:input] @clickables << prims[:increment_button] @clickables << prims[:decrement_button] end # Only real buttons (hashes with on_click), never plain labels or Counter wrappers controls.each do |c| @clickables << c if c.is_a?(Hash) && c[:on_click] end @clickables.compact! end |
#calc(args) ⇒ Object
293 294 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 293 def calc(args) end |
#calc_size(c) ⇒ Object
393 394 395 396 397 398 399 400 401 402 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 393 def calc_size(c) if c.is_a?(Hash) if !c.h || !c.w c_w, c_h = GTK.calcstringbox(c.text) c.w ||= c_w + ((c.padding || 0) * 2) c.h ||= c_h + ((c.padding || 0) * 2) end c[:anchor_y] ||= 0 end end |
#can_respond?(obj, key) ⇒ Boolean
463 464 465 466 467 468 469 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 463 def can_respond?(obj, key) if obj.respond_to?(:key?) true else obj.respond_to?(key) end end |
#dig_assign(hash, path, value) ⇒ Object
243 244 245 246 247 248 249 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 243 def dig_assign(hash, path, value) if path.length <= 0 return end *parents, last = path parents.reduce(hash) { |h, k| h[k] ||= {} }[last] = value end |
#focusable_for(item) ⇒ Object
Only text inputs can take focus; everything else clears it
439 440 441 442 443 444 445 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 439 def focusable_for(item) return nil unless item return item if item.is_a?(Vendor::Input::Text) prims = attr(item, :primitives) prims && prims[:input] end |
#handle_click(args) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 424 def handle_click(args) mouse = args.inputs.mouse return unless mouse.click clicked = Geometry.find_intersect_rect(mouse, @clickables) # Focus first, so an input blurs (and syncs state -> input) # before a +/- button changes the value set_focus(focusable_for(clicked)) on_click = clicked && attr(clicked, :on_click) on_click.call if on_click.is_a?(Proc) end |
#input(args) ⇒ Object
282 283 284 285 286 287 288 289 290 291 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 282 def input(args) if args.inputs.keyboard.key_down.tab idx = @state.views.find_index { |view| @state.view == view } idx += 1 idx = 0 if idx >= @state.views.length @state.next_view = @state.views[idx] end end |
#render(args) ⇒ Object
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 362 363 364 365 366 367 368 369 370 371 372 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 297 def render(args) @state.draw_buffer.primitives << self.serialize render_target = args.outputs[@render_path] render_target.background_color = [255, 255, 255, 255] render_target.w = @w render_target.h = @h # args.outputs.debug << "#{@state.tile_selection}" # args.outputs.debug << "#{args.inputs.mouse}" @primitives = [] cursor_y = 20.from_top cursor_x = 20 gap = 8 controls = [ @buttons.view_file_tree.call, { text: "brush: {" }, @counters.tile_selection_w, @counters.tile_selection_h, { text: "}" } , @counters.offset_x, @counters.offset_y, @counters.column_gap, @counters.row_gap, ] # puts @clickables[-1] if @state.current_sprite controls.concat([ @buttons..call, @counters.source_x, @counters.source_y, @counters.source_w, @counters.source_h, { text: StringHelper.truncate("path: #{@state.current_sprite.path}", max_width: @w - cursor_x - 5) }, @buttons.copy_to_clipboard.call, { text: "Spritesheet Properties:" }, { text: "w: #{@state.current_sprite.spritesheet.w}" }, { text: "h: #{@state.current_sprite.spritesheet.h}" }, ]) end controls.each do |c| calc_size(c) c.x = cursor_x c.y = cursor_y - c.h # y is the bottom edge, so subtract first c.run_tick(args) if c.respond_to?(:run_tick) if c.is_a?(Hash) && c[:run_tick].is_a?(Proc) c.run_tick.call(args) end padding = 0 if can_respond?(c, :padding) padding = (c.padding || 0) * 2 end cursor_y = c.y - gap - padding if (primitives = attr(c, :primitives)) render_primitives(primitives.values, @primitives) primitives.values else render_primitives(c, @primitives) c end end build_clickables(controls) handle_click(args) @state.draw_buffer[@render_path] .concat(@primitives) end |
#render_primitives(c, ary = []) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 374 def render_primitives(c, ary = []) stack = [c] until stack.empty? item = stack.pop next if item.nil? if item.is_a?(Array) stack.concat(item.reverse) # keep original order after pop elsif item.is_a?(Hash) ary << item # a primitive, not a container elsif (prims = attr(item, :primitives)) stack.push(prims) else ary << item # a primitive, not a container end end ary end |
#serialize ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 251 def serialize { x: @x, y: @y, w: @w, h: @h, primitive_marker: :sprite, scale_quality_enum: 2, path: @render_path } end |
#set_focus(target) ⇒ Object
447 448 449 450 451 452 453 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 447 def set_focus(target) return if target.equal?(@focus) @focus&.blur @focus = target @focus&.focus end |
#tick(args) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 263 def tick(args) @clickables = [] if @tick_count > 0 @clickables.concat(@counters.values.map do |c| [ c.primitives[:input], c.primitives[:increment_button], c.primitives[:decrement_button], ] end.flatten) end input(args) calc(args) render(args) @tick_count += 1 end |