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 = Theme::BlankTheme, 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=Theme::BlankTheme,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



146
147
148
# File 'lib/widget.rb', line 146

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



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

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

#drawObject



124
125
# File 'lib/widget.rb', line 124

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



85
86
# File 'lib/widget.rb', line 85

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



117
118
119
120
121
122
# File 'lib/widget.rb', line 117

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

#onClickObject



87
88
# File 'lib/widget.rb', line 87

def onClick
end

#onDeleteObject



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

def onDelete
end

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



91
92
# File 'lib/widget.rb', line 91

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

#onInitializeObject



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

def onInitialize
end

#onKeyPress(key) ⇒ Object



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

def onKeyPress(key)
end

#onMouseOutObject



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

def onMouseOut 
end

#onMouseOverObject



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

def onMouseOver
end

#onRightClickObject



89
90
# File 'lib/widget.rb', line 89

def onRightClick
end

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



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

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

#onStickyBlurObject



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

def onStickyBlur
end

#onStickyFocusObject



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

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



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

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

#sleeping?Boolean

Returns:

  • (Boolean)


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

def sleeping?
  return @sleeping
end

#stickFocusObject



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

def stickFocus
  $window.setStickyFocus(self)
end

#unstickFocusObject



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

def unstickFocus
  $window.setStickyFocus(nil)
end

#wakeUpObject



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

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