lib/ruby2d/window.rb in ruby2d-0.2.1 vs lib/ruby2d/window.rb in ruby2d-0.3.0

- old
+ new

@@ -1,55 +1,72 @@ # window.rb module Ruby2D class Window - attr_reader :title, :width, :height, :mouse_x, :mouse_y - def initialize(width: 640, height: 480, title: "Ruby 2D", fps: 60, vsync: true) - @width, @height, @title = width, height, title - @mouse_x = @mouse_y = 0 - @fps_cap = fps - @fps = 60 - @vsync = vsync - - @objects = [] - @keys = {} - @keys_down = {} - @controller = {} - @update_proc = Proc.new {} + attr_reader :objects + attr_accessor :mouse_x, :mouse_y, :frames, :fps + + def initialize(args = {}) + @title = args[:title] || "Ruby 2D" + @background = Color.new([0.0, 0.0, 0.0, 1.0]) + @width = args[:width] || 640 + @height = args[:height] || 480 + @viewport_width, @viewport_height = nil, nil + @resizable = false + @borderless = false + @fullscreen = false + @highdpi = false + @frames = 0 + @fps_cap = args[:fps] || 60 + @fps = @fps_cap + @vsync = args[:vsync] || true + @mouse_x = 0; @mouse_y = 0 + @objects = [] + @keys_down, @keys, @keys_up, @mouse, @controller = {}, {}, {}, {}, {} + @on_key_proc = Proc.new {} + @on_controller_proc = Proc.new {} + @update_proc = Proc.new {} + @diagnostics = false end def get(sym) case sym - when :window - return self - when :title - return @title - when :width - return @width - when :height - return @height - when :fps - return @fps - when :mouse_x - return @mouse_x - when :mouse_y - return @mouse_y + when :window; self + when :title; @title + when :background; @background + when :width; @width + when :height; @height + when :viewport_width; @viewport_width + when :viewport_height; @viewport_height + when :resizable; @resizable + when :borderless; @borderless + when :fullscreen; @fullscreen + when :highdpi; @highdpi + when :frames; @frames + when :fps; @fps + when :mouse_x; @mouse_x + when :mouse_y; @mouse_y + when :diagnostics; @diagnostics end end def set(opts) - valid_keys = [:title, :width, :height] - valid_opts = opts.reject { |k| !valid_keys.include?(k) } - if !valid_opts.empty? - @title = valid_opts[:title] - @width = valid_opts[:width] - @height = valid_opts[:height] - return true - else - return false + # Store new window attributes, or ignore if nil + @title = opts[:title] || @title + if Color.is_valid? opts[:background] + @background = Color.new(opts[:background]) end + @width = opts[:width] || @width + @height = opts[:height] || @height + @viewport_width = opts[:viewport_width] || @viewport_width + @viewport_height = opts[:viewport_height] || @viewport_height + @resizable = opts[:resizable] || @resizable + @borderless = opts[:borderless] || @borderless + @fullscreen = opts[:fullscreen] || @fullscreen + @highdpi = opts[:highdpi] || @highdpi + @diagnostics = opts[:diagnostics] || @diagnostics end def add(o) case o when nil @@ -81,65 +98,90 @@ def update(&proc) @update_proc = proc true end - def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc) + # def on(mouse: nil, key: nil, key_up: nil, key_down: nil, controller: nil, &proc) + def on(args = {}, &proc) + mouse = args[:mouse] + key = args[:key] + key_up = args[:key_up] + key_down = args[:key_down] + controller = args[:controller] + unless mouse.nil? - # reg_mouse(btn, &proc) + reg_mouse(mouse, &proc) end + unless key_down.nil? + reg_key_down(key_down, &proc) + end + unless key.nil? reg_key(key, &proc) end - unless key_down.nil? - reg_key_down(key_down, &proc) + unless key_up.nil? + reg_key_up(key_up, &proc) end unless controller.nil? reg_controller(controller, &proc) end end - def key_callback(key) - key.downcase! - if @keys.has_key? key - @keys[key].call + def mouse_callback(btn, x, y) + if @mouse.has_key? 'any' + @mouse[btn].call(x, y) end end + def on_key(&proc) + @on_key_proc = proc + true + end + def key_down_callback(key) - key.downcase! + key = key.downcase + if @keys_down.has_key? 'any' + @keys_down['any'].call + end if @keys_down.has_key? key @keys_down[key].call end end - def controller_callback(is_axis, axis, val, is_btn, btn) - - # puts "is_axis: #{is_axis}, axis: #{axis}, val: #{val}, is_btn: #{is_btn}, btn: #{btn}" - - if is_axis - if axis == 0 && val == -32768 - event = 'left' - elsif axis == 0 && val == 32767 - event = 'right' - elsif axis == 1 && val == -32768 - event = 'up' - elsif axis == 1 && val == 32767 - event = 'down' - end - elsif is_btn - event = btn + def key_callback(key) + key = key.downcase + @on_key_proc.call(key) + if @keys.has_key? 'any' + @keys['any'].call end - - if @controller.has_key? event - @controller[event].call + if @keys.has_key? key + @keys[key].call end end + def key_up_callback(key) + key = key.downcase + if @keys_up.has_key? 'any' + @keys_up['any'].call + end + if @keys_up.has_key? key + @keys_up[key].call + end + end + + def on_controller(&proc) + @on_controller_proc = proc + true + end + + def controller_callback(which, is_axis, axis, val, is_btn, btn, pressed) + @on_controller_proc.call(which, is_axis, axis, val, is_btn, btn, pressed) + end + def update_callback @update_proc.call end private @@ -152,17 +194,29 @@ false end end # Register key string with proc + def reg_key_down(key, &proc) + @keys_down[key] = proc + true + end + + # Register key string with proc def reg_key(key, &proc) @keys[key] = proc true end # Register key string with proc - def reg_key_down(key, &proc) - @keys_down[key] = proc + def reg_key_up(key, &proc) + @keys_up[key] = proc + true + end + + # Register mouse button string with proc + def reg_mouse(btn, &proc) + @mouse[btn] = proc true end # Register controller string with proc def reg_controller(event, &proc)