Class: Gosu::Window
- Inherits:
-
Object
- Object
- Gosu::Window
- Defined in:
- lib/gosu_android/main-window.rb
Instance Attribute Summary collapse
-
#activity ⇒ Object
readonly
Returns the value of attribute activity.
-
#caption ⇒ Object
Returns the value of attribute caption.
-
#fonts_manager ⇒ Object
readonly
Returns the value of attribute fonts_manager.
-
#fullscreen ⇒ Object
readonly
Returns the value of attribute fullscreen.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#internal_update_interval ⇒ Object
readonly
Returns the value of attribute internal_update_interval.
-
#media_player ⇒ Object
Returns the value of attribute media_player.
-
#mouse_x ⇒ Object
Returns the value of attribute mouse_x.
-
#mouse_y ⇒ Object
Returns the value of attribute mouse_y.
-
#text_input ⇒ Object
Returns the value of attribute text_input.
-
#update_interval ⇒ Object
readonly
Returns the value of attribute update_interval.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.button_id_to_char(id) ⇒ Object
Returns the character a button usually produces, or nil.
-
.char_to_button_id(char) ⇒ Object
Returns the button that has to be pressed to produce the given character, or nil.
Instance Method Summary collapse
-
#add_key_event_listener ⇒ Object
TODO At least check if the methods were already defined, if so called them after/before this methods.
-
#button_down(id) ⇒ Object
Called before update when the user pressed a button while the window had the focus.
-
#button_down?(id) ⇒ Boolean
Returns true if a button is currently pressed.
-
#button_up(id) ⇒ Object
Same as buttonDown.
-
#clip_to(x, y, w, h, &rendering_code) ⇒ Object
Limits the drawing area to a given rectangle while evaluating the code inside of the block.
-
#close ⇒ Object
Tells the window to end the current show loop as soon as possible.
- #create_image(source, src_x, src_y, src_width, src_height, tileable) ⇒ Object
- #do_show ⇒ Object
- #do_tick ⇒ Object
-
#draw ⇒ Object
Called after every update and when the OS wants the window to repaint itself.
-
#draw_line(x1, y1, c1, x2, y2, c2, z = 0, mode = :default) ⇒ Object
Draws a line from one point to another (last pixel exclusive).
-
#draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z = 0, mode = :default) ⇒ Object
Draws a rectangle (two triangles) with given corners and corresponding colors.
- #draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z = 0, mode = :default) ⇒ Object
-
#flush ⇒ Object
Flushes all drawing operations to OpenGL so that Z-ordering can start anew.
-
#focus_changed(has_focus, width, height) ⇒ Object
TODO On screen rotation the app breaks down.
-
#gl(z = nil, &custom_gl_code) ⇒ Object
For custom OpenGL calls.
- #hide_soft_keyboard ⇒ Object
-
#initialize(width, height, fullscreen, update_interval = 16.666666) ⇒ Window
constructor
- update_interval
-
Interval in milliseconds between two calls to the update member function.
-
#needs_cursor? ⇒ Boolean
Can be overriden to show the system cursor when necessary, e.g.
-
#needs_redraw? ⇒ Boolean
Can be overriden to give the game a chance to say no to being redrawn.
-
#object_collided(x, y, object) ⇒ Object
Called when and object collides with another object.
-
#record(width, height, &rendering_code) ⇒ Gosu::Image
Returns a Gosu::Image that containes everything rendered within the given block.
-
#rotate(angle, around_x = 0, around_y = 0, &rendering_code) ⇒ Object
Rotates everything drawn in the block around (around_x, around_y).
-
#scale(factor_x, factor_y, around_x, around_y, &rendering_code) ⇒ Object
Scales everything drawn in the block by a factor for each dimension.
-
#set_mouse_position(x, y) ⇒ Object
deprecated
Deprecated.
Use Window#mouse_x= and Window#mouse_y= instead.
-
#show ⇒ Object
Enters a modal loop where the Window is visible on screen and receives calls to draw, update etc.
-
#show_soft_keyboard ⇒ Object
TODO It would be nice that the keyboard was transparent.
-
#touch_began(touch) ⇒ Object
Called when the user started a touch on the screen.
-
#touch_ended(touch) ⇒ Object
Called when the user finished a touch on the screen.
-
#touch_moved(touch) ⇒ Object
Called when the user continue a touch on the screen.
-
#transform(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, &rendering_code) ⇒ Object
Applies a free-form matrix rotation to everything drawn in the block.
-
#translate(x, y, &rendering_code) ⇒ Object
Moves everything drawn in the block by an offset in each dimension.
-
#update ⇒ Object
Called every update_interval milliseconds while the window is being shown.
Constructor Details
#initialize(width, height, fullscreen, update_interval = 16.666666) ⇒ Window
- update_interval
-
Interval in milliseconds between two calls
to the update member function. The default means the game will run at 60 FPS, which is ideal on standard 60 Hz TFT screens.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gosu_android/main-window.rb', line 75 def initialize(width, height, fullscreen, update_interval=16.666666) android_initializer = AndroidInitializer.instance @fullscreen = fullscreen @showing = false @activity = android_initializer.activity @display = @activity.getWindowManager.getDefaultDisplay @width = width @height= height @internal_update_interval = update_interval/1000.0 @update_interval = update_interval #@surface_view = GosuSurfaceView.new(@activity) @surface_view = android_initializer.surface_view @input = Input.new(@display, self) @surface_view.atributes(self, @input) #@graphics = Graphics.new(@width, @height, @fullscreen, self) @graphics = android_initializer.graphics @graphics.initialize_window(@width, @height, @fullscreen, self) #@surface_view.renderer = @graphics @surface_view.set_render_mode(JavaImports::GLSurfaceView::RENDERMODE_WHEN_DIRTY) @fonts_manager = FontsManager.new self @media_player = nil add_key_event_listener @activity.input = @input @showing_keyboard = false end |
Instance Attribute Details
#activity ⇒ Object (readonly)
Returns the value of attribute activity.
70 71 72 |
# File 'lib/gosu_android/main-window.rb', line 70 def activity @activity end |
#caption ⇒ Object
Returns the value of attribute caption.
60 61 62 |
# File 'lib/gosu_android/main-window.rb', line 60 def @caption end |
#fonts_manager ⇒ Object (readonly)
Returns the value of attribute fonts_manager.
69 70 71 |
# File 'lib/gosu_android/main-window.rb', line 69 def fonts_manager @fonts_manager end |
#fullscreen ⇒ Object (readonly)
Returns the value of attribute fullscreen.
66 67 68 |
# File 'lib/gosu_android/main-window.rb', line 66 def fullscreen @fullscreen end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
65 66 67 |
# File 'lib/gosu_android/main-window.rb', line 65 def height @height end |
#internal_update_interval ⇒ Object (readonly)
Returns the value of attribute internal_update_interval.
67 68 69 |
# File 'lib/gosu_android/main-window.rb', line 67 def internal_update_interval @internal_update_interval end |
#media_player ⇒ Object
Returns the value of attribute media_player.
64 65 66 |
# File 'lib/gosu_android/main-window.rb', line 64 def media_player @media_player end |
#mouse_x ⇒ Object
Returns the value of attribute mouse_x.
61 62 63 |
# File 'lib/gosu_android/main-window.rb', line 61 def mouse_x @mouse_x end |
#mouse_y ⇒ Object
Returns the value of attribute mouse_y.
62 63 64 |
# File 'lib/gosu_android/main-window.rb', line 62 def mouse_y @mouse_y end |
#text_input ⇒ Object
Returns the value of attribute text_input.
63 64 65 |
# File 'lib/gosu_android/main-window.rb', line 63 def text_input @text_input end |
#update_interval ⇒ Object (readonly)
Returns the value of attribute update_interval.
68 69 70 |
# File 'lib/gosu_android/main-window.rb', line 68 def update_interval @update_interval end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
65 66 67 |
# File 'lib/gosu_android/main-window.rb', line 65 def width @width end |
Class Method Details
.button_id_to_char(id) ⇒ Object
Returns the character a button usually produces, or nil. To implement real text-input facilities, look at the TextInput class instead.
299 |
# File 'lib/gosu_android/main-window.rb', line 299 def self.(id); end |
.char_to_button_id(char) ⇒ Object
Returns the button that has to be pressed to produce the given character, or nil.
302 |
# File 'lib/gosu_android/main-window.rb', line 302 def self.(char); end |
Instance Method Details
#add_key_event_listener ⇒ Object
TODO At least check if the methods were already defined, if so called them after/before this methods
107 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 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/gosu_android/main-window.rb', line 107 def add_key_event_listener # Get the class of the object. @activity.class.class_eval do attr_accessor :input def on_destroy super #Release audio resources Song.release_resources end def onKeyDown(keyCode, event) if @input.feed_key_event_down(keyCode) return true else return super keyCode, event end end def onKeyUp(keyCode, event) if @input.feed_key_event_up(keyCode) return true else return super keyCode, event end end #TODO It does never get called, it does not matter how long #the press was def onKeyLongPress(keyCode, event) if @input.feed_key_event_up(keyCode) return true else return super keyCode, event end end end end |
#button_down(id) ⇒ Object
Called before update when the user pressed a button while the window had the focus.
214 |
# File 'lib/gosu_android/main-window.rb', line 214 def (id); end |
#button_down?(id) ⇒ Boolean
Returns true if a button is currently pressed. Updated every tick.
219 220 221 |
# File 'lib/gosu_android/main-window.rb', line 219 def (id) return @input. id end |
#button_up(id) ⇒ Object
Same as buttonDown. Called then the user released a button.
216 |
# File 'lib/gosu_android/main-window.rb', line 216 def (id); end |
#clip_to(x, y, w, h, &rendering_code) ⇒ Object
Limits the drawing area to a given rectangle while evaluating the code inside of the block.
269 |
# File 'lib/gosu_android/main-window.rb', line 269 def clip_to(x, y, w, h, &rendering_code); end |
#close ⇒ Object
Tells the window to end the current show loop as soon as possible.
184 185 186 187 188 189 |
# File 'lib/gosu_android/main-window.rb', line 184 def close #Self trigger window lost focus, since we are going to home focus_changed false, @screen_width, @screen_height @showing = false @activity.moveTaskToBack(true) end |
#create_image(source, src_x, src_y, src_width, src_height, tileable) ⇒ Object
373 374 375 |
# File 'lib/gosu_android/main-window.rb', line 373 def create_image(source, src_x, src_y, src_width, src_height, tileable) @graphics.create_image(source, src_x, src_y, src_width, src_height, tileable) end |
#do_show ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/gosu_android/main-window.rb', line 173 def do_show @start_time = Time.now do_tick #TODO gosu dark side @end_time = Time.now if (@start_time <= @end_time and (@end_time - @start_time) < @internal_update_interval) sleep(@internal_update_interval - (@end_time - @start_time)) end end |
#do_tick ⇒ Object
317 318 319 320 321 322 323 324 325 |
# File 'lib/gosu_android/main-window.rb', line 317 def do_tick @input.update self.update @input.clear @graphics.begin(Color::BLACK) self.draw @graphics.end @surface_view.request_render end |
#draw ⇒ Object
Called after every update and when the OS wants the window to repaint itself. Your application’s rendering code goes here.
197 |
# File 'lib/gosu_android/main-window.rb', line 197 def draw; end |
#draw_line(x1, y1, c1, x2, y2, c2, z = 0, mode = :default) ⇒ Object
Draws a line from one point to another (last pixel exclusive). Note: OpenGL lines are not reliable at all and may have a missing pixel at the start or end point. Please only use this for debugging purposes. Otherwise, use a quad or image to simulate lines, or contribute a better draw_line to Gosu.
237 238 239 |
# File 'lib/gosu_android/main-window.rb', line 237 def draw_line(x1, y1, c1, x2, y2, c2, z=0, mode=:default) @graphics.draw_line(x1, y1, c1, x2, y2, c2, z, AM_MODES[mode]) end |
#draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z = 0, mode = :default) ⇒ Object
Draws a rectangle (two triangles) with given corners and corresponding colors. The points can be in clockwise order, or in a Z shape.
248 249 250 |
# File 'lib/gosu_android/main-window.rb', line 248 def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z=0, mode=:default) @graphics.draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, AM_MODES[mode]) end |
#draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z = 0, mode = :default) ⇒ Object
241 242 243 |
# File 'lib/gosu_android/main-window.rb', line 241 def draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z=0, mode=:default) @graphics.draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, AM_MODES[mode]) end |
#flush ⇒ Object
Flushes all drawing operations to OpenGL so that Z-ordering can start anew. This is useful when drawing several parts of code on top of each other that use conflicting z positions.
255 |
# File 'lib/gosu_android/main-window.rb', line 255 def flush; end |
#focus_changed(has_focus, width, height) ⇒ Object
TODO On screen rotation the app breaks down
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/gosu_android/main-window.rb', line 328 def focus_changed has_focus, width, height @screen_width = width @screen_height = height if has_focus if @showing and @media_player != nil @media_player.start end #TODO Keyboard does not appears again if @showing_keyboard show_soft_keyboard end else #Hide keyboard but mark it so it will be shown later if @showing_keyboard hide_soft_keyboard @showing_keyboard = true end if @showing and @media_player != nil @media_player.pause end end return true end |
#gl(z = nil, &custom_gl_code) ⇒ Object
For custom OpenGL calls. Executes the given block in a clean OpenGL environment. Use the ruby-opengl gem to access OpenGL function (if you manage to get it to work). IF no z position is given, it will execute the given block immediately, otherwise, the code will be scheduled to be called between Gosu drawing operations.
Note: You cannot call Gosu rendering functions within this block, and you can only call the gl function in the call tree of Window#draw.
See examples/OpenGLIntegration.rb for an example.
266 |
# File 'lib/gosu_android/main-window.rb', line 266 def gl(z=nil, &custom_gl_code); end |
#hide_soft_keyboard ⇒ Object
366 367 368 369 370 371 |
# File 'lib/gosu_android/main-window.rb', line 366 def hide_soft_keyboard context = @activity.getApplicationContext imm = context.getSystemService(Context::INPUT_METHOD_SERVICE) imm.toggleSoftInput(JavaImports::InputMethodManager::HIDE_IMPLICIT_ONLY,0) @showing_keyboard = false end |
#needs_cursor? ⇒ Boolean
Can be overriden to show the system cursor when necessary, e.g. in level editors or other situations where introducing a custom cursor is not desired.
210 |
# File 'lib/gosu_android/main-window.rb', line 210 def needs_cursor?; end |
#needs_redraw? ⇒ Boolean
Can be overriden to give the game a chance to say no to being redrawn. This is not a definitive answer. The operating system can still cause redraws for one reason or another.
By default, the window is redrawn all the time (i.e. Window#needs_redraw? always returns true.)
205 |
# File 'lib/gosu_android/main-window.rb', line 205 def needs_redraw?; end |
#object_collided(x, y, object) ⇒ Object
Called when and object collides with another object
231 |
# File 'lib/gosu_android/main-window.rb', line 231 def object_collided(x, y, object); end |
#record(width, height, &rendering_code) ⇒ Gosu::Image
Returns a Gosu::Image that containes everything rendered within the given block. It can be used to optimize rendering of many static images, e.g. the map. There are still several restrictions that you will be informed about via exceptions.
The returned Gosu::Image will have the width and height you pass as arguments, regardless of how the area you draw on. It is important to pass accurate values if you plan on using Gosu::Image#draw_as_quad or Gosu::Image#draw_rot with the result later.
280 |
# File 'lib/gosu_android/main-window.rb', line 280 def record(width, height, &rendering_code); end |
#rotate(angle, around_x = 0, around_y = 0, &rendering_code) ⇒ Object
Rotates everything drawn in the block around (around_x, around_y).
283 |
# File 'lib/gosu_android/main-window.rb', line 283 def rotate(angle, around_x=0, around_y=0, &rendering_code); end |
#scale(factor_x, factor_y, around_x, around_y, &rendering_code) ⇒ Object
Scales everything drawn in the block by a factor for each dimension.
286 |
# File 'lib/gosu_android/main-window.rb', line 286 def scale(factor_x, factor_y=factor_x, &rendering_code); end |
#set_mouse_position(x, y) ⇒ Object
Use Window#mouse_x= and Window#mouse_y= instead.
305 |
# File 'lib/gosu_android/main-window.rb', line 305 def set_mouse_position(x, y); end |
#show ⇒ Object
Enters a modal loop where the Window is visible on screen and receives calls to draw, update etc.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gosu_android/main-window.rb', line 149 def show @showing = true if @fullscreen #Use full java path for Window, since there is a Gosu::Window @activity.request_window_feature(JavaImports::Window::FEATURE_NO_TITLE) @activity.get_window.set_flags(JavaImports::WindowManager::LayoutParams::FLAG_FULLSCREEN, JavaImports::WindowManager::LayoutParams::FLAG_FULLSCREEN) #@activity.content_view = @surface_view @window = @activity.getWindow #Replace position and size with gosu metrics else @window = @activity.getWindow #Only the thread that created the view can change it, so setLayout #and setTitle cannot be executed here p = Proc.new do @window.setLayout(@width, @height) @activity.setTitle @caption end @activity.runOnUiThread(p) end @screen_width = @surface_view.get_width @screen_height = @surface_view.get_height end |
#show_soft_keyboard ⇒ Object
TODO It would be nice that the keyboard was transparent
359 360 361 362 363 364 |
# File 'lib/gosu_android/main-window.rb', line 359 def show_soft_keyboard context = @activity.getApplicationContext imm = context.getSystemService(Context::INPUT_METHOD_SERVICE) imm.toggleSoftInput(JavaImports::InputMethodManager::SHOW_FORCED,0) @showing_keyboard = true end |
#touch_began(touch) ⇒ Object
Called when the user started a touch on the screen
224 |
# File 'lib/gosu_android/main-window.rb', line 224 def touch_began(touch); end |
#touch_ended(touch) ⇒ Object
Called when the user finished a touch on the screen
228 |
# File 'lib/gosu_android/main-window.rb', line 228 def touch_ended(touch); end |
#touch_moved(touch) ⇒ Object
Called when the user continue a touch on the screen
226 |
# File 'lib/gosu_android/main-window.rb', line 226 def touch_moved(touch); end |
#transform(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, &rendering_code) ⇒ Object
Applies a free-form matrix rotation to everything drawn in the block.
295 |
# File 'lib/gosu_android/main-window.rb', line 295 def transform(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, &rendering_code); end |
#translate(x, y, &rendering_code) ⇒ Object
Moves everything drawn in the block by an offset in each dimension.
292 |
# File 'lib/gosu_android/main-window.rb', line 292 def translate(x, y, &rendering_code); end |
#update ⇒ Object
Called every update_interval milliseconds while the window is being shown. Your application’s main game logic goes here.
193 |
# File 'lib/gosu_android/main-window.rb', line 193 def update; end |