glimr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/lib/glimr.rb +2 -0
  2. data/lib/glimr/configurable.rb +37 -0
  3. data/lib/glimr/default_theme/button_bg.png +0 -0
  4. data/lib/glimr/default_theme/button_bg_down.png +0 -0
  5. data/lib/glimr/default_theme/button_cover.png +0 -0
  6. data/lib/glimr/default_theme/button_cover_down.png +0 -0
  7. data/lib/glimr/default_theme/button_focus.png +0 -0
  8. data/lib/glimr/default_theme/checkbox_bg.png +0 -0
  9. data/lib/glimr/default_theme/checkbox_checked_bg.png +0 -0
  10. data/lib/glimr/default_theme/font.ttf +0 -0
  11. data/lib/glimr/default_theme/hscroller_bg.png +0 -0
  12. data/lib/glimr/default_theme/radiobutton_bg.png +0 -0
  13. data/lib/glimr/default_theme/radiobutton_checked_bg.png +0 -0
  14. data/lib/glimr/default_theme/resizer_down.png +0 -0
  15. data/lib/glimr/default_theme/resizer_up.png +0 -0
  16. data/lib/glimr/default_theme/scroll_down_down.png +0 -0
  17. data/lib/glimr/default_theme/scroll_down_up.png +0 -0
  18. data/lib/glimr/default_theme/scroll_hknob_down.png +0 -0
  19. data/lib/glimr/default_theme/scroll_hknob_up.png +0 -0
  20. data/lib/glimr/default_theme/scroll_left_down.png +0 -0
  21. data/lib/glimr/default_theme/scroll_left_up.png +0 -0
  22. data/lib/glimr/default_theme/scroll_right_down.png +0 -0
  23. data/lib/glimr/default_theme/scroll_right_up.png +0 -0
  24. data/lib/glimr/default_theme/scroll_up_down.png +0 -0
  25. data/lib/glimr/default_theme/scroll_up_up.png +0 -0
  26. data/lib/glimr/default_theme/scroll_vknob_down.png +0 -0
  27. data/lib/glimr/default_theme/scroll_vknob_up.png +0 -0
  28. data/lib/glimr/default_theme/text_cursor.png +0 -0
  29. data/lib/glimr/default_theme/text_cursor_insert.png +0 -0
  30. data/lib/glimr/default_theme/text_input_bg.png +0 -0
  31. data/lib/glimr/default_theme/vscroller_bg.png +0 -0
  32. data/lib/glimr/event.rb +41 -0
  33. data/lib/glimr/eventlistener.rb +209 -0
  34. data/lib/glimr/layoutable.rb +520 -0
  35. data/lib/glimr/renderer.rb +2 -0
  36. data/lib/glimr/renderer/camera.rb +63 -0
  37. data/lib/glimr/renderer/geometry.rb +194 -0
  38. data/lib/glimr/renderer/glutwindow.rb +387 -0
  39. data/lib/glimr/renderer/light.rb +43 -0
  40. data/lib/glimr/renderer/material.rb +66 -0
  41. data/lib/glimr/renderer/model.rb +103 -0
  42. data/lib/glimr/renderer/orthoprojection.rb +21 -0
  43. data/lib/glimr/renderer/raw.rb +34 -0
  44. data/lib/glimr/renderer/sceneobject.rb +279 -0
  45. data/lib/glimr/renderer/shader.rb +14 -0
  46. data/lib/glimr/renderer/texture.rb +280 -0
  47. data/lib/glimr/renderer/transform.rb +322 -0
  48. data/lib/glimr/renderer/viewport.rb +349 -0
  49. data/lib/glimr/renderer_core.rb +10 -0
  50. data/lib/glimr/util.rb +247 -0
  51. data/lib/glimr/widget.rb +87 -0
  52. data/lib/glimr/widgets.rb +37 -0
  53. data/lib/glimr/widgets/button.rb +277 -0
  54. data/lib/glimr/widgets/checkbox.rb +82 -0
  55. data/lib/glimr/widgets/container.rb +84 -0
  56. data/lib/glimr/widgets/image.rb +82 -0
  57. data/lib/glimr/widgets/label.rb +91 -0
  58. data/lib/glimr/widgets/layout.rb +227 -0
  59. data/lib/glimr/widgets/list.rb +28 -0
  60. data/lib/glimr/widgets/radiogroup.rb +118 -0
  61. data/lib/glimr/widgets/resizer.rb +31 -0
  62. data/lib/glimr/widgets/scrollable_container.rb +67 -0
  63. data/lib/glimr/widgets/scrollbar.rb +496 -0
  64. data/lib/glimr/widgets/stretchable_image.rb +135 -0
  65. data/lib/glimr/widgets/text_editor.rb +349 -0
  66. data/tests/assets/datatowers_crop.jpg +0 -0
  67. data/tests/assets/download_progress_meter.png +0 -0
  68. data/tests/assets/metalwing2.png +0 -0
  69. data/tests/assets/redhairgreeneyes3.jpg +0 -0
  70. data/tests/demo_apps/spinning_ruby.rb +37 -0
  71. data/tests/integration_tests/run_all.rb +8 -0
  72. data/tests/integration_tests/test_button.rb +22 -0
  73. data/tests/integration_tests/test_checkbox.rb +21 -0
  74. data/tests/integration_tests/test_container.rb +22 -0
  75. data/tests/integration_tests/test_label.rb +12 -0
  76. data/tests/integration_tests/test_layout.rb +43 -0
  77. data/tests/integration_tests/test_radiogroup.rb +16 -0
  78. data/tests/integration_tests/test_renderer.rb +44 -0
  79. data/tests/integration_tests/test_renderer2.rb +36 -0
  80. data/tests/integration_tests/test_scrollable_container.rb +34 -0
  81. data/tests/integration_tests/test_scrollbar.rb +20 -0
  82. data/tests/integration_tests/test_stretchable_image.rb +34 -0
  83. data/tests/integration_tests/test_text_input.rb +20 -0
  84. data/tests/integration_tests/test_zsort.rb +18 -0
  85. data/tests/unit_tests/test_button.rb +93 -0
  86. data/tests/unit_tests/test_checkbox.rb +35 -0
  87. data/tests/unit_tests/test_label.rb +36 -0
  88. data/tests/unit_tests/test_layout.rb +229 -0
  89. data/tests/unit_tests/test_widget.rb +3 -0
  90. metadata +139 -0
