Working on buffer stuff...
Trying to figure out how/if it fits...
This commit is contained in:
@@ -32,10 +32,12 @@ import (
|
||||
// ArtWidget displays it's text as ascii art.
|
||||
type ArtWidget struct {
|
||||
id string
|
||||
x, y int
|
||||
w, h int
|
||||
style tcell.Style
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
buffer Buffer
|
||||
|
||||
active bool
|
||||
visible bool
|
||||
focusable bool
|
||||
@@ -60,6 +62,7 @@ func NewArtWidget(id string, st tcell.Style) *ArtWidget {
|
||||
|
||||
func (w *ArtWidget) Init(id string, st tcell.Style) {
|
||||
w.id = id
|
||||
w.visible = true
|
||||
w.style = st
|
||||
w.keyMap = BlankKeyMap()
|
||||
w.initFonts()
|
||||
@@ -79,34 +82,10 @@ func (w *ArtWidget) RemoveFromKeyMap(km KeyMap) {
|
||||
func (w *ArtWidget) HandleKey(ev *tcell.EventKey) bool { return false }
|
||||
func (w *ArtWidget) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *ArtWidget) Draw(screen tcell.Screen) {
|
||||
x, y := w.x, w.y
|
||||
for _, ch := range w.text {
|
||||
w.drawRune(ch, x, y, screen)
|
||||
x += w.getRuneWidth(ch)
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (w *ArtWidget) drawRune(r rune, x, y int, screen tcell.Screen) {
|
||||
wx, wy := x, y
|
||||
rs := w.getRune(r)
|
||||
for _, ln := range rs {
|
||||
wx = x
|
||||
for _, d := range ln {
|
||||
screen.SetContent(wx, wy, d, nil, w.style)
|
||||
wx++
|
||||
}
|
||||
wy++
|
||||
}
|
||||
}
|
||||
|
||||
func (w *ArtWidget) getRune(r rune) [][]rune { return w.currFont.GetRune(r) }
|
||||
func (w *ArtWidget) getRuneHeight(r rune) int { return len(w.getRune(r)) }
|
||||
func (w *ArtWidget) getRuneWidth(r rune) int {
|
||||
rn := w.getRune(r)
|
||||
if len(rn) > 0 {
|
||||
return len(rn[0])
|
||||
}
|
||||
return 0
|
||||
w.buffer.Draw(w.x, w.y, screen)
|
||||
}
|
||||
|
||||
func (w *ArtWidget) Active() bool { return w.active }
|
||||
@@ -127,31 +106,47 @@ func (w *ArtWidget) SetH(h int) { w.h = h }
|
||||
func (w *ArtWidget) GetW() int { return w.w }
|
||||
func (w *ArtWidget) GetH() int { return w.h }
|
||||
|
||||
func (w *ArtWidget) WantW() int {
|
||||
var wd int
|
||||
func (w *ArtWidget) WantW() int { return w.buffer.Width() }
|
||||
func (w *ArtWidget) WantH() int { return w.buffer.Height() }
|
||||
func (w *ArtWidget) MinW() int { return w.buffer.Width() }
|
||||
func (w *ArtWidget) MinH() int { return w.buffer.Height() }
|
||||
|
||||
func (w *ArtWidget) SetText(txt string) {
|
||||
w.text = txt
|
||||
w.buildBuffer()
|
||||
}
|
||||
func (w *ArtWidget) GetText() string { return w.text }
|
||||
|
||||
func (w *ArtWidget) SetFont(fnt string) {
|
||||
w.font = fnt
|
||||
w.buildBuffer()
|
||||
}
|
||||
func (w *ArtWidget) GetFont() string { return w.font }
|
||||
func (w *ArtWidget) buildBuffer() {
|
||||
b := NewBuffer()
|
||||
x, y := w.x, w.y
|
||||
chX := x
|
||||
for _, ch := range w.text {
|
||||
r := w.getRune(ch)
|
||||
if len(r) > 0 {
|
||||
wd += len(r[0])
|
||||
if ch == '\n' {
|
||||
x = w.x
|
||||
y = b.Height() + 1
|
||||
continue
|
||||
}
|
||||
rs := w.getRune(ch)
|
||||
var maxX int
|
||||
for rY, ln := range rs {
|
||||
for rX, bt := range ln {
|
||||
b.SetCell(chX+rX, y+rY, *NewCell(bt, w.style))
|
||||
if chX+rX > maxX {
|
||||
maxX = chX + rX
|
||||
}
|
||||
}
|
||||
}
|
||||
chX = maxX + 1
|
||||
}
|
||||
w.Log("%s: Want Width: %d", w.Id(), wd)
|
||||
return wd
|
||||
w.buffer = b
|
||||
}
|
||||
|
||||
func (w *ArtWidget) WantH() int {
|
||||
h := len(w.getRune('Q'))
|
||||
w.Log("%s: Want Height: %d", w.Id(), h)
|
||||
return h
|
||||
}
|
||||
func (w *ArtWidget) MinW() int { return w.WantW() }
|
||||
func (w *ArtWidget) MinH() int { return w.WantH() }
|
||||
|
||||
func (w *ArtWidget) SetText(txt string) { w.text = txt }
|
||||
func (w *ArtWidget) GetText() string { return w.text }
|
||||
|
||||
func (w *ArtWidget) SetFont(fnt string) { w.font = fnt }
|
||||
func (w *ArtWidget) GetFont() string { return w.font }
|
||||
func (w *ArtWidget) getRune(r rune) [][]rune { return w.currFont.GetRune(r) }
|
||||
|
||||
func (w *ArtWidget) initFonts() {
|
||||
f := NewArtWidgetFont("miniwi")
|
||||
|
||||
Reference in New Issue
Block a user