reflexion 0.1.8 → 0.1.9

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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/reflex/application.cpp +5 -1
  3. data/.doc/ext/reflex/arc_shape.cpp +89 -0
  4. data/.doc/ext/reflex/body.cpp +91 -12
  5. data/.doc/ext/reflex/contact_event.cpp +90 -0
  6. data/.doc/ext/reflex/ellipse_shape.cpp +89 -0
  7. data/.doc/ext/reflex/image_view.cpp +0 -16
  8. data/.doc/ext/reflex/native.cpp +18 -6
  9. data/.doc/ext/reflex/rect_shape.cpp +83 -0
  10. data/.doc/ext/reflex/shape_view.cpp +153 -0
  11. data/.doc/ext/reflex/view.cpp +63 -26
  12. data/.doc/ext/reflex/window.cpp +5 -1
  13. data/VERSION +1 -1
  14. data/ext/reflex/application.cpp +6 -2
  15. data/ext/reflex/arc_shape.cpp +94 -0
  16. data/ext/reflex/body.cpp +101 -13
  17. data/ext/reflex/contact_event.cpp +95 -0
  18. data/ext/reflex/ellipse_shape.cpp +94 -0
  19. data/ext/reflex/image_view.cpp +0 -18
  20. data/ext/reflex/native.cpp +18 -6
  21. data/ext/reflex/rect_shape.cpp +86 -0
  22. data/ext/reflex/shape_view.cpp +161 -0
  23. data/ext/reflex/view.cpp +71 -30
  24. data/ext/reflex/window.cpp +5 -1
  25. data/include/reflex/body.h +42 -12
  26. data/include/reflex/event.h +27 -1
  27. data/include/reflex/fixture.h +6 -5
  28. data/include/reflex/image_view.h +5 -5
  29. data/include/reflex/ruby/application.h +27 -6
  30. data/include/reflex/ruby/event.h +11 -0
  31. data/include/reflex/ruby/shape_view.h +96 -0
  32. data/include/reflex/ruby/view.h +60 -5
  33. data/include/reflex/ruby/window.h +12 -3
  34. data/include/reflex/shape_view.h +146 -0
  35. data/include/reflex/view.h +17 -5
  36. data/lib/reflex/application.rb +9 -9
  37. data/lib/reflex/body.rb +2 -0
  38. data/lib/reflex/contact_event.rb +38 -0
  39. data/lib/reflex/image_view.rb +1 -1
  40. data/lib/reflex/shape_view.rb +25 -0
  41. data/lib/reflex/view.rb +19 -9
  42. data/lib/reflex/window.rb +11 -10
  43. data/lib/reflex.rb +15 -13
  44. data/lib/reflexion.rb +25 -18
  45. data/samples/osx/hello/hello/main.cpp +6 -0
  46. data/samples/physics.rb +22 -12
  47. data/samples/reflexion/breakout.rb +52 -0
  48. data/samples/reflexion/hello.rb +5 -7
  49. data/samples/reflexion/paint.rb +10 -11
  50. data/samples/reflexion/physics.rb +28 -0
  51. data/samples/reflexion/pulse.rb +10 -8
  52. data/samples/shapes.rb +2 -2
  53. data/src/body.cpp +241 -40
  54. data/src/event.cpp +32 -2
  55. data/src/shape_view.cpp +306 -0
  56. data/src/view.cpp +232 -66
  57. data/src/world.cpp +110 -30
  58. data/src/world.h +61 -14
  59. metadata +24 -7
  60. data/lib/reflex/arc_shape.rb +0 -20
  61. data/lib/reflex/ellipse_shape.rb +0 -20
  62. data/lib/reflex/line_shape.rb +0 -20
  63. data/lib/reflex/rect_shape.rb +0 -20
  64. data/lib/reflex/shape.rb +0 -34
data/src/view.cpp CHANGED
@@ -4,6 +4,7 @@
4
4
  #include <assert.h>
5
5
  #include <algorithm>
6
6
  #include <boost/scoped_ptr.hpp>
7
+ #include "xot/util.h"
7
8
  #include "reflex/window.h"
8
9
  #include "reflex/body.h"
