-
Notifications
You must be signed in to change notification settings - Fork 21
Add mouse click action #21
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
Open
alfish2000
wants to merge
4
commits into
Martenfur:develop
Choose a base branch
from
alfish2000:feature/click-action-drag-drop-save
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,142 @@ | ||
| using System.Drawing; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading.Tasks; | ||
| using System.Windows; | ||
| using System.Windows.Controls; | ||
| using System.Windows.Controls.Primitives; | ||
| using WindowsInput.Events; | ||
|
|
||
| namespace TabletFriend.Actions | ||
| { | ||
| public class ClickAction : ButtonAction | ||
| { | ||
| private static System.Drawing.Point? _newClickCoordinates = null; | ||
| private static Rectangle _dragBoxFromMouseDown; | ||
|
|
||
| private bool _initialized = false; | ||
| private readonly int _x; | ||
| private readonly int _y; | ||
|
|
||
| public ClickAction(string cmd) | ||
| { | ||
| // The regular expression extracts the coordinates from the given cmd string. | ||
| // Furthermore it validates the format of the string. | ||
| // The string has to consist of two numbers separated by a comma "," (white spaces are ignored) like e.g | ||
| // "10,20" or " 10,20 " or "10, 20" or " 10 , 20 " | ||
| Regex expression = new Regex(@"^\s*(?<x_coordinate>\d+)\s*,\s*(?<y_coordinate>\d+)\s*$"); | ||
| Match match = expression.Match(cmd); | ||
| if (match.Success) | ||
| { | ||
| _x = int.Parse(match.Groups["x_coordinate"].Value); | ||
| _y = int.Parse(match.Groups["y_coordinate"].Value); | ||
| _initialized = true; | ||
| } | ||
| } | ||
|
|
||
| public async override Task Invoke() | ||
| { | ||
| if (_initialized) | ||
| { | ||
| var sim = new EventBuilder(); | ||
| sim.MoveTo(_x, _y).Click(ButtonCode.Left); | ||
alfish2000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await sim.Invoke(); | ||
| } | ||
| } | ||
|
|
||
| public static void AddDragAndDropEventHandlers(ButtonBase uiButton, string key) | ||
| { | ||
| // Add event handlers to the click action button | ||
| // to support drag/drop for defining the destination coordinates. | ||
| uiButton.Name = key; | ||
| uiButton.PreviewMouseLeftButtonUp += MouseUp; | ||
| uiButton.PreviewMouseLeftButtonDown += MouseDown; | ||
| uiButton.PreviewMouseMove += MouseMove; | ||
| uiButton.PreviewQueryContinueDrag += QueryContinueDrag; | ||
| uiButton.PreviewGiveFeedback += GiveFeedback; | ||
| } | ||
|
|
||
| private static void MouseDown(object sender, System.Windows.Input.MouseEventArgs e) | ||
| { | ||
| // Remember the point where the mouse down occurred. The DragSize indicates | ||
| // the size that the mouse can move before a drag event should be started. | ||
| System.Drawing.Size dragSize = System.Windows.Forms.SystemInformation.DragSize; | ||
| var x = System.Windows.Forms.Cursor.Position.X; | ||
| var y = System.Windows.Forms.Cursor.Position.Y; | ||
|
|
||
| System.Windows.Point mpos = e.GetPosition(null); | ||
| // Create a rectangle using the DragSize, with the mouse position being | ||
| // at the center of the rectangle. | ||
| _dragBoxFromMouseDown = new Rectangle( | ||
| new System.Drawing.Point( | ||
| x - (dragSize.Width / 2), | ||
| y - (dragSize.Height / 2)), | ||
| dragSize); | ||
| } | ||
|
|
||
| private static void MouseUp(object sender, System.Windows.Input.MouseEventArgs e) | ||
| { | ||
| // Reset the drag rectangle when the mouse button is raised. | ||
| _dragBoxFromMouseDown = Rectangle.Empty; | ||
| } | ||
|
|
||
| private static void MouseMove(object sender, System.Windows.Input.MouseEventArgs e) | ||
| { | ||
| if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed) | ||
| { | ||
| var x = System.Windows.Forms.Cursor.Position.X; | ||
| var y = System.Windows.Forms.Cursor.Position.Y; | ||
|
|
||
| // If the mouse moves outside the rectangle, start the drag. | ||
| if (_dragBoxFromMouseDown != Rectangle.Empty && | ||
| !_dragBoxFromMouseDown.Contains(x, y)) | ||
| { | ||
| _dragBoxFromMouseDown = Rectangle.Empty; | ||
| { | ||
| DataObject dataObj = new DataObject((sender as Button)); | ||
| System.Windows.DragDrop.DoDragDrop((sender as Button), dataObj, DragDropEffects.None); | ||
| if (_newClickCoordinates.HasValue) | ||
| { | ||
| var result = MessageBox.Show( | ||
| $"Save new click destination {_newClickCoordinates.Value}", | ||
| "Mouse Click Action", | ||
| MessageBoxButton.OKCancel); | ||
|
|
||
| if (result == MessageBoxResult.OK) | ||
| { | ||
| var key = (sender as Button).Name; | ||
| LayoutManager.UpdateClickActionCoordinatesInCurrentLayoutFile(key, _newClickCoordinates.Value); | ||
| } | ||
|
|
||
| _newClickCoordinates = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void GiveFeedback(object sender, GiveFeedbackEventArgs e) | ||
| { | ||
| System.Windows.Input.Mouse.SetCursor(System.Windows.Input.Cursors.Cross); | ||
| e.Handled = true; | ||
| } | ||
|
|
||
| private static void QueryContinueDrag(object sender, QueryContinueDragEventArgs e) | ||
| { | ||
| if (e.KeyStates == DragDropKeyStates.None) | ||
| { | ||
| var pointOnScreen = System.Windows.Forms.Cursor.Position; | ||
| var pointInMainWindow = Application.Current.MainWindow.PointFromScreen( | ||
| new System.Windows.Point(pointOnScreen.X, pointOnScreen.Y)); | ||
| var rs = Application.Current.MainWindow.RenderSize; | ||
| var dropDestinationInsideOfMainWindow = | ||
| (pointInMainWindow.X > 0 && pointInMainWindow.X < rs.Width) && | ||
| (pointInMainWindow.Y > 0 && pointInMainWindow.Y < rs.Height); | ||
|
|
||
| if (!dropDestinationInsideOfMainWindow) | ||
| { | ||
| _newClickCoordinates = pointOnScreen; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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
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.