keymap

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package keymap lays out an on-screen control-hint bar — the "key: action" row the family's apps show along the bottom of the window. It is display-free: it formats and positions the hints and returns them for the caller to draw with its own text face, wrapping to the window width using a caller-supplied width measurement so the layout is identical whatever font the app renders in.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rows added in v0.15.0

func Rows(bindings []Binding, avail int, measure func(string) int) []string

Rows formats bindings as "key: action", joins them with " | ", and wraps them into rows no wider than avail measured by measure. It is BottomBar's layout without its placement, for hints that live somewhere the bottom-left corner is not — inside a stats panel, beside a simulation, as a menu subtitle. The caller positions the rows itself. An empty bindings slice returns nil.

Types

type Binding

type Binding struct {
	// Key names the key or combination, e.g. "space", "+/-", "drag/arrows".
	Key string
	// Action describes what the key does, e.g. "pause", "new track".
	Action string
}

Binding is one control hint: a key or key combination and the action it triggers.

func (Binding) Label

func (b Binding) Label() string

Label renders the binding as "key: action".

type Face

type Face struct {
	// LineHeight is the vertical distance between wrapped rows, in pixels.
	LineHeight int
	// Measure returns the pixel width of s in the caller's font. Wrapping uses
	// it so a row never exceeds the available width.
	Measure func(s string) int
}

Face describes the caller's text metrics so the bar wraps and stacks correctly in whatever font the app draws with.

type Line

type Line struct {
	X, Y int
	Text string
}

Line is one laid-out row of the bar: the text to draw and where to draw it. X and Y are the row's top-left origin, matching the position most text calls expect; add your font's ascent if yours draws from the baseline instead.

func BottomBar

func BottomBar(bindings []Binding, w, h, pad int, f Face) []Line

BottomBar lays bindings out as a control bar anchored to the bottom-left of a w×h screen. It formats each binding as "key: action", joins them with " | ", wraps to the width f allows, and stacks the wrapped rows so the last sits pad above the bottom edge. Rows are returned top-to-bottom; an empty bindings slice returns nil.

Example
package main

import (
	"fmt"

	"github.com/danielriddell21/crucible/keymap"
)

func main() {
	bindings := []keymap.Binding{
		{Key: "space", Action: "pause"},
		{Key: "+/-", Action: "speed"},
		{Key: "r", Action: "restart"},
	}
	// A fixed 6-pixel-per-character face, like the built-in debug font.
	face := keymap.Face{LineHeight: 16, Measure: func(s string) int { return len(s) * 6 }}
	for _, line := range keymap.BottomBar(bindings, 640, 360, 8, face) {
		fmt.Printf("(%d,%d) %s\n", line.X, line.Y, line.Text)
	}
}
Output:
(8,336) space: pause | +/-: speed | r: restart

func CenterPrompt added in v0.13.0

func CenterPrompt(b Binding, w, h int, f Face) Line

CenterPrompt lays out a single binding as a contextual prompt, centred horizontally and sitting in the lower third of a w×h screen. Games use it for "look at a door → E: open" hints that appear only while an interactive object is in focus. The returned Line shares the top-left origin convention of BottomBar.

Jump to

Keyboard shortcuts

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