themes and styles
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package world
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const (
|
||||
testPointClassExtended PointClassID = 1
|
||||
testCircleClassExtended CircleClassID = 1
|
||||
)
|
||||
|
||||
type classThemeA struct{}
|
||||
|
||||
func (classThemeA) ID() string { return "classA" }
|
||||
func (classThemeA) Name() string { return "classA" }
|
||||
func (classThemeA) BackgroundColor() color.Color { return color.RGBA{A: 255} }
|
||||
func (classThemeA) BackgroundImage() image.Image { return nil }
|
||||
func (classThemeA) BackgroundTileMode() BackgroundTileMode { return BackgroundTileNone }
|
||||
func (classThemeA) BackgroundScaleMode() BackgroundScaleMode { return BackgroundScaleNone }
|
||||
func (classThemeA) BackgroundAnchorMode() BackgroundAnchorMode { return BackgroundAnchorWorld }
|
||||
|
||||
func (classThemeA) PointStyle() Style {
|
||||
return Style{FillColor: color.RGBA{R: 10, A: 255}, PointRadiusPx: 2}
|
||||
}
|
||||
func (classThemeA) LineStyle() Style {
|
||||
return Style{StrokeColor: color.RGBA{G: 10, A: 255}, StrokeWidthPx: 1}
|
||||
}
|
||||
func (classThemeA) CircleStyle() Style {
|
||||
return Style{FillColor: color.RGBA{B: 10, A: 255}, StrokeColor: color.RGBA{A: 255}, StrokeWidthPx: 1}
|
||||
}
|
||||
|
||||
func (classThemeA) PointClassOverride(c PointClassID) (StyleOverride, bool) {
|
||||
if c == testPointClassExtended {
|
||||
r := 6.0
|
||||
return StyleOverride{PointRadiusPx: &r}, true
|
||||
}
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
func (classThemeA) LineClassOverride(LineClassID) (StyleOverride, bool) {
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
func (classThemeA) CircleClassOverride(c CircleClassID) (StyleOverride, bool) {
|
||||
if c == testCircleClassExtended {
|
||||
w := 3.0
|
||||
return StyleOverride{StrokeWidthPx: &w}, true
|
||||
}
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
|
||||
type classThemeB struct{ classThemeA }
|
||||
|
||||
func (classThemeB) ID() string { return "classB" }
|
||||
func (classThemeB) Name() string { return "classB" }
|
||||
func (classThemeB) PointStyle() Style {
|
||||
return Style{FillColor: color.RGBA{R: 99, A: 255}, PointRadiusPx: 3}
|
||||
}
|
||||
func (classThemeB) CircleStyle() Style {
|
||||
return Style{FillColor: color.RGBA{B: 99, A: 255}, StrokeColor: color.RGBA{A: 255}, StrokeWidthPx: 2}
|
||||
}
|
||||
|
||||
func (classThemeB) PointClassOverride(c PointClassID) (StyleOverride, bool) {
|
||||
if c == testPointClassExtended {
|
||||
r := 9.0
|
||||
return StyleOverride{PointRadiusPx: &r}, true
|
||||
}
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
|
||||
func TestThemeClassOverride_AppliesAndUpdatesOnThemeChange(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
w := NewWorld(10, 10)
|
||||
w.SetTheme(classThemeA{})
|
||||
|
||||
id, err := w.AddPoint(1, 1, PointWithClass(testPointClassExtended))
|
||||
require.NoError(t, err)
|
||||
|
||||
p := w.objects[id].(Point)
|
||||
s1, ok := w.styles.Get(p.StyleID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 6.0, s1.PointRadiusPx)
|
||||
|
||||
w.SetTheme(classThemeB{})
|
||||
|
||||
p2 := w.objects[id].(Point)
|
||||
s2, ok := w.styles.Get(p2.StyleID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 9.0, s2.PointRadiusPx)
|
||||
}
|
||||
|
||||
func TestThemeClassOverride_MergesWithUserOverride_UserWins(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
w := NewWorld(10, 10)
|
||||
w.SetTheme(classThemeA{})
|
||||
|
||||
// class would set point radius to 6, but user override sets it to 12.
|
||||
rUser := 12.0
|
||||
id, err := w.AddPoint(1, 1,
|
||||
PointWithClass(testPointClassExtended),
|
||||
PointWithStyleOverride(StyleOverride{PointRadiusPx: &rUser}),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
p := w.objects[id].(Point)
|
||||
s1, ok := w.styles.Get(p.StyleID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 12.0, s1.PointRadiusPx)
|
||||
|
||||
// After theme change, class would set to 9, but user override must still win.
|
||||
w.SetTheme(classThemeB{})
|
||||
p2 := w.objects[id].(Point)
|
||||
s2, ok := w.styles.Get(p2.StyleID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 12.0, s2.PointRadiusPx)
|
||||
}
|
||||
Reference in New Issue
Block a user