diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d/application.rb | 20 | ||||
| -rw-r--r-- | lib/ruby2d/color.rb | 32 | ||||
| -rw-r--r-- | lib/ruby2d/dsl.rb | 16 | ||||
| -rw-r--r-- | lib/ruby2d/image.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/line.rb | 6 | ||||
| -rw-r--r-- | lib/ruby2d/music.rb | 18 | ||||
| -rw-r--r-- | lib/ruby2d/quad.rb | 14 | ||||
| -rw-r--r-- | lib/ruby2d/rectangle.rb | 18 | ||||
| -rw-r--r-- | lib/ruby2d/renderable.rb | 6 | ||||
| -rw-r--r-- | lib/ruby2d/sound.rb | 12 | ||||
| -rw-r--r-- | lib/ruby2d/sprite.rb | 26 | ||||
| -rw-r--r-- | lib/ruby2d/square.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/text.rb | 20 | ||||
| -rw-r--r-- | lib/ruby2d/triangle.rb | 10 | ||||
| -rw-r--r-- | lib/ruby2d/window.rb | 50 |
15 files changed, 132 insertions, 132 deletions
diff --git a/lib/ruby2d/application.rb b/lib/ruby2d/application.rb index 9984399..d04af70 100644 --- a/lib/ruby2d/application.rb +++ b/lib/ruby2d/application.rb @@ -3,43 +3,43 @@ 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 diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb index 1103c1b..768ebce 100644 --- a/lib/ruby2d/color.rb +++ b/lib/ruby2d/color.rb @@ -15,18 +15,18 @@ module Ruby2D def length @colors.length end - + def opacity; @colors[0].opacity end - + def opacity=(opacity) @colors.each do |color| color.opacity = opacity end end end - + attr_reader :r, :g, :b, :a - + # Based on clrs.cc @@colors = { 'navy' => '#001F3F', @@ -49,7 +49,7 @@ module Ruby2D 'black' => '#111111', 'random' => '' } - + def initialize(c) if !self.class.is_valid? c raise Error, "`#{c}` is not a valid color" @@ -68,19 +68,19 @@ module Ruby2D end end end - + # Check if string is a proper hex value def self.is_hex?(s) # MRuby doesn't support regex, otherwise we'd do: # !(/^#[0-9A-F]{6}$/i.match(a).nil?) s.class == String && s[0] == '#' && s.length == 7 end - + # Check if the color is valid def self.is_valid?(c) @@colors.key?(c) || # keyword self.is_hex?(c) || # hexadecimal value - + # Array of Floats from 0.0..1.0 c.class == Array && c.length == 4 && c.all? { |el| @@ -97,15 +97,15 @@ module Ruby2D Color.new(input) end end - + def opacity; @a end - + def opacity=(opacity) @a = opacity end - + private - + # TODO: Only `Number` supported in JS # Convert from Fixnum (0..255) to Float (0.0..1.0) def to_f(a) @@ -115,19 +115,19 @@ module Ruby2D end return b end - + # Convert from hex value (e.g. #FFF000) to Float (0.0..1.0) def hex_to_f(h) h = (h[1..-1]).chars.each_slice(2).map(&:join) a = [] - + h.each do |el| a.push(el.to_i(16)) end - + a.push(255) return to_f(a) end - + end end diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb index 2d55f22..aeca4fa 100644 --- a/lib/ruby2d/dsl.rb +++ b/lib/ruby2d/dsl.rb @@ -4,33 +4,33 @@ module Ruby2D::DSL def get(sym) Application.get(sym) end - + def set(opts) Application.set(opts) end - + def on(event, &proc) Application.on(event, &proc) end - + def off(event_descriptor) Application.off(event_descriptor) end - + def update(&proc) Application.update(&proc) end - + def clear Application.clear end - + def show Application.show end - + def close Application.close end - + end diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb index 89c447b..6660a4d 100644 --- a/lib/ruby2d/image.rb +++ b/lib/ruby2d/image.rb @@ -6,15 +6,15 @@ module Ruby2D attr_accessor :x, :y, :width, :height, :data attr_reader :path, :color - + def initialize(x, y, path, z=0) - + unless RUBY_ENGINE == 'opal' unless File.exists? path raise Error, "Cannot find image file `#{path}`" end end - + @type_id = 4 @x, @y, @path = x, y, path @z = z @@ -22,7 +22,7 @@ module Ruby2D ext_image_init(path) add end - + def color=(c) @color = Color.new(c) end diff --git a/lib/ruby2d/line.rb b/lib/ruby2d/line.rb index 4832379..9ef534e 100644 --- a/lib/ruby2d/line.rb +++ b/lib/ruby2d/line.rb @@ -13,14 +13,14 @@ module Ruby2D self.color = c add end - + def color=(c) @color = Color.from(c) update_color(@color) end - + private - + def update_color(c) if c.is_a? Color::Set if c.length == 4 diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb index bd7df45..2437019 100644 --- a/lib/ruby2d/music.rb +++ b/lib/ruby2d/music.rb @@ -2,39 +2,39 @@ module Ruby2D class Music - + attr_accessor :data, :loop attr_reader :path - + def initialize(path) - + unless RUBY_ENGINE == 'opal' unless File.exists? path raise Error, "Cannot find audio file `#{path}`" end end - + @path = path @loop = false ext_music_init(path) end - + def play ext_music_play end - + def pause ext_music_pause end - + def resume ext_music_resume end - + def stop ext_music_stop end - + def fadeout(ms) ext_music_fadeout(ms) end diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb index 60994a5..e3906c0 100644 --- a/lib/ruby2d/quad.rb +++ b/lib/ruby2d/quad.rb @@ -1,7 +1,7 @@ # quad.rb module Ruby2D - class Quad + class Quad include Renderable # Coordinates in clockwise order, starting at top left: # x1,y1 == top left @@ -12,25 +12,25 @@ module Ruby2D :x2, :y2, :c2, :x3, :y3, :c3, :x4, :y4, :c4 - + attr_reader :color - + def initialize(x1=0, y1=0, x2=100, y2=0, x3=100, y3=100, x4=100, y4=100, c='white', z=0) @type_id = 2 @x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4 @z = z - + self.color = c add end - + def color=(c) @color = Color.from(c) update_color(@color) end - + private - + def update_color(c) if c.is_a? Color::Set if c.length == 4 diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb index 37ff3c7..569b8b5 100644 --- a/lib/ruby2d/rectangle.rb +++ b/lib/ruby2d/rectangle.rb @@ -2,9 +2,9 @@ module Ruby2D class Rectangle < Quad - + attr_reader :x, :y, :width, :height - + def initialize(x=0, y=0, w=200, h=100, c='white', z=0) @type_id = 2 @x, @y, @width, @height = x, y, w, h @@ -14,33 +14,33 @@ module Ruby2D self.color = c add end - + def x=(x) @x = @x1 = x @x2 = x + @width @x3 = x + @width @x4 = x end - + def y=(y) @y = @y1 = y @y2 = y @y3 = y + @height @y4 = y + @height end - + def width=(w) @width = w update_coords(@x, @y, w, @height) end - + def height=(h) @height = h update_coords(@x, @y, @width, h) end - + private - + def update_coords(x, y, w, h) @x1 = x @y1 = y @@ -51,6 +51,6 @@ module Ruby2D @x3 = x + w @y3 = y + h end - + end end diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb index 888c29f..d80482a 100644 --- a/lib/ruby2d/renderable.rb +++ b/lib/ruby2d/renderable.rb @@ -13,17 +13,17 @@ module Ruby2D Application.add(self) end end - + def remove if Module.const_defined? :DSL Application.remove(self) end end - + def opacity self.color.opacity end - + def opacity=(val) self.color.opacity = val end diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb index 776d718..c7b03e6 100644 --- a/lib/ruby2d/sound.rb +++ b/lib/ruby2d/sound.rb @@ -2,25 +2,25 @@ module Ruby2D class Sound - + attr_accessor :data attr_reader :path - + def initialize(path) - + unless RUBY_ENGINE == 'opal' unless File.exists? path raise Error, "Cannot find audio file `#{path}`" end end - + @path = path ext_sound_init(path) end - + def play ext_sound_play end - + end end diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb index 4d6ac23..999e66d 100644 --- a/lib/ruby2d/sprite.rb +++ b/lib/ruby2d/sprite.rb @@ -2,7 +2,7 @@ module Ruby2D class Sprite - + attr_accessor :x, :y, :clip_x, :clip_y, :clip_w, :clip_h, :data attr_reader :z @@ -11,7 +11,7 @@ module Ruby2D # unless File.exists? path # raise Error, "Cannot find image file `#{path}`" # end - + @type_id = 5 @x, @y, @path = x, y, path @clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0 @@ -27,16 +27,16 @@ module Ruby2D Application.add(self) end end - + def start(x, y, w, h) @default = [x, y, w, h] clip(x, y, w, h) end - + def add(animations) @animations.merge!(animations) end - + def animate(animation) if @current_animation != animation @current_frame = 0 @@ -45,31 +45,31 @@ module Ruby2D end animate_frames(@animations[animation]) end - + def reset clip(@default[0], @default[1], @default[2], @default[3]) @current_animation = nil end - + # TODO: Sprite already has an `add` method, have to reconsile # def add # if Module.const_defined? :DSL # Application.add(self) # end # end - + def remove if Module.const_defined? :DSL Application.remove(self) end end - + private - + def clip(x, y, w, h) @clip_x, @clip_y, @clip_w, @clip_h = x, y, w, h end - + def animate_frames(frames) if @current_frame_time < frames[@current_frame][4] clip_with_current_frame(frames) @@ -83,11 +83,11 @@ module Ruby2D @current_frame_time = 0 end end - + def clip_with_current_frame(frames) clip(frames[@current_frame][0], frames[@current_frame][1], frames[@current_frame][2], frames[@current_frame][3]) end - + end end diff --git a/lib/ruby2d/square.rb b/lib/ruby2d/square.rb index 66347ec..26a929d 100644 --- a/lib/ruby2d/square.rb +++ b/lib/ruby2d/square.rb @@ -2,9 +2,9 @@ module Ruby2D class Square < Rectangle - + attr_reader :size - + def initialize(x=0, y=0, s=100, c='white', z=0) @type_id = 2 @x, @y = x, y @@ -15,11 +15,11 @@ module Ruby2D self.color = c add end - + def size=(s) self.width = self.height = @size = s end - + private :width=, :height= end end diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 022f79c..d3c525f 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -3,18 +3,18 @@ module Ruby2D class Text include Renderable - + attr_accessor :x, :y, :data attr_reader :text, :size, :width, :height, :font, :color - + def initialize(x=0, y=0, text="Hello World!", size=20, font=nil, c='white', z=0) - + # if File.exists? font @font = font # else # @font = resolve_path(font) # end - + @type_id = 6 @x, @y, @size = x, y, size @z = z @@ -23,18 +23,18 @@ module Ruby2D ext_text_init add end - + def text=(msg) @text = msg.to_s ext_text_set(@text) end - + def color=(c) @color = Color.new(c) end - + private - + def resolve_path(font) if RUBY_PLATFORM =~ /darwin/ font_path = "/Library/Fonts/#{font}.ttf" @@ -42,13 +42,13 @@ module Ruby2D # Linux font_path = "/usr/share/fonts/truetype/#{font}.ttf" end - + unless File.exists? font_path raise Error, "Cannot find system font" else font_path end end - + end end diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb index cf6e24a..eb6b07f 100644 --- a/lib/ruby2d/triangle.rb +++ b/lib/ruby2d/triangle.rb @@ -3,12 +3,12 @@ module Ruby2D class Triangle include Renderable - + attr_accessor :x1, :y1, :c1, :x2, :y2, :c2, :x3, :y3, :c3 attr_reader :color, :type_id - + def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white', z=0) @type_id = 1 @x1, @y1 = x1, y1 @@ -19,14 +19,14 @@ module Ruby2D self.color = c add end - + def color=(c) @color = Color.from(c) update_color(@color) end - + private - + def update_color(c) if c.is_a? Color::Set if c.length == 3 diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 837b55a..9970861 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -2,10 +2,10 @@ module Ruby2D class Window - + attr_reader :objects attr_accessor :mouse_x, :mouse_y, :frames, :fps - + 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) @@ -46,7 +46,7 @@ module Ruby2D @update_proc = Proc.new {} @diagnostics = false end - + def new_event_key @event_key = @event_key.next end @@ -71,7 +71,7 @@ module Ruby2D when :diagnostics; @diagnostics end end - + def set(opts) # Store new window attributes, or ignore if nil @title = opts[:title] || @title @@ -88,7 +88,7 @@ module Ruby2D @highdpi = opts[:highdpi] || @highdpi @diagnostics = opts[:diagnostics] || @diagnostics end - + def add(o) case o when nil @@ -99,12 +99,12 @@ module Ruby2D add_object(o) end end - + def remove(o) if o == nil raise Error, "Cannot remove '#{o.class}' from window!" end - + if i = @objects.index(o) @objects.delete_at(i) true @@ -112,36 +112,36 @@ module Ruby2D false end end - + def clear @objects.clear end - + def update(&proc) @update_proc = proc true end - + def on(event, &proc) event_id = new_event_key @events[event][event_id] = proc EventDescriptor.new(event, event_id) end - + def off(event_descriptor) @events[event_descriptor.type].delete(event_descriptor.id) end def key_callback(type, key) # puts "===", "type: #{type}", "key: #{key}" - + key = key.downcase - + # All key events @events[:key].each do |id, e| e.call(KeyEvent.new(type, key)) end - + case type # When key is pressed, fired once when :down @@ -160,17 +160,17 @@ module Ruby2D end end end - + def mouse_callback(type, button, direction, x, y, delta_x, delta_y) # Convert to symbols (see MRuby bug in native extension) button = button.to_sym unless button == nil direction = direction.to_sym unless direction == nil - + # All mouse events @events[:mouse].each do |id, e| e.call(MouseEvent.new(type, button, direction, x, y, delta_x, delta_y)) end - + case type # When mouse button pressed when :down @@ -194,13 +194,13 @@ module Ruby2D end end end - + def controller_callback(which, type, axis, value, button) # All controller events @events[:controller].each do |id, e| e.call(ControllerEvent.new(which, type, axis, value, button)) end - + case type # When controller axis motion, like analog sticks when :axis @@ -219,21 +219,21 @@ module Ruby2D end end end - + def update_callback @update_proc.call end - + def show ext_window_show end - + def close ext_window_close end - + private - + def add_object(o) if [email protected]?(o) index = @objects.index do |object| @@ -249,6 +249,6 @@ module Ruby2D false end end - + end end |
