From ad7c149baf5008d9b08d73257be29cd8ff6d3fd9 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Thu, 14 May 2020 12:59:46 +0200 Subject: [PATCH] Changed appearance of tooltips a bit. - Bit smaller text, background half transparent. --- control.go | 2 +- tooltip.go | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/control.go b/control.go index f3f98b3..252910c 100644 --- a/control.go +++ b/control.go @@ -45,7 +45,7 @@ func (c *ControlBase) ActualFont(ctx *Context) *Font { } func (c *ControlBase) ActualFontName() string { - if len(c.FontName) == 0 { + if c.FontName == "" { return "default" } return c.FontName diff --git a/tooltip.go b/tooltip.go index 88bea7e..e4a90f9 100644 --- a/tooltip.go +++ b/tooltip.go @@ -9,15 +9,26 @@ type Tooltip struct { } const tooltipBorderThickness = 1 -const tooltipHorizontalPadding = 4 +const tooltipHorizontalPadding = 6 +const tooltipVerticalPadding = 2 const tooltipMouseDistance = 12 +func (t *Tooltip) ActualFontName() string { + if t.FontName == "" { + return "small" + } + return t.FontName +} + func (t *Tooltip) Handle(ctx *Context, event sdl.Event) bool { if len(t.Text) == 0 { return false } font := ctx.Fonts.Font(t.ActualFontName()) + if font == nil { + font = ctx.Fonts.Font("default") + } windowW, windowH, err := ctx.Renderer.GetOutputSize() if err != nil { return false @@ -30,7 +41,7 @@ func (t *Tooltip) Handle(ctx *Context, event sdl.Event) bool { mouse := ctx.MousePosition width := int32(labelW) + 2*tooltipBorderThickness + 2*tooltipHorizontalPadding - height := int32(labelH) + 2*tooltipBorderThickness + height := int32(labelH) + 2*tooltipBorderThickness + 2*tooltipVerticalPadding left := mouse.X + tooltipMouseDistance top := mouse.Y + tooltipMouseDistance @@ -46,14 +57,16 @@ func (t *Tooltip) Handle(ctx *Context, event sdl.Event) bool { } func (t *Tooltip) Render(ctx *Context) { - SetDrawColor(ctx.Renderer, Black) + almostBlack := MustHexColor("#0000007f") + SetDrawColor(ctx.Renderer, almostBlack) ctx.Renderer.FillRect(t.Bounds.SDLPtr()) - SetDrawColor(ctx.Renderer, White) + almostWhite := MustHexColor("ffffff7f") + SetDrawColor(ctx.Renderer, almostWhite) ctx.Renderer.DrawRect(t.Bounds.SDLPtr()) - font := t.ActualFont(ctx) + font := ctx.Fonts.Font(t.ActualFontName()) - bottomLeft := Pt(t.Bounds.X+tooltipBorderThickness+tooltipHorizontalPadding, t.Bounds.Y+t.Bounds.H-tooltipBorderThickness) + bottomLeft := Pt(t.Bounds.X+tooltipBorderThickness+tooltipHorizontalPadding, t.Bounds.Y+t.Bounds.H-tooltipBorderThickness-tooltipVerticalPadding) font.RenderCopy(ctx.Renderer, t.Text, bottomLeft, White) }