Compare commits
3 Commits
ux-stuff
...
430259475b
| Author | SHA1 | Date | |
|---|---|---|---|
|
430259475b
|
|||
|
4829fbb5c7
|
|||
|
573396054f
|
@@ -1,61 +0,0 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image/color"
|
||||
|
||||
"git.vezzani.net/ben/games/common/ux/v1"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
"golang.org/x/image/font"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
Label string
|
||||
Style struct {
|
||||
Width float32
|
||||
Height float32
|
||||
XAlign XAlign
|
||||
YAlign YAlign
|
||||
Offset int
|
||||
Font *font.Face
|
||||
BackgroundColor *color.Color
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Block) backgroundColor() color.Color {
|
||||
var c *color.Color
|
||||
if b.Style.BackgroundColor != nil {
|
||||
c = b.Style.BackgroundColor
|
||||
} else {
|
||||
c = &ux.BackgroundColor
|
||||
}
|
||||
|
||||
return *c
|
||||
}
|
||||
|
||||
func (b *Block) size(ctx context.Context) (x, y float32) {
|
||||
x = b.Style.Width
|
||||
y = b.Style.Height
|
||||
|
||||
if x == 0 || y == 0 {
|
||||
px, py := GetContainerSize(ctx)
|
||||
if x == 0 {
|
||||
x = px
|
||||
}
|
||||
if y == 0 {
|
||||
y = py
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (b *Block) Draw(ctx context.Context, image *ebiten.Image) error {
|
||||
xz, yz := GetZero(ctx)
|
||||
w, h := b.size(ctx)
|
||||
|
||||
vector.StrokeRect(image, xz, yz, w, h, 1, b.backgroundColor(), true)
|
||||
|
||||
return nil
|
||||
}
|
||||
58
common/elements/v1/blocks/block.go
Normal file
58
common/elements/v1/blocks/block.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
)
|
||||
|
||||
func New(ops ...Option) elements.InitFunc {
|
||||
return func(d elements.Bounds) elements.Element {
|
||||
b := Block{
|
||||
ContainerBounds: d,
|
||||
}
|
||||
for i := range ops {
|
||||
ops[i](&b)
|
||||
}
|
||||
return &b
|
||||
}
|
||||
}
|
||||
|
||||
type Block struct {
|
||||
ContainerBounds elements.Bounds
|
||||
backgroundColor *color.Color
|
||||
mouse.NopHandler
|
||||
width float64
|
||||
height float64
|
||||
name string
|
||||
}
|
||||
|
||||
func (b *Block) Size() (w, h float64) {
|
||||
w, h = b.ContainerBounds.Width, b.ContainerBounds.Height
|
||||
if b.width != 0 {
|
||||
w = b.width
|
||||
}
|
||||
if b.height != 0 {
|
||||
h = b.height
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (b *Block) Draw(image *ebiten.Image) {
|
||||
w, h := b.Size()
|
||||
if b.backgroundColor != nil {
|
||||
vector.DrawFilledRect(
|
||||
image,
|
||||
float32(b.ContainerBounds.Min.X),
|
||||
float32(b.ContainerBounds.Min.Y),
|
||||
float32(w),
|
||||
float32(h),
|
||||
*b.backgroundColor,
|
||||
true,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
23
common/elements/v1/blocks/options.go
Normal file
23
common/elements/v1/blocks/options.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package blocks
|
||||
|
||||
import "image/color"
|
||||
|
||||
type Option func(*Block)
|
||||
|
||||
func Size(w, h float64) Option {
|
||||
return func(b *Block) {
|
||||
b.width, b.height = w, h
|
||||
}
|
||||
}
|
||||
|
||||
func BackgroundColor(c color.Color) Option {
|
||||
return func(b *Block) {
|
||||
b.backgroundColor = &c
|
||||
}
|
||||
}
|
||||
|
||||
func Name(n string) Option {
|
||||
return func(b *Block) {
|
||||
b.name = n
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image/color"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
"golang.org/x/image/font"
|
||||
)
|
||||
import "git.vezzani.net/ben/games/common/ux/v1"
|
||||
|
||||
type XAlign int
|
||||
type YAlign int
|
||||
|
||||
const (
|
||||
AlignCente XAlign = iota
|
||||
AlignLeft
|
||||
AlignRight
|
||||
)
|
||||
|
||||
const (
|
||||
AlignCenter YAlign = iota
|
||||
AlignTop
|
||||
AlignBottom
|
||||
)
|
||||
|
||||
type Button struct {
|
||||
mouseHandler
|
||||
Block
|
||||
Label string
|
||||
OnClick func() error
|
||||
OnRightClick func() error
|
||||
Style struct {
|
||||
MouseDownColor *color.Color
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Button) HandleClick(_ MouseState) error {
|
||||
if b.OnClick == nil {
|
||||
return nil
|
||||
}
|
||||
return b.OnClick()
|
||||
}
|
||||
|
||||
func (b *Button) getFont() font.Face {
|
||||
if b.Block.Style.Font == nil {
|
||||
return ux.FontFace
|
||||
}
|
||||
|
||||
return *b.Block.Style.Font
|
||||
}
|
||||
|
||||
func (b *Button) backgroundColor() color.Color {
|
||||
var c *color.Color
|
||||
if b.Block.Style.BackgroundColor != nil {
|
||||
c = b.Block.Style.BackgroundColor
|
||||
} else {
|
||||
c = &ux.BackgroundColor
|
||||
}
|
||||
|
||||
if (b.mouseState.RightDown || b.mouseState.LeftDown) && b.Style.MouseDownColor != nil {
|
||||
c = b.Style.MouseDownColor
|
||||
}
|
||||
|
||||
return *c
|
||||
}
|
||||
|
||||
func (b *Button) Draw(ctx context.Context, image *ebiten.Image) error {
|
||||
xz, yz := GetZero(ctx)
|
||||
w, h := b.size(ctx)
|
||||
|
||||
vector.StrokeRect(image, xz, yz, w, h, 1, b.backgroundColor(), true)
|
||||
|
||||
return nil
|
||||
}
|
||||
50
common/elements/v1/buttons/button.go
Normal file
50
common/elements/v1/buttons/button.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package buttons
|
||||
|
||||
import (
|
||||
"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/mouse"
|
||||
)
|
||||
|
||||
func New(ops ...Option) elements.InitFunc {
|
||||
return func(bounds elements.Bounds) elements.Element {
|
||||
b := Button{
|
||||
Block: blocks.Block{ContainerBounds: bounds},
|
||||
}
|
||||
for op := range ops {
|
||||
ops[op](&b)
|
||||
}
|
||||
|
||||
return &b
|
||||
}
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
blocks.Block
|
||||
|
||||
onClick func(ms mouse.State)
|
||||
onMouseDown func(ms mouse.State)
|
||||
onMouseUp func(ms mouse.State)
|
||||
}
|
||||
|
||||
func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
||||
if !b.Block.ContainerBounds.Contains(elements.Point{
|
||||
X: float64(ms.X),
|
||||
Y: float64(ms.Y),
|
||||
}) {
|
||||
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
|
||||
}
|
||||
32
common/elements/v1/buttons/options.go
Normal file
32
common/elements/v1/buttons/options.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package buttons
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
)
|
||||
|
||||
type Option func(button *Button)
|
||||
|
||||
func OnClick(f func(ms mouse.State)) Option {
|
||||
return func(button *Button) {
|
||||
button.onClick = f
|
||||
}
|
||||
}
|
||||
|
||||
func OnMouseDown(f func(ms mouse.State)) Option {
|
||||
return func(button *Button) {
|
||||
button.onMouseDown = f
|
||||
}
|
||||
}
|
||||
|
||||
func OnMouseUp(f func(ms mouse.State)) Option {
|
||||
return func(button *Button) {
|
||||
button.onMouseUp = f
|
||||
}
|
||||
}
|
||||
|
||||
func BlockOpt(o blocks.Option) Option {
|
||||
return func(s *Button) {
|
||||
o(&s.Block)
|
||||
}
|
||||
}
|
||||
@@ -1,92 +1,26 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type MouseState struct {
|
||||
X, Y int32
|
||||
LeftDown, RightDown bool
|
||||
LeftChanged, RightChanged bool
|
||||
}
|
||||
|
||||
const (
|
||||
ContainerSizeKey = "container_size"
|
||||
ZeroKey = "zero"
|
||||
)
|
||||
|
||||
type Clickable interface {
|
||||
Element
|
||||
HandleClick(ctx context.Context, s MouseState) error
|
||||
}
|
||||
|
||||
type Mouseable interface {
|
||||
Element
|
||||
HandleMouseEnter(ctx context.Context, s MouseState) error
|
||||
HandleMouseLeave(ctx context.Context, s MouseState) error
|
||||
HandleMouseMove(ctx context.Context, s MouseState) error
|
||||
HandleMouseDown(ctx context.Context, s MouseState) error
|
||||
HandleMouseUp(ctx context.Context, s MouseState) error
|
||||
}
|
||||
type InitFunc func(Bounds) Element
|
||||
|
||||
type Element interface {
|
||||
Draw(ctx context.Context, image *ebiten.Image) error
|
||||
Size() (w, h float64)
|
||||
|
||||
Draw(image *ebiten.Image)
|
||||
}
|
||||
|
||||
func SetZero(ctx context.Context, x, y float32) context.Context {
|
||||
return context.WithValue(ctx, ZeroKey, [2]float32{x, y})
|
||||
type Point struct {
|
||||
X, Y float64
|
||||
}
|
||||
|
||||
func SetContainerSize(ctx context.Context, w, h float32) context.Context {
|
||||
return context.WithValue(ctx, ContainerSizeKey, [2]float32{w, h})
|
||||
type Bounds struct {
|
||||
Min Point
|
||||
Width, Height float64
|
||||
}
|
||||
|
||||
func GetZero(ctx context.Context) (x, y float32) {
|
||||
if v, ok := ctx.Value(ZeroKey).([2]float32); ok {
|
||||
return v[0], v[1]
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
func GetContainerSize(ctx context.Context) (w, h float32) {
|
||||
if s, ok := ctx.Value(ContainerSizeKey).([2]float32); ok {
|
||||
return s[0], s[1]
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
type mouseHandler struct {
|
||||
mouseState MouseState
|
||||
prevMouseState MouseState
|
||||
}
|
||||
|
||||
func (b *mouseHandler) HandleMouseEnter(s MouseState) error {
|
||||
b.mouseState = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *mouseHandler) HandleMouseLeave(s MouseState) error {
|
||||
b.prevMouseState = b.mouseState
|
||||
b.mouseState = MouseState{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *mouseHandler) HandleMouseMove(s MouseState) error {
|
||||
b.prevMouseState = b.mouseState
|
||||
b.mouseState = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *mouseHandler) HandleMouseDown(s MouseState) error {
|
||||
b.prevMouseState = b.mouseState
|
||||
b.mouseState = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *mouseHandler) HandleMouseUp(s MouseState) error {
|
||||
b.prevMouseState = b.mouseState
|
||||
b.mouseState = s
|
||||
return nil
|
||||
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
|
||||
}
|
||||
|
||||
92
common/elements/v1/mouse/mouse.go
Normal file
92
common/elements/v1/mouse/mouse.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package mouse
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
)
|
||||
|
||||
type NopHandler struct{}
|
||||
|
||||
func (n NopHandler) HandleMouseEvent(s State) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type ChildrenProvider interface {
|
||||
GetChildren() []elements.Element
|
||||
}
|
||||
|
||||
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 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 {
|
||||
if p, ok := e.(ChildrenProvider); ok {
|
||||
for _, c := range p.GetChildren() {
|
||||
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
|
||||
}
|
||||
}
|
||||
26
common/elements/v1/stacks/options.go
Normal file
26
common/elements/v1/stacks/options.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package stacks
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
)
|
||||
|
||||
type Option func(*Stack)
|
||||
|
||||
func BlockOpt(o blocks.Option) Option {
|
||||
return func(s *Stack) {
|
||||
o(&s.Block)
|
||||
}
|
||||
}
|
||||
|
||||
func Children(children ...elements.InitFunc) Option {
|
||||
return func(s *Stack) {
|
||||
s.childrenInit = children
|
||||
}
|
||||
}
|
||||
|
||||
func Horizontal() Option {
|
||||
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"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
func New(ops ...Option) elements.InitFunc {
|
||||
return func(d elements.Bounds) elements.Element {
|
||||
s := Stack{
|
||||
Block: blocks.Block{ContainerBounds: d},
|
||||
}
|
||||
for op := range ops {
|
||||
ops[op](&s)
|
||||
}
|
||||
|
||||
d.Width, d.Height = s.Block.Size()
|
||||
if s.horizontal {
|
||||
d.Width /= float64(len(s.childrenInit))
|
||||
} else {
|
||||
d.Height /= float64(len(s.childrenInit))
|
||||
}
|
||||
|
||||
s.Children = make([]elements.Element, len(s.childrenInit))
|
||||
var offsetX, offsetY float64
|
||||
for i := range s.childrenInit {
|
||||
s.Children[i] = s.childrenInit[i](d)
|
||||
offsetX, offsetY = s.Children[i].Size()
|
||||
if s.horizontal {
|
||||
d.Min.X += offsetX
|
||||
} else {
|
||||
d.Min.Y += offsetY
|
||||
}
|
||||
}
|
||||
|
||||
return &s
|
||||
}
|
||||
}
|
||||
|
||||
type Stack struct {
|
||||
blocks.Block
|
||||
mouse.NopHandler
|
||||
horizontal bool
|
||||
childrenInit []elements.InitFunc
|
||||
Children []elements.Element
|
||||
}
|
||||
|
||||
func (s *Stack) Draw(image *ebiten.Image) {
|
||||
s.Block.Draw(image)
|
||||
|
||||
for i := range s.Children {
|
||||
s.Children[i].Draw(image)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack) GetChildren() []elements.Element {
|
||||
return s.Children
|
||||
}
|
||||
@@ -1,100 +1,93 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
mouseHandler
|
||||
Block
|
||||
|
||||
ColumnCount int
|
||||
RowCount int
|
||||
|
||||
Cells []Element
|
||||
|
||||
Style struct {
|
||||
}
|
||||
|
||||
mouseOverCell int
|
||||
}
|
||||
|
||||
func (t *Table) HandleClick(ctx context.Context, s MouseState) error {
|
||||
address := t.getAddressUnderMouse()
|
||||
if address < 0 || address >= len(t.Cells) {
|
||||
return fmt.Errorf("cell address under mouse is out of bounds")
|
||||
}
|
||||
|
||||
if clickable, ok := t.Cells[address].(Clickable); ok {
|
||||
return clickable.HandleClick(s)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) HandleMouseEnter(ctx context.Context, s MouseState) error {
|
||||
_ = t.mouseHandler.HandleMouseEnter(s)
|
||||
address := t.getAddressUnderMouse()
|
||||
if address < 0 || address >= len(t.Cells) {
|
||||
return fmt.Errorf("cell address under mouse is out of bounds")
|
||||
}
|
||||
|
||||
if mouseable, ok := t.Cells[address].(Mouseable); ok {
|
||||
s.X -=
|
||||
return mouseable.HandleMouseEnter(s)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) HandleMouseLeave(ctx context.Context, s MouseState) error {
|
||||
_ = t.mouseHandler.HandleMouseLeave(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) HandleMouseMove(ctx context.Context, s MouseState) error {
|
||||
_ = t.mouseHandler.HandleMouseMove(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) HandleMouseDown(ctx context.Context, s MouseState) error {
|
||||
_ = t.mouseHandler.HandleMouseDown(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) HandleMouseUp(ctx context.Context, s MouseState) error {
|
||||
_ = t.mouseHandler.HandleMouseUp(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) CellSize(ctx context.Context) (w, h float32) {
|
||||
w, h = GetContainerSize(ctx)
|
||||
return w / float32(t.ColumnCount), h / float32(t.RowCount)
|
||||
}
|
||||
|
||||
func (t *Table) CellZero(ctx context.Context, addr int) (x, y float32) {
|
||||
x, y = GetZero(ctx)
|
||||
w, h := t.CellSize(ctx)
|
||||
|
||||
col, row := addr%t.ColumnCount, addr/t.ColumnCount
|
||||
return x + float32(col)*w, y + float32(row) + h
|
||||
}
|
||||
|
||||
func (t *Table) Draw(ctx context.Context, screen *ebiten.Image) error {
|
||||
|
||||
}
|
||||
|
||||
func (t *Table) contextForCell(ctx context.Context, addr int) context.Context {
|
||||
zx, zy := t.CellZero(ctx, addr)
|
||||
w, h := t.CellSize(ctx)
|
||||
ctx = SetZero(ctx, zx, zy)
|
||||
ctx = SetContainerSize(ctx, w, h)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (t *Table) getAddressUnderMouse() int {
|
||||
return int(t.mouseState.Y)*t.RowCount + int(t.mouseState.X)
|
||||
}
|
||||
//
|
||||
//import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
//
|
||||
// "github.com/hajimehoshi/ebiten/v2"
|
||||
//)
|
||||
//
|
||||
//type Table struct {
|
||||
// mouseHandler
|
||||
// block
|
||||
//
|
||||
// ColumnCount int
|
||||
// RowCount int
|
||||
//
|
||||
// Cells []Bounds
|
||||
//
|
||||
// Style struct {
|
||||
// }
|
||||
//
|
||||
// mouseOverCell int
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleClick(ctx context.Context, s MouseState) error {
|
||||
// address := t.getAddressUnderMouse()
|
||||
// if address < 0 || address >= len(t.Cells) {
|
||||
// return fmt.Errorf("cell address under mouse is out of bounds")
|
||||
// }
|
||||
//
|
||||
// if clickable, ok := t.Cells[address].(Clickable); ok {
|
||||
// return clickable.HandleClick(s)
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseEnter(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseEnter(s)
|
||||
// address := t.getAddressUnderMouse()
|
||||
// if address < 0 || address >= len(t.Cells) {
|
||||
// return fmt.Errorf("cell address under mouse is out of bounds")
|
||||
// }
|
||||
//
|
||||
// if mouseable, ok := t.Cells[address].(Mouseable); ok {
|
||||
// s.X -=
|
||||
// return mouseable.HandleMouseEnter(s)
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseLeave(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseLeave(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseMove(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseMove(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseDown(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseDown(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseUp(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseUp(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) CellSize() (w, h float32) {
|
||||
// w, h = t.
|
||||
// return w / float32(t.ColumnCount), h / float32(t.RowCount)
|
||||
//}
|
||||
//
|
||||
//func (t *Table) CellZero(ctx context.Context, addr int) (x, y float32) {
|
||||
// x, y = GetZero(ctx)
|
||||
// w, h := t.CellSize(ctx)
|
||||
//
|
||||
// col, row := addr%t.ColumnCount, addr/t.ColumnCount
|
||||
// return x + float32(col)*w, y + float32(row) + h
|
||||
//}
|
||||
//
|
||||
//func (t *Table) Draw(screen *ebiten.Image, zx, zy float64) error {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) getAddressUnderMouse() int {
|
||||
// return int(t.mouseState.Y)*t.RowCount + int(t.mouseState.X)
|
||||
//}
|
||||
|
||||
@@ -3,7 +3,7 @@ package sprites
|
||||
import (
|
||||
"image"
|
||||
|
||||
"git.vezzani.net/ben/games/common/window/v1"
|
||||
"git.vezzani.net/ben/games/common/ux/v1"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
@@ -16,9 +16,9 @@ func Update() {
|
||||
}
|
||||
|
||||
type animation struct {
|
||||
RowNumber uint8
|
||||
FrameCount uint8
|
||||
Scale float32
|
||||
RowNumber uint8
|
||||
TickCount uint8
|
||||
Scale float32
|
||||
}
|
||||
|
||||
type subImager interface {
|
||||
@@ -31,7 +31,12 @@ type Sprite struct {
|
||||
imgData subImager
|
||||
animations map[string]animation
|
||||
|
||||
baseAnim *animation
|
||||
baseAnim *animation
|
||||
currentAnim *animation
|
||||
|
||||
animationStartedAt int
|
||||
animationStopAt int
|
||||
animateOnce bool
|
||||
}
|
||||
|
||||
func (s *Sprite) baseAnimation() animation {
|
||||
@@ -50,22 +55,17 @@ func (s *Sprite) baseAnimation() animation {
|
||||
return animation{}
|
||||
}
|
||||
|
||||
func (s *Sprite) getAnimation(id string) animation {
|
||||
if id == "" {
|
||||
return s.baseAnimation()
|
||||
func (s *Sprite) getAnimation() animation {
|
||||
if s.currentAnim != nil {
|
||||
return *s.currentAnim
|
||||
}
|
||||
|
||||
if anim, ok := s.animations[id]; ok {
|
||||
return anim
|
||||
}
|
||||
|
||||
return s.baseAnimation()
|
||||
}
|
||||
|
||||
func (s *Sprite) Image(ops imageOptions) image.Image {
|
||||
anim := s.getAnimation(ops.animation)
|
||||
anim := s.getAnimation()
|
||||
|
||||
xOffset := updateCount % int(anim.FrameCount) * s.width
|
||||
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())
|
||||
@@ -74,9 +74,12 @@ func (s *Sprite) Image(ops imageOptions) image.Image {
|
||||
}
|
||||
|
||||
func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
|
||||
if s.getAnimation().RowNumber == 0 {
|
||||
}
|
||||
|
||||
ops := imageOptions{
|
||||
scaleX: window.Scale,
|
||||
scaleY: window.Scale,
|
||||
scaleX: ux.Scale,
|
||||
scaleY: ux.Scale,
|
||||
}
|
||||
|
||||
for _, o := range options {
|
||||
@@ -98,11 +101,28 @@ func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
animation string
|
||||
}
|
||||
|
||||
type ImageOption func(options *imageOptions)
|
||||
@@ -114,12 +134,6 @@ func ToScale(x, y float64) ImageOption {
|
||||
}
|
||||
}
|
||||
|
||||
func Animation(name string) ImageOption {
|
||||
return func(o *imageOptions) {
|
||||
o.animation = name
|
||||
}
|
||||
}
|
||||
|
||||
func AtPosition(x, y float64) ImageOption {
|
||||
return func(o *imageOptions) {
|
||||
o.x, o.y = x, y
|
||||
|
||||
75
tools/component_test/editor.go
Normal file
75
tools/component_test/editor.go
Normal file
@@ -0,0 +1,75 @@
|
||||
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/buttons"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
"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 root = stacks.New(
|
||||
stacks.Horizontal(),
|
||||
stacks.BlockOpt(blocks.BackgroundColor(color.White)),
|
||||
stacks.BlockOpt(blocks.Size(200, 200)),
|
||||
stacks.BlockOpt(blocks.Name("parent")),
|
||||
stacks.Children(
|
||||
stacks.New(
|
||||
stacks.BlockOpt(blocks.Name("left")),
|
||||
stacks.Children(
|
||||
buttons.New(
|
||||
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
||||
buttons.OnClick(func(ms mouse.State) {
|
||||
println("green")
|
||||
}),
|
||||
),
|
||||
blocks.New(blocks.BackgroundColor(colornames.Yellow)),
|
||||
)),
|
||||
stacks.New(
|
||||
stacks.BlockOpt(blocks.Name("right")),
|
||||
stacks.Children(
|
||||
blocks.New(blocks.BackgroundColor(colornames.Blue)),
|
||||
blocks.New(blocks.BackgroundColor(colornames.Red)),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
func newEditor() *editor {
|
||||
return &editor{}
|
||||
}
|
||||
|
||||
type editor struct {
|
||||
root elements.Element
|
||||
bounds elements.Bounds
|
||||
handleMouse func() bool
|
||||
}
|
||||
|
||||
func (e *editor) Update() error {
|
||||
sprites.Update()
|
||||
e.handleMouse()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *editor) Draw(screen *ebiten.Image) {
|
||||
e.root.Draw(screen)
|
||||
}
|
||||
|
||||
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||
if e.bounds.Width != float64(outsideWidth) || e.bounds.Height != float64(outsideHeight) {
|
||||
e.bounds = elements.Bounds{
|
||||
Min: elements.Point{},
|
||||
Width: float64(outsideWidth),
|
||||
Height: float64(outsideHeight),
|
||||
}
|
||||
e.root = root(e.bounds)
|
||||
e.handleMouse = mouse.Handler(e.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
|
||||
}
|
||||
@@ -1,90 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/ebitenui/ebitenui"
|
||||
"github.com/ebitenui/ebitenui/image"
|
||||
"github.com/ebitenui/ebitenui/widget"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||
"golang.org/x/image/colornames"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
)
|
||||
|
||||
type Editor struct {
|
||||
ui *ebitenui.UI
|
||||
}
|
||||
|
||||
func NewEditor() *Editor {
|
||||
root := widget.NewContainer(
|
||||
widget.ContainerOpts.BackgroundImage(
|
||||
image.NewNineSliceColor(colornames.Gainsboro),
|
||||
))
|
||||
root.AddChild(
|
||||
widget.NewButton(
|
||||
widget.ButtonOpts.TextLabel("Open Sprite Sheet..."),
|
||||
widget.ButtonOpts.TextFace(DefaultFont()),
|
||||
widget.ButtonOpts.TextColor(&widget.ButtonTextColor{
|
||||
Idle: colornames.Gainsboro,
|
||||
Hover: colornames.Gainsboro,
|
||||
Pressed: colornames.Gainsboro,
|
||||
}),
|
||||
widget.ButtonOpts.Image(&widget.ButtonImage{
|
||||
Idle: DefaultNineSlice(colornames.Darkslategray),
|
||||
Hover: DefaultNineSlice(Mix(colornames.Darkslategray, colornames.Mediumseagreen, 0.4)),
|
||||
Disabled: DefaultNineSlice(Mix(colornames.Darkslategray, colornames.Gainsboro, 0.8)),
|
||||
Pressed: PressedNineSlice(Mix(colornames.Darkslategray, colornames.Black, 0.4)),
|
||||
PressedHover: PressedNineSlice(Mix(colornames.Darkslategray, colornames.Black, 0.4)),
|
||||
}),
|
||||
widget.ButtonOpts.WidgetOpts(
|
||||
widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
|
||||
VerticalPosition: widget.AnchorLayoutPositionCenter,
|
||||
HorizontalPosition: widget.AnchorLayoutPositionCenter,
|
||||
}),
|
||||
widget.WidgetOpts.MinSize(180, 48),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
return &Editor{
|
||||
ui: &ebitenui.UI{Container: root},
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Editor) Update() error {
|
||||
e.ui.Update()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Editor) Draw(screen *ebiten.Image) {
|
||||
e.ui.Draw(screen)
|
||||
}
|
||||
|
||||
func (e *Editor) Layout(w, h int) (int, int) {
|
||||
return w, h
|
||||
}
|
||||
|
||||
func DefaultFont() *text.Face {
|
||||
s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var f text.Face
|
||||
|
||||
f = &text.GoTextFace{
|
||||
Source: s,
|
||||
Size: 20,
|
||||
}
|
||||
|
||||
return &f
|
||||
}
|
||||
|
||||
func main() {
|
||||
ebiten.SetWindowSize(480, 320)
|
||||
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
||||
if err := ebiten.RunGame(NewEditor()); err != nil {
|
||||
ebiten.SetWindowTitle("Sprite Editor")
|
||||
e := newEditor()
|
||||
if err := ebiten.RunGame(e); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user