@@ -0,0 +1,87 @@
1
+ require 'glimr/renderer/model'
2
+ require 'glimr/layoutable'
3
+
4
+
5
+ module GlimR
6
+
7
+ class Widget < Model
8
+ include Layoutable
9
+
10
+ touching_accessor :model, :locked
11
+ attr_accessor :focused
12
+
13
+ attr_reader :hover
14
+
15
+ def self.for(*args,&block)
16
+ GlimR.for(*args,&block)
17
+ end
18
+
19
+ def initialize(*args, &block)
20
+ @buttons_down = {}
21
+ super
22
+ on_mouse_over(:mouse_over)
23
+ on_mouse_out(:mouse_out)
24
+ on_mouse_down(:mouse_down)
25
+ on_mouse_up(:mouse_up)
26
+ on_mouse_move(:mouse_move)
27
+ on_key_down(:key_down)
28
+ on_key_up(:key_up)
29
+ on_click(:click)
30
+ on_activate(:activate)
31
+ on_focus(:focus)
32
+ on_blur(:blur)
33
+ on_lock(:lock)
34
+ on_unlock(:unlock)
35
+ end
36
+
37
+ def mouse_over(o, event)
38
+ @hover = true
39
+ end
40
+
41
+ def mouse_out(o, event)
42
+ @hover = false
43
+ end
44
+
45
+ def mouse_down(o, event)
46
+ focus(o,event) unless focused and event.button == :left
47
+ end
48
+
49
+ def mouse_up(o, event)
50
+ blur(o,event) if focused and !@hover and event.button == :left
51
+ end
52
+
53
+ def mouse_move(o, event)
54
+ end
55
+
56
+ def key_down(o, event)
57
+ end
58
+
59
+ def key_up(o, event)
60
+ end
61
+
62
+ def click(o, event)
63
+ end
64
+
65
+ def activate(o, event)
66
+ end
67
+
68
+ def focus(o, event)
69
+ self.focused = true
70
+ end
71
+
72
+ def blur(o, event)
73
+ self.focused = false
74
+ end
75
+
76
+ def lock(o, event)
77
+ self.locked = true
78
+ end
79
+
80
+ def unlock(o, event)
81
+ self.locked = false
82
+ end
83
+
84
+ end
85
+
86
+
87
+ end # GlimR
@@ -0,0 +1,37 @@
1
+ require 'pathname'
2
+
3
+
4
+ module GlimR
5
+
6
+ class << self
7
+ attr_accessor :widgets, :theme
8
+
9
+ def for(model, arg_hash = {}, &block)
10
+ widgets[model.class].new(arg_hash.merge(:model => model), &block)
11
+ end
12
+ end
13
+
14
+ self.widgets = {}
15
+ self.theme = Pathname.new(__FILE__).dirname + "default_theme"
16
+
17
+ end
18
+
19
+
20
+ require 'glimr/widget'
21
+
22
+ require 'glimr/widgets/layout'
23
+
24
+ require 'glimr/widgets/image'
25
+ require 'glimr/widgets/stretchable_image'
26
+ require 'glimr/widgets/label'
27
+ require 'glimr/widgets/button'
28
+ require 'glimr/widgets/checkbox'
29
+ require 'glimr/widgets/radiogroup'
30
+
31
+ require 'glimr/widgets/container'
32
+
33
+ require 'glimr/widgets/scrollbar'
34
+ require 'glimr/widgets/scrollable_container'
35
+ require 'glimr/widgets/list'
36
+
37
+ require 'glimr/widgets/text_editor'
@@ -0,0 +1,277 @@
1
+ require 'glimr/widgets/label'
2
+ require 'glimr/widgets/stretchable_image'
3
+
4
+
5
+ module GlimR
6
+
7
+ class EmptyButton < Widget
8
+
9
+ @@image_cache ||= {}
10
+
11
+ def self.load_image(img_name)
12
+ @@image_cache[img_name] ||= Texture.load(GlimR.theme + img_name)
13
+ end
14
+
15
+ touching_accessor :label_down_offset
16
+
17
+ def default_config
18
+ super.merge(
19
+ :fit_children => false,
20
+ :align => :center,
21
+ :valign => :middle,
22
+ :label_down_offset => [0,2],
23
+ :up_background => nil,
24
+ :down_background => nil,
25
+ :up_cover => nil,
26
+ :down_cover => nil,
27
+ :up_hover_indicator => nil,
28
+ :down_hover_indicator => nil,
29
+ :up_focus_indicator => nil,
30
+ :down_focus_indicator => nil
31
+ )
32
+ end
33
+
34
+ attr_accessor :background, :label, :cover, :hover_indicator
35
+ attr_accessor :up_background, :down_background, :up_cover, :down_cover
36
+ attr_reader :down
37
+
38
+ def initialize(*args, &block)
39
+ @image_cache = {}
40
+ super(*args, &nil)
41
+ self.background = @up_background
42
+ @min_width = @background_model.width
43
+ @min_height = @background_model.height
44
+ self.label = @label
45
+ self.background = @up_background
46
+ self.cover = @up_cover
47
+ self.hover_indicator = @up_hover_indicator
48
+ self.focus_indicator = @up_focus_indicator
49
+ on_activate(&block) if block_given?
50
+ end
51
+
52
+ def load_image(img_name, z)
53
+ i = @image_cache[[img_name,z]] ||= (
54
+ tex = self.class.load_image(img_name)
55
+ img = StretchableImage.new
56
+ img.texture = tex
57
+ img.z = z
58
+ img
59
+ )
60
+ i.set_dimensions(@width, @height) unless i.width == @width and i.height == @height
61
+ i
62
+ end
63
+
64
+ def mouse_down(o,e)
65
+ super
66
+ if not @down and e.button == :left
67
+ @down = true
68
+ button_down
69
+ end
70
+ end
71
+
72
+ def mouse_up(o,e)
73
+ super
74
+ if @down and e.button == :left
75
+ dispatch_event(Event.new(:activate)) if @hover
76
+ @down = false
77
+ button_up
78
+ end
79
+ end
80
+
81
+ def key_down(o,e)
82
+ super
83
+ if @focused
84
+ case e.key
85
+ when 13
86
+ @down = true
87
+ button_down
88
+ end
89
+ end
90
+ end
91
+
92
+ def key_up(o,e)
93
+ super
94
+ if @focused and @down
95
+ case e.key
96
+ when 13
97
+ dispatch_event(Event.new(:activate))
98
+ @down = false
99
+ button_up
100
+ end
101
+ elsif @down and (e.key == 13)
102
+ @down = false
103
+ button_up
104
+ end
105
+ end
106
+
107
+ def mouse_out(o,e)
108
+ super
109
+ @hover_indicator_model.visible = false if @hover_indicator_model
110
+ button_up if @down
111
+ end
112
+
113
+ def mouse_over(o,e)
114
+ super
115
+ @hover_indicator_model.visible = true if @hover_indicator_model
116
+ button_down if @down
117
+ end
118
+
119
+ def button_down
120
+ self.background = @down_background
121
+ self.cover = @down_cover
122
+ self.hover_indicator = @down_hover_indicator
123
+ self.focus_indicator = @down_focus_indicator
124
+ self.label_model = @down_label_model
125
+ dispatch_event Event.new(:down)
126
+ end
127
+
128
+ def button_up
129
+ self.background = @up_background
130
+ self.cover = @up_cover
131
+ self.hover_indicator = @up_hover_indicator
132
+ self.focus_indicator = @up_focus_indicator
133
+ self.label_model = @up_label_model
134
+ dispatch_event Event.new(:up)
135
+ end
136
+
137
+ def focused= f
138
+ super
139
+ @focus_indicator_model.visible = f if @focus_indicator_model
140
+ end
141
+
142
+ def label= new_label
143
+ @label = new_label
144
+ case @label
145
+ when Layoutable
146
+ @up_label_model = @label
147
+ @down_label_model = @label
148
+ when nil
149
+ @up_label_model = @down_label_model = nil
150
+ else
151
+ l = Label.new(:text => @label)
152
+ @up_label_model = Label.new(
153
+ :text => @label
154
+ )
155
+ @down_label_model = Label.new(
156
+ :text => @label,
157
+ :margin_left => @label_down_offset[0],
158
+ :margin_top => @label_down_offset[1]
159
+ )
160
+ end
161
+ if @up_label_model
162
+ size_changing {
163
+ self.width = @up_label_model.width + padding_left + padding_right
164
+ self.height = @up_label_model.height + padding_top + padding_bottom
165
+ }
166
+ end
167
+ self.label_model = @up_label_model
168
+ end
169
+
170
+ def label_model= new_label_model
171
+ return if new_label_model == @label_model
172
+ @label_model.detach_self if @label_model
173
+ @label_model = new_label_model
174
+ attach @label_model if @label_model
175
+ end
176
+
177
+ def background= new_bg
178
+ return if @background == new_bg
179
+ @background_model.detach_self if @background_model
180
+ @background_model = case new_bg
181
+ when Layoutable
182
+ new_bg
183
+ when String, Pathname
184
+ load_image(new_bg, -1)
185
+ when nil
186
+ new_bg
187
+ else
188
+ raise "Bad background"
189
+ end
190
+ if @background_model
191
+ attach @background_model
192
+ end
193
+ end
194
+
195
+ def cover= new_cover
196
+ return if @cover == new_cover
197
+ @cover_model.detach_self if @cover_model
198
+ @cover_model = case new_cover
199
+ when Layoutable
200
+ new_cover
201
+ when String, Pathname
202
+ load_image(new_cover, 1)
203
+ when nil
204
+ new_cover
205
+ else
206
+ raise "Bad cover"
207
+ end
208
+ if @cover_model
209
+ attach @cover_model
210
+ end
211
+ end
212
+
213
+ def focus_indicator= new_focus_indicator
214
+ return if @focus_indicator == new_focus_indicator
215
+ @focus_indicator_model.detach_self if @focus_indicator_model
216
+ @focus_indicator_model = case new_focus_indicator
217
+ when Layoutable
218
+ new_focus_indicator
219
+ when String, Pathname
220
+ load_image(new_focus_indicator, 3)
221
+ when nil
222
+ new_focus_indicator
223
+ else
224
+ raise "Bad focus indicator"
225
+ end
226
+ if @focus_indicator_model
227
+ @focus_indicator_model.visible = focused
228
+ attach @focus_indicator_model
229
+ end
230
+ end
231
+
232
+ def hover_indicator= new_hover_indicator
233
+ return if @hover_indicator == new_hover_indicator
234
+ @hover_indicator_model.detach_self if @hover_indicator_model
235
+ @hover_indicator_model = case new_hover_indicator
236
+ when Layoutable
237
+ new_hover_indicator
238
+ when String, Pathname
239
+ load_image(new_hover_indicator, 2)
240
+ when nil
241
+ new_hover_indicator
242
+ else
243
+ raise "Bad hover indicator"
244
+ end
245
+ if @hover_indicator_model
246
+ @hover_indicator_model.visible = @hover
247
+ attach @hover_indicator_model
248
+ end
249
+ end
250
+
251
+ end
252
+
253
+
254
+ class Button < EmptyButton
255
+
256
+ def default_config
257
+ super.merge(
258
+ :up_background => "button_bg.png",
259
+ :down_background => "button_bg_down.png",
260
+ :padding_left => 12,
261
+ :padding_right => 12,
262
+ :padding_top => 4,
263
+ :padding_bottom => 4,
264
+ :up_cover => "button_cover.png",
265
+ :down_cover => "button_cover_down.png",
266
+ :up_hover_indicator => "button_focus.png",
267
+ :down_hover_indicator => "button_focus.png",
268
+ :up_focus_indicator => "button_focus.png",
269
+ :down_focus_indicator => "button_focus.png"
270
+ )
271
+ end
272
+
273
+ end
274
+
275
+
276
+ end
277
+
@@ -0,0 +1,82 @@
1
+ require 'glimr/widgets/button'
2
+ require 'glimr/widgets/layout'
3
+
4
+
5
+ module GlimR
6
+
7
+ class Checkbox < HLayout
8
+
9
+ attr_accessor :label
10
+ ivar_accessor_delegate :@button, :state
11
+
12
+ def default_config
13
+ super.merge(
14
+ :button => CheckboxButton.new,
15
+ :valign => :center,
16
+ :label => ""
17
+ )
18
+ end
19
+
20
+ def initialize(*a, &b)
21
+ super
22
+ @label_model = Label.new(:text => @label)
23
+ self.state = @state if @state
24
+ @button.on_activate{|o,e|
25
+ dispatch_event(Event.new(
26
+ :change, {:source => self, :state => state}
27
+ ))
28
+ }
29
+ attach @button, @label_model
30
+ end
31
+
32
+ def label=(l)
33
+ @label = l
34
+ @label_model.text = l
35
+ end
36
+
37
+ end
38
+
39
+
40
+ class CheckboxButton < EmptyButton
41
+
42
+ attr_accessor :state
43
+
44
+ def default_config
45
+ super.merge(
46
+ :state => false,
47
+ :up_background => "checkbox_bg.png",
48
+ :down_background => "checkbox_checked_bg.png",
49
+ :margin => 2,
50
+ :label_down_offset => [0,0]
51
+ )
52
+ end
53
+
54
+ def initialize(*a, &b)
55
+ super
56
+ if @state
57
+ @state = false
58
+ self.state = true
59
+ end
60
+ end
61
+
62
+ def activate(o,e)
63
+ self.state = !state
64
+ end
65
+
66
+ def toggle
67
+ @up_background, @down_background = @down_background, @up_background
68
+ @up_cover, @down_cover = @down_cover, @up_cover
69
+ @up_label_model, @down_label_model = @down_label_model, @up_label_model
70
+ button_up
71
+ end
72
+
73
+ def state= s
74
+ if (!@state) != (!s) # convert both to boolean
75
+ @state = s
76
+ toggle
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end