ui

package
v0.0.0-...-bcb4566 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2025 License: MIT Imports: 8 Imported by: 1

README

####Simple terminal ui package using termbox

Might put this into a seperate repo if i polish it more

Documentation

Index

Constants

View Source
const (
	TextModeOverflow = iota
	TextModeHide
	TextModeWrap
)
View Source
const (
	DefaultWindowFillBG = termbox.ColorBlack
)

Variables

This section is empty.

Functions

func BuildTextLines

func BuildTextLines(in string, width int) []string

func GenCellSlice

func GenCellSlice(str string, points map[int]AttribPair) []termbox.Cell

Generates a cellslice with attributes

func HeightRequired

func HeightRequired(str string, width int) int

Returns the number of lines required

func RunFunc

func RunFunc(e Entity, f func(e Entity))

Runs f recursively and in order on e and its children by in order it means it runs on parent first and children last

func RunFuncCond

func RunFuncCond(e Entity, f func(e Entity) bool) bool

Same as above but stops completely if f returns false

func RunFuncCondTraverse

func RunFuncCondTraverse(e Entity, f func(e Entity) bool)

Same as above but instead of stopping completely, will not traverse the children of e when f returns false

func RuneByPhysPosition

func RuneByPhysPosition(s string, runePos int) (rune, int)

Returns the rune at the physical position, that means it takes The width of runes into account

func SafeSetCursor

func SafeSetCursor(x, y int)

Crashes on windows otherwise if going out of bounds

func SetCells

func SetCells(cells []termbox.Cell, startX, startY, width, height int) int

Sets the cells and returns number of lines

func SimpleSetText

func SimpleSetText(startX, startY, width int, str string, fg, bg termbox.Attribute) int

A Helper for drawing simple text, returns number of lines

func StrSplit

func StrSplit(s string, width int) (split, rest string)

func StringSearch

func StringSearch(search, content string) int

Types

type AttribPair

type AttribPair struct {
	FG termbox.Attribute `json:"fg"`
	BG termbox.Attribute `json:"bg"`
}

Simple bg fg attribute pair

type AutoLayoutContainer

type AutoLayoutContainer struct {
	*BaseEntity
	ForceExpandWidth, ForceExpandHeight bool
	LayoutType                          LayoutType
	LayoutDynamic                       bool
	Spacing                             int
}

func NewAutoLayoutContainer

func NewAutoLayoutContainer() *AutoLayoutContainer

func (*AutoLayoutContainer) BuildLayout

func (a *AutoLayoutContainer) BuildLayout()

func (*AutoLayoutContainer) Destroy

func (a *AutoLayoutContainer) Destroy()

func (AutoLayoutContainer) EmitChangedEvent

func (a AutoLayoutContainer) EmitChangedEvent(e LayoutElement)

func (*AutoLayoutContainer) GetRequiredSize

func (a *AutoLayoutContainer) GetRequiredSize() common.Vector2F

func (*AutoLayoutContainer) IsLayoutDynamic

func (a *AutoLayoutContainer) IsLayoutDynamic() bool

func (*AutoLayoutContainer) Update

func (a *AutoLayoutContainer) Update()

type BackHandler

type BackHandler interface {
	Back() bool // Return false for not handled
}

type BaseEntity

type BaseEntity struct {
	Transform Transform
}

func (*BaseEntity) Children

func (b *BaseEntity) Children(recursive bool) []Entity

func (*BaseEntity) DestroyChildren

func (b *BaseEntity) DestroyChildren()

func (*BaseEntity) GetTransform

func (b *BaseEntity) GetTransform() *Transform

type Container

type Container struct {
	*BaseEntity
	ProxySize     LayoutElement
	Dynamic       bool
	AllowZeroSize bool
}

func NewContainer

func NewContainer() *Container

A bare bones container

func (*Container) Destroy

func (c *Container) Destroy()

func (*Container) GetRequiredSize

func (c *Container) GetRequiredSize() common.Vector2F

func (*Container) IsLayoutDynamic

func (c *Container) IsLayoutDynamic() bool

type DataType

type DataType int
const (
	DataTypeString DataType = iota
	DataTypePassword
	DataTypeInt
	DataTypeFloat
	DataTypeBool
)

type Direction

type Direction int
const (
	DirLeft Direction = iota
	DirRight
	DirUp
	DirDown
	DirEnd
	DirStart
)

type DrawHandler

type DrawHandler interface {
	GetDrawLayer() int
	Draw()
}

