naiad

package module
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: LGPL-3.0 Imports: 6 Imported by: 0

README

naiad

Naiad logo

Naiad lets you create windows with shapes in them. It is designed for creating graphical user interfaces. It wraps pixel and provides a simple way to create and manipulate persistent shapes on screen. It is as cross platform as pixel is, which is to say, very. This library is not designed to be used in conjunction with pixel, it abstracts it away. Eventually it will just use raw OpenGL as a backend.

The reason this exists is because the browser DOM is very useful for building user interfaces because you can just make things and the browser abstracts away all the rendering logic. However, it is inherently resource intensive, has too much vestigial nonsense built into it, and it wasn't even designed for constructing user interfaces (being more oriented towards text documents).

Naiad is designed to let you have your cake and eat it too. You have full freedom to create whatever ridiculous shapes you want, with the ease of working with a DOM, and the assurance that your program will not end up being a 127 megabyte, cpu eating, ram eating piece of garbage.

Disclaimer

It is not done yet dont use it.

Diagram that looks more complicated than it actually is

On-screen shapes in Naiad are structured using groups. These groups contain paths (and soon text as well). At the top there is a window, which is a shape as well, and it acts as a root group. You can push, pop, lift, etc. from the window as you would a normal group.

         ┌────────────────┐
         │ Window         │
         │ ┌────────────┐ │
         │ │ Root Group │ │
         └─┴────┬───┬───┴─┘
                │   │
                │   └──────┐
                │          ▼
            ┌───┴───┐  ┌───────┐
┌─────┬─────┤ Group │  │ Group ├──────┐
│     │     └───┬───┘  └───┬───┘      ▼
│     ▼         │          │      ┌───────┐
│ ┌──────┐      ▼          ▼      │ Path  │
│ │ Path │  ┌───────┐  ┌──────┐   └───────┘
│ └──────┘  │ Group │  │ Path │
│           └───┬───┘  └──────┘
│ ┌──────┐      │
└►│ Path │      ▼
  └──────┘  ┌──────┐
            │ Path │
            └──────┘

Paths, in turn, are just lists of points. Each point contains information like position, color, and line cap type. The entire path contains stuff like line thickness, and if it should be filled or stroked.

Point     Point
  ┌─┬─────┬─┐
  ├─┘     └─┤
  │  Path   │
  ├─┐     ┌─┤
  └─┴─────┴─┘
Point     Point

Groups are very cool because they each have their own internal buffer, so objects inside of them don't need to redraw themselves all the dang time.

Epic checklist

  • Paths
  • Shape Groups
  • Calculate mouse click, drag, and hover information in relation to shapes
  • Pass keyboard input as well
  • Text
  • Stop using pixel as a backend
  • Texture paths, text, etc with images
  • Make these images work as mini raster graphics contexts

Documentation

Overview

Naiad lets you create windows with shapes in them. It is designed for creating graphical user interfaces. It wraps pixel and provides a simple way to create and manipulate persistent shapes on screen. It is as cross platform as pixel is, which is to say, very. This library is not designed to be used in conjunction with pixel, it abstracts it away. Eventually it will just use raw OpenGL as a backend.

The reason this exists is because the browser DOM is very useful for building user interfaces because you can just make things and the browser abstracts away all the rendering logic. However, it is inherently resource intensive, has too much vestigial nonsense built into it, and it wasn't even designed for constructing user interfaces (being more oriented towards text documents).

Naiad is designed to let you have your cake and eat it too. You have full freedom to create whatever ridiculous shapes you want, with the ease of working with a DOM, and the assurance that your program will not end up being a 127 megabyte, cpu eating, ram eating piece of garbage.

On-screen shapes in Naiad are structured using groups. These groups contain paths (and soon text as well). At the top there is a root group which you are not allowed to mess with, but you can add things to it and remove things from it through methods defined on the window object.

Paths, in turn, are just lists of points. Each point contains information like position, color, and line cap type. The entire path contains stuff like line thickness, and if it should be filled or stroked.

Index

Constants

