2018-07-22 00:12:22 +00:00
|
|
|
package sdl
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"fmt"
|
2018-07-22 03:43:01 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-12-30 22:22:50 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/src/events"
|
2018-07-22 00:12:22 +00:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
"github.com/veandco/go-sdl2/ttf"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
// TODO: font filenames
|
|
|
|
var defaultFontFilename = "./fonts/DejaVuSans.ttf"
|
|
|
|
|
|
|
|
var fonts = map[string]*ttf.Font{}
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
// LoadFont loads and caches the font at a given size.
|
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
|
|
|
func LoadFont(filename string, size int) (*ttf.Font, error) {
|
|
|
|
if filename == "" {
|
|
|
|
filename = defaultFontFilename
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cached font available?
|
|
|
|
keyName := fmt.Sprintf("%s@%d", filename, size)
|
|
|
|
if font, ok := fonts[keyName]; ok {
|
2018-07-22 00:12:22 +00:00
|
|
|
return font, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
font, err := ttf.OpenFont(filename, size)
|
2018-07-22 00:12:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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
|
|
|
fonts[keyName] = font
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
return font, nil
|
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Keysym returns the current key pressed, taking into account the Shift
|
|
|
|
// key modifier.
|
|
|
|
func (r *Renderer) Keysym(ev *events.State) string {
|
|
|
|
if key := ev.KeyName.Read(); key != "" {
|
|
|
|
if ev.ShiftActive.Pressed() {
|
|
|
|
if symbol, ok := shiftMap[key]; ok {
|
|
|
|
return symbol
|
|
|
|
}
|
|
|
|
return strings.ToUpper(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// ComputeTextRect computes and returns a Rect for how large the text would
|
|
|
|
// appear if rendered.
|
|
|
|
func (r *Renderer) ComputeTextRect(text render.Text) (render.Rect, error) {
|
|
|
|
var (
|
|
|
|
rect render.Rect
|
|
|
|
font *ttf.Font
|
|
|
|
surface *sdl.Surface
|
|
|
|
color = ColorToSDL(text.Color)
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
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
|
|
|
if font, err = LoadFont(text.FontFilename, text.Size); err != nil {
|
2018-07-25 16:03:49 +00:00
|
|
|
return rect, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if surface, err = font.RenderUTF8Blended(text.Text, color); err != nil {
|
|
|
|
return rect, err
|
|
|
|
}
|
|
|
|
defer surface.Free()
|
|
|
|
|
|
|
|
rect.W = surface.W
|
|
|
|
rect.H = surface.H
|
|
|
|
return rect, err
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// DrawText draws text on the canvas.
|
2018-07-22 03:43:01 +00:00
|
|
|
func (r *Renderer) DrawText(text render.Text, point render.Point) error {
|
2018-07-22 00:12:22 +00:00
|
|
|
var (
|
|
|
|
font *ttf.Font
|
|
|
|
surface *sdl.Surface
|
|
|
|
tex *sdl.Texture
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
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
|
|
|
if font, err = LoadFont(text.FontFilename, text.Size); err != nil {
|
2018-07-22 00:12:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
write := func(dx, dy int32, color sdl.Color) {
|
|
|
|
if surface, err = font.RenderUTF8Blended(text.Text, color); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer surface.Free()
|
|
|
|
|
|
|
|
if tex, err = r.renderer.CreateTextureFromSurface(surface); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tex.Destroy()
|
|
|
|
|
|
|
|
tmp := &sdl.Rect{
|
2018-07-22 03:43:01 +00:00
|
|
|
X: point.X + dx,
|
|
|
|
Y: point.Y + dy,
|
2018-07-22 00:12:22 +00:00
|
|
|
W: surface.W,
|
|
|
|
H: surface.H,
|
|
|
|
}
|
|
|
|
r.renderer.Copy(tex, nil, tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the text have a stroke around it?
|
|
|
|
if text.Stroke != render.Invisible {
|
|
|
|
color := ColorToSDL(text.Stroke)
|
|
|
|
write(-1, -1, color)
|
|
|
|
write(-1, 0, color)
|
|
|
|
write(-1, 1, color)
|
|
|
|
write(1, -1, color)
|
|
|
|
write(1, 0, color)
|
|
|
|
write(1, 1, color)
|
|
|
|
write(0, -1, color)
|
|
|
|
write(0, 1, color)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does it have a drop shadow?
|
|
|
|
if text.Shadow != render.Invisible {
|
|
|
|
write(1, 1, ColorToSDL(text.Shadow))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the text itself.
|
|
|
|
write(0, 0, ColorToSDL(text.Color))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2018-07-22 03:43:01 +00:00
|
|
|
|
|
|
|
// shiftMap maps keys to their Shift versions.
|
|
|
|
var shiftMap = map[string]string{
|
|
|
|
"`": "~",
|
|
|
|
"1": "!",
|
|
|
|
"2": "@",
|
|
|
|
"3": "#",
|
|
|
|
"4": "$",
|
|
|
|
"5": "%",
|
|
|
|
"6": "^",
|
|
|
|
"7": "&",
|
|
|
|
"8": "*",
|
|
|
|
"9": "(",
|
|
|
|
"0": ")",
|
|
|
|
"-": "_",
|
|
|
|
"=": "+",
|
|
|
|
"[": "{",
|
|
|
|
"]": "}",
|
|
|
|
`\`: "|",
|
|
|
|
";": ":",
|
|
|
|
`'`: `"`,
|
|
|
|
",": "<",
|
|
|
|
".": ">",
|
|
|
|
"/": "?",
|
|
|
|
}
|