Class: GGLib::Widget

Inherits:
Tile
  • Object
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.

Instance Attribute Summary collapse

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, setAllInstances, #setCoordinates, #setTile, #width, #xTile

Constructor Details

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

do not modify


28
29
30
31
32
33
34
35
36
37
# File 'lib/widget.rb', line 28

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

Instance Attribute Details

#buttonIdObject (readonly)

Returns the value of attribute buttonId.


26
27
28
# File 'lib/widget.rb', line 26

def buttonId
  @buttonId
end

#defimageObject (readonly)

Returns the value of attribute defimage.


26
27
28
# File 'lib/widget.rb', line 26

def defimage
  @defimage
end

#idObject (readonly)

Returns the value of attribute id.


26
27
28
# File 'lib/widget.rb', line 26

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.


26
27
28
# File 'lib/widget.rb', line 26

def name
  @name
end

#sleepingObject (readonly)

Returns the value of attribute sleeping.


26
27
28
# File 'lib/widget.rb', line 26

def sleeping
  @sleeping
end

#themeObject

Returns the value of attribute theme.


27
28
29
# File 'lib/widget.rb', line 27

def theme
  @theme
end

#windowObject (readonly)

Returns the value of attribute window.


26
27
28
# File 'lib/widget.rb', line 26

def window
  @window
end

#zfocusObject (readonly)

Returns the value of attribute zfocus.


26
27
28
# File 'lib/widget.rb', line 26

def zfocus
  @zfocus
end

Instance Method Details

#blurObject

do not modify


57
58
59
# File 'lib/widget.rb', line 57

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


158
159
160
# File 'lib/widget.rb', line 158

def button
  return Button.getButton(buttonId)
end

#clicked?(x, y) ⇒ Boolean

Returns:

  • (Boolean)

64
65
66
67
68
69
70
# File 'lib/widget.rb', line 64

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

#delObject


162
163
164
165
166
167
168
169
170
# File 'lib/widget.rb', line 162

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


85
86
87
88
89
90
91
# File 'lib/widget.rb', line 85

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

#drawObject


136
137
# File 'lib/widget.rb', line 136

def draw
end

#event(type) ⇒ Object

do not modify


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/widget.rb', line 72

def event(type) #do not modify
  if type==WidgetEvent::MsLeft
    if @acceptstickyfocus
      stickFocus
    end
    onClick
  elsif type==WidgetEvent::MsRight
    onRightClick
  else
    onKeyPress(type)
  end
end

#feedText(char) ⇒ Object


93
94
# File 'lib/widget.rb', line 93

def feedText(char)
end

#focusObject

do not modify


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

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)

38
39
40
41
42
43
44
# File 'lib/widget.rb', line 38

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

#hasStickyFocus?Boolean

do not modify

Returns:

  • (Boolean)

45
46
47
48
49
50
51
# File 'lib/widget.rb', line 45

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

#intDrawObject


129
130
131
132
133
134
# File 'lib/widget.rb', line 129

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

#onClickObject


99
100
# File 'lib/widget.rb', line 99

def onClick
end

#onDeleteObject


117
118
# File 'lib/widget.rb', line 117

def onDelete
end

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


103
104
# File 'lib/widget.rb', line 103

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

#onInitializeObject


115
116
# File 'lib/widget.rb', line 115

def onInitialize
end

#onKeyPress(key) ⇒ Object


119
120
# File 'lib/widget.rb', line 119

def onKeyPress(key)
end

#onMouseDownObject


95
96
# File 'lib/widget.rb', line 95

def onMouseDown
end

#onMouseOutObject


111
112
# File 'lib/widget.rb', line 111

def onMouseOut 
end

#onMouseOverObject


107
108
# File 'lib/widget.rb', line 107

def onMouseOver
end

#onRightClickObject


101
102
# File 'lib/widget.rb', line 101

def onRightClick
end

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


105
106
# File 'lib/widget.rb', line 105

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

#onRightMouseDownObject


97
98
# File 'lib/widget.rb', line 97

def onRightMouseDown
end

#onStickyBlurObject


113
114
# File 'lib/widget.rb', line 113

def onStickyBlur
end

#onStickyFocusObject


109
110
# File 'lib/widget.rb', line 109

def onStickyFocus
end

#over?(x, y) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/widget.rb', line 61

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

#sleepObject


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

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

#sleeping?Boolean

Returns:

  • (Boolean)

148
149
150
# File 'lib/widget.rb', line 148

def sleeping?
  return @sleeping
end

#stickFocusObject


122
123
124
# File 'lib/widget.rb', line 122

def stickFocus
  $window.setStickyFocus(self)
end

#unstickFocusObject


125
126
127
# File 'lib/widget.rb', line 125

def unstickFocus
  $window.setStickyFocus(nil)
end

#wakeUpObject


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

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