reflexion 0.1.3 → 0.1.4

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 (88) hide show
  1. data/.doc/ext/reflex/application.cpp +35 -76
  2. data/.doc/ext/reflex/defs.cpp +8 -0
  3. data/.doc/ext/reflex/key.cpp +38 -43
  4. data/.doc/ext/reflex/native.cpp +6 -4
  5. data/.doc/ext/reflex/points.cpp +47 -52
  6. data/.doc/ext/reflex/reflex.cpp +12 -13
  7. data/.doc/ext/reflex/view.cpp +242 -0
  8. data/.doc/ext/reflex/window.cpp +87 -178
  9. data/.gitignore +14 -0
  10. data/Rakefile +6 -31
  11. data/VERSION +1 -1
  12. data/examples/hello/.gitignore +2 -0
  13. data/examples/ruby/app.rb +2 -2
  14. data/examples/ruby/checker.rb +3 -3
  15. data/examples/ruby/fps.rb +14 -14
  16. data/examples/ruby/grid.rb +65 -0
  17. data/examples/ruby/hello.rb +19 -7
  18. data/examples/ruby/key.rb +4 -4
  19. data/examples/ruby/shapes.rb +6 -6
  20. data/examples/ruby/text.rb +20 -17
  21. data/examples/ruby/views.rb +88 -0
  22. data/examples/ruby/visuals.rb +27 -0
  23. data/ext/reflex/application.cpp +36 -76
  24. data/ext/reflex/defs.cpp +8 -0
  25. data/ext/reflex/defs.h +1 -18
  26. data/ext/reflex/extconf.rb +16 -8
  27. data/ext/reflex/key.cpp +39 -43
  28. data/ext/reflex/native.cpp +6 -4
  29. data/ext/reflex/points.cpp +48 -52
  30. data/ext/reflex/reflex.cpp +12 -13
  31. data/ext/reflex/view.cpp +260 -0
  32. data/ext/reflex/window.cpp +89 -178
  33. data/include/reflex/application.h +14 -7
  34. data/include/reflex/defs.h +8 -6
  35. data/include/reflex/exception.h +1 -1
  36. data/include/reflex/ruby/application.h +31 -10
  37. data/include/reflex/ruby/key.h +3 -3
  38. data/include/reflex/ruby/points.h +3 -3
  39. data/include/reflex/ruby/view.h +106 -0
  40. data/include/reflex/ruby/window.h +83 -12
  41. data/include/reflex/ruby.h +3 -2
  42. data/include/reflex/view.h +103 -0
  43. data/include/reflex/window.h +43 -18
  44. data/include/reflex.h +2 -1
  45. data/lib/reflex/application.rb +8 -7
  46. data/lib/reflex/autoinit.rb +1 -1
  47. data/lib/reflex/bitmap.rb +13 -0
  48. data/lib/reflex/bounds.rb +2 -122
  49. data/lib/reflex/ext.rb +5 -0
  50. data/lib/reflex/helpers.rb +36 -31
  51. data/lib/reflex/image.rb +13 -0
  52. data/lib/reflex/module.rb +9 -2
  53. data/lib/reflex/painter.rb +13 -0
  54. data/lib/reflex/point.rb +3 -59
  55. data/lib/reflex/reflex.rb +1 -1
  56. data/lib/reflex/texture.rb +13 -0
  57. data/lib/reflex/view.rb +33 -0
  58. data/lib/reflex/visuals/string.rb +53 -0
  59. data/lib/reflex/window.rb +18 -43
  60. data/lib/reflex.rb +3 -3
  61. data/reflex.gemspec +16 -42
  62. data/src/cocoa/application.mm +17 -23
  63. data/src/cocoa/applicationdata.h +3 -9
  64. data/src/cocoa/cocoaapplication.h +6 -4
  65. data/src/cocoa/cocoaapplication.mm +61 -19
  66. data/src/cocoa/cocoawindow.h +7 -5
  67. data/src/cocoa/cocoawindow.mm +109 -50
  68. data/src/cocoa/defs.mm +5 -2
  69. data/src/cocoa/window.mm +71 -41
  70. data/src/cocoa/windowdata.h +14 -9
  71. data/src/defs.cpp +1 -1
  72. data/src/exception.cpp +3 -18
  73. data/src/helpers.h +12 -0
  74. data/src/reflex.cpp +11 -5
  75. data/src/view.cpp +326 -0
  76. data/src/win32/application.cpp +7 -8
  77. data/src/win32/defs.h +1 -1
  78. data/src/win32/window.cpp +137 -41
  79. data/src/window.cpp +38 -1
  80. data/test/helpers.rb +2 -5
  81. data/test/test_application.rb +17 -0
  82. data/test/test_reflex.rb +4 -2
  83. data/test/test_view.rb +74 -0
  84. data/test/test_window.rb +33 -2
  85. metadata +157 -97
  86. data/include/reflex/helpers.h +0 -32
  87. data/test/test_bounds.rb +0 -163
  88. data/test/test_point.rb +0 -81
