Class: Savio::Slider

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

Constant Summary collapse

@@sliders =
[]

Instance Attribute Summary collapse

Attributes included from IORenderable

#allowDrag, #displayName, #draggingEnabled, #duplicate, #enabled, #shown, #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 = {}) ⇒ Slider

Returns a new instance of Slider.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/savio/Slider.rb', line 12

def initialize(args = {})
  @@sliders.push(self)
  super(args)

  @length = args[:length]   || 100
  @min = args[:min]         || 0
  @max = args[:max]         || 100

  @style = args[:style] || 'knob'
  if @style != 'knob' && @style != 'fill'
    @style = 'knob'
  end

  @value = args[:value]     || rand(@min..@max)
  @showValue = args[:showValue] || true

  @labelColor  = args[:labelColor]  || Savio::Colors::White
  @sliderColor = args[:sliderColor] || Savio::Colors::Gray
  @knobColor   = args[:knobColor]   || Savio::Colors::Green

  @onChange = Proc.new {}

  build()
end

Instance Attribute Details

#lengthObject

Returns the value of attribute length.



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

def length
  @length
end

#maxObject

Returns the value of attribute max.



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

def max
  @max
end

#minObject

Returns the value of attribute min.



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

def min
  @min
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.slidersObject



8
9
10
# File 'lib/savio/Slider.rb', line 8

def self.sliders
  @@sliders
end

Instance Method Details

#addObject



134
135
136
137
138
139
140
# File 'lib/savio/Slider.rb', line 134

def add()
  super()
  @sliderLine.add
  @knob.add
  @label.add
  @nameLabel.add
end

#buildObject



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

def build()
  @shown = true

  @sliderLine = Line.new(
    x1: @x, y1: @y,
    x2: @x+@length, y2: @y,
    width: @size,
    color: @sliderColor,
    z: @z
  )

  case @style
  when 'knob'
    @knob = Circle.new(
      x: @x, y: @y,
      radius: @size * 1.2,
      color: @knobColor,
      z: @z+1
    )
  when 'fill'
    @knob = Rectangle.new(
      x: @x, y: @y,
      width: 0 , height: @size,
      color: @knobColor,
      z: @z+1
    )
  end

  @label = Text.new(
    @value.to_s,
    x: @x + @length + @size, y: @y - @size * 1.75,
    size: @size * 2.5,
    color: @labelColor,
    z: @z+1
  )

  @label.y = @y + @sliderLine.width / 2 - @label.height / 2

  @nameLabel = Text.new(
    @displayName.to_s,
    x: @x, y: @y - @size * 3 - @size,
    size: @size * 2.5,
    color: @labelColor,
    z: @z+2
  )

  setValue(@value)

  if @showValue == false
    @label.remove
  end
end

#knobColor=(c) ⇒ Object



63
64
65
66
# File 'lib/savio/Slider.rb', line 63

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

#labelColor=(c) ⇒ Object



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

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

#moveKnob(x) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/savio/Slider.rb', line 87

def moveKnob(x)
  if x.between?(@x, @x+@length)
    renderKnobTo(x)

    to_max = @max
    to_min = @min
    from_max = @x + @length
    from_min = @x
    pos = x
    @value = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
    if @showValue == true
      @label.text = @value.round(2).to_s
    end
    @onChange.call
  end
end

#onChange(&proc) ⇒ Object



37
38
39
# File 'lib/savio/Slider.rb', line 37

def onChange(&proc)
  @onChange = proc
end

#removeObject



126
127
128
129
130
131
132
# File 'lib/savio/Slider.rb', line 126

def remove()
  super()
  @sliderLine.remove
  @knob.remove
  @label.remove
  @nameLabel.remove
end

#renderKnobTo(x) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/savio/Slider.rb', line 76

def renderKnobTo(x)
  if x.between?(@x, @x+@length)
    case @style
    when 'knob'
      @knob.x = x
    when 'fill'
      @knob.width = x - @x
    end
  end
end

#setValue(value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/savio/Slider.rb', line 108

def setValue(value)
  if value.between?(@min, @max)
    to_max = @x + @length
    to_min = @x
    from_max = @max.to_f
    from_min = @min.to_f
    pos = value
    knobX = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
    @value = value
    if @showValue == true
      @label.text = @value.round(2).to_s
    end
    renderKnobTo(knobX)
  else
    setValue(@min)
  end
end

#showValue=(state) ⇒ Object



51
52
53
54
# File 'lib/savio/Slider.rb', line 51

def showValue=(state)
  @showValue = state
  rebuild()
end

#sliderColor=(c) ⇒ Object



59
60
61
62
# File 'lib/savio/Slider.rb', line 59

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

#style=(style) ⇒ Object



41
42
43
44
45
46
# File 'lib/savio/Slider.rb', line 41

def style=(style)
  if style == 'knob' || style == 'fill'
    @style = style
    rebuild()
  end
end