Added Resize & SetIcon to Renderer.
Refactored Size (on Renderer) to return geom.Point instead of geom.PointF32. Refactored Width and Height (on Texture) to return int instead of float32. Refactored texture dimensions to be represented by ints instead of float32s.
This commit is contained in:
parent
de87c5d3aa
commit
0f03760e66
@ -180,7 +180,7 @@ func (r *Renderer) CreateFontPath(path string, size int) (ui.Font, error) {
|
||||
return &font{f}, nil
|
||||
}
|
||||
|
||||
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) {
|
||||
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (*texture, error) {
|
||||
im, err := source.CreateImage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -314,15 +314,23 @@ func (r *Renderer) RenderToDisplay() {
|
||||
r.disp.SetAsTarget()
|
||||
}
|
||||
|
||||
func (r *Renderer) Resources() ui.Resources { return r.res }
|
||||
|
||||
func (r *Renderer) Size() geom.PointF32 {
|
||||
return geom.PtF32(float32(r.disp.Width()), float32(r.disp.Height()))
|
||||
func (r *Renderer) Resize(width, height int) {
|
||||
r.disp.Resize(width, height)
|
||||
}
|
||||
|
||||
func (r *Renderer) SetIcon(texture ui.Texture) {
|
||||
bmp := r.mustGetBitmap(texture)
|
||||
r.disp.SetIcon(bmp)
|
||||
func (r *Renderer) Resources() ui.Resources { return r.res }
|
||||
|
||||
func (r *Renderer) Size() geom.Point {
|
||||
return geom.Pt(r.disp.Width(), r.disp.Height())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (r *Renderer) SetMouseCursor(c ui.MouseCursor) {
|
||||
|
@ -20,8 +20,8 @@ func (t *texture) Destroy() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *texture) Height() float32 {
|
||||
return float32(t.bmp.Height())
|
||||
func (t *texture) Height() int {
|
||||
return t.bmp.Height()
|
||||
}
|
||||
|
||||
func (t *texture) CreateImage() (image.Image, error) {
|
||||
@ -31,6 +31,6 @@ func (t *texture) CreateImage() (image.Image, error) {
|
||||
return t.source.CreateImage()
|
||||
}
|
||||
|
||||
func (t *texture) Width() float32 {
|
||||
return float32(t.bmp.Width())
|
||||
func (t *texture) Width() int {
|
||||
return t.bmp.Width()
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ func (r *Renderer) CreateFontPath(path string, size int) (ui.Font, error) {
|
||||
return &Font{font}, nil
|
||||
}
|
||||
|
||||
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) {
|
||||
func (r *Renderer) createSurface(source ui.ImageSource) (*sdl.Surface, error) {
|
||||
m, err := source.CreateImage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -252,6 +252,14 @@ func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Tex
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return surface, nil
|
||||
}
|
||||
|
||||
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) {
|
||||
surface, err := r.createSurface(source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer surface.Free()
|
||||
texture, err := r.renderer.CreateTextureFromSurface(surface)
|
||||
if err != nil {
|
||||
@ -391,6 +399,10 @@ func (r *Renderer) RenderToDisplay() {
|
||||
r.renderer.SetRenderTarget(nil)
|
||||
}
|
||||
|
||||
func (r *Renderer) Resize(width, height int) {
|
||||
r.window.SetSize(int32(width), int32(height))
|
||||
}
|
||||
|
||||
func (r *Renderer) SetDrawColor(c sdl.Color) {
|
||||
r.renderer.SetDrawColor(c.R, c.G, c.B, c.A)
|
||||
}
|
||||
@ -399,14 +411,27 @@ func (r *Renderer) SetDrawColorGo(c color.Color) {
|
||||
r.SetDrawColor(ColorSDL(c))
|
||||
}
|
||||
|
||||
func (r *Renderer) SetIcon(source ui.ImageSource) {
|
||||
window := r.window
|
||||
if window == nil {
|
||||
return
|
||||
}
|
||||
surface, err := r.createSurface(source)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer surface.Free()
|
||||
window.SetIcon(surface)
|
||||
}
|
||||
|
||||
func (r *Renderer) SetMouseCursor(c ui.MouseCursor) { r.cursor = c }
|
||||
|
||||
func (r *Renderer) Size() geom.PointF32 {
|
||||
func (r *Renderer) Size() geom.Point {
|
||||
w, h, err := r.renderer.GetOutputSize()
|
||||
if err != nil {
|
||||
return geom.PtF32(geom.NaN32(), geom.NaN32())
|
||||
return geom.ZeroPt
|
||||
}
|
||||
return geom.PtF32(float32(w), float32(h))
|
||||
return geom.Pt(int(w), int(h))
|
||||
}
|
||||
|
||||
func (r *Renderer) SystemCursor(id sdl.SystemCursor) *sdl.Cursor {
|
||||
@ -493,6 +518,6 @@ func (r *Renderer) SetResourceProvider(resources ui.Resources) {
|
||||
|
||||
func (r *Renderer) Image() image.Image { return nil }
|
||||
|
||||
func (r *Renderer) Height() float32 { return r.Size().Y }
|
||||
func (r *Renderer) Height() int { return r.Size().Y }
|
||||
|
||||
func (r *Renderer) Width() float32 { return r.Size().X }
|
||||
func (r *Renderer) Width() int { return r.Size().X }
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"image/color"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"opslag.de/schobers/geom"
|
||||
"opslag.de/schobers/zntg/ui"
|
||||
)
|
||||
|
||||
@ -21,12 +20,12 @@ type Texture struct {
|
||||
|
||||
var _ ui.Texture = &Texture{}
|
||||
|
||||
func (t *Texture) Height() float32 {
|
||||
func (t *Texture) Height() int {
|
||||
_, _, _, height, err := t.Texture.Query()
|
||||
if err != nil {
|
||||
return geom.NaN32()
|
||||
return -1
|
||||
}
|
||||
return float32(height)
|
||||
return int(height)
|
||||
}
|
||||
|
||||
func (t *Texture) Native() *sdl.Texture { return t.Texture }
|
||||
@ -44,12 +43,12 @@ func (t *Texture) Size() (int32, int32, error) {
|
||||
return width, height, err
|
||||
}
|
||||
|
||||
func (t *Texture) Width() float32 {
|
||||
func (t *Texture) Width() int {
|
||||
_, _, width, _, err := t.Texture.Query()
|
||||
if err != nil {
|
||||
return geom.NaN32()
|
||||
return -1
|
||||
}
|
||||
return float32(width)
|
||||
return int(width)
|
||||
}
|
||||
|
||||
var _ ui.ImageSource = &TextureImageSource{}
|
||||
|
@ -71,7 +71,7 @@ func (b *Button) icon(ctx Context) (Texture, float32, float32) {
|
||||
return nil, 0, 0
|
||||
}
|
||||
icon := ctx.Textures().Texture(b.Icon)
|
||||
iconW, iconH := icon.Width(), icon.Height()
|
||||
iconW, iconH := float32(icon.Width()), float32(icon.Height())
|
||||
if b.IconHeight != 0 {
|
||||
iconW = b.IconHeight * iconW / iconH
|
||||
iconH = b.IconHeight
|
||||
@ -213,7 +213,7 @@ func (b *Button) Render(ctx Context) {
|
||||
if scaled != nil { // let the renderer scale
|
||||
icon = scaled
|
||||
}
|
||||
_, iconW = ScaleToHeight(SizeOfTexture(icon), iconH)
|
||||
_, iconW = ScaleToHeight(SizeOfTexture(icon).ToF32(), iconH)
|
||||
}
|
||||
iconOffsetY = .5 * (boundsH - iconH)
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func (c *Checkbox) desiredSize(ctx Context) geom.PointF32 {
|
||||
w += pad + font.WidthOf(c.Text)
|
||||
}
|
||||
icon := c.getOrCreateNormalIcon(ctx)
|
||||
_, iconWidth := ScaleToHeight(SizeOfTexture(icon), h)
|
||||
_, iconWidth := ScaleToHeight(SizeOfTexture(icon).ToF32(), h)
|
||||
w += pad + iconWidth
|
||||
return geom.PtF32(w+pad, pad+h+pad)
|
||||
}
|
||||
@ -122,7 +122,7 @@ func (c *Checkbox) Render(ctx Context) {
|
||||
if scaledIcon == nil { // let the renderer scale
|
||||
scaledIcon = icon
|
||||
}
|
||||
_, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon), boundsH)
|
||||
_, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon).ToF32(), boundsH)
|
||||
rect := geom.RectRelF32(pos.X, pos.Y, iconWidth, boundsH)
|
||||
ctx.Renderer().DrawTextureOptions(scaledIcon, rect, DrawOptions{Tint: iconColor})
|
||||
pos.X += iconWidth + pad
|
||||
|
10
ui/debug.go
10
ui/debug.go
@ -91,16 +91,18 @@ func (o *debugOverlay) renderControl(ctx Context, control Control) {
|
||||
return
|
||||
}
|
||||
defer nameTexture.Destroy()
|
||||
renderer.FillRectangle(pos.RectRel2D(nameTexture.Width(), nameTexture.Height()), color.Black)
|
||||
nameTextureWidth := float32(nameTexture.Width())
|
||||
nameTextureHeight := float32(nameTexture.Height())
|
||||
renderer.FillRectangle(pos.RectRel2D(nameTextureWidth, nameTextureHeight), color.Black)
|
||||
renderer.DrawTexturePoint(nameTexture, pos)
|
||||
childPos := pos.Add2D(nameTexture.Width()+8, 0)
|
||||
childPos := pos.Add2D(nameTextureWidth+8, 0)
|
||||
for _, child := range node.Children {
|
||||
if childPos.Y == maxY {
|
||||
childPos.Y = maxY + nameTexture.Height()
|
||||
childPos.Y = maxY + nameTextureHeight
|
||||
}
|
||||
renderHoverNode(childPos, child)
|
||||
maxY = childPos.Y
|
||||
childPos.Y += nameTexture.Height() + 8
|
||||
childPos.Y += nameTextureHeight + 8
|
||||
}
|
||||
}
|
||||
renderHoverNode(geom.PtF32(4, 4), o.hoverNodes)
|
||||
|
@ -32,8 +32,10 @@ type Renderer interface {
|
||||
Rectangle(r geom.RectangleF32, c color.Color, thickness float32)
|
||||
RenderTo(Texture)
|
||||
RenderToDisplay()
|
||||
Resize(width, height int)
|
||||
SetIcon(source ImageSource)
|
||||
SetMouseCursor(c MouseCursor)
|
||||
Size() geom.PointF32
|
||||
Size() geom.Point
|
||||
Target() Texture
|
||||
Text(font Font, p geom.PointF32, color color.Color, text string)
|
||||
TextAlign(font Font, p geom.PointF32, color color.Color, text string, align HorizontalAlignment)
|
||||
|
@ -4,8 +4,8 @@ import "opslag.de/schobers/geom"
|
||||
|
||||
type Texture interface {
|
||||
Destroy() error
|
||||
Height() float32
|
||||
Width() float32
|
||||
Height() int
|
||||
Width() int
|
||||
}
|
||||
|
||||
func SizeOfTexture(t Texture) geom.PointF32 { return geom.PtF32(t.Width(), t.Height()) }
|
||||
func SizeOfTexture(t Texture) geom.Point { return geom.Pt(t.Width(), t.Height()) }
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func ScaleTexture(render Renderer, texture Texture, scale float32) Texture {
|
||||
w := uint(texture.Width() * scale)
|
||||
w := uint(float32(texture.Width()) * scale)
|
||||
if w == 0 {
|
||||
return nil
|
||||
}
|
||||
@ -118,7 +118,7 @@ func (t *Textures) Scaled(texture Texture, scale float32) Texture {
|
||||
}
|
||||
|
||||
func (t *Textures) ScaledHeight(texture Texture, height float32) (Texture, float32) {
|
||||
scale := height / texture.Height()
|
||||
scale := height / float32(texture.Height())
|
||||
if geom.IsNaN32(scale) {
|
||||
return nil, 0
|
||||
}
|
||||
|
2
ui/ui.go
2
ui/ui.go
@ -46,7 +46,7 @@ func RunWait(r Renderer, s *Style, view Control, wait bool) error {
|
||||
ctx.Renderer().Refresh()
|
||||
for !ctx.HasQuit() {
|
||||
var size = r.Size()
|
||||
var bounds = geom.RectF32(0, 0, size.X, size.Y)
|
||||
var bounds = geom.RectF32(0, 0, float32(size.X), float32(size.Y))
|
||||
overlays.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
|
||||
overlays.Render(ctx)
|
||||
if ctx.HasQuit() {
|
||||
|
Loading…
Reference in New Issue
Block a user