lib/ext/widgets.rb in gglib-1.2.1 vs lib/ext/widgets.rb in gglib-1.3.0
- old
+ new
@@ -1,30 +1,37 @@
module GGLib
class DebugConsole < Widget
- @@allow=false
- def initialize(name="unnamed", theme=Themes::blank)
+ def initialize(name=:unnamed, theme=Themes::blank)
super(name, 0, 0, 640, 480, theme)
@vline=0
@oldtext=[]
@textm=false
clear
stickFocus
+ begin
+ @theme.font.default = Gosu::Font.new($window, "Courier New", @theme.font.default.height)
+ rescue
+ end
end
- def onKeyPress(key)
- if key==WidgetEvent::KeyDelete
+ def acceptText?
+ return true
+ end
+ def acceptStickyFocus?
+ return true
+ end
+ def onKeyUp(key)
+ if key==Gosu::Button::KbDelete or key==Gosu::Button::KbBackspace
thisline=@text[@line]
if @textm and thisline.length>0
@text[@line]=thisline.slice(0,thisline.length-1)
elsif thisline.length>3
@text[@line]=thisline.slice(0,thisline.length-1)
end
- elsif key==WidgetEvent::KeySpace
- @text[@line]+=" "
- elsif key==WidgetEvent::KeyEscape
+ elsif key==Gosu::Button::KbEscape or $window.button_id_to_char(key) == '`'
sleep
- elsif key==WidgetEvent::KeyEnter
+ elsif key==Gosu::Button::KbReturn or key==Gosu::Button::KbEnter
if @textm
if @text[@line]=="/endtext"
@textm=false
@line+=1
@text[@line]=">> "
@@ -42,24 +49,24 @@
@line+=1
else
runcmd(cmd)
end
@vline=@oldtext.size
- elsif key==WidgetEvent::KeyUp
+ elsif key==Gosu::Button::KbUp
if @vline-1 >= 0 and
@vline-=1
@text[@line]=@oldtext[@vline]
end
- elsif key==WidgetEvent::KeyDown
+ elsif key==Gosu::Button::KbDown
if @vline+1 < @oldtext.size and
@vline+=1
@text[@line]=@oldtext[@vline]
end
end
end
def feedText(char)
- @text[@line]+=char.to_s unless char=="`"
+ @text[@line]+=char.to_s unless (char=="`" || char=="\n" || char=="\r")
end
def justify(ln)
fin=""
size=ln.size
while ln.size>70
@@ -88,15 +95,14 @@
@text[@line]=">> "
}
end
def clear
@text=[]
- @text[0]="CONSOLE"
- @text[1]="#########"
- @text[2]="<Press ` (backquote) or enter '/exit' to exit>"
- @text[3]=">> "
- @line=3
+ @text[0]="-~*CONSOLE*~-"
+ @text[1]="<Press ` (backquote) or enter '/exit' to exit>"
+ @text[2]=">> "
+ @line=2
@vline=@oldtext.size-1
end
private
def runcmd(cmd)
if cmd.slice(0,1)=="/"
@@ -154,38 +160,43 @@
$window.setTextInput(@textinput)
end
def onStickyBlur
$window.setTextInput(nil)
end
- def DebugConsole.enabled=(bool)
- @@allow=bool
- end
- def DebugConsole.enabled
- return @@allow
- end
def draw
i=0
@text.each {|line|
@theme.font.default.draw(line, 10, 10+i*@theme.font.default.height, ZOrder::Text, 1.0, 1.0, 0xffffffff)
i+=1
}
end
end
class TracePanel < Widget
- @@allow=false
- def initialize(name)
+ def initialize(name=:unnamed)
super(name, 0,0,640,60, Themes::tracePanel)
clear
+ begin
+ @theme.font.default = Gosu::Font.new($window, "Courier New", @theme.font.default.height)
+ rescue
+ end
end
- def onKeyPress(key)
- if key==WidgetEvent::KeyDelete
+ def acceptStickyFocus?
+ return true
+ end
+ def onKeyUp(key)
+ if key==Gosu::Button::KbDelete
+ theme = Themes::blank
sleep
- elsif key==WidgetEvent::KeyEnter
+ elsif key==Gosu::Button::KbReturn or key==Gosu::Button::KbEnter
clear
end
end
+ def wakeUp
+ super
+ theme = Themes::tracePanel
+ end
def sput(text)
@line+=1
@text[@line]=text.to_s
end
def put(text)
@@ -209,24 +220,14 @@
@text=[]
@text[0]="TRACE OUTPUT"
@text[1]="Press DELETE to close, ENTER to clear. (Click to activate.)"
@line=2
end
- def TracePanel.enabled=(bool)
- if bool==true
- @@allow=true
- else
- @@allow=false
- end
- end
- def TracePanel.enabled
- return @@allow
- end
def draw
i=0
@text.each {|line|
- @them.font.default.draw(line, 10, 10+i*@font.height, ZOrder::Text, 1.0, 1.0, 0xffffffff)
+ @theme.font.default.draw(line, 10, 10+i*@theme.font.default.height, ZOrder::Text, 1.0, 1.0, 0xffffffff)
i+=1
}
end
end
@@ -250,10 +251,13 @@
@x=x
@y=y
@drawcursor=0
@realwidth=@x + TextPadding
end
+ def acceptStickyFocus?
+ return true
+ end
def onMouseOver
@theme.setOverState unless self.hasStickyFocus?
end
def onMouseOut
@theme.setDefaultState unless self.hasStickyFocus?
@@ -342,17 +346,27 @@
@theme.font.default.draw(@text, @drawx, @drawy, ZOrder::Text, 1, 1, @theme.font.color)
end
end
class Label < Widget
- attr_accessor :text
- def initialize(name, x, y, text="", theme = Themes::blank)
+ attr_reader :text
+ def initialize(name, text, x, y, theme = Themes::blank)
@text = text
+ @initialized = false
super(name, x, y, x+100,y+30,theme)
end
+ def text=(val)
+ @initialized = false
+ @text=val
+ end
def draw
- @theme.font.default.draw(@text, x, y, ZOrder::Text, 1, 1, @theme.font.color)
+ if not @initialized
+ @x2 = @x1 + @theme.font.default.text_width(@text)
+ @y2 = @y1 + @theme.font.default.height
+ @initialized = true
+ end
+ @theme.font.default.draw(@text, @x1, @y1, ZOrder::Text, 1, 1, @theme.font.color)
end
end
class CheckBox < Widget
@@ -504,25 +518,8 @@
return @selected
end
def selected=(val)
@selected = val
end
-=begin
- def selected=(val)
- if val.kind_of?(Widget)
- raise RuntimeException.new("No option #{val} in #{self.inspect} (#{self.name})") unless val.parent == self
- val.checked = true
- return true
- else
- @buttons.each { |button|
- if button.name == val
- val.checked = true
- return
- end
- }
- raise RuntimeException.new("No option #{val} in #{self.inspect} (#{self.name})")
- end
- end
-=end
end
end #module GGLib
\ No newline at end of file