Compare commits
10 Commits
627ca88732
...
element-re
| Author | SHA1 | Date | |
|---|---|---|---|
|
8e7db331a7
|
|||
| 4e6d720a91 | |||
|
5f47a811aa
|
|||
| e454d649a5 | |||
|
dda0a83202
|
|||
|
430259475b
|
|||
|
4829fbb5c7
|
|||
|
573396054f
|
|||
|
8b1bfc8a01
|
|||
|
a7b3a6cd4a
|
8
common/control/v1/mouse.go
Normal file
8
common/control/v1/mouse.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package control
|
||||||
|
|
||||||
|
const (
|
||||||
|
MouseLeft MouseButton = iota
|
||||||
|
MouseRight
|
||||||
|
)
|
||||||
|
|
||||||
|
type MouseButton int
|
||||||
54
common/elements/v1/blocks/block.go
Normal file
54
common/elements/v1/blocks/block.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package blocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(ops ...OptFunc) *Block {
|
||||||
|
b := &Block{}
|
||||||
|
for i := range ops {
|
||||||
|
ops[i](b)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
type Block struct {
|
||||||
|
core.Element
|
||||||
|
|
||||||
|
backgroundColor *color.Color
|
||||||
|
width, height int
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Block) Bounds() geo.Bounds {
|
||||||
|
return geo.Bounds{
|
||||||
|
Min: b.Anchor(),
|
||||||
|
Width: b.width,
|
||||||
|
Height: b.height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Block) Size() (w, h int) {
|
||||||
|
return b.width, b.height
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Block) Draw(image *ebiten.Image) {
|
||||||
|
bnd := b.Bounds()
|
||||||
|
if b.backgroundColor != nil {
|
||||||
|
vector.DrawFilledRect(
|
||||||
|
image,
|
||||||
|
float32(b.Anchor().X),
|
||||||
|
float32(b.Anchor().Y),
|
||||||
|
float32(bnd.Width),
|
||||||
|
float32(bnd.Height),
|
||||||
|
*b.backgroundColor,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
35
common/elements/v1/blocks/options.go
Normal file
35
common/elements/v1/blocks/options.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package blocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OptFunc func(*Block)
|
||||||
|
|
||||||
|
func Core(fs ...core.OptFunc) OptFunc {
|
||||||
|
return func(b *Block) {
|
||||||
|
for _, f := range fs {
|
||||||
|
f(&b.Element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Size(w, h int) OptFunc {
|
||||||
|
return func(b *Block) {
|
||||||
|
b.width, b.height = w, h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BackgroundColor(c color.Color) OptFunc {
|
||||||
|
return func(b *Block) {
|
||||||
|
b.backgroundColor = &c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Name(n string) OptFunc {
|
||||||
|
return func(b *Block) {
|
||||||
|
b.name = n
|
||||||
|
}
|
||||||
|
}
|
||||||
81
common/elements/v1/buttons/button.go
Normal file
81
common/elements/v1/buttons/button.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package buttons
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
"git.vezzani.net/ben/games/common/ux/v1"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/text"
|
||||||
|
"golang.org/x/image/font"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(ops ...OptFunc) *Button {
|
||||||
|
b := Button{
|
||||||
|
Block: blocks.Block{},
|
||||||
|
font: ux.FontFace,
|
||||||
|
color: ux.FontColor,
|
||||||
|
}
|
||||||
|
for op := range ops {
|
||||||
|
ops[op](&b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &b
|
||||||
|
}
|
||||||
|
|
||||||
|
type Button struct {
|
||||||
|
blocks.Block
|
||||||
|
|
||||||
|
label string
|
||||||
|
font font.Face
|
||||||
|
color color.Color
|
||||||
|
|
||||||
|
onClick func(ms mouse.State)
|
||||||
|
onMouseDown func(ms mouse.State)
|
||||||
|
onMouseUp func(ms mouse.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Button) Draw(screen *ebiten.Image) {
|
||||||
|
b.Block.Draw(screen)
|
||||||
|
b.drawLabel(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Button) drawLabel(screen *ebiten.Image) {
|
||||||
|
if b.label != "" {
|
||||||
|
strBounds, _ := font.BoundString(b.font, b.label)
|
||||||
|
|
||||||
|
textWidth := strBounds.Max.X - strBounds.Min.X
|
||||||
|
textHeight := strBounds.Max.Y - strBounds.Min.Y
|
||||||
|
|
||||||
|
bnd := b.Bounds()
|
||||||
|
tx := bnd.Min.X + (bnd.Width / 2) - (textWidth / 2).Round()
|
||||||
|
ty := bnd.Min.Y + (bnd.Height / 2) - (textHeight / 2).Round()
|
||||||
|
|
||||||
|
text.Draw(screen, b.label, b.font, tx, ty, b.color)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
||||||
|
bnd := b.Bounds()
|
||||||
|
|
||||||
|
if !bnd.Contains(ms.Point()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
||||||
|
b.onClick(ms)
|
||||||
|
return true
|
||||||
|
case b.onMouseUp != nil && (ms.RightChanged && !ms.RightDown || ms.LeftChanged && !ms.LeftDown):
|
||||||
|
b.onMouseUp(ms)
|
||||||
|
return true
|
||||||
|
case b.onMouseDown != nil && (ms.RightChanged && ms.RightDown || ms.LeftChanged && ms.LeftDown):
|
||||||
|
b.onMouseDown(ms)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
55
common/elements/v1/buttons/options.go
Normal file
55
common/elements/v1/buttons/options.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package buttons
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OptFunc func(button *Button)
|
||||||
|
|
||||||
|
func Core(fs ...core.OptFunc) OptFunc {
|
||||||
|
return func(b *Button) {
|
||||||
|
for _, f := range fs {
|
||||||
|
f(&b.Element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnClick(f func(ms mouse.State)) OptFunc {
|
||||||
|
return func(button *Button) {
|
||||||
|
button.onClick = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnMouseDown(f func(ms mouse.State)) OptFunc {
|
||||||
|
return func(button *Button) {
|
||||||
|
button.onMouseDown = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnMouseUp(f func(ms mouse.State)) OptFunc {
|
||||||
|
return func(button *Button) {
|
||||||
|
button.onMouseUp = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BlockOpt(o blocks.OptFunc) OptFunc {
|
||||||
|
return func(s *Button) {
|
||||||
|
o(&s.Block)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Label(s string) OptFunc {
|
||||||
|
return func(button *Button) {
|
||||||
|
button.label = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LabelColor(color color.Color) OptFunc {
|
||||||
|
return func(button *Button) {
|
||||||
|
button.color = color
|
||||||
|
}
|
||||||
|
}
|
||||||
17
common/elements/v1/core/config.go
Normal file
17
common/elements/v1/core/config.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
|
||||||
|
type OptFunc func(*Element)
|
||||||
|
|
||||||
|
func Children(children ...elements.Element) OptFunc {
|
||||||
|
return func(c *Element) {
|
||||||
|
c.children = children
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Name(name string) OptFunc {
|
||||||
|
return func(s *Element) {
|
||||||
|
s.name = name
|
||||||
|
}
|
||||||
|
}
|
||||||
32
common/elements/v1/core/core.go
Normal file
32
common/elements/v1/core/core.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Element struct {
|
||||||
|
mouse.NopHandler
|
||||||
|
|
||||||
|
anchor geo.Point
|
||||||
|
children []elements.Element
|
||||||
|
|
||||||
|
name string // For debugging
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Element) Anchor() geo.Point {
|
||||||
|
return c.anchor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Element) SetAnchor(a geo.Point) {
|
||||||
|
d := c.Anchor().Delta(a)
|
||||||
|
for _, ch := range c.Children() {
|
||||||
|
ch.SetAnchor(ch.Anchor().Add(d))
|
||||||
|
}
|
||||||
|
c.anchor = a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Element) Children() []elements.Element {
|
||||||
|
return c.children
|
||||||
|
}
|
||||||
14
common/elements/v1/element.go
Normal file
14
common/elements/v1/element.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package elements
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Element interface {
|
||||||
|
Draw(*ebiten.Image)
|
||||||
|
SetAnchor(geo.Point)
|
||||||
|
Anchor() geo.Point
|
||||||
|
Size() (w, h int)
|
||||||
|
Children() []Element
|
||||||
|
}
|
||||||
94
common/elements/v1/mouse/mouse.go
Normal file
94
common/elements/v1/mouse/mouse.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package mouse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NopHandler struct{}
|
||||||
|
|
||||||
|
func (n NopHandler) HandleMouseEvent(s State) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventHandler interface {
|
||||||
|
HandleMouseEvent(s State) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type State struct {
|
||||||
|
X, Y int
|
||||||
|
LeftDown, RightDown bool
|
||||||
|
LeftChanged, RightChanged bool
|
||||||
|
LeftClicked, RightClicked bool
|
||||||
|
Clicked bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) Point() geo.Point {
|
||||||
|
return geo.Point{
|
||||||
|
X: s.X,
|
||||||
|
Y: s.Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Handler(e elements.Element) func() bool {
|
||||||
|
newState := StateBuilder()
|
||||||
|
ms := newState()
|
||||||
|
return func() bool {
|
||||||
|
ms = newState()
|
||||||
|
return propagateMouse(e, ms)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func propagateMouse(e elements.Element, ms State) bool {
|
||||||
|
for _, c := range e.Children() {
|
||||||
|
if propagateMouse(c, ms) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if e, ok := e.(EventHandler); ok {
|
||||||
|
return e.HandleMouseEvent(ms)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func StateBuilder() func() State {
|
||||||
|
prevState := State{}
|
||||||
|
state := State{}
|
||||||
|
return func() State {
|
||||||
|
state = prevState
|
||||||
|
state.X, state.Y = ebiten.CursorPosition()
|
||||||
|
|
||||||
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||||
|
state.LeftDown = true
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
|
||||||
|
state.RightDown = true
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
|
||||||
|
state.LeftDown = false
|
||||||
|
if !state.RightDown {
|
||||||
|
state.Clicked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
|
||||||
|
state.RightDown = false
|
||||||
|
if !state.LeftDown {
|
||||||
|
state.Clicked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.Clicked = state.Clicked && !prevState.Clicked
|
||||||
|
|
||||||
|
state.LeftChanged = state.LeftDown != prevState.LeftDown
|
||||||
|
state.RightChanged = state.RightDown != prevState.RightDown
|
||||||
|
state.LeftClicked = state.LeftChanged && state.LeftDown
|
||||||
|
state.RightClicked = state.RightChanged && state.RightDown
|
||||||
|
|
||||||
|
prevState = state
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
}
|
||||||
21
common/elements/v1/stacks/options.go
Normal file
21
common/elements/v1/stacks/options.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package stacks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OptFunc func(*Stack)
|
||||||
|
|
||||||
|
func Core(fs ...core.OptFunc) OptFunc {
|
||||||
|
return func(s *Stack) {
|
||||||
|
for _, f := range fs {
|
||||||
|
f(&s.Element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Horizontal() OptFunc {
|
||||||
|
return func(s *Stack) {
|
||||||
|
s.horizontal = true
|
||||||
|
}
|
||||||
|
}
|
||||||
61
common/elements/v1/stacks/stack.go
Normal file
61
common/elements/v1/stacks/stack.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package stacks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(ops ...OptFunc) *Stack {
|
||||||
|
s := Stack{}
|
||||||
|
for op := range ops {
|
||||||
|
ops[op](&s)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cw, ch int
|
||||||
|
anchor := s.Anchor()
|
||||||
|
for _, c := range s.Children() {
|
||||||
|
c.SetAnchor(anchor)
|
||||||
|
cw, ch = c.Size()
|
||||||
|
if s.horizontal {
|
||||||
|
anchor.X += cw
|
||||||
|
} else {
|
||||||
|
anchor.Y += ch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stack struct {
|
||||||
|
core.Element
|
||||||
|
mouse.NopHandler
|
||||||
|
horizontal bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack) Size() (w, h int) {
|
||||||
|
var cw, ch int
|
||||||
|
for _, c := range s.Children() {
|
||||||
|
cw, ch = c.Size()
|
||||||
|
if s.horizontal {
|
||||||
|
w += cw
|
||||||
|
if h < ch {
|
||||||
|
h = ch
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
h += ch
|
||||||
|
if w < cw {
|
||||||
|
w = cw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack) Draw(image *ebiten.Image) {
|
||||||
|
for _, c := range s.Children() {
|
||||||
|
c.Draw(image)
|
||||||
|
}
|
||||||
|
}
|
||||||
10
common/geo/bounds.go
Normal file
10
common/geo/bounds.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
type Bounds struct {
|
||||||
|
Min Point
|
||||||
|
Width, Height int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bounds) Contains(p Point) bool {
|
||||||
|
return p.X >= b.Min.X && p.X <= b.Min.X+b.Width && p.Y >= b.Min.Y && p.Y <= b.Min.Y+b.Height
|
||||||
|
}
|
||||||
19
common/geo/point.go
Normal file
19
common/geo/point.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
type Point struct {
|
||||||
|
X, Y int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Point) Delta(p2 Point) Point {
|
||||||
|
return Point{
|
||||||
|
X: p2.X - p.X,
|
||||||
|
Y: p2.Y - p.Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Point) Add(p2 Point) Point {
|
||||||
|
return Point{
|
||||||
|
X: p.X + p2.X,
|
||||||
|
Y: p.Y + p2.Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
147
common/sprites/v1/sprite.go
Normal file
147
common/sprites/v1/sprite.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package sprites
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/ux/v1"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
updateCount int
|
||||||
|
)
|
||||||
|
|
||||||
|
func Update() {
|
||||||
|
updateCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
type animation struct {
|
||||||
|
RowNumber uint8
|
||||||
|
TickCount uint8
|
||||||
|
Scale float32
|
||||||
|
}
|
||||||
|
|
||||||
|
type subImager interface {
|
||||||
|
image.Image
|
||||||
|
SubImage(r image.Rectangle) image.Image
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sprite struct {
|
||||||
|
width, height int
|
||||||
|
imgData subImager
|
||||||
|
animations map[string]animation
|
||||||
|
|
||||||
|
baseAnim *animation
|
||||||
|
currentAnim *animation
|
||||||
|
|
||||||
|
animationStartedAt int
|
||||||
|
animationStopAt int
|
||||||
|
animateOnce bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) baseAnimation() animation {
|
||||||
|
if s.baseAnim != nil {
|
||||||
|
return *s.baseAnim
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range s.animations {
|
||||||
|
if s.animations[i].RowNumber == 0 {
|
||||||
|
a := s.animations[i]
|
||||||
|
s.baseAnim = &a
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return animation{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) getAnimation() animation {
|
||||||
|
if s.currentAnim != nil {
|
||||||
|
return *s.currentAnim
|
||||||
|
}
|
||||||
|
return s.baseAnimation()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) Image(ops imageOptions) image.Image {
|
||||||
|
anim := s.getAnimation()
|
||||||
|
|
||||||
|
xOffset := updateCount % int(anim.TickCount) * s.width
|
||||||
|
yOffset := int(anim.RowNumber) * s.height
|
||||||
|
|
||||||
|
r := image.Rect(xOffset, yOffset, s.width, s.height).Intersect(s.imgData.Bounds())
|
||||||
|
|
||||||
|
return s.imgData.SubImage(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
|
||||||
|
if s.getAnimation().RowNumber == 0 {
|
||||||
|
}
|
||||||
|
|
||||||
|
ops := imageOptions{
|
||||||
|
scaleX: ux.Scale,
|
||||||
|
scaleY: ux.Scale,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, o := range options {
|
||||||
|
o(&ops)
|
||||||
|
}
|
||||||
|
|
||||||
|
geom := ebiten.GeoM{}
|
||||||
|
geom.Translate(ops.x, ops.y)
|
||||||
|
geom.Scale(ops.scaleX, ops.scaleY)
|
||||||
|
geom.Rotate(ops.rotateTheta)
|
||||||
|
|
||||||
|
screen.DrawImage(
|
||||||
|
ebiten.NewImageFromImage(
|
||||||
|
s.Image(ops),
|
||||||
|
),
|
||||||
|
&ebiten.DrawImageOptions{
|
||||||
|
GeoM: geom,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) StartAnimation(id string, once bool) {
|
||||||
|
if a, ok := s.animations[id]; ok {
|
||||||
|
s.currentAnim = &a
|
||||||
|
}
|
||||||
|
if once {
|
||||||
|
s.animateOnce = true
|
||||||
|
s.animationStopAt = updateCount + int(s.currentAnim.TickCount)
|
||||||
|
}
|
||||||
|
s.animationStartedAt = updateCount
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) StopAnimation() {
|
||||||
|
s.currentAnim = nil
|
||||||
|
s.animationStartedAt = 0
|
||||||
|
s.animationStopAt = 0
|
||||||
|
s.animateOnce = false
|
||||||
|
}
|
||||||
|
|
||||||
|
type imageOptions struct {
|
||||||
|
x, y float64
|
||||||
|
scaleX, scaleY float64
|
||||||
|
rotateTheta float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ImageOption func(options *imageOptions)
|
||||||
|
|
||||||
|
func ToScale(x, y float64) ImageOption {
|
||||||
|
return func(o *imageOptions) {
|
||||||
|
o.scaleX *= x
|
||||||
|
o.scaleY *= y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AtPosition(x, y float64) ImageOption {
|
||||||
|
return func(o *imageOptions) {
|
||||||
|
o.x, o.y = x, y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Rotate(theta float64) ImageOption {
|
||||||
|
return func(o *imageOptions) {
|
||||||
|
o.rotateTheta = theta
|
||||||
|
}
|
||||||
|
}
|
||||||
15
common/ux/v1/ux.go
Normal file
15
common/ux/v1/ux.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package ux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"golang.org/x/image/font"
|
||||||
|
"golang.org/x/image/font/basicfont"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
FontFace font.Face = basicfont.Face7x13
|
||||||
|
FontColor color.Color = color.Black
|
||||||
|
BackgroundColor color.Color = color.White
|
||||||
|
Scale float64 = 1.0
|
||||||
|
)
|
||||||
@@ -3,12 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/games/minesweeper/game"
|
"git.vezzani.net/ben/games/games/minesweeper/v1/game"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
g := game.New(10, 10, 50)
|
g := game.New(10, 10, 15)
|
||||||
w, h := g.Layout(0, 0)
|
w, h := g.Layout(0, 0)
|
||||||
ebiten.SetWindowSize(w, h)
|
ebiten.SetWindowSize(w, h)
|
||||||
ebiten.SetWindowTitle("Minesweeper")
|
ebiten.SetWindowTitle("Minesweeper")
|
||||||
229
games/minesweeper/v2/game/game.go
Normal file
229
games/minesweeper/v2/game/game.go
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
package game
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/colors"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/text"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
|
"golang.org/x/image/font/basicfont"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mouseButton int
|
||||||
|
|
||||||
|
const (
|
||||||
|
cellSize = 32
|
||||||
|
borderThickness = 1
|
||||||
|
|
||||||
|
sideMargin = cellSize
|
||||||
|
topMargin = cellSize * 2
|
||||||
|
bottomMargin = cellSize
|
||||||
|
|
||||||
|
left mouseButton = iota
|
||||||
|
right
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
backgroundColor = colors.FromInt(0xffffff)
|
||||||
|
borderColor = colors.FromInt(0x999999)
|
||||||
|
revealedColor = colors.FromInt(0xdddddd)
|
||||||
|
downColor = colors.FromInt(0xaaaaaa)
|
||||||
|
|
||||||
|
dangerColors = [8]color.Color{
|
||||||
|
colors.FromInt(0x000000),
|
||||||
|
colors.FromInt(0x440000),
|
||||||
|
colors.FromInt(0x880000),
|
||||||
|
colors.FromInt(0xaa0000),
|
||||||
|
colors.FromInt(0xcc0000),
|
||||||
|
colors.FromInt(0xdd0000),
|
||||||
|
colors.FromInt(0xee0000),
|
||||||
|
colors.FromInt(0xff0000),
|
||||||
|
}
|
||||||
|
|
||||||
|
cols, rows int32
|
||||||
|
|
||||||
|
board []square
|
||||||
|
|
||||||
|
leftDown, rightDown bool
|
||||||
|
leftClicked, rightClicked bool
|
||||||
|
|
||||||
|
mouseX, mouseY int
|
||||||
|
|
||||||
|
over bool
|
||||||
|
)
|
||||||
|
|
||||||
|
type square struct {
|
||||||
|
mined bool
|
||||||
|
revealed bool
|
||||||
|
danger uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(colCount, rowCount, mineCount int32) *Core {
|
||||||
|
cols = colCount
|
||||||
|
rows = rowCount
|
||||||
|
board = make([]square, cols*rows)
|
||||||
|
for range mineCount {
|
||||||
|
addr := rand.Int31n(cols * rows)
|
||||||
|
for board[addr].mined {
|
||||||
|
addr = rand.Int31n(cols * rows)
|
||||||
|
}
|
||||||
|
board[addr].mined = true
|
||||||
|
}
|
||||||
|
|
||||||
|
initDanger()
|
||||||
|
|
||||||
|
return &Core{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Core struct{}
|
||||||
|
|
||||||
|
func (c Core) Update() error {
|
||||||
|
mouseX, mouseY = ebiten.CursorPosition()
|
||||||
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||||
|
leftDown = true
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
|
||||||
|
rightDown = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
|
||||||
|
leftDown = false
|
||||||
|
if !rightDown {
|
||||||
|
handleClick(left)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
|
||||||
|
rightDown = false
|
||||||
|
if !leftDown {
|
||||||
|
handleClick(right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Core) Draw(screen *ebiten.Image) {
|
||||||
|
maxX, maxY := c.Layout(0, 0)
|
||||||
|
vector.DrawFilledRect(screen, 0, 0, float32(maxX), float32(maxY), backgroundColor, false)
|
||||||
|
|
||||||
|
drawGrid(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Core) Layout(_, _ int) (screenWidth, screenHeight int) {
|
||||||
|
return int(cellSize*cols + sideMargin*2), int(cellSize*rows + topMargin + bottomMargin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func drawGrid(screen *ebiten.Image) {
|
||||||
|
vector.StrokeRect(screen, sideMargin, topMargin, float32(cellSize*cols), float32(cellSize*rows), borderThickness, borderColor, false)
|
||||||
|
|
||||||
|
mouseSquare := int32(-1)
|
||||||
|
|
||||||
|
if leftDown || rightDown {
|
||||||
|
mouseSquare = mouseOverSquare()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range rows * cols {
|
||||||
|
clr := backgroundColor
|
||||||
|
if board[i].revealed {
|
||||||
|
clr = revealedColor
|
||||||
|
} else {
|
||||||
|
if mouseSquare == i {
|
||||||
|
clr = downColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x, y, w, h := getCellSquare(i)
|
||||||
|
vector.DrawFilledRect(screen, x, y, w, h, clr, false)
|
||||||
|
vector.StrokeRect(screen, x, y, w, h, borderThickness, borderColor, false)
|
||||||
|
if board[i].revealed && board[i].danger > 0 {
|
||||||
|
txt := strconv.Itoa(int(board[i].danger))
|
||||||
|
|
||||||
|
text.Draw(screen, txt, basicfont.Face7x13, int(x)+12, int(y)+20, dangerColors[board[i].danger])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mouseOverSquare() int32 {
|
||||||
|
if int32(mouseX) < sideMargin ||
|
||||||
|
int32(mouseY) < topMargin ||
|
||||||
|
int32(mouseX) > sideMargin+cellSize*cols ||
|
||||||
|
int32(mouseX) > topMargin+cellSize*rows {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
baseMouseX := (int32(mouseX) - sideMargin) / cellSize
|
||||||
|
baseMouseY := (int32(mouseY) - topMargin) / cellSize
|
||||||
|
|
||||||
|
return baseMouseY*rows + baseMouseX
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCellSquare(addr int32) (float32, float32, float32, float32) {
|
||||||
|
c, r := float32(addr%cols), float32(addr/rows)
|
||||||
|
return sideMargin + c*cellSize, topMargin + r*cellSize, cellSize, cellSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleClick(side mouseButton) {
|
||||||
|
sq := mouseOverSquare()
|
||||||
|
if sq == -1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if side == left {
|
||||||
|
if board[sq].mined {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
reveal(sq)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reveal(addr int32) {
|
||||||
|
if board[addr].revealed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
board[addr].revealed = true
|
||||||
|
for _, nAddr := range getNeighbors(addr) {
|
||||||
|
if board[nAddr].revealed || board[nAddr].mined {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
reveal(nAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNeighbors(addr int32) []int32 {
|
||||||
|
ns := make([]int32, 0)
|
||||||
|
sides := make([]int32, 0)
|
||||||
|
c, r := addr%cols, addr/rows
|
||||||
|
if c > 0 {
|
||||||
|
sides = append(sides, addr-1)
|
||||||
|
}
|
||||||
|
if c < cols-1 {
|
||||||
|
sides = append(sides, addr+1)
|
||||||
|
}
|
||||||
|
if r > 0 {
|
||||||
|
ns = append(ns, addr-cols)
|
||||||
|
for _, s := range sides {
|
||||||
|
ns = append(ns, s-cols)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r < rows-1 {
|
||||||
|
ns = append(ns, addr+cols)
|
||||||
|
for _, s := range sides {
|
||||||
|
ns = append(ns, s+cols)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(ns, sides...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDanger() {
|
||||||
|
for i := range board {
|
||||||
|
for _, n := range getNeighbors(int32(i)) {
|
||||||
|
if board[n].mined {
|
||||||
|
board[i].danger++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
games/minesweeper/v2/main.go
Normal file
1
games/minesweeper/v2/main.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package v2
|
||||||
17
go.mod
17
go.mod
@@ -1,17 +1,22 @@
|
|||||||
module git.vezzani.net/ben/games
|
module git.vezzani.net/ben/games
|
||||||
|
|
||||||
go 1.24
|
go 1.25
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/ebitenui/ebitenui v0.7.1
|
||||||
github.com/hajimehoshi/ebiten/v2 v2.8.8
|
github.com/hajimehoshi/ebiten/v2 v2.8.8
|
||||||
golang.org/x/image v0.20.0
|
golang.org/x/image v0.25.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325 // indirect
|
github.com/ebitengine/gomobile v0.0.0-20250209143333-6071a2a2351c // indirect
|
||||||
github.com/ebitengine/hideconsole v1.0.0 // indirect
|
github.com/ebitengine/hideconsole v1.0.0 // indirect
|
||||||
github.com/ebitengine/purego v0.8.0 // indirect
|
github.com/ebitengine/purego v0.8.2 // indirect
|
||||||
|
github.com/frustra/bbcode v0.0.0-20201127003707-6ef347fbe1c8 // indirect
|
||||||
|
github.com/go-text/typesetting v0.3.0 // indirect
|
||||||
github.com/jezek/xgb v1.1.1 // indirect
|
github.com/jezek/xgb v1.1.1 // indirect
|
||||||
golang.org/x/sync v0.8.0 // indirect
|
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
|
||||||
golang.org/x/sys v0.25.0 // indirect
|
golang.org/x/sync v0.12.0 // indirect
|
||||||
|
golang.org/x/sys v0.31.0 // indirect
|
||||||
|
golang.org/x/text v0.23.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
46
go.sum
46
go.sum
@@ -1,22 +1,44 @@
|
|||||||
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325 h1:Gk1XUEttOk0/hb6Tq3WkmutWa0ZLhNn/6fc6XZpM7tM=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325/go.mod h1:ulhSQcbPioQrallSuIzF8l1NKQoD7xmMZc5NxzibUMY=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/ebitengine/gomobile v0.0.0-20250209143333-6071a2a2351c h1:nCxkoQoJMcVLc5aoMp3ULbfyEMcQjxopBKgNQVBQFXE=
|
||||||
|
github.com/ebitengine/gomobile v0.0.0-20250209143333-6071a2a2351c/go.mod h1:yMh1VvLL71zDgHlVlIXXJIGmv36QcJ9ZD2gtIGYAp3I=
|
||||||
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
|
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
|
||||||
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
|
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
|
||||||
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE=
|
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
|
||||||
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||||
|
github.com/ebitenui/ebitenui v0.7.1 h1:5ZOaonRs4EsO9LtVVJr1zXNBXadUA9ktaZQLg9QgWao=
|
||||||
|
github.com/ebitenui/ebitenui v0.7.1/go.mod h1:QiJoDflkWoBv4V/LKErS3cgzTZHrXDQyqajef7IA8vM=
|
||||||
|
github.com/frustra/bbcode v0.0.0-20201127003707-6ef347fbe1c8 h1:sdIsYe6Vv7KIWZWp8KqSeTl+XlF17d+wHCC4lbxFcYs=
|
||||||
|
github.com/frustra/bbcode v0.0.0-20201127003707-6ef347fbe1c8/go.mod h1:0QBxkXxN+o4FyZgLI9FHY/oUizheze3+bNY/kgCKL+4=
|
||||||
|
github.com/go-text/typesetting v0.3.0 h1:OWCgYpp8njoxSRpwrdd1bQOxdjOXDj9Rqart9ML4iF4=
|
||||||
|
github.com/go-text/typesetting v0.3.0/go.mod h1:qjZLkhRgOEYMhU9eHBr3AR4sfnGJvOXNLt8yRAySFuY=
|
||||||
|
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066 h1:qCuYC+94v2xrb1PoS4NIDe7DGYtLnU2wWiQe9a1B1c0=
|
||||||
|
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
|
||||||
github.com/hajimehoshi/bitmapfont/v3 v3.2.0 h1:0DISQM/rseKIJhdF29AkhvdzIULqNIIlXAGWit4ez1Q=
|
github.com/hajimehoshi/bitmapfont/v3 v3.2.0 h1:0DISQM/rseKIJhdF29AkhvdzIULqNIIlXAGWit4ez1Q=
|
||||||
github.com/hajimehoshi/bitmapfont/v3 v3.2.0/go.mod h1:8gLqGatKVu0pwcNCJguW3Igg9WQqVXF0zg/RvrGQWyg=
|
github.com/hajimehoshi/bitmapfont/v3 v3.2.0/go.mod h1:8gLqGatKVu0pwcNCJguW3Igg9WQqVXF0zg/RvrGQWyg=
|
||||||
github.com/hajimehoshi/ebiten/v2 v2.8.8 h1:xyMxOAn52T1tQ+j3vdieZ7auDBOXmvjUprSrxaIbsi8=
|
github.com/hajimehoshi/ebiten/v2 v2.8.8 h1:xyMxOAn52T1tQ+j3vdieZ7auDBOXmvjUprSrxaIbsi8=
|
||||||
github.com/hajimehoshi/ebiten/v2 v2.8.8/go.mod h1:durJ05+OYnio9b8q0sEtOgaNeBEQG7Yr7lRviAciYbs=
|
github.com/hajimehoshi/ebiten/v2 v2.8.8/go.mod h1:durJ05+OYnio9b8q0sEtOgaNeBEQG7Yr7lRviAciYbs=
|
||||||
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
|
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
|
||||||
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||||
|
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
|
||||||
|
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
|
||||||
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
|
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
|
||||||
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||||
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
|
||||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
|
||||||
|
golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ=
|
||||||
|
golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs=
|
||||||
|
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||||
|
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
|
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||||
|
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||||
|
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
85
tools/component_test/editor.go
Normal file
85
tools/component_test/editor.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/buttons"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/stacks"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
"git.vezzani.net/ben/games/common/sprites/v1"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"golang.org/x/image/colornames"
|
||||||
|
)
|
||||||
|
|
||||||
|
var root = stacks.New(
|
||||||
|
stacks.Horizontal(),
|
||||||
|
stacks.Core(core.Name("parent")),
|
||||||
|
stacks.Core(core.Children(
|
||||||
|
stacks.New(
|
||||||
|
stacks.Core(
|
||||||
|
core.Name("left"),
|
||||||
|
core.Children(
|
||||||
|
buttons.New(
|
||||||
|
buttons.BlockOpt(blocks.Size(100, 100)),
|
||||||
|
buttons.Label("hello"),
|
||||||
|
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
||||||
|
buttons.OnClick(func(ms mouse.State) {
|
||||||
|
println("green")
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
blocks.New(
|
||||||
|
blocks.Size(100, 100),
|
||||||
|
blocks.BackgroundColor(colornames.Yellow),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
stacks.New(
|
||||||
|
stacks.Core(core.Name("right")),
|
||||||
|
stacks.Core(core.Children(
|
||||||
|
blocks.New(
|
||||||
|
blocks.Size(100, 100),
|
||||||
|
blocks.BackgroundColor(colornames.Blue),
|
||||||
|
),
|
||||||
|
blocks.New(
|
||||||
|
blocks.Size(100, 100),
|
||||||
|
blocks.BackgroundColor(colornames.Red),
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
func newEditor() *editor {
|
||||||
|
return &editor{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type editor struct {
|
||||||
|
bounds geo.Bounds
|
||||||
|
handleMouse func() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editor) Update() error {
|
||||||
|
sprites.Update()
|
||||||
|
e.handleMouse()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editor) Draw(screen *ebiten.Image) {
|
||||||
|
root.Draw(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||||
|
if e.bounds.Width != outsideWidth || e.bounds.Height != outsideHeight {
|
||||||
|
e.bounds = geo.Bounds{
|
||||||
|
Min: geo.Point{},
|
||||||
|
Width: outsideWidth,
|
||||||
|
Height: outsideHeight,
|
||||||
|
}
|
||||||
|
e.handleMouse = mouse.Handler(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
return outsideWidth, outsideHeight
|
||||||
|
}
|
||||||
15
tools/component_test/main.go
Normal file
15
tools/component_test/main.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ebiten.SetWindowSize(480, 320)
|
||||||
|
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
||||||
|
ebiten.SetWindowTitle("Component/Layout Test")
|
||||||
|
e := newEditor()
|
||||||
|
if err := ebiten.RunGame(e); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
48
tools/spritedit/editor.go
Normal file
48
tools/spritedit/editor.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/stacks"
|
||||||
|
"git.vezzani.net/ben/games/common/sprites/v1"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"golang.org/x/image/colornames"
|
||||||
|
)
|
||||||
|
|
||||||
|
var menu = stacks.New(
|
||||||
|
stacks.BlockOpt(blocks.BackgroundColor(color.White)),
|
||||||
|
stacks.BlockOpt(blocks.Size(100, 200)),
|
||||||
|
stacks.Children(
|
||||||
|
blocks.New(blocks.BackgroundColor(colornames.Green)),
|
||||||
|
blocks.New(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
func newEditor() *editor {
|
||||||
|
return &editor{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type editor struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editor) Update() error {
|
||||||
|
sprites.Update()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editor) Draw(screen *ebiten.Image) {
|
||||||
|
b := screen.Bounds()
|
||||||
|
menu(elements.Bounds{
|
||||||
|
ZX: float64(b.Min.X),
|
||||||
|
ZY: float64(b.Min.Y),
|
||||||
|
WX: float64(b.Max.X),
|
||||||
|
WY: float64(b.Max.Y),
|
||||||
|
}).Draw(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||||
|
return outsideWidth, outsideHeight
|
||||||
|
}
|
||||||
15
tools/spritedit/main.go
Normal file
15
tools/spritedit/main.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ebiten.SetWindowSize(480, 320)
|
||||||
|
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
||||||
|
ebiten.SetWindowTitle("Sprite Editor")
|
||||||
|
e := newEditor()
|
||||||
|
if err := ebiten.RunGame(e); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user