Class: Ogre::ApplicationFrameListener

Inherits:
FrameListenerProxy
  • Object
show all
Defined in:
lib/application_frame_listener.rb

Overview

Do take note of create_frame_listener in the example above. The FrameListener actually takes in the Root object, instead of the application calling root.addFrameListener. We do this because the process of using Ruby C++ subclasses in C++ functions doesn’t really work, and may not even be possible. The fix for this is to build a Proxy class in C++ that is given the Root on construction. It then registers itself with the passed in Root. See OgreProxys for this definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, render_window, camera, buffered_keys = false, buffered_mouse = false, buffered_joy = false) ⇒ ApplicationFrameListener

Returns a new instance of ApplicationFrameListener.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/application_frame_listener.rb', line 87

def initialize(root, render_window, camera, buffered_keys = false, 
							 buffered_mouse = false, buffered_joy = false)
	super(root)
	@camera = camera
	@window = render_window
	@move_speed = 100
	@rotate_speed = 36
	@move_scale = 0.0
	@rot_scale = 0.0
	@translate_vector = Vector3.ZERO
	@rot_x = Degree.new(0)
	@rot_y = Degree.new(0)
	@debug_text = ""

	@debug_overlay = OverlayManager.instance.get_by_name("Core/DebugOverlay")	

	LogManager.instance.log_message("*** Initializing OIS ***")

	windowHnd = @window.get_custom_attribute_unsigned_long("WINDOW")

	@input_manager = OIS::InputManager.create_input_system({"WINDOW" => "#{windowHnd}"})
	@keyboard = @input_manager.create_input_object( OIS::OISKeyboard, buffered_keys)
	@mouse = @input_manager.create_input_object( OIS::OISMouse, buffered_mouse)

	# Most likely won't have a joystick here, so just throw away any exceptions
	@joy_stick = nil
	begin
		@joy_stick = @input_manager.create_input_object( OIS::OISJoyStick, buffered_joy)
	rescue
	end

	show_debug_overlay(true)

	# Currently doesn't work, it's the Ruby subclass back into C++ problem
	#puts "Setting window listener"
	#@window_event_listener = ApplicationWindowListener.new(@input_manager, @window,
																												 #@keyboard, @mouse,
																												 #@joy_stick)

	# Done in proxy constructor
	# WindowEventUtilities.addWindowEventListener(@window, @window_event_listener)
end

Instance Attribute Details

#cameraObject

Returns the value of attribute camera.



85
86
87
# File 'lib/application_frame_listener.rb', line 85

def camera
  @camera
end

#input_managerObject

Returns the value of attribute input_manager.



85
86
87
# File 'lib/application_frame_listener.rb', line 85

def input_manager
  @input_manager
end

#joy_stickObject

Returns the value of attribute joy_stick.



85
86
87
# File 'lib/application_frame_listener.rb', line 85

def joy_stick
  @joy_stick
end

#keyboardObject

Returns the value of attribute keyboard.



85
86
87
# File 'lib/application_frame_listener.rb', line 85

def keyboard
  @keyboard
end

#mouseObject

Returns the value of attribute mouse.



85
86
87
# File 'lib/application_frame_listener.rb', line 85

def mouse
  @mouse
end

#windowObject

Returns the value of attribute window.



85
86
87
# File 'lib/application_frame_listener.rb', line 85

def window
  @window
end

Instance Method Details

#frame_ended(event) ⇒ Object



274
275
276
277
# File 'lib/application_frame_listener.rb', line 274

def frame_ended(event)
	update_stats
	true
end

#frame_started(event) ⇒ Object

Ogre callback, called at the beginning of every frame



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/application_frame_listener.rb', line 230

