Files
galaxy-game/client/world/theme_override_test.go
T
2026-03-08 15:31:17 +02:00

150 lines
4.8 KiB
Go

package world
import (
"image"
"image/color"
"testing"
"github.com/stretchr/testify/require"
)
type themeA struct{}
func (themeA) ID() string { return "A" }
func (themeA) Name() string { return "A" }
func (themeA) BackgroundColor() color.Color { return color.RGBA{A: 255} }
func (themeA) BackgroundImage() image.Image { return nil }
func (themeA) BackgroundTileMode() BackgroundTileMode { return BackgroundTileNone }
func (themeA) BackgroundScaleMode() BackgroundScaleMode { return BackgroundScaleNone }
func (themeA) BackgroundAnchorMode() BackgroundAnchorMode { return BackgroundAnchorWorld }
func (themeA) PointStyle() Style {
return Style{FillColor: color.RGBA{R: 10, A: 255}, PointRadiusPx: 2}
}
func (themeA) LineStyle() Style {
return Style{StrokeColor: color.RGBA{G: 10, A: 255}, StrokeWidthPx: 1}
}
func (themeA) CircleStyle() Style {
return Style{FillColor: color.RGBA{B: 10, A: 255}, StrokeColor: color.RGBA{A: 255}, StrokeWidthPx: 1}
}
func (themeA) PointClassOverride(PointClassID) (StyleOverride, bool) { return StyleOverride{}, false }
func (themeA) LineClassOverride(LineClassID) (StyleOverride, bool) { return StyleOverride{}, false }
func (themeA) CircleClassOverride(CircleClassID) (StyleOverride, bool) { return StyleOverride{}, false }
type themeB struct{}
func (themeB) ID() string { return "B" }
func (themeB) Name() string { return "B" }
func (themeB) BackgroundColor() color.Color { return color.RGBA{A: 255} }
func (themeB) BackgroundImage() image.Image { return nil }
func (themeB) BackgroundTileMode() BackgroundTileMode { return BackgroundTileNone }
func (themeB) BackgroundScaleMode() BackgroundScaleMode { return BackgroundScaleNone }
func (themeB) BackgroundAnchorMode() BackgroundAnchorMode { return BackgroundAnchorWorld }
func (themeB) PointStyle() Style {
return Style{FillColor: color.RGBA{R: 99, A: 255}, PointRadiusPx: 5}
}
func (themeB) LineStyle() Style {
return Style{StrokeColor: color.RGBA{G: 99, A: 255}, StrokeWidthPx: 3}
}
func (themeB) CircleStyle() Style {
return Style{FillColor: color.RGBA{B: 99, A: 255}, StrokeColor: color.RGBA{A: 255}, StrokeWidthPx: 4}
}
func (themeB) PointClassOverride(PointClassID) (StyleOverride, bool) { return StyleOverride{}, false }
func (themeB) LineClassOverride(LineClassID) (StyleOverride, bool) { return StyleOverride{}, false }
func (themeB) CircleClassOverride(CircleClassID) (StyleOverride, bool) { return StyleOverride{}, false }
func TestThemeChange_UpdatesThemeDefaultStyleObjects(t *testing.T) {
t.Parallel()
w := NewWorld(10, 10)
w.SetTheme(themeA{})
id, err := w.AddPoint(1, 1) // default => theme-managed
require.NoError(t, err)
p := w.objects[id].(Point)
styleBefore := p.StyleID
w.SetTheme(themeB{})
p2 := w.objects[id].(Point)
styleAfter := p2.StyleID
require.NotEqual(t, styleBefore, styleAfter)
s, ok := w.styles.Get(styleAfter)
require.True(t, ok)
// From themeB point style
require.Equal(t, 5.0, s.PointRadiusPx)
}
func TestThemeChange_UpdatesThemeRelativeOverride(t *testing.T) {
t.Parallel()
w := NewWorld(10, 10)
w.SetTheme(themeA{})
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
ov := StyleOverride{StrokeColor: white}
id, err := w.AddCircle(5, 5, 2, CircleWithStyleOverride(ov))
require.NoError(t, err)
c1 := w.objects[id].(Circle)
s1, ok := w.styles.Get(c1.StyleID)
require.True(t, ok)
// Stroke overridden to white, fill from themeA (B=10).
require.Equal(t, uint32(0xffff), alphaOf(s1.StrokeColor))
require.Equal(t, u16FromU8(10), blueOf(s1.FillColor))
w.SetTheme(themeB{})
c2 := w.objects[id].(Circle)
s2, ok := w.styles.Get(c2.StyleID)
require.True(t, ok)
// Still white stroke, but fill should now come from themeB (B=99).
require.Equal(t, uint32(0xffff), alphaOf(s2.StrokeColor))
require.Equal(t, u16FromU8(99), blueOf(s2.FillColor))
}
func TestThemeChange_DoesNotAffectFixedStyleID(t *testing.T) {
t.Parallel()
w := NewWorld(10, 10)
w.SetTheme(themeA{})
sw := 2.0
fixed := w.AddStyleCircle(StyleOverride{
FillColor: color.RGBA{A: 0},
StrokeColor: color.RGBA{R: 1, A: 255},
StrokeWidthPx: &sw,
})
id, err := w.AddCircle(5, 5, 2, CircleWithStyleID(fixed))
require.NoError(t, err)
c1 := w.objects[id].(Circle)
require.Equal(t, fixed, c1.StyleID)
w.SetTheme(themeB{})
c2 := w.objects[id].(Circle)
require.Equal(t, fixed, c2.StyleID)
}
func alphaOf(c color.Color) uint32 {
_, _, _, a := c.RGBA()
return a
}
func blueOf(c color.Color) uint32 {
_, _, b, _ := c.RGBA()
return b
}
// u16FromU8 converts an 8-bit channel value to the 16-bit value returned by color.Color.RGBA().
// The standard conversion is v * 257 (0x0101) so that 0xAB becomes 0xABAB.
func u16FromU8(v uint8) uint32 {
return uint32(v) * 257
}