reflexion 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (223) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/reflex/application.cpp +5 -5
  3. data/.doc/ext/reflex/body.cpp +167 -0
  4. data/.doc/ext/reflex/capture_event.cpp +4 -4
  5. data/.doc/ext/reflex/draw_event.cpp +4 -4
  6. data/.doc/ext/reflex/event.cpp +4 -4
  7. data/.doc/ext/reflex/fixture.cpp +101 -0
  8. data/.doc/ext/reflex/focus_event.cpp +4 -4
  9. data/.doc/ext/reflex/frame_event.cpp +4 -4
  10. data/.doc/ext/reflex/image_view.cpp +22 -5
  11. data/.doc/ext/reflex/key_event.cpp +4 -4
  12. data/.doc/ext/reflex/native.cpp +6 -0
  13. data/.doc/ext/reflex/pointer_event.cpp +4 -4
  14. data/.doc/ext/reflex/reflex.cpp +2 -3
  15. data/.doc/ext/reflex/scroll_event.cpp +4 -4
  16. data/.doc/ext/reflex/selector.cpp +4 -4
  17. data/.doc/ext/reflex/style.cpp +4 -4
  18. data/.doc/ext/reflex/style_length.cpp +4 -4
  19. data/.doc/ext/reflex/style_length2.cpp +4 -4
  20. data/.doc/ext/reflex/style_length4.cpp +4 -4
  21. data/.doc/ext/reflex/update_event.cpp +4 -4
  22. data/.doc/ext/reflex/view.cpp +110 -8
  23. data/.doc/ext/reflex/wheel_event.cpp +4 -4
  24. data/.doc/ext/reflex/window.cpp +36 -12
  25. data/README.md +2 -2
  26. data/Rakefile +2 -1
  27. data/VERSION +1 -1
  28. data/ext/reflex/application.cpp +8 -8
  29. data/ext/reflex/body.cpp +181 -0
  30. data/ext/reflex/capture_event.cpp +4 -4
  31. data/ext/reflex/draw_event.cpp +4 -4
  32. data/ext/reflex/event.cpp +4 -4
  33. data/ext/reflex/fixture.cpp +108 -0
  34. data/ext/reflex/focus_event.cpp +4 -4
  35. data/ext/reflex/frame_event.cpp +4 -4
  36. data/ext/reflex/image_view.cpp +24 -5
  37. data/ext/reflex/key_event.cpp +4 -4
  38. data/ext/reflex/native.cpp +6 -0
  39. data/ext/reflex/pointer_event.cpp +4 -4
  40. data/ext/reflex/reflex.cpp +2 -3
  41. data/ext/reflex/scroll_event.cpp +4 -4
  42. data/ext/reflex/selector.cpp +4 -4
  43. data/ext/reflex/style.cpp +4 -4
  44. data/ext/reflex/style_length.cpp +4 -4
  45. data/ext/reflex/style_length2.cpp +4 -4
  46. data/ext/reflex/style_length4.cpp +4 -4
  47. data/ext/reflex/update_event.cpp +4 -4
  48. data/ext/reflex/view.cpp +126 -12
  49. data/ext/reflex/wheel_event.cpp +4 -4
  50. data/ext/reflex/window.cpp +39 -12
  51. data/include/reflex/bitmap.h +20 -0
  52. data/include/reflex/body.h +82 -0
  53. data/include/reflex/bounds.h +20 -0
  54. data/include/reflex/color.h +20 -0
  55. data/include/reflex/color_space.h +20 -0
  56. data/include/reflex/defs.h +5 -1
  57. data/include/reflex/event.h +16 -25
  58. data/include/reflex/fixture.h +116 -0
  59. data/include/reflex/font.h +20 -0
  60. data/include/reflex/image.h +20 -0
  61. data/include/reflex/image_view.h +1 -1
  62. data/include/reflex/matrix.h +20 -0
  63. data/include/reflex/painter.h +20 -0
  64. data/include/reflex/point.h +24 -0
  65. data/include/reflex/ruby/application.h +6 -0
  66. data/include/reflex/ruby/body.h +41 -0
  67. data/include/reflex/ruby/event.h +68 -0
  68. data/include/reflex/ruby/fixture.h +41 -0
  69. data/include/reflex/ruby/image_view.h +14 -0
  70. data/include/reflex/ruby/selector.h +14 -0
  71. data/include/reflex/ruby/style.h +14 -0
  72. data/include/reflex/ruby/style_length.h +26 -0
  73. data/include/reflex/ruby/view.h +43 -39
  74. data/include/reflex/ruby/window.h +27 -21
  75. data/include/reflex/ruby.h +2 -0
  76. data/include/reflex/shader.h +20 -0
  77. data/include/reflex/style.h +2 -3
  78. data/include/reflex/texture.h +20 -0
  79. data/include/reflex/view.h +45 -24
  80. data/include/reflex/window.h +11 -14
  81. data/include/reflex.h +3 -0
  82. data/lib/reflex/application.rb +6 -2
  83. data/lib/reflex/body.rb +17 -0
  84. data/lib/reflex/fixture.rb +17 -0
  85. data/lib/reflex/view.rb +16 -1
  86. data/lib/reflex/window.rb +13 -1
  87. data/lib/reflex.rb +2 -0
  88. data/reflex.gemspec +1 -1
  89. data/samples/bats.rb +4 -3
  90. data/samples/checker.rb +4 -3
  91. data/samples/fans.rb +4 -3
  92. data/samples/fps.rb +5 -3
  93. data/samples/grid.rb +5 -4
  94. data/samples/hello.rb +1 -1
  95. data/samples/image.rb +3 -2
  96. data/samples/ios/hello/hello/main.cpp +12 -3
  97. data/samples/ios/hello/hello.xcodeproj/project.pbxproj +376 -123
  98. data/samples/key.rb +5 -4
  99. data/samples/layout.rb +3 -2
  100. data/samples/model.rb +3 -2
  101. data/samples/osx/hello/hello/main.cpp +12 -3
  102. data/samples/osx/hello/hello.xcodeproj/project.pbxproj +375 -120
  103. data/samples/physics.rb +37 -0
  104. data/samples/repl.rb +3 -2
  105. data/samples/shader.rb +4 -4
  106. data/samples/shapes.rb +4 -4
  107. data/samples/text.rb +6 -4
  108. data/samples/tree.rb +5 -4
  109. data/samples/views.rb +3 -2
  110. data/samples/visuals.rb +1 -2
  111. data/src/body.cpp +244 -0
  112. data/src/event.cpp +18 -18
  113. data/src/fixture.cpp +108 -0
  114. data/src/image_view.cpp +1 -1
  115. data/src/ios/native_window.h +2 -0
  116. data/src/ios/native_window.mm +18 -1
  117. data/src/ios/opengl_view.mm +8 -0
  118. data/src/ios/window.mm +24 -3
  119. data/src/ios/window_data.h +3 -3
  120. data/src/osx/native_window.mm +17 -1
  121. data/src/osx/window.mm +24 -3
  122. data/src/osx/window_data.h +3 -3
  123. data/src/physics/Box2D/Box2D.h +68 -0
  124. data/src/physics/Box2D/Collision/Shapes/b2ChainShape.cpp +193 -0
  125. data/src/physics/Box2D/Collision/Shapes/b2ChainShape.h +105 -0
  126. data/src/physics/Box2D/Collision/Shapes/b2CircleShape.cpp +99 -0
  127. data/src/physics/Box2D/Collision/Shapes/b2CircleShape.h +91 -0
  128. data/src/physics/Box2D/Collision/Shapes/b2EdgeShape.cpp +138 -0
  129. data/src/physics/Box2D/Collision/Shapes/b2EdgeShape.h +74 -0
  130. data/src/physics/Box2D/Collision/Shapes/b2PolygonShape.cpp +467 -0
  131. data/src/physics/Box2D/Collision/Shapes/b2PolygonShape.h +101 -0
  132. data/src/physics/Box2D/Collision/Shapes/b2Shape.h +101 -0
  133. data/src/physics/Box2D/Collision/b2BroadPhase.cpp +119 -0
  134. data/src/physics/Box2D/Collision/b2BroadPhase.h +257 -0
  135. data/src/physics/Box2D/Collision/b2CollideCircle.cpp +154 -0
  136. data/src/physics/Box2D/Collision/b2CollideEdge.cpp +698 -0
  137. data/src/physics/Box2D/Collision/b2CollidePolygon.cpp +239 -0
  138. data/src/physics/Box2D/Collision/b2Collision.cpp +252 -0
  139. data/src/physics/Box2D/Collision/b2Collision.h +277 -0
  140. data/src/physics/Box2D/Collision/b2Distance.cpp +603 -0
  141. data/src/physics/Box2D/Collision/b2Distance.h +141 -0
  142. data/src/physics/Box2D/Collision/b2DynamicTree.cpp +778 -0
  143. data/src/physics/Box2D/Collision/b2DynamicTree.h +289 -0
  144. data/src/physics/Box2D/Collision/b2TimeOfImpact.cpp +486 -0
  145. data/src/physics/Box2D/Collision/b2TimeOfImpact.h +58 -0
  146. data/src/physics/Box2D/Common/b2BlockAllocator.cpp +215 -0
  147. data/src/physics/Box2D/Common/b2BlockAllocator.h +62 -0
  148. data/src/physics/Box2D/Common/b2Draw.cpp +44 -0
  149. data/src/physics/Box2D/Common/b2Draw.h +86 -0
  150. data/src/physics/Box2D/Common/b2GrowableStack.h +85 -0
  151. data/src/physics/Box2D/Common/b2Math.cpp +94 -0
  152. data/src/physics/Box2D/Common/b2Math.h +720 -0
  153. data/src/physics/Box2D/Common/b2Settings.cpp +44 -0
  154. data/src/physics/Box2D/Common/b2Settings.h +151 -0
  155. data/src/physics/Box2D/Common/b2StackAllocator.cpp +83 -0
  156. data/src/physics/Box2D/Common/b2StackAllocator.h +60 -0
  157. data/src/physics/Box2D/Common/b2Timer.cpp +101 -0
  158. data/src/physics/Box2D/Common/b2Timer.h +50 -0
  159. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.cpp +53 -0
  160. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h +39 -0
  161. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.cpp +53 -0
  162. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h +39 -0
  163. data/src/physics/Box2D/Dynamics/Contacts/b2CircleContact.cpp +52 -0
  164. data/src/physics/Box2D/Dynamics/Contacts/b2CircleContact.h +39 -0
  165. data/src/physics/Box2D/Dynamics/Contacts/b2Contact.cpp +247 -0
  166. data/src/physics/Box2D/Dynamics/Contacts/b2Contact.h +349 -0
  167. data/src/physics/Box2D/Dynamics/Contacts/b2ContactSolver.cpp +838 -0
  168. data/src/physics/Box2D/Dynamics/Contacts/b2ContactSolver.h +95 -0
  169. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.cpp +49 -0
  170. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h +39 -0
  171. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.cpp +49 -0
  172. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h +39 -0
  173. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp +49 -0
  174. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h +38 -0
  175. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonContact.cpp +52 -0
  176. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonContact.h +39 -0
  177. data/src/physics/Box2D/Dynamics/Joints/b2DistanceJoint.cpp +260 -0
  178. data/src/physics/Box2D/Dynamics/Joints/b2DistanceJoint.h +169 -0
  179. data/src/physics/Box2D/Dynamics/Joints/b2FrictionJoint.cpp +251 -0
  180. data/src/physics/Box2D/Dynamics/Joints/b2FrictionJoint.h +119 -0
  181. data/src/physics/Box2D/Dynamics/Joints/b2GearJoint.cpp +419 -0
  182. data/src/physics/Box2D/Dynamics/Joints/b2GearJoint.h +125 -0
  183. data/src/physics/Box2D/Dynamics/Joints/b2Joint.cpp +211 -0
  184. data/src/physics/Box2D/Dynamics/Joints/b2Joint.h +226 -0
  185. data/src/physics/Box2D/Dynamics/Joints/b2MotorJoint.cpp +304 -0
  186. data/src/physics/Box2D/Dynamics/Joints/b2MotorJoint.h +133 -0
  187. data/src/physics/Box2D/Dynamics/Joints/b2MouseJoint.cpp +222 -0
  188. data/src/physics/Box2D/Dynamics/Joints/b2MouseJoint.h +129 -0
  189. data/src/physics/Box2D/Dynamics/Joints/b2PrismaticJoint.cpp +629 -0
  190. data/src/physics/Box2D/Dynamics/Joints/b2PrismaticJoint.h +196 -0
  191. data/src/physics/Box2D/Dynamics/Joints/b2PulleyJoint.cpp +348 -0
  192. data/src/physics/Box2D/Dynamics/Joints/b2PulleyJoint.h +152 -0
  193. data/src/physics/Box2D/Dynamics/Joints/b2RevoluteJoint.cpp +502 -0
  194. data/src/physics/Box2D/Dynamics/Joints/b2RevoluteJoint.h +204 -0
  195. data/src/physics/Box2D/Dynamics/Joints/b2RopeJoint.cpp +241 -0
  196. data/src/physics/Box2D/Dynamics/Joints/b2RopeJoint.h +114 -0
  197. data/src/physics/Box2D/Dynamics/Joints/b2WeldJoint.cpp +344 -0
  198. data/src/physics/Box2D/Dynamics/Joints/b2WeldJoint.h +126 -0
  199. data/src/physics/Box2D/Dynamics/Joints/b2WheelJoint.cpp +419 -0
  200. data/src/physics/Box2D/Dynamics/Joints/b2WheelJoint.h +210 -0
  201. data/src/physics/Box2D/Dynamics/b2Body.cpp +549 -0
  202. data/src/physics/Box2D/Dynamics/b2Body.h +860 -0
  203. data/src/physics/Box2D/Dynamics/b2ContactManager.cpp +296 -0
  204. data/src/physics/Box2D/Dynamics/b2ContactManager.h +52 -0
  205. data/src/physics/Box2D/Dynamics/b2Fixture.cpp +303 -0
  206. data/src/physics/Box2D/Dynamics/b2Fixture.h +345 -0
  207. data/src/physics/Box2D/Dynamics/b2Island.cpp +539 -0
  208. data/src/physics/Box2D/Dynamics/b2Island.h +93 -0
  209. data/src/physics/Box2D/Dynamics/b2TimeStep.h +70 -0
  210. data/src/physics/Box2D/Dynamics/b2World.cpp +1339 -0
  211. data/src/physics/Box2D/Dynamics/b2World.h +354 -0
  212. data/src/physics/Box2D/Dynamics/b2WorldCallbacks.cpp +36 -0
  213. data/src/physics/Box2D/Dynamics/b2WorldCallbacks.h +155 -0
  214. data/src/physics/Box2D/Rope/b2Rope.cpp +259 -0
  215. data/src/physics/Box2D/Rope/b2Rope.h +115 -0
  216. data/src/style.cpp +2 -2
  217. data/src/view.cpp +217 -17
  218. data/src/window.cpp +25 -15
  219. data/src/world.cpp +206 -0
  220. data/src/world.h +96 -0
  221. data/task/box2d.rake +25 -0
  222. data/test/test_view.rb +5 -5
  223. metadata +125 -3
