rubysketch 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/VERSION +1 -1
- data/lib/rubysketch/processing.rb +150 -9
- data/lib/rubysketch/starter.rb +9 -3
- data/lib/rubysketch/window.rb +32 -12
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31a6c8685fd803f34636e81ee5098dfda6c2d82ab500952662e5b77c30601c09
|
4
|
+
data.tar.gz: e4f22f4dbf9d01a0cc4f5f084648698ae42c0a1eddf582c0848849b194ac5933
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5df3c266fb39715d6c83cda24268e48f27e922292398cda0f3c84412089fe88bc0a08a6373cd4b97faf342b64a79b3c7d7343794081522c7ae4894e8d7cefdc9
|
7
|
+
data.tar.gz: 2c182badae8ae8f471647a2fc8289e7151730985de955b54d29f51c04ed8ee242ea89c274303e2f9d5b4d6189186b7ea635bc464d3ece7af5329124729f5c999
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--no-private
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
@@ -5,9 +5,10 @@ module RubySketch
|
|
5
5
|
#
|
6
6
|
class Processing1
|
7
7
|
|
8
|
+
include Math
|
9
|
+
|
8
10
|
extend Starter
|
9
11
|
|
10
|
-
PI = Math::PI
|
11
12
|
HALF_PI = PI / 2
|
12
13
|
QUARTER_PI = PI / 4
|
13
14
|
TWO_PI = PI * 2
|
@@ -54,7 +55,7 @@ module RubySketch
|
|
54
55
|
# @private
|
55
56
|
def on_start__ (window)
|
56
57
|
@window__ = window
|
57
|
-
@painter__ = window.
|
58
|
+
@painter__ = window.canvas_painter
|
58
59
|
|
59
60
|
setupDrawBlock__
|
60
61
|
setupMousePressedBlock__
|
@@ -63,22 +64,153 @@ module RubySketch
|
|
63
64
|
setupMouseDraggedBlock__
|
64
65
|
end
|
65
66
|
|
67
|
+
# Returns the absolute number of the value.
|
68
|
+
#
|
69
|
+
# @param value [Numeric] number
|
70
|
+
#
|
71
|
+
# @return [Numeric] absolute number
|
72
|
+
#
|
66
73
|
def abs (value)
|
67
74
|
value.abs
|
68
75
|
end
|
69
76
|
|
77
|
+
# Returns the closest integer number greater than or equal to the value.
|
78
|
+
#
|
79
|
+
# @param value [Numeric] number
|
80
|
+
#
|
81
|
+
# @return [Numeric] rounded up number
|
82
|
+
#
|
70
83
|
def ceil (value)
|
71
84
|
value.ceil
|
72
85
|
end
|
73
86
|
|
87
|
+
# Returns the closest integer number less than or equal to the value.
|
88
|
+
#
|
89
|
+
# @param value [Numeric] number
|
90
|
+
#
|
91
|
+
# @return [Numeric] rounded down number
|
92
|
+
#
|
74
93
|
def floor (value)
|
75
94
|
value.floor
|
76
95
|
end
|
77
96
|
|
97
|
+
# Returns the closest integer number.
|
98
|
+
#
|
99
|
+
# @param value [Numeric] number
|
100
|
+
#
|
101
|
+
# @return [Numeric] rounded number
|
102
|
+
#
|
78
103
|
def round (value)
|
79
104
|
value.round
|
80
105
|
end
|
81
106
|
|
107
|
+
# Returns value raised to the power of exponent.
|
108
|
+
#
|
109
|
+
# @param value [Numeric] base number
|
110
|
+
# @param exponent [Numeric] exponent number
|
111
|
+
#
|
112
|
+
# @return [Numeric] value ** exponent
|
113
|
+
#
|
114
|
+
def pow (value, exponent)
|
115
|
+
value ** exponent
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns squared value.
|
119
|
+
#
|
120
|
+
# @param value [Numeric] number
|
121
|
+
#
|
122
|
+
# @return [Numeric] squared value
|
123
|
+
#
|
124
|
+
def sq (value)
|
125
|
+
value * value
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns the magnitude (or length) of a vector.
|
129
|
+
#
|
130
|
+
# @overload mag(x, y)
|
131
|
+
# @overload mag(x, y, z)
|
132
|
+
#
|
133
|
+
# @param x [Numeric] x of point
|
134
|
+
# @param y [Numeric] y of point
|
135
|
+
# @param z [Numeric] z of point
|
136
|
+
#
|
137
|
+
# @return [Numeric] magnitude
|
138
|
+
#
|
139
|
+
def mag (*args)
|
140
|
+
x, y, z = *args
|
141
|
+
case args.size
|
142
|
+
when 2 then sqrt x * x + y * y
|
143
|
+
when 3 then sqrt x * x + y * y + z * z
|
144
|
+
else raise ArgumentError
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns distance between 2 points.
|
149
|
+
#
|
150
|
+
# @overload dist(x1, y1, x2, y2)
|
151
|
+
# @overload dist(x1, y1, z1, x2, y2, z2)
|
152
|
+
#
|
153
|
+
# @param x1 [Numeric] x of first point
|
154
|
+
# @param y1 [Numeric] y of first point
|
155
|
+
# @param z1 [Numeric] z of first point
|
156
|
+
# @param x2 [Numeric] x of second point
|
157
|
+
# @param y2 [Numeric] y of second point
|
158
|
+
# @param z2 [Numeric] z of second point
|
159
|
+
#
|
160
|
+
# @return [Numeric] distance between 2 points
|
161
|
+
#
|
162
|
+
def dist (*args)
|
163
|
+
case args.size
|
164
|
+
when 4
|
165
|
+
x1, y1, x2, y2 = *args
|
166
|
+
xx, yy = x2 - x1, y2 - y1
|
167
|
+
sqrt xx * xx + yy * yy
|
168
|
+
when 3
|
169
|
+
x1, y1, z1, x2, y2, z2 = *args
|
170
|
+
xx, yy, zz = x2 - x1, y2 - y1, z2 - z1
|
171
|
+
sqrt xx * xx + yy * yy + zz * zz
|
172
|
+
else raise ArgumentError
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
# Normalize the value from range start..stop into 0..1.
|
177
|
+
#
|
178
|
+
# @param value [Numeric] number to be normalized
|
179
|
+
# @param start [Numeric] lower bound of the range
|
180
|
+
# @param stop [Numeric] upper bound of the range
|
181
|
+
#
|
182
|
+
# @return [Numeric] normalized value between 0..1
|
183
|
+
#
|
184
|
+
def norm (value, start, stop)
|
185
|
+
(value.to_f - start.to_f) / (stop.to_f - start.to_f)
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns the interpolated number between range start..stop.
|
189
|
+
#
|
190
|
+
# @param start [Numeric] lower bound of the range
|
191
|
+
# @param stop [Numeric] upper bound of the range
|
192
|
+
# @param amount [Numeric] amount to interpolate
|
193
|
+
#
|
194
|
+
# @return [Numeric] interporated number
|
195
|
+
#
|
196
|
+
def lerp (start, stop, amount)
|
197
|
+
start + (stop - start) * amount
|
198
|
+
end
|
199
|
+
|
200
|
+
# Maps a number from range start1..stop1 to range start2..stop2.
|
201
|
+
#
|
202
|
+
# @param value [Numeric] number to be mapped
|
203
|
+
# @param start1 [Numeric] lower bound of the range1
|
204
|
+
# @param stop1 [Numeric] upper bound of the range1
|
205
|
+
# @param start2 [Numeric] lower bound of the range2
|
206
|
+
# @param stop2 [Numeric] upper bound of the range2
|
207
|
+
#
|
208
|
+
# @return [Numeric] mapped number
|
209
|
+
#
|
210
|
+
def map (value, start1, stop1, start2, stop2)
|
211
|
+
lerp start2, stop2, norm(value, start1, stop1)
|
212
|
+
end
|
213
|
+
|
82
214
|
# Returns minimum value.
|
83
215
|
#
|
84
216
|
# @overload min(a, b)
|
@@ -113,6 +245,14 @@ module RubySketch
|
|
113
245
|
args.flatten.max
|
114
246
|
end
|
115
247
|
|
248
|
+
# Constrains the number between min..max.
|
249
|
+
#
|
250
|
+
# @param value [Numeric] number to be constrained
|
251
|
+
# @param min [Numeric] lower bound of the range
|
252
|
+
# @param max [Numeric] upper bound of the range
|
253
|
+
#
|
254
|
+
# @return [Numeric] constrained number
|
255
|
+
#
|
116
256
|
def constrain (value, min, max)
|
117
257
|
value < min ? min : (value > max ? max : value)
|
118
258
|
end
|
@@ -146,7 +286,7 @@ module RubySketch
|
|
146
286
|
def setupDrawBlock__ ()
|
147
287
|
@window__.draw = proc do |e, painter|
|
148
288
|
@painter__ = painter
|
149
|
-
@
|
289
|
+
@drawBlock__.call e if @drawBlock__
|
150
290
|
@frameCount__ += 1
|
151
291
|
updateMousePrevPos__
|
152
292
|
end
|
@@ -227,7 +367,8 @@ module RubySketch
|
|
227
367
|
@mousePrevY__ = @mouseY__
|
228
368
|
end
|
229
369
|
|
230
|
-
|
370
|
+
# @private
|
371
|
+
def size__ (width, height)
|
231
372
|
raise 'size() must be called on startup or setup block' if @started__
|
232
373
|
|
233
374
|
@painter__.send :end_paint
|
@@ -339,12 +480,12 @@ module RubySketch
|
|
339
480
|
|
340
481
|
# @private
|
341
482
|
def to_rgba__ (*args)
|
342
|
-
|
343
|
-
return parse_color__(
|
483
|
+
a, b, c, d = args
|
484
|
+
return parse_color__(a, b || alphaMax__) if a.kind_of?(String)
|
344
485
|
|
345
486
|
rgba = case args.size
|
346
|
-
when 1, 2 then [
|
347
|
-
when 3, 4 then [
|
487
|
+
when 1, 2 then [a, a, a, b || alphaMax__]
|
488
|
+
when 3, 4 then [a, b, c, d || alphaMax__]
|
348
489
|
else raise ArgumentError
|
349
490
|
end
|
350
491
|
rgba = rgba.map.with_index {|value, i| value / @colorMaxes__[i]}
|
@@ -357,7 +498,7 @@ module RubySketch
|
|
357
498
|
result = str.match /^\s*##{'([0-9a-f]{2})' * 3}\s*$/i
|
358
499
|
raise ArgumentError, "Invalid color code: '#{str}'" unless result
|
359
500
|
|
360
|
-
rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) /
|
501
|
+
rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) / 255.0}
|
361
502
|
return *rgb, (alpha / alphaMax__)
|
362
503
|
end
|
363
504
|
|
data/lib/rubysketch/starter.rb
CHANGED
@@ -4,10 +4,11 @@ module RubySketch
|
|
4
4
|
extend module Functions
|
5
5
|
|
6
6
|
def start (context, start_at_exit: false, &block)
|
7
|
-
window = Window.new
|
7
|
+
window = Window.new do
|
8
|
+
context.instance_eval &block if block
|
9
|
+
end
|
8
10
|
|
9
11
|
context.on_start__ window
|
10
|
-
context.instance_eval &block if block
|
11
12
|
|
12
13
|
start = proc do
|
13
14
|
Reflex.start {window.show}
|
@@ -18,6 +19,8 @@ module RubySketch
|
|
18
19
|
else
|
19
20
|
start.call
|
20
21
|
end
|
22
|
+
|
23
|
+
window
|
21
24
|
end
|
22
25
|
|
23
26
|
private
|
@@ -41,7 +44,10 @@ module RubySketch
|
|
41
44
|
end
|
42
45
|
end
|
43
46
|
|
44
|
-
start context, start_at_exit: true
|
47
|
+
window = start context, start_at_exit: true
|
48
|
+
|
49
|
+
window.canvas_painter.__send__ :begin_paint
|
50
|
+
at_exit {window.canvas_painter.__send__ :end_paint}
|
45
51
|
end
|
46
52
|
|
47
53
|
self
|
data/lib/rubysketch/window.rb
CHANGED
@@ -8,19 +8,24 @@ module RubySketch
|
|
8
8
|
|
9
9
|
attr_accessor :auto_resize
|
10
10
|
|
11
|
-
attr_reader :canvas
|
11
|
+
attr_reader :canvas, :canvas_painter
|
12
12
|
|
13
13
|
def initialize (width = 500, height = 500, *args, &block)
|
14
|
-
@canvas = nil
|
15
14
|
@events = []
|
16
15
|
@auto_resize = true
|
17
16
|
@error = nil
|
18
17
|
|
18
|
+
reset_canvas 1, 1
|
19
|
+
|
19
20
|
super *args, size: [width, height] do |_|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
start &block if block
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def start (&block)
|
26
|
+
@canvas_painter.paint do |_|
|
27
|
+
block.call if block
|
28
|
+
on_setup
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
@@ -38,8 +43,10 @@ module RubySketch
|
|
38
43
|
end
|
39
44
|
|
40
45
|
def on_draw (e)
|
41
|
-
|
42
|
-
|
46
|
+
@canvas_painter.paint do |painter|
|
47
|
+
call_block @draw, e, painter
|
48
|
+
end
|
49
|
+
e.painter.image @canvas
|
43
50
|
end
|
44
51
|
|
45
52
|
def on_key (e)
|
@@ -70,11 +77,24 @@ module RubySketch
|
|
70
77
|
return if width * height == 0
|
71
78
|
return if width == @canvas&.width && height == @canvas&.height
|
72
79
|
|
73
|
-
|
74
|
-
|
75
|
-
|
80
|
+
old_canvas = @canvas
|
81
|
+
old_painter = @canvas_painter
|
82
|
+
|
83
|
+
cs = old_canvas&.color_space || Rays::ColorSpace::RGBA
|
84
|
+
@canvas = Rays::Image.new width, height, cs, painter.pixel_density
|
85
|
+
@canvas_painter = @canvas.painter
|
86
|
+
|
87
|
+
if old_canvas
|
88
|
+
@canvas_painter.paint {image old_canvas}
|
89
|
+
copy_painter_attributes old_painter, @canvas_painter
|
90
|
+
end
|
91
|
+
end
|
76
92
|
|
77
|
-
|
93
|
+
def copy_painter_attributes (from, to)
|
94
|
+
to.fill = from.fill
|
95
|
+
to.stroke = from.stroke
|
96
|
+
to.stroke_width = from.stroke_width
|
97
|
+
to.font = from.font
|
78
98
|
end
|
79
99
|
|
80
100
|
def call_block (block, event, *args)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysketch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xordog
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|
@@ -102,6 +102,7 @@ extensions:
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
|
+
- ".yardopts"
|
105
106
|
- LICENSE
|
106
107
|
- README.md
|
107
108
|
- Rakefile
|