ux stuff
This commit is contained in:
92
common/elements/v1/element.go
Normal file
92
common/elements/v1/element.go
Normal file
@@ -0,0 +1,92 @@
|
||||
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 Element interface {
|
||||
Draw(ctx context.Context, image *ebiten.Image) error
|
||||
}
|
||||
|
||||
func SetZero(ctx context.Context, x, y float32) context.Context {
|
||||
return context.WithValue(ctx, ZeroKey, [2]float32{x, y})
|
||||
}
|
||||
|
||||
func SetContainerSize(ctx context.Context, w, h float32) context.Context {
|
||||
return context.WithValue(ctx, ContainerSizeKey, [2]float32{w, h})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user