data/src/view.cpp CHANGED
@@ -4,10 +4,10 @@
4
4
  #include <assert.h>
5
5
  #include <algorithm>
6
6
  #include <boost/scoped_ptr.hpp>
7
- #include <rays/bounds.h>
8
- #include <rays/painter.h>
9
7
  #include "reflex/window.h"
8
+ #include "reflex/body.h"
10
9
  #include "reflex/exception.h"
10
+ #include "world.h"
11
11
 
12
12
 
13
13
  namespace Reflex
@@ -38,6 +38,10 @@ namespace Reflex
38
38
 
39
39
  boost::scoped_ptr<StyleList> pstyles;
40
40
 
41
+ boost::scoped_ptr<World> pworld;
42
+
43
+ boost::scoped_ptr<Body> pbody;
44
+
41
45
  Style style;
42
46
 
43
47
  ushort capture;
@@ -80,6 +84,30 @@ namespace Reflex
80
84
  return *pstyles;
81
85
  }
82
86
 
87
+ World* world ()
88
+ {
89
+ if (!pworld) pworld.reset(new World());
90
+ return pworld.get();
91
+ }
92
+
93
+ World* parent_world ()
94
+ {
95
+ if (!parent) return NULL;
96
+ return parent->self->world();
97
+ }
98
+
99
+ Body* body (View* this_)
100
+ {
101
+ if (!pbody)
102
+ {
103
+ World* world = parent_world();
104
+ if (!world) return NULL;
105
+ pbody.reset(world->create_body(frame.position()));
106
+ this_->make_body();
107
+ }
108
+ return pbody.get();
109
+ }
110
+
83
111
  };// View::Data
