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 # Gesture Events gesture_states = [:begin, :magnify, :smart_magnify, :rotate, :swipe, :end] gesture_states.each do |gesture_state| define_method("on_gesture_#{gesture_state}") do |&block| self.gestureEnabled = true if block_given? instance_variable_set("@on_gesture_#{gesture_state}_block", block) end end def ccBeginGestureWithEvent(event) @on_gesture_begin_block.call(event) if @on_gesture_begin_block @on_gesture_begin_block ? true : false end def ccMagnifyWithEvent(event) @on_gesture_magnify_block.call(event) if @on_gesture_magnify_block @on_gesture_magnify_block ? true : false end def ccSmartMagnifyWithEvent(event) @on_gesture_smart_magnify_block.call(event) if @on_gesture_smart_magnify_block @on_gesture_smart_magnify_block ? true : false end def ccRotateWithEvent(event) @on_gesture_rotate_block.call(event) if @on_gesture_rotate_block @on_gesture_rotate_block ? true : false end def ccSwipeWithEvent(event) @on_gesture_swipe_block.call(event) if @on_gesture_swipe_block @on_gesture_swipe_block ? true : false end def ccEndGestureWithEvent(event) @on_gesture_end_block.call(event) if @on_gesture_end_block @on_gesture_end_block ? true : false end end end end