microui

package module
v0.0.0-...-0219c4d Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: Unlicense Imports: 6 Imported by: 0

README

microui

A tiny, portable, immediate-mode UI library ported to Go (as of commit 0850aba860959c3e75fb3e97120ca92957f9d057, v2.02)

API changes

  • Functions and structs are renamed to be PascalCase and the prefix mu_ is removed, like this:

    mu_push_command -> PushCommand

    mu_begin_treenode_ex -> BeginTreeNodeEx

    mu_get_clip_rect -> GetClipRect

  • Every function that takes mu_Context (Context) instead has a Context reciever, so Button(ctx, label) becomes ctx.Button(label)

  • Stacks are now slices with variable length, append is used for push and slice = slice[:len(slice)-1] is used for pop

  • mu_Font (Font) is interface{}, since it doesn't store any font data. You can use reflect if you want to store values inside it

  • All pointer-based commands (MU_COMMAND_JUMP) and the Command struct have been reworked to use indices

  • The mu_Real type has been replaced with float32 because Go does not allow implicit casting of identical type aliases

  • The library is split into separate files instead of one file

  • The library is ~1300 lines of code in total

Additional functions:

  • NewContext, which is a helper for creating a new Context
  • ctx.Render, which calls a function for every command inside the command list, then clears it

Integrations, demos, renderers

Notes

The library expects the user to provide input and handle the resultant drawing commands, it does not do any drawing/tessellation itself.

Credits

Thank you @rxi for creating this awesome library and thank you @Zyko0 for contributing numerous fixes to this Go port.

Documentation

Index

Constants

View Source
const (
	MU_COMMANDLIST_SIZE    = 256 * 1024
	MU_ROOTLIST_SIZE       = 32
	MU_CONTAINERSTACK_SIZE = 32
	MU_CLIPSTACK_SIZE      = 32
	MU_IDSTACK_SIZE        = 32
	MU_LAYOUTSTACK_SIZE    = 16
	MU_CONTAINERPOOL_SIZE  = 48
	MU_TREENODEPOOL_SIZE   = 48
	MU_MAX_WIDTHS          = 16
)
View Source
const (
	MU_REAL_FMT   = "%.3g"
	MU_SLIDER_FMT = "%.2f"
	MU_MAX_FMT    = 127
)
View Source
const (
	MU_CLIP_PART = 1 + iota
	MU_CLIP_ALL
)
View Source
const (
	MU_COMMAND_JUMP = 1 + iota
	MU_COMMAND_CLIP
	MU_COMMAND_RECT
	MU_COMMAND_TEXT
	MU_COMMAND_ICON
	MU_COMMAND_MAX
)
View Source
const (
	MU_COLOR_TEXT = iota
	MU_COLOR_BORDER
	MU_COLOR_WINDOWBG
	MU_COLOR_TITLEBG
	MU_COLOR_TITLETEXT
	MU_COLOR_PANELBG
	MU_COLOR_BUTTON
	MU_COLOR_BUTTONHOVER
	MU_COLOR_BUTTONFOCUS
	MU_COLOR_BASE
	MU_COLOR_BASEHOVER
	MU_COLOR_BASEFOCUS
	MU_COLOR_SCROLLBASE
	MU_COLOR_SCROLLTHUMB
	MU_COLOR_MAX
)
View Source
const (
	MU_ICON_CLOSE = 1 + iota
	MU_ICON_CHECK
	MU_ICON_COLLAPSED
	MU_ICON_EXPANDED
	MU_ICON_MAX
)
View Source
const (
	MU_RES_ACTIVE = (1 << 0)
	MU_RES_SUBMIT = (1 << 1)
	MU_RES_CHANGE = (1 << 2)
)
View Source
const (
	MU_OPT_ALIGNCENTER = (1 << 0)
	MU_OPT_ALIGNRIGHT  = (1 << 1)
	MU_OPT_NOINTERACT  = (1 << 2)
	MU_OPT_NOFRAME     = (1 << 3)
	MU_OPT_NORESIZE    = (1 << 4)
	MU_OPT_NOSCROLL    = (1 << 5)
	MU_OPT_NOCLOSE     = (1 << 6)
	MU_OPT_NOTITLE     = (1 << 7)
	MU_OPT_HOLDFOCUS   = (1 << 8)
	MU_OPT_AUTOSIZE    = (1 << 9)
	MU_OPT_POPUP       = (1 << 10)
	MU_OPT_CLOSED      = (1 << 11)
	MU_OPT_EXPANDED    = (1 << 12)
)
View Source
const (
	MU_MOUSE_LEFT   = (1 << 0)
	MU_MOUSE_RIGHT  = (1 << 1)
	MU_MOUSE_MIDDLE = (1 << 2)
)
View Source
const (
	MU_KEY_SHIFT     = (1 << 0)
	MU_KEY_CTRL      = (1 << 1)
	MU_KEY_ALT       = (1 << 2)
	MU_KEY_BACKSPACE = (1 << 3)
	MU_KEY_RETURN    = (1 << 4)
)
View Source
const (
	RELATIVE = 1 + iota
	ABSOLUTE
)
View Source
const (
	HASH_INITIAL = 2166136261 // 32bit fnv-1a hash
)
View Source
const MU_VERSION = "2.01"

