Class: Rubygoo::Widget

Inherits:
Object
  • Object
show all
Extended by:
Publisher
Defined in:
lib/rubygoo/widget.rb

Direct Known Subclasses

Button, CheckBox, Container, Icon, Label, MouseCursor

Constant Summary collapse

DEFAULT_PARAMS =
{
  :x => 0,
  :y => 0,
  :w => 1,
  :h => 1,
  :x_pad => 2,
  :y_pad => 2,
  :relative => false,
  :enabled => true,
  :visible => true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Widget

Returns a new instance of Widget.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubygoo/widget.rb', line 28

def initialize(opts={})
  merged_opts = DEFAULT_PARAMS.merge opts
  @x = merged_opts[:x]
  @y = merged_opts[:y]
  @x_pad = merged_opts[:x_pad]
  @y_pad = merged_opts[:y_pad]
  @w = merged_opts[:w]
  @h = merged_opts[:h]
  @relative = merged_opts[:relative]
  @enabled = merged_opts[:enabled]
  @visible = merged_opts[:visible]

  @opts = merged_opts

  update_rect
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def app
  @app
end

#containerObject

Returns the value of attribute container.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def container
  @container
end

#enabledObject

Returns the value of attribute enabled.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def enabled
  @enabled
end

#focus_priorityObject

Returns the value of attribute focus_priority.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def focus_priority
  @focus_priority
end

#focussedObject

Returns the value of attribute focussed.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def focussed
  @focussed
end

#hObject

Returns the value of attribute h.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def h
  @h
end

#mouse_overObject

Returns the value of attribute mouse_over.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def mouse_over
  @mouse_over
end

#optsObject

Returns the value of attribute opts.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def opts
  @opts
end

#parentObject

Returns the value of attribute parent.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def parent
  @parent
end

#relativeObject

Returns the value of attribute relative.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def relative
  @relative
end

#visibleObject

Returns the value of attribute visible.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def visible
  @visible
end

#wObject

Returns the value of attribute w.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def w
  @w
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def x
  @x
end

#x_padObject

Returns the value of attribute x_pad.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def x_pad
  @x_pad
end

#yObject

Returns the value of attribute y.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def y
  @y
end

#y_padObject

Returns the value of attribute y_pad.



13
14
15
# File 'lib/rubygoo/widget.rb', line 13

def y_pad
  @y_pad
end

Instance Method Details

#_draw(screen) ⇒ Object

:nodoc:



256
257
258
# File 'lib/rubygoo/widget.rb', line 256

def _draw(screen) #:nodoc:
  draw screen
end

#_focusObject

:nodoc:



214
215
216
217
218
# File 'lib/rubygoo/widget.rb', line 214

def _focus() #:nodoc:
  fire :focus
  @focussed = true
  focus
end

#_key_pressed(event) ⇒ Object

:nodoc:



246
247
248
249
# File 'lib/rubygoo/widget.rb', line 246

def _key_pressed(event) #:nodoc:
  fire :key_pressed, event
  key_pressed event
end

#_key_released(event) ⇒ Object

:nodoc:



251
252
253
254
# File 'lib/rubygoo/widget.rb', line 251

def _key_released(event) #:nodoc:
  fire :key_released, event
  key_released event
end

#_mouse_down(event) ⇒ Object

:nodoc:



241
242
243
244
# File 'lib/rubygoo/widget.rb', line 241

def _mouse_down(event) #:nodoc:
  fire :mouse_down, event
  mouse_down event
end

#_mouse_drag(event) ⇒ Object

:nodoc:



231
232
233
234
# File 'lib/rubygoo/widget.rb', line 231

def _mouse_drag(event) #:nodoc:
  fire :mouse_drag, event
  mouse_drag event
end

#_mouse_dragging(event) ⇒ Object

:nodoc:



226
227
228
229
# File 'lib/rubygoo/widget.rb', line 226

def _mouse_dragging(event) #:nodoc:
  fire :mouse_dragging, event
  mouse_dragging event
end

#_mouse_motion(event) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubygoo/widget.rb', line 57

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:



236
237
238
239
# File 'lib/rubygoo/widget.rb', line 236

def _mouse_up(event) #:nodoc:
  fire :mouse_up, event
  mouse_up event
end

#_unfocusObject

:nodoc:



220
221
222
223
224
# File 'lib/rubygoo/widget.rb', line 220

def _unfocus() #:nodoc:
  fire :unfocus
  @focussed = false
  unfocus
end

#_update(time) ⇒ Object

:nodoc:



210
211
212
# File 'lib/rubygoo/widget.rb', line 210

def _update(time) #:nodoc:
  update time
end

#addedObject

called when the widget is added to a container



274
275
# File 'lib/rubygoo/widget.rb', line 274

def added()
end

#contains?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rubygoo/widget.rb', line 49

def contains?(x, y)
  @rect.collide_point? x, y
end

#disableObject

called when the widget is disabled



173
174
175
176
177
178
# File 'lib/rubygoo/widget.rb', line 173

def disable()
  if enabled?
    fire :disable
    @enabled = false
  end
end

#draw(screen) ⇒ Object

draw ourself on the screen



323
324
# File 'lib/rubygoo/widget.rb', line 323

def draw(screen)
end

#enableObject

called when the widget is enabled



181
182
183
184
185
186
# File 'lib/rubygoo/widget.rb', line 181

def enable()
  unless enabled?
    fire :enable
    @enabled = true
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/rubygoo/widget.rb', line 168

def enabled?()
  @enabled
end

#focusObject

called when the widget receives focus



327
328
# File 'lib/rubygoo/widget.rb', line 327

def focus()
end

#focussed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rubygoo/widget.rb', line 75

def focussed?()
  @focussed
end

#get_color(color) ⇒ Object

converts theme property to a GooColor



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubygoo/widget.rb', line 104

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

#hideObject

called when the widget is hidden



193
194
195
196
197
198
199
# File 'lib/rubygoo/widget.rb', line 193

def hide()
  if visible?
    fire :hide
    @visible = false
    disable
  end
end

#key_pressed(event) ⇒ Object

called when a key press is sent to us



315
316
# File 'lib/rubygoo/widget.rb', line 315

def key_pressed(event)
end

#key_released(event) ⇒ Object

called when a key release is sent to us



319
320
# File 'lib/rubygoo/widget.rb', line 319

def key_released(event)
end

#modal?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/rubygoo/widget.rb', line 269

def modal?()
  false
end

#mouse_down(event) ⇒ Object

called when there is a mouse click



295
296
# File 'lib/rubygoo/widget.rb', line 295

def mouse_down(event)
end

#mouse_drag(event) ⇒ Object

called when there is a mouse release at the end of a drag



299
300
# File 'lib/rubygoo/widget.rb', line 299

def mouse_drag(event)
end

#mouse_dragging(event) ⇒ Object

called when there is a mouse motion and a button pressed



287
288
# File 'lib/rubygoo/widget.rb', line 287

def mouse_dragging(event)
end

#mouse_enter(event) ⇒ Object

called when the mouse first enters a widget



303
304
# File 'lib/rubygoo/widget.rb', line 303

def mouse_enter(event)
end

#mouse_exit(event) ⇒ Object

called when the mouse exits a widget



307
308
# File 'lib/rubygoo/widget.rb', line 307

def mouse_exit(event)
end

#mouse_motion(event) ⇒ Object

called when there is a mouse motion and no button pressed



311
312
# File 'lib/rubygoo/widget.rb', line 311

def mouse_motion(event)
end

#mouse_over?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rubygoo/widget.rb', line 53

def mouse_over?()
  @mouse_over
end

#mouse_up(event) ⇒ Object

called when there is a mouse release w/o drag



291
292
# File 'lib/rubygoo/widget.rb', line 291

def mouse_up(event)
end

#removedObject

called when the widget is removed from a container



278
279
# File 'lib/rubygoo/widget.rb', line 278

def removed()
end

#showObject

called when the widget is shown



202
203
204
205
206
207
208
# File 'lib/rubygoo/widget.rb', line 202

def show()
  unless visible?
    fire :show
    @visible = true
    enable
  end
end

#tab_to?Boolean

does this widget want tabbed focus? Widget do usually

Returns:

  • (Boolean)


265
266
267
# File 'lib/rubygoo/widget.rb', line 265

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubygoo/widget.rb', line 81

def theme_property(prop_key)
  prop = nil
  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
  if prop_key.to_s.match(/color/i) and prop
    get_color prop
  else
    prop
  end
end

#unfocusObject

called when the widget loses focus



331
332
# File 'lib/rubygoo/widget.rb', line 331

def unfocus()
end

#update(time) ⇒ Object

called each update cycle with the amount of time that has passed. useful for animations, etc



283
284
# File 'lib/rubygoo/widget.rb', line 283

def update(time)
end

#update_rectObject



45
46
47
# File 'lib/rubygoo/widget.rb', line 45

def update_rect()
  @rect = Rect.new [@x,@y,@w,@h]
end

#visible?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/rubygoo/widget.rb', line 188

def visible?()
  @visible
end