API 參考取自原始碼中的 Go 文件註解,以英文撰寫。
package mcpui
import "github.com/nsgo-dev/game/pkg/mcp/mcpui"概覽
Package mcpui is experimental: its API and documented formats may change in a minor release. See the API stability policy.
Package mcpui defines the optional runtime MCP contract for game UI trees: serializable widget records, a provider the game implements over its own widgets, and a lookup by widget path or label. It imports neither gameui nor pkg/mcp, so a game links it only when it opts in. Package game/mcp registers game_ui_tree and game_ui_activate over a Provider set as mcp.Config.UI.
索引
變數
var ErrAmbiguous = errors.New("mcpui: label matches more than one widget")ErrAmbiguous reports that a 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 ErrNotFound = errors.New("mcpui: widget not found")ErrNotFound reports that no widget matches the requested path or label.
函式
func Activate(p Provider, pathOrLabel string, action Action) errorActivate 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.
型別
type Action struct {
Kind string
Value string
}Action is what to do to a widget, such as Kind "press", "toggle", "focus" or "set" with Value holding the new value.
type Provider interface {
Snapshot() Snapshot
Activate(path string, action Action) error
}Provider is implemented by the game over its own widgets. Activate must run the widget's own handlers, so the same code runs as for a tap or a key press.
type Snapshot struct {
Widgets []Widget `json:"widgets"`
}Snapshot is the game's UI tree flattened in traversal order.
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"`
}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.