last_light/engine/grid.go

203 lines
5.2 KiB
Go
Raw Normal View History

2024-05-06 20:43:35 +03:00
package engine
2024-04-19 23:11:21 +03:00
import (
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
2024-04-19 23:11:21 +03:00
"github.com/google/uuid"
)
2024-04-24 17:11:33 +03:00
type Grid struct {
2024-04-19 23:11:21 +03:00
id uuid.UUID
2024-05-06 21:19:08 +03:00
internalCellSize Size
numCellsHorizontal int
numCellsVertical int
2024-05-06 21:19:08 +03:00
position Position
2024-04-19 23:11:21 +03:00
style tcell.Style
2024-05-03 13:46:32 +03:00
highlightStyle tcell.Style
2024-04-19 23:11:21 +03:00
northBorder rune
westBorder rune
eastBorder rune
southBorder rune
internalVerticalBorder rune
internalHorizontalBorder rune
2024-04-19 23:11:21 +03:00
nwCorner rune
swCorner rune
seCorner rune
neCorner rune
verticalDownwardsTJunction rune
verticalUpwardsTJunction rune
horizontalLeftTJunction rune
horizontalRightTJunction rune
crossJunction rune
2024-04-19 23:11:21 +03:00
2024-05-03 13:46:32 +03:00
isHighlighted bool
2024-05-06 21:19:08 +03:00
highlightedGrid Position
2024-05-03 13:46:32 +03:00
2024-04-19 23:11:21 +03:00
fillRune rune
}
func CreateSimpleGrid(
x, y int,
cellWidth, cellHeight int,
numCellsHorizontal, numCellsVertical int,
borderRune, fillRune rune,
2024-05-03 13:46:32 +03:00
style tcell.Style, highlightStyle tcell.Style,
) *Grid {
return CreateGrid(
x, y, cellWidth, cellHeight, numCellsHorizontal, numCellsVertical,
borderRune, borderRune, borderRune, borderRune,
borderRune, fillRune, borderRune, borderRune,
borderRune, borderRune, borderRune, borderRune,
borderRune, borderRune, borderRune, borderRune,
2024-05-03 13:46:32 +03:00
style, highlightStyle,
)
}
// '┌', '─', '┬', '┐',
// '│', '#', '│', '│',
// '├', '─', '┼', '┤',
// '└', '─', '┴', '┘',
2024-04-19 23:11:21 +03:00
func CreateGrid(
x int,
y int,
cellWidth int,
cellHeight int,
numCellsHorizontal int,
numCellsVertical int,
nwCorner, northBorder, verticalDownwardsTJunction, neCorner,
westBorder, fillRune, internalVerticalBorder, eastBorder,
horizontalRightTJunction, internalHorizontalBorder, crossJunction, horizontalLeftTJunction,
swCorner, southBorder, verticalUpwardsTJunction, seCorner rune,
2024-05-03 13:46:32 +03:00
style tcell.Style, highlightStyle tcell.Style,
) *Grid {
return &Grid{
id: uuid.New(),
2024-05-06 21:19:08 +03:00
internalCellSize: SizeOf(cellWidth, cellHeight),
numCellsHorizontal: numCellsHorizontal,
numCellsVertical: numCellsVertical,
2024-05-03 13:46:32 +03:00
isHighlighted: false,
2024-05-06 21:19:08 +03:00
position: PositionAt(x, y),
style: style,
2024-05-03 13:46:32 +03:00
highlightStyle: highlightStyle,
northBorder: northBorder,
eastBorder: eastBorder,
southBorder: southBorder,
westBorder: westBorder,
internalVerticalBorder: internalVerticalBorder,
internalHorizontalBorder: internalHorizontalBorder,
nwCorner: nwCorner,
seCorner: seCorner,
swCorner: swCorner,
neCorner: neCorner,
verticalDownwardsTJunction: verticalDownwardsTJunction,
verticalUpwardsTJunction: verticalUpwardsTJunction,
horizontalRightTJunction: horizontalRightTJunction,
horizontalLeftTJunction: horizontalLeftTJunction,
fillRune: fillRune,
crossJunction: crossJunction,
2024-04-19 23:11:21 +03:00
}
}
2024-05-03 13:46:32 +03:00
func (g *Grid) UniqueId() uuid.UUID {
2024-04-19 23:11:21 +03:00
return g.id
}
2024-05-06 21:19:08 +03:00
func (g *Grid) Highlight(highlightedGrid Position) {
2024-05-03 13:46:32 +03:00
g.isHighlighted = true
g.highlightedGrid = highlightedGrid
}
func (g *Grid) Unhighlight() {
g.isHighlighted = false
}
2024-05-06 21:19:08 +03:00
func (g *Grid) Position() Position {
2024-05-06 20:43:35 +03:00
return g.position
}
// C###T###T###C
// # # # #
// # # # #
// # # # #
// T###X###X###T
// # # # #
// # # # #
// # # # #
// T###X###X###T
// # # # #
// # # # #
// # # # #
// C###T###T###C
2024-05-03 13:46:32 +03:00
func (g *Grid) drawBorders(v views.View) {
2024-04-24 17:11:33 +03:00
iCellSizeWidth := g.internalCellSize.Width()
iCellSizeHeight := g.internalCellSize.Height()
width := 1 + (iCellSizeWidth * int(g.numCellsHorizontal)) + (int(g.numCellsHorizontal))
height := 1 + (iCellSizeHeight * int(g.numCellsVertical)) + (int(g.numCellsVertical))
x := g.position.X()
y := g.position.Y()
2024-05-03 13:46:32 +03:00
style := g.style
2024-04-24 17:11:33 +03:00
2024-05-03 13:46:32 +03:00
for w := 0; w < width; w++ {
for iw := 1; iw < g.numCellsVertical; iw++ {
v.SetContent(x+w, y+(iw*iCellSizeHeight+iw), g.internalHorizontalBorder, nil, style)
2024-04-24 17:11:33 +03:00
}
if w%(iCellSizeWidth+1) == 0 {
2024-05-03 13:46:32 +03:00
v.SetContent(x+w, y, g.verticalDownwardsTJunction, nil, style)
v.SetContent(x+w, y+height-1, g.verticalUpwardsTJunction, nil, style)
2024-04-24 17:11:33 +03:00
continue
}
2024-05-03 13:46:32 +03:00
v.SetContent(x+w, y, g.northBorder, nil, style)
v.SetContent(x+w, y+height-1, g.southBorder, nil, style)
}
2024-05-03 13:46:32 +03:00
for h := 0; h < height; h++ {
if h == 0 {
v.SetContent(x, y, g.nwCorner, nil, style)
v.SetContent(x, y+height-1, g.swCorner, nil, style)
continue
}
if h == height-1 {
v.SetContent(x+width-1, y, g.neCorner, nil, style)
v.SetContent(x+width-1, y+height-1, g.seCorner, nil, style)
continue
}
2024-04-24 17:11:33 +03:00
2024-05-03 13:46:32 +03:00
for ih := 1; ih < g.numCellsHorizontal; ih++ {
2024-04-24 17:11:33 +03:00
if h%(iCellSizeHeight+1) == 0 {
2024-05-03 13:46:32 +03:00
v.SetContent(x+(ih*iCellSizeWidth+ih), y+h, g.crossJunction, nil, style)
2024-04-24 17:11:33 +03:00
continue
}
2024-05-03 13:46:32 +03:00
v.SetContent(x+(ih*iCellSizeWidth+ih), y+h, g.internalVerticalBorder, nil, style)
2024-04-24 17:11:33 +03:00
}
if h%(iCellSizeHeight+1) == 0 {
2024-05-03 13:46:32 +03:00
v.SetContent(x, y+h, g.horizontalRightTJunction, nil, style)
v.SetContent(x+width-1, y+h, g.horizontalLeftTJunction, nil, style)
2024-04-24 17:11:33 +03:00
continue
}
2024-05-03 13:46:32 +03:00
v.SetContent(x, y+h, g.westBorder, nil, style)
v.SetContent(x+width-1, y+h, g.eastBorder, nil, style)
}
}
2024-05-03 13:46:32 +03:00
func (g *Grid) drawFill(v views.View) {
}
2024-04-19 23:11:21 +03:00
2024-05-03 13:46:32 +03:00
func (g *Grid) Draw(v views.View) {
g.drawBorders(v)
g.drawFill(v)
2024-04-19 23:11:21 +03:00
}