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
@@ -2,6 +2,7 @@
2
2
  #include "reflex/application.h"
3
3
 
4
4
 
5
+ #include <assert.h>
5
6
  #import <Cocoa/Cocoa.h>
6
7
  #include <reflex/exception.h>
7
8
  #include "applicationdata.h"
@@ -24,25 +25,14 @@ namespace Reflex
24
25
 
25
26
  Application::Application ()
26
27
  {
27
- if (instance) error("multiple application instance.");
28
-
28
+ if (instance) reflex_error("multiple application instance.");
29
29
  instance = this;
30
30
 
31
- self->this_ = this;
32
- self->self_ = NSApp ? [NSApp retain] : nil;
33
- if (self->self_)
34
- [self->self_ setApplicationData: self];
31
+ if (NSApp) [NSApp bind: this];
35
32
  }
36
33
 
37
34
  Application::~Application ()
38
35
  {
39
- if (self->self_)
40
- {
41
- [self->self_ release];
42
- self->self_ = nil;
43
- }
44
- self->this_ = NULL;
45
-
46
36
  instance = NULL;
47
37
  }
48
38
 
@@ -50,7 +40,8 @@ namespace Reflex
50
40
  Application::run ()
51
41
  {
52
42
  if (!*this) return false;
53
- [self->self_ run];
43
+
44
+ [self->cocoa run];
54
45
  return true;
55
46
  }
56
47
 
@@ -58,7 +49,8 @@ namespace Reflex
58
49
  Application::quit ()
59
50
  {
60
51
  if (!*this) return false;
61
- [self->self_ terminate: nil];
52
+
53
+ [self->cocoa terminate: nil];
62
54
  return true;
63
55
  }
64
56
 
@@ -72,15 +64,8 @@ namespace Reflex
72
64
  Application::about ()
73
65
  {
74
66
  if (!*this) return false;
75
- [self->self_ orderFrontStandardAboutPanel: nil];
76
- return true;
77
- }
78
67
 
79
- bool
80
- Application::get_name (String* name) const
81
- {
82
- if (!*this || !name) return false;
83
- *name = self->name;
68
+ [self->cocoa orderFrontStandardAboutPanel: nil];
84
69
  return true;
85
70
  }
86
71
 
@@ -88,10 +73,19 @@ namespace Reflex
88
73
  Application::set_name (const char* name)
89
74
  {
90
75
  if (!*this || !name) return false;
76
+
91
77
  self->name = name;
92
78
  return true;
93
79
  }
94
80
 
81
+ const char*
82
+ Application::name () const
83
+ {
84
+ if (!*this) return NULL;
85
+
86
+ return self->name.c_str();
87
+ }
88
+
95
89
  Application::operator bool () const
96
90
  {
97
91
  return self && *self;
@@ -4,7 +4,6 @@
4
4
  #define __REFLEX_COCOA_APPLICATIONDATA_H__
5
5
 
6
6
 
7
- #include <boost/shared_ptr.hpp>
8
7
  #include <reflex/application.h>
9
8
 
10
9
 
@@ -18,20 +17,18 @@ namespace Reflex
18
17
  struct Application::Data
19
18
  {
20
19
 
21
- Application* this_;
22
-
23
- CocoaApplication* self_;
20
+ CocoaApplication* cocoa;
24
21
 
25
22
  String name;
26
23
 
27
24
  Data ()
28
- : this_(NULL), self_(nil)
25
+ : cocoa(nil)
29
26
  {
30
27
  }
31
28
 
32
29
  operator bool () const
33
30
  {
34
- return this_ && self_;
31
+ return cocoa;
35
32
  }
36
33
 
37
34
  bool operator ! () const
@@ -42,9 +39,6 @@ namespace Reflex
42
39
  };// Application::Data
43
40
 
44
41
 
45
- typedef boost::shared_ptr<Reflex::Application::Data> ApplicationData;
46
-
47
-
48
42
  }// Reflex