View Source
const (
	KeyUnknown      = Key(pixelgl.KeyUnknown)
	KeySpace        = Key(pixelgl.KeySpace)
	KeyApostrophe   = Key(pixelgl.KeyApostrophe)
	KeyComma        = Key(pixelgl.KeyComma)
	KeyMinus        = Key(pixelgl.KeyMinus)
	KeyPeriod       = Key(pixelgl.KeyPeriod)
	KeySlash        = Key(pixelgl.KeySlash)
	Key0            = Key(pixelgl.Key0)
	Key1            = Key(pixelgl.Key1)
	Key2            = Key(pixelgl.Key2)
	Key3            = Key(pixelgl.Key3)
	Key4            = Key(pixelgl.Key4)
	Key5            = Key(pixelgl.Key5)
	Key6            = Key(pixelgl.Key6)
	Key7            = Key(pixelgl.Key7)
	Key8            = Key(pixelgl.Key8)
	Key9            = Key(pixelgl.Key9)
	KeySemicolon    = Key(pixelgl.KeySemicolon)
	KeyEqual        = Key(pixelgl.KeyEqual)
	KeyA            = Key(pixelgl.KeyA)
	KeyB            = Key(pixelgl.KeyB)
	KeyC            = Key(pixelgl.KeyC)
	KeyD            = Key(pixelgl.KeyD)
	KeyE            = Key(pixelgl.KeyE)
	KeyF            = Key(pixelgl.KeyF)
	KeyG            = Key(pixelgl.KeyG)
	KeyH            = Key(pixelgl.KeyH)
	KeyI            = Key(pixelgl.KeyI)
	KeyJ            = Key(pixelgl.KeyJ)
	KeyK            = Key(pixelgl.KeyK)
	KeyL            = Key(pixelgl.KeyL)
	KeyM            = Key(pixelgl.KeyM)
	KeyN            = Key(pixelgl.KeyN)
	KeyO            = Key(pixelgl.KeyO)
	KeyP            = Key(pixelgl.KeyP)
	KeyQ            = Key(pixelgl.KeyQ)
	KeyR            = Key(pixelgl.KeyR)
	KeyS            = Key(pixelgl.KeyS)
	KeyT            = Key(pixelgl.KeyT)
	KeyU            = Key(pixelgl.KeyU)
	KeyV            = Key(pixelgl.KeyV)
	KeyW            = Key(pixelgl.KeyW)
	KeyX            = Key(pixelgl.KeyX)
	KeyY            = Key(pixelgl.KeyY)
	KeyZ            = Key(pixelgl.KeyZ)
	KeyLeftBracket  = Key(pixelgl.KeyLeftBracket)
	KeyBackslash    = Key(pixelgl.KeyBackslash)
	KeyRightBracket = Key(pixelgl.KeyRightBracket)
	KeyGraveAccent  = Key(pixelgl.KeyGraveAccent)
	KeyWorld1       = Key(pixelgl.KeyWorld1)
	KeyWorld2       = Key(pixelgl.KeyWorld2)
	KeyEscape       = Key(pixelgl.KeyEscape)
	KeyEnter        = Key(pixelgl.KeyEnter)
	KeyTab          = Key(pixelgl.KeyTab)
	KeyBackspace    = Key(pixelgl.KeyBackspace)
	KeyInsert       = Key(pixelgl.KeyInsert)
	KeyDelete       = Key(pixelgl.KeyDelete)
	KeyRight        = Key(pixelgl.KeyRight)
	KeyLeft         = Key(pixelgl.KeyLeft)
	KeyDown         = Key(pixelgl.KeyDown)
	KeyUp           = Key(pixelgl.KeyUp)
	KeyPageUp       = Key(pixelgl.KeyPageUp)
	KeyPageDown     = Key(pixelgl.KeyPageDown)
	KeyHome         = Key(pixelgl.KeyHome)
	KeyEnd          = Key(pixelgl.KeyEnd)
	KeyCapsLock     = Key(pixelgl.KeyCapsLock)
	KeyScrollLock   = Key(pixelgl.KeyScrollLock)
	KeyNumLock      = Key(pixelgl.KeyNumLock)
	KeyPrintScreen  = Key(pixelgl.KeyPrintScreen)
	KeyPause        = Key(pixelgl.KeyPause)
	KeyF1           = Key(pixelgl.KeyF1)
	KeyF2           = Key(pixelgl.KeyF2)
	KeyF3           = Key(pixelgl.KeyF3)
	KeyF4           = Key(pixelgl.KeyF4)
	KeyF5           = Key(pixelgl.KeyF5)
	KeyF6           = Key(pixelgl.KeyF6)
	KeyF7           = Key(pixelgl.KeyF7)
	KeyF8           = Key(pixelgl.KeyF8)
	KeyF9           = Key(pixelgl.KeyF9)
	KeyF10          = Key(pixelgl.KeyF10)
	KeyF11          = Key(pixelgl.KeyF11)
	KeyF12          = Key(pixelgl.KeyF12)
	KeyF13          = Key(pixelgl.KeyF13)
	KeyF14          = Key(pixelgl.KeyF14)
	KeyF15          = Key(pixelgl.KeyF15)
	KeyF16          = Key(pixelgl.KeyF16)
	KeyF17          = Key(pixelgl.KeyF17)
	KeyF18          = Key(pixelgl.KeyF18)
	KeyF19          = Key(pixelgl.KeyF19)
	KeyF20          = Key(pixelgl.KeyF20)
	KeyF21          = Key(pixelgl.KeyF21)
	KeyF22          = Key(pixelgl.KeyF22)
	KeyF23          = Key(pixelgl.KeyF23)
	KeyF24          = Key(pixelgl.KeyF24)
	KeyF25          = Key(pixelgl.KeyF25)
	KeyKP0          = Key(pixelgl.KeyKP0)
	KeyKP1          = Key(pixelgl.KeyKP1)
	KeyKP2          = Key(pixelgl.KeyKP2)
	KeyKP3          = Key(pixelgl.KeyKP3)
	KeyKP4          = Key(pixelgl.KeyKP4)
	KeyKP5          = Key(pixelgl.KeyKP5)
	KeyKP6          = Key(pixelgl.KeyKP6)
	KeyKP7          = Key(pixelgl.KeyKP7)
	KeyKP8          = Key(pixelgl.KeyKP8)
	KeyKP9          = Key(pixelgl.KeyKP9)
	KeyKPDecimal    = Key(pixelgl.KeyKPDecimal)
	KeyKPDivide     = Key(pixelgl.KeyKPDivide)
	KeyKPMultiply   = Key(pixelgl.KeyKPMultiply)
	KeyKPSubtract   = Key(pixelgl.KeyKPSubtract)
	KeyKPAdd        = Key(pixelgl.KeyKPAdd)
	KeyKPEnter      = Key(pixelgl.KeyKPEnter)
	KeyKPEqual      = Key(pixelgl.KeyKPEqual)
	KeyLeftShift    = Key(pixelgl.KeyLeftShift)
	KeyLeftControl  = Key(pixelgl.KeyLeftControl)
	KeyLeftAlt      = Key(pixelgl.KeyLeftAlt)
	KeyLeftSuper    = Key(pixelgl.KeyLeftSuper)
	KeyRightShift   = Key(pixelgl.KeyRightShift)
	KeyRightControl = Key(pixelgl.KeyRightControl)
	KeyRightAlt     = Key(pixelgl.KeyRightAlt)
	KeyRightSuper   = Key(pixelgl.KeyRightSuper)
	KeyMenu         = Key(pixelgl.KeyMenu)
	KeyLast         = Key(pixelgl.KeyLast)
)

