func (t *TextField) Draw(drawArea *Rect) { realRect := NewRect(t.Rect().X+drawArea.X, t.Rect().Y+drawArea.Y, t.Rect().Width, t.Rect().Height) inRect := drawArea.Intersection(realRect) if t.backgroundColor != nil { DrawFillRect(inRect, t.backgroundColor.R, t.backgroundColor.G, t.backgroundColor.B, 255) } if t.borderColor != nil { DrawRect(inRect, t.borderColor.R, t.borderColor.G, t.borderColor.B, 255) } if t.image != nil { t.image.DrawRectInRect(t.Rect(), drawArea) } caretX := 0 textX := t.Rect().X + (t.font.GetStringWidth(" ")) textY := t.Rect().Y + ((t.Rect().Height / 2) - (t.font.GetStringHeight() / 2)) if t.text != "" && t.font != nil { var drawText string if t.password { for i := 0; i < len(t.text); i++ { drawText += "*" } } else { drawText = t.text } t.font.SetStyle(t.bold, t.italic, t.underlined) t.font.SetColor(t.fontColor.R, t.fontColor.G, t.fontColor.B) t.font.DrawTextInRect(drawText, drawArea.X+textX, drawArea.Y+textY, inRect) caretX = t.font.GetStringWidth(drawText) } //draw caret if t.caret && !t.readonly && t.focus { caretX += textX if inRect.Contains(drawArea.X+caretX, drawArea.Y+textY) { DrawLine(t.fontColor.R, t.fontColor.G, t.fontColor.B, 255, drawArea.X+caretX, drawArea.Y+textY, drawArea.X+caretX, drawArea.Y+textY+t.font.GetStringHeight()) } } if sdl.GetTicks()-t.caretLast >= CARET_TIME { t.caret = !t.caret t.caretLast = sdl.GetTicks() } }
func NewTextField(x, y, width, height int) *TextField { textfield := &TextField{} textfield.Init(x, y, width, height) textfield.focusable = true textfield.SetFont(g_game.guiManager.defaultFont) textfield.fontColor = &sdl.Color{uint8(255), uint8(255), uint8(255), uint8(255)} textfield.caretLast = sdl.GetTicks() return textfield }
//Game loop func (game *Game) Run() { defer game.Exit() if game.initFun == nil { fmt.Println("Go2D Warning: No init function set!") } if game.updateFun == nil { fmt.Println("Go2D Warning: No update function set!") } if game.drawFun == nil { fmt.Println("Go2D Warning: No draw function set!") } //Initialize the game game.initialize() var dt, old_t, now_t uint32 = 0, 0, 0 for g_running { //Check for events and handle them for { event, present := sdl.PollEvent() if present { EventHandler(event) } else { break } } //Calculate time delta now_t = sdl.GetTicks() dt = now_t - old_t old_t = now_t //Update game.update(dt) //Draw game.draw() //Give the CPU some time to do other stuff sdl.Delay(1) } }