2018-07-25 16:03:49 +00:00
|
|
|
package ui
|
|
|
|
|
2018-08-01 00:18:13 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 20:31:58 +00:00
|
|
|
"strings"
|
2018-08-01 00:18:13 +00:00
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
2018-08-01 00:18:13 +00:00
|
|
|
)
|
2018-07-25 16:03:49 +00:00
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// DefaultFont is the default font settings used for a Label.
|
|
|
|
var DefaultFont = render.Text{
|
|
|
|
Size: 12,
|
|
|
|
Color: render.Black,
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// Label is a simple text label widget.
|
|
|
|
type Label struct {
|
|
|
|
BaseWidget
|
2018-08-05 19:54:57 +00:00
|
|
|
|
|
|
|
// Configurable fields for the constructor.
|
|
|
|
Text string
|
|
|
|
TextVariable *string
|
|
|
|
Font render.Text
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
lineHeight int
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLabel creates a new label.
|
2018-08-05 19:54:57 +00:00
|
|
|
func NewLabel(c Label) *Label {
|
2018-08-01 00:18:13 +00:00
|
|
|
w := &Label{
|
2018-08-05 19:54:57 +00:00
|
|
|
Text: c.Text,
|
|
|
|
TextVariable: c.TextVariable,
|
2018-08-11 00:19:47 +00:00
|
|
|
Font: DefaultFont,
|
|
|
|
}
|
|
|
|
if !c.Font.IsZero() {
|
|
|
|
w.Font = c.Font
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
2018-08-01 00:18:13 +00:00
|
|
|
w.IDFunc(func() string {
|
2018-08-11 00:19:47 +00:00
|
|
|
return fmt.Sprintf(`Label<"%s">`, w.text().Text)
|
2018-08-01 00:18:13 +00:00
|
|
|
})
|
|
|
|
return w
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
w.Font.Text = w.Text
|
|
|
|
return w.Font
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// Compute the size of the label widget.
|
|
|
|
func (w *Label) Compute(e render.Engine) {
|
2018-10-19 20:31:58 +00:00
|
|
|
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>"
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
text.Text = line // only this line at this time.
|
|
|
|
rect, err := e.ComputeTextRect(text)
|
|
|
|
if err != nil {
|
2019-04-10 00:35:44 +00:00
|
|
|
panic(fmt.Sprintf("%s: failed to compute text rect: %s", w, err)) // TODO return an error
|
2018-10-19 20:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rect.W > maxRect.W {
|
|
|
|
maxRect.W = rect.W
|
|
|
|
}
|
|
|
|
maxRect.H += rect.H
|
|
|
|
w.lineHeight = int(rect.H)
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
2018-08-02 01:52:52 +00:00
|
|
|
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
var (
|
|
|
|
padX = w.Font.Padding + w.Font.PadX
|
|
|
|
padY = w.Font.Padding + w.Font.PadY
|
|
|
|
)
|
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
if !w.FixedSize() {
|
2019-04-19 20:51:27 +00:00
|
|
|
w.ResizeAuto(render.Rect{
|
2018-10-19 20:31:58 +00:00
|
|
|
W: maxRect.W + (padX * 2),
|
|
|
|
H: maxRect.H + (padY * 2),
|
2018-08-02 01:52:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-01 00:18:13 +00:00
|
|
|
w.MoveTo(render.Point{
|
2018-10-19 20:31:58 +00:00
|
|
|
X: maxRect.X + w.BoxThickness(1),
|
|
|
|
Y: maxRect.Y + w.BoxThickness(1),
|
2018-08-01 00:18:13 +00:00
|
|
|
})
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Present the label widget.
|
2018-08-05 19:54:57 +00:00
|
|
|
func (w *Label) Present(e render.Engine, P render.Point) {
|
2018-10-08 20:06:42 +00:00
|
|
|
if w.Hidden() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
border := w.BoxThickness(1)
|
|
|
|
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
var (
|
2018-10-19 20:31:58 +00:00
|
|
|
text = w.text()
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
padX = w.Font.Padding + w.Font.PadX
|
|
|
|
padY = w.Font.Padding + w.Font.PadY
|
|
|
|
)
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
w.DrawBox(e, P)
|
2018-10-19 20:31:58 +00:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|