Variables

This section is empty.

Functions

func Run

func Run(callback func())

Run starts naiad. All graphics and window manipulation needs to be performed within the callback function.

Types

type Key

type Key pixelgl.Button

Key represents a keyboard key.

type LineCap

type LineCap int

LineCap determines how corners and ends of lines should look. Each point has one of these values.

const (
	LineCapNone LineCap = iota
	LineCapSharp
	LineCapRound
)

type Point

type Point struct {
	Vector
	// contains filtered or unexported fields
}

Point represents a single point on a path. It has information about its position, color, and line cap type.

func P

func P(x, y float64, color color.Color, cap LineCap) (point Point)

P provides a quick way to create a point given a position, color, and line cap type.

type Shape

type Shape interface {

	// Kind returns what kind of shape it is.
	Kind() (kind ShapeKind)

	// Dirty returns wether the shape is dirty or not.
	Dirty() (isDirty bool)

	// SetDirty causes the shape to be flagged as dirty.
	SetDirty()

	// SetClean causes the shape to be flagged as clean.
	SetClean()

	// Bounds returns the bounds of the shape, relative to its origin. This
	// is usually in the top left, which means min will usually be (0, 0).
	// Hovever, in shapes such as paths, the min bound may be in a different
	// spot due to things such as stroke thickness and points with negative
	// coordinates.
	Bounds() (min, max Vector)

	// Contains takes in mouse coordinates, and recursively checks if those
	// coordinates are contained in the shape or any of its children. It
	// returns a slice of all shapes (including itself) that the points are
	// contained within. If the position is not inside the shape, it should
	// immediately stop, return nil, and not recurse into anything else.
	Contains(position Vector) (shapes []Shape)
	// contains filtered or unexported methods
}