84
112
 
85
113
 
@@ -110,6 +138,7 @@ namespace Reflex
110
138
  {
111
139
  Event e;
112
140
  this_->on_attach(&e);
141
+ this_->resize_to_fit();
113
142
  }
114
143
  }
115
144
 
@@ -133,6 +162,21 @@ namespace Reflex
133
162
  set_window(this_, parent_ ? parent_->window() : NULL);
134
163
  }
135
164
 
165
+ static void
166
+ update_world (View* view, float dt)
167
+ {
168
+ World* world = view->self->pworld.get();
169
+ if (world) world->step(dt);
170
+
171
+ Body* body = view->self->pbody.get();
172
+ if (body)
173
+ {
174
+ Bounds b = view->frame();
175
+ b.move_to(body->position());
176
+ view->set_frame(b);
177
+ }
178
+ }
179
+
136
180
  void
137
181
  update_view_tree (View* view, const UpdateEvent& event)
138
182
  {
@@ -140,6 +184,7 @@ namespace Reflex
140
184
  argument_error(__FILE__, __LINE__);
141
185
 
142
186
  UpdateEvent e = event;
187
+ update_world(view, e.dt);
143
188
  view->on_update(&e);
144
189
 
145
190
  View::child_iterator end = view->child_end();
@@ -147,6 +192,13 @@ namespace Reflex
147
192
  update_view_tree(it->get(), e);
148
193
  }
