2024-05-06 20:43:35 +03:00
|
|
|
package engine
|
2024-04-19 23:11:21 +03:00
|
|
|
|
|
|
|
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 Rectangle struct {
|
2024-04-19 23:11:21 +03:00
|
|
|
id uuid.UUID
|
|
|
|
|
|
|
|
size util.Size
|
|
|
|
position util.Position
|
|
|
|
style tcell.Style
|
|
|
|
|
|
|
|
northBorder rune
|
|
|
|
westBorder rune
|
|
|
|
eastBorder rune
|
|
|
|
southBorder rune
|
|
|
|
|
|
|
|
nwCorner rune
|
|
|
|
swCorner rune
|
|
|
|
seCorner rune
|
|
|
|
neCorner rune
|
|
|
|
|
2024-04-21 21:51:43 +03:00
|
|
|
isBorderless bool
|
|
|
|
isFilled bool
|
|
|
|
|
2024-04-19 23:11:21 +03:00
|
|
|
fillRune rune
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:32:05 +03:00
|
|
|
func CreateBorderlessRectangle(x, y int, width, height int, fillRune rune, style tcell.Style) Rectangle {
|
2024-04-21 21:51:43 +03:00
|
|
|
return CreateRectangle(
|
|
|
|
x, y, width, height,
|
|
|
|
0, 0, 0,
|
|
|
|
0, fillRune, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
true, true, style,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:32:05 +03:00
|
|
|
func CreateSimpleEmptyRectangle(x, y int, width, height int, borderRune rune, style tcell.Style) Rectangle {
|
2024-04-21 21:51:43 +03:00
|
|
|
return CreateRectangle(
|
|
|
|
x, y, width, height,
|
|
|
|
borderRune, borderRune, borderRune,
|
|
|
|
borderRune, 0, borderRune,
|
|
|
|
borderRune, borderRune, borderRune,
|
|
|
|
false, false, style,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:32:05 +03:00
|
|
|
func CreateSimpleRectangle(x int, y int, width int, height int, borderRune rune, fillRune rune, style tcell.Style) Rectangle {
|
2024-04-19 23:11:21 +03:00
|
|
|
return CreateRectangle(
|
|
|
|
x, y, width, height,
|
|
|
|
borderRune, borderRune, borderRune,
|
|
|
|
borderRune, fillRune, borderRune,
|
|
|
|
borderRune, borderRune, borderRune,
|
2024-04-21 21:51:43 +03:00
|
|
|
false, true, style,
|
2024-04-19 23:11:21 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func CreateRectangleV2(
|
2024-04-27 22:32:05 +03:00
|
|
|
x, y int, width, height int,
|
2024-04-24 17:11:33 +03:00
|
|
|
upper, middle, lower string,
|
|
|
|
isBorderless, isFilled bool,
|
|
|
|
style tcell.Style,
|
|
|
|
) Rectangle {
|
|
|
|
upperRunes := []rune(upper)
|
|
|
|
middleRunes := []rune(middle)
|
|
|
|
lowerRunes := []rune(lower)
|
|
|
|
|
|
|
|
return CreateRectangle(
|
|
|
|
x, y, width, height,
|
|
|
|
upperRunes[0], upperRunes[1], upperRunes[2],
|
|
|
|
middleRunes[0], middleRunes[1], middleRunes[2],
|
|
|
|
lowerRunes[0], lowerRunes[1], lowerRunes[2],
|
|
|
|
isBorderless, isFilled, style,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:11:21 +03:00
|
|
|
// CreateRectangle(
|
|
|
|
//
|
2024-04-21 21:51:43 +03:00
|
|
|
// x, y, width, height,
|
|
|
|
// '┌', '─', '┐',
|
|
|
|
// '│', ' ', '│',
|
|
|
|
// '└', '─', '┘',
|
|
|
|
// false, true,
|
|
|
|
// style
|
2024-04-19 23:11:21 +03:00
|
|
|
//
|
|
|
|
// )
|
|
|
|
func CreateRectangle(
|
2024-04-27 22:32:05 +03:00
|
|
|
x int,
|
|
|
|
y int,
|
|
|
|
width int,
|
|
|
|
height int,
|
2024-04-19 23:11:21 +03:00
|
|
|
nwCorner, northBorder, neCorner,
|
|
|
|
westBorder, fillRune, eastBorder,
|
|
|
|
swCorner, southBorder, seCorner rune,
|
2024-04-21 21:51:43 +03:00
|
|
|
isBorderless, isFilled bool,
|
2024-04-19 23:11:21 +03:00
|
|
|
style tcell.Style,
|
2024-04-24 17:11:33 +03:00
|
|
|
) Rectangle {
|
|
|
|
return Rectangle{
|
2024-04-21 21:51:43 +03:00
|
|
|
id: uuid.New(),
|
|
|
|
size: util.SizeOf(width, height),
|
|
|
|
position: util.PositionAt(x, y),
|
|
|
|
style: style,
|
|
|
|
northBorder: northBorder,
|
|
|
|
eastBorder: eastBorder,
|
|
|
|
southBorder: southBorder,
|
|
|
|
westBorder: westBorder,
|
|
|
|
nwCorner: nwCorner,
|
|
|
|
seCorner: seCorner,
|
|
|
|
swCorner: swCorner,
|
|
|
|
neCorner: neCorner,
|
|
|
|
isBorderless: isBorderless,
|
|
|
|
isFilled: isFilled,
|
|
|
|
fillRune: fillRune,
|
2024-04-19 23:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func (rect Rectangle) UniqueId() uuid.UUID {
|
2024-04-19 23:11:21 +03:00
|
|
|
return rect.id
|
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func (rect Rectangle) Position() util.Position {
|
|
|
|
return rect.position
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rect Rectangle) drawBorders(v views.View) {
|
2024-04-19 23:11:21 +03:00
|
|
|
width := rect.size.Width()
|
|
|
|
height := rect.size.Height()
|
|
|
|
x := rect.position.X()
|
|
|
|
y := rect.position.Y()
|
|
|
|
|
2024-04-21 21:51:43 +03:00
|
|
|
v.SetContent(x, y, rect.nwCorner, nil, rect.style)
|
|
|
|
v.SetContent(x+width-1, y, rect.neCorner, nil, rect.style)
|
|
|
|
v.SetContent(x, y+height-1, rect.swCorner, nil, rect.style)
|
|
|
|
v.SetContent(x+width-1, y+height-1, rect.seCorner, nil, rect.style)
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
for w := 1; w < width-1; w++ {
|
|
|
|
v.SetContent(x+w, y, rect.northBorder, nil, rect.style)
|
|
|
|
v.SetContent(x+w, y+height-1, rect.southBorder, nil, rect.style)
|
2024-04-21 21:51:43 +03:00
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
for h := 1; h < height-1; h++ {
|
|
|
|
v.SetContent(x, y+h, rect.westBorder, nil, rect.style)
|
|
|
|
v.SetContent(x+width-1, y+h, rect.eastBorder, nil, rect.style)
|
2024-04-21 21:51:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func (rect Rectangle) drawFill(v views.View) {
|
|
|
|
width := rect.size.Width()
|
|
|
|
height := rect.size.Height()
|
|
|
|
x := rect.position.X()
|
|
|
|
y := rect.position.Y()
|
|
|
|
|
|
|
|
for w := 1; w < width-1; w++ {
|
|
|
|
for h := 1; h < height-1; h++ {
|
|
|
|
v.SetContent(x+w, y+h, rect.fillRune, nil, rect.style)
|
2024-04-19 23:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-21 21:51:43 +03:00
|
|
|
|
2024-04-24 17:11:33 +03:00
|
|
|
func (rect Rectangle) Draw(v views.View) {
|
2024-04-21 21:51:43 +03:00
|
|
|
if !rect.isBorderless {
|
|
|
|
rect.drawBorders(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rect.isFilled {
|
|
|
|
rect.drawFill(v)
|
|
|
|
}
|
|
|
|
}
|