glmenu

package module
v0.0.0-...-61f1a0d Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2018 License: BSD-2-Clause Imports: 13 Imported by: 0

README

Modern opengl menus

  • Create simple menus.
  • OnHover/OnNotHover callbacks.
  • OnClick callback.
  • Alignment of labels. Default is centered.
  • Barebones at the moment.

Example

Screenshot depicting off center menu with right justification of the text.

Alt text

Documentation

Index

Constants

View Source
const (
	FormatableLabel   = 0
	FormatableTextbox = 1
)
View Source
const (
	MouseUnclicked MouseClick = iota
	MouseLeft
	MouseRight
	MouseCenter

	NavigationMouse Navigation = 0
	NavigationKey              = 1

	AlignCenter Alignment = 0
	AlignRight            = 1
	AlignLeft             = 2

	ScreenCenter      ScreenPosition = 0
	ScreenTopLeft                    = 1
	ScreenTopCenter                  = 2
	ScreenTopRight                   = 3
	ScreenLeft                       = 4
	ScreenRight                      = 5
	ScreenLowerLeft                  = 6
	ScreenLowerCenter                = 7
	ScreenLowerRight                 = 8

	ScreenPadding = float32(10) // used by screen positioning calculations
)

Variables

View Source
var IsDebug = false

Functions

func MenuDebug(message string)

Types

type Alignment

type Alignment int

type Formatable

type Formatable interface {
	// perform click action as appropriate
	// if formatable has no reasonable click action (TextBox) returns false
	Follow() bool
	// up/down key navigation
	NavigateTo()
	NavigateAway() bool
	// is the this object something that can be interacted with by the user
	IsNoop() bool
	// rendering
	GetPosition() mgl32.Vec2
	SetPosition(v mgl32.Vec2)
	GetPadding() Padding
	Height() float32
	Width() float32

	Type() FormatableType
}

type FormatableType

type FormatableType int

type Label

type Label struct {
	Config  LabelConfig
	Menu    *Menu
	Text    *v41.Text
	IsHover bool
	IsClick bool

	// public methods are expected to be defined by the user and run before the private method are called
	// if a public method is undefined, it is skipped.  Currently I have only defined onRelease as private.
	OnClick LabelInteraction

	StopOnRelease bool // set to true in order to prevent onRelease being called
	OnRelease     LabelInteraction
	OnHover       LabelInteraction
	OnNotHover    func()
	// contains filtered or unexported fields
}

func (*Label) Draw

func (label *Label) Draw()

func (*Label) Follow

func (label *Label) Follow() bool

func (*Label) GetPadding

func (label *Label) GetPadding() Padding

func (*Label) GetPosition

func (label *Label) GetPosition() mgl32.Vec2

func (*Label) Height

func (label *Label) Height() float32

func (*Label) InsidePoint

func (label *Label) InsidePoint() (P Point)

InsidePoint returns a point nearby the center of the label Used to locate a screen position where clicking can be simulated

func (*Label) IsClicked

func (label *Label) IsClicked(xPos, yPos float64, button MouseClick)

IsClicked uses a bounding box to determine clicks

func (*Label) IsHovered

func (label *Label) IsHovered(xPos, yPos float64)

IsHovered uses a bounding box

func (*Label) IsNoop

func (label *Label) IsNoop() bool

func (*Label) IsReleased

func (label *Label) IsReleased(xPos, yPos float64, button MouseClick)

IsReleased is checked for all labels in a menu when mouseup occurs

func (*Label) NavigateAway

func (label *Label) NavigateAway() bool

NavigateAway if we end up needing to navigate away from this item then let the caller know because it might need that information. return value of 'true'

func (*Label) NavigateTo

func (label *Label) NavigateTo()

func (*Label) OrthoToScreenCoord

func (label *Label) OrthoToScreenCoord() (X1 Point, X2 Point)

func (*Label) Reset

func (label *Label) Reset()

func (*Label) SetPosition

func (label *Label) SetPosition(v mgl32.Vec2)

func (*Label) SetString

func (label *Label) SetString(str string, argv ...interface{})

func (*Label) Type

func (label *Label) Type() FormatableType

func (*Label) Width

func (label *Label) Width() float32

type LabelAction

type LabelAction int
const (
	NOOP LabelAction = iota
	GOTO_MENU
	EXIT_MENU
	EXIT_GAME
)

type LabelConfig

type LabelConfig struct {
	Padding Padding
	Action  LabelAction
	Goto    string
}

type LabelInteraction