149
194
 
195
+ static void
196
+ draw_world (View* view, Painter* painter)
197
+ {
198
+ World* world = view->self->pworld.get();
199
+ if (world) world->draw(painter);
200
+ }
201
+
150
202
  void
151
203
  draw_view_tree (
152
204
  View* view, const DrawEvent& event, const Point& offset, const Bounds& clip)
@@ -154,6 +206,8 @@ namespace Reflex
154
206
  if (!view)
155
207
  argument_error(__FILE__, __LINE__);
156
208
 
209
+ if (view->hidden()) return;
210
+
157
211
  DrawEvent e = event;
158
212
  e.painter->push_matrix();
159
213
  e.painter->push_attr();
@@ -174,6 +228,8 @@ namespace Reflex
174
228
  for (View::child_iterator it = view->child_begin(); it != end; ++it)
175
229
  draw_view_tree(it->get(), e, p, c);
176
230
 
231
+ draw_world(view, e.painter);
232
+
177
233
  e.painter->pop_attr();
178
234
  e.painter->pop_matrix();
179
235
  }
@@ -651,8 +707,17 @@ namespace Reflex
651
707
  if (!*this)
652
708
  invalid_state_error(__FILE__, __LINE__);
653
709
 
654
- --self->hide_count;
655
- redraw();
710
+ int new_count = self->hide_count - 1;
711
+ if (new_count == 0)
712
+ {
713
+ Event e;
714
+ on_show(&e);
715
+ if (e.is_blocked()) return;
716
+
717
+ redraw();
718
+ }
719
+
720
+ self->hide_count = new_count;
656
721
  }
