Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs(ui): toggle/dropdown as data + two-way binding in the components guide (bilingual)

esenginec6ff0abad1e8
diff --git a/docs/astro/src/content/docs/guides/ui-components.mdx b/docs/astro/src/content/docs/guides/ui-components.mdx
index 7cdbc91c9..34b9c3c14 100644
--- a/docs/astro/src/content/docs/guides/ui-components.mdx
+++ b/docs/astro/src/content/docs/guides/ui-components.mdx
@@ -84,7 +84,6 @@ const toggle = createToggle({
});
toggle.setIsOn(false); // programmatic change (fires onChange)
-toggle.setIsOn(true, true); // silent — skips onChange
```
| Option | Description |
@@ -94,7 +93,14 @@ toggle.setIsOn(true, true); // silent — skips onChange
| `isOn` | Initial state. |
| `onChange(isOn, entity)` | Fires on toggle. |
-**Handle:** `isOn()`, `setIsOn(value, silent?)`, `setDisabled(disabled)`, `entity`, `dispose()`.
+**Handle:** `isOn()`, `setIsOn(value)`, `setDisabled(disabled)`, `entity`, `dispose()`.
+
+<Aside type="note">
+ State lives in a **`UIToggle` component**; the behavior system flips it on click
+ and is the single writer of the check visual — `setIsOn`, a binding, or the
+ editor inspector on a placed prefab all flip the indicator and fire `change`
+ identically.
+</Aside>
## Slider
@@ -202,16 +208,20 @@ createDropdown({
| `options` | The list of selectable values. |
| `selectedIndex` | Initial selection. |
| `optionToLabel(option, i)` | Map a non-string option to its display text. |
-| `buttonStates` / `optionStates` | Colors for the main button and the option rows. |
+| `buttonStates` | Colors for the main button (option rows use theme roles). |
| `optionHeight` | Row height in pixels (default `32`). |
+| `disabled` / `focusable` / `tabIndex` | Interactivity + keyboard reachability. |
| `onSelect(index, option, entity)` | Fires on selection. |
-**Handle:** `getSelected()`, `getSelectedIndex()`, `setSelectedIndex(i, silent?)`,
+**Handle:** `getSelected()`, `getSelectedIndex()`, `setSelectedIndex(i)`,
`open()`, `close()`, `isOpen()`, `dispose()`.
<Aside type="note">
- Click-outside-to-close isn't wired — call `close()` from your own global input layer
- if you need it.
+ Labels + selection live in a **`UIDropdown` component**; the behavior system owns
+ the popup (click toggles it, **a click anywhere else closes it**), row selection,
+ and **arrow-key stepping** while the closed control is focused — so a
+ prefab-placed dropdown is fully functional without code. The popup is rebuilt on
+ each open with fresh theme roles.
</Aside>
## Dialog
@@ -379,6 +389,26 @@ Click (or Tab to) the field to focus it; **Escape** blurs, **Enter** fires
hidden native textarea for IME support) — it's inert in the editor's edit view.
</Aside>
+## Data binding
+
+Widget values are plain component fields, so the [signal](/docs/guides/ui/) layer
+binds to them directly — and `bindWidgetValue` makes it **two-way**: the signal
+drives the component, and the widget's `change` event writes back.
+
+```typescript
+import { signal, bindWidgetValue, UISlider, UIToggle } from 'esengine';
+
+const volume = signal(50);
+bindWidgetValue(world, events, slider.entity, UISlider, 'value', volume);
+const muted = signal(false);
+bindWidgetValue(world, events, toggle.entity, UIToggle, 'isOn', muted);
+
+volume.set(80); // moves the slider
+// dragging the slider ↦ volume.get() follows
+```
+
+The signal's equality guard breaks the loop, so a round trip settles in one hop.
+
## Drag & drop
Add `Draggable` to any interactable UI entity and pointer drags move it — its
diff --git a/docs/astro/src/content/docs/zh-cn/guides/ui-components.mdx b/docs/astro/src/content/docs/zh-cn/guides/ui-components.mdx
index 41c474569..32beb6ded 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/ui-components.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/ui-components.mdx
@@ -78,7 +78,6 @@ const toggle = createToggle({
});
toggle.setIsOn(false); // 程序化改变(触发 onChange)
-toggle.setIsOn(true, true); // 静默——跳过 onChange
```
| 选项 | 说明 |
@@ -88,7 +87,12 @@ toggle.setIsOn(true, true); // 静默——跳过 onChange
| `isOn` | 初始状态。 |
| `onChange(isOn, entity)` | 切换时触发。 |
-**句柄:** `isOn()`、`setIsOn(value, silent?)`、`setDisabled(disabled)`、`entity`、`dispose()`。
+**句柄:** `isOn()`、`setIsOn(value)`、`setDisabled(disabled)`、`entity`、`dispose()`。
+
+<Aside type="note">
+ 状态存在 **`UIToggle` 组件**里;行为系统在点击时翻转它,并且是勾选视觉的唯一写者——
+ `setIsOn`、绑定、或编辑器 Inspector 改摆放的 prefab,都会同样地翻转指示器并触发 `change`。
+</Aside>
## Slider 滑块
@@ -193,15 +197,18 @@ createDropdown({
| `options` | 可选值列表。 |
| `selectedIndex` | 初始选择。 |
| `optionToLabel(option, i)` | 把非字符串选项映射为显示文本。 |
-| `buttonStates` / `optionStates` | 主按钮和选项行的颜色。 |
+| `buttonStates` | 主按钮的颜色(选项行走主题角色)。 |
| `optionHeight` | 行高(像素,默认 `32`)。 |
+| `disabled` / `focusable` / `tabIndex` | 交互性与键盘可达性。 |
| `onSelect(index, option, entity)` | 选择时触发。 |
-**句柄:** `getSelected()`、`getSelectedIndex()`、`setSelectedIndex(i, silent?)`、`open()`、
+**句柄:** `getSelected()`、`getSelectedIndex()`、`setSelectedIndex(i)`、`open()`、
`close()`、`isOpen()`、`dispose()`。
<Aside type="note">
- 点击外部关闭没有接线——需要的话从你自己的全局输入层调 `close()`。
+ 标签与选择存在 **`UIDropdown` 组件**里;行为系统拥有弹出层(点击开合,**点击其他任意位置
+ 自动关闭**)、行选择、以及闭合聚焦时的**方向键换选**——摆出来的 prefab 无需代码即完整可用。
+ 弹出层每次打开都用新鲜的主题角色重建。
</Aside>
## Dialog 对话框
@@ -359,6 +366,25 @@ events.on(field, 'submit', () => save(world.get(field, TextInput).value)); //
textarea 以支持 IME)——在编辑器编辑态下不生效。
</Aside>
+## 数据绑定
+
+控件的值就是普通组件字段,[signal](/docs/zh-cn/guides/ui/) 层可以直接绑——
+`bindWidgetValue` 让它**双向**:signal 驱动组件,控件的 `change` 事件回写 signal。
+
+```typescript
+import { signal, bindWidgetValue, UISlider, UIToggle } from 'esengine';
+
+const volume = signal(50);
+bindWidgetValue(world, events, slider.entity, UISlider, 'value', volume);
+const muted = signal(false);
+bindWidgetValue(world, events, toggle.entity, UIToggle, 'isOn', muted);
+
+volume.set(80); // 滑块随之移动
+// 拖动滑块 ↦ volume.get() 跟随
+```
+
+signal 的等值守卫会打断回环,一个来回一跳即收敛。
+
## 拖放
给任何可交互的 UI 实体加 `Draggable`,指针拖动就能移动它——有 `UINode` 时移动