Variables

View Source
var (
	UnclippedRect = Rect{0, 0, 0x1000000, 0x1000000}
)

Functions

func PtrToBytes

func PtrToBytes(ptr unsafe.Pointer) []byte

Convinience function for custom widgets. This will convert the literal pointer (NOT the data it points to) to a byte slice (to be used with GetID()). This is basically equivalent to passing a pointer pointer to mu_get_id.

This function will allocate a new byte slice on the heap. It produces different results based on the endianess of the machine, but that should be fine for hashing purposes.

Types

type BaseCommand

type BaseCommand struct {
	Type int
}

type ClipCommand

type ClipCommand struct {
	Base BaseCommand
	Rect Rect
}

type Color

type Color struct {
	R, G, B, A uint8
}

func NewColor

func NewColor(r, g, b, a uint8) Color

func (*Color) ToRGBA

func (c *Color) ToRGBA() color.RGBA

type Command

type Command struct {
	Type int
	Idx  int
	Base BaseCommand // type 0 (TODO)
	Jump JumpCommand // type 1
	Clip ClipCommand // type 2
	Rect RectCommand // type 3
	Text TextCommand // type 4
	Icon IconCommand // type 5
}

type Container

type Container struct {
	HeadIdx     int
	TailIdx     int
	Rect        Rect
	Body        Rect
	ContentSize Vec2
	Scroll      Vec2
	Zindex      int
	Open        bool
}

func (*Container) Clear

func (c *Container) Clear()

type Context

type Context struct {
	TextWidth  func(font Font, str string) int
	TextHeight func(font Font) int
	DrawFrame  func(ctx *Context, rect Rect, colorid int)

	Style         *Style
	Hover         mu_Id
	Focus         mu_Id
	LastID        mu_Id
	LastRect      Rect
	LastZindex    int
	UpdatedFocus  bool
	Frame         int
	HoverRoot     *Container
	NextHoverRoot *Container
	ScrollTarget  *Container
	NumberEditBuf string
	NumberEdit    mu_Id

	CommandList    []*Command
	RootList       []*Container
	ContainerStack []*Container
	ClipStack      []Rect
	IdStack        []mu_Id
	LayoutStack    []Layout

	ContainerPool [MU_CONTAINERPOOL_SIZE]MuPoolItem
	Containers    [MU_CONTAINERPOOL_SIZE]Container
	TreeNodePool  [MU_TREENODEPOOL_SIZE]MuPoolItem

	MousePos Vec2

	MouseDelta   Vec2
	ScrollDelta  Vec2
	MouseDown    int
	MousePressed int
	KeyDown      int
	KeyPressed   int
	TextInput    []rune
	// contains filtered or unexported fields
}

func NewContext

func NewContext() *Context

func (*Context) AddScrollbar

func (ctx *Context) AddScrollbar(cnt *Container, b *Rect, cs Vec2, swap bool)

if `swap` is true, X = Y, Y = X, W = H, H = W

func (*Context) Begin

func (ctx *Context) Begin()

func (*Context) BeginPanel

func (ctx *Context) BeginPanel(name string)

func (*Context) BeginPanelEx

func (ctx *Context) BeginPanelEx(name string, opt int)

func (*Context) BeginPopup

func (ctx *Context) BeginPopup(name string) int

func (*Context) BeginRootContainer

func (ctx *Context) BeginRootContainer(cnt *Container)

func (*Context) BeginTreeNode

func (ctx *Context) BeginTreeNode(label string) bool