49
43
 
50
44
 
@@ -1,19 +1,21 @@
1
1
  // -*- objc -*-
2
2
  #import <AppKit/NSApplication.h>
3
- #include "applicationdata.h"
3
+ #include "reflex/application.h"
4
4
 
5
5
 
6
6
  @interface CocoaApplication : NSApplication <NSApplicationDelegate>
7
7
 
8
8
  {
9
9
  @private
10
- Reflex::ApplicationData data;
10
+ Reflex::Application::Ref* pref;
11
11
  }
12
12
 
13
+ - (BOOL) bind: (Reflex::Application*) instance;
14
+
15
+ - (void) unbind;
16
+
13
17
  - (void) about;
14
18
 
15
19
  - (void) quit;
16
20
 
17
- - (BOOL) setApplicationData: (Reflex::ApplicationData) data;
18
-
19
21
  @end// CocoaApplication
@@ -2,8 +2,10 @@
2
2
  #import "cocoaapplication.h"
3
3
 
4
4
 
5
+ #include <assert.h>
5
6
  #import <Cocoa/Cocoa.h>
6
7
  #include <reflex/reflex.h>
8
+ #include "applicationdata.h"
7
9
 
8
10
 
9
11
  @implementation CocoaApplication
@@ -17,34 +19,84 @@
17
19
  [self setActivationPolicy: NSApplicationActivationPolicyRegular];
18
20
  [self activateIgnoringOtherApps: YES];
19
21
 
22
+ pref = new Reflex::Application::Ref;
23
+
20
24
  return self;
21
25
  }
22
26
 
23
- - (BOOL) setApplicationData: (Reflex::ApplicationData) data_
27
+ - (void) dealloc
28
+ {
29
+ [self unbind];
30
+ delete pref;
31
+
32
+ [super dealloc];
33
+ }
34
+
35
+ - (BOOL) bind: (Reflex::Application*) instance
24
36
  {
25
- if (data || !data_) return NO;
26
- data = data_;
37
+ if (instance && instance->self->cocoa)
38
+ return NO;
39
+
40
+ [self unbind];
41
+
42
+ assert(pref);
43
+ Reflex::Application::Ref& ref = *pref;
44
+
45
+ ref = instance;
46
+ if (ref) ref->self->cocoa = [self retain];
27
47
  return YES;
28
48
  }
29
49
 
50
+ - (void) unbind
51
+ {
52
+ assert(pref);
53
+ Reflex::Application::Ref& ref = *pref;
54
+ if (!ref) return;
55
+
56
+ if (ref->self->cocoa) [ref->self->cocoa release];
57
+ ref->self->cocoa = nil;
58
+ ref.reset();
59
+ }
60
+
30
61
  - (void) quit
31
62
  {
32
- if (data && *data)
33
- data->this_->quit();
63
+ assert(pref);
64
+
65
+ if (*pref)
66
+ (*pref)->quit();
34
67
  else
35
68
  Reflex::quit();
36
69
  }
37
70
 
38
71
  - (void) about
39
72
  {
40
- if (data && *data)
41
- data->this_->about();
73
+ assert(pref);
74
+
75
+ if (*pref)
76
+ (*pref)->about();
42
77
  else
43
78
  [self orderFrontStandardAboutPanel: nil];
44
79
  }
45
80
 
