Class: SpriteKit::ToolDrawer
- Inherits:
-
Object
- Object
- SpriteKit::ToolDrawer
- Defined in:
- mygame/lib/sprite_kit/tool_drawer.rb
Instance Attribute Summary collapse
-
#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
- #calc(args) ⇒ Object
- #handle_keyboard(obj, key, keyboard) ⇒ Object
- #handle_text(obj, key, text) ⇒ Object
-
#initialize(state:) ⇒ ToolDrawer
constructor
A new instance of ToolDrawer.
- #input(args) ⇒ Object
- #render(args) ⇒ Object
- #serialize ⇒ Object
- #tick(args) ⇒ Object
Constructor Details
#initialize(state:) ⇒ ToolDrawer
Returns a new instance of ToolDrawer.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 8 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: { id: :view_file_tree, label: proc { { id: :view_file_tree, text: "View File Tree" } }, handle_click: proc { @state.next_view = :file_tree } }, gap_button: { id: :gap_button, label: proc { { id: :gap_button, text: (@state.current_sprite.prefab ? "Add gap" : "Remove Gap") } }, handle_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 } }, copy_to_clipboard: { id: :copy_to_clipboard, label: proc { { id: :copy_to_clipboard, text: "Copy to clipboard" } }, handle_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) } }, # save_format: { # label: proc { { id: :save_format, text: "Copy to clipboard" } }, # } } @counters = { tile_selection_w: { label: proc { { id: :tile_selection_w, text: " w: #{@state.tile_selection.w}" } }, increment: proc { @state.tile_selection.w += 1 }, decrement: proc { @state.tile_selection.w -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :w, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :w, keyboard) } }, tile_selection_h: { label: proc { { id: :tile_selection_h, text: " h: #{@state.tile_selection.h}" } }, increment: proc { @state.tile_selection.h += 1 }, decrement: proc { @state.tile_selection.h -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :h, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :h, keyboard) } }, row_gap: { label: proc { { id: :row_gap, text: "row_gap: #{@state.tile_selection.row_gap}" } }, increment: proc { @state.tile_selection.row_gap += 1 }, decrement: proc { @state.tile_selection.row_gap -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :row_gap, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :row_gap, keyboard) } }, column_gap: { label: proc { { id: :column_gap, text: "column_gap: #{@state.tile_selection.column_gap}" } }, increment: proc { @state.tile_selection.column_gap += 1 }, decrement: proc { @state.tile_selection.column_gap -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :column_gap, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :column_gap, keyboard) } }, offset_x: { label: proc { { id: :offset_x, text: "offset_x: #{@state.tile_selection.offset_x}" } }, increment: proc { @state.tile_selection.offset_x += 1 }, decrement: proc { @state.tile_selection.offset_x -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :offset_x, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :offset_x, keyboard) } }, offset_y: { label: proc { { id: :offset_y, text: "offset_y: #{@state.tile_selection.offset_y}" } }, increment: proc { @state.tile_selection.offset_y += 1 }, decrement: proc { @state.tile_selection.offset_y -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :offset_y, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :offset_y, keyboard) } }, source_x: { label: proc { { id: :source_x, text: "source_x: #{@state.current_sprite.source_x}" } }, increment: proc { @state.current_sprite.source_x += 1 }, decrement: proc { @state.current_sprite.source_x -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :source_x, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :source_x, keyboard) } }, source_y: { label: proc { { id: :source_y, text: "source_y: #{@state.current_sprite.source_y}" } }, increment: proc { @state.current_sprite.source_y += 1 }, decrement: proc { @state.current_sprite.source_y -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :source_y, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :source_y, keyboard) } }, source_w: { label: proc { { id: :source_w, text: "source_w: #{@state.current_sprite.source_w}" } }, increment: proc { @state.current_sprite.source_w += 1 }, decrement: proc { @state.current_sprite.source_w -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :source_w, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :source_w, keyboard) } }, source_h: { label: proc { { id: :source_h, text: "source_h: #{@state.current_sprite.source_h}" } }, increment: proc { @state.current_sprite.source_h += 1 }, decrement: proc { @state.current_sprite.source_h -= 1 }, handle_text: proc { |text| handle_text(@state.tile_selection, :source_h, text) }, handle_keyboard: proc { |keyboard| handle_keyboard(@state.tile_selection, :source_h, keyboard) } }, } end |
Instance Attribute Details
#h ⇒ Object
Returns the value of attribute h.
6 7 8 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 6 def h @h end |
#render_path ⇒ Object
Returns the value of attribute render_path.
6 7 8 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 6 def render_path @render_path end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 6 def state @state end |
#w ⇒ Object
Returns the value of attribute w.
6 7 8 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 6 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
6 7 8 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 6 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
6 7 8 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 6 def y @y end |
Instance Method Details
#calc(args) ⇒ Object
212 213 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 212 def calc(args) end |
#handle_keyboard(obj, key, keyboard) ⇒ Object
413 414 415 416 417 418 419 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 413 def handle_keyboard(obj, key, keyboard) if keyboard.key_down.backspace || keyboard.key_repeat.backspace val = obj.send(key.to_sym).to_s val = val.slice(0, val.length - 1).to_i obj.send("#{key}=".to_sym, val) end end |
#handle_text(obj, key, text) ⇒ Object
407 408 409 410 411 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 407 def handle_text(obj, key, text) val = obj.send(key.to_sym) val = "#{val}#{text.join}".to_i obj.send("#{key}=".to_sym, val) end |
#input(args) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 189 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 if @focus counter = @counters[@focus.id] if counter text = args.inputs.text if text.length > 0 counter.handle_text&.call(text) end counter.handle_keyboard.call(args.inputs.keyboard) end else # DR.stop_text_input end end |
#render(args) ⇒ Object
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 215 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 label_offset_x = 20 label_offset_y = 40 text = [ @buttons.view_file_tree.label.call, { text: "brush: { " }, @counters.tile_selection_w.label.call, @counters.tile_selection_h.label.call, { text: "}" } , @counters.offset_x.label.call, @counters.offset_y.label.call, @counters.column_gap.label.call, @counters.row_gap.label.call, ] if @state.current_sprite text.concat([ @buttons..label.call, @counters.source_x.label.call, @counters.source_y.label.call, @counters.source_w.label.call, @counters.source_h.label.call, { text: StringHelper.truncate("path: #{@state.current_sprite.path}", max_width: @w - label_offset_x - 5) }, @buttons.copy_to_clipboard.label.call, { text: "Spritesheet Properties:" }, { text: "w: #{@state.current_sprite.spritesheet.w}" }, { text: "h: #{@state.current_sprite.spritesheet.h}" }, ]) end prev_y = 0 label_gap = 14 text.each_with_index do |label, index| label_w, label_h = GTK.calcstringbox(label.text) label.w = label_w label.h = label_h label.x = label_offset_x label.y = @h - label_offset_y - prev_y - label_h - (label_gap * index) label.primitive_marker = :label label.anchor_y = 0 prev_y += label_h end if args.inputs.mouse.click focus = Geometry.find_intersect_rect(args.inputs.mouse, text) if focus @focus = focus DR.start_text_input else @focus = nil DR.stop_text_input end end label_offset_x = 20 = [] = [] @buttons.each do |key, | label = text.find { |hash| hash[:id] == key } if label .concat(Primitives.borders(label, padding: 8).values) if args.inputs.click && args.inputs.mouse.intersect_rect?(label) .handle_click.call end end end @counters.each do |key, counter| hash = text.find { |hash| hash[:id] == key } next if !hash row_gap = 8 btn_x = hash.x + row_gap + hash.w btn_y = hash.y gap = 20 = { x: btn_x + gap, y: btn_y + 5, w: 16, h: 16, path: SpriteKit.to_load_path("sprites/minus-sprite.png") } gap = 20 = .merge({ x: .x + .w + gap, y: .y, path: SpriteKit.to_load_path("sprites/plus-sprite.png") }) << << if args.inputs.mouse.click || args.inputs.mouse..left.held if args.inputs.mouse.intersect_rect?() if args.inputs.mouse.click counter.increment.call elsif args.inputs.mouse..left.held start_tick = args.inputs.mouse..left.click_at current_tick = Kernel.tick_count diff = (current_tick - (start_tick + 30)) if diff > 0 counter.increment.call end end elsif args.inputs.mouse.intersect_rect?() if args.inputs.mouse.click counter.decrement.call elsif args.inputs.mouse..left.held start_tick = args.inputs.mouse..left.click_at current_tick = Kernel.tick_count diff = (current_tick - (start_tick + 30)) if diff > 0 counter.decrement.call end end end end end @state.draw_buffer[@render_path] .concat() .concat(text) .concat() if @focus @focus = text.find { |obj| obj.id == @focus.id } if @focus @state.draw_buffer[@render_path].concat( Primitives.borders(@focus, padding: 8).values ) end end # need this top-layer # path = labels[-5] # if path && current_sprite # text_width, text_height = GTK.calcstringbox(path.text) # path_rect = path.merge({ # w: text_width, # h: text_height, # }) # if args.inputs.mouse.intersect_rect?(serialize) && args.inputs.mouse.intersect_rect?(path_rect) # solid = { # x: path_rect.x, # y: path_rect.y, # w: path_rect.w, # h: path_rect.h, # r: 0, # b: 0, # g: 0, # a: 255, # primitive_marker: :solid # }.anchor_rect(path_rect.anchor_x || 0, path_rect.anchor_y || 0) # solid.x -= 4 # solid.y -= 4 # solid.w += 8 # solid.h += 8 # @state.draw_buffer[:top_layer].concat([ # solid, # { # x: path_rect.x, # y: path_rect.y, # text: path_rect.text, # primitive_marker: :label, # anchor_y: path_rect.anchor_y, # r: 255, # g: 255, # b: 255, # a: 255, # }.label!, # ]) # end # end end |
#serialize ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 171 def serialize { x: @x, y: @y, w: @w, h: @h, primitive_marker: :sprite, scale_quality_enum: 2, path: @render_path } end |
#tick(args) ⇒ Object
183 184 185 186 187 |
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 183 def tick(args) input(args) calc(args) render(args) end |