func (*Context) BeginTreeNodeEx

func (ctx *Context) BeginTreeNodeEx(label string, opt int) int

func (*Context) BeginWindow

func (ctx *Context) BeginWindow(title string, rect Rect) bool

func (*Context) BeginWindowEx

func (ctx *Context) BeginWindowEx(title string, rect Rect, opt int) int

func (*Context) BringToFront

func (ctx *Context) BringToFront(cnt *Container)

func (*Context) Button

func (ctx *Context) Button(label string) bool

func (*Context) ButtonEx

func (ctx *Context) ButtonEx(label string, icon int, opt int) int

func (*Context) CheckClip

func (ctx *Context) CheckClip(r Rect) int

func (*Context) Checkbox

func (ctx *Context) Checkbox(label string, state *bool) int

func (*Context) DrawBox

func (ctx *Context) DrawBox(rect Rect, color Color)

func (*Context) DrawControlFrame

func (ctx *Context) DrawControlFrame(id mu_Id, rect Rect, colorid int, opt int)

func (*Context) DrawControlText

func (ctx *Context) DrawControlText(str string, rect Rect, colorid int, opt int)

func (*Context) DrawIcon

func (ctx *Context) DrawIcon(id int, rect Rect, color Color)

func (*Context) DrawRect

func (ctx *Context) DrawRect(rect Rect, color Color)

pushes a new rect command

func (*Context) DrawText

func (ctx *Context) DrawText(font Font, str string, pos Vec2, color Color)

func (*Context) End

func (ctx *Context) End()

func (*Context) EndPanel

func (ctx *Context) EndPanel()

func (*Context) EndPopup

func (ctx *Context) EndPopup()

func (*Context) EndRootContainer

func (ctx *Context) EndRootContainer()

func (*Context) EndTreeNode

func (ctx *Context) EndTreeNode()

func (*Context) EndWindow

func (ctx *Context) EndWindow()

func (*Context) GetClipRect

func (ctx *Context) GetClipRect() Rect

func (*Context) GetContainer

func (ctx *Context) GetContainer(name string) *Container

func (*Context) GetCurrentContainer

func (ctx *Context) GetCurrentContainer() *Container

func (*Context) GetID

func (ctx *Context) GetID(data []byte) mu_Id

func (*Context) GetLayout

func (ctx *Context) GetLayout() *Layout

func (*Context) Header

func (ctx *Context) Header(label string) bool

func (*Context) HeaderEx

func (ctx *Context) HeaderEx(label string, opt int) int

func (*Context) InHoverRoot

func (ctx *Context) InHoverRoot() bool

func (*Context) InputKeyDown

func (ctx *Context) InputKeyDown(key int)

func (*Context) InputKeyUp

func (ctx *Context) InputKeyUp(key int)

func (*Context) InputMouseDown

func (ctx *Context) InputMouseDown(x, y int, btn int)

func (*Context) InputMouseMove

func (ctx *Context) InputMouseMove(x, y int)

func (*Context) InputMouseUp

func (ctx *Context) InputMouseUp(x, y int, btn int)

func (*Context) InputScroll

func (ctx *Context) InputScroll(x, y int)

func (*Context) InputText

func (ctx *Context) InputText(text []rune)

func (*Context) Label

func (ctx *Context) Label(text string)

func (*Context) LayoutBeginColumn

func (ctx *Context) LayoutBeginColumn()

func (*Context) LayoutEndColumn

func (ctx *Context) LayoutEndColumn()

func (*Context) LayoutHeight

func (ctx *Context) LayoutHeight(height int)

sets layout size.y

func (*Context) LayoutNext

func (ctx *Context) LayoutNext() Rect

func (*Context) LayoutRow

func (ctx *Context) LayoutRow(items int, widths []int, height int)

func (*Context) LayoutSetNext

func (ctx *Context) LayoutSetNext(r Rect, relative bool)

func (*Context) LayoutWidth

func (ctx *Context) LayoutWidth(width int)

sets layout size.x

func (*Context) MouseOver

func (ctx *Context) MouseOver(rect Rect) bool

func (*Context) MuHeader

func (ctx *Context) MuHeader(label string, istreenode bool, opt int) int

func (*Context) NextCommand

func (ctx *Context) NextCommand(cmd **Command) bool

sets cmd to the next command in command_list, returns true if success

func (*Context) Number

func (ctx *Context) Number(value *float32, step float32) int

