last_light/engine/engine_rectangle.go

217 lines
4.7 KiB
Go
Raw Permalink 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 Rectangle struct {
2024-04-19 23:11:21 +03:00
id uuid.UUID
2024-05-06 21:19:08 +03:00
size Size
position Position
2024-04-19 23:11:21 +03:00
style tcell.Style
northBorder rune
westBorder rune
eastBorder rune
southBorder rune
nwCorner rune
swCorner rune
seCorner rune
neCorner rune
isBorderless bool
isFilled bool
2024-04-19 23:11:21 +03:00
fillRune rune
}
func CreateBorderlessRectangle(x, y int, width, height int, fillRune rune, style tcell.Style) Rectangle {
return CreateRectangle(
x, y, width, height,
0, 0, 0,
0, fillRune, 0,
0, 0, 0,
true, true, style,
)
}
func CreateSimpleEmptyRectangle(x, y int, width, height int, borderRune rune, style tcell.Style) Rectangle {
return CreateRectangle(
x, y, width, height,
borderRune, borderRune, borderRune,
borderRune, 0, borderRune,
borderRune, borderRune, borderRune,
false, false, style,
)
}
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,
false, true, style,
2024-04-19 23:11:21 +03:00
)
}
2024-04-24 17:11:33 +03:00
func CreateRectangleV2(
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(
//
// x, y, width, height,
// '┌', '─', '┐',
// '│', ' ', '│',
// '└', '─', '┘',
// false, true,
// style
2024-04-19 23:11:21 +03:00
//
// )
func CreateRectangle(
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,
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{
id: uuid.New(),
2024-05-06 21:19:08 +03:00
size: SizeOf(width, height),
position: 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-05-06 21:19:08 +03:00
func (rect Rectangle) Position() Position {
2024-04-24 17:11:33 +03:00
return rect.position
}
2024-05-30 23:39:54 +03:00
func (rect Rectangle) Size() Size {
return rect.size
}
2024-04-24 17:11:33 +03:00
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()
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-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-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-24 17:11:33 +03:00
func (rect Rectangle) Draw(v views.View) {
if !rect.isBorderless {
rect.drawBorders(v)
}
if rect.isFilled {
rect.drawFill(v)
}
}
2024-05-30 23:39:54 +03:00
func DrawRectangle(
x int,
y int,
width int,
height int,
nwCorner, northBorder, neCorner,
westBorder, fillRune, eastBorder,
swCorner, southBorder, seCorner rune,
isBorderless, isFilled bool,
style tcell.Style, v views.View,
) {
v.SetContent(x, y, nwCorner, nil, style)
v.SetContent(x+width-1, y, neCorner, nil, style)
v.SetContent(x, y+height-1, swCorner, nil, style)
v.SetContent(x+width-1, y+height-1, seCorner, nil, style)
if !isBorderless {
for w := 1; w < width-1; w++ {
v.SetContent(x+w, y, northBorder, nil, style)
v.SetContent(x+w, y+height-1, southBorder, nil, style)
}
for h := 1; h < height-1; h++ {
v.SetContent(x, y+h, westBorder, nil, style)
v.SetContent(x+width-1, y+h, eastBorder, nil, style)
}
}
if !isFilled {
return
}
for w := 1; w < width-1; w++ {
for h := 1; h < height-1; h++ {
v.SetContent(x+w, y+h, fillRune, nil, style)
}
}
}