101 lines
2.2 KiB
Go
101 lines
2.2 KiB
Go
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)
|
|
}
|