接入 Hud 输入提示并完善 InputModule 提示映射
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using SepCore.InputModule;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public static class InputPromptTextUtility
|
||||
{
|
||||
public static string BuildTmpText(InputPrompt prompt)
|
||||
{
|
||||
string spriteText = BuildSpriteTags(prompt.SpriteName);
|
||||
if (string.IsNullOrEmpty(spriteText))
|
||||
{
|
||||
return prompt.TextLabel;
|
||||
}
|
||||
|
||||
return string.IsNullOrEmpty(prompt.TextLabel)
|
||||
? spriteText
|
||||
: $"{spriteText} {prompt.TextLabel}";
|
||||
}
|
||||
|
||||
public static string BuildSpriteTags(string spriteNames)
|
||||
{
|
||||
if (string.IsNullOrEmpty(spriteNames))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
string[] names = spriteNames.Split('|');
|
||||
string result = string.Empty;
|
||||
for (int i = 0; i < names.Length; i++)
|
||||
{
|
||||
string name = names[i].Trim();
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
result += " ";
|
||||
}
|
||||
|
||||
result += $"<sprite name=\"{name}\">";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 314a19cffa89e7a448d576d26d8787bf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"reference": "GUID:73a59a40d41e9ce48b3e0515aeec4217"
|
||||
}
|
||||
"reference": "GUID:0e1d182005e0ae647ab3fa40f5492dbb"
|
||||
}
|
||||
@@ -87,7 +87,7 @@ namespace SepCore.InputModule.Runtime
|
||||
Add(InputActionId.Navigate, InputDeviceKind.KeyboardMouse, "WASD");
|
||||
Add(InputActionId.Confirm, InputDeviceKind.KeyboardMouse, "Enter", "keyboard_enter");
|
||||
Add(InputActionId.Cancel, InputDeviceKind.KeyboardMouse, "Esc", "keyboard_esc");
|
||||
Add(InputActionId.Move, InputDeviceKind.KeyboardMouse, "WASD");
|
||||
Add(InputActionId.Move, InputDeviceKind.KeyboardMouse, "移动", "keyboard_w|keyboard_a|keyboard_s|keyboard_d");
|
||||
Add(InputActionId.Sprint, InputDeviceKind.KeyboardMouse, "L Shift", "keyboard_shift");
|
||||
Add(InputActionId.Interact, InputDeviceKind.KeyboardMouse, "E", "keyboard_e");
|
||||
}
|
||||
@@ -95,10 +95,10 @@ namespace SepCore.InputModule.Runtime
|
||||
private void PopulateGamepad()
|
||||
{
|
||||
Add(InputActionId.Pause, InputDeviceKind.Gamepad, "Menu", "gamepad_menu");
|
||||
Add(InputActionId.Navigate, InputDeviceKind.Gamepad, "L Stick", "gamepad_full_ls");
|
||||
Add(InputActionId.Navigate, InputDeviceKind.Gamepad, "L Stick", "gamepad_ls");
|
||||
Add(InputActionId.Confirm, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
||||
Add(InputActionId.Cancel, InputDeviceKind.Gamepad, "B", "gamepad_b");
|
||||
Add(InputActionId.Move, InputDeviceKind.Gamepad, "L Stick", "gamepad_full_ls");
|
||||
Add(InputActionId.Move, InputDeviceKind.Gamepad, "移动", "gamepad_ls");
|
||||
Add(InputActionId.Sprint, InputDeviceKind.Gamepad, "L3", "gamepad_press_ls");
|
||||
Add(InputActionId.Interact, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
||||
}
|
||||
|
||||
+2
-1
@@ -117,7 +117,8 @@ namespace SepCore.InputModule.Tests
|
||||
{
|
||||
_map.TryGetPrompt(InputActionId.Move, InputDeviceKind.KeyboardMouse, out InputPrompt prompt);
|
||||
|
||||
Assert.That(prompt.TextLabel, Is.EqualTo("WASD"));
|
||||
Assert.That(prompt.TextLabel, Is.EqualTo("移动"));
|
||||
Assert.That(prompt.SpriteName, Is.EqualTo("keyboard_w|keyboard_a|keyboard_s|keyboard_d"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -589,7 +589,7 @@ GameEntry.InputModule.RegisterListener(InputActionId.Confirm, _ =>
|
||||
|
||||
- `InputPrompt`(Base 层 readonly struct):承载提示数据
|
||||
- `TextLabel`:文本标签(如 `”A”`, `”Enter”`, `”E”`)
|
||||
- `SpriteName`:可选的 sprite 资源键(项目自定义,模块不管理 sprite 资源)
|
||||
- `SpriteName`:可选的 sprite 资源键(项目自定义,模块不管理 sprite 资源);多个 sprite 可用 `|` 分隔,如 `keyboard_w|keyboard_a|keyboard_s|keyboard_d`
|
||||
- `HasSprite` / `IsValid`:便捷检查属性
|
||||
- `IInputPromptMap`(Base 层接口):项目可自定义实现
|
||||
- `TryGetPrompt(InputActionId, InputDeviceKind, out InputPrompt)`:查找提示
|
||||
@@ -623,6 +623,21 @@ map.TryGetPrompt(InputActionId.Confirm, InputDeviceKind.Gamepad, out InputPrompt
|
||||
GameEntry.InputModule.PromptMap = new MyPlayStationPromptMap();
|
||||
```
|
||||
|
||||
**TMP 文本工具:**
|
||||
|
||||
`Assets/Plugins/InputModule/Presentation/InputPromptTextUtility.cs` 提供了将 `InputPrompt` 转成 TMP 文本的工具:
|
||||
|
||||
```csharp
|
||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Move, out InputPrompt prompt))
|
||||
{
|
||||
_movePromptText.text = InputPromptTextUtility.BuildTmpText(prompt);
|
||||
}
|
||||
```
|
||||
|
||||
- `BuildTmpText(InputPrompt)`:输出 `"<sprite name=\"keyboard_w\"> <sprite name=\"keyboard_a\"> ... 移动"` 这类可直接给 `TMP_Text.text` 的字符串
|
||||
- `BuildSpriteTags(string)`:只把 `SpriteName` 转成 TMP sprite tag
|
||||
- `SpriteName` 使用 `|` 分隔时会输出多个 sprite tag;单个 sprite 仍按原方式输出
|
||||
|
||||
**自定义 PromptMap 示例:**
|
||||
|
||||
```csharp
|
||||
@@ -651,30 +666,70 @@ public sealed class PlayStationPromptMap : IInputPromptMap
|
||||
|
||||
**触发器:** `DeviceKindChanged` 事件。无需单独的 `PromptsChanged` 事件。
|
||||
|
||||
**消费模式:**
|
||||
**推荐接入现有 UI 架构:**
|
||||
|
||||
当前项目 UI 采用 `Controller -> Context -> Form`,输入提示也应沿用这条数据流:
|
||||
|
||||
- Controller:监听 `GameEntry.InputModule.DeviceKindChanged`,查询 `TryGetPrompt()`,用 `InputPromptTextUtility.BuildTmpText()` 生成 TMP 文本
|
||||
- Context:保存当前 UI 需要展示的提示字符串,如 `MovePrompt`、`ConfirmPrompt`、`CancelPrompt`
|
||||
- Form:只负责展示,不直接判断设备类型,也不直接查询 InputModule
|
||||
|
||||
**Hud 示例:**
|
||||
|
||||
```csharp
|
||||
private Action<InputDeviceKind> _onDeviceChanged;
|
||||
|
||||
private void OnEnable()
|
||||
public class HudContext : UIContext
|
||||
{
|
||||
_onDeviceChanged = _ => RefreshPrompts();
|
||||
GameEntry.InputModule.DeviceKindChanged += _onDeviceChanged;
|
||||
RefreshPrompts(); // 初始状态
|
||||
public string MovePrompt { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
private static HudContext BuildHudContext()
|
||||
{
|
||||
return new HudContext
|
||||
{
|
||||
MovePrompt = BuildPromptText(InputActionId.Move)
|
||||
};
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
private static string BuildPromptText(InputActionId actionId)
|
||||
{
|
||||
GameEntry.InputModule.DeviceKindChanged -= _onDeviceChanged;
|
||||
_onDeviceChanged = null;
|
||||
if (GameEntry.InputModule == null || !GameEntry.InputModule.TryGetPrompt(actionId, out InputPrompt prompt))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return InputPromptTextUtility.BuildTmpText(prompt);
|
||||
}
|
||||
|
||||
private void RefreshPrompts()
|
||||
protected override void SubscribeCustomEvents()
|
||||
{
|
||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Interact, out InputPrompt p))
|
||||
_interactLabel.text = p.TextLabel;
|
||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Cancel, out InputPrompt c))
|
||||
_cancelLabel.text = c.TextLabel;
|
||||
GameEntry.InputModule.DeviceKindChanged += OnDeviceKindChanged;
|
||||
}
|
||||
|
||||
protected override void UnsubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.InputModule.DeviceKindChanged -= OnDeviceKindChanged;
|
||||
}
|
||||
|
||||
private void OnDeviceKindChanged(InputDeviceKind deviceKind)
|
||||
{
|
||||
if (Context == null || Form == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Context.MovePrompt = BuildPromptText(InputActionId.Move);
|
||||
Form.RefreshMovePrompt(Context.MovePrompt);
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
public void RefreshMovePrompt(string prompt)
|
||||
{
|
||||
bool visible = !string.IsNullOrEmpty(prompt);
|
||||
_movePromptRoot.SetActive(visible);
|
||||
_movePromptText.text = prompt;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -792,17 +847,16 @@ private void RefreshPrompts()
|
||||
```csharp
|
||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Interact, out InputPrompt prompt))
|
||||
{
|
||||
// 文本标签
|
||||
_label.text = prompt.TextLabel;
|
||||
|
||||
// 图标(通过 TMP Sprite Tag)
|
||||
if (prompt.HasSprite)
|
||||
{
|
||||
_iconText.text = $"<sprite=\"InputPrompt\" name={prompt.SpriteName}>";
|
||||
}
|
||||
_label.text = InputPromptTextUtility.BuildTmpText(prompt);
|
||||
}
|
||||
```
|
||||
|
||||
如果只想显示图标,可以使用:
|
||||
|
||||
```csharp
|
||||
_iconText.text = InputPromptTextUtility.BuildSpriteTags(prompt.SpriteName);
|
||||
```
|
||||
|
||||
**说明:**
|
||||
|
||||
- 此步骤完全可选;不设置 Default Sprite Asset 不影响任何核心功能
|
||||
|
||||
Reference in New Issue
Block a user