Skip to content

Commit

Permalink
Code refactor: update color lib (goplus#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan authored Dec 6, 2024
1 parent 39b8d3c commit c171baf
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 245 deletions.
2 changes: 1 addition & 1 deletion camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Camera struct {
on_ interface{}
}

func (c *Camera) init(g *Game, winW, winH float64, worldW, worldH float64) {
func (c *Camera) init(g *Game) {
c.g = g
}

Expand Down
2 changes: 1 addition & 1 deletion game.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
}
engine.SyncPlatformSetWindowSize(int64(p.windowWidth_), int64(p.windowHeight_))

p.Camera.init(p, float64(p.windowWidth_), float64(p.windowHeight_), float64(p.worldWidth_), float64(p.worldHeight_))
p.Camera.init(p)

// setup proxy's property
p.proxy = engine.SyncNewBackdropProxy(p, p.getCostumePath(), p.getCostumeRenderScale())
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.21.3
require (
github.com/pkg/errors v0.9.1
github.com/realdream-ai/gdspx v0.0.0-20241204130734-35c372f84f57
github.com/realdream-ai/mathf v0.0.0-20241204125103-284ee6a3722d
golang.org/x/image v0.18.0
github.com/realdream-ai/mathf v0.0.0-20241205091834-a85cb2402a16
golang.org/x/mobile v0.0.0-20220518205345-8578da9835fd
)

require golang.org/x/image v0.23.0 // indirect

replace (
golang.org/x/image => golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
golang.org/x/mobile => golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/realdream-ai/gdspx v0.0.0-20241204130734-35c372f84f57 h1:iF9BObP4FNyjdyxJD8ng0mCf+dJTaVExco+QWChoukc=
github.com/realdream-ai/gdspx v0.0.0-20241204130734-35c372f84f57/go.mod h1:UCzetvoQdU+yfesI6xAL33acsuEbYICGQUcYpGiAW00=
github.com/realdream-ai/mathf v0.0.0-20241204125103-284ee6a3722d h1:hEIUl272UkUUd2F77nONdEDR1tHLpe1MAgaYAGoVnEk=
github.com/realdream-ai/mathf v0.0.0-20241204125103-284ee6a3722d/go.mod h1:oAFczOO99bG05kC9tKp5deLqrvZSjtBDIpNH36ZWwnY=
github.com/realdream-ai/mathf v0.0.0-20241205091834-a85cb2402a16 h1:V/zAuyQF0v6ng+UVwJi+qF0s+P/nEHb/lha3Uf86TQM=
github.com/realdream-ai/mathf v0.0.0-20241205091834-a85cb2402a16/go.mod h1:zCbLmeT7a+pH1sknxgdFLiha6xA3gezTtwbTuuNRJWE=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
105 changes: 0 additions & 105 deletions internal/gdi/color_utils.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/gdi/text.go → internal/text/text.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gdi
package text

import (
"strings"
Expand Down Expand Up @@ -29,7 +29,7 @@ func calculateWordLength(word string) int {
}

// splitString splits the input string into lines with a maximum placeholder width n
func SplitString(input string, n int) string {
func SplitLines(input string, n int) string {
words := strings.Fields(input)
var result strings.Builder
lineLength := 0
Expand Down
12 changes: 6 additions & 6 deletions internal/ui/ui_say.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
. "github.com/realdream-ai/gdspx/pkg/engine"

"github.com/goplus/spx/internal/engine"
"github.com/goplus/spx/internal/gdi"
"github.com/goplus/spx/internal/text"
)

const (
SayMsgSpliteWidth = 25
SayMsgLineHeight = 26
SayMsgDefaultHeight = 77
sayMsgSpliteWidth = 25
sayMsgLineHeight = 26
sayMsgDefaultHeight = 77
)

type UiSay struct {
Expand Down Expand Up @@ -50,10 +50,10 @@ func (pself *UiSay) SetText(winX, winY float64, x, y float64, w, h float64, msg
hasNextLine := strings.ContainsRune(msg, '\n')
finalMsg := msg
if !hasNextLine {
finalMsg = gdi.SplitString(msg, SayMsgSpliteWidth)
finalMsg = text.SplitLines(msg, sayMsgSpliteWidth)
}
lineCount := strings.Count(finalMsg, "\n")
uiHeight := SayMsgDefaultHeight + float64(lineCount)*SayMsgLineHeight
uiHeight := sayMsgDefaultHeight + float64(lineCount)*sayMsgLineHeight
maxYPos := winY/2 - uiHeight
yPos = math.Max(-winY/2, math.Min(yPos, maxYPos))
xPos = math.Max(-winX/2, math.Min(x, winX/2))
Expand Down
7 changes: 4 additions & 3 deletions measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/goplus/spx/internal/ui"
"github.com/realdream-ai/mathf"
)

const (
Expand All @@ -38,7 +39,7 @@ type measure struct {

// computed properties
text string
color Color
color mathf.Color
svgLineStyle string
svgRotate string
svgSize int // size*scale + 0.5 + measureLineWidth
Expand All @@ -52,7 +53,7 @@ func newMeasure(v specsp) *measure {
text = strings.TrimSuffix(text, ".0")
heading := getSpcspVal(v, "heading", 0.0).(float64)
svgSize := int(size*scale + 0.5 + measureLineWidth)
c, err := parseColor(getSpcspVal(v, "color", 0.0))
c, err := mathf.NewColorAny(getSpcspVal(v, "color", 0.0))
if err != nil {
panic(err)
}
Expand All @@ -69,7 +70,7 @@ func newMeasure(v specsp) *measure {
svgSize: svgSize,
panel: panel,
}
panel.UpdateInfo(meansureObj.x, meansureObj.y, size*scale, heading, text, toEngineColor(c))
panel.UpdateInfo(meansureObj.x, meansureObj.y, size*scale, heading, text, c)
return meansureObj
}

Expand Down
29 changes: 5 additions & 24 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/goplus/spx/internal/tools"
"github.com/goplus/spx/internal/ui"
"github.com/realdream-ai/mathf"
)

// -------------------------------------------------------------------------------------
Expand All @@ -38,7 +39,7 @@ type Monitor struct {
val string
eval func() string
mode int
color Color
color mathf.Color
x, y float64
label string
visible bool
Expand Down Expand Up @@ -72,9 +73,9 @@ func newMonitor(g reflect.Value, v specsp) (*Monitor, error) {
return nil, syscall.ENOENT
}
mode := int(v["mode"].(float64))
color, err := parseColor(getSpcspVal(v, "color"))
color, err := mathf.NewColorAny(getSpcspVal(v, "color"))
if err != nil {
color = Color{R: 0x28, G: 0x9c, B: 0xfc, A: 0xff}
color = RGBA(0x28, 0x9c, 0xfc, 0xff)
}
label := v["label"].(string)
x := v["x"].(float64)
Expand All @@ -100,7 +101,7 @@ func (pself *Monitor) onUpdate(delta float64) {
pself.panel.UpdateScale(pself.size)
pself.panel.UpdatePos(pself.x, pself.y)
pself.panel.UpdateText(pself.label, val)
pself.panel.UpdateColor(toEngineColor(pself.color))
pself.panel.UpdateColor(pself.color)
}

func getTarget(g reflect.Value, target string) (reflect.Value, int) {
Expand Down Expand Up @@ -151,26 +152,6 @@ func (p *Monitor) setVisible(visible bool) {
p.visible = visible
}

const (
stmDefaultW = 47
stmDefaultSmW = 41
stmCornerSize = 2
stmVertGapSm = 4
stmHoriGapSm = 5
)

var (
stmBackground = Color{R: 0xf6, G: 0xf8, B: 0xfa, A: 0xff}
stmBackgroundPen = Color{R: 0xf6, G: 0xf8, B: 0xfa, A: 0xff}
stmValueground = Color{R: 0x21, G: 0x9f, B: 0xfc, A: 0xff}
stmValueRectPen = Color{R: 0xf6, G: 0xf8, B: 0xfa, A: 0xff}
)

type rectKey struct {
x, y, w, h float64
clr, clrPen Color
}

// -------------------------------------------------------------------------------------
// IWidget
func (pself *Monitor) GetName() WidgetName {
Expand Down
6 changes: 2 additions & 4 deletions pen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

package spx

import (
"image/color"
)
import "github.com/realdream-ai/mathf"

// -------------------------------------------------------------------------------------

type penLine struct {
x1, y1 int
x2, y2 int
clr color.RGBA
clr mathf.Color
width int
}

Expand Down
17 changes: 7 additions & 10 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
package spx

import (
"image/color"
"log"
"math"
"reflect"

"github.com/goplus/spx/internal/engine"
"github.com/goplus/spx/internal/gdi"
"github.com/goplus/spx/internal/time"
"github.com/goplus/spx/internal/tools"
"github.com/realdream-ai/mathf"
Expand Down Expand Up @@ -199,7 +197,7 @@ type SpriteImpl struct {
animBindings map[string]string
defaultAnimation SpriteAnimationName

penColor color.RGBA
penColor mathf.Color
penShade float64
penHue float64
penWidth float64
Expand Down Expand Up @@ -1394,8 +1392,6 @@ func (p *SpriteImpl) ClearGraphEffects() {

// -----------------------------------------------------------------------------

type Color = color.RGBA

func (p *SpriteImpl) TouchingColor(color Color) bool {
panic("todo gdspx")
}
Expand Down Expand Up @@ -1544,7 +1540,7 @@ func (p *SpriteImpl) PenDown() {
}

func (p *SpriteImpl) SetPenColor(color Color) {
h, _, v := gdi.RGB2HSV(color.R, color.G, color.B)
h, _, v := color.ToHSV()
p.penHue = (200 * h) / 360
p.penShade = 50 * v
p.penColor = color
Expand Down Expand Up @@ -1595,17 +1591,18 @@ func (p *SpriteImpl) setPenShade(v float64, change bool) {
}

func (p *SpriteImpl) doUpdatePenColor() {
r, g, b := gdi.HSV2RGB((p.penHue*180)/100, 1, 1)
color := mathf.NewColorHSV((p.penHue*180)/100, 1, 1)
shade := p.penShade
if shade > 100 { // range 0..100
shade = 200 - shade
}
a := p.penColor.A
if shade < 50 {
r, g, b = gdi.MixRGB(0, 0, 0, r, g, b, (10+shade)/60)
p.penColor = mathf.LerpColor(mathf.ColorBlack(), color, (10+shade)/60)
} else {
r, g, b = gdi.MixRGB(r, g, b, 255, 255, 255, (shade-50)/60)
p.penColor = mathf.LerpColor(color, mathf.ColorWhite(), (shade-50)/60)
}
p.penColor = color.RGBA{R: r, G: g, B: b, A: p.penColor.A}
p.penColor.A = a
}

func (p *SpriteImpl) SetPenSize(size float64) {
Expand Down
19 changes: 0 additions & 19 deletions uinode.go

This file was deleted.

Loading

0 comments on commit c171baf

Please sign in to comment.