Class: Rubygoo::Widget
- Inherits:
-
Object
- Object
- Rubygoo::Widget
- Extended by:
- Publisher
- Includes:
- Inflector
- Defined in:
- lib/rubygoo/widget.rb
Constant Summary collapse
- DEFAULT_PARAMS =
{ :x => 0, :y => 0, :w => 1, :h => 1, :padding_left => 2, :padding_right => 2, :padding_top => 2, :padding_bottom => 2, :relative => false, :enabled => true, :visible => true, }
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#container ⇒ Object
Returns the value of attribute container.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#focus_priority ⇒ Object
Returns the value of attribute focus_priority.
-
#focussed ⇒ Object
Returns the value of attribute focussed.
-
#goo_id ⇒ Object
Returns the value of attribute goo_id.
-
#h ⇒ Object
Returns the value of attribute h.
-
#mouse_over ⇒ Object
Returns the value of attribute mouse_over.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#padding_left ⇒ Object
Returns the value of attribute padding_left.
-
#padding_top ⇒ Object
Returns the value of attribute padding_top.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#relative ⇒ Object
Returns the value of attribute relative.
-
#visible ⇒ Object
Returns the value of attribute visible.
-
#w ⇒ Object
Returns the value of attribute w.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
-
#_draw(screen) ⇒ Object
:nodoc:.
-
#_focus ⇒ Object
:nodoc:.
-
#_key_pressed(event) ⇒ Object
:nodoc:.
-
#_key_released(event) ⇒ Object
:nodoc:.
-
#_mouse_down(event) ⇒ Object
:nodoc:.
-
#_mouse_drag(event) ⇒ Object
:nodoc:.
-
#_mouse_dragging(event) ⇒ Object
:nodoc:.
-
#_mouse_motion(event) ⇒ Object
:nodoc:.
-
#_mouse_up(event) ⇒ Object
:nodoc:.
-
#_unfocus ⇒ Object
:nodoc:.
-
#_update(time) ⇒ Object
:nodoc:.
-
#added ⇒ Object
called when the widget is added to a container.
- #contains?(x, y) ⇒ Boolean
-
#disable ⇒ Object
called when the widget is disabled.
-
#draw(screen) ⇒ Object
draw ourself on the screen.
-
#enable ⇒ Object
called when the widget is enabled.
- #enabled? ⇒ Boolean
-
#focus ⇒ Object
called when the widget receives focus.
- #focussed? ⇒ Boolean
- #get(id) ⇒ Object
-
#get_color(color) ⇒ Object
converts theme property to a GooColor.
-
#hide ⇒ Object
called when the widget is hidden.
-
#initialize(opts = {}, &block) ⇒ Widget
constructor
A new instance of Widget.
-
#key_pressed(event) ⇒ Object
called when a key press is sent to us.
-
#key_released(event) ⇒ Object
called when a key release is sent to us.
- #modal? ⇒ Boolean
-
#mouse_down(event) ⇒ Object
called when there is a mouse click.
-
#mouse_drag(event) ⇒ Object
called when there is a mouse release at the end of a drag.
-
#mouse_dragging(event) ⇒ Object
called when there is a mouse motion and a button pressed.
-
#mouse_enter(event) ⇒ Object
called when the mouse first enters a widget.
-
#mouse_exit(event) ⇒ Object
called when the mouse exits a widget.
-
#mouse_motion(event) ⇒ Object
called when there is a mouse motion and no button pressed.
- #mouse_over? ⇒ Boolean
-
#mouse_up(event) ⇒ Object
called when there is a mouse release w/o drag.
-
#removed ⇒ Object
called when the widget is removed from a container.
-
#show ⇒ Object
called when the widget is shown.
-
#tab_to? ⇒ Boolean
does this widget want tabbed focus? Widget do usually.
-
#theme_property(prop_key) ⇒ Object
gets the property for the class asking for it.
-
#unfocus ⇒ Object
called when the widget loses focus.
-
#update(time) ⇒ Object
called each update cycle with the amount of time that has passed.
- #update_rect ⇒ Object
- #visible? ⇒ Boolean
Methods included from Inflector
#camelize, #classify, #constantize, #dasherize, #demodulize, #foreign_key, #humanize, #inflections, #ordinalize, #pluralize, #singularize, #tableize, #titleize, #underscore
Constructor Details
#initialize(opts = {}, &block) ⇒ Widget
Returns a new instance of Widget.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubygoo/widget.rb', line 49 def initialize(opts={}, &block) merged_opts = DEFAULT_PARAMS.merge opts @x = merged_opts[:x] @y = merged_opts[:y] @padding_left = merged_opts[:padding_left] @padding_right = merged_opts[:padding_right] @padding_top = merged_opts[:padding_top] @padding_bottom = merged_opts[:padding_bottom] @w = merged_opts[:w] @h = merged_opts[:h] @relative = merged_opts[:relative] @enabled = merged_opts[:enabled] @visible = merged_opts[:visible] @goo_id = merged_opts[:id] @opts = merged_opts update_rect instance_eval &block if block_given? end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def app @app end |
#container ⇒ Object
Returns the value of attribute container.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def container @container end |
#enabled ⇒ Object
Returns the value of attribute enabled.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def enabled @enabled end |
#focus_priority ⇒ Object
Returns the value of attribute focus_priority.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def focus_priority @focus_priority end |
#focussed ⇒ Object
Returns the value of attribute focussed.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def focussed @focussed end |
#goo_id ⇒ Object
Returns the value of attribute goo_id.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def goo_id @goo_id end |
#h ⇒ Object
Returns the value of attribute h.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def h @h end |
#mouse_over ⇒ Object
Returns the value of attribute mouse_over.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def mouse_over @mouse_over end |
#opts ⇒ Object
Returns the value of attribute opts.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def opts @opts end |
#padding_left ⇒ Object
Returns the value of attribute padding_left.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def padding_left @padding_left end |
#padding_top ⇒ Object
Returns the value of attribute padding_top.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def padding_top @padding_top end |
#parent ⇒ Object
Returns the value of attribute parent.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def parent @parent end |
#relative ⇒ Object
Returns the value of attribute relative.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def relative @relative end |
#visible ⇒ Object
Returns the value of attribute visible.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def visible @visible end |
#w ⇒ Object
Returns the value of attribute w.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
15 16 17 |
# File 'lib/rubygoo/widget.rb', line 15 def y @y end |
Class Method Details
.goo_prop(*keys) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubygoo/widget.rb', line 34 def self.goo_prop(*keys) for k in keys define_method k do |*args| v = args.shift ivar = "@#{k.to_s}" instance_variable_set(ivar,v) if v instance_variable_get(ivar) end end end |
.inherited(by_obj) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/rubygoo/widget.rb', line 71 def self.inherited(by_obj) meth = Inflector.underscore(Inflector.demodulize(by_obj)).to_sym if meth == :app Object.class_eval "def goo_app(*args, &block);#{by_obj}.new(*args,&block);end" else Widget.class_eval "def #{meth}(*args, &block);obj=#{by_obj}.new(*args,&block);add(obj) if self.respond_to?(:add);obj;end" end end |
Instance Method Details
#_draw(screen) ⇒ Object
:nodoc:
312 313 314 |
# File 'lib/rubygoo/widget.rb', line 312 def _draw(screen) #:nodoc: draw screen end |
#_focus ⇒ Object
:nodoc:
270 271 272 273 274 |
# File 'lib/rubygoo/widget.rb', line 270 def _focus() #:nodoc: fire :focus @focussed = true focus end |
#_key_pressed(event) ⇒ Object
:nodoc:
302 303 304 305 |
# File 'lib/rubygoo/widget.rb', line 302 def _key_pressed(event) #:nodoc: fire :key_pressed, event key_pressed event end |
#_key_released(event) ⇒ Object
:nodoc:
307 308 309 310 |
# File 'lib/rubygoo/widget.rb', line 307 def _key_released(event) #:nodoc: fire :key_released, event key_released event end |
#_mouse_down(event) ⇒ Object
:nodoc:
297 298 299 300 |
# File 'lib/rubygoo/widget.rb', line 297 def _mouse_down(event) #:nodoc: fire :mouse_down, event mouse_down event end |
#_mouse_drag(event) ⇒ Object
:nodoc:
287 288 289 290 |
# File 'lib/rubygoo/widget.rb', line 287 def _mouse_drag(event) #:nodoc: fire :mouse_drag, event mouse_drag event end |
#_mouse_dragging(event) ⇒ Object
:nodoc:
282 283 284 285 |
# File 'lib/rubygoo/widget.rb', line 282 def _mouse_dragging(event) #:nodoc: fire :mouse_dragging, event mouse_dragging event end |
#_mouse_motion(event) ⇒ Object
:nodoc:
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rubygoo/widget.rb', line 92 def _mouse_motion(event) #:nodoc: if contains?(event.data[:x],event.data[:y]) unless @mouse_over @mouse_over = true fire :mouse_enter, event mouse_enter event end else if @mouse_over @mouse_over = false fire :mouse_exit, event mouse_exit event end end fire :mouse_motion, event mouse_motion event end |
#_mouse_up(event) ⇒ Object
:nodoc:
292 293 294 295 |
# File 'lib/rubygoo/widget.rb', line 292 def _mouse_up(event) #:nodoc: fire :mouse_up, event mouse_up event end |
#_unfocus ⇒ Object
:nodoc:
276 277 278 279 280 |
# File 'lib/rubygoo/widget.rb', line 276 def _unfocus() #:nodoc: fire :unfocus @focussed = false unfocus end |
#_update(time) ⇒ Object
:nodoc:
266 267 268 |
# File 'lib/rubygoo/widget.rb', line 266 def _update(time) #:nodoc: update time end |
#added ⇒ Object
called when the widget is added to a container
330 331 |
# File 'lib/rubygoo/widget.rb', line 330 def added() end |
#contains?(x, y) ⇒ Boolean
84 85 86 |
# File 'lib/rubygoo/widget.rb', line 84 def contains?(x, y) @rect.collide_point? x, y end |
#disable ⇒ Object
called when the widget is disabled
229 230 231 232 233 234 |
# File 'lib/rubygoo/widget.rb', line 229 def disable() if enabled? fire :disable @enabled = false end end |
#draw(screen) ⇒ Object
draw ourself on the screen
379 380 |
# File 'lib/rubygoo/widget.rb', line 379 def draw(screen) end |
#enable ⇒ Object
called when the widget is enabled
237 238 239 240 241 242 |
# File 'lib/rubygoo/widget.rb', line 237 def enable() unless enabled? fire :enable @enabled = true end end |
#enabled? ⇒ Boolean
224 225 226 |
# File 'lib/rubygoo/widget.rb', line 224 def enabled?() @enabled end |
#focus ⇒ Object
called when the widget receives focus
383 384 |
# File 'lib/rubygoo/widget.rb', line 383 def focus() end |
#focussed? ⇒ Boolean
110 111 112 |
# File 'lib/rubygoo/widget.rb', line 110 def focussed?() @focussed end |
#get(id) ⇒ Object
390 391 392 |
# File 'lib/rubygoo/widget.rb', line 390 def get(id) @goo_id == id ? self : nil end |
#get_color(color) ⇒ Object
converts theme property to a GooColor
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rubygoo/widget.rb', line 160 def get_color(color) new_color = nil if color.is_a? Array if color.size > 3 new_color = color else # fill in zeros for all other colors (3 - color.size).times do color << 0 end # fill in alpha as 255 color << 255 end elsif color.is_a? Symbol new_color = COLORS[color] else raise "invalid color" end GooColor.new *new_color end |
#hide ⇒ Object
called when the widget is hidden
249 250 251 252 253 254 255 |
# File 'lib/rubygoo/widget.rb', line 249 def hide() if visible? fire :hide @visible = false disable end end |
#key_pressed(event) ⇒ Object
called when a key press is sent to us
371 372 |
# File 'lib/rubygoo/widget.rb', line 371 def key_pressed(event) end |
#key_released(event) ⇒ Object
called when a key release is sent to us
375 376 |
# File 'lib/rubygoo/widget.rb', line 375 def key_released(event) end |
#modal? ⇒ Boolean
325 326 327 |
# File 'lib/rubygoo/widget.rb', line 325 def modal?() false end |
#mouse_down(event) ⇒ Object
called when there is a mouse click
351 352 |
# File 'lib/rubygoo/widget.rb', line 351 def mouse_down(event) end |
#mouse_drag(event) ⇒ Object
called when there is a mouse release at the end of a drag
355 356 |
# File 'lib/rubygoo/widget.rb', line 355 def mouse_drag(event) end |
#mouse_dragging(event) ⇒ Object
called when there is a mouse motion and a button pressed
343 344 |
# File 'lib/rubygoo/widget.rb', line 343 def mouse_dragging(event) end |
#mouse_enter(event) ⇒ Object
called when the mouse first enters a widget
359 360 |
# File 'lib/rubygoo/widget.rb', line 359 def mouse_enter(event) end |
#mouse_exit(event) ⇒ Object
called when the mouse exits a widget
363 364 |
# File 'lib/rubygoo/widget.rb', line 363 def mouse_exit(event) end |
#mouse_motion(event) ⇒ Object
called when there is a mouse motion and no button pressed
367 368 |
# File 'lib/rubygoo/widget.rb', line 367 def mouse_motion(event) end |
#mouse_over? ⇒ Boolean
88 89 90 |
# File 'lib/rubygoo/widget.rb', line 88 def mouse_over?() @mouse_over end |
#mouse_up(event) ⇒ Object
called when there is a mouse release w/o drag
347 348 |
# File 'lib/rubygoo/widget.rb', line 347 def mouse_up(event) end |
#removed ⇒ Object
called when the widget is removed from a container
334 335 |
# File 'lib/rubygoo/widget.rb', line 334 def removed() end |
#show ⇒ Object
called when the widget is shown
258 259 260 261 262 263 264 |
# File 'lib/rubygoo/widget.rb', line 258 def show() unless visible? fire :show @visible = true enable end end |
#tab_to? ⇒ Boolean
does this widget want tabbed focus? Widget do usually
321 322 323 |
# File 'lib/rubygoo/widget.rb', line 321 def tab_to?() true end |
#theme_property(prop_key) ⇒ Object
gets the property for the class asking for it. it will walk up the object hierarchy if needed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rubygoo/widget.rb', line 116 def theme_property(prop_key) prop = nil return @opts[prop_key] if @opts and @opts[prop_key] # look up based on :id if @goo_id id_theme = app.theme[@goo_id] if id_theme id_prop = id_theme[prop_key] if id_prop return nil if id_prop == :none prop = id_prop end end end unless prop # look up based on class parent = self.class until parent == Object class_theme = app.theme[parent.to_s] if class_theme class_prop = class_theme[prop_key] if class_prop return nil if class_prop == :none prop = class_prop break end end parent = parent.superclass end end # replace colors w/ GooColor if prop_key.to_s.match(/color/i) and prop get_color prop else prop end end |
#unfocus ⇒ Object
called when the widget loses focus
387 388 |
# File 'lib/rubygoo/widget.rb', line 387 def unfocus() end |
#update(time) ⇒ Object
called each update cycle with the amount of time that has passed. useful for animations, etc
339 340 |
# File 'lib/rubygoo/widget.rb', line 339 def update(time) end |
#update_rect ⇒ Object
80 81 82 |
# File 'lib/rubygoo/widget.rb', line 80 def update_rect() @rect = Rect.new [@x,@y,@w,@h] end |
#visible? ⇒ Boolean
244 245 246 |
# File 'lib/rubygoo/widget.rb', line 244 def visible?() @visible end |