9
10
  #include "reflex/exception.h"
@@ -30,6 +31,8 @@ namespace Reflex
30
31
 
31
32
  Bounds frame;
32
33
 
34
+ float angle, scale;
35
+
33
36
  boost::scoped_ptr<Selector> pselector;
34
37
 
35
38
  boost::scoped_ptr<Point> pscroll;
@@ -51,7 +54,7 @@ namespace Reflex
51
54
  uint flags;
52
55
 
53
56
  Data ()
54
- : window(NULL), parent(NULL),
57
+ : window(NULL), parent(NULL), angle(0), scale(1),
55
58
  capture(CAPTURE_NONE), hide_count(0), flags(0)
56
59
  {
57
60
  }
@@ -84,16 +87,22 @@ namespace Reflex
84
87
  return *pstyles;
85
88
  }
86
89
 
87
- World* world ()
90
+ World* world (View* this_)
88
91
  {
89
- if (!pworld) pworld.reset(new World());
92
+ assert(this_);
93
+
94
+ if (!pworld)
95
+ {
96
+ pworld.reset(new World(this_));
97
+ resize_world(this_);
98
+ }
90
99
  return pworld.get();
91
100
  }
92
101
 
93
102
  World* parent_world ()
94
103
  {
95
104
  if (!parent) return NULL;
96
- return parent->self->world();
105
+ return parent->self->world(parent);
97
106
  }
98
107
 
99
108
  Body* body (View* this_)
@@ -102,12 +111,34 @@ namespace Reflex
102
111
  {
103
112
  World* world = parent_world();
104
113
  if (!world) return NULL;
105
- pbody.reset(world->create_body(frame.position()));
114
+ pbody.reset(world->create_body(this_, frame.position()));
106
115
  this_->make_body();
107
116
  }
108
117
  return pbody.get();
109
118
  }
110
119
 
120
+ void resize_world (View* this_)
121
+ {
122
+ assert(this_);
123
+
124
+ World* w = pworld.get();
125
+ if (!w) return;
126
+
127
+ w->resize(frame.width, frame.height);
128
+ this_->redraw();
129
+ }
130
+
131
+ void update_body (View* this_)
132
+ {
133
+ assert(this_);
134
+
135
+ Body* b = pbody.get();
136
+ if (!b) return;
137
+
138
+ b->set_transform(frame.x, frame.y, angle);
139
+ this_->redraw();
140
+ }
141
+
111
142
  };// View::Data
112
143
 
113
144
 
@@ -162,6 +193,43 @@ namespace Reflex
162
193
  set_window(this_, parent_ ? parent_->window() : NULL);
163
194
  }
164
195
 
196
+ static void reflow_children (View* parent, const FrameEvent* event = NULL);
197
+
198
+ static void
199
+ update_view_frame (View* view, const Bounds& frame, float angle, bool update_body)
200
+ {
201
+ if (!view)
202
+ argument_error(__FILE__, __LINE__);
203
+
204
+ if (!*view)
205
+ invalid_state_error(__FILE__, __LINE__);
206
+
207
+ View::Data* self = view->self.get();
208
+ if (frame == self->frame && angle == self->angle) return;
209
+
210
+ FrameEvent event(frame, self->frame, angle, self->angle);
211
+ self->frame = frame;
212
+ self->angle = angle;
213
+
214
+ bool move = event.is_move(), rotate = event.is_rotate();
215
+
216
+ if (move) view->on_move(&event);
217
+ if (rotate) view->on_rotate(&event);
218
+
219
+ if (update_body && (move || rotate))
220
+ self->update_body(view);
221
+
222
+ if (event.is_resize())
223
+ {
224
+ self->resize_world(view);
225
+ view->on_resize(&event);
226
+
227
+ reflow_children(view, &event);
228
+ }
229
+
230
+ view->redraw();
231
+ }
232
+
165
233
  static void
166
234
  update_world (View* view, float dt)
167
235
  {
@@ -173,7 +241,7 @@ namespace Reflex
173
241
  {
174
242
  Bounds b = view->frame();
175
243
  b.move_to(body->position());
176
- view->set_frame(b);
244
+ update_view_frame(view, b, body->angle(), false);
177
245
  }
178
246
  }
