uix

package
v0.0.0-...-79996cc Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2023 License: GPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrawCrosshair

func DrawCrosshair(e render.Engine, child ui.Widget, color render.Color, lengthPct int)

DrawCrosshair renders a crosshair on the screen. It appears while the mouse cursor is over the child widget and draws within the bounds of the child widget.

The lengthPct is an integer ranged 0 to 100 to be a percentage length of the crosshair. A value of zero will not draw anything and just return.

Types

type Actor

type Actor struct {
	Drawing     *doodads.Drawing
	Actor       *level.Actor
	Canvas      *Canvas
	LevelCanvas *Canvas // the parent Canvas of the level we reside in
	// contains filtered or unexported fields
}

Actor is an object that marries together the three things that make a Doodad instance "tick" while inside a Canvas:

  • uix.Actor is a doodads.Drawing so it fulfills doodads.Actor to be a dynamic object during gameplay.
  • It has a pointer to the level.Actor indicating its static level data as defined in the map: its spawn coordinate and configuration.
  • A uix.Canvas that can present the actor's graphics to the screen.

func NewActor

func NewActor(id string, levelActor *level.Actor, doodad *doodads.Doodad) *Actor

NewActor sets up a uix.Actor. If the id is blank, a new UUIDv4 is generated.

func (*Actor) AddAnimation

func (a *Actor) AddAnimation(name string, interval int64, layers []interface{}) error

AddAnimation installs a new animation into the scripting engine for this actor.

The layers can be an array of string names or integer indexes.

func (*Actor) AddItem

func (a *Actor) AddItem(itemName string, quantity int)

AddItem adds an item doodad to the actor's inventory. Item name is usually the doodad filename.

func (*Actor) ClearInventory

func (a *Actor) ClearInventory()

ClearInventory removes all items from the actor's inventory.

func (*Actor) Destroy

func (a *Actor) Destroy()

Destroy deletes the actor from the running level.

func (*Actor) Doodad

func (a *Actor) Doodad() *doodads.Doodad

Doodad offers access to the underlying Doodad object. Shortcut to the `.Drawing.Doodad` property path.

func (*Actor) Freeze

func (a *Actor) Freeze()

Freeze an actor. For the player character, this means arrow key inputs will stop moving the actor.

func (*Actor) GetBoundingRect

func (a *Actor) GetBoundingRect() render.Rect

GetBoundingRect gets the bounding box of the actor's doodad.

func (*Actor) GetOption

func (a *Actor) GetOption(name string) *level.Option

Get an option value from the actor. If the option is not configured, returns the default Doodad option, or nil if not there either.

func (*Actor) Grounded

func (a *Actor) Grounded() bool

Grounded returns if the actor is touching a floor.

func (*Actor) HasGravity

func (a *Actor) HasGravity() bool

HasGravity returns if gravity applies to the actor.

func (*Actor) HasInventory

func (a *Actor) HasInventory() bool

HasInventory returns if the actor is capable of carrying items.

func (*Actor) HasItem

func (a *Actor) HasItem(itemName string) int

HasItem checks the actor's inventory for the item and returns the quantity.

A return value of -1 means the item was not found. The value 0 indicates a key item (one with no quantity). Values >= 1 would be consumable items.

func (*Actor) Hide

func (a *Actor) Hide()

Hide makes the actor invisible.

func (*Actor) Hitbox

func (a *Actor) Hitbox() render.Rect

Hitbox returns the actor's elected hitbox. If the JavaScript did not set a hitbox, it defers to the Doodad's metadata hitbox.

func (*Actor) ID

func (a *Actor) ID() string

ID returns the actor's ID. This is the underlying doodle.Drawing.ID().

func (*Actor) Inventory

func (a *Actor) Inventory() map[string]int

Inventory returns a copy of the actor's inventory struct.

func (*Actor) Invulnerable

func (a *Actor) Invulnerable() bool

Invulnerable returns whether the actor is marked as immortal.

func (*Actor) IsAnimating

func (a *Actor) IsAnimating() bool

