Files
games/common/elements/v1/table.go
2025-08-19 22:29:10 -04:00

85 lines
1.7 KiB
Go

package elements
import (
"context"
"fmt"
)
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(ctx, s)
}
return nil
}
func (t *Table) HandleMouseEnter(s MouseState) error {
_ = t.mouseHandler.HandleMouseEnter(s)
return nil
}
func (t *Table) HandleMouseLeave(s MouseState) error {
_ = t.mouseHandler.HandleMouseLeave(s)
return nil
}
func (t *Table) HandleMouseMove(s MouseState) error {
_ = t.mouseHandler.HandleMouseMove(s)
return nil
}
func (t *Table) HandleMouseDown(s MouseState) error {
_ = t.mouseHandler.HandleMouseDown(s)
return nil
}
func (t *Table) HandleMouseUp(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) 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)
}