81
+ - (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app
82
+ {
83
+ return YES;
84
+ }
85
+
86
+ - (void) applicationDidFinishLaunching: (NSNotification*) notification
87
+ {
88
+ [self setupMenu];
89
+ }
90
+
91
+ - (void) applicationWillTerminate: (NSNotification*) notification
92
+ {
93
+ [self unbind];
94
+ }
95
+
46
96
  - (BOOL) setupApplicationMenu: (NSMenu*) parent
47
97
  {
98
+ assert(pref);
99
+
48
100
  if (!parent) return NO;
49
101
 
50
102
  NSMenu* menu = [[[NSMenu alloc]
@@ -53,8 +105,8 @@
53
105
  if ([self respondsToSelector: @selector(setAppleMenu:)])
54
106
  [self performSelector: @selector(setAppleMenu:) withObject: menu];
55
107
 
56
- NSString* name = !data->name.empty() ?
57
- [NSString stringWithUTF8String: data->name.c_str()] : @"";
108
+ NSString* name = !(*pref)->self->name.empty() ?
109
+ [NSString stringWithUTF8String: (*pref)->self->name.c_str()] : @"";
58
110
  if ([name length] > 0)
59
111
  name = [@" " stringByAppendingString: name];
60
112
 
@@ -167,14 +219,4 @@
167
219
  return YES;
168
220
  }
169
221
 
170
- - (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app
171
- {
172
- return YES;
173
- }
174
-
175
- - (void) applicationDidFinishLaunching: (NSNotification*) notification
176
- {
177
- [self setupMenu];
178
- }
179
-
180
222
  @end// CocoaApplication
@@ -1,6 +1,6 @@
1
1
  // -*- objc -*-
2
2
  #import <AppKit/NSWindow.h>
3
- #include "windowdata.h"
3
+ #include "reflex/window.h"
4
4
 
5
5
 
6
6
  @class OpenGLView;
@@ -10,12 +10,14 @@
10
10
 
11
11
  {
12
12
  @private
13
- Reflex::WindowData data;
13
+ Reflex::Window::Ref* pref;
14
14
  OpenGLView* view;
15
15
  NSTimer* timer;
16
16
  }
17
17
 
18
- - (id) initWithWindowData: (Reflex::WindowData) data;
18
+ - (BOOL) bind: (Reflex::Window*) instance;
19
+
20
+ - (void) unbind;
19
21
 
20
22
  - (void) setupContentView;
21
23
 
@@ -27,8 +29,6 @@
27
29
 
28
30
  - (void) draw;
29
31
 
30
- - (void) redraw;
31
-
32
32
  - (void) keyDown: (NSEvent*) event;
33
33
 
34
34
  - (void) keyUp: (NSEvent*) event;
@@ -41,4 +41,6 @@
41
41
 
42
42
  - (void) mouseDragged: (NSEvent*) event;
43
43
 
44
+ + (NSRect) frameRectForContentRect: (NSRect) contentRect;
45
+
44
46
  @end// CocoaWindow
@@ -2,32 +2,35 @@
2
2
  #import "cocoawindow.h"
3
3
 
4
4
 
5
+ #include <assert.h>
5
6
  #import <Cocoa/Cocoa.h>
6
- #import "openglview.h"
7
-
7
+ #include <rays/bounds.h>
8
8
  #include <reflex/window.h>
9
9
  #include "defs.h"
10
+ #include "windowdata.h"
11
+ #import "openglview.h"
12
+
13
+
14
+ static const NSUInteger WINDOW_STYLE_MASK =
15
+ NSTitledWindowMask |
16
+ NSClosableWindowMask |
17
+ NSMiniaturizableWindowMask |
18
+ NSResizableWindowMask |
19
+ 0;//NSTexturedBackgroundWindowMask
10
20
 
11
21
 
12
22
  @implementation CocoaWindow
13
23
 
14
- - (id) initWithWindowData: (Reflex::WindowData) data_
24
+ - (id) init
15
25
  {
16
- NSRect frame = NSMakeRect(0, 0, 0, 0);
17
-
18
26
  self = [super
19
27
  initWithContentRect: NSMakeRect(0, 0, 0, 0)
20
- styleMask:
21
- NSTitledWindowMask |
22
- NSClosableWindowMask |
23
- NSMiniaturizableWindowMask |
24
- NSResizableWindowMask |
25
- 0//NSTexturedBackgroundWindowMask
28
+ styleMask: WINDOW_STYLE_MASK
26
29
  backing: NSBackingStoreBuffered
27
30
  defer: NO];
28
31
  if (!self) return nil;
29
32
 
30
- data = data_;
33
+ pref = new Reflex::Window::Ref;
31
34
  view = nil;
32
35
  timer = nil;
33
36
 
@@ -43,11 +46,41 @@
43
46
  - (void) dealloc
44
47
  {
45
48
  [self stopTimer];
46
- [view release];
49
+
50
+ if (view) [view release];
51
+
52
+ [self unbind];
53
+ delete pref;
47
54
 
48
55
  [super dealloc];
49
56
  }
50
57
 
58
+ - (BOOL) bind: (Reflex::Window*) instance
59
+ {
60
+ if (instance && instance->self->cocoa)
61
+ return NO;
62
+
63
+ [self unbind];
64
+
65
+ assert(pref);
66
+ Reflex::Window::Ref& ref = *pref;
67
+
68
+ ref = instance;
69
+ if (ref) ref->self->cocoa = [self retain];
70
+ return YES;
71
+ }
72
+
73
+ - (void) unbind
74
+ {
75
+ assert(pref);
76
+ Reflex::Window::Ref& ref = *pref;
77
+ if (!ref) return;
78
+
79
+ if (ref->self->cocoa) [ref->self->cocoa release];
80
+ ref->self->cocoa = nil;
81
+ ref.reset();
82
+ }
83
+
51
84
  - (void) setupContentView
52
85
  {
53
86
  NSRect rect = [self contentRectForFrameRect: [self frame]];
@@ -78,94 +111,120 @@
78
111
 
79
112
  - (void) stopTimer
80
113
  {
81
- if (timer)
82
- {
83
- if ([timer isValid]) [timer invalidate];
84
- [timer release];
85
- timer = nil;
86
- }
114
+ if (!timer) return;
115
+
116
+ [timer invalidate];
117
+ timer = nil;
87
118
  }
88
119
 
89
120
  - (void) update: (NSTimer*) t
90
121
  {
91
- if (!data || !*data) return;
92
- data->this_->update();
122
+ assert(pref);
123
+ if (!*pref) return;
124
+
125
+ (*pref)->update(1);
126
+
127
+ if ((*pref)->self->redraw)
128
+ {
129
+ [self display];
130
+ (*pref)->self->redraw = false;
131
+ }
93
132
  }
94
133
 
95
134
  - (void) draw
96
135
  {
97
- if (!data || !*data) return;
98
- data->this_->draw();
99
- }
136
+ assert(pref);
137
+ if (!*pref) return;
100
138
 
101
- - (void) redraw
102
- {
103
- [self display];
139
+ (*pref)->draw();
104
140
  }
105
141
 
106
142
  - (BOOL) windowShouldClose: (id) sender
107
143
  {
108
- if (!data || !*data) return YES;
109
- data->this_->close();
144
+ assert(pref);
145
+ if (!*pref) return YES;
146
+
147
+ (*pref)->close();
110
148
  return NO;
111
149
  }
112
150
 
113
151
  - (void) windowWillClose: (NSNotification*) notification
114
152
  {
115
153
  [self stopTimer];
116
- data.reset();
154
+ [self unbind];
117
155
  }
118
156
 
119
157
  - (void) windowDidMove: (NSNotification*) notification
120
158
  {
121
- if (!data || !*data) return;
122
- Reflex::coord x = 0, y = 0;
123
- data->this_->get_bounds(&x, &y);
124
- data->this_->moved(x, y);
159
+ assert(pref);
160
+ if (!*pref) return;
161
+
162
+ const Reflex::Bounds& b = (*pref)->bounds();
163
+ (*pref)->moved(b.x, b.y);
125
164
  }
126
165
 
127
166
  - (void) windowDidResize: (NSNotification*) notification
128
167
  {
129
- if (!data || !*data) return;
130
- Reflex::coord w = 0, h = 0;
131
- data->this_->get_bounds(NULL, NULL, &w, &h);
132
- data->this_->resized(w, h);
168
+ assert(pref);
169
+ if (!*pref) return;
170
+
171
+ const Reflex::Bounds& b = (*pref)->bounds();
172
+ (*pref)->resized(b.width, b.height);
133
173
  }
134
174
 
135
175
  - (void) keyDown: (NSEvent*) event
136
176
  {
137
- if (!data || !*data) return;
138
- data->this_->key_down(Reflex::CocoaKey(event));
177
+ assert(pref);
178
+ if (!*pref) return;
179
+
180
+ (*pref)->key_down(Reflex::CocoaKey(event));
139
181
  }
140
182
 
141
183
  - (void) keyUp: (NSEvent*) event
142
184
  {
143
- if (!data || !*data) return;
144
- data->this_->key_up(Reflex::CocoaKey(event));
185
+ assert(pref);
186
+ if (!*pref) return;
187
+
188
+ (*pref)->key_up(Reflex::CocoaKey(event));
145
189
  }
146
190
 
147
191
  - (void) mouseDown: (NSEvent*) event
148
192
  {
149
- if (!data || !*data) return;
150
- data->this_->points_down(Reflex::CocoaPoints(event, view));
193
+ assert(pref);
194
+ if (!*pref) return;
195
+
196
+ (*pref)->points_down(Reflex::CocoaPoints(event, view));
151
197
  }
152
198
 
153
199
  - (void) mouseUp: (NSEvent*) event
154
200
  {
155
- if (!data || !*data) return;
156
- data->this_->points_up(Reflex::CocoaPoints(event, view));
201
+ assert(pref);
202
+ if (!*pref) return;
203
+
204
+ (*pref)->points_up(Reflex::CocoaPoints(event, view));
157
205
  }
158
206
 
159
207
  - (void) mouseMoved: (NSEvent*) event
160
208
  {
161
- if (!data || !*data) return;
162
- data->this_->points_moved(Reflex::CocoaPoints(event, view));
209
+ assert(pref);
210
+ if (!*pref) return;
211
+
212
+ (*pref)->points_moved(Reflex::CocoaPoints(event, view));
163
213
  }
164
214
 
165
215
  - (void) mouseDragged: (NSEvent*) event
166
216
  {
167
- if (!data || !*data) return;
168
- data->this_->points_moved(Reflex::CocoaPoints(event, view));
217
+ assert(pref);
218
+ if (!*pref) return;
219
+
220
+ (*pref)->points_moved(Reflex::CocoaPoints(event, view));
221
+ }
222
+
223
+ + (NSRect) frameRectForContentRect: (NSRect) contentRect
224
+ {
225
+ return [NSWindow
226
+ frameRectForContentRect: contentRect
227
+ styleMask: WINDOW_STYLE_MASK];
169
228
  }
170
229
 
171
230
  @end// CocoaWindow
data/src/cocoa/defs.mm CHANGED
@@ -1,3 +1,4 @@
1
+ // -*- c++ -*-
1
2
  #include "defs.h"
2
3
 
3
4
 
@@ -71,13 +72,15 @@ namespace Reflex
71
72
 
72
73
  CocoaPoints::CocoaPoints (NSEvent* e, NSView* view)
73
74
  : Points(
74
- get_point_type(e), (coord) 0, (coord) 0, get_modifiers(e), [e clickCount],
75
+ get_point_type(e), (coord) 0, (coord) 0,
76
+ get_modifiers(e), (uint) [e clickCount],
75
77
  [e type] == NSLeftMouseDragged || [e type] == NSRightMouseDragged)
76
78
  {
77
79
  NSPoint p = [e locationInWindow];
80
+ NSRect b = [view bounds];
78
81
  if (view) p = [view convertPoint: p fromView: nil];
79
82
  x = p.x;
80
- y = p.y;
83
+ y = b.size.height - p.y;
81
84
  }
82
85
 
83
86