IsAnimating returns if the current actor is playing an animation.

func (*Actor) IsFrozen

func (a *Actor) IsFrozen() bool

IsFrozen returns true if the actor is frozen.

func (*Actor) IsMobile

func (a *Actor) IsMobile() bool

IsMobile returns whether the actor is a mobile character.

func (*Actor) IsOnScreen

func (a *Actor) IsOnScreen() bool

IsOnScreen checks whether the actor's visual sprite box is on-screen in the level viewport.

func (*Actor) IsPlayer

func (a *Actor) IsPlayer() bool

IsPlayer returns whether the actor is the player character. It's true when the Actor ID is "PLAYER"

func (*Actor) IsWet

func (a *Actor) IsWet() bool

Wet returns whether the actor is in contact with water pixels in a level.

func (*Actor) LayerCount

func (a *Actor) LayerCount() int

LayerCount returns the number of layers in this actor's drawing.

func (*Actor) ListItems

func (a *Actor) ListItems() []string

ListItems returns a sorted list of the items in the actor's inventory.

func (*Actor) MoveBy

func (a *Actor) MoveBy(p render.Point)

MoveBy adjusts the actor's position.

func (*Actor) MoveTo

func (a *Actor) MoveTo(p render.Point)

MoveTo sets the actor's position.

func (*Actor) Options

func (a *Actor) Options() []string

Options returns the list of all available Doodad options, sorted.

func (*Actor) PlayAnimation

func (a *Actor) PlayAnimation(name string, callback goja.Value) error

PlayAnimation starts an animation and then calls a JavaScript function when the last frame has played out. Set a null function to ignore the callback.

func (*Actor) Position

func (a *Actor) Position() render.Point

Position returns the actor's position.

func (*Actor) RemoveItem

func (a *Actor) RemoveItem(itemName string, quantity int) bool

RemoveItem removes a quantity of an item from the actor's inventory.

Provide a quantity of 0 to remove the item completely. Otherwise provides a number greater than zero and you will subtract this quantity from the item. If the item then is at <= zero, it is removed from inventory.

func (*Actor) SetGravity

func (a *Actor) SetGravity(v bool)

SetGravity configures whether the actor is affected by gravity.

func (*Actor) SetGrounded

func (a *Actor) SetGrounded(v bool)

SetGrounded sets the actor's grounded value. If true, also sets their Y velocity to zero.

func (*Actor) SetHitbox

func (a *Actor) SetHitbox(x, y, w, h int)

SetHitbox sets the actor's elected hitbox.

func (*Actor) SetInventory

func (a *Actor) SetInventory(v bool)

SetInventory configures whether the actor is capable of carrying items.

func (*Actor) SetInvulnerable

func (a *Actor) SetInvulnerable(v bool)

SetInvulnerable sets the actor's immortal flag.

func (*Actor) SetMobile

func (a *Actor) SetMobile(v bool)

SetMobile configures whether the actor is a mobile character (i.e. is the player or a mobile enemy). Mobile characters can set off certain traps when touched but non-mobile actors don't set each other off if touching.

func (*Actor) SetNoclip

func (a *Actor) SetNoclip(v bool)

SetNoclip sets the noclip setting for an actor. If true, the actor can clip through level geometry.

func (*Actor) SetUsing

func (a *Actor) SetUsing(v bool)

SetUsing enables the "Use Key" flag, mainly for the player character to activate certain doodads in the level.

func (*Actor) SetVelocity

func (a *Actor) SetVelocity(v physics.Vector)

SetVelocity updates the actor's velocity vector.

func (*Actor) SetWet

func (a *Actor) SetWet(v bool)

SetWet updates the state of the actor's wet-ness.

func (*Actor) Show

func (a *Actor) Show()

Show a hidden actor.

func (*Actor) ShowLayer

func (a *Actor) ShowLayer(index int) error

ShowLayer sets the actor's ActiveLayer to the index given.

func (*Actor) ShowLayerNamed

func (a *Actor) ShowLayerNamed(name string) error

