rubysketch 0.3.22 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/{release.yml → release-gem.yml} +21 -21
  3. data/.github/workflows/tag.yml +35 -0
  4. data/.github/workflows/test.yml +8 -9
  5. data/.github/workflows/utils.rb +55 -0
  6. data/ChangeLog.md +7 -164
  7. data/LICENSE +1 -1
  8. data/README.md +1 -1
  9. data/Rakefile +10 -34
  10. data/RubySketch.podspec +4 -3
  11. data/VERSION +1 -1
  12. data/examples/hello.rb +6 -6
  13. data/lib/rubysketch/all.rb +9 -0
  14. data/lib/rubysketch/context.rb +47 -0
  15. data/lib/rubysketch/{module.rb → extension.rb} +3 -3
  16. data/lib/rubysketch/graphics_context.rb +37 -0
  17. data/lib/rubysketch/helper.rb +5 -0
  18. data/lib/rubysketch/sprite.rb +101 -0
  19. data/lib/rubysketch.rb +30 -12
  20. data/rubysketch.gemspec +15 -8
  21. data/src/{RubySketch.h → RubyProcessing.h} +0 -4
  22. data/src/RubyProcessing.mm +25 -0
  23. data/test/helper.rb +3 -10
  24. data/test/test_sprite.rb +23 -0
  25. metadata +119 -35
  26. data/examples/breakout.rb +0 -212
  27. data/examples/camera.rb +0 -14
  28. data/examples/clock.rb +0 -57
  29. data/examples/delay_camera.rb +0 -33
  30. data/examples/glsl.rb +0 -14
  31. data/examples/image.rb +0 -13
  32. data/examples/shapes.rb +0 -121
  33. data/lib/rubysketch/app.rb +0 -13
  34. data/lib/rubysketch/glsl.rb +0 -44
  35. data/lib/rubysketch/processing.rb +0 -3191
  36. data/lib/rubysketch/window.rb +0 -232
  37. data/lib/rubysketch-glsl.rb +0 -12
  38. data/lib/rubysketch-processing.rb +0 -25
  39. data/src/RubySketch.mm +0 -55
  40. data/test/processing/helper.rb +0 -11
  41. data/test/processing/test_graphics.rb +0 -21
  42. data/test/processing/test_shader.rb +0 -47
  43. data/test/processing/test_utility.rb +0 -41
  44. data/test/processing/test_vector.rb +0 -394
