2024-04-19 23:11:21 +03:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mvvasilev/last_light/util"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
2024-04-21 21:51:43 +03:00
|
|
|
"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
|
|
|
|
|
|
|
|
internalCellSize util.Size
|
2024-04-27 22:32:05 +03:00
|
|
|
numCellsHorizontal int
|
|
|
|
numCellsVertical int
|
2024-04-19 23:11:21 +03:00
|
|
|
position util.Position
|
|
|
|
style tcell.Style
|
|
|
|
|
|
|
|
northBorder rune
|
|
|
|
westBorder rune
|
|
|
|
eastBorder rune
|
|
|
|
southBorder rune
|
|
|
|
|
2024-04-21 21:51:43 +03:00
|
|
|
internalVerticalBorder rune
|
|
|
|
internalHorizontalBorder rune
|
|
|
|
|
2024-04-19 23:11:21 +03:00
|
|
|
nwCorner rune
|
|
|
|
swCorner rune
|
|
|
|
seCorner rune
|
|
|
|
neCorner rune
|
|
|
|
|
2024-04-21 21:51:43 +03:00
|
|
|
verticalDownwardsTJunction rune
|
|
|
|
verticalUpwardsTJunction rune
|
|
|
|
horizontalLeftTJunction rune
|
|
|
|
horizontalRightTJunction rune
|
|
|
|
crossJunction rune
|
2024-04-19 23:11:21 +03:00
|
|
|
|
|
|
|
fillRune rune
|
|
|
|
}
|
|
|
|
|
2024-04-21 21:51:43 +03:00
|
|
|
func CreateSimpleGrid(
|
2024-04-27 22:32:05 +03:00
|
|
|
x, y int,
|
|
|
|
cellWidth, cellHeight int,
|
|
|
|
numCellsHorizontal, numCellsVertical int,
|
2024-04-21 21:51:43 +03:00
|
|
|
borderRune, fillRune rune,
|
|
|
|
style tcell.Style,
|
2024-04-24 17:11:33 +03:00
|
|
|
) Grid {
|
2024-04-21 21:51:43 +03:00
|
|
|
return CreateGrid(
|
|
|
|
x, y, cellWidth, cellHeight, numCellsHorizontal, numCellsVertical,
|
|
|
|
borderRune, borderRune, borderRune, borderRune,
|
|
|
|
borderRune, fillRune, borderRune, borderRune,
|
|
|
|
borderRune, borderRune, borderRune, borderRune,
|
|
|
|
borderRune, borderRune, borderRune, borderRune,
|
|
|
|
style,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// '┌', '─', '┬', '┐',
|
|
|
|
// '│', '#', '│', '│',
|
|
|
|
// '├', '─', '┼', '┤',
|
|
|
|
// '└', '─', '┴', '┘',
|
2024-04-19 23:11:21 +03:00
|
|
|
func CreateGrid(
|
2024-04-27 22:32:05 +03:00
|
|
|
x int,
|
|
|
|
y int,
|
|
|
|
cellWidth int,
|
|
|
|
cellHeight int,
|
|
|
|
numCellsHorizontal int,
|
|
|
|
numCellsVertical int,
|
2024-04-21 21:51:43 +03:00
|
|
|
nwCorner, northBorder, verticalDownwardsTJunction, neCorner,
|
|
|
|
westBorder, fillRune, internalVerticalBorder, eastBorder,
|
|
|
|
horizontalRightTJunction, internalHorizontalBorder, crossJunction, horizontalLeftTJunction,
|
|
|
|
swCorner, southBorder, verticalUpwardsTJunction, seCorner rune,
|
2024-04-19 23:11:21 +03:00
|
|
|
style tcell.Style,
|
2024-04-24 17:11:33 +03:00
|
|
|
) Grid {
|
|
|
|
return Grid{
|
2024-04-21 21:51:43 +03:00
|
|
|
id: uuid.New(),
|
|
|
|
internalCellSize: util.SizeOf(cellWidth, cellHeight),
|
|
|
|
numCellsHorizontal: numCellsHorizontal,
|
|
|
|
numCellsVertical: numCellsVertical,
|
|
|
|
position: util.PositionAt(x, y),
|
|
|
|
style: style,
|
|
|
|
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-04-24 17:11:33 +03:00
|
|
|
func (g Grid) UniqueId() uuid.UUID {
|
2024-04-19 23:11:21 +03:00
|
|
|
return g.id
|
|
|
|
}
|
|
|
|
|
2024-04-21 21:51:43 +03:00
|
|
|
// C###T###T###C
|
|
|
|
// # # # #
|
|
|
|
// # # # #
|
|
|
|
// # # # #
|
|
|
|
// T###X###X###T
|
|
|
|
// # # # #
|
|
|
|
// # # # #
|
|
|
|
// # # # #
|
|
|
|
// T###X###X###T
|
|
|
|
// # # # #
|
|
|
|
// # # # #
|
|
|
|
// # # # #
|
|
|
|
// C###T###T###C
|
2024-04-24 17:11:33 +03:00
|
|
|
func (g Grid) drawBorders(v views.View) {
|
|
|
|
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))
|
2024-04-21 21:51:43 +03:00
|
|
|
x := g.position.X()
|
|
|
|
y := g.position.Y()
|
|
|
|
|
|
|
|
v.SetContent(x, y, g.nwCorner, nil, g.style)
|
|
|
|
v.SetContent(x+width-1, y, g.neCorner, nil, g.style)
|
|
|
|
v.SetContent(x, y+height-1, g.swCorner, nil, g.style)
|
|
|
|
v.SetContent(x+width-1, y+height-1, g.seCorner, nil, g.style)
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
for w := 1; w < width-1; w++ {
|
|
|
|
|
|
|
|
for iw := 1; iw < int(g.numCellsVertical); iw++ {
|
|
|
|
if w%(iCellSizeWidth+1) == 0 {
|
|
|
|
v.SetContent(x+w, y+(iw*iCellSizeHeight+iw), g.crossJunction, nil, g.style)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetContent(x+w, y+(iw*iCellSizeHeight+iw), g.internalHorizontalBorder, nil, g.style)
|
|
|
|
}
|
|
|
|
|
|
|
|
if w%(iCellSizeWidth+1) == 0 {
|
|
|
|
v.SetContent(x+w, y, g.verticalDownwardsTJunction, nil, g.style)
|
|
|
|
v.SetContent(x+w, y+height-1, g.verticalUpwardsTJunction, nil, g.style)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetContent(x+w, y, g.northBorder, nil, g.style)
|
|
|
|
v.SetContent(x+w, y+height-1, g.southBorder, nil, g.style)
|
2024-04-21 21:51:43 +03:00
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
for h := 1; h < height-1; h++ {
|
|
|
|
|
|
|
|
for ih := 1; ih < int(g.numCellsHorizontal); ih++ {
|
|
|
|
if h%(iCellSizeHeight+1) == 0 {
|
|
|
|
v.SetContent(x+(ih*iCellSizeHeight+ih), y+h, g.crossJunction, nil, g.style)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetContent(x+(ih*iCellSizeHeight+ih), y+h, g.internalVerticalBorder, nil, g.style)
|
|
|
|
}
|
|
|
|
|
|
|
|
if h%(iCellSizeHeight+1) == 0 {
|
|
|
|
v.SetContent(x, y+h, g.horizontalRightTJunction, nil, g.style)
|
|
|
|
v.SetContent(x+width-1, y+h, g.horizontalLeftTJunction, nil, g.style)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetContent(x, y+h, g.westBorder, nil, g.style)
|
|
|
|
v.SetContent(x+width-1, y+h, g.eastBorder, nil, g.style)
|
2024-04-21 21:51:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func (g Grid) drawFill(v views.View) {
|
2024-04-21 21:51:43 +03:00
|
|
|
|
|
|
|
}
|
2024-04-19 23:11:21 +03:00
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func (g Grid) Draw(v views.View) {
|
2024-04-21 21:51:43 +03:00
|
|
|
g.drawBorders(v)
|
|
|
|
g.drawFill(v)
|
2024-04-19 23:11:21 +03:00
|
|
|
}
|