Class: GlimR::Viewport
Overview
Viewport is a scene graph root node and takes care of drawing its subtree and mapping mouse events to its child nodes.
Instance Attribute Summary collapse
Attributes inherited from SceneObject
#children, #mtime, #parent
#event_listeners, #listener_count
Instance Method Summary
collapse
-
#absolute_geometry ⇒ Object
-
#absolute_material ⇒ Object
-
#absolute_shader ⇒ Object
-
#absolute_texture ⇒ Object
-
#absolute_transform ⇒ Object
The absolute state of a viewport is empty starter state.
-
#absolute_transform_for_drawing ⇒ Object
Behave like a square when going through drawing list.
-
#apply ⇒ Object
Applies viewport to OpenGL state, scissor to viewport, enable blending and apply camera.
-
#apply_blend ⇒ Object
-
#apply_camera ⇒ Object
-
#apply_clip ⇒ Object
-
#aspect ⇒ Object
Width per height aspect of the viewport.
-
#changed? ⇒ Boolean
-
#clear_viewport ⇒ Object
-
#click_handler(o, e) ⇒ Object
-
#collides?(d, ex, ey) ⇒ Boolean
Tests if the coordinates ex and ey fall within the rectangle of drawable d.
-
#default_config ⇒ Object
-
#draw_drawables ⇒ Object
Draws the viewport drawables using painter’s algorithm.
-
#drawables ⇒ Object
Viewport presents only itself to its parent.
-
#drawables_under_cursor(e) ⇒ Object
-
#event_root ⇒ Object
Acts as root node for dispatched events.
-
#initialize(*a, &b) ⇒ Viewport
constructor
A new instance of Viewport.
-
#max_x ⇒ Object
-
#max_y ⇒ Object
-
#min_x ⇒ Object
-
#min_y ⇒ Object
-
#mouse_down_handler(o, e) ⇒ Object
-
#mouse_move_handler(o, e) ⇒ Object
Tracks mouse pointer position and sends hover, mouse_out, and mouse_over -events to nodes interacting with the mouse pointer.
-
#mouse_up_handler(o, e) ⇒ Object
-
#render ⇒ Object
Draws the viewport drawables using painter’s algorithm.
-
#resize_handler(o, e) ⇒ Object
-
#viewport ⇒ Object
The viewport of a Viewport is the Viewport itself.
-
#viewport_height ⇒ Object
-
#viewport_width ⇒ Object
-
#viewport_x ⇒ Object
-
#viewport_y ⇒ Object
Methods inherited from SceneObject
#<<, #add_drawables, #attach, #clone, #detach, #detach_self, #inspect, #pop_state, #push_state, #remove_drawables, #replace_node, #root, #touch!, #visible
#add_event_listener, #decrement_listener_count, #dispatch_event, #increment_listener_count, #method_missing, #multicast_event, #process_event, #remove_event_listener
Constructor Details
#initialize(*a, &b) ⇒ Viewport
Returns a new instance of Viewport.
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/glimr/renderer/viewport.rb', line 30
def initialize(*a,&b)
@down_x = 0
@down_y = 0
@old_collisions = []
@mouse_down_collisions = []
@drag_targets = {}
super
add_event_listener(:mouse_move, :mouse_move_handler, true)
add_event_listener(:click, :click_handler, true)
add_event_listener(:mouse_down, :mouse_down_handler, true)
add_event_listener(:mouse_up, :mouse_up_handler, true)
add_event_listener(:resize, :resize_handler, true)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class GlimR::EventListener
Instance Attribute Details
Returns the value of attribute camera.
16
17
18
|
# File 'lib/glimr/renderer/viewport.rb', line 16
def camera
@camera
end
|
Returns the value of attribute height.
16
17
18
|
# File 'lib/glimr/renderer/viewport.rb', line 16
def height
@height
end
|
Returns the value of attribute width.
16
17
18
|
# File 'lib/glimr/renderer/viewport.rb', line 16
def width
@width
end
|
Returns the value of attribute x.
16
17
18
|
# File 'lib/glimr/renderer/viewport.rb', line 16
def x
@x
end
|
Returns the value of attribute y.
16
17
18
|
# File 'lib/glimr/renderer/viewport.rb', line 16
def y
@y
end
|
Instance Method Details
#absolute_geometry ⇒ Object
252
253
254
|
# File 'lib/glimr/renderer/viewport.rb', line 252
def absolute_geometry
Geometry.new
end
|
#absolute_material ⇒ Object
244
245
246
|
# File 'lib/glimr/renderer/viewport.rb', line 244
def absolute_material
Material.new
end
|
#absolute_shader ⇒ Object
256
257
258
|
# File 'lib/glimr/renderer/viewport.rb', line 256
def absolute_shader
Shader.new
end
|
#absolute_texture ⇒ Object
248
249
250
|
# File 'lib/glimr/renderer/viewport.rb', line 248
def absolute_texture
Texture.new
end
|
The absolute state of a viewport is empty starter state. This to provide the SceneObject absolute state methods a starting point.
235
236
237
|
# File 'lib/glimr/renderer/viewport.rb', line 235
def absolute_transform
camera.absolute_transform
end
|
Behave like a square when going through drawing list.
240
241
242
|
# File 'lib/glimr/renderer/viewport.rb', line 240
def absolute_transform_for_drawing
parent.absolute_transform
end
|
Applies viewport to OpenGL state, scissor to viewport, enable blending and apply camera.
266
267
268
269
270
271
272
273
|
# File 'lib/glimr/renderer/viewport.rb', line 266
def apply
@last_frame_time = @mtime
apply_clip
clear_viewport
apply_camera
apply_blend
draw_drawables
end
|
#apply_blend ⇒ Object
289
290
291
292
|
# File 'lib/glimr/renderer/viewport.rb', line 289
def apply_blend
Enable GL::BLEND
BlendFunc(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA)
end
|
#apply_camera ⇒ Object
294
295
296
297
298
299
|
# File 'lib/glimr/renderer/viewport.rb', line 294
def apply_camera
if camera
camera.viewport = self
camera.apply
end
end
|
#apply_clip ⇒ Object
275
276
277
278
279
|
# File 'lib/glimr/renderer/viewport.rb', line 275
def apply_clip
Viewport(viewport_x, viewport_y, viewport_width, viewport_height)
Enable SCISSOR_TEST
Scissor(viewport_x, viewport_y, viewport_width, viewport_height)
end
|
Width per height aspect of the viewport.
334
335
336
|
# File 'lib/glimr/renderer/viewport.rb', line 334
def aspect
width.to_f / height
end
|
#changed? ⇒ Boolean
260
261
262
|
# File 'lib/glimr/renderer/viewport.rb', line 260
def changed?
@last_frame_time != @mtime
end
|
#clear_viewport ⇒ Object
281
282
283
284
285
286
287
|
# File 'lib/glimr/renderer/viewport.rb', line 281
def clear_viewport
if background
GL.ClearColor(*background)
GL.Clear(GL::COLOR_BUFFER_BIT)
GL.Clear(GL::DEPTH_BUFFER_BIT) if clear_depth_buffer
end
end
|
#click_handler(o, e) ⇒ Object
197
198
199
|
# File 'lib/glimr/renderer/viewport.rb', line 197
def click_handler(o,e)
e.phase = :bubble
end
|
#collides?(d, ex, ey) ⇒ Boolean
Tests if the coordinates ex and ey fall within the rectangle of drawable d.
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/glimr/renderer/viewport.rb', line 210
def collides?( d, ex, ey )
MatrixMode MODELVIEW
PushMatrix()
LoadIdentity()
MultMatrix(d.absolute_transform_for_drawing)
Translate(-ex,-ey,0)
camatrix = GetDoublev(MODELVIEW_MATRIX)
PopMatrix()
if (camatrix[3][0]+d.min_x+d.max_x >= d.min_x and camatrix[3][0]+d.min_x+d.max_x <= d.max_x and
camatrix[3][1]+d.min_y+d.max_y >= d.min_y and camatrix[3][1]+d.min_y+d.max_y <= d.max_y)
[-camatrix[3][0], -camatrix[3][1], d]
else
nil
end
end
|
#default_config ⇒ Object
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/glimr/renderer/viewport.rb', line 19
def default_config
super.merge(
:click_time => 0.3,
:click_radius => 4,
:background => nil,
:clear_depth_buffer => false,
:camera => Camera.new,
:x => 0, :y => 0, :width => 1, :height => 1
)
end
|
#draw_drawables ⇒ Object
Draws the viewport drawables using painter’s algorithm.
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# File 'lib/glimr/renderer/viewport.rb', line 302
def draw_drawables
collapsed = (@drawables).find_all{|d| d.visible }
collapsed = collapsed.sort_by{|d|
d.absolute_transform.flatten[14]
}
collapsed.each{|d|
PushAttrib( TEXTURE_BIT | LIGHTING_BIT )
LoadMatrix(d.absolute_transform_for_drawing)
d.absolute_texture.apply
d.absolute_material.apply
d.push_state
d.apply
d.pop_state
PopAttrib()
if d.is_a? Viewport
apply_clip
apply_camera
end
}
end
|
#drawables ⇒ Object
Viewport presents only itself to its parent.
329
330
331
|
# File 'lib/glimr/renderer/viewport.rb', line 329
def drawables
Set.new + [self]
end
|
#drawables_under_cursor(e) ⇒ Object
201
202
203
204
205
206
207
|
# File 'lib/glimr/renderer/viewport.rb', line 201
def drawables_under_cursor(e)
collapsed = (@drawables - [self])
collisions = collapsed.map{|d|
collides?( d, e.x, e.y ) if d.visible
}.compact
collisions.sort_by{|c| -c[2].absolute_transform.flatten[14] }
end
|
#event_root ⇒ Object
Acts as root node for dispatched events
45
46
47
|
# File 'lib/glimr/renderer/viewport.rb', line 45
def event_root
true
end
|
53
54
55
|
# File 'lib/glimr/renderer/viewport.rb', line 53
def max_x
viewport_width
end
|
61
62
63
|
# File 'lib/glimr/renderer/viewport.rb', line 61
def max_y
viewport_height
end
|
49
50
51
|
# File 'lib/glimr/renderer/viewport.rb', line 49
def min_x
@x
end
|
57
58
59
|
# File 'lib/glimr/renderer/viewport.rb', line 57
def min_y
@y
end
|
#mouse_down_handler(o, e) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/glimr/renderer/viewport.rb', line 137
def mouse_down_handler(o,e)
params = e.params.clone
params[:x] += x
params[:y] += y
@down_x = params[:x]
@down_y = params[:y]
@click_valid = true
@down_time = Time.now.to_f
collisions = drawables_under_cursor(e)
@mouse_down_collisions = collisions.map{|rx,ry,c| c }
collisions.each{|rx, ry, c|
evt = Event.new(:mouse_down,
e.params.merge(:sender => self, :target => c, :rx => rx, :ry => ry))
c.dispatch_event(evt)
}
if collisions.first
@drag_targets[e.button] = collisions.first[2]
else
@drag_targets.delete(e.button)
end
e.phase = :bubble
end
|
#mouse_move_handler(o, e) ⇒ Object
Tracks mouse pointer position and sends hover, mouse_out, and mouse_over -events to nodes interacting with the mouse pointer.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/glimr/renderer/viewport.rb', line 108
def mouse_move_handler(o,e)
params = e.params.clone
params[:x] += x
params[:y] += y
if Vector[@down_x - params[:x], @down_y - params[:y]].r > @click_radius
@click_valid = false
end
duc = drawables_under_cursor(e)
collisions = duc.map{|rx,ry,c| c }
(@old_collisions - collisions).each{|oc|
evt = Event.new(:mouse_out,
params.merge(:sender => self, :target => oc))
oc.dispatch_event(evt)
}
(collisions - @old_collisions).each{|oc|
evt = Event.new(:mouse_over,
params.merge(:sender => self, :target => oc))
oc.dispatch_event(evt)
}
@old_collisions = collisions
children.each{|c|
c.multicast_event(Event.new(:mouse_move, params))
}
@drag_targets.each{|k,t|
t.dispatch_event(Event.new(:drag, params.merge(:button => k)))
}
e.phase = :bubble
end
|
#mouse_up_handler(o, e) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/glimr/renderer/viewport.rb', line 160
def mouse_up_handler(o,e)
params = e.params.clone
params[:x] += x
params[:y] += y
if Vector[@down_x - params[:x], @down_y - params[:y]].r > @click_radius or (Time.now.to_f - @down_time) > @click_time
@click_valid = false
end
children.each{|c|
c.multicast_event(Event.new(:mouse_up, params))
}
if @click_valid or @drag_targets[e.button]
collisions = drawables_under_cursor(e)
if @click_valid
collisions.each{|rx,ry,c|
if @mouse_down_collisions.include? c
evt = Event.new(:click, params.merge(:sender => self, :rx => rx, :ry => ry))
c.dispatch_event(evt)
end
break
}
end
if @drag_targets[e.button]
if collisions.first
@drag_targets[e.button].dispatch_event(
Event.new(
:drop,
params.merge(:drop_target => collisions.first[2], :rx => collisions.first[0], :ry => collisions.first[1])
)
)
end
@drag_targets.delete(e.button)
end
end
@mouse_down_collisions.clear
e.phase = :bubble
end
|
Draws the viewport drawables using painter’s algorithm.
227
228
229
|
# File 'lib/glimr/renderer/viewport.rb', line 227
def render
apply
end
|
#resize_handler(o, e) ⇒ Object
98
99
100
101
102
103
|
# File 'lib/glimr/renderer/viewport.rb', line 98
def resize_handler(o,e)
if !parent
self.width = e.width
self.height = e.height
end
end
|
The viewport of a Viewport is the Viewport itself.
324
325
326
|
# File 'lib/glimr/renderer/viewport.rb', line 324
def viewport
self
end
|
#viewport_height ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/glimr/renderer/viewport.rb', line 90
def viewport_height
if parent
@height * parent.absolute_transform.flatten[5]
else
@height
end
end
|
#viewport_width ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/glimr/renderer/viewport.rb', line 82
def viewport_width
if parent
@width * parent.absolute_transform.flatten[0]
else
@width
end
end
|
#viewport_x ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/glimr/renderer/viewport.rb', line 65
def viewport_x
if parent
abs_x = @x + parent.absolute_transform.flatten[12]
else
@x
end
end
|
#viewport_y ⇒ Object
73
74
75
76
77
78
79
80
|
# File 'lib/glimr/renderer/viewport.rb', line 73
def viewport_y
if parent
abs_y = @y + parent.absolute_transform.flatten[13]
parent.viewport.height - abs_y - @height
else
-@y
end
end
|