@@ -1,232 +0,0 @@
1
- module RubySketch
2
-
3
-
4
- class Window < Reflex::Window
5
-
6
- class CanvasView < Reflex::View
7
- def on_update(e)
8
- window.on_canvas_update e
9
- end
10
-
11
- def on_draw(e)
12
- window.on_canvas_draw e
13
- end
14
-
15
- def on_pointer(e)
16
- window.on_canvas_pointer e
17
- end
18
-
19
- def on_resize(e)
20
- window.on_canvas_resize e
21
- end
22
- end
23
-
24
- attr_accessor :setup, :update, :draw, :resize,
25
- :key_down, :key_up,
26
- :pointer_down, :pointer_up, :pointer_move, :pointer_drag,
27
- :motion,
28
- :before_draw, :after_draw, :update_canvas
29
-
30
- attr_accessor :auto_resize
31
-
32
- def initialize(width = 500, height = 500, *args, **kwargs, &block)
33
- RubySketch.instance_variable_set :@window, self
34
-
35
- @events = []
36
- @error = nil
37
- @auto_resize = true
38
- @canvas = Canvas.new self
39
- @canvas_view = add CanvasView.new name: :canvas
40
-
41
- super(*args, size: [width, height], **kwargs, &block)
42
- end
43
-
44
- def canvas_image()
45
- @canvas.image
46
- end
47
-
48
- def canvas_painter()
49
- @canvas.painter
50
- end
51
-
52
- def window_painter()
53
- self.painter
54
- end
55
-
56
- def event()
57
- @events.last
58
- end
59
-
60
- def start(&block)
61
- draw_canvas do
62
- block.call if block
63
- on_setup
64
- end
65
- end
66
-
67
- def resize_canvas(width, height, pixel_density = nil)
68
- if @canvas.resize width, height, pixel_density
69
- @update_canvas.call canvas_image, canvas_painter if @update_canvas
70
- size width, height
71
- end
72
- end
73
-
74
- def on_setup()
75
- call_block @setup, nil
76
- end
77
-
78
- def on_resize(e)
79
- on_canvas_resize e
80
- end
81
-
82
- def on_draw(e)
83
- window_painter.pixel_density.tap do |pd|
84
- resize_canvas width, height, pd if pd != canvas_painter.pixel_density
85
- end
86
- update_canvas_view
87
- end
88
-
89
- def on_key(e)
90
- block = case e.action
91
- when :down then @key_down
92
- when :up then @key_up
93
- end
94
- draw_canvas {call_block block, e} if block
95
- end
96
-
97
- def on_motion(e)
98
- draw_canvas {call_block @motion, e} if @motion
99
- end
100
-
101
- def on_canvas_update(e)
102
- call_block @update, e
103
- @canvas_view.redraw
104
- end
105
-
106
- def on_canvas_draw(e)
107
- draw_canvas {call_block @draw, e} if @draw
108
- draw_screen e.painter
109
- end
110
-
111
- def on_canvas_pointer(e)
112
- block = case e.action
113
- when :down then @pointer_down
114
- when :up, :cancel then @pointer_up
115
- when :move then e.drag? ? @pointer_drag : @pointer_move
116
- end
117
- draw_canvas {call_block block, e} if block
118
- end
119
-
120
- def on_canvas_resize(e)
121
- resize_canvas e.width, e.height, window_painter.pixel_density if @auto_resize
122
- draw_canvas {call_block @resize, e} if @resize
123
- end
124
-
125
- private
126
-
127
- def update_canvas_view()
128
- scrollx, scrolly, zoom = get_scroll_and_zoom
129
- @canvas_view.scroll_to scrollx, scrolly
130
- @canvas_view.zoom zoom
131
- end
132
-
133
- def get_scroll_and_zoom()
134
- ww, wh = width.to_f, height.to_f
135
- cw, ch = canvas_image.width.to_f, canvas_image.height.to_f
136
- return [0, 0, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
137
-
138
- wratio, cratio = ww / wh, cw / ch
139
- if wratio >= cratio
140
- scaled_w = wh * cratio
141
- return (ww - scaled_w) / 2, 0, scaled_w / cw
142
- else
143
- scaled_h = ww / cratio
144
- return 0, (wh - scaled_h) / 2, ww / cw
145
- end
146
- end
147
-
148
- def draw_canvas(&block)
149
- begin_draw
150
- block.call
151
- ensure
152
- end_draw
153
- end
154
-
155
- def begin_draw()
156
- canvas_painter.__send__ :begin_paint
157
- @before_draw&.call
158
- end
159
-
160
- def end_draw()
161
- @after_draw&.call
162
- canvas_painter.__send__ :end_paint
163
- end
164
-
165
- def draw_screen(painter)
166
- window_painter.image canvas_image
167
- end
168
-
169
- def call_block(block, event, *args)
170
- @events.push event
171
- block.call event, *args if block && !@error
172
- rescue Exception => e
173
- @error = e
174
- $stderr.puts e.full_message
175
- ensure
176
- @events.pop
177
- end
178
-
179
- end# Window
180
-
181
-
182
- class Window::Canvas
183
-
184
- attr_reader :image, :painter
185
-
186
- def initialize(window)
187
- @image = nil
188
- @painter = window.painter
189
-
190
- resize 1, 1
191
- painter.miter_limit = 10
192
- end
193
-
194
- def resize(width, height, pixel_density = nil)
195
- return false if width * height == 0
196
-
197
- return false if
198
- width == @image&.width &&
199
- height == @image&.height &&
200
- pixel_density == @painter.pixel_density
201
-
202
- old_image = @image
203
- old_painter = @painter
204
- cs = old_image&.color_space || Rays::RGBA
205
- pd = pixel_density || old_painter.pixel_density
206
-
207
- @image = Rays::Image.new width, height, cs, pd
208
- @painter = @image.painter
209
-
210
- @painter.paint {image old_image} if old_image
211
- copy_painter old_painter, @painter
212
-
213
- GC.start
214
- return true
215
- end
216
-
217
- private
218
-
219
- def copy_painter(from, to)
220
- to.fill = from.fill
221
- to.stroke = from.stroke
222
- to.stroke_width = from.stroke_width
223
- to.stroke_cap = from.stroke_cap
224
- to.stroke_join = from.stroke_join
225
- to.miter_limit = from.miter_limit
226
- to.font = from.font
227
- end
228
-
229
- end# Window::Canvas
230
-
231
-
232
- end# RubySketch
@@ -1,12 +0,0 @@
1
- require 'rubysketch'
2
-
3
-
4
- def (RubySketch::GLSL).run(
5
- shader_source,
6
- title: 'RubySketch',
7
- width: 500, height: 500)
8
- w = RubySketch::Window.new(title: title, w: width, h: height) {start}
9
- c = RubySketch::GLSL::Context.new w
10
- c.run shader_source
11
- at_exit {RubySketch::App.new {w.show}.start unless $!}
12
- end
@@ -1,25 +0,0 @@
1
- require 'rubysketch'
2
-
3
-
4
- begin
5
- window = RubySketch::Window.new {start}
6
- context = RubySketch::Processing::Context.new window
7
-
8
- (context.methods - Object.instance_methods).each do |method|
9
- define_method method do |*args, **kwargs, &block|
10
- context.__send__ method, *args, **kwargs, &block
11
- end
12
- end
13
-
14
- context.class.constants.each do |const|
15
- self.class.const_set const, context.class.const_get(const)
16
- end
17
-
18
- window.__send__ :begin_draw
19
- at_exit do
20
- window.__send__ :end_draw
21
- RubySketch::App.new {window.show}.start unless $!
22
- end
23
-
24
- RUBYSKETCH_WINDOW = window
25
- end
data/src/RubySketch.mm DELETED
@@ -1,55 +0,0 @@
1
- #import <CRuby.h>
2
- #import "RubySketch.h"
3
- #include "../src/ios/view_controller.h"
4
-
5
-
6
- static ReflexViewController* active_reflex_view_controller = nil;
7
-
8
- static ReflexViewController*
9
- ReflexViewController_create()
10
- {
11
- return active_reflex_view_controller;
12
- }
13
-
14
- static void
15
- ReflexViewController_show (UIViewController*, ReflexViewController*)
16
- {
17
- }
18
-
19
-
20
- @implementation RubySketch
21
-
22
- + (void) setup
23
- {
24
- static BOOL done = NO;
25
- if (done) return;
26
- done = YES;
27
-
28
- [CRuby addLibrary:@"RubySketch" bundle:[NSBundle bundleForClass:RubySketch.class]];
29
-
30
- ReflexViewController_set_create_fun(ReflexViewController_create);
31
- ReflexViewController_set_show_fun(ReflexViewController_show);
32
- }
33
-
34
- + (void) start: (NSString*) path
35
- {
36
- [CRuby evaluate:[NSString stringWithFormat:@
37
- "raise 'already started' unless require 'rubysketch-processing'\n"
38
- "load '%@'\n"
39
- "RUBYSKETCH_WINDOW.__send__ :end_draw\n"
40
- "RUBYSKETCH_WINDOW.show",
41
- path
42
- ]];
43
- }
44
-
45
- + (void) setActiveReflexViewController: (id) reflexViewController
46
- {
47
- active_reflex_view_controller = reflexViewController;
48
- }
49
-
50
- + (void) resetActiveReflexViewController
51
- {
52
- active_reflex_view_controller = nil;
53
- }
54
-
55
- @end
@@ -1,11 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- require_relative '../helper'
5
-
6
-
7
- def graphics(width = 10, height = 10, &block)
8
- RubySketch::Processing::Graphics.new(width, height).tap do |g|
9
- g.beginDraw {block.call g, g.getInternal__} if block
10
- end
11
- end
@@ -1,21 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- require_relative 'helper'
5
-
6
-
7
- class TestProcessingGraphics < Test::Unit::TestCase
8
-
9
- P = RubySketch::Processing
10
-
11
- def graphics(w = 10, h = 10)
12
- P::Graphics.new w, h
13
- end
14
-
15
- def test_beginDraw()
16
- g = graphics
17
- g.beginDraw
18
- assert_raise {g.beginDraw}
19
- end
20
-
21
- end# TestProcessingGraphics
@@ -1,47 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- require_relative 'helper'
5
-
6
-
7
- class TestProcessingShader < Test::Unit::TestCase
8
-
9
- def color(*args)
10
- Rays::Color.new(*args)
11
- end
12
-
13
- def shader(vs = vshader, fs = fshader)
14
- RubySketch::Processing::Shader.new vs, fs
15
- end
16
-
17
- def vshader()
18
- "void main() {gl_Position = vec4(0.0, 0.0, 0.0, 1.0);}"
19
- end
20
-
21
- def fshader()
22
- "void main() {gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
23
- end
24
-
25
- def test_initialize()
26
- assert shader vshader, fshader
27
- assert shader nil, fshader
28
-
29
- assert_raise(ArgumentError) {shader "", fshader}
30
- assert_raise(ArgumentError) {shader vshader, ""}
31
- assert_raise(ArgumentError) {shader vshader, nil}
32
- end
33
-
34
- def test_uniform()
35
- sh = shader(nil, <<~END).tap {|s| s.set :val, 1.0}
36
- uniform float val;
37
- void main() {gl_FragColor = vec4(val, 0.0, 0.0, 1.0);}
38
- END
39
-
40
- graphics do |g, image|
41
- g.shader sh
42
- g.rect 0, 0, 10, 10
43
- assert_equal color(1, 0, 0, 1), image[0, 0]
44
- end
45
- end
46
-
47
- end# TestProcessingShader
@@ -1,41 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- require_relative 'helper'
5
-
6
-
7
- class TestProcessingUtility < Test::Unit::TestCase
8
-
9
- P = RubySketch::Processing
10
-
11
- include P::GraphicsContext
12
-
13
- def test_random()
14
- assert_equal Float, random(1).class
15
- assert_equal Float, random(1.0).class
16
- assert_equal Symbol, random((:a..:z).to_a).class
17
-
18
- assert_not_equal random, random
19
-
20
- 10000.times do
21
- n = random
22
- assert 0 <= n && n < 1
23
-
24
- n = random 1
25
- assert 0 <= n && n < 1
26
-
27
- n = random 1, 2
28
- assert 1.0 <= n && n < 2.0
29
- end
30
- end
31
-
32
- def test_createVector()
33
- assert_equal P::Vector, createVector(1, 2).class
34
- end
35
-
36
- def test_createShader()
37
- fs = "void main() {gl_FragColor = vec4(1.0);}"
38
- assert_equal P::Shader, createShader(nil, fs).class
39
- end
40
-
41
- end# TestProcessingUtility