Skip to content

Commit

Permalink
Add cursor to nickname scene
Browse files Browse the repository at this point in the history
  • Loading branch information
armsnyder committed Oct 20, 2020
1 parent cf57543 commit b3653b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func setupChangeSceneHandler(currentScene *scenes.Scene, c *websocket.Conn) erro

*currentScene = scene

termbox.HideCursor()

if err := scene.Setup(changeScene, sendMessage); err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/scenes/draw_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func draw(anchor anchor, color color, text interface{}) {
}
}

// setCursor uses an anchor to determine the position of the cursor, so it can be used in
// conjunction with draw to place the cursor.
func setCursor(anchor anchor) {
x, y, _, _ := anchor()
termbox.SetCursor(x, y-1)
}

// anchor defines a position offset and direction that can be used for drawing.
type anchor func() (positionX, positionY int, drawDirectionX, drawDirectionY float64)

Expand Down
19 changes: 15 additions & 4 deletions pkg/client/scenes/nickname.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ func (n *Nickname) OnTerminalEvent(event termbox.Event) error {
n.nickname = n.nickname[:len(n.nickname)-1]
}

if len(n.nickname) >= maxNicknameLen {
return nil
var setLastChar func(rune)

if len(n.nickname) < maxNicknameLen {
setLastChar = func(r rune) {
n.nickname += string(r)
}
} else {
setLastChar = func(r rune) {
n.nickname = n.nickname[:maxNicknameLen-1] + string(r)
}
}

if event.Key == termbox.KeySpace {
n.nickname += " "
setLastChar(' ')
return nil
}

Expand All @@ -75,7 +83,7 @@ func (n *Nickname) OnTerminalEvent(event termbox.Event) error {
return nil
}

n.nickname += string(letter)
setLastChar(letter)

return nil
}
Expand All @@ -101,6 +109,9 @@ func (n *Nickname) Draw() {
}

draw(offset(center, 0, 4), normal, sb.String())

cursorX := min(len(n.nickname), maxNicknameLen-1) - maxNicknameLen/2
setCursor(offset(center, cursorX, 4))
}

func (n *Nickname) load() error {
Expand Down

0 comments on commit b3653b4

Please sign in to comment.