module GGLib class DebugConsole < Widget @@allow=false def initialize(name="unnamed", theme=Themes::blank) super(name, 0, 0, 640, 480, theme) @vline=0 @oldtext=[] @textm=false clear stickFocus end def onKeyPress(key) if key==WidgetEvent::KeyDelete 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 sleep elsif key==WidgetEvent::KeyEnter if @textm if @text[@line]=="/endtext" @textm=false @line+=1 @text[@line]=">> " else @line+=1 @text[@line]="" end return end @oldtext.push @text[@line] cmd=@text[@line].slice(3,@text[@line].length-3) if cmd.slice(cmd.size-3,3)=="/nl" @text[@line]=@text[@line].slice(0,@text[@line].length-3) @text[@line+1]="|> " @line+=1 else runcmd(cmd) end @vline=@oldtext.size elsif key==WidgetEvent::KeyUp if @vline-1 >= 0 and @vline-=1 @text[@line]=@oldtext[@vline] end elsif key==WidgetEvent::KeyDown 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=="`" end def justify(ln) fin="" size=ln.size while ln.size>70 fin+=ln.slice(0,70)+"\fNL" ln=ln.slice(70,ln.size) end fin+=ln.to_s return fin end def respond(text) text=text.to_s @line+=1 if text=="" text="" end text=justify(text) if text.index("\fNL")==nil text=text.split("\fNL") mult=false text.each { |ln| if ln.slice(0,4)=="\fERR" @text[@line]="!> "+ln.strip else @text[@line]="^> "+ln end @line+=1 @text[@line]=">> " } end def clear @text=[] @text[0]="CONSOLE" @text[1]="#########" @text[2]="" @text[3]=">> " @line=3 @vline=@oldtext.size-1 end private def runcmd(cmd) if cmd.slice(0,1)=="/" if cmd=="/exit" sleep return elsif cmd=="/clear" clear return elsif cmd=="/help" respond("Enter a Ruby command to execute it\fNLEnter '/' followed by a word to execute a console command\fNLFor a list of console commands, enter '/list'") return elsif cmd=="/list" respond("/clear\fNL/clearhist\fNL/endtext\fNL/exit\fNL/help\fNL/hist\fNL/list\fNL/text") return elsif cmd=="/hist" resp="" @oldtext.each { |item| resp+="\fNL" resp+=item } respond(resp) return elsif cmd=="/text" @textm=true @line+=1 @text[@line]="" return elsif cmd=="/clearhist" @oldtext=[] @vline=0 return end return end output=eval("begin\n"+cmd+"\nrescue\n'\fERR: '+$@[0]+': '+$!\nend",TOPLEVEL_BINDING) respond(output) end public def sleep @text=[] @line=0 @vline=0 super end def wakeUp @oldtext=[] @vline=0 @text=false clear stickFocus super end def onStickyFocus $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) super(name, 0,0,640,60, Themes::tracePanel) clear end def onKeyPress(key) if key==WidgetEvent::KeyDelete sleep elsif key==WidgetEvent::KeyEnter clear end end def sput(text) @line+=1 @text[@line]=text.to_s end def put(text) @line+=1 @text[@line]=text.to_s wakeUp end def <<(text) sput(text) end def *(nullarg=nil) wakeUp end def ~() sleep end def pause gets end def clear @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) i+=1 } end end class TextBox < Widget CaretColor=0xff646464 TextColor=0xff646464 SelColor=0xff33FFFF TextPadding=5 def initialize(name, x, y, len=12, theme=Themes::blank, w=nil,height=nil) if width!=nil and height!=nil super(name, x, y, x+w, y+h, theme) else @theme = theme.newInstance(self) #Get specialized instance early @theme.setCoords(1,1,1,1) super(name, x, y, x+@theme.width, y+@theme.height, theme) @theme.setCoords(@x1, @y1, @x2, @y2) end @textinput=Gosu::TextInput.new @offsety=((@theme.height-@theme.font.editable.height)/2).floor @maxlength=len @x=x @y=y @drawcursor=0 @realwidth=@x + TextPadding end def onMouseOver @theme.setOverState unless self.hasStickyFocus? end def onMouseOut @theme.setDefaultState unless self.hasStickyFocus? end def onMouseClick stickFocus end def onStickyFocus @theme.setOverState $window.setTextInput(@textinput) end def onStickyBlur @theme.setDefaultState $window.setTextInput(nil) end def width return @maxlength end def text return @textinput.text end def text=(val) val=val.slice(0,12) if val.size>12 @textinput.text = val end def draw text=@textinput.text.slice(0,12) if self.hasStickyFocus? pos_x = @x + @theme.font.editable.text_width(@textinput.text[0...@textinput.caret_pos]) + TextPadding sel_x = @x + @theme.font.editable.text_width(@textinput.text[0...@textinput.selection_start]) + TextPadding @realwidth+=@theme.font.editable.text_width(text) if pos_x > @realwidth pos_x=@realwidth+1 sel_x=@realwidth+1 @textinput.text=text end @realwidth-=@theme.font.editable.text_width(text) if @drawcursor < 18 $window.draw_line(pos_x, @y+@offsety, CaretColor, pos_x, @y+@theme.font.editable.height+@offsety, CaretColor, ZOrder::Text) elsif @drawcursor > 36 @drawcursor=0 end @drawcursor+=1 $window.draw_quad(sel_x, @y+@offsety, @theme.font.selcolor, pos_x, @y+@offsety, @theme.font.selcolor, sel_x, @y+@theme.font.editable.height+@offsety, @theme.font.selcolor, pos_x, @y+@theme.font.editable.height+@offsety, @theme.font.selcolor, ZOrder::Text-0.5) end @theme.font.editable.draw(text, @x+TextPadding, @y+@offsety, ZOrder::Text, 1, 1, @theme.font.color) end end class Button < Widget TextColor=0xff000000 attr_accessor :text def initialize(name, text, x, y, onClickPrc=Proc.new{}, theme = Themes::blank) super(name, x, y, x+100, y+30, theme) @text=text @onClickPrc=onClickPrc @drawx=x+((100-@theme.font.default.text_width(@text))/2).floor @drawy=y+((30-@theme.font.default.height)/2).floor end def x1= super @drawx=x+((100-@theme.font.default.text_width(@text))/2).floor end def y1= super @drawy=y+((30-@theme.font.default.height)/2).floor end def onClick @onClickPrc.call(self) end def onMouseOver @theme.setOverState end def onMouseOut @theme.setDefaultState end def draw @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, text, x, y, theme = Themes::blank) @text = text super(name,x,y,x+100,y+30,theme) end def draw @theme.font.default.draw(@text, x, y, ZOrder::Text, 1, 1, @theme.font.color) end end end #module GGLib