Class: SpriteKit::ToolDrawer::Counter

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

Constant Summary collapse

INPUT_H =
24
PAD =
{ top: 4, bottom: 0, left: 8, right: 4 }

Instance Method Summary collapse

Methods inherited from Sprite

#[], #[]=, #key?, #method_missing, #respond_to_missing?, #serialize

Constructor Details

#initialize(label:, value: 0, get_value: proc {}, set_value: proc {}, on_increment: proc {}, on_decrement: proc {}, **kwargs) ⇒ Counter

Returns a new instance of Counter.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 14

def initialize(label:, value: 0, get_value: proc {}, set_value: proc {}, on_increment: proc {}, on_decrement: proc {}, **kwargs)
  super(
    on_increment: on_increment,
    on_decrement: on_decrement,
    get_value: get_value,
    set_value: set_value,
    value: 0,
    **kwargs
  )
  @value = value
  @primitives = { label: { text: label, anchor_y: 0, primitive_marker: :label } }
  @h = INPUT_H + PAD[:top] + PAD[:bottom]
  @last_input_value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SpriteKit::Sprite

Instance Method Details

#run_tick(args) ⇒ Object



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

def run_tick(args)
  if !@primitives[:input]
    @primitives[:input] = Vendor::Input::Text.new(args, x: @x, y: @y, w: 40, h: INPUT_H, focussed: false)
    @primitives[:input].value.replace(@value.to_s)
  end

  if !@primitives[:decrement_button]
    @primitives.decrement_button = {
      w: 16,
      h: 16,
      path: SpriteKit.to_load_path("sprites/minus-sprite.png"),
      on_click: @on_decrement
    }
  end

  if !@primitives[:increment_button]
    @primitives.increment_button = {
      w: 16,
      h: 16,
      path: SpriteKit.to_load_path("sprites/plus-sprite.png"),
      on_click: @on_increment
    }
  end

  label = @primitives[:label]
  input = @primitives[:input]

  label.x = @x
  label.y = @y + PAD[:bottom]
  label_w, _ = GTK.calcstringbox(label.text)

  @w = @primitives.input.w + label_w + PAD.right + PAD.left

  input.x = @x + label_w + 20
  input.y = @y + PAD[:bottom]             # border extends PAD below input, so lift it
  input.tick
  sync(input)

  @primitives[:decrement_button].tap do |button|
    button.x = input.x + input.w + 20
    button.y = input.y + PAD[:bottom] + PAD[:top] + 1
  end

  @primitives[:increment_button].tap do |button|
    button.x = @primitives[:decrement_button].x + 32
    button.y = @primitives[:decrement_button].y
  end

  @primitives[:borders] = SpriteKit::Primitives.borders(input, padding: PAD).values
end

#sync(input) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'mygame/lib/sprite_kit/tool_drawer.rb', line 80

def sync(input)
  if input.focussed?
    # input -> state, only when the text actually changed
    return if input.value.text == @last_input_value

    @last_input_value = input.value.text.to_s
    @value = input.value.text.to_s
    set_value.call(@value)
  else
    # state -> input
    @value = get_value.call(@value)
    s = @value.to_s

    return if input.value.text.to_s == s

    input.value.replace(s)
    @last_input_value = s.to_s
  end
end