joybox 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +6 -0
  4. data/Gemfile.lock +27 -0
  5. data/LICENSE +22 -0
  6. data/README.md +86 -0
  7. data/Rakefile +14 -0
  8. data/app/app_delegate.rb +49 -0
  9. data/app/layers/box2d_layer.rb +109 -0
  10. data/app/layers/cocos2d_layer.rb +14 -0
  11. data/app/layers/cocos_motion_layer.rb +145 -0
  12. data/app/layers/menu_layer.rb +59 -0
  13. data/joybox.gemspec +25 -0
  14. data/lib/joybox.rb +47 -0
  15. data/lib/joybox/box2d/body.rb +106 -0
  16. data/lib/joybox/box2d/world.rb +79 -0
  17. data/lib/joybox/cocos2d/actions/actions.rb +7 -0
  18. data/lib/joybox/cocos2d/actions/move.rb +31 -0
  19. data/lib/joybox/cocos2d/actions/repeat.rb +24 -0
  20. data/lib/joybox/cocos2d/actions/rotate.rb +31 -0
  21. data/lib/joybox/cocos2d/actions/scale.rb +31 -0
  22. data/lib/joybox/cocos2d/actions/sequence.rb +22 -0
  23. data/lib/joybox/cocos2d/actions/skew.rb +36 -0
  24. data/lib/joybox/cocos2d/cocos2d.rb +15 -0
  25. data/lib/joybox/cocos2d/common/cg_point.rb +37 -0
  26. data/lib/joybox/cocos2d/common/color.rb +49 -0
  27. data/lib/joybox/cocos2d/common/gc_size.rb +39 -0
  28. data/lib/joybox/cocos2d/common/ns_set.rb +5 -0
  29. data/lib/joybox/cocos2d/common/numeric.rb +14 -0
  30. data/lib/joybox/cocos2d/common/screen.rb +39 -0
  31. data/lib/joybox/cocos2d/common/ui_touch.rb +15 -0
  32. data/lib/joybox/cocos2d/configuration/configuration.rb +68 -0
  33. data/lib/joybox/cocos2d/configuration/file_utils.rb +33 -0
  34. data/lib/joybox/cocos2d/configuration/gl_view.rb +44 -0
  35. data/lib/joybox/cocos2d/configuration/texture_2d.rb +27 -0
  36. data/lib/joybox/cocos2d/core/layer.rb +87 -0
  37. data/lib/joybox/cocos2d/core/node.rb +26 -0
  38. data/lib/joybox/cocos2d/core/physics_sprite.rb +52 -0
  39. data/lib/joybox/cocos2d/core/scene.rb +24 -0
  40. data/lib/joybox/cocos2d/core/sprite.rb +43 -0
  41. data/lib/joybox/cocos2d/core/sprite_batch.rb +24 -0
  42. data/lib/joybox/cocos2d/director.rb +43 -0
  43. data/lib/joybox/cocos2d/macros.rb +234 -0
  44. data/lib/joybox/cocos2d/ui/label.rb +34 -0
  45. data/lib/joybox/cocos2d/ui/menu.rb +25 -0
  46. data/lib/joybox/cocos2d/ui/menu_image.rb +22 -0
  47. data/lib/joybox/cocos2d/ui/menu_label.rb +45 -0
  48. data/lib/joybox/cocos2d/ui/ui.rb +7 -0
  49. data/lib/joybox/version.rb +3 -0
  50. data/resources/box2d/blocks_sprite_sheet.png +0 -0
  51. data/resources/cocos_motion/dot.png +0 -0
  52. data/resources/cocos_motion/dot_selected.png +0 -0
  53. data/resources/cocos_motion/grossini.png +0 -0
  54. data/resources/cocos_motion/grossinis_sister.png +0 -0
  55. data/resources/cocos_motion/grossinis_sister_two.png +0 -0
  56. data/resources/cocos_motion/left_arrow.png +0 -0
  57. data/resources/cocos_motion/left_arrow_selected.png +0 -0
  58. data/resources/cocos_motion/right_arrow.png +0 -0
  59. data/resources/cocos_motion/right_arrow_selected.png +0 -0
  60. data/resources/fps_images-hd.png +0 -0
  61. data/resources/fps_images-ipadhd.png +0 -0
  62. data/resources/fps_images.png +0 -0
  63. data/spec/lib/joybox/cocos2d/cocos2d_spec.rb +7 -0
  64. data/spec/lib/joybox/cocos2d/macros_spec.rb +37 -0
  65. data/spec/main_spec.rb +9 -0
  66. data/vendor/Box2D.framework/Box2D +0 -0
  67. data/vendor/Box2D.framework/Box2D.framework.bridgesupport +166 -0
  68. data/vendor/Box2D.framework/Versions/A/Box2D +0 -0
  69. data/vendor/Box2D.framework/Versions/A/Headers/Collision/B2DContactListener.h +25 -0
  70. data/vendor/Box2D.framework/Versions/A/Headers/Collision/Shapes/B2DEdgeShape.h +17 -0
  71. data/vendor/Box2D.framework/Versions/A/Headers/Collision/Shapes/B2DPolygonShape.h +16 -0
  72. data/vendor/Box2D.framework/Versions/A/Headers/Collision/Shapes/B2DShape.h +15 -0
  73. data/vendor/Box2D.framework/Versions/A/Headers/Dynamics/B2DBody.h +43 -0
  74. data/vendor/Box2D.framework/Versions/A/Headers/Dynamics/B2DWorld.h +41 -0
  75. data/vendor/Box2D.framework/Versions/A/Resources/Box2DConfig.cmake +3 -0
  76. data/vendor/Box2D.framework/Versions/A/Resources/CMakeLists.txt +205 -0
  77. data/vendor/Cocos2D/Cocos2D.bridgesupport +7808 -0
  78. data/vendor/Cocos2D/include/CCAction.h +195 -0
  79. data/vendor/Cocos2D/include/CCActionCamera.h +73 -0
  80. data/vendor/Cocos2D/include/CCActionCatmullRom.h +139 -0
  81. data/vendor/Cocos2D/include/CCActionEase.h +159 -0
  82. data/vendor/Cocos2D/include/CCActionGrid.h +165 -0
  83. data/vendor/Cocos2D/include/CCActionGrid3D.h +205 -0
  84. data/vendor/Cocos2D/include/CCActionInstant.h +227 -0
  85. data/vendor/Cocos2D/include/CCActionInterval.h +431 -0
  86. data/vendor/Cocos2D/include/CCActionManager.h +113 -0
  87. data/vendor/Cocos2D/include/CCActionPageTurn3D.h +42 -0
  88. data/vendor/Cocos2D/include/CCActionProgressTimer.h +59 -0
  89. data/vendor/Cocos2D/include/CCActionTiledGrid.h +211 -0
  90. data/vendor/Cocos2D/include/CCActionTween.h +62 -0
  91. data/vendor/Cocos2D/include/CCAnimation.h +150 -0
  92. data/vendor/Cocos2D/include/CCAnimationCache.h +74 -0
  93. data/vendor/Cocos2D/include/CCArray.h +119 -0
  94. data/vendor/Cocos2D/include/CCAtlasNode.h +96 -0
  95. data/vendor/Cocos2D/include/CCCamera.h +98 -0
  96. data/vendor/Cocos2D/include/CCConfiguration.h +126 -0
  97. data/vendor/Cocos2D/include/CCDirector.h +344 -0
  98. data/vendor/Cocos2D/include/CCDirectorIOS.h +104 -0
  99. data/vendor/Cocos2D/include/CCDrawingPrimitives.h +139 -0
  100. data/vendor/Cocos2D/include/CCES2Renderer.h +83 -0
  101. data/vendor/Cocos2D/include/CCESRenderer.h +54 -0
  102. data/vendor/Cocos2D/include/CCFileUtils.h +188 -0
  103. data/vendor/Cocos2D/include/CCGL.h +72 -0
  104. data/vendor/Cocos2D/include/CCGLProgram.h +150 -0
  105. data/vendor/Cocos2D/include/CCGLView.h +160 -0
  106. data/vendor/Cocos2D/include/CCGrabber.h +44 -0
  107. data/vendor/Cocos2D/include/CCGrid.h +130 -0
  108. data/vendor/Cocos2D/include/CCLabelAtlas.h +68 -0
  109. data/vendor/Cocos2D/include/CCLabelBMFont.h +216 -0
  110. data/vendor/Cocos2D/include/CCLabelTTF.h +132 -0
  111. data/vendor/Cocos2D/include/CCLayer.h +280 -0
  112. data/vendor/Cocos2D/include/CCMenu.h +100 -0
  113. data/vendor/Cocos2D/include/CCMenuItem.h +423 -0
  114. data/vendor/Cocos2D/include/CCMotionStreak.h +87 -0
  115. data/vendor/Cocos2D/include/CCNS.h +54 -0
  116. data/vendor/Cocos2D/include/CCNode+Debug.h +39 -0
  117. data/vendor/Cocos2D/include/CCNode.h +569 -0
  118. data/vendor/Cocos2D/include/CCParallaxNode.h +50 -0
  119. data/vendor/Cocos2D/include/CCParticleBatchNode.h +99 -0
  120. data/vendor/Cocos2D/include/CCParticleExamples.h +95 -0
  121. data/vendor/Cocos2D/include/CCParticleSystem.h +453 -0
  122. data/vendor/Cocos2D/include/CCParticleSystemQuad.h +73 -0
  123. data/vendor/Cocos2D/include/CCProfiling.h +89 -0
  124. data/vendor/Cocos2D/include/CCProgressTimer.h +94 -0
  125. data/vendor/Cocos2D/include/CCProtocols.h +132 -0
  126. data/vendor/Cocos2D/include/CCRenderTexture.h +139 -0
  127. data/vendor/Cocos2D/include/CCScene.h +43 -0
  128. data/vendor/Cocos2D/include/CCScheduler.h +230 -0
  129. data/vendor/Cocos2D/include/CCShaderCache.h +60 -0
  130. data/vendor/Cocos2D/include/CCSprite.h +288 -0
  131. data/vendor/Cocos2D/include/CCSpriteBatchNode.h +138 -0
  132. data/vendor/Cocos2D/include/CCSpriteFrame.h +126 -0
  133. data/vendor/Cocos2D/include/CCSpriteFrameCache.h +121 -0
  134. data/vendor/Cocos2D/include/CCTMXLayer.h +164 -0
  135. data/vendor/Cocos2D/include/CCTMXObjectGroup.h +67 -0
  136. data/vendor/Cocos2D/include/CCTMXTiledMap.h +145 -0
  137. data/vendor/Cocos2D/include/CCTMXXMLParser.h +217 -0
  138. data/vendor/Cocos2D/include/CCTexture2D.h +357 -0
  139. data/vendor/Cocos2D/include/CCTextureAtlas.h +188 -0
  140. data/vendor/Cocos2D/include/CCTextureCache.h +146 -0
  141. data/vendor/Cocos2D/include/CCTexturePVR.h +129 -0
  142. data/vendor/Cocos2D/include/CCTileMapAtlas.h +83 -0
  143. data/vendor/Cocos2D/include/CCTouchDelegateProtocol.h +75 -0
  144. data/vendor/Cocos2D/include/CCTouchDispatcher.h +120 -0
  145. data/vendor/Cocos2D/include/CCTouchHandler.h +93 -0
  146. data/vendor/Cocos2D/include/CCTransition.h +296 -0
  147. data/vendor/Cocos2D/include/CCTransitionPageTurn.h +60 -0
  148. data/vendor/Cocos2D/include/CCTransitionProgress.h +62 -0
  149. data/vendor/Cocos2D/include/CCVertex.h +36 -0
  150. data/vendor/Cocos2D/include/CGPointExtension.h +344 -0
  151. data/vendor/Cocos2D/include/CocosDenshion/CDAudioManager.h +243 -0
  152. data/vendor/Cocos2D/include/CocosDenshion/CDConfig.h +60 -0
  153. data/vendor/Cocos2D/include/CocosDenshion/CDOpenALSupport.h +77 -0
  154. data/vendor/Cocos2D/include/CocosDenshion/CDXPropertyModifierAction.h +45 -0
  155. data/vendor/Cocos2D/include/CocosDenshion/CocosDenshion.h +440 -0
  156. data/vendor/Cocos2D/include/CocosDenshion/SimpleAudioEngine.h +90 -0
  157. data/vendor/Cocos2D/include/NSThread+performBlock.h +22 -0
  158. data/vendor/Cocos2D/include/OpenGL_Internal.h +86 -0
  159. data/vendor/Cocos2D/include/RubyMotionSchedule.h +7 -0
  160. data/vendor/Cocos2D/include/TGAlib.h +55 -0
  161. data/vendor/Cocos2D/include/TransformUtils.h +36 -0
  162. data/vendor/Cocos2D/include/ZipUtils.h +91 -0
  163. data/vendor/Cocos2D/include/base64.h +33 -0
  164. data/vendor/Cocos2D/include/ccCArray.h +229 -0
  165. data/vendor/Cocos2D/include/ccConfig.h +245 -0
  166. data/vendor/Cocos2D/include/ccDeprecated.h +300 -0
  167. data/vendor/Cocos2D/include/ccGLStateCache.h +138 -0
  168. data/vendor/Cocos2D/include/ccMacros.h +353 -0
  169. data/vendor/Cocos2D/include/ccShader_PositionColor_frag.h +12 -0
  170. data/vendor/Cocos2D/include/ccShader_PositionColor_vert.h +17 -0
  171. data/vendor/Cocos2D/include/ccShader_PositionTextureA8Color_frag.h +16 -0
  172. data/vendor/Cocos2D/include/ccShader_PositionTextureA8Color_vert.h +21 -0
  173. data/vendor/Cocos2D/include/ccShader_PositionTextureColorAlphaTest_frag.h +23 -0
  174. data/vendor/Cocos2D/include/ccShader_PositionTextureColor_frag.h +14 -0
  175. data/vendor/Cocos2D/include/ccShader_PositionTextureColor_vert.h +22 -0
  176. data/vendor/Cocos2D/include/ccShader_PositionTexture_frag.h +13 -0
  177. data/vendor/Cocos2D/include/ccShader_PositionTexture_uColor_frag.h +16 -0
  178. data/vendor/Cocos2D/include/ccShader_PositionTexture_uColor_vert.h +18 -0
  179. data/vendor/Cocos2D/include/ccShader_PositionTexture_vert.h +17 -0
  180. data/vendor/Cocos2D/include/ccShader_Position_uColor_frag.h +12 -0
  181. data/vendor/Cocos2D/include/ccShader_Position_uColor_vert.h +19 -0
  182. data/vendor/Cocos2D/include/ccShaders.h +45 -0
  183. data/vendor/Cocos2D/include/ccTypes.h +374 -0
  184. data/vendor/Cocos2D/include/ccUtils.h +36 -0
  185. data/vendor/Cocos2D/include/cocos2d.h +178 -0
  186. data/vendor/Cocos2D/include/kazmath/GL/mat4stack.h +51 -0
  187. data/vendor/Cocos2D/include/kazmath/GL/matrix.h +58 -0
  188. data/vendor/Cocos2D/include/kazmath/aabb.h +53 -0
  189. data/vendor/Cocos2D/include/kazmath/kazmath.h +39 -0
  190. data/vendor/Cocos2D/include/kazmath/mat3.h +75 -0
  191. data/vendor/Cocos2D/include/kazmath/mat4.h +93 -0
  192. data/vendor/Cocos2D/include/kazmath/neon_matrix_impl.h +41 -0
  193. data/vendor/Cocos2D/include/kazmath/plane.h +70 -0
  194. data/vendor/Cocos2D/include/kazmath/quaternion.h +113 -0
  195. data/vendor/Cocos2D/include/kazmath/ray2.h +50 -0
  196. data/vendor/Cocos2D/include/kazmath/utility.h +74 -0
  197. data/vendor/Cocos2D/include/kazmath/vec2.h +64 -0
  198. data/vendor/Cocos2D/include/kazmath/vec3.h +68 -0
  199. data/vendor/Cocos2D/include/kazmath/vec4.h +68 -0
  200. data/vendor/Cocos2D/include/uthash.h +972 -0
  201. data/vendor/Cocos2D/include/utlist.h +490 -0
  202. data/vendor/Cocos2D/libCocosDenshion.a +0 -0
  203. data/vendor/Cocos2D/libcocos2d.a +0 -0
  204. metadata +277 -0
