module GGLib #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. class Widget < Tile attr_reader :window, :name, :sleeping, :defimage, :buttonId, :id, :zfocus attr_accessor :theme 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 def hasFocus? #do not modify if $window.hasFocus == self return true else return false end end def hasStickyFocus? #do not modify if $window.hasStickyFocus == self return true else return false end end 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 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 def over?(x,y) return iTile(x,y) end def clicked?(x,y) if $window.button_down?(Gosu::Button::MsLeft) return xTile(x,y) else return false end end 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 def feedText(char) end def onClick end def onRightClick end def onDrag(x1, y1, x2, y2) end def onRightDrag(x1, y1, x2, y2) end def onMouseOver end def onStickyFocus end def onMouseOut end def onStickyBlur end def onInitialize end def onDelete end def onKeyPress(key) end def stickFocus $window.setStickyFocus(self) end def unstickFocus $window.setStickyFocus(nil) end def intDraw if not @sleeping @theme.draw draw end end def draw end def sleep if not @sleeping if hasStickyFocus? $window.setStickyFocus(nil) end @theme.setSleepState @sleeping=true end end def sleeping? return @sleeping end def wakeUp if @sleeping @theme.setWakeState @sleeping=false end end def button return Button.getButton(buttonId) end 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 end class WidgetEvent MsLeft=0 MsRight=1 KeyUp=2 KeyDown=3 KeyLeft=4 KeyRight=5 KeyEscape=6 KeyEnter=7 KeyAlt=8 KeyControl=9 KeyShift=10 KeySpace=11 GpEnter=12 GpCancel=13 KeyText=14 KeyDelete=15 MsLeftDrag=16 MsRightDrag=17 end WidgetTextCaps = { ","=>"<", "."=>">", "/"=>"?", "'"=>"\"", ";"=>":", "["=>"{", "]"=>"}", "\\"=>"|", "="=>"+", "-"=>"_", "0"=>")", "9"=>"(", "8"=>"*", "7"=>"&", "6"=>"^", "5"=>"%", "4"=>"$", "3"=>"#", "2"=>"@", "1"=>"!", "`"=>"~" } end #module GGLib