2019-03-05 20:52:18 +00:00
|
|
|
package allg5ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
2019-03-12 18:43:52 +00:00
|
|
|
"math"
|
2020-05-15 07:20:44 +00:00
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"opslag.de/schobers/zntg"
|
2019-03-05 20:52:18 +00:00
|
|
|
|
2019-12-19 06:07:43 +00:00
|
|
|
"opslag.de/schobers/allg5"
|
2019-03-05 20:52:18 +00:00
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
"opslag.de/schobers/zntg/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ ui.Renderer = &Renderer{}
|
|
|
|
|
|
|
|
func NewRenderer(w, h int, opts allg5.NewDisplayOptions) (*Renderer, error) {
|
2020-05-15 07:20:44 +00:00
|
|
|
var clean zntg.Actions
|
|
|
|
defer func() { clean.Do() }()
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
var err = allg5.Init(allg5.InitAll)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
disp, err := allg5.NewDisplay(w, h, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
clean = clean.Add(disp.Destroy)
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
eq, err := allg5.NewEventQueue()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
clean = clean.Add(eq.Destroy)
|
2019-03-05 20:52:18 +00:00
|
|
|
|
2019-03-06 17:18:35 +00:00
|
|
|
user := allg5.NewUserEventSource()
|
2019-03-05 20:52:18 +00:00
|
|
|
eq.RegisterKeyboard()
|
|
|
|
eq.RegisterMouse()
|
|
|
|
eq.RegisterDisplay(disp)
|
2019-03-06 17:18:35 +00:00
|
|
|
eq.RegisterUserEvents(user)
|
2019-03-05 20:52:18 +00:00
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
allg5.CaptureNewBitmapFlags().Mutate(func(m allg5.FlagMutation) {
|
|
|
|
m.Set(allg5.NewBitmapFlagMinLinear)
|
|
|
|
})
|
|
|
|
clean = nil
|
|
|
|
|
2020-05-17 13:30:52 +00:00
|
|
|
return &Renderer{disp, eq, nil, user, &ui.OSResources{}, dispPos(disp), ui.KeyState{}, ui.KeyModifierNone, ui.MouseCursorDefault}, nil
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Renderer implements ui.Renderer using Allegro 5.
|
|
|
|
type Renderer struct {
|
2020-05-15 07:20:44 +00:00
|
|
|
disp *allg5.Display
|
|
|
|
eq *allg5.EventQueue
|
|
|
|
unh func(allg5.Event)
|
|
|
|
user *allg5.UserEventSource
|
2020-05-25 20:24:06 +00:00
|
|
|
res ui.PhysicalResources
|
2020-05-15 07:20:44 +00:00
|
|
|
|
2020-05-17 13:30:52 +00:00
|
|
|
dispPos geom.Point
|
2020-05-15 07:20:44 +00:00
|
|
|
keys ui.KeyState
|
|
|
|
modifiers ui.KeyModifier
|
2019-03-11 16:30:41 +00:00
|
|
|
cursor ui.MouseCursor
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Renderer implementation (events)
|
|
|
|
|
2020-05-17 19:02:07 +00:00
|
|
|
func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) bool {
|
2019-03-05 20:52:18 +00:00
|
|
|
r.disp.Flip()
|
|
|
|
|
|
|
|
var ev = eventWait(r.eq, wait)
|
2019-07-04 20:03:29 +00:00
|
|
|
if ev == nil {
|
2020-05-17 19:02:07 +00:00
|
|
|
return false
|
2019-07-04 20:03:29 +00:00
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
|
|
|
|
cursor := r.cursor
|
|
|
|
r.cursor = ui.MouseCursorDefault
|
|
|
|
var unhandled bool
|
2019-07-04 20:03:29 +00:00
|
|
|
for ev != nil {
|
2019-03-05 20:52:18 +00:00
|
|
|
switch e := ev.(type) {
|
|
|
|
case *allg5.DisplayCloseEvent:
|
|
|
|
t.Handle(&ui.DisplayCloseEvent{EventBase: eventBase(e)})
|
2019-03-05 21:51:57 +00:00
|
|
|
case *allg5.DisplayResizeEvent:
|
|
|
|
t.Handle(&ui.DisplayResizeEvent{EventBase: eventBase(e), Bounds: geom.RectF32(float32(e.X), float32(e.Y), float32(e.X+e.Width), float32(e.Y+e.Height))})
|
2019-04-10 19:23:56 +00:00
|
|
|
case *allg5.KeyCharEvent:
|
2020-05-15 07:20:44 +00:00
|
|
|
if r.modifiers&ui.KeyModifierControl == ui.KeyModifierNone && !unicode.IsControl(e.UnicodeCharacter) {
|
|
|
|
t.Handle(&ui.TextInputEvent{EventBase: eventBase(e), Character: e.UnicodeCharacter})
|
|
|
|
} else {
|
|
|
|
unhandled = true
|
|
|
|
}
|
|
|
|
case *allg5.KeyDownEvent:
|
|
|
|
key := key(e.KeyCode)
|
|
|
|
r.keys[key] = true
|
|
|
|
r.modifiers = r.keys.Modifiers()
|
|
|
|
t.Handle(&ui.KeyDownEvent{EventBase: eventBase(e), Key: key, Modifiers: r.modifiers})
|
|
|
|
case *allg5.KeyUpEvent:
|
|
|
|
key := key(e.KeyCode)
|
|
|
|
r.keys[key] = false
|
|
|
|
r.modifiers = r.keys.Modifiers()
|
|
|
|
t.Handle(&ui.KeyUpEvent{EventBase: eventBase(e), Key: key, Modifiers: r.modifiers})
|
2019-03-05 20:52:18 +00:00
|
|
|
case *allg5.MouseButtonDownEvent:
|
|
|
|
t.Handle(&ui.MouseButtonDownEvent{MouseEvent: mouseEvent(e.MouseEvent), Button: ui.MouseButton(e.Button)})
|
|
|
|
case *allg5.MouseButtonUpEvent:
|
|
|
|
t.Handle(&ui.MouseButtonUpEvent{MouseEvent: mouseEvent(e.MouseEvent), Button: ui.MouseButton(e.Button)})
|
2019-06-24 19:56:52 +00:00
|
|
|
case *allg5.MouseEnterEvent:
|
|
|
|
t.Handle(&ui.MouseLeaveEvent{MouseEvent: mouseEvent(e.MouseEvent)})
|
|
|
|
case *allg5.MouseLeaveEvent:
|
|
|
|
t.Handle(&ui.MouseLeaveEvent{MouseEvent: mouseEvent(e.MouseEvent)})
|
2019-03-05 20:52:18 +00:00
|
|
|
case *allg5.MouseMoveEvent:
|
2019-03-05 21:42:57 +00:00
|
|
|
t.Handle(&ui.MouseMoveEvent{MouseEvent: mouseEvent(e.MouseEvent), MouseWheel: float32(e.DeltaZ)})
|
2019-03-06 17:18:35 +00:00
|
|
|
case *allg5.UserEvent:
|
2019-03-13 20:20:31 +00:00
|
|
|
t.Handle(&ui.RefreshEvent{EventBase: eventBase(e)})
|
2019-10-12 07:01:46 +00:00
|
|
|
default:
|
|
|
|
if r.unh != nil {
|
|
|
|
r.unh(e)
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
unhandled = true
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
ev = r.eq.Get()
|
|
|
|
}
|
2020-05-17 13:30:52 +00:00
|
|
|
dispPos := dispPos(r.disp)
|
|
|
|
if dispPos != r.dispPos {
|
|
|
|
r.dispPos = dispPos
|
|
|
|
w := r.disp.Width()
|
|
|
|
h := r.disp.Height()
|
|
|
|
t.Handle(&ui.DisplayMoveEvent{Bounds: r.dispPos.RectRel2D(w, h).ToF32()})
|
|
|
|
}
|
2019-03-11 16:30:41 +00:00
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
if !unhandled && cursor != r.cursor {
|
2019-03-11 16:30:41 +00:00
|
|
|
switch r.cursor {
|
|
|
|
case ui.MouseCursorNone:
|
|
|
|
r.disp.SetMouseCursor(allg5.MouseCursorNone)
|
|
|
|
case ui.MouseCursorDefault:
|
|
|
|
r.disp.SetMouseCursor(allg5.MouseCursorDefault)
|
|
|
|
case ui.MouseCursorNotAllowed:
|
|
|
|
r.disp.SetMouseCursor(allg5.MouseCursorUnavailable)
|
|
|
|
case ui.MouseCursorPointer:
|
|
|
|
r.disp.SetMouseCursor(allg5.MouseCursorLink)
|
|
|
|
case ui.MouseCursorText:
|
|
|
|
r.disp.SetMouseCursor(allg5.MouseCursorEdit)
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 19:02:07 +00:00
|
|
|
return true
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 07:01:46 +00:00
|
|
|
func (r *Renderer) RegisterRecorder(rec *allg5.Recorder) {
|
|
|
|
r.eq.RegisterRecorder(rec)
|
|
|
|
}
|
|
|
|
|
2019-03-06 17:18:35 +00:00
|
|
|
func (r *Renderer) Refresh() {
|
|
|
|
r.user.EmitEvent()
|
|
|
|
}
|
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
func (r *Renderer) Stamp() float64 {
|
|
|
|
return allg5.GetTime()
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
// Renderer implementation (lifetime)
|
|
|
|
|
|
|
|
func (r *Renderer) Destroy() error {
|
2019-03-06 17:18:35 +00:00
|
|
|
r.user.Destroy()
|
2019-03-05 20:52:18 +00:00
|
|
|
r.eq.Destroy()
|
|
|
|
r.disp.Destroy()
|
2020-05-15 12:20:07 +00:00
|
|
|
r.res.Destroy()
|
2019-03-05 20:52:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renderer implementation (drawing)
|
|
|
|
|
|
|
|
func (r *Renderer) Clear(c color.Color) {
|
|
|
|
allg5.ClearToColor(newColor(c))
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func (r *Renderer) CreateFontPath(path string, size int) (ui.Font, error) {
|
|
|
|
path, err := r.res.FetchResource(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := allg5.LoadTTFFont(path, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &font{f}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:40:50 +00:00
|
|
|
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (*texture, error) {
|
2020-05-15 07:20:44 +00:00
|
|
|
im, err := source.CreateImage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 20:52:18 +00:00
|
|
|
bmp, err := allg5.NewBitmapFromImage(im, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
if keepSource {
|
|
|
|
return &texture{bmp, source}, nil
|
|
|
|
}
|
|
|
|
return &texture{bmp, nil}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) CreateTexture(source ui.ImageSource) (ui.Texture, error) {
|
|
|
|
return r.createTexture(source, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) CreateTextureGo(im image.Image, source bool) (ui.Texture, error) {
|
2020-05-20 17:45:45 +00:00
|
|
|
return r.createTexture(ui.ImageSourceGo{Image: im}, source)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
func (r *Renderer) CreateTexturePath(path string, source bool) (ui.Texture, error) {
|
2020-05-22 07:08:50 +00:00
|
|
|
resourcePath, err := r.res.FetchResource(path)
|
2020-05-15 12:20:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-22 07:08:50 +00:00
|
|
|
bmp, err := allg5.LoadBitmap(resourcePath)
|
2019-03-05 20:52:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-20 17:45:45 +00:00
|
|
|
if source {
|
|
|
|
return &texture{bmp, ui.ImageSourceResource{Resources: r.res, Name: path}}, nil
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
return &texture{bmp, nil}, nil
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
func (r *Renderer) CreateTextureTarget(w, h float32) (ui.Texture, error) {
|
2019-03-05 20:52:18 +00:00
|
|
|
bmp, err := allg5.NewVideoBitmap(int(w), int(h))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
return &texture{bmp, nil}, nil
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func (r *Renderer) DefaultTarget() ui.Texture {
|
2020-05-15 07:20:44 +00:00
|
|
|
return &texture{r.disp.Target(), nil}
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 20:05:08 +00:00
|
|
|
func (r *Renderer) Display() *allg5.Display { return r.disp }
|
|
|
|
|
2020-05-17 09:12:45 +00:00
|
|
|
func (r *Renderer) DrawTexture(texture ui.Texture, p geom.RectangleF32) {
|
|
|
|
r.DrawTextureOptions(texture, p, ui.DrawOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) DrawTextureOptions(texture ui.Texture, p geom.RectangleF32, opts ui.DrawOptions) {
|
2020-05-23 06:20:04 +00:00
|
|
|
bmp, ok := r.mustGetSubBitmap(texture, opts.Source)
|
|
|
|
if ok {
|
|
|
|
defer bmp.Destroy()
|
|
|
|
}
|
2020-05-17 09:12:45 +00:00
|
|
|
x, y := snap(p.Min)
|
|
|
|
var o allg5.DrawOptions
|
|
|
|
if opts.Tint != nil {
|
|
|
|
tint := newColor(opts.Tint)
|
|
|
|
o.Tint = &tint
|
|
|
|
}
|
|
|
|
w, h := p.Dx(), p.Dy()
|
|
|
|
bmpW, bmpH := float32(bmp.Width()), float32(bmp.Height())
|
|
|
|
if w != bmpW || h != bmpH {
|
|
|
|
o.Scale = &allg5.Scale{Horizontal: w / bmpW, Vertical: h / bmpH}
|
|
|
|
}
|
|
|
|
bmp.DrawOptions(x, y, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) DrawTexturePoint(texture ui.Texture, p geom.PointF32) {
|
2020-05-12 21:03:43 +00:00
|
|
|
bmp := r.mustGetBitmap(texture)
|
2019-03-13 18:49:00 +00:00
|
|
|
x, y := snap(p)
|
|
|
|
bmp.Draw(x, y)
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:12:45 +00:00
|
|
|
func (r *Renderer) DrawTexturePointOptions(texture ui.Texture, p geom.PointF32, opts ui.DrawOptions) {
|
2020-05-23 06:20:04 +00:00
|
|
|
bmp, ok := r.mustGetSubBitmap(texture, opts.Source)
|
|
|
|
if ok {
|
|
|
|
defer bmp.Destroy()
|
|
|
|
}
|
2019-03-13 18:49:00 +00:00
|
|
|
var o allg5.DrawOptions
|
|
|
|
if opts.Tint != nil {
|
|
|
|
tint := newColor(opts.Tint)
|
|
|
|
o.Tint = &tint
|
|
|
|
}
|
|
|
|
x, y := snap(p)
|
|
|
|
bmp.DrawOptions(x, y, o)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) FillRectangle(rect geom.RectangleF32, c color.Color) {
|
|
|
|
allg5.DrawFilledRectangle(rect.Min.X, rect.Min.Y, rect.Max.X, rect.Max.Y, newColor(c))
|
|
|
|
}
|
|
|
|
|
2021-03-23 11:31:31 +00:00
|
|
|
func (r *Renderer) Line(p, q geom.PointF32, color color.Color, thickness float32) {
|
|
|
|
allg5.DrawLine(p.X, p.Y, q.X, q.Y, newColor(color), thickness)
|
|
|
|
}
|
|
|
|
|
2022-05-01 12:02:28 +00:00
|
|
|
func (r *Renderer) Location() geom.Point {
|
|
|
|
return geom.Pt(r.disp.Position()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) Move(to geom.Point) {
|
|
|
|
r.disp.SetPosition(to.X, to.Y)
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func (r *Renderer) mustGetBitmap(t ui.Texture) *allg5.Bitmap {
|
|
|
|
texture, ok := t.(*texture)
|
2019-03-05 20:52:18 +00:00
|
|
|
if !ok {
|
2020-05-15 07:20:44 +00:00
|
|
|
panic("texture must be created on same renderer")
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
return texture.bmp
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 06:20:04 +00:00
|
|
|
func (r *Renderer) mustGetSubBitmap(t ui.Texture, source *geom.RectangleF32) (*allg5.Bitmap, bool) {
|
|
|
|
texture, ok := t.(*texture)
|
|
|
|
if !ok {
|
|
|
|
panic("texture must be created on same renderer")
|
|
|
|
}
|
|
|
|
if source == nil {
|
|
|
|
return texture.bmp, false
|
|
|
|
}
|
|
|
|
src := source.ToInt()
|
|
|
|
return texture.bmp.Sub(src.Min.X, src.Min.Y, src.Dx(), src.Dy()), true
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (r *Renderer) Rectangle(rect geom.RectangleF32, c color.Color, thickness float32) {
|
2019-03-13 18:49:00 +00:00
|
|
|
minX, minY := snap(rect.Min)
|
|
|
|
maxX, maxY := snap(rect.Max)
|
|
|
|
allg5.DrawRectangle(minX, minY, maxX, maxY, newColor(c), thickness)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func (r *Renderer) RenderTo(texture ui.Texture) {
|
|
|
|
bmp := r.mustGetBitmap(texture)
|
2019-03-05 20:52:18 +00:00
|
|
|
bmp.SetAsTarget()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) RenderToDisplay() {
|
|
|
|
r.disp.SetAsTarget()
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:40:50 +00:00
|
|
|
func (r *Renderer) Resize(width, height int) {
|
|
|
|
r.disp.Resize(width, height)
|
|
|
|
}
|
|
|
|
|
2020-05-15 12:20:07 +00:00
|
|
|
func (r *Renderer) Resources() ui.Resources { return r.res }
|
|
|
|
|
2020-12-12 13:40:50 +00:00
|
|
|
func (r *Renderer) Size() geom.Point {
|
|
|
|
return geom.Pt(r.disp.Width(), r.disp.Height())
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:40:50 +00:00
|
|
|
func (r *Renderer) SetIcon(source ui.ImageSource) {
|
|
|
|
texture, err := r.createTexture(source, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer texture.Destroy()
|
|
|
|
r.disp.SetIcon(texture.bmp)
|
2019-03-06 17:18:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 16:30:41 +00:00
|
|
|
func (r *Renderer) SetMouseCursor(c ui.MouseCursor) {
|
2020-05-15 07:20:44 +00:00
|
|
|
r.cursor = c
|
2019-03-11 16:30:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 19:02:38 +00:00
|
|
|
func (r *Renderer) SetPosition(p geom.PointF32) { r.disp.SetPosition(int(p.X), int(p.Y)) }
|
|
|
|
|
2020-07-07 16:19:34 +00:00
|
|
|
func (r *Renderer) SetResourceProvider(res ui.Resources) {
|
2020-05-15 12:20:07 +00:00
|
|
|
if r.res != nil {
|
|
|
|
r.res.Destroy()
|
|
|
|
}
|
2020-07-07 16:19:34 +00:00
|
|
|
|
|
|
|
if phys, ok := res.(ui.PhysicalResources); ok {
|
|
|
|
r.res = phys
|
|
|
|
} else {
|
|
|
|
copy, err := ui.NewCopyResources("allg5ui", res, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.res = copy
|
2020-05-25 20:24:06 +00:00
|
|
|
}
|
2020-05-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 07:01:46 +00:00
|
|
|
func (r *Renderer) SetUnhandledEventHandler(handler func(allg5.Event)) {
|
|
|
|
r.unh = handler
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (r *Renderer) SetWindowTitle(t string) {
|
|
|
|
r.disp.SetWindowTitle(t)
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func (r *Renderer) Target() ui.Texture {
|
2020-05-15 07:20:44 +00:00
|
|
|
return &texture{allg5.CurrentTarget(), nil}
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func (r *Renderer) text(f ui.Font, p geom.PointF32, c color.Color, t string, align allg5.HorizontalAlignment) {
|
|
|
|
font, ok := f.(*font)
|
|
|
|
if !ok {
|
2019-03-05 20:52:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-13 18:49:00 +00:00
|
|
|
x, y := snap(p)
|
2020-05-15 13:42:24 +00:00
|
|
|
font.Draw(x, y, newColor(c), align, t)
|
2019-06-24 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func (r *Renderer) Text(font ui.Font, p geom.PointF32, c color.Color, t string) {
|
|
|
|
r.text(font, p, c, t, allg5.AlignLeft)
|
2019-06-24 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func (r *Renderer) TextAlign(font ui.Font, p geom.PointF32, c color.Color, t string, align ui.HorizontalAlignment) {
|
2019-06-24 18:58:14 +00:00
|
|
|
var alignment = allg5.AlignLeft
|
|
|
|
switch align {
|
|
|
|
case ui.AlignCenter:
|
|
|
|
alignment = allg5.AlignCenter
|
|
|
|
case ui.AlignRight:
|
|
|
|
alignment = allg5.AlignRight
|
|
|
|
}
|
2020-05-15 13:42:24 +00:00
|
|
|
r.text(font, p, c, t, alignment)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 15:42:02 +00:00
|
|
|
func (r *Renderer) TextTexture(font ui.Font, color color.Color, text string) (ui.Texture, error) {
|
|
|
|
return ui.TextTexture(r, font, color, text)
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
// Utility functions
|
|
|
|
|
|
|
|
func eventWait(eq *allg5.EventQueue, wait bool) allg5.Event {
|
|
|
|
if wait {
|
|
|
|
return eq.GetWait()
|
|
|
|
}
|
|
|
|
return eq.Get()
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventBase(e allg5.Event) ui.EventBase {
|
|
|
|
return ui.EventBase{StampInSeconds: e.Stamp()}
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:23:56 +00:00
|
|
|
func keyModifiers(mods allg5.KeyMod) ui.KeyModifier {
|
|
|
|
var m ui.KeyModifier
|
|
|
|
if mods&allg5.KeyModShift == allg5.KeyModShift {
|
|
|
|
m |= ui.KeyModifierShift
|
|
|
|
} else if mods&allg5.KeyModCtrl == allg5.KeyModCtrl {
|
|
|
|
m |= ui.KeyModifierControl
|
|
|
|
} else if mods&allg5.KeyModAlt == allg5.KeyModAlt {
|
|
|
|
m |= ui.KeyModifierAlt
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func mouseEvent(e allg5.MouseEvent) ui.MouseEvent {
|
|
|
|
return ui.MouseEvent{EventBase: eventBase(e), X: float32(e.X), Y: float32(e.Y)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newColor(c color.Color) allg5.Color {
|
|
|
|
if c == nil {
|
|
|
|
return newColor(color.Black)
|
|
|
|
}
|
2019-03-13 18:49:00 +00:00
|
|
|
return allg5.NewColorGo(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func snap(p geom.PointF32) (float32, float32) {
|
|
|
|
return float32(math.Round(float64(p.X))), float32(math.Round(float64(p.Y)))
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
2020-05-17 13:30:52 +00:00
|
|
|
|
|
|
|
func dispPos(disp *allg5.Display) geom.Point {
|
|
|
|
x, y := disp.Position()
|
|
|
|
return geom.Pt(x, y)
|
|
|
|
}
|