657
722
 
658
723
  void
@@ -661,8 +726,17 @@ namespace Reflex
661
726
  if (!*this)
662
727
  invalid_state_error(__FILE__, __LINE__);
663
728
 
664
- ++self->hide_count;
665
- redraw();
729
+ int new_count = self->hide_count + 1;
730
+ if (new_count == 1)
731
+ {
732
+ Event e;
733
+ on_hide(&e);
734
+ if (e.is_blocked()) return;
735
+
736
+ redraw();
737
+ }
738
+
739
+ self->hide_count = new_count;
666
740
  }
667
741
 
668
742
  bool
@@ -979,6 +1053,32 @@ namespace Reflex
979
1053
  return -1;
980
1054
  }
981
1055
 
1056
+ void
1057
+ View::make_body ()
1058
+ {
1059
+ Body* b = body();
1060
+ if (!b) return;
1061
+
1062
+ b->clear_fixtures();
1063
+
1064
+ const Point& size = frame().size();
1065
+ b->add_box(size.x, size.y);
1066
+ }
1067
+
1068
+ void
1069
+ View::clear_body ()
1070
+ {
1071
+ Body* body = self->pbody.get();
1072
+ if (!body) return;
1073
+
1074
+ World* world = self->parent_world();
1075
+ if (!world)
1076
+ invalid_state_error(__FILE__, __LINE__);
1077
+
1078
+ world->destroy_body(body);
1079
+ self->pbody.reset();
1080
+ }
1081
+
982
1082
  void
983
1083
  View::set_name (const char* name)
984
1084
  {
@@ -1224,15 +1324,100 @@ namespace Reflex
1224
1324
  return const_cast<View*>(this)->window();
1225
1325
  }
1226
1326
 