Shape is an interface representing an on-screen shape. For a shape to be inserted into naiad's shape hierarchy, it must support these behaviors.

type ShapeGroup

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

ShapeGroup is a group containing other shapes. It has its own internal buffer that the shapes inside of it will be drawn to, reducing the need for redraws. It should be used to group objects composed of several different shapes together, especially if they move together.

func NewShapeGroup

func NewShapeGroup(
	x, y float64,
	w, h float64,
) (
	group *ShapeGroup,
)

NewShapeGroup creates a new shape group with the specified position and bounds.

func (*ShapeGroup) Bounds

func (base *ShapeGroup) Bounds() (min, max Vector)

Bounds returns the bounds of the shape, relative to its origin. This is usually in the top left, which means min will usually be (0, 0). Hovever, in shapes such as paths, the min bound may be in a different spot due to things such as stroke thickness and points with negative coordinates.

func (*ShapeGroup) Contains

func (group *ShapeGroup) Contains(position Vector) (shapes []Shape)

Contains takes in mouse coordinates, and determines if they are inside of the shape. If it is, the returned slice will have one item pointing to this shape. If not, the returned slice will be nil.

func (*ShapeGroup) Dirty

func (base *ShapeGroup) Dirty() (isDirty bool)

Dirty returns wether the shape is dirty or not.

func (*ShapeGroup) Kind

func (group *ShapeGroup) Kind() (kind ShapeKind)

Kind returns ShapeKindGroup.

func (*ShapeGroup) Lift

func (group *ShapeGroup) Lift(shape Shape) (found bool)

Lift removes the specified shape from the group. If it was found in the group, it returns true. If it was not found, it returns false.

func (*ShapeGroup) Pop

func (group *ShapeGroup) Pop() (shape Shape)

Pop removes the top-most shape from the shape group, and returns it.

func (*ShapeGroup) PopBottom

func (group *ShapeGroup) PopBottom() (shape Shape)

PopBottom removes the bottom-most shape from the shape group, and returns it.

func (*ShapeGroup) Push

func (group *ShapeGroup) Push(shape Shape)

Push adds a new shape to the top of the shape group.

func (*ShapeGroup) PushBottom

func (group *ShapeGroup) PushBottom(shape Shape)

PushBottom inserts a shape at the bottom of the shape group.

func (*ShapeGroup) SetBounds

func (group *ShapeGroup) SetBounds(max Vector)

SetBounds sets the size of the shape group.

func (*ShapeGroup) SetClean

func (base *ShapeGroup) SetClean()

SetClean causes the shape to be flagged as clean.

func (*ShapeGroup) SetDirty

func (base *ShapeGroup) SetDirty()

SetDirty causes the shape to be flagged as dirty.

func (*ShapeGroup) SetPosition

func (base *ShapeGroup) SetPosition(position Vector)

SetPosition sets the position of the shape.

func (*ShapeGroup) SetX

func (base *ShapeGroup) SetX(x float64)

SetX sets the x position of the shape.

func (*ShapeGroup) SetY

func (base *ShapeGroup) SetY(y float64)

SetY sets the y position of the shape.

func (*ShapeGroup) X

func (base *ShapeGroup) X() (x float64)

X returns the x position of the shape.

func (*ShapeGroup) Y

func (base *ShapeGroup) Y() (y float64)

Y returns the y position of the shape.

type ShapeKind