179
247
 
@@ -209,29 +277,41 @@ namespace Reflex
209
277
  if (view->hidden()) return;
210
278
 
211
279
  DrawEvent e = event;
212
- e.painter->push_matrix();
213
- e.painter->push_attr();
280
+ Painter* p = e.painter;
281
+ Body* b = view->self->pbody.get();
282
+
283
+ p->push_matrix();
284
+ p->push_attr();
285
+
286
+ Bounds frame = view->frame();
287
+ Point pos = frame.position() - view->scroll();
288
+ p->translate(pos);
214
289
 
215
- Bounds b = view->frame();
216
- Point p = b.position() - view->scroll();
217
- e.painter->translate(p);
290
+ float angle = view->self->angle;
291
+ if (angle != 0) p->rotate(angle);
218
292
 
219
- p += offset;
220
- Bounds c = clip & b.move_to(p);
221
- e.painter->set_clip(c);
293
+ float scale = view->self->scale;
294
+ if (scale != 1) p->scale(scale);
295
+
296
+ pos += offset;
297
+ Bounds clip2 = clip & frame.move_to(pos);
298
+ if (b)
299
+ p->no_clip();
300
+ else
301
+ p->set_clip(clip2);
222
302
 
223
303
  e.view = view;
224
- e.bounds = b.move_to(0, 0, b.z);
304
+ e.bounds = frame.move_to(0, 0, frame.z);
225
305
  view->on_draw(&e);
226
306
 
227
307
  View::child_iterator end = view->child_end();
228
308
  for (View::child_iterator it = view->child_begin(); it != end; ++it)
229
- draw_view_tree(it->get(), e, p, c);
309
+ draw_view_tree(it->get(), e, pos, clip2);
230
310
 
231
- draw_world(view, e.painter);
311
+ draw_world(view, p);
232
312
 
233
- e.painter->pop_attr();
234
- e.painter->pop_matrix();
313
+ p->pop_attr();
314
+ p->pop_matrix();
235
315
  }
236
316
  #if 0
237
317
  void
@@ -554,7 +634,7 @@ namespace Reflex
554
634
  }
555
635
  #endif
556
636
  static void
557
- reflow_children (View* parent, const FrameEvent* event = NULL)
637
+ reflow_children (View* parent, const FrameEvent* event)
558
638
  {
559
639
  if (!parent)
560
640
  argument_error(__FILE__, __LINE__);
@@ -797,6 +877,8 @@ namespace Reflex
797
877
  if (child->parent() != this)
798
878
  invalid_state_error(__FILE__, __LINE__);
799
879
 
880
+ child->clear_body();
881
+
800
882
  set_parent(child, NULL);
801
883
  self->pchildren->erase(it);
802
884
  }
@@ -1199,33 +1281,34 @@ namespace Reflex
1199
1281
 
1200
1282
  void
1201
1283
  View::set_frame (const Bounds& frame)
1284
+ {
1285
+ update_view_frame(this, frame, self->angle, true);
1286
+ }
1287
+
1288
+ const Bounds&
1289
+ View::frame () const
1202
1290
  {
1203
1291
  if (!*this)
1204
1292
  invalid_state_error(__FILE__, __LINE__);
1205
1293
 
1206
- const Bounds& current = self->frame;
1207
- if (frame == current) return;
1208
-
1209
- self->frame = frame;
1210
-
1211
- FrameEvent event(
1212
- self->frame,
1213
- frame.x - current.x, frame.y - current.y,
1214
- frame.w - current.w, frame.h - current.h);
1215
- if (event.is_move()) on_move(&event);
1216
- if (event.is_resize()) on_resize(&event);
1294
+ return self->frame;
1295
+ }
1217
1296
 
1218
- reflow_children(this, &event);
1219
- redraw();
1297
+ #if 0
1298
+ void
1299
+ View::set_angle (float degree)
1300
+ {
1301
+ update_view_frame(this, self->frame, degree, true);
1220
1302
  }
1303
+ #endif
1221
1304
 
