rubygoo 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,11 @@ module Rubygoo
10
10
  @screen.draw_box [x1,y1], [x2,y2], convert_color(color)
11
11
  end
12
12
 
13
+ def draw_line(x1,y1,x2,y2,color)
14
+ c = convert_color(color)
15
+ @screen.draw_line_a [x1, y1], [x2, y2], c
16
+ end
17
+
13
18
  def draw_circle(cx,cy,radius,color)
14
19
  @screen.draw_circle_a [cx,cy],radius,convert_color(color)
15
20
  end
@@ -36,8 +41,22 @@ module Rubygoo
36
41
  @screen.flip
37
42
  end
38
43
 
39
- def draw_image(img, x, y, color=nil)
40
- img.blit @screen, [x,y]
44
+ def draw_image(img, to_x, to_y, color=nil)
45
+ img.blit @screen, [to_x,to_y]
46
+ end
47
+
48
+ def draw_partial_image(img, to_x, to_y,
49
+ from_x=nil,from_y=nil,from_w=nil,from_h=nil, color=nil)
50
+ if from_x
51
+ if from_w
52
+ from = [from_x,from_y,from_w,from_h]
53
+ else
54
+ from = [from_x,from_y]
55
+ end
56
+ img.blit @screen, [to_x,to_y], from
57
+ else
58
+ img.blit @screen, [to_x,to_y]
59
+ end
41
60
  end
42
61
 
43
62
  def size_text(text, font_file, font_size)
@@ -9,7 +9,7 @@ module Rubygoo
9
9
  DEFAULT_PARAMS = {:theme=>'default',:x=>10,:y=>10,:data_dir=>File.join(File.dirname(__FILE__),"..","..","themes"),:mouse_cursor => true}
10
10
  attr_accessor :theme_name, :theme, :data_dir, :theme_dir, :renderer, :tab_groups, :mouse
11
11
 
12
- def initialize(opts={})
12
+ def initialize(opts={}, &block)
13
13
  merged_opts = DEFAULT_PARAMS.merge opts
14
14
  @widgets = []
15
15
  @tab_groups = []
@@ -20,7 +20,6 @@ module Rubygoo
20
20
  @theme_name = theme_name
21
21
  @theme = load_theme theme_name
22
22
  @renderer = merged_opts[:renderer]
23
- super merged_opts
24
23
 
25
24
  # should this go here?
26
25
  @mouse_cursor = merged_opts[:mouse_cursor]
@@ -31,6 +30,8 @@ module Rubygoo
31
30
  @mouse.added
32
31
  end
33
32
  @mouse_dragging = false
33
+
34
+ super merged_opts, &block
34
35
  end
35
36
 
36
37
  def app()
@@ -160,7 +161,11 @@ module Rubygoo
160
161
  def update(time)
161
162
  _update(time)
162
163
  end
163
-
164
-
165
164
  end
166
165
  end
166
+ #class Object
167
+ # p "DEFINING IN OBJECT"
168
+ # def app(*args, &block)
169
+ # ::Rubygoo::App.new(*args,&block)
170
+ # end
171
+ #end
@@ -1,12 +1,17 @@
1
1
  module Rubygoo
2
2
  class Button < Widget
3
3
  can_fire :pressed
4
- def initialize(text, opts={})
5
- super opts
6
4
 
7
- @icon = opts[:icon]
5
+ attr_reader :text
6
+ goo_prop :icon_image, :image, :hover_image
7
+
8
+ def initialize(new_text, opts={})
9
+ @icon_image = opts[:icon]
10
+ @image = opts[:image]
11
+ @hover_image = opts[:hover_image]
12
+ @text = new_text
8
13
 
9
- @text = text
14
+ super opts
10
15
  end
11
16
 
12
17
  def added()
@@ -21,12 +26,19 @@ module Rubygoo
21
26
  @disabled_color = theme_property :disabled_color
22
27
 
23
28
  @font_file = File.join(@app.theme_dir,font)
24
- @rendered_text ||= @app.renderer.render_text @text, @font_file, @font_size, @color
25
-
26
- @w = @rendered_text.width+2*@x_pad
27
- @h = @rendered_text.height+2*@y_pad
28
- @x = @x - @x_pad
29
- @y = @y - @y_pad
29
+ @rendered_text ||= @app.renderer.render_text @text, @font_file, @font_size, @color if @text and !@text.empty?
30
+
31
+ if @image
32
+ @w = @image.width+2*@padding_left
33
+ @h = @image.height+2*@padding_top
34
+ @x = @x - @padding_left
35
+ @y = @y - @padding_top
36
+ else
37
+ @w = @rendered_text.width+2*@padding_left
38
+ @h = @rendered_text.height+2*@padding_top
39
+ @x = @x - @padding_left
40
+ @y = @y - @padding_top
41
+ end
30
42
 