def frame_started(event)
	return false if @window.closed?

	# Need to capture input from each device
	@keyboard.capture
	@mouse.capture
	@joy_stick.capture if @joy_stick

	buffered_joy = @joy_stick.nil? ? true : @joy_stick.buffered

	if !@mouse.buffered || !@keyboard.buffered || !buffered_joy
		# If this is the first frame, pick a speed
		if event.time_since_last_frame == 0	
			@move_scale = 1
			@rot_scale = 0.1
		else
			# ~100 units / second
			@move_scale = @move_speed * event.time_since_last_frame
			# ~10 seconds for full rotation
			@rot_scale = @rotate_speed * event.time_since_last_frame
		end

		@rot_x = Degree.new 0
		@rot_y = Degree.new 0

		@translate_vector = Vector3.ZERO
	end

	if !@keyboard.buffered
		return false unless process_unbuffered_key_input(event)
	end

	if !@mouse.buffered
		return false unless process_unbuffered_mouse_input(event)
	end

	if !@mouse.buffered || !@keyboard.buffered || !buffered_joy
		move_camera
	end
	
	# Keep rendering!
	true
end

#move_cameraObject

Process the movement we’ve calculated this frame



212
213
214
215
216
# File 'lib/application_frame_listener.rb', line 212

def move_camera
	@camera.yaw(@rot_x)
	@camera.pitch(@rot_y)
	@camera.move_relative(@translate_vector)
end

#process_unbuffered_key_input(event) ⇒ Object

Keyboard processing



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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/application_frame_listener.rb', line 145

def process_unbuffered_key_input(event)
	# Start with basic movement, add overlay stuff later

	# Left
	if @keyboard.key_down?(OIS::KC_A)
		@translate_vector.x = -@move_scale
	end

	# Right
	if @keyboard.key_down?(OIS::KC_D)
		@translate_vector.x = @move_scale
	end

	# Forward
	if @keyboard.key_down?(OIS::KC_UP) || @keyboard.key_down?(OIS::KC_W)
		@translate_vector.z = -@move_scale
	end

	# Back
	if @keyboard.key_down?(OIS::KC_DOWN) || @keyboard.key_down?(OIS::KC_S)
		@translate_vector.z = @move_scale
	end

	# Up
	if @keyboard.key_down?(OIS::KC_PGUP)
		@translate_vector.y = @move_scale
	end

	# Down
	if @keyboard.key_down?(OIS::KC_PGDOWN)
		@translate_vector.y = -@move_scale
	end

	# Turn Right
	if @keyboard.key_down?(OIS::KC_RIGHT)
		@camera.yaw(-@rot_scale)
	end

	# Turn Left
	if @keyboard.key_down?(OIS::KC_LEFT)
		@camera.yaw(@rot_scale)
	end

	# Quit
	if @keyboard.key_down?(OIS::KC_ESCAPE) || @keyboard.key_down?(OIS::KC_Q)
		return false;
	end

	# Keep rendering
	true
end

#process_unbuffered_mouse_input(event) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/application_frame_listener.rb', line 197

def process_unbuffered_mouse_input(event)
	state = @mouse.mouse_state

	if state.button_down?(OIS::MB_Right)
		@translate_vector.x += state.X.rel * 0.13
		@translate_vector.y -= state.Y.rel * 0.13
	else
		@rot_x = Degree.new(-state.X.rel * 0.13)
		@rot_y = Degree.new(-state.Y.rel * 0.13)
	end

	true
end

#show_debug_overlay(show) ⇒ Object

Toggle debug overlay if one exists



219
220
221
222
223
224
225
226
227
# File 'lib/application_frame_listener.rb', line 219

def show_debug_overlay(show)
	if @debug_overlay
		if show
			@debug_overlay.show
		else
			@debug_overlay.hide
		end
	end
end

#shutdownObject

Ruby doesn’t have destructors, just manually call shutdown to clean this up



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/application_frame_listener.rb', line 131

def shutdown
	#WindowEventUtilities::removeWindowEventListener(@window, @window_event_listener)
	#@window_event_listener.windowClosed(@window)
	
	# Clean up our input objects
	@input_manager.destroy_input_object(@mouse)
	@input_manager.destroy_input_object(@keyboard)
	@input_manager.destroy_input_object(@joy_stick)

	OIS::InputManager.destroy_input_system(@input_manager)
	@input_manager = nil
end