Class: Savio::Button

Inherits:
Object
  • Object
show all
Includes:
IORenderable
Defined in:
lib/savio/Button.rb

Constant Summary collapse

@@buttons =
[]

Instance Attribute Summary collapse

Attributes included from IORenderable

#allowDrag, #displayName, #draggingEnabled, #duplicate, #enabled, #shown, #size, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IORenderable

#context, #drag, #dragType=, #endDrag, #kill, #rebuild

Constructor Details

#initialize(args = {}) ⇒ Button

Returns a new instance of Button.



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
# File 'lib/savio/Button.rb', line 13

def initialize(args = {})
  super(args)

  @@buttons.push(self)

  @baseColor = args[:baseColor] || Savio::Colors::White
  @selectedColor = args[:selectedColor] || Savio::Colors::Blue
  @labelActiveColor = args[:labelActiveColor] || Savio::Colors::White
  @labelInactiveColor = args[:labelInactiveColor] || Savio::Colors::White

  @cooldownTime = args[:cooldownTime] || 0.0
  @timeLastClicked = 0.0

  @selected = args[:selected] || false

  @buttonManager = args[:buttonManager] || nil
  @enforceManager = args[:enforceManager] || true

  @type = args[:type] || 'toggle'
  if @type != 'toggle' && @type != 'clicker'
    @type = 'toggle'
  end

  @style = args[:style] || 'badge'
  if @style != 'box' && @style != 'badge'
    @style = 'badge'
  end

  if @style == 'box'
    @size *= 2
    @labelActiveColor = args[:labelActiveColor] || Savio::Colors::White
    @labelInactiveColor = args[:labelInactiveColor] || Savio::Colors::Gray
  end

  @length = args[:length] || @size * 10
  @height = args[:height] || @size * 2

  @onClick = Proc.new {}

  build()
end

Instance Attribute Details

#buttonManagerObject

Returns the value of attribute buttonManager.



6
7
8
# File 'lib/savio/Button.rb', line 6

def buttonManager
  @buttonManager
end

#cooldownTimeObject

Returns the value of attribute cooldownTime.



6
7
8
# File 'lib/savio/Button.rb', line 6

def cooldownTime
  @cooldownTime
end

#enforceManagerObject

Returns the value of attribute enforceManager.



5
6
7
# File 'lib/savio/Button.rb', line 5

def enforceManager
  @enforceManager
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/savio/Button.rb', line 6

def height
  @height
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/savio/Button.rb', line 6

def length
  @length
end

#selectedObject

Returns the value of attribute selected.



6
7
8
# File 'lib/savio/Button.rb', line 6

def selected
  @selected
end

#styleObject

Returns the value of attribute style.



6
7
8
# File 'lib/savio/Button.rb', line 6

def style
  @style
end

#timeLastClickedObject (readonly)

Returns the value of attribute timeLastClicked.



6
7
8
# File 'lib/savio/Button.rb', line 6

def timeLastClicked
  @timeLastClicked
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/savio/Button.rb', line 6

def type
  @type
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/savio/Button.rb', line 5

def value
  @value
end

Class Method Details

.buttonsObject



9
10
11
# File 'lib/savio/Button.rb', line 9

def self.buttons
  @@buttons
end

Instance Method Details

#addObject



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/savio/Button.rb', line 168

def add()
  super()
  @nameLabel.add
  @baseCircle.add
  @selectCircle.add
  if @selected
    select()
  else
    deselect()
  end
end

#baseColor=(c) ⇒ Object



76
77
78
79
# File 'lib/savio/Button.rb', line 76

def baseColor=(c)
  @baseColor = c
  rebuild()
end

#buildObject



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
# File 'lib/savio/Button.rb', line 184