type Entity

type Entity interface {
	Children(recursive bool) []Entity
	Destroy()
	GetTransform() *Transform
}

type InputHandler

type InputHandler interface {
	HandleInput(event termbox.Event)
}

type LateUpdateHandler

type LateUpdateHandler interface {
	LateUpdate() // Ran after update, shouldnt change the size of the element
}

type LayoutChangeHandler

type LayoutChangeHandler interface {
	OnLayoutChanged()
}

type LayoutElement

type LayoutElement interface {
	GetRequiredSize() common.Vector2F
	GetTransform() *Transform
	IsLayoutDynamic() bool
}

type LayoutType

type LayoutType int
const (
	LayoutTypeVertical LayoutType = iota
	LayoutTypeHorizontal
)

type Manager

type Manager struct {
	Windows     []Entity
	TextInputs  []*TextInput
	ActiveInput *TextInput
}

func NewManager

func NewManager() *Manager

func (*Manager) AddInput

func (u *Manager) AddInput(input *TextInput, setActive bool)

Inputs

func (*Manager) AddWindow

func (u *Manager) AddWindow(e Entity)

Windows

func (*Manager) AddWindowFront

func (u *Manager) AddWindowFront(e Entity)

func (*Manager) CurrentWindow

func (u *Manager) CurrentWindow() Entity

func (*Manager) RemoveInput

func (u *Manager) RemoveInput(input *TextInput, reAssignActive bool)

func (*Manager) RemoveWindow

func (u *Manager) RemoveWindow(e Entity) bool

func (*Manager) SetActiveInput

func (u *Manager) SetActiveInput(input *TextInput)
type MenuItem struct {
	Name       string
	IsCategory bool
	Decorative bool // Not selectable

	IsInput          bool
	InputType        DataType
	InputDefaultText string

	Marked      bool
	Highlighted bool
	Info        string

	UserData interface{}

	Children []*MenuItem

	Text  *Text
	Input *TextInput
	// contains filtered or unexported fields
}

func FilterOptionsByPath

func FilterOptionsByPath(path []string, options []*MenuItem) []*MenuItem

func SearchFilter

func SearchFilter(searchBy string, in []*MenuItem, path string) []*MenuItem
func (mi *MenuItem) GetDisplayName(searching bool) string
func (mi *MenuItem) RunFunc(f func(mi *MenuItem) bool) bool

Runs f recursively on all children

type MenuItemSlice []*MenuItem
func (mi MenuItemSlice) Len() int
func (mi MenuItemSlice) Less(a, b int) bool
func (mi MenuItemSlice) Swap(i, j int)
type MenuWindow struct {
	*BaseEntity
	Window      *Window
	LowerWindow *Window

	MainContainer     *AutoLayoutContainer
	TopContainer      *Container
	MenuItemContainer *AutoLayoutContainer
	LowerContainer    *Container

	InfoText    *Text
	SearchInput *TextInput

	// Style
	StyleNormal         AttribPair
	StyleMarked         AttribPair
	StyleSelected       AttribPair
	StyleMarkedSelected AttribPair
	StyleInputNormal    AttribPair

	Options         []*MenuItem
	FilteredOptions []*MenuItem
	Selectables     []int
	Highlighted     int

	CurDir []string

	Dirty bool

	Layer int

	OnSelect func(*MenuItem)
	// contains filtered or unexported fields
}

func NewMenuWindow

func NewMenuWindow(layer int, manager *Manager, searchEnabled bool) *MenuWindow
func (mw *MenuWindow) AddMarked(index int)
func (mw *MenuWindow) ApplyStyleToItem(item *MenuItem)
func (mw *MenuWindow) Back() bool
func (mw *MenuWindow) CheckBounds(index int) int

Makes sure index is within len(options)

func (mw *MenuWindow) CheckBoundsSelectedable(index int) int
func (mw *MenuWindow) Destroy()
func (mw *MenuWindow) FilterOptions()
func (mw *MenuWindow) GetHighlighted() *MenuItem
func (mw *MenuWindow) GetIndex(item *MenuItem) int
func (mw *MenuWindow) OptionsHeight() int
func (mw *MenuWindow) Rebuild()
func (mw *MenuWindow) RemoveMarked(index int)
func (mw *MenuWindow) RunFunc(f func(item *MenuItem) bool)
func (mw *MenuWindow) Scroll(dir Direction, amount int)
func (mw *MenuWindow) Select()
func (mw *MenuWindow) SetHighlighted(index int)
func (mw *MenuWindow) SetOptions(options []*MenuItem)
func (mw *MenuWindow) SetOptionsString(options []string)
func (mw *MenuWindow) Update()