type LabelInteraction func(
	xPos, yPos float64,
	button MouseClick,
	isInBoundingBox bool,
)
type Menu struct {
	// parent MenuManager
	*MenuManager

	Name string

	// trigger
	OnShow         func()
	OnComplete     func()
	OnEnterRelease func()

	// options
	Defaults     MenuDefaults
	IsVisible    bool
	ShowOnKey    glfw.Key
	Height       float32
	Width        float32
	IsAutoCenter bool

	// interactive objects
	Font       *v41.Font
	Labels     []*Label
	TextBoxes  []*TextBox
	Formatable []Formatable // all labels and textboxes

	// Up/Down keypress -> NavigationVia set to "Key"
	// When in "Key", mouse navigation only happens once the mouse has been moved enough from LastMousePosition
	LastMousePosition mgl32.Vec2
	NavigationVia     Navigation
	NavigationIndex   int // once up/down arrows are pressed, determine which element needs to be entered/hovered over

	// increment during a scale operation
	TextScaleRate float32

	// opengl oriented
	ScreenPosition ScreenPosition

	Window       *glfw.Window
	WindowWidth  float32
	WindowHeight float32
	// contains filtered or unexported fields
}

func NewMenu

func NewMenu(window *glfw.Window, name string, font *v41.Font, defaults MenuDefaults, screenPosition ScreenPosition) (*Menu, error)

NewMenu creates a new menu object with a background centered on the screen or positioned using offsetBy

func (menu *Menu) Draw() bool
func (menu *Menu) Finalize(align Alignment)
func (menu *Menu) Hide()
func (menu *Menu) KeyRelease(key glfw.Key, withShift bool)
func (menu *Menu) MouseClick(xPos, yPos float64, button MouseClick)
func (menu *Menu) MouseHover(xPos, yPos float64)
func (menu *Menu) MouseRelease(xPos, yPos float64, button MouseClick)
func (menu *Menu) NewLabel(str string, config LabelConfig) *Label

NewLabel handles spacing layout as defined on the menu level

func (menu *Menu) NewMenuTexture(imagePath string, dimensions mgl32.Vec2) (mt *MenuTexture, err error)

NewMenuTexture accepts an image location and the internal dimensions of the smaller images embedded within the texture. The image will be subdivided into evenly spaced rectangles based on the dimensions given. This is very beta.

func (menu *Menu) NewTextBox(str string, width, height float32, borderWidth int32) *TextBox

NewTextBox handles vertical spacing

func (menu *Menu) Release()
func (menu *Menu) ResizeWindow(width float32, height float32)
func (menu *Menu) Show()
func (menu *Menu) Toggle()
type MenuDefaults struct {
	TextColor       mgl32.Vec3
	TextHover       mgl32.Vec3
	TextClick       mgl32.Vec3
	BackgroundColor mgl32.Vec4
	BorderColor     mgl32.Vec4
	Border          mgl32.Vec2
	Dimensions      mgl32.Vec2
	Padding         mgl32.Vec2
	HoverPadding    mgl32.Vec2
}
type MenuImage struct {
	MenuTexture      *MenuTexture
	MenuTextureIndex int

	// X1, X2: the lower left and upper right points of a box that bounds the text with a center point (0,0)
	// lower left
	X1 Point
	// upper right
	X2 Point

	// Screen position away from center
	Position mgl32.Vec2
	// contains filtered or unexported fields
}
type MenuLogger struct {
	*log.Logger
}

func NewMenuLogger

func NewMenuLogger(location string) (*MenuLogger, error)
func (nm *MenuLogger) Close() error
type MenuManager struct {
	Font        *v41.Font
	StartKey    glfw.Key // they key that, when pressed, will display the StartMenu
	StartMenu   string   // the name passed to each NewMenu call
	Menus       map[string]*Menu
	IsFinalized bool
}

func NewMenuManager

func NewMenuManager(font *v41.Font, startKey glfw.Key, startMenu string) *MenuManager

NewMenuManager handles a tree of menus that interact with one another

func (mm *MenuManager) Draw() bool
func (mm *MenuManager) Finalize(align Alignment) error

Finalize connects menus together and performs final formatting steps this must be run after all menus are prepared

func (mm *MenuManager) Hide()
func (mm *MenuManager) IsVisible() bool
func (mm *MenuManager) KeyRelease(key glfw.Key, withShift bool)
func (mm *MenuManager) MouseClick(xPos, yPos float64, button MouseClick)

Clicked resolves menus that have been clicked

