Skip to content

Commit

Permalink
add erase mode, fix blinking text
Browse files Browse the repository at this point in the history
  • Loading branch information
aca committed Apr 28, 2020
1 parent dac2df1 commit 77a8c39
Showing 1 changed file with 68 additions and 24 deletions.
92 changes: 68 additions & 24 deletions tdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

const (
MODE_BOX = "BOX"
MODE_LINE = "LINE"
MODE_TEXT = "TEXT"
MODE_ERASOR = "ERASE"
MODE_BOX = "BOX"
MODE_LINE = "LINE"
MODE_TEXT = "TEXT"
MODE_ERASE = "ERASE"
)

var defStyle tcell.Style
Expand Down Expand Up @@ -78,23 +78,21 @@ func drawLine(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
}

return
// for col := x1; col <= x2; col++ {
// // RuneHLine = '─'
// s.SetContent(col, y1, tcell.RuneHLine, nil, tcell.StyleDefault)
// s.SetContent(col, y2, tcell.RuneHLine, nil, tcell.StyleDefault)
// }
// for row := y1 + 1; row <= y2; row++ {
// // RuneVLine = '│'
// s.SetContent(x1, row, tcell.RuneVLine, nil, tcell.StyleDefault)
// s.SetContent(x2, row, tcell.RuneVLine, nil, tcell.StyleDefault)
// }
// if y1 != y2 && x1 != x2 {
// // Only add corners if we need to
// s.SetContent(x1, y1, tcell.RuneULCorner, nil, tcell.StyleDefault)
// s.SetContent(x2, y1, tcell.RuneURCorner, nil, tcell.StyleDefault)
// s.SetContent(x1, y2, tcell.RuneLLCorner, nil, tcell.StyleDefault)
// s.SetContent(x2, y2, tcell.RuneLRCorner, nil, tcell.StyleDefault)
// }
}

func drawErase(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}

for col := x1; col <= x2; col++ {
for row := y1; row <= y2; row++ {
s.SetContent(col, row, ' ', nil, tcell.StyleDefault)
}
}
}

func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
Expand Down Expand Up @@ -197,7 +195,7 @@ func main() {

for {
// r, _, _, _ := s.GetContent(mx, my)
emitStr(s, 1, 1, defStyle, fmt.Sprintf("[%3v,%3v] %-7s | t:text / l:line / esc:box / MouseR: erase", mx, my, mode))
emitStr(s, 1, 1, defStyle, fmt.Sprintf("[%3v,%3v] %-7s | esc:box / t:text / l:line / e:erase / MouseR: eraser", mx, my, mode))

s.Show()
ev := s.PollEvent()
Expand Down Expand Up @@ -268,16 +266,39 @@ func main() {
imodeCurX, imodeCurY = mx, my
imodeStartX, _ = mx, my
s.SetContent(imodeCurX, imodeCurY, '_', nil, st.Blink(true))
case 'e':
mode = MODE_ERASE
case 'l':
mode = MODE_LINE
}
}

case MODE_ERASE:
switch ev.Key() {
case tcell.KeyEscape:
mode = MODE_BOX
default:
switch ev.Rune() {
case 'e':
mode = MODE_ERASE
case 'l':
mode = MODE_LINE
case 't', 'i':
mode = MODE_TEXT
imodeCurX, imodeCurY = mx, my
imodeStartX, _ = mx, my
s.SetContent(imodeCurX, imodeCurY, '_', nil, st.Blink(true))
}
}

case MODE_LINE:
switch ev.Key() {
case tcell.KeyEscape:
mode = MODE_BOX
default:
switch ev.Rune() {
case 'e':
mode = MODE_ERASE
case 't', 'i':
mode = MODE_TEXT
imodeCurX, imodeCurY = mx, my
Expand All @@ -304,7 +325,7 @@ func main() {
s.SetContent(imodeCurX, imodeCurY, '_', nil, st)
}
default:
s.SetContent(imodeCurX, imodeCurY, ev.Rune(), nil, st.Blink(true))
s.SetContent(imodeCurX, imodeCurY, ev.Rune(), nil, st)
imodeCurX += 1
s.SetContent(imodeCurX, imodeCurY, '_', nil, st.Blink(true))
}
Expand All @@ -319,6 +340,29 @@ func main() {
}

switch mode {

case MODE_ERASE:
switch ev.Buttons() {
case tcell.ButtonNone:
if ox >= 0 {
bg := tcell.Color((lchar - '0') * 2)
drawErase(s, ox, oy, x, y, up.Background(bg), lchar)
ox, oy = -1, -1
bx, by = -1, -1
}
case tcell.Button1:
ch := ' '
bx, by = x, y
lchar = rune(ch)
if ox >= 0 && bx >= 0 {
drawSelect(s, ox, oy, bx, by, true)
}
case tcell.Button3:
ox, oy = -1, -1
bx, by = -1, -1
s.SetContent(x, y, ' ', nil, tcell.StyleDefault)
}

case MODE_BOX:
switch ev.Buttons() {
case tcell.ButtonNone:
Expand Down Expand Up @@ -361,8 +405,8 @@ func main() {
bx, by = -1, -1
s.SetContent(x, y, ' ', nil, tcell.StyleDefault)
}

}

}
}
}

0 comments on commit 77a8c39

Please sign in to comment.