Documentation
¶
Overview ¶
Package mcpui is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package mcpui attaches optional game UI widget tree inspection and activation tools to a running game's *mcp.Bridge.
Call Attach once alongside mcp.Attach on the engine goroutine:
- game_ui_tree: walk a gameui.Root, gameui.BuiltScreen, gameui.SettingsMenu, or custom Provider, returning deterministic widget paths, IDs, kinds, labels, virtual and game-space rectangles, visibility, enabled, focus, and value state.
- game_ui_activate: drive a widget by path, ID, or unique label (press/click, toggle, set_value/slider, select, focus) directly through the widget's own handlers so pressing "Play" runs the exact same code as a tap or click.
Keeping this package separate from pkg/mcp lets games without UI avoid linking gameui into their MCP bridge. Under -tags release, Attach is a zero-cost no-op.
Index ¶
Examples ¶
Constants ¶
const ( // ToolUITree inspects the running game's UI widget tree. ToolUITree = "game_ui_tree" // ToolUIActivate activates, toggles, adjusts, selects, or focuses a UI widget through its own handler. ToolUIActivate = "game_ui_activate" )
Tool names registered on an *mcp.Bridge by Attach.
Variables ¶
var ErrAmbiguous = errors.New("mcpui: label matches more than one widget")
ErrAmbiguous reports that an ID or label matches more than one widget; address the widget by its path instead.
var ErrDisabled = errors.New("mcpui: widget disabled")
ErrDisabled reports that the matched widget is disabled and cannot be activated.
var ErrHidden = errors.New("mcpui: widget hidden")
ErrHidden reports that the matched widget (or an ancestor) is hidden and cannot be activated.
var ErrNotFound = errors.New("mcpui: widget not found")
ErrNotFound reports that no widget matches the requested path, ID, or label.
var ErrUnsupportedAction = errors.New("mcpui: unsupported action for widget")
ErrUnsupportedAction reports that the requested action is not supported by the target widget kind.
Functions ¶
func Activate ¶
Activate resolves pathOrLabel against the provider's current snapshot and passes the matched widget's path and the action to the provider. It returns ErrNotFound for a nil provider or no match and ErrDisabled for a disabled widget.
Example ¶
ExampleActivate finds and activates widgets by path or label.
package main
import (
"errors"
"fmt"
"github.com/nsgo-dev/game/pkg/mcp/mcpui"
)
// menu is a tiny in-memory Provider that records the actions it receives.
type menu struct{ widgets []mcpui.Widget }
func (m *menu) Snapshot() mcpui.Snapshot { return mcpui.Snapshot{Widgets: m.widgets} }
func (m *menu) Activate(path string, action mcpui.Action) error {
fmt.Printf("activate %s: %s\n", path, action.Kind)
return nil
}
// ExampleActivate finds and activates widgets by path or label.
func main() {
m := &menu{widgets: []mcpui.Widget{
{Path: "menu/play", Kind: "button", Label: "Play", Enabled: true},
{Path: "menu/options", Kind: "button", Label: "Options", Enabled: true},
{Path: "menu/continue", Kind: "button", Label: "Continue"},
}}
w, err := mcpui.Find(m.Snapshot(), "options")
fmt.Println(w.Path, err)
// Labels match case-insensitively; the provider receives the widget's path.
fmt.Println(mcpui.Activate(m, "play", mcpui.Action{Kind: "press"}))
err = mcpui.Activate(m, "Continue", mcpui.Action{Kind: "press"})
fmt.Println(errors.Is(err, mcpui.ErrDisabled), err)
err = mcpui.Activate(m, "Quit", mcpui.Action{Kind: "press"})
fmt.Println(errors.Is(err, mcpui.ErrNotFound))
}
Output: menu/options <nil> activate menu/play: press <nil> true mcpui: widget disabled: menu/continue true
Types ¶
type Action ¶
Action is what to do to a widget, such as Kind "press", "click", "toggle", "focus", "select", or "set" / "set_value" / "slider" with Value holding the new value.
type Config ¶
type Config struct {
// Root is the active gameui.Root for widget tree inspection and activation.
Root *gameui.Root
// RootFunc dynamically resolves the active gameui.Root on each tool call (for example when switching between title, gameplay HUD, and pause screens).
RootFunc func() *gameui.Root
// Screen is an active gameui.BuiltScreen built from a *.screen.yaml file; its Root, Navigator, and node IDs are inspected automatically.
Screen *gameui.BuiltScreen
// ScreenFunc dynamically resolves the active gameui.BuiltScreen on each tool call.
ScreenFunc func() *gameui.BuiltScreen
// Settings is an optional gameui.SettingsMenu whose Root and Navigator are exposed when active.
Settings *gameui.SettingsMenu
// SettingsFunc dynamically resolves the active gameui.SettingsMenu on each tool call.
SettingsFunc func() *gameui.SettingsMenu
Navigator *gameui.SpatialNavigator
NavigatorFunc func() *gameui.SpatialNavigator
// Viewport optionally maps virtual UI coordinates into game/window coordinates when Root.Viewport is nil. When nil, falls back to the attached Bridge's Viewport.
Viewport *screen.Viewport
// ViewportFunc dynamically resolves the active screen.Viewport on each tool call.
ViewportFunc func() screen.Viewport
// WidgetID optionally assigns stable IDs to widgets not built by BuiltScreen.
WidgetID func(gameui.Widget) string
// Provider optionally supplies a custom non-gameui UI tree and activation handler.
Provider Provider
}
Config names the gameui (or custom Provider) source exposed over MCP by Attach. Every field is optional; *Func getters override the corresponding static pointer when returning non-nil.
type Provider ¶
Provider is implemented by a game over custom widgets when not using gameui directly. Activate must run the widget's own handlers, so the same code runs as for a tap or a key press.
type Rect ¶
type Rect struct {
X float32 `json:"x"`
Y float32 `json:"y"`
Width float32 `json:"width"`
Height float32 `json:"height"`
}
Rect is a 2D axis-aligned rectangle in virtual or game/window coordinates.
type Snapshot ¶
type Snapshot struct {
Widgets []Widget `json:"widgets"`
}
Snapshot is the game's UI tree flattened in traversal order.
type Widget ¶
type Widget struct {
Path string `json:"path"`
Kind string `json:"kind,omitempty"`
Label string `json:"label,omitempty"`
X float32 `json:"x"`
Y float32 `json:"y"`
Width float32 `json:"width"`
Height float32 `json:"height"`
Enabled bool `json:"enabled"`
Focused bool `json:"focused"`
Value string `json:"value,omitempty"`
ID string `json:"id,omitempty"`
Parent string `json:"parent,omitempty"`
Visible bool `json:"visible"`
Focusable bool `json:"focusable,omitempty"`
Checked *bool `json:"checked,omitempty"`
Min *float32 `json:"min,omitempty"`
Max *float32 `json:"max,omitempty"`
Selected *int `json:"selected,omitempty"`
Items []string `json:"items,omitempty"`
GameRect *Rect `json:"gameRect,omitempty"`
}
Widget is one widget in a UI snapshot. Path is unique within a snapshot; X, Y, Width and Height are the widget's rectangle in the game's virtual coordinates.