func (mm *MenuManager) MouseHover(xPos, yPos float64)
func (mm *MenuManager) MouseRelease(xPos, yPos float64, button MouseClick)
func (mm *MenuManager) NewMenu(window *glfw.Window, name string, menuDefaults MenuDefaults, screenPosition ScreenPosition) (*Menu, error)
func (mm *MenuManager) Release()
func (mm *MenuManager) SetText(name string, index int, text string) error
func (mm *MenuManager) Show(name string) error
func (mm *MenuManager) Toggle(name string) error
type MenuTexture struct {
	Menu *Menu

	Dimensions mgl32.Vec2

	OrthographicMatrix mgl32.Mat4

	WindowWidth  float32
	WindowHeight float32

	Image *image.NRGBA
	// contains filtered or unexported fields
}
func (mt *MenuTexture) Release()
func (mt *MenuTexture) ResizeWindow(width float32, height float32)

type MouseClick

type MouseClick int
type Navigation int

type Padding

type Padding struct {
	X, Y float32
}

type Point

type Point struct {
	X, Y float32
}

type ScreenPosition

type ScreenPosition int

type TextBox

type TextBox struct {
	Menu               *Menu
	Text               *v41.Text
	Cursor             *v41.Text
	CursorIndex        int   // position of the cursor within the text
	CursorBarFrequency int64 // how long does each flash cycle last (visible -> invisible -> visible)
	MaxLength          int
	Time               time.Time
	IsEdit             bool
	IsClick            bool

	// user defined
	OnClick    TextBoxInteraction
	OnRelease  TextBoxInteraction
	FilterRune func(r rune) bool

	// X1, X2: the lower left and upper right points of a box that bounds the text
	X1          Point
	X2          Point
	BorderWidth int32

	Position mgl32.Vec2
	// contains filtered or unexported fields
}

func (*TextBox) Backspace

func (textbox *TextBox) Backspace()

func (*TextBox) Draw

func (textbox *TextBox) Draw()

func (*TextBox) Edit

func (textbox *TextBox) Edit(key glfw.Key, withShift bool)

func (*TextBox) Follow

func (textbox *TextBox) Follow() bool

func (*TextBox) GetBoundingBox

func (textbox *TextBox) GetBoundingBox() (X1, X2 Point)

func (*TextBox) GetPadding

func (textbox *TextBox) GetPadding() Padding

func (*TextBox) GetPosition

func (textbox *TextBox) GetPosition() mgl32.Vec2

func (*TextBox) Height

func (textbox *TextBox) Height() float32

func (*TextBox) ImmediateCursorDraw

func (textbox *TextBox) ImmediateCursorDraw()

func (*TextBox) InsidePoint

func (textbox *TextBox) InsidePoint() (P Point)

InsidePoint returns a point nearby the center of the label Used to locate a screen position where clicking can be simulated Click on the right side in order to place the cursor to the very right of any text

func (*TextBox) IsClicked

func (textbox *TextBox) IsClicked(xPos, yPos float64, button MouseClick)

func (*TextBox) IsNoop

func (textbox *TextBox) IsNoop() bool

func (*TextBox) IsReleased

func (textbox *TextBox) IsReleased(xPos, yPos float64, button MouseClick)

func (*TextBox) KeyRelease

func (textbox *TextBox) KeyRelease(key glfw.Key, withShift bool)

func (*TextBox) Load

func (textbox *TextBox) Load(menu *Menu, width, height float32, borderWidth int32) (err error)

func (*TextBox) MoveCursor

func (textbox *TextBox) MoveCursor(offset int)

func (*TextBox) NavigateAway

func (textbox *TextBox) NavigateAway() bool

func (*TextBox) NavigateTo

func (textbox *TextBox) NavigateTo()

func (*TextBox) OrthoToScreenCoord

func (textbox *TextBox) OrthoToScreenCoord() (X1 Point, X2 Point)

func (*TextBox) SetColor

func (textbox *TextBox) SetColor(color mgl32.Vec3)

func (*TextBox) SetPosition

func (textbox *TextBox) SetPosition(v mgl32.Vec2)

func (*TextBox) SetString

func (textbox *TextBox) SetString(str string, argv ...interface{})

func (*TextBox) Type

func (textbox *TextBox) Type() FormatableType

func (*TextBox) Width

func (textbox *TextBox) Width() float32

type TextBoxInteraction

type TextBoxInteraction func(
	textbox *TextBox,
	xPos, yPos float64,
	button MouseClick,
	isInBoundingBox bool,
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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