data/src/cocoa/window.mm CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
 
5
5
  #import <Cocoa/Cocoa.h>
6
+ #include <rays/bounds.h>
6
7
  #include "windowdata.h"
7
8
  #import "cocoawindow.h"
8
9
 
@@ -11,38 +12,32 @@ namespace Reflex
11
12
  {
12
13
 
13
14
 
15
+ void set_owner_window (View* view, Window* owner);
16
+
17
+
14
18
  Window::Window ()
15
19
  {
16
- self->this_ = this;
17
- self->self_ = [[CocoaWindow alloc] initWithWindowData: self];
20
+ [[[[CocoaWindow alloc] init] autorelease] bind: this];
21
+
22
+ set_owner_window(self->root.get(), this);
23
+
24
+ self->painter.canvas(0, 0, 1, 1);
18
25
  }
19
26
 
20
27
  Window::~Window ()
21
28
  {
22
- if (self->self_)
23
- {
24
- [self->self_ release];
25
- self->self_ = nil;
26
- }
27
- self->this_ = NULL;
28
- }
29
+ //close();
29
30
 
30
- bool
31
- Window::close ()
32
- {
33
- if (!*this) return false;
34
- [self->self_ close];
35
- return true;
31
+ set_owner_window(self->root.get(), NULL);
36
32
  }
37
33
 
38
34
  bool
39
35
  Window::show ()
40
36
  {
41
37
  if (!*this) return false;
38
+
42
39
  if (--self->hidecount == 0)
43
- {
44
- [self->self_ makeKeyAndOrderFront: nil];
45
- }
40
+ [self->cocoa makeKeyAndOrderFront: nil];
46
41
  return true;
47
42
  }
48
43
 
@@ -50,63 +45,98 @@ namespace Reflex
50
45
  Window::hide ()
51
46
  {
52
47
  if (!*this) return false;
48
+
53
49
  if (++self->hidecount == 1)
54
- [self->self_ orderOut: self->self_];
50
+ [self->cocoa orderOut: self->cocoa];
55
51
  return true;
56
52
  }
57
53
 
58
54
  bool
59
- Window::hidden () const
55
+ Window::close ()
60
56
  {
61
57
  if (!*this) return false;
62
- return self->hidecount > 0;
58
+
59
+ [self->cocoa close];
60
+ return true;
63
61
  }
64
62
 
65
63
  bool
66
64
  Window::redraw ()
67
65
  {
68
66
  if (!*this) return false;
69
- [self->self_ redraw];
67
+
68
+ self->redraw = true;
70
69
  return true;
71
70
  }
72
71
 
73
72
  bool
74
- Window::get_title (String* title) const
73
+ Window::set_title (const char* title)
75
74
  {
76
75
  if (!*this || !title) return false;
77
- NSString* s = [self->self_ title];
78
- *title = s ? [s UTF8String] : "";
76
+
77
+ [self->cocoa setTitle: [NSString stringWithUTF8String: title]];
79
78
  return true;
80
79
  }
81
80
 
81
+ const char*
82
+ Window::title () const
83
+ {
84
+ if (!*this) return "";
85
+
86
+ NSString* s = [self->cocoa title];
87
+ self->title_tmp = s ? [s UTF8String] : "";
88
+ return self->title_tmp.c_str();
89
+ }
90
+
82
91
  bool
83
- Window::set_title (const char* title)
92
+ Window::set_bounds (coord x, coord y, coord width, coord height)
84
93
  {
85
- if (!*this || !title) return false;
86
- [self->self_ setTitle: [NSString stringWithUTF8String: title]];
94
+ if (!*this) return false;
95
+
96
+ NSRect frame =
97
+ [CocoaWindow frameRectForContentRect: NSMakeRect(x, y, width, height)];
98
+ [self->cocoa setFrame: frame display: NO animate: NO];
87
99
  return true;
88
100
  }
89
101
 
90
102
  bool
91
- Window::get_bounds (coord* x, coord* y, coord* width, coord* height)
103
+ Window::set_bounds (const Bounds& bounds)
92
104
  {
93
- if (!*this || (!x && !y && !width && !height))
94
- return false;
95
- NSRect frame = [self->self_ frame];
96
- if (x) *x = frame.origin.x;
97
- if (y) *y = frame.origin.y;
98
- if (width) *width = frame.size.width;
99
- if (height) *height = frame.size.height;
100
- return true;
105
+ return set_bounds(bounds.x, bounds.y, bounds.width, bounds.height);
106
+ }
107
+
108
+ Bounds
109
+ Window::bounds () const
110
+ {
111
+ Bounds b;
112
+ if (!*this) return b;
113
+
114
+ NSRect frame = [self->cocoa contentRectForFrameRect: [self->cocoa frame]];
115
+ b.x = frame.origin.x;
116
+ b.y = frame.origin.y;
117
+ b.width = frame.size.width;
118
+ b.height = frame.size.height;
119
+ return b;
101
120
  }
102
121
 
103
122
  bool
104
- Window::set_bounds (coord x, coord y, coord width, coord height)
123
+ Window::hidden () const
105
124
  {
106
125
  if (!*this) return false;
107
- NSRect frame = NSMakeRect(x, y, width, height);
108
- [self->self_ setFrame: frame display: NO animate: NO];
109
- return true;
126
+
127
+ return self->hidecount > 0;
128
+ }
129
+
130
+ View*
131
+ Window::root ()
132
+ {
133
+ return self->root.get();
134
+ }
135
+
136
+ Painter*
137
+ Window::painter ()
138
+ {
139
+ return &self->painter;
110
140
  }
111
141
 
112
142
  Window::operator bool () const
@@ -4,8 +4,9 @@
4
4
  #define __REFLEX_COCOA_WINDOWDATA_H__
5
5
 
6
6
 
7
- #include <boost/shared_ptr.hpp>
7
+ #include <rays/painter.h>
8
8
  #include <reflex/window.h>
9
+ #include <reflex/view.h>
9
10
 
10
11
 
11
12
  @class CocoaWindow;
@@ -18,20 +19,27 @@ namespace Reflex
18
19
  struct Window::Data
19
20
  {
20
21
 
21
- Window* this_;
22
-
23
- CocoaWindow* self_;
22
+ CocoaWindow* cocoa;
24
23
 
25
24
  int hidecount;
26
25
 
26
+ bool redraw;
27
+
28
+ Painter painter;
29
+
30
+ View::Ref root;
31
+
32
+ String title_tmp;
33
+
27
34
  Data ()
28
- : this_(NULL), self_(nil), hidecount(1)
35
+ : cocoa(nil), hidecount(1), redraw(true), root(new View)
29
36
  {
37
+ root->set_name("root");
30
38
  }
31
39
 
32
40
  operator bool () const
33
41
  {
34
- return this_ && self_;
42
+ return cocoa;
35
43
  }
36
44
 
37
45
  bool operator ! () const
@@ -42,9 +50,6 @@ namespace Reflex
42
50
  };// Window::Data
43
51
 
44
52
 
45
- typedef boost::shared_ptr<Reflex::Window::Data> WindowData;
46
-
47
-
48
53
  }// Reflex