def build()
  @shown = true
  case @style
  when 'badge'
    @baseCircle = Circle.new(
      x: @x, y: @y,
      radius: @size,
      color: @baseColor,
      z: @z
    )
    @selectCircle = Circle.new(
      x: @x, y: @y,
      radius: @size * 0.8,
      color: @selectedColor,
      z: @z+1
    )
    @nameLabel = Text.new(
      @displayName.to_s,
      x: @x + @size * 2, y: @y - @size * 1.5,
      size: @size * 2,
      color: @labelInactiveColor,
      z: @z
    )
    @nameLabel.y = @baseCircle.y - @baseCircle.radius / 4 - @nameLabel.height / 2
  when 'box'
    @baseCircle = Rectangle.new(
      x: @x, y: @y,
      height: @height, width: @length,
      color: @baseColor,
      z: @z
    )
    @selectCircle = Rectangle.new(
      x: @x + (@height * 0.1), y: @y + (@height * 0.1),
      height: @height - (@height * 0.2), width: @length - (@height * 0.2),
      color: @selectedColor,
      z: @z+1
    )
    @nameLabel = Text.new(
      @displayName.to_s,
      x: @x, y: @y,
      size: @size,
      color: @labelInactiveColor,
      z: @z+2
    )
    @nameLabel.x = @baseCircle.x + @baseCircle.width / 2 - @nameLabel.width / 2
    @nameLabel.y = @baseCircle.y + @baseCircle.height / 2 - @nameLabel.height / 2
  end

  if @buttonManager == nil
    if @selected
      select()
    else
      deselect()
    end
  else
    if @buttonManager.class.name != 'Savio::ButtonManager'
      raise ArgumentError, 'Given object ' + @buttonManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
    end
    @buttonManager.addButton(self)

    if @selected
      @buttonManager.select(self)
    else
      @buttonManager.deselect(self)
    end
  end

end

#clickObject



180
181
182
# File 'lib/savio/Button.rb', line 180

def click()
  @onClick.call()
end

#deselect(enforce = @enforceManager) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/savio/Button.rb', line 143

def deselect(enforce = @enforceManager)
  if enforce == true && @buttonManager != nil
    @buttonManager.deselect(self)
  else
    @selectCircle.remove
    @nameLabel.color = @labelInactiveColor
    @selected = false
  end
end

#labelColor=(c) ⇒ Object



84
85
86
87
# File 'lib/savio/Button.rb', line 84

def labelColor=(c)
  @labelColor = c
  rebuild()
end

#onClick(&proc) ⇒ Object



97
98
99
# File 'lib/savio/Button.rb', line 97

def onClick(&proc)
  @onClick = proc
end

#removeObject



161
162
163
164
165
166
# File 'lib/savio/Button.rb', line 161

def remove()
  super()
  @nameLabel.remove
  @baseCircle.remove
  @selectCircle.remove
end

#select(enforce = @enforceManager) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/savio/Button.rb', line 119

def select(enforce = @enforceManager)
  if Time.now.to_f - @timeLastClicked.to_f >= @cooldownTime.to_f
    @timeLastClicked = Time.now.to_f
    click()
    if enforce == true && @buttonManager != nil
      @buttonManager.select(self)
    else
      @selectCircle.add
      @nameLabel.color = @labelActiveColor
      @selected = true
      if @type == 'clicker'
        fade = Thread.new {
          @selectCircle.add
          @nameLabel.color = @labelActiveColor
          sleep(0.06)
          @selectCircle.remove
          @nameLabel.color = @labelInactiveColor
        }
        deselect(enforce)
      end
    end
  end
end

#selectedColor=(c) ⇒ Object



80
81
82
83
# File 'lib/savio/Button.rb', line 80

def selectedColor=(c)
  @selectedColor = c
  rebuild()
end

#size=(size) ⇒ Object



55
56
57
58
59
# File 'lib/savio/Button.rb', line 55

def size=(size)
  @length = size * 10
  @height = size * 2
  super(size)
end

#toggle(enforce = @enforceManager) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/savio/Button.rb', line 153

def toggle(enforce = @enforceManager)
  if @selected
    deselect(enforce)
  else
    select(enforce)
  end
end