From 77a8c39ff3759ec5403dc7ffd310b3e08be7e8b1 Mon Sep 17 00:00:00 2001 From: aca Date: Wed, 29 Apr 2020 08:23:13 +0900 Subject: [PATCH] add erase mode, fix blinking text --- tdraw.go | 92 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/tdraw.go b/tdraw.go index db90b34..e792e6a 100644 --- a/tdraw.go +++ b/tdraw.go @@ -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 @@ -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) { @@ -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() @@ -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 @@ -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)) } @@ -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: @@ -361,8 +405,8 @@ func main() { bx, by = -1, -1 s.SetContent(x, y, ' ', nil, tcell.StyleDefault) } - } + } } }