nksdl

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 7 Imported by: 0

README

go-nksdl

A bridge between go-nk and go-sdl2, using the SDL2 renderer. See cmd/demo/main.go for example usage.

Documentation

Overview

Package nksdl provides integration between Nuklear and SDL2 via the github.com/kbolino/go-nk and github.com/veandco/go-sdl2 modules.

Index

Constants

This section is empty.

Variables

View Source
var DefaultBindings = map[KeyInput]KeyAction{
	{Code: sdl.K_LSHIFT}:                                 {Key1: nk.KeyShift},
	{Code: sdl.K_RSHIFT}:                                 {Key1: nk.KeyShift},
	{Code: sdl.K_RETURN}:                                 {Key1: nk.KeyEnter},
	{Code: sdl.K_TAB}:                                    {Key1: nk.KeyTab},
	{Code: sdl.K_BACKSPACE}:                              {Key1: nk.KeyBackspace},
	{Code: sdl.K_HOME}:                                   {Key1: nk.KeyTextStart, Key2: nk.KeyScrollStart},
	{Code: sdl.K_END}:                                    {Key1: nk.KeyTextEnd, Key2: nk.KeyScrollEnd},
	{Code: sdl.K_PAGEUP}:                                 {Key1: nk.KeyScrollUp},
	{Code: sdl.K_PAGEDOWN}:                               {Key2: nk.KeyScrollDown},
	{Code: sdl.K_UP}:                                     {Key1: nk.KeyUp},
	{Code: sdl.K_DOWN}:                                   {Key1: nk.KeyDown},
	{Code: sdl.K_LEFT}:                                   {Key1: nk.KeyLeft},
	{Code: sdl.K_RIGHT}:                                  {Key1: nk.KeyRight},
	{Code: sdl.K_c, Mod: sdl.KMOD_CTRL}:                  {Key1: nk.KeyCopy},
	{Code: sdl.K_x, Mod: sdl.KMOD_CTRL}:                  {Key1: nk.KeyCut},
	{Code: sdl.K_v, Mod: sdl.KMOD_CTRL}:                  {Key1: nk.KeyPaste},
	{Code: sdl.K_a, Mod: sdl.KMOD_CTRL}:                  {Key1: nk.KeyTextLineStart},
	{Code: sdl.K_e, Mod: sdl.KMOD_CTRL}:                  {Key1: nk.KeyTextLineEnd},
	{Code: sdl.K_z, Mod: sdl.KMOD_CTRL}:                  {Key1: nk.KeyTextUndo},
	{Code: sdl.K_z, Mod: sdl.KMOD_CTRL | sdl.KMOD_SHIFT}: {Key1: nk.KeyTextRedo},
}
View Source
var ErrQuit = errQuit{}

ErrQuit is an error sentinel value used to indicate that the application should quit.

Functions

This section is empty.

Types

type ConvertOpts

type ConvertOpts struct {
	GlobalAlpha        float32
	LineAA, ShapeAA    nk.AntiAliasing
	CircleSegmentCount uint32
	CurveSegmentCount  uint32
	ArcSegmentCount    uint32
}

ConvertOpts contains options used by DefaultNkContext.CreateConvertConfig.

type DefaultNkDriver

type DefaultNkDriver struct {
	// Font contains options for creating fonts.
	Font FontOpts
	// Convert contains options for creating vertex buffer conversion
	// configurations.
	Convert ConvertOpts
}

DefaultNkDriver is the default implementation of NkDriver. It can be used directly as an NkDriver or extended to patch/override its behavior.

func (*DefaultNkDriver) CreateContext

func (d *DefaultNkDriver) CreateContext() (*nk.Context, error)

func (*DefaultNkDriver) CreateConvertConfig

func (d *DefaultNkDriver) CreateConvertConfig(
	vertexLayout []nk.DrawVertexLayoutElement,
	vertexSize, vertexAlignment uint32,
	null nk.DrawNullTexture,
) *nk.ConvertConfig

func (*DefaultNkDriver) CreateFont

func (d *DefaultNkDriver) CreateFont(atlas *nk.FontAtlas, scale float32) (*nk.Font, error)

func (*DefaultNkDriver) CreateFontAtlas

func (d *DefaultNkDriver) CreateFontAtlas() (*nk.FontAtlas, error)

type DefaultSDLDriver

type DefaultSDLDriver struct {
	// InitFlags contains flags to pass to sdl.Init.
	InitFlags uint32
	// Hints contains hint keys and values to pass to sdl.SetHint.
	Hints map[string]string
	// Window contains options for creating the window.
	Window WindowOpts
	// Render contains options for creating the renderer.
	Render RenderOpts
}

DefaultSDLDriver is the default implementation of SDLDriver. It can be used directly as an SDLDriver, or extended to patch/override its behavior.

func (*DefaultSDLDriver) CreateRenderer

func (d *DefaultSDLDriver) CreateRenderer(window *sdl.Window) (*sdl.Renderer, error)

func (*DefaultSDLDriver) CreateWindow

func (d *DefaultSDLDriver) CreateWindow() (*sdl.Window, error)

func (*DefaultSDLDriver) InitSDL

func (d *DefaultSDLDriver) InitSDL() error

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver is the main focus of NkSDL. It is used to manage SDL resources and Nuklear resources through initial setup, per-frame rendering, and pre-quit teardown. Driver delegates SDL-related setup to SDLDriver and Nuklear-related setup to NkDriver.

func NewDriver

func NewDriver(
	sdlDriver SDLDriver,
	nkDriver NkDriver,
	bindings map[KeyInput]KeyAction,
	eventListener EventListener,
) *Driver

