doodle/pkg/windows/text_tool.go
Noah Petherbridge 77297fd60d Text Tool and Pan Tool
Two new tools added to the Level Editor:

* Pan Tool: left-click to scroll the level around safely.
* Text Tool: write text onto your level.

Features of the Text Tool:

* Can choose from the game's built-in fonts, size and enter the message
  you want to write.
* The mouse cursor previews the text when hovered over the level.
* Click to "stamp" the text onto your level. The currently selected
  color swatch will be used to color the text in.
* Adds two new fonts: Azulian.ttf and Rive.ttf that can be selected in
  the Text Tool.

Some implementation notes:

* Added package native/engine_sdl.go that handles the lower-level
  SDL2_TTF logic to rasterize the text into a black&white image.
* WASM not supported yet (if the game even still built for WASM);
  native/engine_wasm.go stubs out the TextToImage() call with a "not
  supported" error just in case.

Other changes:

* New Toolbar icons: they are 24x24 instead of 32x32 to make more room
  for more tools.
* The toolbar now shows two buttons per row for a more densely packed
  layout. For very narrow screen widths (< 600px) the default Vertical
  Toolbar layout will use one-button-per-row to not eat too much screen
  real estate.
* In the Horizontal Toolbars layout there are 2 buttons per column.
2022-03-05 15:34:20 -08:00

125 lines
2.9 KiB
Go

package windows
import (
"strconv"
"git.kirsle.net/apps/doodle/assets"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/branding"
"git.kirsle.net/apps/doodle/pkg/shmem"
magicform "git.kirsle.net/apps/doodle/pkg/uix/magic-form"
"git.kirsle.net/go/render"
"git.kirsle.net/go/ui"
)
// TextTool window.
type TextTool struct {
// Settings passed in by doodle
Supervisor *ui.Supervisor
Engine render.Engine
// Callback when font settings are changed.
OnChangeSettings func(font string, size int, message string)
}
// NewTextToolWindow initializes the window.
func NewTextToolWindow(cfg TextTool) *ui.Window {
window := ui.NewWindow("Text Tool")
window.SetButtons(ui.CloseButton)
window.Configure(ui.Config{
Width: 330,
Height: 170,
Background: render.Grey,
})
// Text variables
var (
currentText = branding.AppName
fontName = balance.TextToolDefaultFont
fontSize = 16
)
// Get a listing of the available fonts.
fonts, _ := assets.AssetDir("assets/fonts")
var fontOption = []magicform.Option{}
for _, font := range fonts {
// Select the first font by default.
if fontName == "" {
fontName = font
}
fontOption = append(fontOption, magicform.Option{
Label: font,
Value: font,
})
}
// Send the default config out.
if cfg.OnChangeSettings != nil {
cfg.OnChangeSettings(fontName, fontSize, currentText)
}
form := magicform.Form{
Supervisor: cfg.Supervisor,
Engine: cfg.Engine,
Vertical: true,
LabelWidth: 100,
PadY: 2,
}
form.Create(window.ContentFrame(), []magicform.Field{
{
Label: "Font Face:",
Font: balance.LabelFont,
Options: fontOption,
SelectValue: fontName,
OnSelect: func(v interface{}) {
fontName = v.(string)
if cfg.OnChangeSettings != nil {
cfg.OnChangeSettings(fontName, fontSize, currentText)
}
},
},
{
Label: "Font Size:",
Font: balance.LabelFont,
IntVariable: &fontSize,
OnClick: func() {
shmem.Prompt("Enter new font size: ", func(answer string) {
if answer != "" {
if i, err := strconv.Atoi(answer); err == nil {
fontSize = i
if cfg.OnChangeSettings != nil {
cfg.OnChangeSettings(fontName, fontSize, currentText)
}
} else {
shmem.FlashError("Not a valid font size: %s", answer)
}
}
})
},
},
{
Label: "Message:",
Font: balance.LabelFont,
TextVariable: &currentText,
OnClick: func() {
shmem.Prompt("Enter new message: ", func(answer string) {
if answer != "" {
currentText = answer
if cfg.OnChangeSettings != nil {
cfg.OnChangeSettings(fontName, fontSize, currentText)
}
}
})
},
},
{
Label: "Be sure the Text Tool is selected, and click onto your\n" +
"drawing to place this text onto it.",
Font: balance.UIFont,
},
})
return window
}