picrate 0.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +47 -0
- data/.mvn/extensions.xml +9 -0
- data/.mvn/wrapper/maven-wrapper.properties +1 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE.md +165 -0
- data/README.md +51 -0
- data/Rakefile +59 -0
- data/bin/picrate +8 -0
- data/docs/.gitignore +6 -0
- data/docs/_config.yml +30 -0
- data/docs/_includes/footer.html +38 -0
- data/docs/_includes/head.html +16 -0
- data/docs/_includes/header.html +27 -0
- data/docs/_includes/icon-github.html +1 -0
- data/docs/_includes/icon-github.svg +1 -0
- data/docs/_includes/icon-twitter.html +1 -0
- data/docs/_includes/icon-twitter.svg +1 -0
- data/docs/_includes/navigation.html +24 -0
- data/docs/_layouts/default.html +20 -0
- data/docs/_layouts/page.html +14 -0
- data/docs/_layouts/post.html +15 -0
- data/docs/_posts/2018-05-06-getting_started.md +8 -0
- data/docs/_posts/2018-05-06-install_jruby.md +35 -0
- data/docs/_sass/_base.scss +206 -0
- data/docs/_sass/_layout.scss +242 -0
- data/docs/_sass/_syntax-highlighting.scss +71 -0
- data/docs/about.md +10 -0
- data/docs/css/main.scss +38 -0
- data/docs/favicon.ico +0 -0
- data/docs/feed.xml +30 -0
- data/docs/index.html +38 -0
- data/lib/picrate.rb +10 -0
- data/lib/picrate/app.rb +187 -0
- data/lib/picrate/creators/sketch_class.rb +57 -0
- data/lib/picrate/creators/sketch_factory.rb +12 -0
- data/lib/picrate/creators/sketch_writer.rb +21 -0
- data/lib/picrate/helper_methods.rb +214 -0
- data/lib/picrate/helpers/numeric.rb +9 -0
- data/lib/picrate/library.rb +69 -0
- data/lib/picrate/library_loader.rb +53 -0
- data/lib/picrate/native_folder.rb +35 -0
- data/lib/picrate/native_loader.rb +27 -0
- data/lib/picrate/runner.rb +81 -0
- data/lib/picrate/version.rb +4 -0
- data/library/boids/boids.rb +209 -0
- data/library/chooser/chooser.rb +19 -0
- data/library/control_panel/control_panel.rb +182 -0
- data/library/library_proxy/README.md +99 -0
- data/library/library_proxy/library_proxy.rb +14 -0
- data/library/slider/slider.rb +42 -0
- data/library/vector_utils/vector_utils.rb +69 -0
- data/library/video_event/video_event.rb +3 -0
- data/license.txt +508 -0
- data/picrate.gemspec +35 -0
- data/pom.rb +122 -0
- data/pom.xml +214 -0
- data/src/main/java/japplemenubar/JAppleMenuBar.java +88 -0
- data/src/main/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
- data/src/main/java/monkstone/ColorUtil.java +115 -0
- data/src/main/java/monkstone/MathToolModule.java +236 -0
- data/src/main/java/monkstone/PicrateLibrary.java +47 -0
- data/src/main/java/monkstone/core/LibraryProxy.java +127 -0
- data/src/main/java/monkstone/fastmath/Deglut.java +122 -0
- data/src/main/java/monkstone/fastmath/package-info.java +6 -0
- data/src/main/java/monkstone/filechooser/Chooser.java +48 -0
- data/src/main/java/monkstone/noise/SimplexNoise.java +465 -0
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +168 -0
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +182 -0
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +149 -0
- data/src/main/java/monkstone/slider/SimpleSlider.java +196 -0
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +163 -0
- data/src/main/java/monkstone/slider/Slider.java +67 -0
- data/src/main/java/monkstone/slider/SliderBar.java +277 -0
- data/src/main/java/monkstone/slider/SliderGroup.java +78 -0
- data/src/main/java/monkstone/slider/WheelHandler.java +35 -0
- data/src/main/java/monkstone/vecmath/AppRender.java +87 -0
- data/src/main/java/monkstone/vecmath/JRender.java +56 -0
- data/src/main/java/monkstone/vecmath/ShapeRender.java +87 -0
- data/src/main/java/monkstone/vecmath/package-info.java +20 -0
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +757 -0
- data/src/main/java/monkstone/vecmath/vec2/package-info.java +6 -0
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +727 -0
- data/src/main/java/monkstone/vecmath/vec3/package-info.java +6 -0
- data/src/main/java/monkstone/videoevent/VideoInterface.java +42 -0
- data/src/main/java/monkstone/videoevent/package-info.java +20 -0
- data/src/main/java/processing/awt/PGraphicsJava2D.java +3098 -0
- data/src/main/java/processing/awt/PShapeJava2D.java +401 -0
- data/src/main/java/processing/awt/PSurfaceAWT.java +1660 -0
- data/src/main/java/processing/core/PApplet.java +17647 -0
- data/src/main/java/processing/core/PConstants.java +1033 -0
- data/src/main/java/processing/core/PFont.java +1250 -0
- data/src/main/java/processing/core/PGraphics.java +9614 -0
- data/src/main/java/processing/core/PImage.java +3608 -0
- data/src/main/java/processing/core/PMatrix.java +347 -0
- data/src/main/java/processing/core/PMatrix2D.java +694 -0
- data/src/main/java/processing/core/PMatrix3D.java +1153 -0
- data/src/main/java/processing/core/PShape.java +4332 -0
- data/src/main/java/processing/core/PShapeOBJ.java +544 -0
- data/src/main/java/processing/core/PShapeSVG.java +1987 -0
- data/src/main/java/processing/core/PStyle.java +208 -0
- data/src/main/java/processing/core/PSurface.java +242 -0
- data/src/main/java/processing/core/PSurfaceNone.java +479 -0
- data/src/main/java/processing/core/PVector.java +1140 -0
- data/src/main/java/processing/data/FloatDict.java +829 -0
- data/src/main/java/processing/data/FloatList.java +912 -0
- data/src/main/java/processing/data/IntDict.java +796 -0
- data/src/main/java/processing/data/IntList.java +913 -0
- data/src/main/java/processing/data/JSONArray.java +1260 -0
- data/src/main/java/processing/data/JSONObject.java +2282 -0
- data/src/main/java/processing/data/JSONTokener.java +435 -0
- data/src/main/java/processing/data/Sort.java +46 -0
- data/src/main/java/processing/data/StringDict.java +601 -0
- data/src/main/java/processing/data/StringList.java +775 -0
- data/src/main/java/processing/data/Table.java +4923 -0
- data/src/main/java/processing/data/TableRow.java +198 -0
- data/src/main/java/processing/data/XML.java +1149 -0
- data/src/main/java/processing/event/Event.java +108 -0
- data/src/main/java/processing/event/KeyEvent.java +70 -0
- data/src/main/java/processing/event/MouseEvent.java +149 -0
- data/src/main/java/processing/event/TouchEvent.java +57 -0
- data/src/main/java/processing/javafx/PGraphicsFX2D.java +354 -0
- data/src/main/java/processing/opengl/FontTexture.java +379 -0
- data/src/main/java/processing/opengl/FrameBuffer.java +503 -0
- data/src/main/java/processing/opengl/LinePath.java +623 -0
- data/src/main/java/processing/opengl/LineStroker.java +685 -0
- data/src/main/java/processing/opengl/PGL.java +3366 -0
- data/src/main/java/processing/opengl/PGraphics2D.java +615 -0
- data/src/main/java/processing/opengl/PGraphics3D.java +281 -0
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13634 -0
- data/src/main/java/processing/opengl/PJOGL.java +1966 -0
- data/src/main/java/processing/opengl/PShader.java +1478 -0
- data/src/main/java/processing/opengl/PShapeOpenGL.java +5234 -0
- data/src/main/java/processing/opengl/PSurfaceJOGL.java +1315 -0
- data/src/main/java/processing/opengl/Texture.java +1670 -0
- data/src/main/java/processing/opengl/VertexBuffer.java +88 -0
- data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
- data/src/main/java/processing/opengl/cursors/cross.png +0 -0
- data/src/main/java/processing/opengl/cursors/hand.png +0 -0
- data/src/main/java/processing/opengl/cursors/license.txt +27 -0
- data/src/main/java/processing/opengl/cursors/move.png +0 -0
- data/src/main/java/processing/opengl/cursors/text.png +0 -0
- data/src/main/java/processing/opengl/cursors/wait.png +0 -0
- data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/ColorVert.glsl +34 -0
- data/src/main/java/processing/opengl/shaders/LightFrag.glsl +33 -0
- data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +154 -0
- data/src/main/java/processing/opengl/shaders/LightVert.glsl +151 -0
- data/src/main/java/processing/opengl/shaders/LineFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/LineVert.glsl +100 -0
- data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
- data/src/main/java/processing/opengl/shaders/PointFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/PointVert.glsl +56 -0
- data/src/main/java/processing/opengl/shaders/TexFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +160 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
- data/src/main/java/processing/opengl/shaders/TexVert.glsl +38 -0
- data/src/main/resources/icon/icon-1024.png +0 -0
- data/src/main/resources/icon/icon-128.png +0 -0
- data/src/main/resources/icon/icon-16.png +0 -0
- data/src/main/resources/icon/icon-256.png +0 -0
- data/src/main/resources/icon/icon-32.png +0 -0
- data/src/main/resources/icon/icon-48.png +0 -0
- data/src/main/resources/icon/icon-512.png +0 -0
- data/src/main/resources/icon/icon-64.png +0 -0
- data/src/main/resources/license.txt +508 -0
- data/test/create_test.rb +68 -0
- data/test/deglut_spec_test.rb +24 -0
- data/test/helper_methods_test.rb +58 -0
- data/test/math_tool_test.rb +75 -0
- data/test/respond_to_test.rb +215 -0
- data/test/sketches/key_event.rb +37 -0
- data/test/sketches/library/my_library/my_library.rb +32 -0
- data/test/test_helper.rb +3 -0
- data/test/vecmath_spec_test.rb +522 -0
- data/vendors/Rakefile +127 -0
- metadata +289 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative 'sketch_writer'
|
2
|
+
|
3
|
+
class SketchFactory
|
4
|
+
NAMES = %w[One Two Three]
|
5
|
+
def initialize(argc)
|
6
|
+
NAMES.each do |name|
|
7
|
+
SketchWriter.new(File.basename(name, '.rb'), width: 300, height: 300).write
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
SketchFactory.new(File.join(ENV['HOME'], 'test'))
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'sketch_class'
|
3
|
+
|
4
|
+
# The file writer can write a sketch when given instance of Sketch type
|
5
|
+
class SketchWriter
|
6
|
+
attr_reader :file, :sketch
|
7
|
+
|
8
|
+
def initialize(path, args)
|
9
|
+
@sketch = SketchClass.new(
|
10
|
+
name: path,
|
11
|
+
width: args[0].to_i,
|
12
|
+
height: args[1].to_i,
|
13
|
+
mode: args[2]
|
14
|
+
)
|
15
|
+
@file = format('%s/%s.rb', File.dirname(path), path)
|
16
|
+
end
|
17
|
+
|
18
|
+
def write
|
19
|
+
File.open(file, 'w+') { |f| f.write sketch.lines.join("\n") }
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
# processing module wrapper
|
3
|
+
require_relative 'helpers/numeric'
|
4
|
+
module Processing
|
5
|
+
# Provides some convenience methods
|
6
|
+
module HelperMethods
|
7
|
+
# processings epsilon may not be defined yet
|
8
|
+
EPSILON ||= 9.999999747378752e-05
|
9
|
+
# Nice block method to draw to a buffer.
|
10
|
+
# You can optionally pass it a width, a height, and a renderer.
|
11
|
+
# Takes care of starting and ending the draw for you.
|
12
|
+
def buffer(buf_width = width, buf_height = height, renderer = @render_mode)
|
13
|
+
buf = create_graphics(buf_width, buf_height, renderer)
|
14
|
+
buf.begin_draw
|
15
|
+
yield buf
|
16
|
+
buf.end_draw
|
17
|
+
buf
|
18
|
+
end
|
19
|
+
|
20
|
+
def kamera(
|
21
|
+
eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)),
|
22
|
+
center: Vec3D.new(width / 2.0, height / 2.0, 0),
|
23
|
+
up: Vec3D.new(0, 1.0, 0)
|
24
|
+
)
|
25
|
+
camera(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z)
|
26
|
+
end
|
27
|
+
|
28
|
+
def perspektiv(
|
29
|
+
fov: Math::PI / 3.0,
|
30
|
+
aspect_ratio: width.to_f / height,
|
31
|
+
near_z: (height / 20.0) / tan(fov / 2.0),
|
32
|
+
far_z: (height * 5) / tan(fov / 2.0)
|
33
|
+
)
|
34
|
+
perspective(fov, aspect_ratio, near_z, far_z)
|
35
|
+
end
|
36
|
+
|
37
|
+
# A nice method to run a given block for a grid.
|
38
|
+
# Lifted from action_coding/Nodebox.
|
39
|
+
# def grid(cols, rows, col_size = 1, row_size = 1) { |x, y| block_stuff }
|
40
|
+
# NB: now implemented in java
|
41
|
+
|
42
|
+
# lerp_color takes three or four arguments, in Java that's two
|
43
|
+
# different methods, one regular and one static, so:
|
44
|
+
def lerp_color(*args)
|
45
|
+
args.length > 3 ? self.class.lerp_color(*args) : super(*args)
|
46
|
+
end
|
47
|
+
|
48
|
+
# hue, sat, brightness in range 0..1.0 returns RGB color int
|
49
|
+
def hsb_color(hue, sat, brightness)
|
50
|
+
Java::Monkstone::ColorUtil.hsbToRgB(hue, sat, brightness)
|
51
|
+
end
|
52
|
+
|
53
|
+
def color(*args)
|
54
|
+
return super(*args) unless args.length == 1
|
55
|
+
super(hex_color(args[0]))
|
56
|
+
end
|
57
|
+
|
58
|
+
def web_to_color_array(web)
|
59
|
+
Java::Monkstone::ColorUtil.webArray(web)
|
60
|
+
end
|
61
|
+
|
62
|
+
def int_to_ruby_colors(hex)
|
63
|
+
Java::Monkstone::ColorUtil.rubyString(hex)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Overrides Processing convenience function thread, which takes a String
|
67
|
+
# arg (for a function) to more rubylike version, takes a block...
|
68
|
+
def thread(&block)
|
69
|
+
if block_given?
|
70
|
+
Thread.new(&block)
|
71
|
+
else
|
72
|
+
raise ArgumentError, 'thread must be called with a block', caller
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# explicitly provide 'processing.org' min instance method
|
77
|
+
# to return a float:- a, b and c need to be floats
|
78
|
+
# you might choose to use ruby method directly and then
|
79
|
+
# provide a block to alter comparator eg
|
80
|
+
# args.min(&block) # { |a, b| a.value <=> b.value }
|
81
|
+
|
82
|
+
def min(*args)
|
83
|
+
args.min
|
84
|
+
end
|
85
|
+
|
86
|
+
# explicitly provide 'processing.org' max instance method
|
87
|
+
# to return a float:- a, b and c need to be floats see above
|
88
|
+
|
89
|
+
def max(*args)
|
90
|
+
args.max
|
91
|
+
end
|
92
|
+
|
93
|
+
# explicitly provide 'processing.org' dist instance method
|
94
|
+
def dist(*args)
|
95
|
+
case args.length
|
96
|
+
when 4
|
97
|
+
return dist2d(*args)
|
98
|
+
when 6
|
99
|
+
return dist3d(*args)
|
100
|
+
else
|
101
|
+
raise ArgumentError, 'takes 4 or 6 parameters'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Uses PImage class method under hood
|
106
|
+
def blend_color(c1, c2, mode)
|
107
|
+
Java::ProcessingCore::PImage.blendColor(c1, c2, mode)
|
108
|
+
end
|
109
|
+
|
110
|
+
# There's just so many functions in Processing,
|
111
|
+
# Here's a convenient way to look for them.
|
112
|
+
def find_method(method_name)
|
113
|
+
reg = Regexp.new(method_name.to_s, true)
|
114
|
+
methods.sort.select { |meth| reg.match(meth) }
|
115
|
+
end
|
116
|
+
|
117
|
+
# Proxy over a list of Java declared fields that have the same name as
|
118
|
+
# some methods. Add to this list as needed.
|
119
|
+
def proxy_java_fields
|
120
|
+
fields = %w(key frameRate mousePressed keyPressed)
|
121
|
+
methods = fields.map { |field| java_class.declared_field(field) }
|
122
|
+
@declared_fields = Hash[fields.zip(methods)]
|
123
|
+
end
|
124
|
+
|
125
|
+
# Fix java conversion problems getting the last key
|
126
|
+
# If it's ASCII, return the character, otherwise the integer
|
127
|
+
def key
|
128
|
+
int = @declared_fields['key'].value(java_self)
|
129
|
+
int < 256 ? int.chr : int
|
130
|
+
end
|
131
|
+
|
132
|
+
# Provide a convenient handle for the Java-space version of self.
|
133
|
+
def java_self
|
134
|
+
@java_self ||= to_java(Java::ProcessingCore::PApplet)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Fields that should be made accessible as under_scored.
|
138
|
+
define_method(:mouse_x) { mouseX }
|
139
|
+
|
140
|
+
define_method(:mouse_y) { mouseY }
|
141
|
+
|
142
|
+
define_method(:pmouse_x) { pmouseX }
|
143
|
+
|
144
|
+
define_method(:pmouse_y) { pmouseY }
|
145
|
+
|
146
|
+
define_method(:frame_count) { frameCount }
|
147
|
+
|
148
|
+
define_method(:mouse_button) { mouseButton }
|
149
|
+
|
150
|
+
define_method(:key_code) { keyCode }
|
151
|
+
|
152
|
+
# Ensure that load_strings returns a real Ruby array
|
153
|
+
def load_strings(file_or_url)
|
154
|
+
loadStrings(file_or_url).to_a
|
155
|
+
end
|
156
|
+
|
157
|
+
# Writes an array of strings to a file, one line per string.
|
158
|
+
# This file is saved to the sketch's data folder
|
159
|
+
def save_strings(filename, strings)
|
160
|
+
saveStrings(filename, [strings].flatten.to_java(:String))
|
161
|
+
end
|
162
|
+
|
163
|
+
# frame_rate needs to support reading and writing
|
164
|
+
def frame_rate(fps = nil)
|
165
|
+
return @declared_fields['frameRate'].value(java_self) unless fps
|
166
|
+
super(fps)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Is the mouse pressed for this frame?
|
170
|
+
def mouse_pressed?
|
171
|
+
@declared_fields['mousePressed'].value(java_self)
|
172
|
+
end
|
173
|
+
|
174
|
+
# Is a key pressed for this frame?
|
175
|
+
def key_pressed?
|
176
|
+
@declared_fields['keyPressed'].value(java_self)
|
177
|
+
end
|
178
|
+
|
179
|
+
private
|
180
|
+
|
181
|
+
INTEGER_COL = -> (x) { x.is_a?(Integer) }
|
182
|
+
STRING_COL = -> (x) { x.is_a?(String) }
|
183
|
+
FLOAT_COL = -> (x) { x.is_a?(Float) }
|
184
|
+
# parse single argument color int/double/String
|
185
|
+
def hex_color(a)
|
186
|
+
case a
|
187
|
+
when INTEGER_COL
|
188
|
+
Java::Monkstone::ColorUtil.colorLong(a)
|
189
|
+
when STRING_COL
|
190
|
+
return Java::Monkstone::ColorUtil.colorString(a) if a =~ /#\h+/
|
191
|
+
raise StandardError, 'Dodgy Hexstring'
|
192
|
+
when FLOAT_COL
|
193
|
+
Java::Monkstone::ColorUtil.colorDouble(a)
|
194
|
+
else
|
195
|
+
raise StandardError, 'Dodgy Color Conversion'
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def dist2d(*args)
|
200
|
+
dx = args[0] - args[2]
|
201
|
+
dy = args[1] - args[3]
|
202
|
+
return 0 if dx.abs < EPSILON && dy.abs < EPSILON
|
203
|
+
Math.hypot(dx, dy)
|
204
|
+
end
|
205
|
+
|
206
|
+
def dist3d(*args)
|
207
|
+
dx = args[0] - args[3]
|
208
|
+
dy = args[1] - args[4]
|
209
|
+
dz = args[2] - args[5]
|
210
|
+
return 0 if dx.abs < EPSILON && dy.abs < EPSILON && dz.abs < EPSILON
|
211
|
+
Math.sqrt(dx * dx + dy * dy + dz * dz)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative 'native_folder'
|
2
|
+
require_relative 'native_loader'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
# This class knows where to find picrate libraries
|
6
|
+
class Library
|
7
|
+
attr_reader :name, :path, :dir, :ppath
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
@ruby = true
|
12
|
+
end
|
13
|
+
|
14
|
+
def locate
|
15
|
+
return if (@path = Pathname.new(
|
16
|
+
File.join(Processing::SKETCH_ROOT, 'library', name, "#{name}.rb")
|
17
|
+
)).exist?
|
18
|
+
return if (@path = Pathname.new(
|
19
|
+
File.join(PICRATE_ROOT, 'library', name, "#{name}.rb")
|
20
|
+
)).exist?
|
21
|
+
locate_java
|
22
|
+
end
|
23
|
+
|
24
|
+
def locate_java
|
25
|
+
@dir = Pathname.new(
|
26
|
+
File.join(Processing::SKETCH_ROOT, 'library', name)
|
27
|
+
)
|
28
|
+
locate_installed_java
|
29
|
+
end
|
30
|
+
|
31
|
+
def locate_installed_java
|
32
|
+
unless dir.directory?
|
33
|
+
@dir = Pathname.new(
|
34
|
+
File.join(ENV['HOME'], '.picrate', 'libraries', name, 'library')
|
35
|
+
)
|
36
|
+
end
|
37
|
+
@path = dir.join(Pathname.new("#{name}.jar"))
|
38
|
+
end
|
39
|
+
|
40
|
+
def ruby?
|
41
|
+
path.extname == '.rb'
|
42
|
+
end
|
43
|
+
|
44
|
+
def exist?
|
45
|
+
path.exist?
|
46
|
+
end
|
47
|
+
|
48
|
+
def load_jars
|
49
|
+
Dir.glob("#{dir}/*.jar").each do |jar|
|
50
|
+
require jar
|
51
|
+
end
|
52
|
+
return true unless native_binaries?
|
53
|
+
add_binaries_to_classpath
|
54
|
+
end
|
55
|
+
|
56
|
+
def native_binaries?
|
57
|
+
native_folder = NativeFolder.new
|
58
|
+
native = native_folder.name
|
59
|
+
@ppath = File.join(dir, native)
|
60
|
+
File.directory?(ppath) &&
|
61
|
+
!Dir.glob(File.join(ppath, native_folder.extension)).empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
def add_binaries_to_classpath
|
65
|
+
native_loader = NativeLoader.new
|
66
|
+
native_loader.add_native_path(ppath)
|
67
|
+
true
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
3
|
+
# The processing wrapper module
|
4
|
+
module Processing
|
5
|
+
require_relative 'library'
|
6
|
+
|
7
|
+
# Encapsulate library loader functionality as a class
|
8
|
+
class LibraryLoader
|
9
|
+
attr_reader :library
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@loaded_libraries = Hash.new(false)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Detect if a library has been loaded (for conditional loading)
|
16
|
+
def library_loaded?(library_name)
|
17
|
+
@loaded_libraries[library_name.to_sym]
|
18
|
+
end
|
19
|
+
|
20
|
+
# Load a list of Ruby or Java libraries (in that order)
|
21
|
+
# Usage: load_libraries :video, :video_event
|
22
|
+
#
|
23
|
+
# If a library is put into a 'library' folder next to the sketch it will
|
24
|
+
# be used instead of an installed picrate library.
|
25
|
+
def load_libraries(*args)
|
26
|
+
message = 'no such file to load -- %s'
|
27
|
+
args.each do |lib|
|
28
|
+
loaded = loader(lib)
|
29
|
+
raise(LoadError.new, format(message, lib)) unless loaded
|
30
|
+
end
|
31
|
+
end
|
32
|
+
alias load_library load_libraries
|
33
|
+
|
34
|
+
def loader(name)
|
35
|
+
return true if @loaded_libraries.include?(name)
|
36
|
+
fname = name.to_s
|
37
|
+
library = Library.new(fname)
|
38
|
+
library.locate
|
39
|
+
return require_library(library, name) if library.ruby?
|
40
|
+
warn("Not found library: #{fname}") unless library.exist?
|
41
|
+
load_jars(library, name)
|
42
|
+
end
|
43
|
+
|
44
|
+
def load_jars(lib, name)
|
45
|
+
lib.load_jars
|
46
|
+
@loaded_libraries[name] = true
|
47
|
+
end
|
48
|
+
|
49
|
+
def require_library(lib, name)
|
50
|
+
@loaded_libraries[name] = require lib.path
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
|
3
|
+
# Utility to load native binaries on Java CLASSPATH
|
4
|
+
class NativeFolder
|
5
|
+
attr_reader :os, :bit
|
6
|
+
|
7
|
+
WIN_FORMAT = 'windows%d'.freeze
|
8
|
+
LINUX_FORMAT = 'linux%d'.freeze
|
9
|
+
ARM32 = '-armv6hf'.freeze
|
10
|
+
ARM64 = '-aarch64'.freeze
|
11
|
+
# WIN_PATTERNS = [
|
12
|
+
# /bccwin/i,
|
13
|
+
# /cygwin/i,
|
14
|
+
# /djgpp/i,
|
15
|
+
# /ming/i,
|
16
|
+
# /mswin/i,
|
17
|
+
# /wince/i
|
18
|
+
# ].freeze
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@os = RbConfig::CONFIG['host_os'].downcase
|
22
|
+
@bit = java.lang.System.get_property('os.arch') =~ /64/ ? 64 : 32
|
23
|
+
end
|
24
|
+
|
25
|
+
def name
|
26
|
+
return 'macosx' if os =~ /darwin/ || os =~ /mac/
|
27
|
+
return format(LINUX_FORMAT, ARM32) if os =~ /^arm/
|
28
|
+
return format(LINUX_FORMAT, bit) if os =~ /linux/
|
29
|
+
end
|
30
|
+
|
31
|
+
def extension
|
32
|
+
return '*.so' if os =~ /linux/
|
33
|
+
'*.dylib' # MacOS
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# This class knows how to dynamically set the 'java' native library path
|
2
|
+
# It might not work with java 9?
|
3
|
+
class NativeLoader
|
4
|
+
attr_reader :separator, :current_path
|
5
|
+
|
6
|
+
# This module wraps java_import with namespace JC
|
7
|
+
module JC
|
8
|
+
java_import 'java.lang.Class'
|
9
|
+
java_import 'java.lang.System'
|
10
|
+
java_import 'java.io.File'
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@separator = JC::File.pathSeparatorChar
|
15
|
+
@current_path = JC::System.getProperty('java.library.path')
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_native_path(pth)
|
19
|
+
current_path << separator << pth
|
20
|
+
JC::System.setProperty('java.library.path', current_path)
|
21
|
+
field = JC::Class.for_name('java.lang.ClassLoader')
|
22
|
+
.get_declared_field('sys_paths')
|
23
|
+
return unless field
|
24
|
+
field.accessible = true # some jruby magic
|
25
|
+
field.set(JC::Class.for_name('java.lang.System').get_class_loader, nil)
|
26
|
+
end
|
27
|
+
end
|