module Joybox module Core class Layer < CCLayer # Mouse Events mouse_buttons = [:left, :right, :other] mouse_states = [:down, :dragged, :up, :moved, :entered, :exited, :scroll] mouse_states.each do |mouse_state| define_method("on_mouse_#{mouse_state}") do |&block| self.mouseEnabled = true if block_given? instance_variable_set("@on_mouse_#{mouse_state}_block", block) end end # These methods can't be autogenerated using metaprogramming # because Objective-C will call them def ccMouseDown(event) @on_mouse_down_block.call(event, :left) if @on_mouse_down_block @on_mouse_down_block ? true : false end def ccRightMouseDown(event) @on_mouse_down_block.call(event, :right) if @on_mouse_down_block @on_mouse_down_block ? true : false end def ccOtherMouseDown(event) @on_mouse_down_block.call(event, :other) if @on_mouse_down_block @on_mouse_down_block ? true : false end def ccMouseDragged(event) @on_mouse_dragged_block.call(event, :left) if @on_mouse_dragged_block @on_mouse_dragged_block ? true : false end def ccRightMouseDragged(event) @on_mouse_dragged_block.call(event, :right) if @on_mouse_dragged_block @on_mouse_dragged_block ? true : false end def ccOtherMouseDragged(event) @on_mouse_dragged_block.call(event, :other) if @on_mouse_dragged_block @on_mouse_dragged_block ? true : false end def ccMouseUp(event) @on_mouse_up_block.call(event, :left) if @on_mouse_up_block @on_mouse_up_block ? true : false end def ccRightMouseUp(event) @on_mouse_up_block.call(event, :right) if @on_mouse_up_block @on_mouse_up_block ? true : false end def ccOtherMouseUp(event) @on_mouse_up_block.call(event, :other) if @on_mouse_up_block @on_mouse_up_block ? true : false end def ccMouseMoved(event) @on_mouse_moved_block.call(event) if @on_mouse_moved_block @on_mouse_moved_block ? true : false end def ccMouseEntered(event) @on_mouse_entered_block.call(event) if @on_mouse_entered_block end def ccMouseExited(event) @on_mouse_exited_block.call(event) if @on_mouse_exited_block end def ccScrollWheel(event) @on_mouse_scroll_block.call(event) if @on_mouse_scroll_block @on_mouse_scroll_block ? true : false end # Keyboard Events key_states = [:down, :up, :flag_changed] key_states.each do |key_state| define_method("on_key_#{key_state}") do |&block| self.keyboardEnabled = true if block_given? instance_variable_set("@on_key_#{key_state}_block", block) end end # These methods can't be autogenerated using metaprogramming # because Objective-C will call them def ccKeyDown(event) @on_key_down_block.call(event) if @on_key_down_block @on_key_down_block ? true : false end def ccKeyUp(event) @on_key_up_block.call(event) if @on_key_up_block @on_key_up_block ? true : false end def ccFlagsChanged(event) @on_key_flag_changed_block.call(event) if @on_key_flag_changed_block @on_key_flag_changed_block ? true : false end end end end