Repository navigation
refactor: extract mouse UI request preparation #1632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hatayama
merged 4 commits into
v3-beta
from
refactor/hatayama/extract-mouse-ui-command-validation
Jul 8, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Packages/src/Editor/FirstPartyTools/SimulateMouseUi/MouseUiSimulationCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #nullable enable | ||
| using System; | ||
|
|
||
| using io.github.hatayama.UnityCliLoop.Runtime; | ||
|
|
||
| namespace io.github.hatayama.UnityCliLoop.FirstPartyTools | ||
| { | ||
| /// <summary> | ||
| /// Provides Mouse UI Simulation Command behavior for Unity CLI Loop. | ||
| /// </summary> | ||
| internal sealed class MouseUiSimulationCommand | ||
| { | ||
| private MouseUiSimulationCommand(SimulateMouseUiSchema request, MouseAction action) | ||
| { | ||
| Action = action; | ||
| X = request.X; | ||
| Y = request.Y; | ||
| FromX = request.FromX; | ||
| FromY = request.FromY; | ||
| DragSpeed = request.DragSpeed; | ||
| Duration = request.Duration; | ||
| Button = ToRuntimeMouseButton(request.Button); | ||
| BypassRaycast = request.BypassRaycast; | ||
| TargetPath = request.TargetPath ?? ""; | ||
| DropTargetPath = request.DropTargetPath ?? ""; | ||
| } | ||
|
|
||
| public MouseAction Action { get; } | ||
| public float X { get; } | ||
| public float Y { get; } | ||
| public float FromX { get; } | ||
| public float FromY { get; } | ||
| public float DragSpeed { get; } | ||
| public float Duration { get; } | ||
| public MouseButton Button { get; } | ||
| public bool BypassRaycast { get; } | ||
| public string TargetPath { get; } | ||
| public string DropTargetPath { get; } | ||
|
|
||
| // Returns (command, error). Error is non-null iff the caller supplied an out-of-range enum value. | ||
| public static (MouseUiSimulationCommand? command, string? errorMessage) TryFromSchema(SimulateMouseUiSchema request) | ||
| { | ||
| if (request == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(request)); | ||
| } | ||
|
|
||
| MouseAction? action = TryConvertMouseAction(request.Action); | ||
| if (action == null) | ||
| { | ||
| return (null, $"Unknown mouse UI action: {request.Action}"); | ||
| } | ||
|
|
||
| return (new MouseUiSimulationCommand(request, action.Value), null); | ||
| } | ||
|
|
||
| private static MouseAction? TryConvertMouseAction(UnityCliLoopMouseUiAction action) | ||
| { | ||
| switch (action) | ||
| { | ||
| case UnityCliLoopMouseUiAction.Click: | ||
| return MouseAction.Click; | ||
| case UnityCliLoopMouseUiAction.Drag: | ||
| return MouseAction.Drag; | ||
| case UnityCliLoopMouseUiAction.DragStart: | ||
| return MouseAction.DragStart; | ||
| case UnityCliLoopMouseUiAction.DragMove: | ||
| return MouseAction.DragMove; | ||
| case UnityCliLoopMouseUiAction.DragEnd: | ||
| return MouseAction.DragEnd; | ||
| case UnityCliLoopMouseUiAction.LongPress: | ||
| return MouseAction.LongPress; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static MouseButton ToRuntimeMouseButton(UnityCliLoopMouseButton button) | ||
| { | ||
| switch (button) | ||
| { | ||
| case UnityCliLoopMouseButton.Right: | ||
| return MouseButton.Right; | ||
| case UnityCliLoopMouseButton.Middle: | ||
| return MouseButton.Middle; | ||
| default: | ||
| return MouseButton.Left; | ||
| } | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
Packages/src/Editor/FirstPartyTools/SimulateMouseUi/MouseUiSimulationCommand.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
Packages/src/Editor/FirstPartyTools/SimulateMouseUi/MouseUiSimulationValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #nullable enable | ||
| using UnityEngine.EventSystems; | ||
|
|
||
| using io.github.hatayama.UnityCliLoop.Runtime; | ||
| using io.github.hatayama.UnityCliLoop.ToolContracts; | ||
|
|
||
| namespace io.github.hatayama.UnityCliLoop.FirstPartyTools | ||
| { | ||
| /// <summary> | ||
| /// Validates mouse UI simulation preconditions and request options. | ||
| /// </summary> | ||
| internal static class MouseUiSimulationValidator | ||
| { | ||
| internal static SimulateMouseUiResponse? ValidateSimulationStart( | ||
| MouseUiSimulationCommand parameters, | ||
| EventSystem? eventSystem, | ||
| string pausedActionDescription) | ||
| { | ||
| ValidationResult playModeResult = | ||
| PlayModeToolPreflightService.RequireActiveAndNotPaused(pausedActionDescription); | ||
| if (!playModeResult.IsValid) | ||
| { | ||
| return CreateFailure(parameters, playModeResult.ErrorMessage); | ||
| } | ||
|
|
||
| if (eventSystem == null) | ||
| { | ||
| return CreateFailure( | ||
| parameters, | ||
| "No EventSystem found in the scene. Ensure an EventSystem GameObject exists."); | ||
| } | ||
|
|
||
| return ValidateSimulationRequestOptions(parameters); | ||
| } | ||
|
|
||
| internal static SimulateMouseUiResponse? ValidateActiveDragState(MouseUiSimulationCommand parameters) | ||
| { | ||
| if (!MouseDragState.IsDragging || !RequiresIdlePointer(parameters.Action)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return CreateFailure( | ||
| parameters, | ||
| $"Cannot {parameters.Action.ToString()} while a split drag is active. Call DragEnd first."); | ||
| } | ||
|
|
||
| internal static SimulateMouseUiResponse CreateFailure( | ||
| MouseUiSimulationCommand parameters, | ||
| string message) | ||
| { | ||
| return new SimulateMouseUiResponse | ||
| { | ||
| Success = false, | ||
| Message = message, | ||
| Action = parameters.Action.ToString() | ||
| }; | ||
| } | ||
|
|
||
| private static SimulateMouseUiResponse? ValidateSimulationRequestOptions( | ||
| MouseUiSimulationCommand parameters) | ||
| { | ||
| if (parameters.Action != MouseAction.Click && parameters.Action != MouseAction.LongPress && parameters.DragSpeed < 0f) | ||
| { | ||
| return CreateFailure(parameters, $"DragSpeed must be non-negative, got: {parameters.DragSpeed}"); | ||
| } | ||
|
|
||
| if (IsDragAction(parameters.Action) && parameters.Button != MouseButton.Left) | ||
| { | ||
| return CreateFailure( | ||
| parameters, | ||
| $"Drag actions only support Left button (uGUI ignores non-left drags), got: {parameters.Button}"); | ||
| } | ||
|
|
||
| if (parameters.BypassRaycast && !SupportsBypassRaycast(parameters.Action)) | ||
| { | ||
| return CreateFailure(parameters, "BypassRaycast is not supported for this action."); | ||
| } | ||
|
|
||
| if (parameters.BypassRaycast && | ||
| RequiresBypassTargetPath(parameters.Action) && | ||
| string.IsNullOrWhiteSpace(parameters.TargetPath)) | ||
| { | ||
| return CreateFailure( | ||
| parameters, | ||
| "TargetPath is required when BypassRaycast is true for Click, LongPress, Drag, or DragStart."); | ||
| } | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(parameters.DropTargetPath) && | ||
| parameters.Action != MouseAction.Drag && | ||
| parameters.Action != MouseAction.DragEnd) | ||
| { | ||
| return CreateFailure(parameters, "DropTargetPath supports Drag and DragEnd only."); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static bool RequiresIdlePointer(MouseAction action) | ||
| { | ||
| return action == MouseAction.Click || action == MouseAction.Drag || action == MouseAction.LongPress; | ||
| } | ||
|
|
||
| private static bool SupportsBypassRaycast(MouseAction action) | ||
| { | ||
| return action == MouseAction.Click | ||
| || action == MouseAction.LongPress | ||
| || IsDragAction(action); | ||
| } | ||
|
|
||
| private static bool RequiresBypassTargetPath(MouseAction action) | ||
| { | ||
| return action == MouseAction.Click | ||
| || action == MouseAction.LongPress | ||
| || action == MouseAction.Drag | ||
| || action == MouseAction.DragStart; | ||
| } | ||
|
|
||
| private static bool IsDragAction(MouseAction action) | ||
| { | ||
| return action == MouseAction.Drag | ||
| || action == MouseAction.DragStart | ||
| || action == MouseAction.DragMove | ||
| || action == MouseAction.DragEnd; | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Packages/src/Editor/FirstPartyTools/SimulateMouseUi/MouseUiSimulationValidator.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.