Class: SpriteKit::ToolDrawer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state:) ⇒ ToolDrawer

Returns a new instance of ToolDrawer.



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
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 9

def initialize(state:)
  @tools = [
    :sprite,
  ]
  @render_path = :tool_drawer
  @state = state

  @x = 0
  @y = 0
  @h = Grid.h
  @w = (Grid.w / 5).ceil

  @buttons = {
    gap_button: {
      id: :gap,
      label: proc { { id: :gap, 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)
        GTK.exec("echo \"#{str}\" | pbcopy")
      }
    }
    # 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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
    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 }
    },
  }
end

Instance Attribute Details

#hObject

Returns the value of attribute h.



7
8
9
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7

def h
  @h
end

#render_pathObject

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

#stateObject

Returns the value of attribute state.



7
8
9
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7

def state
  @state
end

#wObject

Returns the value of attribute w.



7
8
9
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7

def w
  @w
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 7

def x
  @x
end

#yObject

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

#calc(args) ⇒ Object



126
127
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 126

def calc(args)
end

#input(args) ⇒ Object



120
121
122
123
124
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 120

def input(args)
  # if @render_target && @current_sprite && args.inputs.click && @world_mouse.intersect_rect?(@render_path)
  #   @world_mouse
  # end
end

#render(args) ⇒ Object



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
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 129

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 = [
    { 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.gap_button.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

  label_offset_x = 20
  counter_buttons = []
  button_borders = []

  @buttons.each do |key, button|
    label = text.find { |hash| hash[:id] == button.id }
    if label
      button_borders.concat(Primitives.borders(label, padding: 8).values)
      if args.inputs.click && args.inputs.mouse.intersect_rect?(label)
        button.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
    decrement_button = {
      x: btn_x + gap,
      y: btn_y + 5,
      w: 16,
      h: 16,
      path: SpriteKit.to_load_path("sprites/minus-sprite.png")
    }

    gap = 20
    increment_button = decrement_button.merge({
      x: decrement_button.x + decrement_button.w + gap,
      y: decrement_button.y,
      path: SpriteKit.to_load_path("sprites/plus-sprite.png")
    })

    counter_buttons << increment_button
    counter_buttons << decrement_button

    if args.inputs.mouse.click || args.inputs.mouse.buttons.left.held
      if args.inputs.mouse.intersect_rect?(increment_button)
        if args.inputs.mouse.click
          counter.increment.call
        elsif args.inputs.mouse.buttons.left.held
          start_tick = args.inputs.mouse.buttons.left.click_at
          current_tick = Kernel.tick_count

          diff = (current_tick - (start_tick + 75))
          if diff > 0 && diff % 4 == 0
            counter.increment.call
          end
        end
      elsif args.inputs.mouse.intersect_rect?(decrement_button)
        if args.inputs.mouse.click
          counter.decrement.call
        elsif args.inputs.mouse.buttons.left.held
          start_tick = args.inputs.mouse.buttons.left.click_at
          current_tick = Kernel.tick_count

          diff = (current_tick - (start_tick + 75))
          if diff > 0 && diff % 4 == 0
            counter.decrement.call
          end
        end
      end
    end
  end

  @state.draw_buffer[@render_path]
    .concat(button_borders)
    .concat(text)
    .concat(counter_buttons)


  # 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

#serializeObject



102
103
104
105
106
107
108
109
110
111
112
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 102

def serialize
  {
    x: @x,
    y: @y,
    w: @w,
    h: @h,
    primitive_marker: :sprite,
    scale_quality_enum: 2,
    path: @render_path
  }
end

#tick(args) ⇒ Object



114
115
116
117
118
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 114

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