type Scrollable

type Scrollable interface {
	Scroll(dir Direction, amount int)
}

type SelectAble

type SelectAble interface {
	Select()
}

type SimpleEntity

type SimpleEntity struct {
	*BaseEntity
}

func NewSimpleEntity

func NewSimpleEntity() *SimpleEntity

func (*SimpleEntity) Destroy

func (s *SimpleEntity) Destroy()

type Text

type Text struct {
	*BaseEntity
	Disabled bool // won't draw then

	Text string

	SkipLines int

	Mode int

	Style AttribPair

	Layer    int
	Userdata interface{}
	// contains filtered or unexported fields
}

func NewText

func NewText() *Text

func (*Text) BuildLines

func (t *Text) BuildLines()

func (*Text) Destroy

func (t *Text) Destroy()

func (*Text) Draw

func (t *Text) Draw()

func (*Text) GetDrawLayer

func (t *Text) GetDrawLayer() int

func (*Text) GetRequiredSize

func (t *Text) GetRequiredSize() common.Vector2F

Implement LayoutElement

func (*Text) HeightRequired

func (t *Text) HeightRequired() int

func (*Text) IsLayoutDynamic

func (t *Text) IsLayoutDynamic() bool

func (*Text) SetAttribs

func (t *Text) SetAttribs(attribs map[int]AttribPair)

type TextInput

type TextInput struct {
	*BaseEntity
	Text *Text

	TextBuffer          string
	CursorLocation      int
	Active              bool
	MaskInput           bool // Replecas everything with "*"
	HideCursorWhenEmpty bool
	Layer               int

	DataType  DataType
	MinHeight int

	Manager *Manager
}

func NewTextInput

func NewTextInput(manager *Manager, layer int) *TextInput

func (*TextInput) Destroy

func (ti *TextInput) Destroy()

func (*TextInput) Draw

func (ti *TextInput) Draw()

func (*TextInput) Erase

func (ti *TextInput) Erase(dir Direction, amount int, byWords bool)

func (*TextInput) GetDrawLayer

func (ti *TextInput) GetDrawLayer() int

func (*TextInput) GetRequiredSize

func (ti *TextInput) GetRequiredSize() common.Vector2F

Implement LayoutElement

func (*TextInput) HandleInput

func (ti *TextInput) HandleInput(event termbox.Event)

func (*TextInput) IsLayoutDynamic

func (ti *TextInput) IsLayoutDynamic() bool

func (*TextInput) MoveCursor

func (ti *TextInput) MoveCursor(dir Direction, amount int, byWords bool)

func (*TextInput) SetActive

func (ti *TextInput) SetActive(active bool)

func (*TextInput) Update

func (ti *TextInput) Update()

type ToggleAble

type ToggleAble interface {
	Toggle()
}

type Transform

type Transform struct {
	AnchorMin common.Vector2F
	AnchorMax common.Vector2F

	Position common.Vector2F
	Size     common.Vector2F

	Top, Bottom, Left, Right int

	Parent   *Transform
	Children []Entity
}

Unity3d like UI transform (minus scale, pivot and rotation)

func (*Transform) AddChildren

func (t *Transform) AddChildren(children ...Entity)

func (*Transform) AddFirst

func (t *Transform) AddFirst(child Entity)

func (*Transform) ClearChildren

func (t *Transform) ClearChildren(destroy bool)

Revmoves and optinally clears children

func (*Transform) GetRect

func (t *Transform) GetRect() common.Rect

Incorrect.. will fix as i come by the silly mistakes

func (*Transform) RemoveChild

func (t *Transform) RemoveChild(child Entity, destroy bool)

type UpdateHandler

type UpdateHandler interface {
	Update() // Ran before drawing, for example add or remove children
}

type Window

type Window struct {
	*BaseEntity

	Title  string
	Footer string

	Layer int

	Border AttribPair
	FillBG termbox.Attribute

	Manager *Manager
}

func NewWindow

func NewWindow(manager *Manager) *Window

func (*Window) Destroy

func (w *Window) Destroy()

func (*Window) Draw

func (w *Window) Draw()

func (*Window) GetDrawLayer

func (w *Window) GetDrawLayer() int

func (*Window) Init

func (w *Window) Init()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL