diff options
| author | Tom Black <[email protected]> | 2018-09-20 20:57:55 -0700 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2018-09-20 20:57:55 -0700 |
| commit | 35a1c94eeef017413d0a20e68082441d2cd13264 (patch) | |
| tree | 93603028741c922eefe3c36862b9a590db1d1d80 /lib | |
| parent | 5d386b3cf58dfeb1d9c02621814944c0e0648afc (diff) | |
| download | ruby2d-35a1c94eeef017413d0a20e68082441d2cd13264.tar.gz ruby2d-35a1c94eeef017413d0a20e68082441d2cd13264.zip | |
Expose window attributes through class methods
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d.rb | 1 | ||||
| -rw-r--r-- | lib/ruby2d/application.rb | 47 | ||||
| -rw-r--r-- | lib/ruby2d/dsl.rb | 19 | ||||
| -rw-r--r-- | lib/ruby2d/renderable.rb | 4 | ||||
| -rw-r--r-- | lib/ruby2d/window.rb | 151 |
5 files changed, 147 insertions, 75 deletions
diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb index 02aba95..823512f 100644 --- a/lib/ruby2d.rb +++ b/lib/ruby2d.rb @@ -4,7 +4,6 @@ require 'ruby2d/renderable' require 'ruby2d/exceptions' require 'ruby2d/color' require 'ruby2d/window' -require 'ruby2d/application' require 'ruby2d/dsl' require 'ruby2d/quad' require 'ruby2d/line' diff --git a/lib/ruby2d/application.rb b/lib/ruby2d/application.rb deleted file mode 100644 index d04af70..0000000 --- a/lib/ruby2d/application.rb +++ /dev/null @@ -1,47 +0,0 @@ -# application.rb - -module Ruby2D::Application - class << self - @@window = Ruby2D::Window.new - - def get(sym) - @@window.get(sym) - end - - def set(opts) - @@window.set(opts) - end - - def on(event, &proc) - @@window.on(event, &proc) - end - - def off(event_descriptor) - @@window.off(event_descriptor) - end - - def add(o) - @@window.add(o) - end - - def remove(o) - @@window.remove(o) - end - - def clear - @@window.clear - end - - def update(&proc) - @@window.update(&proc) - end - - def show - @@window.show - end - - def close - @@window.close - end - end -end diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb index aeca4fa..993b5ca 100644 --- a/lib/ruby2d/dsl.rb +++ b/lib/ruby2d/dsl.rb @@ -1,36 +1,39 @@ # dsl.rb module Ruby2D::DSL + + Ruby2D::Window.new + def get(sym) - Application.get(sym) + Window.get(sym) end def set(opts) - Application.set(opts) + Window.set(opts) end def on(event, &proc) - Application.on(event, &proc) + Window.on(event, &proc) end def off(event_descriptor) - Application.off(event_descriptor) + Window.off(event_descriptor) end def update(&proc) - Application.update(&proc) + Window.update(&proc) end def clear - Application.clear + Window.clear end def show - Application.show + Window.show end def close - Application.close + Window.close end end diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb index 8a581ae..d704c17 100644 --- a/lib/ruby2d/renderable.rb +++ b/lib/ruby2d/renderable.rb @@ -10,13 +10,13 @@ module Ruby2D def add if Module.const_defined? :DSL - Application.add(self) + Window.add(self) end end def remove if Module.const_defined? :DSL - Application.remove(self) + Window.remove(self) end end diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 54fd649..6ed6677 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -1,37 +1,67 @@ -# window.rb +# Ruby 2D Window class +# Represents a window on screen, responsible for storing renderable graphics, +# event handlers, the update loop, showing and closing the window. module Ruby2D class Window - attr_reader :objects - attr_accessor :mouse_x, :mouse_y, :frames, :fps - + # Event structures EventDescriptor = Struct.new(:type, :id) MouseEvent = Struct.new(:type, :button, :direction, :x, :y, :delta_x, :delta_y) KeyEvent = Struct.new(:type, :key) - ControllerEvent = Struct.new(:which, :type, :axis, :value, :button) + ControllerEvent = Struct.new(:which, :type, :axis, :value, :button) ControllerAxisEvent = Struct.new(:which, :axis, :value) ControllerButtonEvent = Struct.new(:which, :button) def initialize(args = {}) - @title = args[:title] || "Ruby 2D" + + # This window instance, stored so it can be called by the class methods + @@window = self + + # Title of the window + @title = args[:title] || "Ruby 2D" + + # Window background color @background = Color.new([0.0, 0.0, 0.0, 1.0]) + + # Window icon + @icon = nil + + # Window size and characteristics @width = args[:width] || 640 @height = args[:height] || 480 - @viewport_width, @viewport_height = nil, nil - @display_width, @display_height = nil, nil @resizable = false @borderless = false @fullscreen = false @highdpi = false - @frames = 0 - @fps_cap = args[:fps_cap] || 60 - @fps = @fps_cap - @vsync = args[:vsync] || true + + # Size of the window's viewport (the drawable area) + @viewport_width, @viewport_height = nil, nil + + # Size of the computer's display + @display_width, @display_height = nil, nil + + # Total number of frames that have been rendered + @frames = 0 + + # Frames per second upper limit, and the actual FPS + @fps_cap = args[:fps_cap] || 60 + @fps = @fps_cap + + # Vertical synchronization, set to prevent screen tearing (recommended) + @vsync = args[:vsync] || true + + # Mouse X and Y position in the window @mouse_x, @mouse_y = 0, 0 - @objects = [] - @event_key = 0 - @events = { + + # Renderable objects currently in the window, like a linear scene graph + @objects = [] + + # Unique ID for the input event being registered + @event_key = 0 + + # Registered input events + @events = { key: {}, key_down: {}, key_held: {}, @@ -46,14 +76,80 @@ module Ruby2D controller_button_down: {}, controller_button_up: {} } + + # The window update block @update_proc = Proc.new {} + + # Whether diagnostic messages should be printed @diagnostics = false end - def new_event_key - @event_key = @event_key.next + # Class methods for convenient access to properties + class << self + def current; get(:window) end + def title; get(:title) end + def background; get(:background) end + def width; get(:width) end + def height; get(:height) end + def viewport_width; get(:viewport_width) end + def viewport_height; get(:viewport_height) end + def display_width; get(:display_width) end + def display_height; get(:display_height) end + def resizable; get(:resizable) end + def borderless; get(:borderless) end + def fullscreen; get(:fullscreen) end + def highdpi; get(:highdpi) end + def frames; get(:frames) end + def fps; get(:fps) end + def fps_cap; get(:fps_cap) end + def mouse_x; get(:mouse_x) end + def mouse_y; get(:mouse_y) end + def diagnostics; get(:diagnostics) end + + def get(sym) + @@window.get(sym) + end + + def set(opts) + @@window.set(opts) + end + + def on(event, &proc) + @@window.on(event, &proc) + end + + def off(event_descriptor) + @@window.off(event_descriptor) + end + + def add(o) + @@window.add(o) + end + + def remove(o) + @@window.remove(o) + end + + def clear + @@window.clear + end + + def update(&proc) + @@window.update(&proc) + end + + def show + @@window.show + end + + def close + @@window.close + end end + # Public instance methods + + # Retrieve an attribute of the window def get(sym) case sym when :window; self @@ -83,6 +179,7 @@ module Ruby2D end end + # Set a window attribute def set(opts) # Store new window attributes, or ignore if nil @title = opts[:title] || @title @@ -101,6 +198,7 @@ module Ruby2D @diagnostics = opts[:diagnostics] || @diagnostics end + # Add an object to the window def add(o) case o when nil @@ -112,6 +210,7 @@ module Ruby2D end end + # Remove an object from the window def remove(o) if o == nil raise Error, "Cannot remove '#{o.class}' from window!" @@ -125,15 +224,23 @@ module Ruby2D end end + # Clear all objects from the window def clear @objects.clear end + # Set an update callback def update(&proc) @update_proc = proc true end + # Generate a new event key (ID) + def new_event_key + @event_key = @event_key.next + end + + # Set an event handler def on(event, &proc) unless @events.has_key? event raise Error, "`#{event}` is not a valid event type" @@ -143,10 +250,12 @@ module Ruby2D EventDescriptor.new(event, event_id) end + # Remove an event handler def off(event_descriptor) @events[event_descriptor.type].delete(event_descriptor.id) end + # Key callback method, called by the native and web extentions def key_callback(type, key) key = key.downcase @@ -174,6 +283,7 @@ module Ruby2D end end + # Mouse callback method, called by the native and web extentions def mouse_callback(type, button, direction, x, y, delta_x, delta_y) # All mouse events @events[:mouse].each do |id, e| @@ -204,6 +314,7 @@ module Ruby2D end end + # Controller callback method, called by the native and web extentions def controller_callback(which, type, axis, value, button) # All controller events @events[:controller].each do |id, e| @@ -229,20 +340,26 @@ module Ruby2D end end + # Update callback method, called by the native and web extentions def update_callback @update_proc.call end + # Show the window def show ext_show end + # Close the window def close ext_close end + # Private instance methods + private + # An an object to the window, used by the public `add` method def add_object(o) if [email protected]?(o) index = @objects.index do |object| |
