2019-04-10 00:35:44 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2019-04-10 00:35:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultFont is the default font settings used for a Label.
|
|
|
|
var DefaultFont = render.Text{
|
|
|
|
Size: 12,
|
|
|
|
Color: render.Black,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Label is a simple text label widget.
|
|
|
|
type Label struct {
|
|
|
|
BaseWidget
|
|
|
|
|
|
|
|
// Configurable fields for the constructor.
|
|
|
|
Text string
|
|
|
|
TextVariable *string
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
IntVariable *int
|
2019-04-10 00:35:44 +00:00
|
|
|
Font render.Text
|
|
|
|
|
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
lineHeight int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLabel creates a new label.
|
|
|
|
func NewLabel(c Label) *Label {
|
|
|
|
w := &Label{
|
|
|
|
Text: c.Text,
|
|
|
|
TextVariable: c.TextVariable,
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
IntVariable: c.IntVariable,
|
2019-04-10 00:35:44 +00:00
|
|
|
Font: DefaultFont,
|
|
|
|
}
|
|
|
|
if !c.Font.IsZero() {
|
|
|
|
w.Font = c.Font
|
|
|
|
}
|
|
|
|
w.IDFunc(func() string {
|
|
|
|
return fmt.Sprintf(`Label<"%s">`, w.text().Text)
|
|
|
|
})
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// text returns the label's displayed text, coming from the TextVariable if
|
|
|
|
// available or else the Text attribute instead.
|
|
|
|
func (w *Label) text() render.Text {
|
|
|
|
if w.TextVariable != nil {
|
|
|
|
w.Font.Text = *w.TextVariable
|
|
|
|
return w.Font
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
} else if w.IntVariable != nil {
|
|
|
|
w.Font.Text = fmt.Sprintf("%d", *w.IntVariable)
|
|
|
|
return w.Font
|
2019-04-10 00:35:44 +00:00
|
|
|
}
|
|
|
|
w.Font.Text = w.Text
|
|
|
|
return w.Font
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the current text value displayed in the widget, whether it was
|
|
|
|
// the hardcoded value or a TextVariable.
|
|
|
|
func (w *Label) Value() string {
|
|
|
|
return w.text().Text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the size of the label widget.
|
|
|
|
func (w *Label) Compute(e render.Engine) {
|
|
|
|
text := w.text()
|
|
|
|
lines := strings.Split(text.Text, "\n")
|
|
|
|
|
|
|
|
// Max rect to encompass all lines of text.
|
|
|
|
var maxRect = render.Rect{}
|
|
|
|
for _, line := range lines {
|
2019-04-14 22:25:03 +00:00
|
|
|
if line == "" {
|
|
|
|
line = "<empty>"
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
text.Text = line // only this line at this time.
|
|
|
|
rect, err := e.ComputeTextRect(text)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("%s: failed to compute text rect: %s", w, err)) // TODO return an error
|
|
|
|
}
|
|
|
|
|
|
|
|
if rect.W > maxRect.W {
|
|
|
|
maxRect.W = rect.W
|
|
|
|
}
|
|
|
|
maxRect.H += rect.H
|
|
|
|
w.lineHeight = int(rect.H)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
padX = w.Font.Padding + w.Font.PadX
|
|
|
|
padY = w.Font.Padding + w.Font.PadY
|
|
|
|
)
|
|
|
|
|
|
|
|
if !w.FixedSize() {
|
2019-04-19 22:08:00 +00:00
|
|
|
w.ResizeAuto(render.Rect{
|
2019-04-10 00:35:44 +00:00
|
|
|
W: maxRect.W + (padX * 2),
|
|
|
|
H: maxRect.H + (padY * 2),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
w.MoveTo(render.Point{
|
|
|
|
X: maxRect.X + w.BoxThickness(1),
|
|
|
|
Y: maxRect.Y + w.BoxThickness(1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present the label widget.
|
|
|
|
func (w *Label) Present(e render.Engine, P render.Point) {
|
|
|
|
if w.Hidden() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
border := w.BoxThickness(1)
|
|
|
|
|
|
|
|
var (
|
|
|
|
text = w.text()
|
|
|
|
padX = w.Font.Padding + w.Font.PadX
|
|
|
|
padY = w.Font.Padding + w.Font.PadY
|
|
|
|
)
|
|
|
|
|
|
|
|
w.DrawBox(e, P)
|
|
|
|
for i, line := range strings.Split(text.Text, "\n") {
|
|
|
|
text.Text = line
|
|
|
|
e.DrawText(text, render.Point{
|
|
|
|
X: P.X + border + padX,
|
|
|
|
Y: P.Y + border + padY + int32(i*w.lineHeight),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|