tui/types.go

143 lines
2.6 KiB
Go
Raw Normal View History

2022-04-01 20:10:51 +02:00
package tui
import (
"fmt"
2023-04-24 11:55:04 +02:00
"git.milar.in/milarin/buf2d"
2022-04-01 20:10:51 +02:00
"github.com/gdamore/tcell"
)
type ViewBuffer = buf2d.Buffer[Rune]
type KeyEvent = tcell.EventKey
type Style = tcell.Style
type Color = tcell.Color
var StyleDefault Style = tcell.StyleDefault
2022-04-02 13:01:41 +02:00
type Point struct {
X, Y int
}
func P(x, y int) Point {
return Point{x, y}
}
func (p Point) In(d Dimension) bool {
2022-05-04 15:17:26 +02:00
return p.X >= d.X && p.X < d.X+d.Width && p.Y >= d.Y && p.Y < d.Y+d.Height
}
func (p Point) String() string {
return fmt.Sprintf("P(%d, %d)", p.X, p.Y)
}
2022-04-02 13:01:41 +02:00
type Size struct {
Width, Height int
}
func S(w, h int) Size {
return Size{w, h}
}
func (s Size) String() string {
return fmt.Sprintf("S(%d, %d)", s.Width, s.Height)
}
2022-04-02 13:01:41 +02:00
type Dimension struct {
Point
Size
}
func D(x, y, w, h int) Dimension {
return Dimension{P(x, y), S(w, h)}
}
func (d Dimension) String() string {
return fmt.Sprintf("D(%d, %d, %d, %d)", d.X, d.Y, d.Width, d.Height)
}
2022-04-02 13:01:41 +02:00
type Orientation uint8
const (
Horizontal Orientation = iota
Vertical
)
type Side uint8
const (
2022-04-02 15:09:52 +02:00
SideTop Side = iota
SideBottom
SideLeft
SideRight
)
2022-05-04 10:17:16 +02:00
// Horizontal returns true if s is either SideLeft or SideRight
func (s Side) Horizontal() bool {
return s == SideLeft || s == SideRight
}
// Vertical returns true if s is either SideTop or SideBottom
func (s Side) Vertical() bool {
return s == SideTop || s == SideBottom
}
2022-04-02 15:09:52 +02:00
type Anchor uint8
const (
AnchorTopLeft Anchor = iota
AnchorTop
AnchorTopRight
AnchorLeft
AnchorCenter
AnchorRight
AnchorBottomLeft
AnchorBottom
AnchorBottomRight
2022-04-02 13:01:41 +02:00
)
type MouseEvent struct {
Position Point
Button MouseButton
Modifiers tcell.ModMask
}
type MouseButton uint8
const (
MouseButtonNone MouseButton = iota
MouseButtonLeft
MouseButtonMiddle
MouseButtonRight
MouseButtonNext
MouseButtonPrev
MouseWheelUp
MouseWheelDown
MouseWheelLeft
MouseWheelRight
)
func convertMouseButton(mask tcell.ButtonMask) MouseButton {
if mask&tcell.Button1 == tcell.Button1 {
return MouseButtonLeft
} else if mask&tcell.Button2 == tcell.Button2 {
return MouseButtonMiddle
} else if mask&tcell.Button3 == tcell.Button3 {
return MouseButtonRight
} else if mask&tcell.Button4 == tcell.Button4 {
return MouseButtonNext
} else if mask&tcell.Button5 == tcell.Button5 {
return MouseButtonPrev
} else if mask&tcell.WheelUp == tcell.WheelUp {
return MouseWheelUp
} else if mask&tcell.WheelDown == tcell.WheelDown {
return MouseWheelDown
} else if mask&tcell.WheelLeft == tcell.WheelLeft {
return MouseWheelLeft
} else if mask&tcell.WheelRight == tcell.WheelRight {
return MouseWheelRight
}
return MouseButtonNone
}