1227
- Rays::Point
1228
- View::from_parent (const Rays::Point& point) const
1327
+ Body*
1328
+ View::body ()
1329
+ {
1330
+ if (!*this)
1331
+ invalid_state_error(__FILE__, __LINE__);
1332
+
1333
+ return self->body(this);
1334
+ }
1335
+
1336
+ const Body*
1337
+ View::body () const
1338
+ {
1339
+ return const_cast<View*>(this)->body();
1340
+ }
1341
+
1342
+ void
1343
+ View::set_density (float density)
1344
+ {
1345
+ Body* b = body();
1346
+ if (!b)
1347
+ invalid_state_error(__FILE__, __LINE__);
1348
+
1349
+ b->set_density(density);
1350
+ }
1351
+
1352
+ void
1353
+ View::set_friction (float friction)
1354
+ {
1355
+ Body* b = body();
1356
+ if (!b)
1357
+ invalid_state_error(__FILE__, __LINE__);
1358
+
1359
+ b->set_friction(friction);
1360
+ }
1361
+
1362
+ void
1363
+ View::set_restitution (float restitution)
1364
+ {
1365
+ Body* b = body();
1366
+ if (!b)
1367
+ invalid_state_error(__FILE__, __LINE__);
1368
+
1369
+ b->set_restitution(restitution);
1370
+ }
1371
+
1372
+ void
1373
+ View::set_gravity (float x, float y)
1374
+ {
1375
+ set_gravity(Point(x, y));
1376
+ }
1377
+
1378
+ void
1379
+ View::set_gravity (const Point& vector)
1380
+ {
1381
+ World* world = self->world();
1382
+ if (!world)
1383
+ invalid_state_error(__FILE__, __LINE__);
1384
+
1385
+ world->set_gravity(vector);
1386
+ }
1387
+
1388
+ Point
1389
+ View::gravity () const
1390
+ {
1391
+ World* world = self->pworld.get();
1392
+ return world ? world->gravity() : 0;
1393
+ }
1394
+
1395
+ void
1396
+ View::set_debug (bool state)
1397
+ {
1398
+ World* world = self->world();
1399
+ if (!world)
1400
+ invalid_state_error(__FILE__, __LINE__);
1401
+
1402
+ world->set_debug(state);
1403
+ }
1404
+
1405
+ bool
1406
+ View::is_debug () const
1407
+ {
1408
+ World* world = self->pworld.get();
1409
+ return world ? world->is_debug() : false;
1410
+ }
1411
+
1412
+ Point
1413
+ View::from_parent (const Point& point) const
1229
1414
  {
1230
1415
  not_implemented_error(__FILE__, __LINE__);
1231
1416
  return 0;
1232
1417
  }
1233
1418
 
1234
- Rays::Point
1235
- View::to_parent (const Rays::Point& point) const
1419
+ Point
1420
+ View::to_parent (const Point& point) const
1236
1421
  {
1237
1422
  not_implemented_error(__FILE__, __LINE__);
1238
1423
  return 0;
@@ -1247,22 +1432,22 @@ namespace Reflex
1247
1432
  return p;
1248
1433
  }
1249
1434
 
1250
- Rays::Point
1251
- View::to_window (const Rays::Point& point) const
1435
+ Point
1436
+ View::to_window (const Point& point) const
1252
1437
  {
1253
1438
  not_implemented_error(__FILE__, __LINE__);
1254
1439
  return 0;
1255
1440
  }
1256
1441
 
1257
- Rays::Point
1258
- View::from_screen (const Rays::Point& point) const
1442
+ Point
1443
+ View::from_screen (const Point& point) const
1259
1444
  {
1260
1445
  not_implemented_error(__FILE__, __LINE__);
1261
1446
  return 0;
1262
1447
  }
1263
1448
 
1264
- Rays::Point
1265
- View::to_screen (const Rays::Point& point) const
1449
+ Point
1450
+ View::to_screen (const Point& point) const
1266
1451
  {
1267
1452
  not_implemented_error(__FILE__, __LINE__);
1268
1453
  return 0;
@@ -1271,7 +1456,8 @@ namespace Reflex
1271
1456
  void
1272
1457
  View::on_attach (Event* e)
1273
1458
  {
1274
- resize_to_fit();
1459
+ if (!e)
1460
+ argument_error(__FILE__, __LINE__);
1275
1461
  }
1276
1462
 
1277
1463
  void
@@ -1281,6 +1467,20 @@ namespace Reflex
1281
1467
  argument_error(__FILE__, __LINE__);
1282
1468
  }
1283
1469
 
1470
+ void
1471
+ View::on_show (Event* e)
1472
+ {
1473
+ if (!e)
1474
+ argument_error(__FILE__, __LINE__);
1475
+ }
1476
+
1477
+ void
1478
+ View::on_hide (Event* e)
1479
+ {
1480
+ if (!e)
1481
+ argument_error(__FILE__, __LINE__);
1482
+ }
1483
+
1284
1484
  void