type ShapeKind int

ShapeKind determines what sort of shape a Shape should be cast to. It can be retrieved by calling the Kind() method.

const (
	ShapeKindPath ShapeKind = iota
	ShapeKindText
	ShapeKindGroup
)

type ShapePath

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

ShapePath is a polygon (or line) composed of a set of points.

func NewShapePath

func NewShapePath(
	x, y float64,
) (
	shape *ShapePath,
)

NewShapePath creates a new path at the given x and y coordinates.

func (*ShapePath) Bounds

func (base *ShapePath) Bounds() (min, max Vector)

Bounds returns the bounds of the shape, relative to its origin. This is usually in the top left, which means min will usually be (0, 0). Hovever, in shapes such as paths, the min bound may be in a different spot due to things such as stroke thickness and points with negative coordinates.

func (*ShapePath) Contains

func (shape *ShapePath) Contains(position Vector) (shapes []Shape)

Contains takes in mouse coordinates, and determines if they are inside of the shape. If it is, the returned slice will have one item pointing to this shape. If not, the returned slice will be nil.

func (*ShapePath) Dirty

func (base *ShapePath) Dirty() (isDirty bool)

Dirty returns wether the shape is dirty or not.

func (*ShapePath) Kind

func (shape *ShapePath) Kind() (kind ShapeKind)

Kind returns ShapeKindPath.

func (*ShapePath) Open

func (shape *ShapePath) Open() (closed bool)

Open returns whether or not the shape is open on one end.

func (*ShapePath) Pop

func (shape *ShapePath) Pop() (point Point)

Pop removes the last point from the shape and returns it.

func (*ShapePath) Push

func (shape *ShapePath) Push(point Point)

Push adds a point to the shape.

func (*ShapePath) SetClean

func (base *ShapePath) SetClean()

SetClean causes the shape to be flagged as clean.

func (*ShapePath) SetDirty

func (base *ShapePath) SetDirty()

SetDirty causes the shape to be flagged as dirty.

func (*ShapePath) SetOpen

func (shape *ShapePath) SetOpen(open bool)

SetOpen sets whether or not the shape is open on one end. Set this to false to create a polyline as opposed to a polygon.

func (*ShapePath) SetPosition

func (base *ShapePath) SetPosition(position Vector)

SetPosition sets the position of the shape.

func (*ShapePath) SetThickness

func (shape *ShapePath) SetThickness(thickness float64)

SetThickness sets the stroke thickness of the shape. If the thickness is 0, the shape will be filled instead.

func (*ShapePath) SetX

func (base *ShapePath) SetX(x float64)

SetX sets the x position of the shape.

func (*ShapePath) SetY

func (base *ShapePath) SetY(y float64)

SetY sets the y position of the shape.

func (*ShapePath) Thickness

func (shape *ShapePath) Thickness() (thickness float64)

Thickness returns the stroke thickness of the shape.

func (*ShapePath) X

func (base *ShapePath) X() (x float64)

X returns the x position of the shape.

func (*ShapePath) Y

func (base *ShapePath) Y() (y float64)

Y returns the y position of the shape.

type Vector

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

Vector represents a coordinate pair. It can be used to specify things such as bounds, or 2D coordinates.

func V

func V(x, y float64) (vector Vector)

V is a quick way to create a new vector out of x and y coordinates.

func (*Vector) SetX

func (vector *Vector) SetX(x float64)

SetX sets the x value of the vector.

func (*Vector) SetY

func (vector *Vector) SetY(y float64)

SetY sets the y value of the vector.

func (Vector) X

func (vector Vector) X() (x float64)

X returns the x value of the vector.

func (Vector) Y

func (vector Vector) Y() (y float64)

Y returns thw x value of the vector.

type Window

type Window struct {
	ShapeGroup
	// contains filtered or unexported fields
}

Window represents an on-screen window.

func (*Window) Await

func (window *Window) Await(timeout time.Duration)

Await waits for an event to occur, or the timeout to elapse. It then redraws the screen if needed. If the timeout is zero, there won't actually be a timeout and it will just wait forever for an event.

func (*Window) Bounds

func (window *Window) Bounds() (min, max Vector)

Bounds returns the dimensions of the window as a vector.

