world refactor
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package world
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"sync"
|
||||
)
|
||||
@@ -29,6 +30,10 @@ var (
|
||||
transparentColor color.Color = &color.RGBA{A: 0}
|
||||
)
|
||||
|
||||
// TransparentFill returns a reusable fully transparent color value.
|
||||
//
|
||||
// It is intended for callers that want to explicitly disable fill while still
|
||||
// setting a non-nil FillColor override.
|
||||
func TransparentFill() color.Color { return transparentColor }
|
||||
|
||||
// Style is a fully resolved style used by the renderer.
|
||||
@@ -232,3 +237,461 @@ func (t *StyleTable) Count() int {
|
||||
defer t.mu.RUnlock()
|
||||
return len(t.styles)
|
||||
}
|
||||
|
||||
// BackgroundTileMode defines how the background image is tiled.
|
||||
type BackgroundTileMode uint8
|
||||
|
||||
const (
|
||||
BackgroundTileNone BackgroundTileMode = iota
|
||||
BackgroundTileRepeat
|
||||
)
|
||||
|
||||
// BackgroundAnchorMode defines whether the background image scrolls with the world or stays fixed to viewport.
|
||||
type BackgroundAnchorMode uint8
|
||||
|
||||
const (
|
||||
BackgroundAnchorWorld BackgroundAnchorMode = iota
|
||||
BackgroundAnchorViewport
|
||||
)
|
||||
|
||||
// BackgroundScaleMode defines how the background image is scaled.
|
||||
// (Step 1: defined for API completeness; used later when rendering background image.)
|
||||
type BackgroundScaleMode uint8
|
||||
|
||||
const (
|
||||
BackgroundScaleNone BackgroundScaleMode = iota
|
||||
BackgroundScaleFit
|
||||
BackgroundScaleFill
|
||||
)
|
||||
|
||||
// StyleTheme describes a cohesive style set (theme) for rendering.
|
||||
// Step 1: we store it in World and use it for background and default base styles.
|
||||
// Step 2+: theme-relative overrides and background image drawing.
|
||||
type StyleTheme interface {
|
||||
ID() string
|
||||
Name() string
|
||||
|
||||
BackgroundColor() color.Color
|
||||
BackgroundImage() image.Image
|
||||
|
||||
BackgroundTileMode() BackgroundTileMode
|
||||
BackgroundScaleMode() BackgroundScaleMode
|
||||
BackgroundAnchorMode() BackgroundAnchorMode
|
||||
|
||||
PointStyle() Style
|
||||
LineStyle() Style
|
||||
CircleStyle() Style
|
||||
|
||||
// Class overrides (relative to base kind style).
|
||||
// Return (override, true) when class is supported; (zero, false) means "no override".
|
||||
PointClassOverride(class PointClassID) (StyleOverride, bool)
|
||||
LineClassOverride(class LineClassID) (StyleOverride, bool)
|
||||
CircleClassOverride(class CircleClassID) (StyleOverride, bool)
|
||||
}
|
||||
|
||||
// DefaultTheme is a conservative theme matching built-in default styles.
|
||||
type DefaultTheme struct{}
|
||||
|
||||
func (DefaultTheme) ID() string { return "default" }
|
||||
func (DefaultTheme) Name() string { return "Default" }
|
||||
|
||||
func (DefaultTheme) BackgroundColor() color.Color { return color.RGBA{A: 255} }
|
||||
func (DefaultTheme) BackgroundImage() image.Image { return nil }
|
||||
|
||||
func (DefaultTheme) BackgroundTileMode() BackgroundTileMode { return BackgroundTileNone }
|
||||
func (DefaultTheme) BackgroundScaleMode() BackgroundScaleMode { return BackgroundScaleNone }
|
||||
func (DefaultTheme) BackgroundAnchorMode() BackgroundAnchorMode { return BackgroundAnchorWorld }
|
||||
|
||||
func (DefaultTheme) PointStyle() Style {
|
||||
s, _ := NewStyleTable().Get(StyleIDDefaultPoint)
|
||||
return s
|
||||
}
|
||||
func (DefaultTheme) LineStyle() Style {
|
||||
s, _ := NewStyleTable().Get(StyleIDDefaultLine)
|
||||
return s
|
||||
}
|
||||
func (DefaultTheme) CircleStyle() Style {
|
||||
s, _ := NewStyleTable().Get(StyleIDDefaultCircle)
|
||||
return s
|
||||
}
|
||||
|
||||
func (DefaultTheme) PointClassOverride(PointClassID) (StyleOverride, bool) {
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
func (DefaultTheme) LineClassOverride(LineClassID) (StyleOverride, bool) {
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
func (DefaultTheme) CircleClassOverride(CircleClassID) (StyleOverride, bool) {
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
|
||||
// This file provides two sample themes for demos and UI integration:
|
||||
// LightTheme uses only background color, while DarkTheme also carries a
|
||||
// prebuilt tiled texture image.
|
||||
|
||||
var (
|
||||
// ThemeLight is the shared light theme instance used by the client package.
|
||||
ThemeLight = &LightTheme{}
|
||||
// ThemeDark is the shared dark theme instance used by the client package.
|
||||
ThemeDark = NewDarkTheme()
|
||||
)
|
||||
|
||||
// -----------------------------
|
||||
// Helpers
|
||||
// -----------------------------
|
||||
|
||||
// cRGBA constructs an sRGB color from 8-bit RGBA channels.
|
||||
func cRGBA(r, g, b, a uint8) color.Color { return color.RGBA{R: r, G: g, B: b, A: a} }
|
||||
|
||||
// -----------------------------
|
||||
// Light Theme (color only)
|
||||
// -----------------------------
|
||||
|
||||
// LightTheme is a soft high-contrast theme intended for bright backgrounds.
|
||||
type LightTheme struct{}
|
||||
|
||||
func (LightTheme) ID() string { return "theme.light.v1" }
|
||||
func (LightTheme) Name() string { return "Light (Soft)" }
|
||||
|
||||
func (LightTheme) BackgroundColor() color.Color { return cRGBA(244, 246, 248, 255) } // #F4F6F8
|
||||
func (LightTheme) BackgroundImage() image.Image { return nil }
|
||||
|
||||
func (LightTheme) BackgroundTileMode() BackgroundTileMode { return BackgroundTileNone }
|
||||
func (LightTheme) BackgroundScaleMode() BackgroundScaleMode { return BackgroundScaleNone }
|
||||
func (LightTheme) BackgroundAnchorMode() BackgroundAnchorMode { return BackgroundAnchorWorld }
|
||||
|
||||
// Base styles per primitive kind (full Style, not override).
|
||||
func (LightTheme) PointStyle() Style {
|
||||
return Style{
|
||||
FillColor: cRGBA(32, 161, 145, 255), // soft teal
|
||||
StrokeColor: nil,
|
||||
StrokeWidthPx: 0,
|
||||
PointRadiusPx: 3.0,
|
||||
}
|
||||
}
|
||||
|
||||
func (LightTheme) LineStyle() Style {
|
||||
return Style{
|
||||
FillColor: nil,
|
||||
StrokeColor: cRGBA(70, 108, 196, 220), // soft blue
|
||||
StrokeWidthPx: 2.0,
|
||||
StrokeDashes: nil,
|
||||
StrokeDashOffset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (LightTheme) CircleStyle() Style {
|
||||
return Style{
|
||||
FillColor: cRGBA(133, 110, 201, 60), // soft purple with low alpha
|
||||
StrokeColor: cRGBA(133, 110, 201, 200), // soft purple
|
||||
StrokeWidthPx: 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
// Point class overrides.
|
||||
func (LightTheme) PointClassOverride(class PointClassID) (StyleOverride, bool) {
|
||||
switch class {
|
||||
case PointClassDefault:
|
||||
return StyleOverride{}, false
|
||||
|
||||
case PointClassTrackUnknown:
|
||||
// muted gray-blue
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(120, 135, 160, 230),
|
||||
PointRadiusPx: new(3.0),
|
||||
}, true
|
||||
|
||||
case PointClassTrackIncoming:
|
||||
// soft green
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(76, 171, 107, 240),
|
||||
PointRadiusPx: new(3.5),
|
||||
}, true
|
||||
|
||||
case PointClassTrackOutgoing:
|
||||
// soft orange
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(222, 142, 70, 240),
|
||||
PointRadiusPx: new(3.5),
|
||||
}, true
|
||||
|
||||
default:
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (LightTheme) LineClassOverride(class LineClassID) (StyleOverride, bool) {
|
||||
switch class {
|
||||
case LineClassDefault:
|
||||
return StyleOverride{}, false
|
||||
|
||||
case LineClassTrackIncoming:
|
||||
return StyleOverride{
|
||||
StrokeColor: cRGBA(76, 171, 107, 220),
|
||||
StrokeWidthPx: new(2.5),
|
||||
}, true
|
||||
|
||||
case LineCLassTrackOutgoing:
|
||||
return StyleOverride{
|
||||
StrokeColor: cRGBA(222, 142, 70, 220),
|
||||
StrokeWidthPx: new(2.5),
|
||||
}, true
|
||||
|
||||
case LineClassMeasurement:
|
||||
// dashed neutral line
|
||||
d := []float64{6, 4}
|
||||
return StyleOverride{
|
||||
StrokeColor: cRGBA(100, 110, 125, 200),
|
||||
StrokeWidthPx: new(1.8),
|
||||
StrokeDashes: &d,
|
||||
StrokeDashOffset: new(0.),
|
||||
}, true
|
||||
|
||||
default:
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (LightTheme) CircleClassOverride(class CircleClassID) (StyleOverride, bool) {
|
||||
switch class {
|
||||
case CircleClassDefault:
|
||||
return StyleOverride{}, false
|
||||
|
||||
case CircleClassHome:
|
||||
// teal-ish, a bit stronger stroke
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(32, 161, 145, 50),
|
||||
StrokeColor: cRGBA(32, 161, 145, 210),
|
||||
StrokeWidthPx: new(2.5),
|
||||
}, true
|
||||
|
||||
case CircleClassAcquired:
|
||||
// blue
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(70, 108, 196, 45),
|
||||
StrokeColor: cRGBA(70, 108, 196, 220),
|
||||
StrokeWidthPx: new(2.2),
|
||||
}, true
|
||||
|
||||
case CircleClassOccupied:
|
||||
// orange
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(222, 142, 70, 50),
|
||||
StrokeColor: cRGBA(222, 142, 70, 220),
|
||||
StrokeWidthPx: new(2.2),
|
||||
}, true
|
||||
|
||||
case CircleClassFree:
|
||||
// green
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(76, 171, 107, 45),
|
||||
StrokeColor: cRGBA(76, 171, 107, 220),
|
||||
StrokeWidthPx: new(2.2),
|
||||
}, true
|
||||
|
||||
default:
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Dark Theme (color + tiled image)
|
||||
// -----------------------------
|
||||
|
||||
// DarkTheme is a dark theme with an optional reusable background tile.
|
||||
type DarkTheme struct {
|
||||
bg image.Image
|
||||
}
|
||||
|
||||
// NewDarkTheme constructs a DarkTheme with its immutable texture tile prepared.
|
||||
func NewDarkTheme() *DarkTheme {
|
||||
return &DarkTheme{bg: makeDarkBackgroundTile(96, 96)}
|
||||
}
|
||||
|
||||
func (*DarkTheme) ID() string { return "theme.dark.v1" }
|
||||
func (*DarkTheme) Name() string { return "Dark (Soft + Texture)" }
|
||||
|
||||
func (*DarkTheme) BackgroundColor() color.Color { return cRGBA(30, 32, 38, 255) } // #1E2026
|
||||
func (t *DarkTheme) BackgroundImage() image.Image {
|
||||
return nil
|
||||
// This image is immutable after creation.
|
||||
// return t.bg
|
||||
}
|
||||
|
||||
func (*DarkTheme) BackgroundTileMode() BackgroundTileMode { return BackgroundTileRepeat }
|
||||
func (*DarkTheme) BackgroundScaleMode() BackgroundScaleMode { return BackgroundScaleNone }
|
||||
func (*DarkTheme) BackgroundAnchorMode() BackgroundAnchorMode { return BackgroundAnchorViewport }
|
||||
|
||||
// Base styles for dark theme.
|
||||
func (*DarkTheme) PointStyle() Style {
|
||||
return Style{
|
||||
FillColor: cRGBA(120, 214, 198, 255), // brighter teal for dark bg
|
||||
StrokeColor: nil,
|
||||
StrokeWidthPx: 0,
|
||||
PointRadiusPx: 3.0,
|
||||
}
|
||||
}
|
||||
|
||||
func (*DarkTheme) LineStyle() Style {
|
||||
return Style{
|
||||
FillColor: nil,
|
||||
StrokeColor: cRGBA(155, 175, 235, 220), // soft bluish
|
||||
StrokeWidthPx: 2.0,
|
||||
StrokeDashes: nil,
|
||||
StrokeDashOffset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (*DarkTheme) CircleStyle() Style {
|
||||
return Style{
|
||||
FillColor: cRGBA(186, 160, 255, 55), // soft lavender, low alpha
|
||||
StrokeColor: cRGBA(186, 160, 255, 200), // soft lavender
|
||||
StrokeWidthPx: 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
// Point class overrides.
|
||||
func (*DarkTheme) PointClassOverride(class PointClassID) (StyleOverride, bool) {
|
||||
switch class {
|
||||
case PointClassDefault:
|
||||
return StyleOverride{}, false
|
||||
|
||||
case PointClassTrackUnknown:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(150, 160, 175, 230),
|
||||
PointRadiusPx: new(3.0),
|
||||
}, true
|
||||
|
||||
case PointClassTrackIncoming:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(132, 219, 162, 245),
|
||||
PointRadiusPx: new(3.5),
|
||||
}, true
|
||||
|
||||
case PointClassTrackOutgoing:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(245, 178, 120, 245),
|
||||
PointRadiusPx: new(3.5),
|
||||
}, true
|
||||
|
||||
default:
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (*DarkTheme) LineClassOverride(class LineClassID) (StyleOverride, bool) {
|
||||
switch class {
|
||||
case LineClassDefault:
|
||||
return StyleOverride{}, false
|
||||
|
||||
case LineClassTrackIncoming:
|
||||
return StyleOverride{
|
||||
StrokeColor: cRGBA(132, 219, 162, 220),
|
||||
StrokeWidthPx: new(2.5),
|
||||
}, true
|
||||
|
||||
case LineCLassTrackOutgoing:
|
||||
return StyleOverride{
|
||||
StrokeColor: cRGBA(245, 178, 120, 220),
|
||||
StrokeWidthPx: new(2.5),
|
||||
}, true
|
||||
|
||||
case LineClassMeasurement:
|
||||
d := []float64{6, 4}
|
||||
return StyleOverride{
|
||||
StrokeColor: cRGBA(170, 175, 190, 200),
|
||||
StrokeWidthPx: new(1.8),
|
||||
StrokeDashes: &d,
|
||||
StrokeDashOffset: new(0.),
|
||||
}, true
|
||||
|
||||
default:
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (*DarkTheme) CircleClassOverride(class CircleClassID) (StyleOverride, bool) {
|
||||
switch class {
|
||||
case CircleClassDefault:
|
||||
return StyleOverride{}, false
|
||||
|
||||
case CircleClassHome:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(120, 214, 198, 50),
|
||||
StrokeColor: cRGBA(120, 214, 198, 210),
|
||||
StrokeWidthPx: new(2.5),
|
||||
}, true
|
||||
|
||||
case CircleClassAcquired:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(155, 175, 235, 45),
|
||||
StrokeColor: cRGBA(155, 175, 235, 220),
|
||||
StrokeWidthPx: new(2.2),
|
||||
}, true
|
||||
|
||||
case CircleClassOccupied:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(245, 178, 120, 45),
|
||||
StrokeColor: cRGBA(245, 178, 120, 220),
|
||||
StrokeWidthPx: new(2.2),
|
||||
}, true
|
||||
|
||||
case CircleClassFree:
|
||||
return StyleOverride{
|
||||
FillColor: cRGBA(132, 219, 162, 45),
|
||||
StrokeColor: cRGBA(132, 219, 162, 220),
|
||||
StrokeWidthPx: new(2.2),
|
||||
}, true
|
||||
|
||||
default:
|
||||
return StyleOverride{}, false
|
||||
}
|
||||
}
|
||||
|
||||
// makeDarkBackgroundTile creates a subtle, low-contrast texture tile.
|
||||
// It is intentionally simple: a faint grid + a few diagonal accents.
|
||||
// The tile is meant to be repeated.
|
||||
func makeDarkBackgroundTile(w, h int) image.Image {
|
||||
if w <= 0 || h <= 0 {
|
||||
return nil
|
||||
}
|
||||
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
|
||||
// Base is transparent; background color is drawn separately.
|
||||
// We draw subtle strokes with low alpha.
|
||||
grid := color.RGBA{R: 255, G: 255, B: 255, A: 12} // very faint
|
||||
diag := color.RGBA{R: 255, G: 255, B: 255, A: 18} // slightly stronger
|
||||
dots := color.RGBA{R: 255, G: 255, B: 255, A: 10} // faint dots
|
||||
|
||||
// Grid spacing (pixels).
|
||||
const step = 12
|
||||
|
||||
// Vertical grid lines.
|
||||
for x := 0; x < w; x += step {
|
||||
for y := 0; y < h; y++ {
|
||||
img.SetRGBA(x, y, grid)
|
||||
}
|
||||
}
|
||||
// Horizontal grid lines.
|
||||
for y := 0; y < h; y += step {
|
||||
for x := 0; x < w; x++ {
|
||||
img.SetRGBA(x, y, grid)
|
||||
}
|
||||
}
|
||||
|
||||
// Diagonal accents (sparse).
|
||||
for x := 0; x < w; x += step * 2 {
|
||||
for i := 0; i < step && x+i < w && i < h; i++ {
|
||||
img.SetRGBA(x+i, i, diag)
|
||||
}
|
||||
}
|
||||
|
||||
// Small dot pattern.
|
||||
for y := step / 2; y < h; y += step {
|
||||
for x := step / 2; x < w; x += step {
|
||||
img.SetRGBA(x, y, dots)
|
||||
}
|
||||
}
|
||||
|
||||
return img
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user