ShowLayerNamed sets the actor's ActiveLayer to the one named.

func (*Actor) Size

func (a *Actor) Size() render.Rect

Size returns the size of the actor, from the underlying doodads.Drawing.

func (*Actor) StopAnimation

func (a *Actor) StopAnimation()

StopAnimation stops any current animations.

func (*Actor) TickAnimation

func (a *Actor) TickAnimation(an *Animation) bool

TickAnimation advances an animation forward.

This method is called by canvas.Loop() only when the actor is currently `animating` and their current animation's nextFrameAt has been reached by the current time.Now().

Returns true when the animation has finished and false if there is still more frames left to animate.

func (*Actor) Unfreeze

func (a *Actor) Unfreeze()

Unfreeze an actor.

func (*Actor) Velocity

func (a *Actor) Velocity() physics.Vector

Velocity returns the actor's current velocity vector.

type Animation

type Animation struct {
	Name     string
	Interval time.Duration
	Layers   []int
	// contains filtered or unexported fields
}

Animation holds a named animation for a doodad script.

type Canvas

type Canvas struct {
	ui.Frame
	Palette *level.Palette

	// Editable and Scrollable go hand in hand and, if you initialize a
	// NewCanvas() with editable=true, they are both enabled.
	Editable   bool // Clicking will edit pixels of this canvas.
	Scrollable bool // Cursor keys will scroll the viewport of this canvas.
	Zoom       int  // Zoom level on the canvas.

	// Set this if your Canvas is a small fixed size (e.g. in doodad dropper),
	// so that doodads will crop their texture (if chunk size larger than your
	// Canvas) as to not overflow the canvas bounds. Not needed for Level canvases.
	CroppedSize bool

	// Toogle for doodad canvases in the Level Editor to show their buttons.
	ShowDoodadButtons bool

	OnDoodadConfig func(*Actor)

	// Custom label to place in the lower-right corner of the canvas.
	// Used for e.g. the quantity badge on Inventory items.
	CornerLabel string

	// Selected draw tool/mode, default Pencil, for editable canvases.
	Tool      drawtool.Tool
	BrushSize int // thickness of selected brush

	// MaskColor will force every pixel to render as this color regardless of
	// the palette index of that pixel. Otherwise pixels behave the same and
	// the palette does work as normal. Set to render.Invisible (zero value)
	// to remove the mask.
	MaskColor render.Color

	// Actor ID to follow the camera on automatically, i.e. the main player.
	FollowActor string

	// Debug tools
	// NoLimitScroll suppresses the scroll limit for bounded levels.
	NoLimitScroll bool

	// Show custom mouse cursors over this canvas (eg. editor tools)
	FancyCursors bool

	// PlayScene can set whether the Levelpack has a valid signature on it, so that
	// in InstallActors() we can allow a levelpack to load attached doodads on free
	// mode for their levels.
	IsSignedLevelPack *levelpack.LevelPack // filled in ONLY IF SIGNED.

	// When the Canvas wants to delete Actors, but ultimately it is upstream
	// that controls the actors. Upstream should delete them and then reinstall
	// the actor list from scratch.
	OnDeleteActors func([]*Actor)
	OnDragStart    func(*level.Actor)

	// -- WHEN Canvas.Tool is "Link" --
	// When the Canvas wants to link two actors together. Arguments are the IDs
	// of the two actors.
	OnLinkActors func(a, b *Actor)

	// Collision handlers for level geometry.
	OnLevelCollision func(*Actor, *collision.Collide)

	// Handler when a doodad script called Actors.SetPlayerCharacter.
	// The filename.doodad is given.
	OnSetPlayerCharacter func(filename string)

	// Handler for when a doodad script calls Level.ResetTimer().
	OnResetTimer func()

	// We inherit the ui.Widget which manages the width and height.
	Scroll render.Point // Scroll offset for which parts of canvas are visible.
	// contains filtered or unexported fields
}

Canvas is a custom ui.Widget that manages a single drawing.

func NewCanvas

