Skip to content

Commit 4a4527b

Browse files
committed
refinement
1 parent 14e2ccc commit 4a4527b

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
require 'forwardable'
3+
# placeholder for vector coordinates
4+
Vect = Struct.new(:x, :y)
5+
6+
# A class to contain frame corners, uses forwardable
7+
class Corners
8+
include Enumerable
9+
extend Forwardable
10+
def_delegators :@corners, :each_with_index, :[], :[]=, :<<
11+
attr_reader :idx
12+
13+
def initialize(width, height, ws, wh)
14+
@corners = [
15+
Vect.new(width / 2 - ws, height / 2 - wh),
16+
Vect.new(width / 2 + ws, height / 2 - wh),
17+
Vect.new(width / 2 + ws, height / 2 + wh),
18+
Vect.new(width / 2 - ws, height / 2 + wh)
19+
]
20+
@idx = -1
21+
end
22+
23+
def set_corner(mx, my)
24+
self[idx] = Vect.new(mx, my)
25+
end
26+
27+
def selected?
28+
idx != -1
29+
end
30+
31+
def set_index(sel)
32+
@idx = sel
33+
end
34+
end

processing_app/library/glvideo/video_mapping.rb

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
1-
load_library :glvideo
1+
load_libraries :glvideo, :corners
22
include_package 'gohai.glvideo'
33

4-
# placeholder for vector coordinates
5-
Vect = Struct.new(:x, :y)
64
# placeholder for image sources
75
ImageSource = Struct.new(:video, :image)
86

9-
attr_reader :sources, :sel, :video, :corners, :quads, :last_mouse_move, :sel_corner
7+
attr_reader :sources, :sel, :video, :corners, :quads, :last_mouse_move
108
RES = 5 # number of subdivisions (e.g. 5 x 5)
119

1210
def setup
1311
@last_mouse_move = 0
14-
@sel_corner = -1
1512
no_cursor
1613
@video = GLMovie.new(self, data_path('launch2.mp4'))
1714
video.loop
1815
image = load_image(data_path('checkerboard.png'))
1916
@sources = ImageSource.new(video, image)
2017
@sel = sources.video
21-
@corners = [
22-
Vect.new(width / 2 - 100, height / 2 - 100),
23-
Vect.new(width / 2 + 100, height / 2 - 100),
24-
Vect.new(width / 2 + 100, height / 2 + 100),
25-
Vect.new(width / 2 - 100, height / 2 + 100)
26-
]
18+
@corners = Corners.new(width, height, sources.image.width / 2, sources.image.height / 2)
2719
@quads = create_mesh(sel, corners, RES)
2820
end
2921

3022
def draw
3123
background(0)
32-
video.read if (sel.respond_to?(:read) && video.available)
24+
video.read if sel.respond_to?(:read) && video.available
3325
# regenerate mesh if we're dragging a corner
34-
if (sel_corner != -1 && (pmouse_x != mouse_x || pmouse_y != mouse_y))
35-
corners[sel_corner] = Vect.new(mouse_x, mouse_y)
26+
if corners.selected? && (pmouse_x != mouse_x || pmouse_y != mouse_y)
27+
corners.set_corner(mouse_x, mouse_y)
3628
# this improves performance, but will be replaced by a
3729
# more elegant way in a future release
3830
@quads = []
@@ -41,28 +33,28 @@ def draw
4133
end
4234

4335
# display
44-
quads.map { |quad| shape(quad) } if !quads.empty?
36+
quads.map { |quad| shape(quad) } unless quads.empty?
4537
# hide the mouse cursor after two seconds
46-
if (pmouse_x != mouse_x || pmouse_y != mouse_y)
38+
if pmouse_x != mouse_x || pmouse_y != mouse_y
4739
cursor
4840
@last_mouse_move = millis
49-
elsif (!last_mouse_move.zero? && 2000 < millis - last_mouse_move)
41+
elsif !last_mouse_move.zero? && 2000 < millis - last_mouse_move
5042
no_cursor
5143
@last_mouse_move = 0
5244
end
5345
end
5446

5547
def mouse_pressed
5648
corners.each_with_index do |corner, i|
57-
return @sel_corner = i if dist(mouse_x, mouse_y, corner.x, corner.y) < 20
49+
return corners.set_index(i) if dist(mouse_x, mouse_y, corner.x, corner.y) < 20
5850
end
5951
# no corner? then switch texture
6052
@sel = sel.respond_to?(:loop) ? sources.image : sources.video
61-
quads = create_mesh(sel, corners, RES)
53+
@quads = create_mesh(sel, corners, RES)
6254
end
6355

6456
def mouse_released
65-
@sel_corner = -1
57+
corners.set_index(-1)
6658
end
6759

6860
def create_mesh(tex, corners, res)
@@ -86,9 +78,9 @@ def create_mesh(tex, corners, res)
8678
sh.normal(0, 0, 1)
8779
point = warp_perspective.map_dest_point(x * x_step, y * y_step)
8880
sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, x.to_f / res, y.to_f / res)
89-
point = warp_perspective.map_dest_point((x+1) * x_step, y * y_step)
81+
point = warp_perspective.map_dest_point((x + 1) * x_step, y * y_step)
9082
sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, (x + 1).to_f / res, y.to_f / res)
91-
point = warp_perspective.map_dest_point((x+1) * x_step, (y + 1) * y_step)
83+
point = warp_perspective.map_dest_point((x + 1) * x_step, (y + 1) * y_step)
9284
sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, (x + 1).to_f / res, (y + 1).to_f / res)
9385
point = warp_perspective.map_dest_point(x * x_step, (y + 1) * y_step)
9486
sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, x.to_f / res, (y + 1).to_f / res)

0 commit comments

Comments
 (0)