1285
1485
  View::on_update (UpdateEvent* e)
1286
1486
  {
data/src/window.cpp CHANGED
@@ -1,8 +1,6 @@
1
1
  #include "reflex/window.h"
2
2
 
3
3
 
4
- #include <rays/bounds.h>
5
- #include <rays/painter.h>
6
4
  #include "reflex/view.h"
7
5
  #include "reflex/exception.h"
8
6
 
@@ -11,11 +9,6 @@ namespace Reflex
11
9
  {
12
10
 
13
11
 
14
- void update_view_tree (View* v, const UpdateEvent& e);
15
-
16
- void draw_view_tree (View* v, const DrawEvent& e, const Point& offset, const Bounds& clip);
17
-
18
-
19
12
  const View*
20
13
  Window::root () const
21
14
  {
@@ -34,36 +27,53 @@ namespace Reflex
34
27
  return const_cast<Window*>(this)->painter();
35
28
  }
36
29
 
37
- Rays::Point
38
- Window::from_screen (const Rays::Point& point) const
30
+ Point
31
+ Window::from_screen (const Point& point) const
39
32
  {
40
33
  not_implemented_error(__FILE__, __LINE__);
41
34
  return 0;
42
35
  }
43
36
 
44
- Rays::Point
45
- Window::to_screen (const Rays::Point& point) const
37
+ Point
38
+ Window::to_screen (const Point& point) const
46
39
  {
47
40
  not_implemented_error(__FILE__, __LINE__);
48
41
  return 0;
49
42
  }
50
43
 
51
44
  void
52
- Window::on_update (UpdateEvent* e)
45
+ Window::on_show (Event* e)
53
46
  {
54
47
  if (!e)
55
48
  argument_error(__FILE__, __LINE__);
49
+ }
56
50
 
57
- update_view_tree(root(), *e);
51
+ void
52
+ Window::on_hide (Event* e)
53
+ {
54
+ if (!e)
55
+ argument_error(__FILE__, __LINE__);
58
56
  }
59
57
 
60
58
  void
61
- Window::on_draw (DrawEvent* e)
59
+ Window::on_close (Event* e)
62
60
  {
63
61
  if (!e)
64
62
  argument_error(__FILE__, __LINE__);
63
+ }
65
64
 
66
- draw_view_tree(root(), *e, 0, frame().dup().move_to(0));
65
+ void
66
+ Window::on_update (UpdateEvent* e)
67
+ {
68
+ if (!e)
69
+ argument_error(__FILE__, __LINE__);
70
+ }
71
+
72
+ void
73
+ Window::on_draw (DrawEvent* e)
74
+ {
75
+ if (!e)
76
+ argument_error(__FILE__, __LINE__);
67
77
  }
68
78
 
69
79
  void
data/src/world.cpp ADDED
@@ -0,0 +1,206 @@
1
+ #include "world.h"
2
+
3
+
4
+ #include <assert.h>
5
+ #include <boost/scoped_array.hpp>
6
+ #include <Box2D/Dynamics/b2Body.h>
7
+ #include <Box2D/Common/b2Draw.h>
8
+ #include "xot/util.h"
9
+ #include "reflex/body.h"
10
+
11
+
12
+ namespace Reflex
13
+ {
14
+
15
+
16
+ class DebugDraw : public b2Draw
17
+ {
18
+
19
+ public:
20
+
21
+ DebugDraw (float scale)
22
+ : scale(scale), painter(NULL)
23
+ {
24
+ SetFlags(e_shapeBit);
25
+ }
26
+
27
+ void begin (Painter* painter)
28
+ {
29
+ this->painter = painter;
30
+ painter->push_attr();
31
+ }
32
+
33
+ void end ()
34
+ {
35
+ painter->pop_attr();
36
+ painter = NULL;
37
+ }
38
+
39
+ void DrawPolygon (
40
+ const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
41
+ {
42
+ assert(painter);
43
+
44
+ painter->no_fill();
45
+ painter->set_stroke(color.r, color.g, color.b, color.a * 0.5);
46
+
47
+ boost::scoped_array<Coord2> points(new Coord2[vertexCount]);
48
+ for (int i = 0; i < vertexCount; ++i)
49
+ points[i] = to_coord2(vertices[i], scale);
50
+
51
+ painter->polygon(&points[0], vertexCount);
52
+ }
53
+
54
+ void DrawSolidPolygon (
55
+ const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
56
+ {
57
+ assert(painter);
58
+
59
+ painter->set_fill(color.r, color.g, color.b, color.a * 0.5);
60
+ painter->no_stroke();
61
+
62
+ boost::scoped_array<Coord2> points(new Coord2[vertexCount]);
63
+ for (int i = 0; i < vertexCount; ++i)
64
+ points[i] = to_coord2(vertices[i], scale);
65
+
66
+ painter->polygon(&points[0], vertexCount);
67
+ }
68
+
69
+ void DrawCircle (
70
+ const b2Vec2& center, float32 radius, const b2Color& color)
71
+ {
72
+ assert(painter);
73
+
74
+ painter->no_fill();
75
+ painter->set_stroke(color.r, color.g, color.b, color.a * 0.5);
76
+ painter->ellipse(Point(center.x * scale, center.y * scale), radius * scale);
77
+ }
78
+
79
+ void DrawSolidCircle (
80
+ const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
81
+ {
82
+ assert(painter);
83
+
84
+ painter->set_fill(color.r, color.g, color.b, color.a * 0.5);
85
+ painter->no_stroke();
86
+ painter->ellipse(Point(center.x * scale, center.y * scale), radius * scale);
87
+ }
88
+
89
+ void DrawSegment (
90
+ const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
91
+ {
92
+ assert(painter);
93
+
94
+ painter->no_fill();
95
+ painter->set_stroke(color.r, color.g, color.b, color.a * 0.5);
96
+ painter->line(p1.x * scale, p1.y * scale, p2.x * scale, p2.y * scale);
97
+ }
98
+
99
+ void DrawTransform (const b2Transform& transform)
100
+ {
101
+ assert(painter);
102
+ }
103
+
104
+ private:
105
+
106
+ float scale;
107
+
108
+ Painter* painter;
109
+
110
+ };// DebugDraw
111
+
112
+
113
+ World::World (float scale)
114
+ : world(b2Vec2(0, 0)), scale(scale), wall(NULL), debug_draw(NULL)
115
+ {
116
+ wall = create_body();
117
+ }
118
+
119
+ World::~World ()
120
+ {
121
+ delete debug_draw;
122
+ }
123
+
124
+ void
125
+ World::step (float dt)
126
+ {
127
+ world.Step(dt, 6, 2);
128
+ }
129
+
130
+ Body*
131
+ World::create_body (const Point& position, float degree)
132
+ {
133
+ b2BodyDef def;
134
+ def.position = to_box2d(position, scale);
135
+ def.angle = Xot::deg2rad(degree);
136
+ return new Body(world.CreateBody(&def), scale);
137
+ }
138
+
139
+ void
140
+ World::destroy_body (Body* body)
141
+ {
142
+ world.DestroyBody((b2Body*) body->handle);
143
+ }
144
+
145
+ void
146
+ World::resize (coord width, coord height)
147
+ {
148
+ wall->clear_fixtures();
149
+
150
+ coord w = width / scale;
151
+ coord h = height / scale;
152
+ const Point points[] = {
153
+ Point(0, 0),
154
+ Point(0, h),
155
+ Point(w, h),
156
+ Point(w, 0),
157
+ };
158
+ wall->add_edge(points, 4, true);
159
+ }
160
+
161
+ void
162
+ World::draw (Painter* painter)
163
+ {
164
+ if (!debug_draw) return;
165
+
166
+ debug_draw->begin(painter);
167
+ world.DrawDebugData();
168
+ debug_draw->end();
169
+ }
170
+
171
+ void
172
+ World::set_gravity (const Point& gravity)
173
+ {
174
+ world.SetGravity(to_box2d(gravity, scale));
175
+ }
176
+
177
+ Point
178
+ World::gravity () const
179
+ {
180
+ return to_point(world.GetGravity(), scale);
181
+ }
182
+
183
+ void
184
+ World::set_debug (bool state)
185
+ {
186
+ if (state == is_debug()) return;
187
+
188
+ if (!debug_draw)
189
+ debug_draw = new DebugDraw(scale);
190
+ else
191
+ {
192
+ delete debug_draw;
193
+ debug_draw = NULL;
194
+ }
195
+
196
+ world.SetDebugDraw(debug_draw);
197
+ }
198
+
199
+ bool
200
+ World::is_debug () const
201
+ {
202
+ return debug_draw;
203
+ }
204
+
205
+
206
+ }// Reflex