func NewCanvas(size uint8, editable bool) *Canvas

NewCanvas initializes a Canvas widget.

size is the Chunker size (uint8)

If editable is true, Scrollable is also set to true, which means the arrow keys will scroll the canvas viewport which is desirable in Edit Mode.

func (*Canvas) Actors

func (w *Canvas) Actors() []*Actor

Actors returns the list of actors currently in the Canvas.

func (*Canvas) AddActor

func (w *Canvas) AddActor(actor *Actor) error

AddActor injects additional actors into the canvas, such as a Player doodad.

func (*Canvas) AddStroke

func (w *Canvas) AddStroke(stroke *drawtool.Stroke)

AddStroke installs a new Stroke to be superimposed over drawing data in the canvas.

The stroke is added to the canvas's map by its ID so it can be removed later. The stroke must have a non-zero ID value set or this function will panic. drawtool.NewStroke() creates an initialized Stroke object to use here.

func (*Canvas) Chunker

func (w *Canvas) Chunker() *level.Chunker

Chunker returns the underlying Chunker object.

func (*Canvas) ClearActors

func (w *Canvas) ClearActors()

ClearActors removes all the actors from the Canvas.

func (*Canvas) Compute

func (w *Canvas) Compute(e render.Engine)

Compute the canvas.

func (*Canvas) Destroy

func (w *Canvas) Destroy()

Destroy the canvas.

This function satisfies the ui.Widget interface but it also calls Teardown() methods on the level or doodad as well as any level actors, which frees up SDL2 texture memory.

Note: the rest of the data can be garbage collected by Go normally, the textures are able to regenerate themselves again if needed.

func (*Canvas) DrawStrokes

func (w *Canvas) DrawStrokes(e render.Engine, strokes []*drawtool.Stroke)

DrawStrokes is the common base function behind presentStrokes and presentActorLinks to actually draw the lines to the canvas.

func (*Canvas) GetCanvasesByActorName

func (c *Canvas) GetCanvasesByActorName(filename string) []*Canvas

GetCanvasesByActorName searches a (level) canvas's installed actors and returns any of them having this Title or Filename, with filename being more precise.

func (*Canvas) GetLinkedActors

func (w *Canvas) GetLinkedActors(a *Actor) []*Actor

GetLinkedActors returns the live Actor instances (Play Mode) which are linked to the live actor given.

func (*Canvas) GetZoomMultiplier

func (w *Canvas) GetZoomMultiplier() float64

GetZoomMultiplier parses the .Zoom integer and returns a multiplier.

Examples:

Zoom = 0: neutral (100% scale, 1x)
Zoom = 1: 2x zoom
Zoom = 2: 4x zoom
Zoom = 3: 8x zoom
Zoom = -1: 0.5x zoom
Zoom = -2: 0.25x zoom

func (*Canvas) InstallActors

func (w *Canvas) InstallActors(actors level.ActorMap) error

InstallActors adds external Actors to the canvas to be superimposed on top of the drawing.

func (*Canvas) InstallScripts

func (w *Canvas) InstallScripts() error

InstallScripts loads all the current actors' scripts into the scripting engine supervisor.

func (*Canvas) IsCursorOver

func (w *Canvas) IsCursorOver() bool

IsCursorOver returns true if the mouse cursor is physically over top of the canvas's widget space.

func (*Canvas) LinkAdd

func (w *Canvas) LinkAdd(a *Actor) error

LinkAdd adds an actor to be linked in the Link tool.

func (*Canvas) Load

func (w *Canvas) Load(p *level.Palette, g *level.Chunker)

Load initializes the Canvas using an existing Palette and Grid.

func (*Canvas) LoadDoodad

func (w *Canvas) LoadDoodad(d *doodads.Doodad)

LoadDoodad initializes a Canvas from a Doodad object.

func (*Canvas) LoadDoodadToLayer

func (w *Canvas) LoadDoodadToLayer(d *doodads.Doodad, index int)

LoadDoodadToLayer initializes a Canvas from a Doodad object and picks a layer to load.