1222
- const Bounds&
1223
- View::frame () const
1305
+ float
1306
+ View::angle () const
1224
1307
  {
1225
1308
  if (!*this)
1226
1309
  invalid_state_error(__FILE__, __LINE__);
1227
1310
 
1228
- return self->frame;
1311
+ return self->angle;
1229
1312
  }
1230
1313
 
1231
1314
  static const Point ZERO_SCROLL;
@@ -1233,6 +1316,9 @@ namespace Reflex
1233
1316
  void
1234
1317
  View::scroll_to (coord x, coord y, coord z)
1235
1318
  {
1319
+ if (!*this)
1320
+ invalid_state_error(__FILE__, __LINE__);
1321
+
1236
1322
  Point old = self->scroll();
1237
1323
  self->scroll().reset(x, y, z);
1238
1324
  ScrollEvent e(x, y, z, x - old.x, y - old.y, z - old.z);
@@ -1262,6 +1348,9 @@ namespace Reflex
1262
1348
  const Point&
1263
1349
  View::scroll () const
1264
1350
  {
1351
+ if (!*this)
1352
+ invalid_state_error(__FILE__, __LINE__);
1353
+
1265
1354
  if (self->pscroll)
1266
1355
  return self->scroll();
1267
1356
  else
@@ -1271,6 +1360,9 @@ namespace Reflex
1271
1360
  void
1272
1361
  View::set_capture (uint types)
1273
1362
  {
1363
+ if (!*this)
1364
+ invalid_state_error(__FILE__, __LINE__);
1365
+
1274
1366
  if (types == self->capture) return;
1275
1367
 
1276
1368
  uint old = self->capture;
@@ -1291,6 +1383,9 @@ namespace Reflex
1291
1383
  uint
1292
1384
  View::capture () const
1293
1385
  {
1386
+ if (!*this)
1387
+ invalid_state_error(__FILE__, __LINE__);
1388
+
1294
1389
  return self->capture;
1295
1390
  }
1296
1391
 
@@ -1339,34 +1434,43 @@ namespace Reflex
1339
1434
  return const_cast<View*>(this)->body();
1340
1435
  }
1341
1436
 
1342
- void
1343
- View::set_density (float density)
1437
+ float
1438
+ View::meter2pixel (float meter, bool create_world)
1344
1439
  {
1345
- Body* b = body();
1346
- if (!b)
1440
+ if (!*this)
1347
1441
  invalid_state_error(__FILE__, __LINE__);
1348
1442
 
1349
- b->set_density(density);
1350
- }
1443
+ Body* body = self->pbody.get();
1444
+ if (body)
1445
+ return body->meter2pixel(meter);
1351
1446
 
1352
- void
1353
- View::set_friction (float friction)
1354
- {
1355
- Body* b = body();
1356
- if (!b)
1447
+ World* world = self->pworld.get();
1448
+ if (world)
1449
+ return world->meter2pixel(meter);
1450
+
1451
+ View* parent_ = NULL;
1452
+ World* parent_world = NULL;
1453
+ if (
1454
+ (parent_ = parent()) &&
1455
+ (parent_world = parent_->self->pworld.get()))
1456
+ {
1457
+ return parent_world->meter2pixel(meter);
1458
+ }
1459
+
1460
+ if (!create_world)
1357
1461
  invalid_state_error(__FILE__, __LINE__);
1358
1462
 
1359
- b->set_friction(friction);
1463
+ World* new_world = self->world(this);
1464
+ if (!new_world)
1465
+ invalid_state_error(__FILE__, __LINE__);
1466
+
1467
+ return new_world->meter2pixel(meter);
1360
1468
  }
1361
1469
 
1362
- void
1363
- View::set_restitution (float restitution)
1470
+ float
1471
+ View::meter2pixel (float meter) const
1364
1472
  {
1365
- Body* b = body();
1366
- if (!b)
1367
- invalid_state_error(__FILE__, __LINE__);
1368
-
1369
- b->set_restitution(restitution);
1473
+ return const_cast<View*>(this)->meter2pixel(meter, false);
1370
1474
  }
1371
1475
 
1372
1476
  void
@@ -1378,35 +1482,62 @@ namespace Reflex
1378
1482
  void
1379
1483
  View::set_gravity (const Point& vector)
1380
1484
  {
1381
- World* world = self->world();
1382
- if (!world)
1485
+ if (!*this)
1486
+ invalid_state_error(__FILE__, __LINE__);
1487
+
1488
+ World* w = self->world(this);
1489
+ if (!w)
1383
1490
  invalid_state_error(__FILE__, __LINE__);
1384
1491
 
1385
- world->set_gravity(vector);
1492
+ w->set_gravity(vector);
1386
1493
  }
1387
1494
 
1388
1495
  Point
1389
1496
  View::gravity () const
1390
1497
  {
1391
- World* world = self->pworld.get();
1392
- return world ? world->gravity() : 0;
1498
+ if (!*this)
1499
+ invalid_state_error(__FILE__, __LINE__);
1500
+
1501
+ World* w = self->pworld.get();
1502
+ return w ? w->gravity() : 0;
1503
+ }
1504
+
1505
+ Body*
1506
+ View::wall ()
1507
+ {
1508
+ if (!*this)
1509
+ invalid_state_error(__FILE__, __LINE__);
1510
+
1511
+ return self->world(this)->wall();
1512
+ }
1513
+
1514
+ const Body*
1515
+ View::wall () const
1516
+ {
1517
+ return const_cast<View*>(this)->wall();
1393
1518
  }
1394
1519
 
1395
1520
  void
1396
1521
  View::set_debug (bool state)
1397
1522
  {
1398
- World* world = self->world();
1399
- if (!world)
1523
+ if (!*this)
1400
1524
  invalid_state_error(__FILE__, __LINE__);
1401
1525
 
1402
- world->set_debug(state);
1526
+ World* w = self->world(this);
1527
+ if (!w)
1528
+ invalid_state_error(__FILE__, __LINE__);
1529
+
1530
+ w->set_debug(state);
1403
1531
  }
1404
1532
 
1405
1533
  bool
1406
- View::is_debug () const
1534
+ View::debugging () const
1407
1535
  {
1408
- World* world = self->pworld.get();
1409
- return world ? world->is_debug() : false;
1536
+ if (!*this)
1537
+ invalid_state_error(__FILE__, __LINE__);
1538
+
1539
+ World* w = self->pworld.get();
1540
+ return w ? w->debugging() : false;
1410
1541
  }
1411
1542
 
1412
1543
  Point
@@ -1509,6 +1640,13 @@ namespace Reflex
1509
1640
  argument_error(__FILE__, __LINE__);
1510
1641
  }
1511
1642
 
1643
+ void
1644
+ View::on_rotate (FrameEvent* e)
1645
+ {
1646
+ if (!e)
1647
+ argument_error(__FILE__, __LINE__);
1648
+ }
1649
+
1512
1650
  void
1513
1651
  View::on_scroll (ScrollEvent* e)
1514
1652
  {
@@ -1610,6 +1748,34 @@ namespace Reflex
1610
1748
  argument_error(__FILE__, __LINE__);
1611
1749
  }
1612
1750
 
1751
+ void
1752
+ View::on_contact (ContactEvent* e)
1753
+ {
1754
+ if (!e)
1755
+ argument_error(__FILE__, __LINE__);
1756
+
1757
+ switch (e->type)
1758
+ {
1759
+ case ContactEvent::BEGIN: on_contact_begin(e); break;
1760
+ case ContactEvent::END: on_contact_end(e); break;
1761
+ case ContactEvent::NONE: break;
1762
+ }
1763
+ }
1764
+
1765
+ void
1766
+ View::on_contact_begin (ContactEvent* e)
1767
+ {
1768
+ if (!e)
1769
+ argument_error(__FILE__, __LINE__);
1770
+ }
1771
+
1772
+ void
1773
+ View::on_contact_end (ContactEvent* e)
1774
+ {
1775
+ if (!e)
1776
+ argument_error(__FILE__, __LINE__);
1777
+ }
1778
+
1613
1779
  View::operator bool () const
1614
1780
  {
1615
1781
  return true;