savio 0.1.4 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/savio/Scene.rb CHANGED
@@ -1,55 +1,55 @@
1
- module Savio
2
- class Scene
3
- attr_accessor :elements
4
- def initialize(file, color = 'black')
5
- @file = file
6
- @color = color
7
- show()
8
- end
9
-
10
- def show()
11
- @bg = Square.new(x: 0, y: 0, z: 999, size: Window.width+1, color: @color)
12
- @elements = []
13
- content = File.read(@file)
14
- content = content.gsub('[', "").gsub(']', "").gsub('"',"").chomp.split(",")
15
-
16
- (content.count-1).times do |i|
17
- if (i % 2) == 0
18
- classType = Object.const_get(content[i].gsub(" ",""))
19
- hash = content[i+1].gsub(';', ',').gsub('"',"").chomp
20
- pairs = hash.split(",").to_s.gsub('"',"").chomp
21
- keypair = pairs.split("=>").to_s.gsub('"',"").chomp
22
- new = keypair.gsub('{', "").gsub('}', "").gsub('[', "").gsub(']', "").gsub('"',"").gsub(" ","").chomp.split(",")
23
- build = {}
24
- (new.count-1).times do |j|
25
- if (j % 2) == 0
26
- case Savio.guessType(new[j+1].chomp)
27
- when "int"
28
- value = new[j+1].chomp.to_i
29
- when "float"
30
- value = new[j+1].chomp.to_f
31
- when "str"
32
- value = new[j+1].chomp.to_s
33
- when "bool"
34
- value = Savio.makeBool(new[j+1].chomp)
35
- end
36
- build[new[j].intern] = value
37
- end
38
- end
39
- puts "SCENE: " + @file.to_s + build.to_s
40
- @elements.push(classType.new(build))
41
- end
42
- end
43
- @elements.each do |e|
44
- e.z = 1000
45
- end
46
- end
47
-
48
- def remove()
49
- @bg.remove
50
- @elements.each do |e|
51
- e.kill()
52
- end
53
- end
54
- end
55
- end
1
+ module Savio
2
+ class Scene
3
+ attr_accessor :elements
4
+ def initialize(file, color = 'black')
5
+ @file = file
6
+ @color = color
7
+ show()
8
+ end
9
+
10
+ def show()
11
+ @bg = Square.new(x: 0, y: 0, z: 999, size: Window.width+1, color: @color)
12
+ @elements = []
13
+ content = File.read(@file)
14
+ content = content.gsub('[', "").gsub(']', "").gsub('"',"").chomp.split(",")
15
+
16
+ (content.count-1).times do |i|
17
+ if (i % 2) == 0
18
+ classType = Object.const_get(content[i].gsub(" ",""))
19
+ hash = content[i+1].gsub(';', ',').gsub('"',"").chomp
20
+ pairs = hash.split(",").to_s.gsub('"',"").chomp
21
+ keypair = pairs.split("=>").to_s.gsub('"',"").chomp
22
+ new = keypair.gsub('{', "").gsub('}', "").gsub('[', "").gsub(']', "").gsub('"',"").gsub(" ","").chomp.split(",")
23
+ build = {}
24
+ (new.count-1).times do |j|
25
+ if (j % 2) == 0
26
+ case Savio.guessType(new[j+1].chomp)
27
+ when "int"
28
+ value = new[j+1].chomp.to_i
29
+ when "float"
30
+ value = new[j+1].chomp.to_f
31
+ when "str"
32
+ value = new[j+1].chomp.to_s
33
+ when "bool"
34
+ value = Savio.makeBool(new[j+1].chomp)
35
+ end
36
+ build[new[j].intern] = value
37
+ end
38
+ end
39
+ puts "SCENE: " + @file.to_s + build.to_s
40
+ @elements.push(classType.new(build))
41
+ end
42
+ end
43
+ @elements.each do |e|
44
+ e.z = 1000
45
+ end
46
+ end
47
+
48
+ def remove()
49
+ @bg.remove
50
+ @elements.each do |e|
51
+ e.kill()
52
+ end
53
+ end
54
+ end
55
+ end
data/lib/savio/Slider.rb CHANGED
@@ -1,195 +1,195 @@
1
- module Savio
2
- class Slider
3
- include IORenderable
4
-
5
- attr_reader :length, :min, :max, :size, :value
6
-
7
- @@sliders = []
8
- def self.sliders
9
- @@sliders
10
- end
11
-
12
- def initialize(args = {})
13
- @@sliders.push(self)
14
- super(args)
15
-
16
- @length = args[:length] || 100
17
- @min = args[:min] || 0
18
- @max = args[:max] || 100
19
-
20
- @style = args[:style] || 'knob'
21
- if @style != 'knob' && @style != 'fill'
22
- @style = 'knob'
23
- end
24
-
25
- @value = args[:value] || rand(@min..@max)
26
- @showValue = args[:showValue] || true
27
-
28
- @labelColor = args[:labelColor] || Savio::Colors::White
29
- @sliderColor = args[:sliderColor] || Savio::Colors::Gray
30
- @knobColor = args[:knobColor] || Savio::Colors::Green
31
-
32
- @onChange = Proc.new {}
33
-
34
- build()
35
- end
36
-
37
- def onChange(&proc)
38
- @onChange = proc
39
- end
40
-
41
- def style=(style)
42
- if style == 'knob' || style == 'fill'
43
- @style = style
44
- rebuild()
45
- end
46
- end
47
- def length=(length)
48
- @length = length.clamp(1, Window.width-@x)
49
- rebuild()
50
- end
51
- def showValue=(state)
52
- @showValue = state
53
- rebuild()
54
- end
55
- def labelColor=(c)
56
- @labelColor = c
57
- rebuild()
58
- end
59
- def sliderColor=(c)
60
- @sliderColor = c
61
- rebuild()
62
- end
63
- def knobColor=(c)
64
- @knobColor = c
65
- rebuild()
66
- end
67
- def min=(x)
68
- @min = x
69
- setValue(@value)
70
- end
71
- def max=(x)
72
- @max = x
73
- setValue(@value)
74
- end
75
-
76
- def renderKnobTo(x)
77
- if x.between?(@x, @x+@length)
78
- case @style
79
- when 'knob'
80
- @knob.x = x
81
- when 'fill'
82
- @knob.width = x - @x
83
- end
84
- end
85
- end
86
-
87
- def moveKnob(x)
88
- if x.between?(@x, @x+@length)
89
- renderKnobTo(x)
90
-
91
- to_max = @max
92
- to_min = @min
93
- from_max = @x + @length
94
- from_min = @x
95
- pos = x
96
- @value = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
97
- if @showValue == true
98
- @label.text = @value.round(2).to_s
99
- end
100
- @onChange.call
101
- end
102
- end
103
-
104
- def value=(value)
105
- setValue(value)
106
- end
107
-
108
- def setValue(value)
109
- if value.between?(@min, @max)
110
- to_max = @x + @length
111
- to_min = @x
112
- from_max = @max.to_f
113
- from_min = @min.to_f
114
- pos = value
115
- knobX = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
116
- @value = value
117
- if @showValue == true
118
- @label.text = @value.round(2).to_s
119
- end
120
- renderKnobTo(knobX)
121
- else
122
- setValue(@min)
123
- end
124
- end
125
-
126
- def remove()
127
- super()
128
- @sliderLine.remove
129
- @knob.remove
130
- @label.remove
131
- @nameLabel.remove
132
- end
133
-
134
- def add()
135
- super()
136
- @sliderLine.add
137
- @knob.add
138
- @label.add
139
- @nameLabel.add
140
- end
141
-
142
- def build()
143
- @shown = true
144
-
145
- @sliderLine = Line.new(
146
- x1: @x, y1: @y,
147
- x2: @x+@length, y2: @y,
148
- width: @size,
149
- color: @sliderColor,
150
- z: @z
151
- )
152
-
153
- case @style
154
- when 'knob'
155
- @knob = Circle.new(
156
- x: @x, y: @y,
157
- radius: @size * 1.2,
158
- color: @knobColor,
159
- z: @z+1
160
- )
161
- when 'fill'
162
- @knob = Rectangle.new(
163
- x: @x, y: @y,
164
- width: 0 , height: @size,
165
- color: @knobColor,
166
- z: @z+1
167
- )
168
- end
169
-
170
- @label = Text.new(
171
- @value.to_s,
172
- x: @x + @length + @size, y: @y - @size * 1.75,
173
- size: @size * 2.5,
174
- color: @labelColor,
175
- z: @z+1
176
- )
177
-
178
- @label.y = @y + @sliderLine.width / 2 - @label.height / 2
179
-
180
- @nameLabel = Text.new(
181
- @displayName.to_s,
182
- x: @x, y: @y - @size * 3 - @size,
183
- size: @size * 2.5,
184
- color: @labelColor,
185
- z: @z+2
186
- )
187
-
188
- setValue(@value)
189
-
190
- if @showValue == false
191
- @label.remove
192
- end
193
- end
194
- end
195
- end
1
+ module Savio
2
+ class Slider
3
+ include IORenderable
4
+
5
+ attr_reader :length, :min, :max, :size, :value
6
+
7
+ @@sliders = []
8
+ def self.sliders
9
+ @@sliders
10
+ end
11
+
12
+ def initialize(args = {})
13
+ @@sliders.push(self)
14
+ super(args)
15
+
16
+ @length = args[:length] || 100
17
+ @min = args[:min] || 0
18
+ @max = args[:max] || 100
19
+
20
+ @style = args[:style] || 'knob'
21
+ if @style != 'knob' && @style != 'fill'
22
+ @style = 'knob'
23
+ end
24
+
25
+ @value = args[:value] || rand(@min..@max)
26
+ @showValue = args[:showValue] || true
27
+
28
+ @labelColor = args[:labelColor] || Savio::Colors::White
29
+ @sliderColor = args[:sliderColor] || Savio::Colors::Gray
30
+ @knobColor = args[:knobColor] || Savio::Colors::Green
31
+
32
+ @onChange = Proc.new {}
33
+
34
+ build()
35
+ end
36
+
37
+ def onChange(&proc)
38
+ @onChange = proc
39
+ end
40
+
41
+ def style=(style)
42
+ if style == 'knob' || style == 'fill'
43
+ @style = style
44
+ rebuild()
45
+ end
46
+ end
47
+ def length=(length)
48
+ @length = length.clamp(1, Window.width-@x)
49
+ rebuild()
50
+ end
51
+ def showValue=(state)
52
+ @showValue = state
53
+ rebuild()
54
+ end
55
+ def labelColor=(c)
56
+ @labelColor = c
57
+ rebuild()
58
+ end
59
+ def sliderColor=(c)
60
+ @sliderColor = c
61
+ rebuild()
62
+ end
63
+ def knobColor=(c)
64
+ @knobColor = c
65
+ rebuild()
66
+ end
67
+ def min=(x)
68
+ @min = x
69
+ setValue(@value)
70
+ end
71
+ def max=(x)
72
+ @max = x
73
+ setValue(@value)
74
+ end
75
+
76
+ def renderKnobTo(x)
77
+ if x.between?(@x, @x+@length)
78
+ case @style
79
+ when 'knob'
80
+ @knob.x = x
81
+ when 'fill'
82
+ @knob.width = x - @x
83
+ end
84
+ end
85
+ end
86
+
87
+ def moveKnob(x)
88
+ if x.between?(@x, @x+@length)
89
+ renderKnobTo(x)
90
+
91
+ to_max = @max
92
+ to_min = @min
93
+ from_max = @x + @length
94
+ from_min = @x
95
+ pos = x
96
+ @value = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
97
+ if @showValue == true
98
+ @label.text = @value.round(2).to_s
99
+ end
100
+ @onChange.call
101
+ end
102
+ end
103
+
104
+ def value=(value)
105
+ setValue(value)
106
+ end
107
+
108
+ def setValue(value)
109
+ if value.between?(@min, @max)
110
+ to_max = @x + @length
111
+ to_min = @x
112
+ from_max = @max.to_f
113
+ from_min = @min.to_f
114
+ pos = value
115
+ knobX = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
116
+ @value = value
117
+ if @showValue == true
118
+ @label.text = @value.round(2).to_s
119
+ end
120
+ renderKnobTo(knobX)
121
+ else
122
+ setValue(@min)
123
+ end
124
+ end
125
+
126
+ def remove()
127
+ super()
128
+ @sliderLine.remove
129
+ @knob.remove
130
+ @label.remove
131
+ @nameLabel.remove
132
+ end
133
+
134
+ def add()
135
+ super()
136
+ @sliderLine.add
137
+ @knob.add
138
+ @label.add
139
+ @nameLabel.add
140
+ end
141
+
142
+ def build()
143
+ @shown = true
144
+
145
+ @sliderLine = Line.new(
146
+ x1: @x, y1: @y,
147
+ x2: @x+@length, y2: @y,
148
+ width: @size,
149
+ color: @sliderColor,
150
+ z: @z
151
+ )
152
+
153
+ case @style
154
+ when 'knob'
155
+ @knob = Circle.new(
156
+ x: @x, y: @y,
157
+ radius: @size * 1.2,
158
+ color: @knobColor,
159
+ z: @z+1
160
+ )
161
+ when 'fill'
162
+ @knob = Rectangle.new(
163
+ x: @x, y: @y,
164
+ width: 0 , height: @size,
165
+ color: @knobColor,
166
+ z: @z+1
167
+ )
168
+ end
169
+
170
+ @label = Text.new(
171
+ @value.to_s,
172
+ x: @x + @length + @size, y: @y - @size * 1.75,
173
+ size: @size * 2.5,
174
+ color: @labelColor,
175
+ z: @z+1
176
+ )
177
+
178
+ @label.y = @y + @sliderLine.width / 2 - @label.height / 2
179
+
180
+ @nameLabel = Text.new(
181
+ @displayName.to_s,
182
+ x: @x, y: @y - @size * 3 - @size,
183
+ size: @size * 2.5,
184
+ color: @labelColor,
185
+ z: @z+2
186
+ )
187
+
188
+ setValue(@value)
189
+
190
+ if @showValue == false
191
+ @label.remove
192
+ end
193
+ end
194
+ end
195
+ end
data/lib/savio/hsv2rgb.rb CHANGED
@@ -1,78 +1,78 @@
1
- module Savio
2
- RgbColor = Struct.new(:r, :g, :b) do
3
- def self.newFromHSV(hsv)
4
- rgb = hsv.to_rgb()
5
- return RgbColor.new(rgb.r,rgb.g,rgb.b)
6
- end
7
- def to_hsv()
8
- max = [r, g, b].max
9
- min = [r, g, b].min
10
- delta = max - min
11
- v = max
12
-
13
- if (max != 0.0)
14
- s = delta / max
15
- else
16
- s = 0.0
17
- end
18
-
19
- if (s == 0.0)
20
- h = 0.0
21
- else
22
- if (r == max)
23
- h = 0 + (g - b) / delta
24
- elsif (g == max)
25
- h = 2 + (b - r) / delta
26
- elsif (b == max)
27
- h = 4 + (r - g) / delta
28
- end
29
-
30
- h *= 60.0
31
-
32
- if (h < 0)
33
- h += 360.0
34
- end
35
- end
36
- return HsvColor.new(h,s,v)
37
- end
38
- end
39
- HsvColor = Struct.new(:h, :s, :v) do
40
- def self.newFromRGB(rgb)
41
- hsv = rgb.to_hsv()
42
- return HsvColor.new(hsv.h,hsv.s,hsv.v)
43
- end
44
- def to_rgb()
45
- hue = h.to_f
46
- saturation = s.to_f
47
- value = v.to_f
48
-
49
- chroma = (value * saturation).to_f
50
- hPrime = hue/60.0
51
- x = (chroma * (1 - (hPrime % 2 - 1).abs)).to_f
52
-
53
- if 0 <= hPrime && hPrime < 1
54
- rgb = [chroma, x, 0]
55
- elsif 1 <= hPrime && hPrime < 2
56
- rgb = [x, chroma, 0]
57
- elsif 2 <= hPrime && hPrime < 3
58
- rgb = [0, chroma, x]
59
- elsif 3 <= hPrime && hPrime < 4
60
- rgb = [0, x, chroma]
61
- elsif 4 <= hPrime && hPrime < 5
62
- rgb = [x, 0, chroma]
63
- elsif 5 <= hPrime && hPrime < 6
64
- rgb = [chroma, 0, x]
65
- else
66
- rgb = [0,0,0]
67
- end
68
-
69
- match = (value - chroma).to_f
70
-
71
- rgb[0] = (rgb[0] + match).to_f
72
- rgb[1] = (rgb[1] + match).to_f
73
- rgb[2] = (rgb[2] + match).to_f
74
-
75
- return RgbColor.new(rgb[0],rgb[1],rgb[2])
76
- end
77
- end
78
- end
1
+ module Savio
2
+ RgbColor = Struct.new(:r, :g, :b) do
3
+ def self.newFromHSV(hsv)
4
+ rgb = hsv.to_rgb()
5
+ return RgbColor.new(rgb.r,rgb.g,rgb.b)
6
+ end
7
+ def to_hsv()
8
+ max = [r, g, b].max
9
+ min = [r, g, b].min
10
+ delta = max - min
11
+ v = max
12
+
13
+ if (max != 0.0)
14
+ s = delta / max
15
+ else
16
+ s = 0.0
17
+ end
18
+
19
+ if (s == 0.0)
20
+ h = 0.0
21
+ else
22
+ if (r == max)
23
+ h = 0 + (g - b) / delta
24
+ elsif (g == max)
25
+ h = 2 + (b - r) / delta
26
+ elsif (b == max)
27
+ h = 4 + (r - g) / delta
28
+ end
29
+
30
+ h *= 60.0
31
+
32
+ if (h < 0)
33
+ h += 360.0
34
+ end
35
+ end
36
+ return HsvColor.new(h,s,v)
37
+ end
38
+ end
39
+ HsvColor = Struct.new(:h, :s, :v) do
40
+ def self.newFromRGB(rgb)
41
+ hsv = rgb.to_hsv()
42
+ return HsvColor.new(hsv.h,hsv.s,hsv.v)
43
+ end
44
+ def to_rgb()
45
+ hue = h.to_f
46
+ saturation = s.to_f
47
+ value = v.to_f
48
+
49
+ chroma = (value * saturation).to_f
50
+ hPrime = hue/60.0
51
+ x = (chroma * (1 - (hPrime % 2 - 1).abs)).to_f
52
+
53
+ if 0 <= hPrime && hPrime < 1
54
+ rgb = [chroma, x, 0]
55
+ elsif 1 <= hPrime && hPrime < 2
56
+ rgb = [x, chroma, 0]
57
+ elsif 2 <= hPrime && hPrime < 3
58
+ rgb = [0, chroma, x]
59
+ elsif 3 <= hPrime && hPrime < 4
60
+ rgb = [0, x, chroma]
61
+ elsif 4 <= hPrime && hPrime < 5
62
+ rgb = [x, 0, chroma]
63
+ elsif 5 <= hPrime && hPrime < 6
64
+ rgb = [chroma, 0, x]
65
+ else
66
+ rgb = [0,0,0]
67
+ end
68
+
69
+ match = (value - chroma).to_f
70
+
71
+ rgb[0] = (rgb[0] + match).to_f
72
+ rgb[1] = (rgb[1] + match).to_f
73
+ rgb[2] = (rgb[2] + match).to_f
74
+
75
+ return RgbColor.new(rgb[0],rgb[1],rgb[2])
76
+ end
77
+ end
78
+ end