func (*Canvas) LoadLevel

func (w *Canvas) LoadLevel(level *level.Level)

LoadLevel initializes a Canvas from a Level object.

func (*Canvas) LoadUnloadChunks

func (w *Canvas) LoadUnloadChunks(force ...bool)

LoadUnloadChunks optimizes memory for (level) canvases by warming up chunk images that fall within the LoadingViewport and freeing chunks that are outside of it.

func (*Canvas) LoadUnloadMetrics

func (w *Canvas) LoadUnloadMetrics() (inside, outside int)

LoadUnloadMetrics returns the canvas's stored metrics from the LoadUnloadChunks function, for the debug overlay.

func (*Canvas) LoadingViewport

func (w *Canvas) LoadingViewport() render.Rect

LoadingViewport is the viewport of chunks that ought to be preloaded and ready to display soon. It is the Viewport of chunks on screen + a margin of neighboring chunks outside the screen.

For memory optimization, chunks falling inside this viewport have their Go image.Image rendered and cached ready to convert to an SDL2 Texture when they come on screen. Chunks outside of the LoadingViewport can be unloaded (textures and images freed) to keep memory consumption on large levels under control.

func (*Canvas) Loop

func (w *Canvas) Loop(ev *event.State) error

Loop is called on the scene's event loop to handle mouse interaction with the canvas, i.e. to edit it.

func (*Canvas) MakeScriptAPI

func (w *Canvas) MakeScriptAPI(vm *scripting.VM)

MakeScriptAPI makes several useful globals available to doodad scripts such as Actors.At()

func (*Canvas) MakeSelfAPI

func (w *Canvas) MakeSelfAPI(actor *Actor) map[string]interface{}

MakeSelfAPI generates the `Self` object for the scripting API in reference to a live Canvas actor in the level.

func (*Canvas) Modified

func (w *Canvas) Modified() bool

Modified returns whether the canvas has been modified since it was last loaded. Methods like Load and LoadFile will set modified to false, and commitStroke sets it to true.

func (*Canvas) Present

func (w *Canvas) Present(e render.Engine, p render.Point)

Present the canvas.

func (*Canvas) PresentWallpaper

func (w *Canvas) PresentWallpaper(e render.Engine, p render.Point) error

PresentWallpaper draws the wallpaper. Point p is the one given to Canvas.Present(), i.e., the position of the top-left corner of the Canvas widget relative to the application window.

func (w *Canvas) PrintLinks()

PrintLinks is a debug function that describes all actor links to console.

func (*Canvas) RedoStroke

func (w *Canvas) RedoStroke() bool

RedoStroke rolls the level's UndoHistory forwards again and replays the recently undone changes.

func (*Canvas) RemoveActor

func (w *Canvas) RemoveActor(actor *Actor)

RemoveActor removes the actor from the canvas.

func (*Canvas) RemoveStroke

func (w *Canvas) RemoveStroke(stroke *drawtool.Stroke) bool

RemoveStroke uninstalls a Stroke from the canvas using its ID.

Returns true if the stroke existed to begin with, false if not.

func (*Canvas) ScrollBy

func (w *Canvas) ScrollBy(by render.Point)

ScrollBy adjusts the viewport scroll position.

func (*Canvas) ScrollTo

func (w *Canvas) ScrollTo(to render.Point)

ScrollTo sets the viewport scroll position.

func (*Canvas) SetModified

func (w *Canvas) SetModified(v bool)

SetModified sets the modified bit on the canvas.

func (*Canvas) SetScriptSupervisor

func (w *Canvas) SetScriptSupervisor(s *scripting.Supervisor)

SetScriptSupervisor assigns the Canvas scripting supervisor to enable interaction with actor scripts.

func (*Canvas) SetSwatch

func (w *Canvas) SetSwatch(s *level.Swatch)

SetSwatch changes the currently selected swatch for editing.

func (*Canvas) UndoStroke

func (w *Canvas) UndoStroke() bool

