Class: GGLib::Widget

Inherits:
Containable show all
Defined in:
lib/widget.rb

Overview

The Widget class is an interface between the button class and the window class which allows for user interaction with an area of the screen. This area is referred to as the widget. To make a Widget, simply create a class derived from Widget and override methods as needed:

class MyWidget < Widget #all widgets are subclasses of Widget

attr_reader :text,:font    #most widgets will have text and font attributes
def initialize(window,text)
  super(window,Button::Image,0,400,640,480,"img/gui/mywidget image.png",name="My Widget")       #use super to initialize the button
  @text=text; @font=Gosu::Font.new(window, "arial", 50)          #initialize widget specific variables
end
def draw                        #the window will call draw every time it executes GameWindow::draw
  font.draw(text,0,0)           #you do not have to draw the image here, Button class does that for you
  if clicked?                   #here you do widget specific things like drawing text,
    setImage(Gosu::Image.new)   #changing the picture on mouse over or click, etc
  end                                     
end
def onDelete        #onDelete is called when the window wants to delete the widget
  puts "deleted"    #execute widget specific code here
end                 #or leave the function alone if there is no widget specific code

end

See the ‘Widget Programmer’s Handbook` for more info.

Defined Under Namespace

Modules: Event

Instance Attribute Summary collapse

Attributes inherited from Containable

#align, #container, #maxSize, #minSize, #offset, #padding, #valign

Attributes inherited from Tile

#inclusive, #x1, #x2, #y1, #y2

Instance Method Summary collapse

Methods inherited from Tile

#centerOn, deleteAllInstances, deleteById, #each, #eachBorder, getAllInstances, getById, #height, #iTile, intersect?, #intersect?, #isInTile?, #move, #resize, setAllInstances, #setCoordinates, #setTile, #width, #xTile

Constructor Details

#initialize(name = "unnamed", x1 = 0, y1 = 0, x2 = 1, y2 = 1, theme = Themes::blank, z = 0) ⇒ Widget

do not modify



141
142
143
144
145
146
147
148
149
# File 'lib/widget.rb', line 141

def initialize(name="unnamed",x1=0,y1=0,x2=1,y2=1,theme=Themes::blank,z=0) #do not modify
  super(x1,y1,x2,y2)
  @id=$window.newWidget(self)
  @theme=theme.newInstance(self)
  @name=name
  @sleeping=false
  @zfocus=z
  onInitialize
end

Instance Attribute Details

#buttonIdObject (readonly)

Returns the value of attribute buttonId.



139
140
141
# File 'lib/widget.rb', line 139

def buttonId
  @buttonId
end

#defimageObject (readonly)

Returns the value of attribute defimage.



139
140
141
# File 'lib/widget.rb', line 139

def defimage
  @defimage
end

#idObject (readonly)

Returns the value of attribute id.



139
140
141
# File 'lib/widget.rb', line 139

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



139
140
141
# File 'lib/widget.rb', line 139

def name
  @name
end

#sleepingObject (readonly)

Returns the value of attribute sleeping.



139
140
141
# File 'lib/widget.rb', line 139

def sleeping
  @sleeping
end

#themeObject

Returns the value of attribute theme.



140
141
142
# File 'lib/widget.rb', line 140

def theme
  @theme
end

#windowObject (readonly)

Returns the value of attribute window.



139
140
141
# File 'lib/widget.rb', line 139

def window
  @window
end

#zfocusObject (readonly)

Returns the value of attribute zfocus.



139
140
141
# File 'lib/widget.rb', line 139

def zfocus
  @zfocus
end

Instance Method Details

#acceptStickyFocus?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/widget.rb', line 213

def acceptStickyFocus?
  return false
end

#acceptText?Boolean

Returns:

  • (Boolean)


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

def acceptText?
  return false
end

#blurObject

do not modify



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

def blur #do not modify
  onMouseOut #blur is triggered by the onMouseOut event, so when the window blurs a widget, it must preform the mouseout event
end

#buttonObject



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

def button
  return Button.getButton(buttonId)
end

#clicked?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
182
# File 'lib/widget.rb', line 176

def clicked?(x,y)
  if $window.button_down?(Gosu::Button::MsLeft)
    return xTile(x,y)
  else
    return false
  end
end

#delObject



290
291
292
293
294
295
296
297
298
# File 'lib/widget.rb', line 290

def del
  onDelete
  if $window.nil?
    raise "Window assigned to nil" #Very strange bug. Should be dead by now. (This is just in case.)
    return
  end
  $window.deleteWidget(self)
  super
end

#downevent(type) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/widget.rb', line 199

def downevent(type)
  if type==Widget::Event::MsLeft
    onMouseDown
  elsif type==Widget::Event::MsRight
    onRightMouseDown
  end
end

#drawObject



264
265
# File 'lib/widget.rb', line 264

def draw
end

#event(type, dat = nil) ⇒ Object

do not modify



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/widget.rb', line 184

def event(type, dat=nil) #do not modify
  if type==Event::MsLeft
    if acceptStickyFocus?
      stickFocus
    end
    onClick
  elsif type==Event::MsRight
    onRightClick
  elsif type==Event::KeyUp
    onKeyUp(dat)
  elsif type==Event::KeyDown
    onKeyDown(dat)
  end
end

#feedText(char) ⇒ Object



207
208
# File 'lib/widget.rb', line 207

def feedText(char)
end

#focusObject

do not modify



164
165
166
167
168
# File 'lib/widget.rb', line 164

def focus #do not modify
  #debugger
  $window.setFocus(@id) 
  onMouseOver #focus is triggered by the onMouseOver event, so when the window gives a widget focus, it must preform the mouseover event 
end

#hasFocus?Boolean

do not modify

Returns:

  • (Boolean)


150
151
152
153
154
155
156
# File 'lib/widget.rb', line 150

def hasFocus? #do not modify
  if $window.hasFocus == self
    return true 
  else
    return false
  end
end

#hasStickyFocus?Boolean

do not modify

Returns:

  • (Boolean)


157
158
159
160
161
162
163
# File 'lib/widget.rb', line 157

def hasStickyFocus? #do not modify
  if $window.hasStickyFocus == self
    return true 
  else
    return false
  end
end

#intDrawObject



257
258
259
260
261
262
# File 'lib/widget.rb', line 257

def intDraw
  if not @sleeping
    @theme.draw
    draw
  end
end

#onClickObject



221
222
# File 'lib/widget.rb', line 221

def onClick
end

#onDeleteObject



239
240
# File 'lib/widget.rb', line 239

def onDelete
end

#onDrag(x1, y1, x2, y2) ⇒ Object



225
226
# File 'lib/widget.rb', line 225

def onDrag(x1, y1, x2, y2)
end

#onInitializeObject



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

def onInitialize
end

#onKeyDown(key) ⇒ Object



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

def onKeyDown(key)
end

#onKeyUp(key) ⇒ Object



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

def onKeyUp(key)
end

#onMouseDownObject



217
218
# File 'lib/widget.rb', line 217

def onMouseDown
end

#onMouseOutObject



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

def onMouseOut 
end

#onMouseOverObject



229
230
# File 'lib/widget.rb', line 229

def onMouseOver
end

#onRightClickObject



223
224
# File 'lib/widget.rb', line 223

def onRightClick
end

#onRightDrag(x1, y1, x2, y2) ⇒ Object



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

def onRightDrag(x1, y1, x2, y2)
end

#onRightMouseDownObject



219
220
# File 'lib/widget.rb', line 219

def onRightMouseDown
end

#onStickyBlurObject



235
236
# File 'lib/widget.rb', line 235

def onStickyBlur
end

#onStickyFocusObject



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

def onStickyFocus
end

#over?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/widget.rb', line 173

def over?(x,y)
  return iTile(x,y)      
end

#sleepObject



267
268
269
270
271
272
273
274
275
# File 'lib/widget.rb', line 267

def sleep
  if not @sleeping
    if hasStickyFocus?
      $window.setStickyFocus(nil)
    end
    @theme.setSleepState
    @sleeping=true
  end
end

#sleeping?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/widget.rb', line 276

def sleeping?
  return @sleeping
end

#stickFocusObject



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

def stickFocus
  if acceptStickyFocus?
    $window.setStickyFocus(self)
  end
end

#unstickFocusObject



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

def unstickFocus
  if hasStickyFocus?
    $window.setStickyFocus(nil)
  end
end

#wakeUpObject



279
280
281
282
283
284
# File 'lib/widget.rb', line 279

def wakeUp
  if @sleeping
    @theme.setWakeState
    @sleeping=false
  end
end