49
54
 
50
55
 
data/src/defs.cpp CHANGED
@@ -32,7 +32,7 @@ namespace Reflex
32
32
  Points::Points (
33
33
  int type, const Point* points_, size_t size_,
34
34
  uint modifiers, uint count, bool drag)
35
- : type(type), size(size), modifiers(modifiers), count(count), drag(drag)
35
+ : type(type), size(size_), modifiers(modifiers), count(count), drag(drag)
36
36
  {
37
37
  if (size > MAX) size = MAX;
38
38
 
data/src/exception.cpp CHANGED
@@ -1,21 +1,6 @@
1
1
  #include "reflex/exception.h"
2
2
 
3
3
 
4
- #define VA_STRING(format, result) \
5
- String result; \
6
- do \
7
- { \
8
- if (format) \
9
- { \
10
- va_list args; \
11
- va_start(args, format); \
12
- result = Xot::stringf(format, args); \
13
- va_end(args); \
14
- } \
15
- } \
16
- while (false)
17
-
18
-
19
4
  namespace Reflex
20
5
  {
21
6
 
@@ -23,7 +8,7 @@ namespace Reflex
23
8
  ReflexException::ReflexException (const char* format, ...)
24
9
  : Super("")
25
10
  {
26
- VA_STRING(format, s);
11
+ XOT_STRINGF(format, s);
27
12
  text = s;
28
13
  }
29
14
 
@@ -39,9 +24,9 @@ namespace Reflex
39
24
 
40
25
 
41
26
  void
42
- error (const char* format, ...)
27
+ reflex_error (const char* format, ...)
43
28
  {
44
- VA_STRING(format, s);
29
+ XOT_STRINGF(format, s);
45
30
  throw ReflexException(s.c_str());
46
31
  }
47
32
 
data/src/helpers.h ADDED
@@ -0,0 +1,12 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __REFLEX_HELPERS_H__
4
+ #define __REFLEX_HELPERS_H__
5
+
6
+
7
+ namespace Reflex
8
+ {
9
+ }// Reflex
10
+
11
+
12
+ #endif//EOH
data/src/reflex.cpp CHANGED
@@ -14,15 +14,21 @@ namespace Reflex
14
14
  bool
15
15
  run (const char* name)
16
16
  {
17
- if (!initialized() || app()) return false;
17
+ if (!initialized()) return false;
18
18
 
19
- Application a;
20
- if (!a) return false;
19
+ Application::Ref ref;
20
+ if (!app())
21
+ {
22
+ ref = new Application;
23
+ if (!ref || !*ref) return false;
24
+ }
21
25
 
22
- if (name && !a.set_name(name))
26
+ if (!app()) return false;
27
+
28
+ if (name && !app()->set_name(name))
23
29
  return false;
24
30
 
25
- return a.run();
31
+ return app()->run();
26
32
  }
27
33
 
28
34
  bool
data/src/view.cpp ADDED
@@ -0,0 +1,326 @@
1
+ #include "reflex/view.h"
2
+
3
+
4
+ #include <string.h>
5
+ #include <rays/bounds.h>
6
+ #include <rays/painter.h>
7
+ #include <reflex/window.h>
8
+
9
+
10
+ namespace Reflex
11
+ {
12
+
13
+
14
+ typedef std::vector<View::Ref> ViewList;
15
+
16
+ static ViewList empty_list;
17
+
18
+
19
+ struct View::Data
20
+ {
21
+
22
+ typedef ViewList:: iterator iterator;
23
+
24
+ typedef ViewList::const_iterator const_iterator;
25
+
26
+ String name;
27
+
28
+ Bounds bounds;
29
+
30
+ int hidecount;
31
+
32
+ View* parent;
33
+
34
+ Window* window;
35
+
36
+ ViewList* pchildren_;
37
+
38
+ Data ()
39
+ : hidecount(0), parent(NULL), pchildren_(NULL)
40
+ {
41
+ }
42
+
43
+ ~Data ()
44
+ {
45
+ delete pchildren_;
46
+ }
47
+
48
+ bool set_parent (View* parent_)
49
+ {
50
+ if (parent_ && parent) return false;
51
+
52
+ parent = parent_;
53
+ window = parent_ ? parent_->window() : NULL;
54
+ return true;
55
+ }
56
+
57
+ ViewList* children ()
58
+ {
59
+ if (pchildren_ == NULL) pchildren_ = new ViewList();
60
+ return pchildren_;
61
+ }
62
+
63
+ iterator begin ()
64
+ {
65
+ if (!pchildren_) return empty_list.begin();
66
+ return pchildren_->begin();
67
+ }
68
+
69
+ const_iterator begin () const
70
+ {
71
+ if (!pchildren_) return empty_list.begin();
72
+ return pchildren_->begin();
73
+ }
74
+
75
+ iterator end ()
76
+ {
77
+ if (!pchildren_) return empty_list.end();
78
+ return pchildren_->end();
79
+ }
80
+
81
+ const_iterator end () const
82
+ {
83
+ if (!pchildren_) return empty_list.end();
84
+ return pchildren_->end();
85
+
86
+ }
87
+
88
+ };// View::Data
89
+
90
+
91
+ void
92
+ set_owner_window (View* view, Window* owner)
93
+ {
94
+ if (!view) return;
95
+ view->self->window = owner;
96
+ }
97
+
98
+ void
99
+ update_view_tree (View* view, float dt)
100
+ {
101
+ if (!view) return;
102
+
103
+ view->update(dt);
104
+
105
+ View::Data::iterator end = view->self->end();
106
+ for (View::Data::iterator it = view->self->begin(); it != end; ++it)
107
+ update_view_tree(it->get(), dt);
108
+ }
109
+
110
+ void
111
+ draw_view_tree (View* view, Painter* p, coord left, coord top)
112
+ {
113
+ if (!view || !p) return;
114
+
115
+ const Bounds& b = view->bounds();
116
+ left += b.x;
117
+ top += b.y;
118
+
119
+ p->push_matrix();
120
+ p->translate(b.x, b.y);
121
+ p->set_clip(left, top, b.width, b.height);
122
+
123
+ view->draw(p, Bounds(0, 0, b.width, b.height));
124
+
125
+ View::Data::iterator end = view->self->end();
126
+ for (View::Data::iterator it = view->self->begin(); it != end; ++it)
127
+ draw_view_tree(it->get(), p, left, top);
128
+
129
+ p->pop_matrix();
130
+ }
131
+
132
+
133
+ View::View ()
134
+ {
135
+ }
136
+
137
+ View::~View ()
138
+ {
139
+ }
140
+
141
+ bool
142
+ View::show ()
143
+ {
144
+ --self->hidecount;
145
+ return true;
146
+ }
147
+
148
+ bool
149
+ View::hide ()
150
+ {
151
+ ++self->hidecount;
152
+ return true;
153
+ }
154
+
155
+ bool
156
+ View::redraw ()
157
+ {
158
+ Window* w = window();
159
+ return w && w->redraw();
160
+ }
161
+
162
+ bool
163
+ View::add_child (View* child)
164
+ {
165
+ if (!child) return false;
166
+
167
+ View* parent = child->parent();
168
+ if (parent) return parent == this;
169
+
170
+ Data::iterator it = std::find(self->begin(), self->end(), child);
171
+ if (it != self->end()) return false;
172
+
173
+ if (!child->self->set_parent(this))
174
+ return false;
175
+
176
+ self->children()->push_back(child);
177
+ return true;
178
+ }
179
+
180
+ bool
181
+ View::remove_child (View* child)
182
+ {
183
+ if (!self->pchildren_ || !child || child->parent() != this)
184
+ return false;
185
+
186
+ Data::iterator it = std::find(self->begin(), self->end(), child);
187
+ if (it == self->end()) return false;
188
+
189
+ if (!child->self->set_parent(NULL))
190
+ return false;
191
+
192
+ self->pchildren_->erase(it);
193
+ return true;
194
+ }
195
+
196
+ View*
197
+ View::find_child (const char* name, size_t index, bool recursive)
198
+ {
199
+ if (recursive && index > 0) return NULL;
200
+
201
+ for (Data::iterator it = self->begin(); it != self->end(); ++it)
202
+ {
203
+ if (
204
+ strcmp((*it)->name(), name) == 0 &&
205
+ index-- == 0)
206
+ {
207
+ return (View*) it->get();
208
+ }
209
+
210
+ This* p = NULL;
211
+ if (recursive && (p = dynamic_cast<This*>(it->get())))
212
+ {
213
+ View* child = p->find_child(name, 0, true);
214
+ if (child) return child;
215
+ }
216
+ }
217
+
218
+ return NULL;
219
+ }
220
+
221
+ const View*
222
+ View::find_child (const char* name, size_t index, bool recursive) const
223
+ {
224
+ return const_cast<This*>(this)->find_child(name, index, recursive);
225
+ }
226
+
227
+ bool
228
+ View::set_name (const char* name)
229
+ {
230
+ self->name = name ? name : "";
231
+ return true;
232
+ }
233
+
234
+ const char*
235
+ View::name () const
236
+ {
237
+ return self->name.c_str();
238
+ }
239
+
240
+ bool
241
+ View::set_bounds (coord x, coord y, coord width, coord height)
242
+ {
243
+ Bounds& b = self->bounds;
244
+ b.x = x;
245
+ b.y = y;
246
+ b.width = width;
247
+ b.height = height;
248
+ return true;
249
+ }
250
+
251
+ bool
252
+ View::set_bounds (const Bounds& bounds)
253
+ {
254
+ self->bounds = bounds;
255
+ return true;
256
+ }
257
+
258
+ const Bounds&
259
+ View::bounds () const
260
+ {
261
+ return self->bounds;
262
+ }
263
+
264
+ bool
265
+ View::hidden () const
266
+ {
267
+ return self->hidecount > 0;
268
+ }
269
+
270
+ View*
271
+ View::parent ()
272
+ {
273
+ return self->parent;
274
+ }
275
+
276
+ const View*
277
+ View::parent () const
278
+ {
279
+ return const_cast<This*>(this)->parent();
280
+ }
281
+
282
+ Window*
283
+ View::window ()
284
+ {
285
+ return self->window;
286
+ }
287
+
288
+ const Window*
289
+ View::window () const
290
+ {
291
+ return const_cast<View*>(this)->window();
292
+ }
293
+
294
+ void
295
+ View::update (float dt)
296
+ {
297
+ }
298
+
299
+ void
300
+ View::draw (Painter* p, const Bounds& b)
301
+ {
302
+ }
303
+
304
+ void
305
+ View::moved (coord dx, coord dy)
306
+ {
307
+ }
308
+
309
+ void
310
+ View::resized (coord dwidth, coord dheight)
311
+ {
312
+ }
313
+
314
+ View::operator bool () const
315
+ {
316
+ return true;
317
+ }
318
+
319
+ bool
320
+ View::operator ! () const
321
+ {
322
+ return !operator bool();
323
+ }
324
+
325
+
326
+ }// Reflex
@@ -34,7 +34,7 @@ namespace Reflex
34
34
 
35
35
  Application::Application ()
36
36
  {
37
- if (instance) error("multiple application instance.");
37
+ if (instance) reflex_error("multiple application instance.");
38
38
 
39
39
  instance = this;
40
40
  }
@@ -81,19 +81,18 @@ namespace Reflex
81
81
  }
82
82
 
83
83
  bool
84
- Application::get_name (String* name) const
84
+ Application::set_name (const char* name)
85
85
  {
86
86
  if (!*this || !name) return false;
87
- *name = self->name;
87
+ self->name = name;
88
88
  return true;
89
89
  }
90
90
 
91
- bool
92
- Application::set_name (const char* name)
91
+ const char*
92
+ Application::name () const
93
93
  {
94
- if (!*this || !name) return false;
95
- self->name = name;
96
- return true;
94
+ if (!*this) return "";
95
+ return self->name.c_str();
97
96
  }
98
97
 
99
98
  Application::operator bool () const
data/src/win32/defs.h CHANGED
@@ -4,7 +4,7 @@
4
4
  #define __REFLEX_WIN32_DEFS_H__
5
5
 
6
6
 
7
- #import <windows.h>
7
+ #include <windows.h>
8
8
  #include <reflex/defs.h>
9
9
 
10
10