func (*Context) NumberEx

func (ctx *Context) NumberEx(value *float32, step float32, format string, opt int) int

func (*Context) NumberTextBox

func (ctx *Context) NumberTextBox(value *float32, r Rect, id mu_Id) bool

func (*Context) OpenPopup

func (ctx *Context) OpenPopup(name string)

func (*Context) PoolGet

func (ctx *Context) PoolGet(items []MuPoolItem, id mu_Id) int

returns the index of an ID in the pool. returns -1 if it is not found

func (*Context) PoolInit

func (ctx *Context) PoolInit(items []MuPoolItem, id mu_Id) int

func (*Context) PoolUpdate

func (ctx *Context) PoolUpdate(items []MuPoolItem, idx int)

func (*Context) PopClipRect

func (ctx *Context) PopClipRect()

func (*Context) PopContainer

func (ctx *Context) PopContainer()

func (*Context) PopID

func (ctx *Context) PopID()

func (*Context) PushClipRect

func (ctx *Context) PushClipRect(rect Rect)

func (*Context) PushCommand

func (ctx *Context) PushCommand(cmd_type int) *Command

adds a new command with type cmd_type to command_list

func (*Context) PushContainerBody

func (ctx *Context) PushContainerBody(cnt *Container, body Rect, opt int)

func (*Context) PushID

func (ctx *Context) PushID(data []byte)

func (*Context) PushJump

func (ctx *Context) PushJump(dstIdx int) int

pushes a new jump command to command_list

func (*Context) PushLayout

func (ctx *Context) PushLayout(body Rect, scroll Vec2)

func (*Context) Render

func (ctx *Context) Render(nextCmdFunc func(cmd *Command))

calls nextCmdFunc for every command in the command list, clears it when done. equivalent to calling `ctx.NextCommand` in a loop

func (*Context) Scrollbars

func (ctx *Context) Scrollbars(cnt *Container, body *Rect)

func (*Context) SetClip

func (ctx *Context) SetClip(rect Rect)

pushes a new clip command

func (*Context) SetFocus

func (ctx *Context) SetFocus(id mu_Id)

func (*Context) Slider

func (ctx *Context) Slider(value *float32, lo, hi float32) int

func (*Context) SliderEx

func (ctx *Context) SliderEx(value *float32, low float32, high float32, step float32, format string, opt int) int

func (*Context) Text

func (ctx *Context) Text(text string)

func (*Context) TextBox

func (ctx *Context) TextBox(buf *string) int

func (*Context) TextBoxEx

func (ctx *Context) TextBoxEx(buf *string, opt int) int

func (*Context) TextboxRaw

func (ctx *Context) TextboxRaw(buf *string, id mu_Id, r Rect, opt int) int

func (*Context) UpdateControl

func (ctx *Context) UpdateControl(id mu_Id, rect Rect, opt int)

type Font

type Font interface{} // Font is interface{}, microui does not manage fonts

type IconCommand

type IconCommand struct {
	Base  BaseCommand
	Rect  Rect
	Id    int
	Color Color
}

type JumpCommand

type JumpCommand struct {
	Base   BaseCommand
	DstIdx int
}

type Layout

type Layout struct {
	Body      Rect
	Next      Rect
	Position  Vec2
	Size      Vec2
	Max       Vec2
	Widths    [MU_MAX_WIDTHS]int
	Items     int
	ItemIndex int
	NextRow   int
	NextType  int
	Indent    int
}

type MuPoolItem

type MuPoolItem struct {
	ID         mu_Id
	LastUpdate int
}

type Rect

type Rect struct {
	X, Y, W, H int
}

func NewRect

func NewRect(x, y, w, h int) Rect

type RectCommand

type RectCommand struct {
	Base  BaseCommand
	Rect  Rect
	Color Color
}

type Style

type Style struct {
	Font          Font
	Size          Vec2
	Padding       int
	Spacing       int
	Indent        int
	TitleHeight   int
	ScrollbarSize int
	ThumbSize     int
	Colors        [MU_COLOR_MAX]Color
}

type TextCommand

type TextCommand struct {
	Base  BaseCommand
	Font  Font
	Pos   Vec2
	Color Color
	Str   string
}

type Vec2

type Vec2 struct {
	X, Y int
}

func NewVec2

func NewVec2(x, y int) Vec2

Jump to

Keyboard shortcuts

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