31
43
  update_rect
32
44
  end
@@ -55,33 +67,42 @@ module Rubygoo
55
67
  x2 = @rect[2] + x1
56
68
  y2 = @rect[3] + y1
57
69
 
58
-
59
- if @focussed
60
- adapter.fill x1, y1, x2, y2, @focus_color
61
- elsif @bg_color
62
- adapter.fill x1, y1, x2, y2, @bg_color
63
- end
64
- if @border_color
65
- adapter.draw_box x1, y1, x2, y2, @border_color
66
- end
67
-
68
- if mouse_over? and @hover_color
69
- adapter.fill x1, y1, x2, y2, @hover_color
70
+ img = @image.nil? ? @rendered_text : @image
71
+ if @image
72
+ adapter.draw_image @image, @x+@padding_left, @y+@padding_top, @color
73
+ else
74
+
75
+ if @focussed
76
+ adapter.fill x1, y1, x2, y2, @focus_color
77
+ elsif @bg_color
78
+ adapter.fill x1, y1, x2, y2, @bg_color
79
+ end
80
+ if @border_color
81
+ adapter.draw_box x1, y1, x2, y2, @border_color
82
+ end
83
+
84
+ if @disabled_color and !enabled?
85
+ adapter.fill x1, y1, x2, y2, @disabled_color
86
+ end
87
+
88
+ if @icon_image
89
+ # TODO center icon
90
+ ix = x1#+((x2-x1)-@icon.w)
91
+ iy = y1#+((y2-y1)-@icon.h)
92
+ adapter.draw_image @icon_image, ix+@padding_left,iy+@padding_top
93
+ end
94
+
95
+ adapter.draw_image @rendered_text, @x+@padding_left, @y+@padding_top, @color
70
96
  end
71
97
 
72
- if @disabled_color and !enabled?
73
- adapter.fill x1, y1, x2, y2, @disabled_color
98
+ if mouse_over? and (@hover_image or @hover_color)
99
+ if @hover_image
100
+ adapter.draw_image @hover_image, @x+@padding_left, @y+@padding_top, @color
101
+ else
102
+ adapter.fill x1, y1, x2, y2, @hover_color
103
+ end
74
104
  end
75
105
 
76
- if @icon
77
- # TODO center icon
78
- ix = x1#+((x2-x1)-@icon.w)
79
- iy = y1#+((y2-y1)-@icon.h)
80
- adapter.draw_image @icon, ix+@x_pad,iy+@y_pad
81
- end
82
-
83
- adapter.draw_image @rendered_text, @x+@x_pad, @y+@y_pad, @color
84
-
85
106
  end
86
107
  end
87
108
  end
@@ -2,17 +2,22 @@ require 'label'
2
2
  module Rubygoo
3
3
  class CheckBox < Widget
4
4
  attr_accessor :checked
5
+ attr_reader :label_text
5
6
  can_fire :checked
7
+
6
8
  DEFAULT_PARAMS = {:align=>:right}
9
+ DEFAULT_PARAMS.keys.each do |k|
10
+ goo_prop k
11
+ end
12
+
7
13
  def initialize(opts={})
8
14
  opts = DEFAULT_PARAMS.merge opts
9
- super opts
10
15
  @checked = opts[:checked]
11
16
 
12
- # only supports label on the right
13
- # TODO maybe I should do this in added
14
- @label_text = opts[:label]
17
+ @label_text = opts[:label_text]
15
18
  @label_alignment = opts[:align]
19
+
20
+ super opts
16
21
  end
17
22
 
18
23
  def added()
@@ -23,18 +28,18 @@ module Rubygoo
23
28
  @checked_color = theme_property :checked_color
24
29
  @hover_color = theme_property :hover_color
25
30
 
26
- @rect = Rect.new [@x-@x_pad,@y-@y_pad,@w+2*@x_pad,@h+2*@y_pad]
31
+ @rect = Rect.new [@x-@padding_left,@y-@padding_top,@w+2*@padding_left,@h+2*@padding_top]
27
32
  unless @label_text.nil? or @label_text.empty?
28
33
  @label = Label.new @label_text, :x=>0,:y=>0, :relative=>@relative, :visible=>false
29
34
  @parent.add @label
30
35
 
31
36
  case @label_alignment
32
37
  when :right
33
- lx = @x+2*@x_pad+@w
38
+ lx = @x+2*@padding_left+@w
34
39
  ly = @y
35
40
  when :left
36
41
  ly = @y
37
- lx = @x-2*@x_pad-@label.w
42
+ lx = @x-2*@padding_left-@label.w
38
43
  end
39
44
  @label.x = lx
40
45
  @label.y = ly
@@ -94,7 +99,7 @@ module Rubygoo
94
99
  end
95
100
 
96
101
  if @checked
97
- rect = @rect.inflate(-@x_pad,-@y_pad)
102
+ rect = @rect.inflate(-@padding_left,-@padding_top)
98
103
  cx1 = rect[0]
99
104
  cy1 = rect[1]
100
105
  cx2 = rect[2] + x1
@@ -110,6 +115,13 @@ module Rubygoo
110
115
  adapter.draw_box x1, y1, x2, y2, @border_color
111
116
  end
112
117
  end
118
+
119
+ # DSL methods
120
+ def label_text(new_val=nil)
121
+ @label_text = new_val if new_val
122
+ @label_text
123
+ end
124
+
113
125
  end
114
126
  end
115
127
 
@@ -3,12 +3,14 @@ module Rubygoo
3
3
  attr_accessor :widgets, :bg_color, :rect, :queued_widgets
4
4
 
5
5
  def initialize(opts={})
6
- super opts
6
+ @auto_resize = opts[:auto_resize]
7
7
  # cannot get this if we don't have an app yet
8
8
  @bg_color = theme_property :bg_color if self.app
9
9
  @queued_widgets = []
10
10
  @widgets = []
11
11
  @modal_widgets = []
12
+
13
+ super opts
12
14
  end
13
15
 
14
16
  # called when we are added to another container
@@ -36,8 +38,10 @@ module Rubygoo
36
38
  end
37
39
  w.added
38
40
 
39
- w.when :resized do |resized_widget|
40
- resize resized_widget
41
+ if @auto_resize
42
+ w.when :resized do |resized_widget|
43
+ resize resized_widget
44
+ end
41
45
  end
42
46
 
43
47
  @widgets << w
@@ -61,6 +65,17 @@ module Rubygoo
61
65
  end
62
66
  end
63
67
 
68
+ # Remove all our children from the container.
69
+ def clear()
70
+ death_widgets = @widgets + @queued_widgets + @modal_widgets
71
+ @widgets = []
72
+ @queued_widgets = []
73
+ @modal_widgets = []
74
+ death_widgets.each do |w|
75
+ w.removed
76
+ end
77
+ end
78
+
64
79
  # draw ourself and our children
65
80
  def _draw(adapter)
66
81
  # any container specific code here (border_colors?)
@@ -115,6 +130,14 @@ module Rubygoo
115
130
  end
116
131
  end
117
132
 
133
+ # called when there is motion w/ the mouse button down
134
+ def _mouse_dragging(event)
135
+ mouse_dragging event
136
+ @widgets.select{|w| w.contains? event.data[:x],event.data[:y] and w.enabled?}.each do |w|
137
+ w._mouse_dragging event
138
+ end
139
+ end
140
+
118
141
  # pass on the key press to our widgets
119
142
  def _key_pressed(event)
120
143
  key_pressed event
@@ -156,10 +179,22 @@ module Rubygoo
156
179
  max_w = w_width if w_width > max_w
157
180
  max_h = w_height if w_height > max_h
158
181
  end
159
- @w = max_w - @x + 2*@x_pad
160
- @h = max_h - @y + 2*@y_pad
182
+ @w = max_w - @x + 2*@padding_left
183
+ @h = max_h - @y + 2*@padding_top
161
184
  update_rect
162
185
  end
186
+
187
+ # def self.inherited(by_obj)
188
+ # Widget.inherited(by_obj)
189
+ # end
190
+
191
+ # returns the child widget with the id passed in
192
+ def get(id)
193
+ @widgets.each do |w|
194
+ found_w = w.get(id)
195
+ return found_w if found_w
196
+ end
197
+ end
163
198
  end
164
199
  end
165
200
 
@@ -19,8 +19,16 @@ module Rubygoo
19
19
  # TODO center icon
20
20
  ix = x1#+((x2-x1)-@icon.w)
21
21
  iy = y1#+((y2-y1)-@icon.h)
22
- adapter.draw_image @icon, ix+@x_pad,iy+@y_pad
22
+ adapter.draw_image @icon, ix+@padding_left,iy+@padding_top
23
23
  end