func (*Window) Closed

func (window *Window) Closed() (closed bool)

Closed returns. whether the window is closed

func (*Window) Dirty

func (base *Window) Dirty() (isDirty bool)

Dirty returns wether the shape is dirty or not.

func (*Window) IsHovered

func (window *Window) IsHovered(shape Shape) (hovered bool)

IsHovered returns whether or not the specified shape is being hovered over by the mouse.

func (*Window) IsLeftClicked

func (window *Window) IsLeftClicked(shape Shape) (clicked bool)

IsLeftClicked returns whether or not the specified shape was just clicked with the left mouse button.

func (*Window) IsLeftHeld

func (window *Window) IsLeftHeld(shape Shape) (held bool)

IsLeftHeld returns whether or not the specified shape is being held by the left mouse button.

func (*Window) JustPressed

func (window *Window) JustPressed(key Key) (pressed bool)

JustPressed returns whether a key has just been pressed down.

func (*Window) JustReleased

func (window *Window) JustReleased(key Key) (released bool)

JustReleased returns whether a key has just been released.

func (*Window) MouseHover

func (window *Window) MouseHover() (hoveredShapes []Shape)

MouseHover returns a list of all shapes the mouse is hovering over. The first shape is the least specific, and the last shape is the most specific.

func (*Window) MouseLeftClick

func (window *Window) MouseLeftClick() (clickedShapes []Shape)

MouseLeftClicked returns a list of alll shapes that were clicked with the left mouse button. This emulates behavior found in most other programs, where for an object to be clicked the mouse must be held down in that object, and then released in that object.

func (*Window) MouseLeftHold

func (window *Window) MouseLeftHold() (heldShapes []Shape)

MouseLeftHold returns a list of all shapes that were under the mouse when its left button started being held down. If the mouse's left button is not currently being held down, this function returns nil.

func (*Window) MousePosition

func (window *Window) MousePosition() (position Vector)

MousePosition returns the current position of the mouse inside the window bounds.

func (*Window) MousePreviousPosition

func (window *Window) MousePreviousPosition() (previousPosition Vector)

MousePreviousPosition returns the position of the mouse inside the window bounds the previous time window.Await or window.Poll etc. was called.

func (*Window) Open

func (window *Window) Open() (err error)

Open brings the window on screen. If the window has already been opened, this method does nothing.

func (*Window) Poll

func (window *Window) Poll()

Poll polls events, and redraws the screen if needed. This is non-blocking.

func (*Window) Pressed

func (window *Window) Pressed(key Key) (pressed bool)

Pressed returns whether a key is currently being pressed.

func (*Window) Repeated

func (window *Window) Repeated(key Key) (repeated bool)

Repeated returns whether a repeat event is being triggered for a key (when it has been held down).

func (*Window) SetBounds

func (window *Window) SetBounds(size Vector)

SetBounds sets the size of the window to the dimensions specified by a vector.

func (*Window) SetClean

func (base *Window) SetClean()

SetClean causes the shape to be flagged as clean.

func (*Window) SetDirty

func (base *Window) SetDirty()

SetDirty causes the shape to be flagged as dirty.

func (*Window) SetIcon

func (window *Window) SetIcon(icon ...image.Image)

SetIcon takes in different resolutions of the same icon (all images) and sets the window icon. This will not do anything after open has been called.

func (*Window) SetPosition

func (base *Window) SetPosition(position Vector)

SetPosition sets the position of the shape.

func (*Window) SetTitle

func (window *Window) SetTitle(title string)

SetTitle sets the window title.

func (*Window) SetTransparent

func (window *Window) SetTransparent(transparent bool)

SetTransparent sets whether or not the window has a transparent framebuffer, if supported.

func (*Window) SetX

func (base *Window) SetX(x float64)

SetX sets the x position of the shape.

func (*Window) SetY

func (base *Window) SetY(y float64)

SetY sets the y position of the shape.

func (*Window) Typed

func (window *Window) Typed() (text string)

Typed returns the string of characters typed since the window input was last updated.

func (*Window) X

func (base *Window) X() (x float64)

X returns the x position of the shape.

func (*Window) Y

func (base *Window) Y() (y float64)

Y returns the y position of the shape.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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