lib/widget.rb in gglib-1.2.1 vs lib/widget.rb in gglib-1.3.0

- old
+ new

@@ -1,7 +1,110 @@ module GGLib + +class Containable < Tile + class Padding + attr_reader :left, :right, :top, :bottom + attr_accessor :container + def initialize(t, r, b, l, contain=nil) + @top, @right, @bottom, @left = t, r, b, l + @container=contain + end + def left=(val) + @left=val + @container.align unless @container.nil? + end + def right=(val) + @right=val + @container.align unless @container.nil? + end + def top=(val) + @top=val + @container.align unless @container.nil? + end + def bottom=(val) + @bottom=val + @container.align unless @container.nil? + end + end + + class Dimension + Infinite = -1 + attr_reader :vertical, :horizontal + attr_accessor :container + def initialize(h, v) + @horizontal, @vertical = h, v + end + def vertical=(val) + @vertical=val + @container.align unless @container.nil? + end + def horizontal=(val) + @horizontal=val + @container.align unless @container.nil? + end + end + + module Align + Default = -1 + Left = 0 + Center = 1 + Right = 2 + end + + module VAlign + Default = -1 + Top = 0 + Center = 1 + Bottom = 2 + end + + attr_reader :padding, :offset, :minSize, :maxSize, :align, :valign + attr_reader :container + + def initialize(x1, y1, x2, y2, container=nil) + super(x1, y1, x2, y2) + @padding = Padding.new(0,0,0,0) + @offset = Dimension.new(0,0) + @maxSize = Dimension.new(Dimension::Infinite,Dimension::Infinite) + @minSize = Dimension.new(0,0) + @align = Align::Default + @valign = VAlign::Default + @container = container + end + def padding=(val) + @padding=val + @padding.container = @container + @container.align unless @container.nil? + end + def offset=(val) + @offset=val + @offset.container = @container + @container.align unless @container.nil? + end + def minSize=(val) + @minSize=val + @minSize.container = @container + @container.align unless @container.nil? + end + def maxSize=(val) + @maxSize=val + @maxSize.container = @container + @container.align unless @container.nil? + end + def align=(val) + @align=val + @align.container = @container + @container.align unless @container.nil? + end + def valign=(val) + @valign=val + @valign.container = @container + @container.align unless @container.nil? + end +end + #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 @@ -20,21 +123,30 @@ # 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 +class Widget < Containable + + module Event + MsLeft=0 + MsRight=1 + KeyUp=2 + KeyDown=3 + MsLeftDrag=16 + MsRightDrag=17 + end + 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=Themes::blank,acceptsticky=true,z=0) #do not modify + 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 - @acceptstickyfocus=acceptsticky onInitialize end def hasFocus? #do not modify if $window.hasFocus == self return true @@ -67,33 +179,43 @@ else return false end end - def event(type) #do not modify - if type==WidgetEvent::MsLeft - if @acceptstickyfocus + def event(type, dat=nil) #do not modify + if type==Event::MsLeft + if acceptStickyFocus? stickFocus end onClick - elsif type==WidgetEvent::MsRight + elsif type==Event::MsRight onRightClick - else - onKeyPress(type) + elsif type==Event::KeyUp + onKeyUp(dat) + elsif type==Event::KeyDown + onKeyDown(dat) end end def downevent(type) - if type==WidgetEvent::MsLeft + if type==Widget::Event::MsLeft onMouseDown - elsif type==WidgetEvent::MsRight + elsif type==Widget::Event::MsRight onRightMouseDown end end def feedText(char) end + def acceptText? + return false + end + + def acceptStickyFocus? + return false + end + def onMouseDown end def onRightMouseDown end def onClick @@ -114,18 +236,24 @@ end def onInitialize end def onDelete end - def onKeyPress(key) + def onKeyUp(key) end + def onKeyDown(key) + end def stickFocus - $window.setStickyFocus(self) + if acceptStickyFocus? + $window.setStickyFocus(self) + end end def unstickFocus - $window.setStickyFocus(nil) + if hasStickyFocus? + $window.setStickyFocus(nil) + end end def intDraw if not @sleeping @theme.draw @@ -166,30 +294,9 @@ 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 = { ","=>"<", "."=>">", \ No newline at end of file