24
+
25
+ #DSL methods
26
+ def icon_image(new_val=nil)
27
+ @icon = new_val if new_val
28
+ @icon
29
+ end
30
+
24
31
  end
32
+
25
33
  end
26
34
 
@@ -1,6 +1,7 @@
1
1
  require 'publisher'
2
2
  module Rubygoo
3
3
  class Label < Widget
4
+ attr_reader :text
4
5
  def initialize(text, opts={})
5
6
  @font_size = opts[:font_size]
6
7
  super opts
@@ -17,11 +17,11 @@ module Rubygoo
17
17
  end
18
18
 
19
19
  if @checked
20
- adapter.draw_circle_filled @rect.centerx, @rect.centery, radius-@x_pad, @checked_color
20
+ adapter.draw_circle_filled @rect.centerx, @rect.centery, radius-@padding_left, @checked_color
21
21
  end
22
22
 
23
23
  if mouse_over? and @hover_color
24
- adapter.draw_circle_filled @rect.centerx, @rect.centery, radius-@x_pad, @hover_color
24
+ adapter.draw_circle_filled @rect.centerx, @rect.centery, radius-@padding_left, @hover_color
25
25
  end
26
26
 
27
27
  if @border_color
@@ -2,6 +2,7 @@ module Rubygoo
2
2
  class TextField < Widget
3
3
  SPACE = " "
4
4
 
5
+ attr_reader :text
5
6
  def initialize(text, opts={})
6
7
  super opts
7
8
  @text = text
@@ -58,14 +59,14 @@ module Rubygoo
58
59
 
59
60
  def render()
60
61
  if @text.empty?
61
- w = [@w,@min_w].max + @x_pad
62
- h = [@h,@min_h].max + @y_pad
62
+ w = [@w,@min_w].max + @padding_left
63
+ h = [@h,@min_h].max + @padding_top
63
64
  @rendered_text = nil
64
65
  @rect = Rect.new [@x,@y,w,h]
65
66
  else
66
67
  @rendered_text = @app.renderer.render_text @text, @font_file, @font_size, @color
67
- w = [@rendered_text.width,@min_w].max + @x_pad
68
- h = [@rendered_text.height,@min_h].max + @y_pad
68
+ w = [@rendered_text.width,@min_w].max + @padding_left
69
+ h = [@rendered_text.height,@min_h].max + @padding_top
69
70
  if @max_length
70
71
  w = [w,@max_w].min
71
72
  h = [h,@max_h].min
@@ -287,9 +288,9 @@ module Rubygoo
287
288
  end
288
289
 
289
290
  when K_DELETE
290
- if @select_pos != @caret_pos:
291
+ if @select_pos != @caret_pos
291
292
  delete_selected()
292
- elsif @caret_pos < @text.length:
293
+ elsif @caret_pos < @text.length
293
294
  @text = @text.slice(0,@caret_pos) + @text.slice(@caret_pos+1,@text.length-@caret_pos-1)
294
295
  @select_pos = @caret_pos
295
296
  end
@@ -1,9 +1,11 @@
1
1
  require 'publisher'
2
2
  require 'goo_color'
3
3
  require 'rect'
4
+ require 'inflector'
4
5
  module Rubygoo
5
6
  class Widget
6
7
  extend Publisher
8
+ include Inflector
7
9
 
8
10
  # fire resized whenever a dimensional variable changes
9
11
  can_fire :resized, :mouse_enter, :mouse_exit, :focus, :unfocus,
@@ -11,7 +13,7 @@ module Rubygoo
11
13
  :mouse_dragging, :mouse_motion, :enable, :disable, :hide, :show
12
14
 
13
15
  attr_accessor :enabled, :parent, :container, :opts,
14
- :x, :y, :w, :h, :app, :x_pad, :y_pad, :focussed,
16
+ :x, :y, :w, :h, :app, :padding_left, :padding_top, :focussed, :goo_id,
15
17
  :focus_priority, :relative, :mouse_over, :visible
16
18
 
17
19
  DEFAULT_PARAMS = {
@@ -19,27 +21,60 @@ module Rubygoo
19
21
  :y => 0,
20
22
  :w => 1,
21
23
  :h => 1,
22
- :x_pad => 2,
23
- :y_pad => 2,
24
+ :padding_left => 2,
25
+ :padding_right => 2,
26
+ :padding_top => 2,
27
+ :padding_bottom => 2,
24
28
  :relative => false,
25
29
  :enabled => true,
26
30
  :visible => true,
27
31
  }