@@ -0,0 +1,87 @@
1
+ module Joybox
2
+ module Core
3
+
4
+ class Layer < CCLayer
5
+
6
+ touch_states = [:began, :moved, :ended, :cancelled]
7
+
8
+ touch_states.each do |touch_state|
9
+
10
+ define_method("on_touches_#{touch_state}") do |&block|
11
+
12
+ self.isTouchEnabled = true if block_given?
13
+
14
+ instance_variable_set("@on_touches_#{touch_state}_block", block)
15
+ end
16
+ end
17
+
18
+
19
+ def self.scene
20
+
21
+ define_singleton_method(:scene) do
22
+
23
+ scene = CCScene.new
24
+
25
+ menu_layer = self.new
26
+
27
+ scene << menu_layer
28
+ end
29
+ end
30
+
31
+
32
+ def onEnter
33
+
34
+ super
35
+
36
+ on_enter if defined? (on_enter)
37
+ end
38
+
39
+
40
+ def onExit
41
+
42
+ super
43
+
44
+ on_exit if defined? (on_exit)
45
+ end
46
+
47
+
48
+ def schedule_update(&block)
49
+
50
+ @schedule_update_block = block if block_given?
51
+
52
+ scheduleUpdate
53
+ end
54
+
55
+
56
+ def update(dt)
57
+
58
+ @schedule_update_block.call(dt) if @schedule_update_block
59
+ end
60
+
61
+
62
+ # These methods can't be autogenerated using metaprogramming
63
+ # because Objective-C will call them
64
+ def ccTouchesBegan(touches, withEvent: event)
65
+
66
+ @on_touches_began_block.call(touches, event) if @on_touches_began_block
67
+ end
68
+
69
+ def ccTouchesMoved(touches, withEvent: event)
70
+
71
+ @on_touches_moved_block.call(touches, event) if @on_touches_moved_block
72
+ end
73
+
74
+ def ccTouchesEnded(touches, withEvent: event)
75
+
76
+ @on_touches_ended_block.call(touches, event) if @on_touches_ended_block
77
+ end
78
+
79
+ def ccTouchesCancelled(touches, withEvent: event)
80
+
81
+ @on_touches_cancelled_block.call(touches, event) if @on_touches_cancelled_block
82
+ end
83
+
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ class CCNode
2
+
3
+ alias_method :add_child, :addChild
4
+
5
+ def << (node)
6
+
7
+ self.addChild(node)
8
+ end
9
+
10
+ def self.inherited(base)
11
+
12
+ base.send(:include, Joybox)
13
+ end
14
+
15
+ #TODO: Revisar el nombre de este metodo
16
+ def add_childs(*nodes)
17
+
18
+ nodes.each do |node|
19
+
20
+ self.addChild(node)
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,52 @@
1
+ # TODO: Revisar el nombre de esto
2
+ module Joybox
3
+ module Core
4
+
5
+ class PhysicsSprite < Sprite
6
+
7
+ attr_accessor :body
8
+
9
+ def self.new(options = {})
10
+
11
+ sprite = super
12
+ sprite.body = options[:body] if options.include? :body
13
+
14
+ sprite
15
+ end
16
+
17
+
18
+ def dirty
19
+
20
+ true
21
+ end
22
+
23
+ # We need this method to return true, so Cocos2d continue
24
+ # to call the nodeToParentTransform method
25
+ def nodeToParentTransform
26
+
27
+ position = @body.position.to_uikit_coordinates
28
+
29
+ position = position + anchorPointInPoints if ignoreAnchorPointForPosition
30
+
31
+ angle = @body.angle
32
+
33
+ x = position.x
34
+ y = position.y
35
+ cosine = Math.cos(angle)
36
+ sine = Math.sin(angle)
37
+
38
+
39
+ unless anchorPointInPoints == CGPointZero
40
+
41
+ x = x + cosine * -anchorPointInPoints.x + -sine * -anchorPointInPoints.y
42
+ y = y + sine * -anchorPointInPoints.x + cosine * -anchorPointInPoints.y
43
+ end
44
+
45
+
46
+ CGAffineTransformMake(cosine, sine, -sine, cosine, x, y)
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ module Joybox
2
+ module Core
3
+
4
+ class Scene < CCScene
5
+
6
+ def onEnter
7
+
8
+ super
9
+
10
+ on_enter if defined? (on_enter)
11
+ end
12
+
13
+
14
+ def onExit
15
+
16
+ super
17
+
18
+ on_exit if defined? (on_exit)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ module Joybox
2
+ module Core
3
+
4
+ class Sprite < CCSprite
5
+
6
+ alias_method :stop_all_actions, :stopAllActions
7
+ alias_method :run_action, :runAction
8
+
9
+ def self.new(options = {})
10
+
11
+ sprite ||= new_with_file(options) if options.has_key? (:file_name)
12
+ sprite ||= new_with_texture(options) if options.has_key? (:texture)
13
+
14
+ sprite.position = options[:position] if options.has_key? (:position)
15
+
16
+ sprite
17
+ end
18
+
19
+
20
+ private
21
+
22
+ def self.new_with_file(options = {})
23
+
24
+ spriteWithFile(options[:file_name])
25
+ end
26
+
27
+
28
+ def self.new_with_texture(options = {})
29
+
30
+ if options.has_key? (:rect)
31
+
32
+ spriteWithTexture(options[:texture], rect: options[:rect])
33
+ else
34
+
35
+ spriteWithTexture(options:[:texture])
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ module Joybox
2
+ module Core
3
+
4
+ class SpriteBatch < CCSpriteBatchNode
5
+
6
+ def self.defaults
7
+ {
8
+ capacity: 100
9
+ }
10
+ end
11
+
12
+
13
+ def self.new(options = {})
14
+
15
+ options = options.nil? ? defaults : defaults.merge!(options)
16
+
17
+ batchNodeWithFile(options[:file_name],
18
+ capacity: options[:capacity])
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ module Joybox
2
+ class Director < CCDirectorDisplayLink
3
+
4
+ alias_method :push_scene, :pushScene
5
+ alias_method :pop_scene, :popScene
6
+ alias_method :pop_to_root_scene, :popToRootScene
7
+ alias_method :replace_scene, :replaceScene
8
+ alias_method :stop_animation, :stopAnimation
9
+ alias_method :start_animation, :startAnimation
10
+ alias_method :purge_cached_data, :purgeCachedData
11
+ alias_method :set_next_delta_time_zero, :setNextDeltaTimeZero
12
+
13
+ def self.default_configuration
14
+ {
15
+ wants_full_screen_layout: true,
16
+ display_stats: false,
17
+ animation_interval: 1.0/60,
18
+ projection: KCCDirectorProjection2D,
19
+ enable_retina_display: true,
20
+ delegate: nil
21
+ }
22
+ end
23
+
24
+ def self.configure(options = {})
25
+ options = options.nil? ? default_configuration : default_configuration.merge!(options)
26
+
27
+ director = Director.sharedDirector
28
+
29
+ director.wantsFullScreenLayout = options.fetch(:wants_full_screen_layout)
30
+ director.displayStats = options.fetch(:display_stats)
31
+ director.animationInterval = options.fetch(:animation_interval)
32
+ director.projection = options.fetch(:projection)
33
+ director.enableRetinaDisplay(options.fetch(:enable_retina_display))
34
+ director.delegate = options.fetch(:delegate)
35
+ end
36
+
37
+
38
+ def << (scene)
39
+ self.push_scene(scene)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,234 @@
1
+ # Extracted from the Cocos2D source: https://github.com/cocos2d/cocos2d-iphone
2
+ # amazing work! Thank you Cocos2D team (https://github.com/cocos2d)!
3
+ #
4
+ # Ruby Motion does detect the original functions (Ex: ccp), thats why we
5
+ # have to change the name of the method prefix to jbp (joy box point)
6
+ #
7
+ module Joybox
8
+ module Macros
9
+ # Creates a CGPoint
10
+ def jbp(x, y)
11
+ CGPointMake(x, y)
12
+ end
13
+
14
+ # Opposite of point
15
+ def jbpNeg(point)
16
+ jbp(-point.x, -point.y)
17
+ end
18
+
19
+ # Sum of two points
20
+ def jbpAdd(first_point, second_point)
21
+ jbp(first_point.x + second_point.x, first_point.y + second_point.y)
22
+ end
23
+
24
+ # Difference of two points
25
+ def jbpSub(first_point, second_point)
26
+ jbp(first_point.x - second_point.x, first_point.y - second_point.y)
27
+ end
28
+
29
+ # Point multiplied by factor
30
+ def jbpMult(first_point, factor)
31
+ jbp(first_point.x * factor, first_point.y * factor)
32
+ end
33
+
34
+ # Midpoint between two points
35
+ def jbpMidPoint(first_point, second_point)
36
+ jbpMult(jbpAdd(first_point, second_point), 0.5)
37
+ end
38
+
39
+ # Product of two points
40
+ def jbpDot(first_point, second_point)
41
+ first_point.x * second_point.x + first_point.y * second_point.y
42
+ end
43
+
44
+ # Cross product of two points
45
+ def jbpCross(first_point, second_point)
46
+ first_point.x * second_point.y - first_point.y * second_point.x
47
+ end
48
+
49
+ # Perpendicular of point, rotated 90 degrees counter-clockwise
50
+ def jbpPerp(point)
51
+ jbp(-point.y, point.x)
52
+ end
53
+
54
+ # Perpendicular of v, rotated 90 degrees clockwise
55
+ def jbpRPerp(point)
56
+ jbp(point.y, -point.x)
57
+ end
58
+
59
+ # Projection of first point over second point
60
+ def jbpProject(first_point, second_point)
61
+ jbpMult(second_point, jbpDot(first_point, second_point) / jbpDot(second_point, second_point))
62
+ end
63
+
64
+ # Rotates two points
65
+ def jbpRotate(first_point, second_point)
66
+ jbp(first_point.x * second_point.x - first_point.y * second_point.y,
67
+ first_point.x * second_point.y + first_point.y * second_point.x)
68
+ end
69
+
70
+ # Unrotates two points
71
+ def jbpUnrotate(first_point, second_point)
72
+ jbp(first_point.x * second_point.x + first_point.y * second_point.y,
73
+ first_point.y * second_point.x - first_point.x * second_point.y)
74
+ end
75
+
76
+ # Square lenght of a CGPoint
77
+ def jbpLengthSQ(point)
78
+ jbpDot(point, point)
79
+ end
80
+
81
+ # Square distance between two points
82
+ def jbpDistanceSQ(first_point, second_point)
83
+ jbpLengthSQ(jbpSub(first_point, second_point))
84
+ end
85
+
86
+ # Distance between point an origin
87
+ def jbpLenght(point)
88
+ Math.sqrt(jbpLengthSQ(point))
89
+ end
90
+
91
+ # Distance between two points
92
+ def jbpDistance(first_point, second_point)
93
+ jbpLenght(jbpSub(first_point, second_point))
94
+ end
95
+
96
+ # Point multiplied to a lenght of 1
97
+ def jbpNormalize(point)
98
+ jbpMult(point, 1.0 / jbpLenght(point))
99
+ end
100
+
101
+ # Converts radians to a normalized vector
102
+ def jbpForAngle(angle)
103
+ jbp(Math.cos(angle), Math.sin(angle))
104
+ end
105
+
106
+ # Converts vector to radians
107
+ def jbpToAngle(point)
108
+ Math.atan2(v.y, v.x)
109
+ end
110
+
111
+ # Clamp a value between from and to
112
+ def clamp(value, minimum_inclusive, maximum_inclusive)
113
+ if minimum_inclusive > maximum_inclusive
114
+
115
+ swap = minimum_inclusive
116
+
117
+ minimum_inclusive = maximum_inclusive
118
+ maximum_inclusive = swap
119
+ end
120
+
121
+ value < minimum_inclusive ? minimum_inclusive : value < maximum_inclusive ? value : maximum_inclusive
122
+ end
123
+
124
+ # Clamp a point between from and to
125
+ def jbpClamp(point, minimum_inclusive, maximum_inclusive)
126
+ jbp(clamp(point.x, minimum_inclusive.x, maximum_inclusive.x),
127
+ clamp(point.y, minimum_inclusive.y, maximum_inclusive.y))
128
+ end
129
+
130
+ # CGSize from a CGPoint
131
+ def jbpFromSize(size)
132
+ jbp(size.width, size.height)
133
+ end
134
+
135
+ # Run a math operation function on each point component
136
+ def jbpCompOp(point, &block)
137
+ jbp(block.call(point.x), block.call(point.y))
138
+ end
139
+
140
+ # Linear interpolation between two points
141
+ def jbpLerp(first_point, second_point, alpha)
142
+ jbpAdd(jbpMult(first_point, 1 - alpha), jbpMult(second_point, alpha))
143
+ end
144
+
145
+ # Determine if two points are equal with some degree of variance
146
+ def jbpFuzzyEqual(first_point, second_point, variance)
147
+ if first_point.x - variance <= second_point.x and second_point.x <= first_point.x + variance
148
+ if first_point.y - variance <= second_point.y and second_point.y <= first_point.y + variance
149
+ return true
150
+ end
151
+ end
152
+
153
+ false
154
+ end
155
+
156
+ # Multiples the first and second point
157
+ def jbpCompMult(first_point, second_point)
158
+ jbp(first_point.x * second_point.x, first_point.y * second_point.y)
159
+ end
160
+
161
+ # Signed angle in radians between two vector directions
162
+ def jbpAngleSigned(first_point, second_point)
163
+ first_point = jbpNormalize(first_point)
164
+ second_point = jbpNormalize(second_point)
165
+
166
+ angle = Math.atan2(first_point.x * second_point.y - first_point.y * second_point.x),
167
+ jbpDot(first_point, second_point)
168
+
169
+ angle = 0.0 if angle.abs < Float::EPSILON
170
+ angle
171
+ end
172
+
173
+ # Angle in radians between two vector directions
174
+ def jbpAngle(first_point, second_point)
175
+ angle = Math.acos(jbpDot(jbpNormalize(first_point), jbpNormalize(second_point)))
176
+ angle = 0.0 if angle.abs < Float::EPSILON
177
+ angle
178
+ end
179
+
180
+ # Rotates a point counter-clockwise by the angle around the pivot
181
+ def jbpRotateByAngle(first_point, pivot_point, angle)
182
+ rotation_point = jbpSub(first_point, pivot_point)
183
+ cosine = Math.cos(angle)
184
+ sine = Math.sin(angle)
185
+
186
+ rotation_point.x = rotation_point.x * cosine - rotation_point.y * sine + pivot_point.x
187
+ rotation_point.y = rotation_point.y * sine + rotation_point.y * cosine + pivot_point.y
188
+
189
+ rotation_point
190
+ end
191
+
192
+ # Evaluates if two lines intersect
193
+ def jbpLineIntersect(point_a, point_b, point_c, point_d)
194
+ # Line undefined
195
+ return false if point_a.x == point_b.x && point_a.y == point_b.y || point_c.x == point_d.x && point_c.y == point_d.y
196
+
197
+ x_b_to_a = point_b.x - point_a.x
198
+ y_b_to_a = point_b.y - point_a.y
199
+ x_d_to_c = point_d.x - point_c.x
200
+ y_d_to_c = point_d.y - point_c.y
201
+ x_a_to_c = point_a.x - point_c.x
202
+ y_a_to_c = point_a.y - point_c.y
203
+
204
+ s = x_d_to_c * y_a_to_c - y_d_to_c * x_a_to_c
205
+ t = x_b_to_a * y_a_to_c - y_b_to_a * x_a_to_c
206
+
207
+ denom = y_d_to_c * x_b_to_a - x_d_to_c * y_b_to_a
208
+ if denom == 0
209
+ # Lines incident
210
+ return { s: hit_point, t: second_hit_point } unless s != 0 && t != 0
211
+
212
+ # Lines parallel and not incident
213
+ return nil
214
+ end
215
+
216
+ { s: hit_point / denom, t: second_hit_point / denom }
217
+ end
218
+
219
+ # Evaluates if two segments intersect
220
+ def jbpSegmentIntersect(point_a, point_b, point_c, point_d)
221
+ r = jbpLineIntersect(point_a, point_b, point_c, point_d)
222
+ return false unless r[:s] >= 0.0 && r[:s] <= 1.0 && r[:t] >= 0.0 && r[:t] <= 1.0
223
+ return true
224
+ end
225
+
226
+ # Intersection point between the two lines
227
+ def jbpIntersectPoint(point_a, point_b, point_c, point_d)
228
+ r = jbpLineIntersect(point_a, point_b, point_c, point_d)
229
+ return CGPointZero unless r
230
+ return CGPointMake(point_a.x + r[:s] * (point_b.x - point_a.x), point_a.y + r[:s] * (point_b.y - point_a.y))
231
+ end
232
+
233
+ end
234
+ end