UndoStroke rolls back the level's UndoHistory and deletes the pixels last added to the level. Returns false and emits a warning to the log if the canvas has no level loaded properly.

func (*Canvas) Viewport

func (w *Canvas) Viewport() render.Rect

Viewport returns a rect containing the viewable drawing coordinates in this canvas. The X,Y values are the scroll offset (top left) and the W,H values are the scroll offset plus the width/height of the Canvas widget.

The Viewport rect are the Absolute World Coordinates of the drawing that are visible inside the Canvas. The X,Y is the top left World Coordinate and the W,H are the bottom right World Coordinate, making this rect an absolute slice of the world. For a normal rect with a relative width and height, use ViewportRelative().

The rect X,Y are the negative Scroll Value. The rect W,H are the Canvas widget size minus the Scroll Value.

func (*Canvas) ViewportRelative

func (w *Canvas) ViewportRelative() render.Rect

ViewportRelative returns a relative viewport where the Width and Height values are zero-relative: so you can use it with point.Inside(viewport) to see if a World Index point should be visible on screen.

The rect X,Y are the negative Scroll Value The rect W,H are the Canvas widget size.

func (*Canvas) WorldIndexAt

func (w *Canvas) WorldIndexAt(screenPixel render.Point) render.Point

WorldIndexAt returns the World Index that corresponds to a Screen Pixel on the screen. If the screen pixel is the mouse coordinate (relative to the application window) this will return the World Index of the pixel below the mouse cursor.

func (*Canvas) ZoomDivide

func (w *Canvas) ZoomDivide(value int) int

ZoomDivide divides an integer by the zoom inversely.

The algo is: int(float64(value) * divider)

Where the divider is a map of:

w.Zoom => divider
-2 => 4
-1 => 2
0  => 1
1  => 0.675*
2  => 0.5
3  => 0.404*

The 0.675 and 0.404 numbers I don't understand but were discovered the hard way when the 1.5x and 2.5x zoom levels were coming out jank. Expected to be 0.25 and 0.75.

func (*Canvas) ZoomMultiply

func (w *Canvas) ZoomMultiply(value int) int

ZoomMultiply multiplies a width or height value by the Zoom Multiplier and returns the modified integer.

Usage is like:

// when building a render.Rect destination box.
dest.W *= ZoomMultiply(dest.W)
dest.H *= ZoomMultiply(dest.H)

func (*Canvas) ZoomStroke

func (w *Canvas) ZoomStroke(stroke *drawtool.Stroke) *drawtool.Stroke

ZoomStroke adjusts a drawn stroke on the canvas to account for the zoom level.

Returns a copy Stroke value without changing the original.

type CollideEvent

type CollideEvent struct {
	Actor    *Actor
	Overlap  render.Rect
	InHitbox bool // If the two elected hitboxes are overlapping
	Settled  bool // Movement phase finished, actor script can fire actions
}

CollideEvent holds data sent to an actor's Collide handler.

type Crosshair

type Crosshair struct {
	LengthPct float64 // between 0 and 1
	Widget    ui.Widget
}

func NewCrosshair

func NewCrosshair(child ui.Widget, length float64) *Crosshair

type UseEvent

type UseEvent struct {
	Actor *Actor
}

UseEvent holds data sent to an actor's OnUse handler.

type Wallpaper

type Wallpaper struct {

	// Pointer to the Wallpaper datum.
	WP *wallpaper.Wallpaper
	// contains filtered or unexported fields
}

Wallpaper configures the wallpaper in a Canvas.

func (*Wallpaper) Load

func (wp *Wallpaper) Load(pageType level.PageType, v *wallpaper.Wallpaper) error

Load the wallpaper settings from a level.

func (*Wallpaper) Valid

func (wp *Wallpaper) Valid() bool

Valid returns whether the Wallpaper is configured. Only Levels should have wallpapers and Doodads will have nil ones.

Directories

Path Synopsis
Package magicform helps create simple form layouts with go/ui.
Package magicform helps create simple form layouts with go/ui.

Jump to

Keyboard shortcuts

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