28
- def initialize(opts={})
32
+
33
+
34
+ def self.goo_prop(*keys)
35
+ for k in keys
36
+ define_method k do |*args|
37
+ v = args.shift
38
+ ivar = "@#{k.to_s}"
39
+ instance_variable_set(ivar,v) if v
40
+ instance_variable_get(ivar)
41
+ end
42
+ end
43
+ end
44
+
45
+ DEFAULT_PARAMS.keys.each do |k|
46
+ goo_prop k
47
+ end
48
+
49
+ def initialize(opts={}, &block)
29
50
  merged_opts = DEFAULT_PARAMS.merge opts
30
51
  @x = merged_opts[:x]
31
52
  @y = merged_opts[:y]
32
- @x_pad = merged_opts[:x_pad]
33
- @y_pad = merged_opts[:y_pad]
53
+ @padding_left = merged_opts[:padding_left]
54
+ @padding_right = merged_opts[:padding_right]
55
+ @padding_top = merged_opts[:padding_top]
56
+ @padding_bottom = merged_opts[:padding_bottom]
34
57
  @w = merged_opts[:w]
35
58
  @h = merged_opts[:h]
36
59
  @relative = merged_opts[:relative]
37
60
  @enabled = merged_opts[:enabled]
38
61
  @visible = merged_opts[:visible]
62
+ @goo_id = merged_opts[:id]
39
63
 
40
64
  @opts = merged_opts
41
65
 
42
66
  update_rect
67
+
68
+ instance_eval &block if block_given?
69
+ end
70
+
71
+ def self.inherited(by_obj)
72
+ meth = Inflector.underscore(Inflector.demodulize(by_obj)).to_sym
73
+ if meth == :app
74
+ Object.class_eval "def goo_app(*args, &block);#{by_obj}.new(*args,&block);end"
75
+ else
76
+ Widget.class_eval "def #{meth}(*args, &block);obj=#{by_obj}.new(*args,&block);add(obj) if self.respond_to?(:add);obj;end"
77
+ end
43
78
  end
44
79
 
45
80
  def update_rect()
@@ -80,19 +115,40 @@ module Rubygoo
80
115
  # it will walk up the object hierarchy if needed
81
116
  def theme_property(prop_key)
82
117
  prop = nil
83
- parent = self.class
84
- until parent == Object
85
- class_theme = app.theme[parent.to_s]
86
- if class_theme
87
- class_prop = class_theme[prop_key]
88
- if class_prop
89
- return nil if class_prop == :none
90
- prop = class_prop
91
- break
118
+ return @opts[prop_key] if @opts and @opts[prop_key]
119
+
120
+
121
+ # look up based on :id
122
+ if @goo_id
123
+ id_theme = app.theme[@goo_id]
124
+ if id_theme
125
+ id_prop = id_theme[prop_key]
126
+ if id_prop
127
+ return nil if id_prop == :none
128
+ prop = id_prop
92
129
  end
93
130
  end
94
- parent = parent.superclass
131
+
95
132
  end
133
+
134
+ unless prop
135
+ # look up based on class
136
+ parent = self.class
137
+ until parent == Object
138
+ class_theme = app.theme[parent.to_s]
139
+ if class_theme
140
+ class_prop = class_theme[prop_key]
141
+ if class_prop
142
+ return nil if class_prop == :none
143
+ prop = class_prop
144
+ break
145
+ end
146
+ end
147
+ parent = parent.superclass
148
+ end
149
+ end
150
+
151
+ # replace colors w/ GooColor
96
152
  if prop_key.to_s.match(/color/i) and prop
97
153
  get_color prop
98
154
  else
@@ -152,15 +208,15 @@ module Rubygoo
152
208
  end
153
209
 
154
210
  # sets x padding and fires resized event
155
- def x_pad=(val)
156
- @x_pad = val
211
+ def padding_left=(val)
212
+ @padding_left = val
157
213
  update_rect
158
214
  fire :resized, self
159
215
  end
160
216
 
161
217
  # sets y padding and fires resized event
162
- def y_pad=(val)
163
- @y_pad = val
218
+ def padding_top=(val)
219
+ @padding_top = val
164
220
  update_rect
165
221
  fire :resized, self
166
222
  end
@@ -331,5 +387,9 @@ module Rubygoo
331
387
  def unfocus()
332
388
  end
333
389
 
390
+ def get(id)
391
+ @goo_id == id ? self : nil
392
+ end
393
+
334
394
  end
335
395
  end