NewDriver creates a new Driver from the given parameters. The sdlDriver and nkDriver must not be nil, or else NewDriver will panic. The bindings map is used to map keys to Nuklear actions. The eventListener is optional, but if non-nil will be called for every SDL event after it is handled by Nuklear.

func (*Driver) BGColor added in v0.2.0

func (d *Driver) BGColor() sdl.Color

func (*Driver) Context

func (d *Driver) Context() *nk.Context

func (*Driver) Destroy

func (d *Driver) Destroy() (err error)

Destroy fress resources used by the Driver. Destroy should be called once in the lifetime of a Driver, after the last call to FrameEnd.

func (*Driver) FrameEnd added in v0.2.0

func (d *Driver) FrameEnd() (err error)

FrameEnd performs late frame actions, including converting UI draw commands to vertex buffer draw commands, passing the vertex buffers to the renderer, and presenting the renderer. FrameEnd should be called once at the end of every frame.

func (*Driver) FrameStart added in v0.2.0

func (d *Driver) FrameStart() error

FrameStart performs early frame actions, including polling for events, mapping input events to actions, as well as scaling and clearing the renderer. FrameStart should be called once at the beginning of every frame.

func (*Driver) Init

func (d *Driver) Init() error

Init initializes the Driver, creating the SDL window and renderer as well as the Nuklear context and fonts. Init should be called once in the lifetime of a Driver, before any calls to FrameStart.

func (*Driver) RenderScale added in v0.3.0

func (d *Driver) RenderScale() float32

func (*Driver) Renderer

func (d *Driver) Renderer() *sdl.Renderer

func (*Driver) SetBGColor

func (d *Driver) SetBGColor(color sdl.Color)

SetBGColor sets the desired background color when clearing the renderer.

func (*Driver) SetRenderScale added in v0.3.0

func (d *Driver) SetRenderScale(renderScale float32) error

SetRenderScale sets the desired rendering scale. To compute the scale automatically (e.g. on a high-DPI display), use a renderScale of 0.

func (*Driver) Window

func (d *Driver) Window() *sdl.Window

type EventHandler

type EventHandler struct {
	// contains filtered or unexported fields
}

EventHandler is used to handle input events from SDL and report them to an nk.Context.

func NewEventHandler

func NewEventHandler(bindings map[KeyInput]KeyAction) EventHandler

NewEventHandler creates a new EventHandler from the given bindings. The map will be copied, and any generic modifier key bindings will be expanded into left and right key bindings. For example, if there is a binding with Ctrl modifier set it will be expanded into separate bindings to the same action for both LCtrl and RCtrl instead. NewEventHandler panics if the resulting bindings conflict.

func (EventHandler) HandleEvent

func (h EventHandler) HandleEvent(nkc *nk.Context, event sdl.Event) (EventType, bool)

HandleEvent handles the given event, reporting its actions to nkc, using the defined bindings. The return value indicates the type of the event and whether the event was used at all.

type EventListener

type EventListener func(event sdl.Event, eventType EventType, usedByNuklear bool) error

EventListener is the function signature for the optional event listener, which is called after Nuklear handles an event. See the EventHandler type for a description of the other parameters.

type EventType

type EventType int32

EventType represents the type of a handled event. This is not an exhaustive list of event types, and only represents those that are recognized by EventHandler, with a placeholder (EventTypeUnhandled) for all other types.

const (
	EventTypeUnhandled EventType = iota
	EventTypeQuit
	EventTypeInputMotion
	EventTypeInputButton
	EventTypeInputScroll
	EventTypeInputKey
	EventTypeInputUnicode
)

type FontOpts

type FontOpts struct {
	Path string
	Size float32
}

FontOpts contains options used by DefaultNkContext.CreateFont.

type KeyAction

type KeyAction struct {
	Key1 nk.Key
	Key2 nk.Key
}

KeyAction represents the action(s) to be taken when key input is received. Key2 is optional; if only a single action is needed, leave Key2 unset or set it to KeyNone.

type KeyInput

type KeyInput struct {
	Code sdl.Keycode
	Mod  sdl.Keymod
}

KeyInput is the reduced form of sdl.Keysym containing only the keycode and modifiers, used to match input events.

func KeysymInput

func KeysymInput(sym sdl.Keysym) KeyInput

KeysymInput converts sym to KeyInput.

func (KeyInput) String

func (ki KeyInput) String() string

String calls ToString("GUI").

func (KeyInput) ToString

func (ki KeyInput) ToString(guiKeyName string) string

ToString returns the string representation of ki, with the given name for the "GUI" key (aka "Meta", "Win", "Super", "Cmd").

type NkDriver

type NkDriver interface {
	CreateContext() (*nk.Context, error)
	CreateFontAtlas() (*nk.FontAtlas, error)
	CreateFont(atlas *nk.FontAtlas, scale float32) (*nk.Font, error)
	CreateConvertConfig(
		vertexLayout []nk.DrawVertexLayoutElement,
		vertexSize, vertexAlignment uint32,
		null nk.DrawNullTexture,
	) *nk.ConvertConfig
}

NkDriver is implemented by any type capable of initializing Nuklear and its core resources.

type RenderOpts

type RenderOpts struct {
	Drivers []string
	Flags   uint32
}

RenderOpts sets options for DefaultSDLDriver.CreateRenderer.

type SDLDriver

type SDLDriver interface {
	InitSDL() error
	CreateWindow() (*sdl.Window, error)
	CreateRenderer(window *sdl.Window) (*sdl.Renderer, error)
}

SDLDriver is implemented by any type capable of initializing SDL and creating its core resources.

type WindowOpts

type WindowOpts struct {
	Title         string
	PosX, PosY    int32
	Width, Height int32
	Flags         uint32
}

WindowOpts sets options for DefaultSDLDriver.CreateWindow.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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