Compare commits
No commits in common. "f3ce48a1bad6854805ebc6585b6cbd0ffec6a25f" and "de87c5d3aa9d5d416ee7c96fb86f0cda9736e139" have entirely different histories.
f3ce48a1ba
...
de87c5d3aa
@ -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) (*texture, error) {
|
||||
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) {
|
||||
im, err := source.CreateImage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -314,23 +314,15 @@ func (r *Renderer) RenderToDisplay() {
|
||||
r.disp.SetAsTarget()
|
||||
}
|
||||
|
||||
func (r *Renderer) Resize(width, height int) {
|
||||
r.disp.Resize(width, height)
|
||||
}
|
||||
|
||||
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) Size() geom.PointF32 {
|
||||
return geom.PtF32(float32(r.disp.Width()), float32(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) SetIcon(texture ui.Texture) {
|
||||
bmp := r.mustGetBitmap(texture)
|
||||
r.disp.SetIcon(bmp)
|
||||
}
|
||||
|
||||
func (r *Renderer) SetMouseCursor(c ui.MouseCursor) {
|
||||
|
@ -238,7 +238,7 @@ func (r *Renderer) CreateFontPath(path string, size int) (ui.Font, error) {
|
||||
return &Font{font}, nil
|
||||
}
|
||||
|
||||
func (r *Renderer) createSurface(source ui.ImageSource) (*sdl.Surface, error) {
|
||||
func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) {
|
||||
m, err := source.CreateImage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -252,14 +252,6 @@ func (r *Renderer) createSurface(source ui.ImageSource) (*sdl.Surface, error) {
|
||||
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 {
|
||||
@ -399,10 +391,6 @@ 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)
|
||||
}
|
||||
@ -411,27 +399,14 @@ 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.Point {
|
||||
func (r *Renderer) Size() geom.PointF32 {
|
||||
w, h, err := r.renderer.GetOutputSize()
|
||||
if err != nil {
|
||||
return geom.ZeroPt
|
||||
return geom.PtF32(geom.NaN32(), geom.NaN32())
|
||||
}
|
||||
return geom.Pt(int(w), int(h))
|
||||
return geom.PtF32(float32(w), float32(h))
|
||||
}
|
||||
|
||||
func (r *Renderer) SystemCursor(id sdl.SystemCursor) *sdl.Cursor {
|
||||
|
23
ui/button.go
23
ui/button.go
@ -41,7 +41,7 @@ func BuildIconButton(icon, text string, fn func(b *Button)) *Button {
|
||||
|
||||
func (b *Button) desiredSize(ctx Context) geom.PointF32 {
|
||||
var pad = ctx.Style().Dimensions.TextPadding
|
||||
var font = b.Font_(ctx)
|
||||
var font = ctx.Fonts().Font(b.FontName(ctx))
|
||||
var w, h float32 = 0, font.Height()
|
||||
|
||||
icon, iconW, iconH := b.icon(ctx)
|
||||
@ -155,13 +155,6 @@ func (b *Button) fillColor(p *Palette) color.Color {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Button) fontColor(c color.Color) color.Color {
|
||||
if b.Font.Color == nil {
|
||||
return c
|
||||
}
|
||||
return b.Font.Color
|
||||
}
|
||||
|
||||
func (b *Button) textColor(p *Palette) color.Color {
|
||||
if b.Disabled {
|
||||
if b.Background != nil {
|
||||
@ -173,19 +166,22 @@ func (b *Button) textColor(p *Palette) color.Color {
|
||||
}
|
||||
return b.disabledColor(p)
|
||||
}
|
||||
if b.Font.Color != nil {
|
||||
return b.Font.Color
|
||||
}
|
||||
switch b.Type {
|
||||
case ButtonTypeContained:
|
||||
return b.fontColor(p.TextOnPrimary)
|
||||
return p.TextOnPrimary
|
||||
case ButtonTypeIcon:
|
||||
if b.over {
|
||||
if b.HoverColor != nil {
|
||||
return b.HoverColor
|
||||
}
|
||||
return b.fontColor(p.Primary)
|
||||
return p.Primary
|
||||
}
|
||||
return b.fontColor(p.Text)
|
||||
return p.Text
|
||||
default:
|
||||
return b.fontColor(p.Primary)
|
||||
return p.Primary
|
||||
}
|
||||
}
|
||||
|
||||
@ -225,7 +221,8 @@ func (b *Button) Render(ctx Context) {
|
||||
pos.X += iconW + pad
|
||||
}
|
||||
if len(b.Text) != 0 {
|
||||
font := b.Font_(ctx)
|
||||
fontName := b.FontName(ctx)
|
||||
font := ctx.Fonts().Font(fontName)
|
||||
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), textColor, b.Text)
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ func BuildCheckbox(text string, fn func(c *Checkbox)) *Checkbox {
|
||||
|
||||
func (c *Checkbox) desiredSize(ctx Context) geom.PointF32 {
|
||||
var pad = ctx.Style().Dimensions.TextPadding
|
||||
font := c.Font_(ctx)
|
||||
var font = ctx.Fonts().Font(c.FontName(ctx))
|
||||
var w, h float32 = 0, font.Height()
|
||||
if len(c.Text) != 0 {
|
||||
w += pad + font.WidthOf(c.Text)
|
||||
@ -128,7 +128,8 @@ func (c *Checkbox) Render(ctx Context) {
|
||||
pos.X += iconWidth + pad
|
||||
}
|
||||
if len(c.Text) != 0 {
|
||||
font := c.Font_(ctx)
|
||||
var fontName = c.FontName(ctx)
|
||||
var font = ctx.Fonts().Font(fontName)
|
||||
ctx.Renderer().Text(font, geom.PtF32(pos.X, pos.Y+.5*(boundsH-font.Height())), fore, c.Text)
|
||||
}
|
||||
}
|
||||
|
@ -26,15 +26,6 @@ func (c *ContainerBase) Arrange(ctx Context, bounds geom.RectangleF32, offset ge
|
||||
c.ControlBase.Arrange(ctx, bounds, offset, parent)
|
||||
}
|
||||
|
||||
func (c *ContainerBase) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
|
||||
var max geom.PointF32
|
||||
for _, child := range c.Children {
|
||||
s := child.DesiredSize(ctx, size)
|
||||
max = geom.MaxPtF32(max, s)
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
func (c *ContainerBase) Handle(ctx Context, e Event) bool {
|
||||
if c.ControlBase.Handle(ctx, e) {
|
||||
return true
|
||||
|
@ -188,11 +188,6 @@ func (c *ControlBase) HandleNotify(ctx Context, e Event, notifier Notifier) bool
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *ControlBase) Font_(ctx Context) Font {
|
||||
name := c.FontName(ctx)
|
||||
return ctx.Fonts().Font(name)
|
||||
}
|
||||
|
||||
func (c *ControlBase) FontColor(ctx Context, color color.Color) color.Color {
|
||||
if c.Disabled {
|
||||
return ctx.Style().Palette.TextOnDisabled
|
||||
|
@ -3,7 +3,6 @@ package ui
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
"opslag.de/schobers/zntg"
|
||||
)
|
||||
|
||||
@ -46,25 +45,3 @@ func (s ImageSourceResource) CreateImage() (image.Image, error) {
|
||||
}
|
||||
return value.(image.Image), nil
|
||||
}
|
||||
|
||||
func ImageSourceFromResources(res Resources, name string) ImageSourceResource {
|
||||
return ImageSourceResource{Resources: res, Name: name}
|
||||
}
|
||||
|
||||
func Scaled(source ImageSource, width, height int) ImageSource {
|
||||
return scaledImageSource{ImageSource: source, width: width, height: height}
|
||||
}
|
||||
|
||||
type scaledImageSource struct {
|
||||
ImageSource
|
||||
|
||||
width, height int
|
||||
}
|
||||
|
||||
func (s scaledImageSource) CreateImage() (image.Image, error) {
|
||||
im, err := s.ImageSource.CreateImage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resize.Resize(uint(s.width), uint(s.height), im, resize.Bicubic), nil
|
||||
}
|
||||
|
32
ui/label.go
32
ui/label.go
@ -1,8 +1,6 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"opslag.de/schobers/geom"
|
||||
)
|
||||
|
||||
@ -10,7 +8,6 @@ type Label struct {
|
||||
ControlBase
|
||||
|
||||
Text string
|
||||
DropShadow color.Color
|
||||
|
||||
desired CachedValue
|
||||
}
|
||||
@ -28,7 +25,8 @@ func (l *Label) hashDesiredSize(ctx Context) string {
|
||||
}
|
||||
|
||||
func (l *Label) desiredSize(ctx Context) interface{} {
|
||||
font := l.Font_(ctx)
|
||||
fontName := l.FontName(ctx)
|
||||
font := ctx.Fonts().Font(fontName)
|
||||
width := font.WidthOf(l.Text)
|
||||
height := font.Height()
|
||||
pad := ctx.Style().Dimensions.TextPadding
|
||||
@ -39,26 +37,18 @@ func (l *Label) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
||||
return l.desired.GetContext(ctx, l.desiredSize, l.hashDesiredSize).(geom.PointF32)
|
||||
}
|
||||
|
||||
func (l *Label) getLabelTopLeft(ctx Context) geom.PointF32 {
|
||||
pad := ctx.Style().Dimensions.TextPadding
|
||||
bounds := l.bounds.Inset(pad)
|
||||
switch l.TextAlignment {
|
||||
case AlignRight:
|
||||
return geom.PtF32(bounds.Max.X, bounds.Min.Y)
|
||||
case AlignCenter:
|
||||
return geom.PtF32(.5*(bounds.Min.X+bounds.Max.X), bounds.Min.Y)
|
||||
default:
|
||||
return bounds.Min
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Label) Render(ctx Context) {
|
||||
l.RenderBackground(ctx)
|
||||
fontColor := l.TextColor(ctx)
|
||||
fontName := l.FontName(ctx)
|
||||
topLeft := l.getLabelTopLeft(ctx)
|
||||
if l.DropShadow != nil {
|
||||
ctx.Fonts().TextAlign(fontName, topLeft.Add2D(1, 1), l.DropShadow, l.Text, l.TextAlignment)
|
||||
pad := ctx.Style().Dimensions.TextPadding
|
||||
bounds := l.bounds.Inset(pad)
|
||||
switch l.TextAlignment {
|
||||
case AlignLeft:
|
||||
ctx.Fonts().TextAlign(fontName, bounds.Min, fontColor, l.Text, l.TextAlignment)
|
||||
case AlignRight:
|
||||
ctx.Fonts().TextAlign(fontName, geom.PtF32(bounds.Max.X, bounds.Min.Y), fontColor, l.Text, l.TextAlignment)
|
||||
case AlignCenter:
|
||||
ctx.Fonts().TextAlign(fontName, geom.PtF32(.5*(bounds.Min.X+bounds.Max.X), bounds.Min.Y), fontColor, l.Text, l.TextAlignment)
|
||||
}
|
||||
ctx.Fonts().TextAlign(fontName, topLeft, fontColor, l.Text, l.TextAlignment)
|
||||
}
|
||||
|
@ -23,17 +23,11 @@ type overflow struct {
|
||||
ver *Scrollbar
|
||||
}
|
||||
|
||||
type ScrollControl interface {
|
||||
Control
|
||||
|
||||
SetScrollbarColor(bar color.Color, hover color.Color)
|
||||
}
|
||||
|
||||
func Overflow(content Control) ScrollControl {
|
||||
func Overflow(content Control) Control {
|
||||
return OverflowBackground(content, nil)
|
||||
}
|
||||
|
||||
func OverflowBackground(content Control, back color.Color) ScrollControl {
|
||||
func OverflowBackground(content Control, back color.Color) Control {
|
||||
var o = &overflow{Proxy: Proxy{Content: content}, Background: back}
|
||||
o.hor = BuildScrollbar(OrientationHorizontal, func(*Scrollbar) {})
|
||||
o.ver = BuildScrollbar(OrientationVertical, func(*Scrollbar) {})
|
||||
@ -158,10 +152,3 @@ func (o *overflow) Render(ctx Context) {
|
||||
bar.Render(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
func (o *overflow) SetScrollbarColor(bar color.Color, hover color.Color) {
|
||||
o.hor.BarColor = bar
|
||||
o.hor.BarHoverColor = hover
|
||||
o.ver.BarColor = bar
|
||||
o.ver.BarHoverColor = hover
|
||||
}
|
||||
|
@ -26,7 +26,8 @@ func BuildParagraph(text string, fn func(*Paragraph)) *Paragraph {
|
||||
func fastFormatFloat32(f float32) string { return strconv.FormatFloat(float64(f), 'b', 32, 32) }
|
||||
|
||||
func (p *Paragraph) desiredSize(ctx Context) interface{} {
|
||||
font := p.Font_(ctx)
|
||||
fontName := p.FontName(ctx)
|
||||
font := ctx.Fonts().Font(fontName)
|
||||
|
||||
pad := ctx.Style().Dimensions.TextPadding
|
||||
lines := p.splitInLines(ctx, p.width-2*pad)
|
||||
@ -42,7 +43,8 @@ func (p *Paragraph) hashTextDesired(ctx Context) string {
|
||||
}
|
||||
|
||||
func (p *Paragraph) splitInLines(ctx Context, width float32) []string {
|
||||
font := p.Font_(ctx)
|
||||
fontName := p.FontName(ctx)
|
||||
font := ctx.Fonts().Font(fontName)
|
||||
|
||||
spaces := func(s string) []int { // creates a slice with indices where spaces can be found in string s
|
||||
var spaces []int
|
||||
|
@ -32,10 +32,8 @@ 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.Point
|
||||
Size() geom.PointF32
|
||||
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)
|
||||
|
@ -1,8 +1,6 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"opslag.de/schobers/geom"
|
||||
"opslag.de/schobers/zntg"
|
||||
)
|
||||
@ -12,8 +10,6 @@ type Scrollbar struct {
|
||||
|
||||
Orientation Orientation
|
||||
|
||||
BarColor color.Color
|
||||
BarHoverColor color.Color
|
||||
ContentLength float32
|
||||
ContentOffset float32
|
||||
|
||||
@ -62,7 +58,7 @@ func (s *Scrollbar) Handle(ctx Context, e Event) bool {
|
||||
|
||||
func (s *Scrollbar) Render(ctx Context) {
|
||||
ctx.Renderer().FillRectangle(s.bounds, zntg.RGBA(0, 0, 0, 1))
|
||||
s.handle.Render(ctx, s)
|
||||
s.handle.Render(ctx)
|
||||
}
|
||||
|
||||
func (s *Scrollbar) updateBar(ctx Context) {
|
||||
@ -87,22 +83,12 @@ type ScrollbarHandle struct {
|
||||
ControlBase
|
||||
}
|
||||
|
||||
func (h *ScrollbarHandle) Render(ctx Context, s *Scrollbar) {
|
||||
func (h *ScrollbarHandle) Render(ctx Context) {
|
||||
h.RenderBackground(ctx)
|
||||
p := ctx.Style().Palette
|
||||
var fill color.Color
|
||||
fill := p.Primary
|
||||
if h.over {
|
||||
if s.BarHoverColor == nil {
|
||||
fill = p.PrimaryLight
|
||||
} else {
|
||||
fill = s.BarHoverColor
|
||||
}
|
||||
} else {
|
||||
if s.BarColor == nil {
|
||||
fill = p.Primary
|
||||
} else {
|
||||
fill = s.BarColor
|
||||
}
|
||||
}
|
||||
ctx.Renderer().FillRectangle(h.bounds.Inset(1), fill)
|
||||
}
|
||||
|
@ -57,7 +57,8 @@ func (b *TextBox) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.Poi
|
||||
}
|
||||
|
||||
func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
||||
font := b.Font_(ctx)
|
||||
var fontName = b.FontName(ctx)
|
||||
var font = ctx.Fonts().Font(fontName)
|
||||
var width = font.WidthOf(b.Text)
|
||||
var height = font.Height()
|
||||
var pad = b.pad(ctx)
|
||||
@ -67,7 +68,7 @@ func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
||||
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
|
||||
p := b.ToControlPosition(e.Pos())
|
||||
offset := p.X - b.box.bounds.Min.X
|
||||
f := b.Font_(ctx)
|
||||
f := ctx.Fonts().Font(b.FontName(ctx))
|
||||
var carets = [3]int{0, 0, len(b.Text)}
|
||||
var offsets = [3]float32{0, 0, f.WidthOf(b.Text)}
|
||||
var updateCenter = func() {
|
||||
@ -258,7 +259,8 @@ func (b *TextBox) Render(ctx Context) {
|
||||
back = ctx.Style().Palette.Background
|
||||
}
|
||||
renderer.Clear(back)
|
||||
font := b.Font_(ctx)
|
||||
fontName := b.FontName(ctx)
|
||||
font := ctx.Fonts().Font(fontName)
|
||||
if b.Selection.Start != b.Selection.End {
|
||||
left, right := font.WidthOf(b.Text[:b.Selection.Start]), font.WidthOf(b.Text[:b.Selection.End])
|
||||
renderer.FillRectangle(geom.RectF32(left, 0, right, size.Y), style.Palette.PrimaryHighlight)
|
||||
|
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, float32(size.X), float32(size.Y))
|
||||
var bounds = geom.RectF32(0, 0, size.X, size.Y)
|
||||
overlays.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
|
||||
overlays.Render(ctx)
|
||||
if ctx.HasQuit() {
|
||||
|
Loading…
Reference in New Issue
Block a user