From f490821a85b16e23024f628b62af646ac814f2c5 Mon Sep 17 00:00:00 2001 From: "joogab.yun" Date: Mon, 2 Dec 2024 18:03:27 +0900 Subject: [PATCH] PenWave --- .../src/public/Canvas/PenWaveCanvas.cs | 39 +++++++++++--- .../src/public/Picker/PWToolPicker.cs | 4 +- .../src/public/Tools/Command.cs | 53 ------------------- .../src/public/Tools/Eraser/EraserTool.cs | 18 ++++--- .../src/public/Tools/ICommand.cs | 35 ------------ .../src/public/Tools/Pencil/PencilTool.cs | 18 ++++--- .../public/Tools/Selection/SelectionTool.cs | 2 +- .../src/public/Tools/Toolbase.cs | 3 +- .../src/public/Tools/UnRedoManager.cs | 33 ++++++------ .../src/PenWaveSample.cs | 15 ++++++ 10 files changed, 90 insertions(+), 130 deletions(-) delete mode 100644 src/Tizen.NUI.PenWave/src/public/Tools/Command.cs delete mode 100644 src/Tizen.NUI.PenWave/src/public/Tools/ICommand.cs diff --git a/src/Tizen.NUI.PenWave/src/public/Canvas/PenWaveCanvas.cs b/src/Tizen.NUI.PenWave/src/public/Canvas/PenWaveCanvas.cs index 08300615327..cee78a119cd 100644 --- a/src/Tizen.NUI.PenWave/src/public/Canvas/PenWaveCanvas.cs +++ b/src/Tizen.NUI.PenWave/src/public/Canvas/PenWaveCanvas.cs @@ -117,16 +117,39 @@ public ToolBase Tool } } + /// + /// Notifies that the canvas has started an action. + /// + private void NotifyActionStarted(object sender, EventArgs e) + { + ActionStarted?.Invoke(this, EventArgs.Empty); + } + + /// + /// Notifies that the canvas has finished an action. + /// + private void NotifyActionFinished(object sender, EventArgs e) + { + ActionFinished?.Invoke(this, EventArgs.Empty); + } + + // Event handlers for action started. private void OnStarted(object sender, EventArgs e) { - ActionStarted?.Invoke(this, e); + NotifyActionStarted(sender, e); } // Event handlers for action finished. private void OnFinished(object sender, EventArgs e) { - ActionFinished?.Invoke(this, e); + RegisterUndo(); + NotifyActionFinished(sender, e); + } + + private void RegisterUndo() + { + unredoManager.RegisterUndo(); } /// @@ -135,8 +158,9 @@ private void OnFinished(object sender, EventArgs e) [EditorBrowsable(EditorBrowsableState.Never)] public void ClearCanvas() { - var command = new Command(() => renderer.ClearCanvas()); - unredoManager.Execute(command); + renderer.ClearCanvas(); + RegisterUndo(); + NotifyActionFinished(this, EventArgs.Empty); } /// @@ -198,8 +222,9 @@ public void ToggleGrid(GridDensityType gridType) [EditorBrowsable(EditorBrowsableState.Never)] public void AddPicture(string path, Size2D size, Position2D position) { - var command = new Command(() => renderer.AddPicture(path, size, position)); - unredoManager.Execute(command); + renderer.AddPicture(path, size, position); + RegisterUndo(); + NotifyActionFinished(this, EventArgs.Empty); } /// @@ -209,7 +234,7 @@ public void AddPicture(string path, Size2D size, Position2D position) [EditorBrowsable(EditorBrowsableState.Never)] public void HandleInput(Touch touch) { - currentTool?.HandleInput(touch, unredoManager); + currentTool?.HandleInput(touch); } /// diff --git a/src/Tizen.NUI.PenWave/src/public/Picker/PWToolPicker.cs b/src/Tizen.NUI.PenWave/src/public/Picker/PWToolPicker.cs index c2526f6089b..20f1293efd4 100644 --- a/src/Tizen.NUI.PenWave/src/public/Picker/PWToolPicker.cs +++ b/src/Tizen.NUI.PenWave/src/public/Picker/PWToolPicker.cs @@ -105,7 +105,7 @@ public class PWToolPicker : View /// The ToolChanged event. It is triggered when the selected tool changes. /// [EditorBrowsable(EditorBrowsableState.Never)] - public event Action ToolChanged; + public event EventHandler ToolChanged; /// /// Creates a new instance of PWToolPicker. @@ -332,7 +332,7 @@ private void SetTool(ToolBase tool) { canvasView.Tool = tool; ShowToolSettings(tool); - ToolChanged?.Invoke(tool); + ToolChanged?.Invoke(this, EventArgs.Empty); } } diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/Command.cs b/src/Tizen.NUI.PenWave/src/public/Tools/Command.cs deleted file mode 100644 index b8879c25f82..00000000000 --- a/src/Tizen.NUI.PenWave/src/public/Tools/Command.cs +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright(c) 2024 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -using System; -using System.ComponentModel; - -namespace Tizen.NUI.PenWave -{ - /// - /// Represents a command that can be executed. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public class Command : ICommand - { - /// - /// The action to be executed when command is invoked. - /// - internal readonly Action executeAction; - - /// - /// Constructor. - /// - /// The action to be executed when the command is invoked. - [EditorBrowsable(EditorBrowsableState.Never)] - public Command(Action executeAction) - { - this.executeAction = executeAction; - } - - /// - /// Executes the command. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public void Execute() - { - executeAction?.Invoke(); - } - } -} diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/Eraser/EraserTool.cs b/src/Tizen.NUI.PenWave/src/public/Tools/Eraser/EraserTool.cs index 41fefd582da..91e75f32225 100644 --- a/src/Tizen.NUI.PenWave/src/public/Tools/Eraser/EraserTool.cs +++ b/src/Tizen.NUI.PenWave/src/public/Tools/Eraser/EraserTool.cs @@ -29,6 +29,9 @@ namespace Tizen.NUI.PenWave [EditorBrowsable(EditorBrowsableState.Never)] public class EraserTool : ToolBase { + /// The current state of the tool. + private bool isActive = false; + /// /// The type of eraser tool. /// @@ -82,7 +85,7 @@ public override void Deactivate() /// Handle input events. /// [EditorBrowsable(EditorBrowsableState.Never)] - internal override void HandleInput(Touch touch, UnRedoManager unredoManager) + internal override void HandleInput(Touch touch) { if (touch == null || touch.GetPointCount() == 0) return; @@ -106,8 +109,7 @@ internal override void HandleInput(Touch touch, UnRedoManager unredoManager) break; case PointStateType.Up: case PointStateType.Leave: - var command = new Command(() => EndDrawing()); - unredoManager.Execute(command); + EndDrawing(); break; } } @@ -115,6 +117,7 @@ internal override void HandleInput(Touch touch, UnRedoManager unredoManager) // Start drawing at the given position. private void StartDrawing(Vector2 position, uint touchTime) { + isActive = true; PenWave.Instance.EraseShape((int)position.X, (int)position.Y, EraserRadius, (Eraser == EraserType.Partial)); NotifyActionStarted(); } @@ -128,10 +131,13 @@ private void ContinueDrawing(Vector2 position, uint touchTime) // End drawing at the given position. private void EndDrawing() { - PenWave.Instance.StopErasing(); - NotifyActionFinished(); + if (isActive) + { + PenWave.Instance.StopErasing(); + NotifyActionFinished(); + isActive = false; + } } - } } diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/ICommand.cs b/src/Tizen.NUI.PenWave/src/public/Tools/ICommand.cs deleted file mode 100644 index cf9847d230a..00000000000 --- a/src/Tizen.NUI.PenWave/src/public/Tools/ICommand.cs +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright(c) 2024 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -using System; -using System.ComponentModel; - -namespace Tizen.NUI.PenWave -{ - /// - /// Commands that can be executed. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public interface ICommand - { - /// - /// Executes the command. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - void Execute(); - } -} diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/Pencil/PencilTool.cs b/src/Tizen.NUI.PenWave/src/public/Tools/Pencil/PencilTool.cs index da574e59b98..072916f362e 100644 --- a/src/Tizen.NUI.PenWave/src/public/Tools/Pencil/PencilTool.cs +++ b/src/Tizen.NUI.PenWave/src/public/Tools/Pencil/PencilTool.cs @@ -133,7 +133,7 @@ public override void Deactivate() /// /// Handles input from the user. /// - internal override void HandleInput(Touch touch, UnRedoManager unredoManager) + internal override void HandleInput(Touch touch) { if (touch == null || touch.GetPointCount() == 0) return; @@ -157,8 +157,6 @@ internal override void HandleInput(Touch touch, UnRedoManager unredoManager) break; case PointStateType.Up: case PointStateType.Leave: - var command = new Command(() => EndDrawing()); - unredoManager.Execute(command); EndDrawing(); break; } @@ -168,7 +166,10 @@ internal override void HandleInput(Touch touch, UnRedoManager unredoManager) private void StartDrawing(Vector2 position, uint touchTime) { currentShapeId = PenWave.Instance.BeginShapeDraw(position.X, position.Y, touchTime); - NotifyActionStarted(); + if (currentShapeId > 0) + { + NotifyActionStarted(); + } } // Continues drawing the current shape. @@ -192,9 +193,12 @@ private void ContinueDrawing(Vector2 position, uint touchTime) // Ends drawing the current shape. private void EndDrawing() { - PenWave.Instance.EndShapeDraw(currentShapeId, 0); - currentShapeId = 0; - NotifyActionFinished(); + if (currentShapeId > 0) + { + PenWave.Instance.EndShapeDraw(currentShapeId, 0); + currentShapeId = 0; + NotifyActionFinished(); + } } } diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/Selection/SelectionTool.cs b/src/Tizen.NUI.PenWave/src/public/Tools/Selection/SelectionTool.cs index 8eab0dea467..8113367363e 100644 --- a/src/Tizen.NUI.PenWave/src/public/Tools/Selection/SelectionTool.cs +++ b/src/Tizen.NUI.PenWave/src/public/Tools/Selection/SelectionTool.cs @@ -115,7 +115,7 @@ public override void Deactivate() /// /// /// - internal override void HandleInput(Touch touch, UnRedoManager unredoManager) + internal override void HandleInput(Touch touch) { if (touch == null || touch.GetPointCount() == 0) return; diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/Toolbase.cs b/src/Tizen.NUI.PenWave/src/public/Tools/Toolbase.cs index 253fdfd136f..844f8c3b810 100644 --- a/src/Tizen.NUI.PenWave/src/public/Tools/Toolbase.cs +++ b/src/Tizen.NUI.PenWave/src/public/Tools/Toolbase.cs @@ -40,8 +40,7 @@ public abstract class ToolBase /// Handles input events such as touch events and updates the state of the tool accordingly. /// /// The touch event data. - /// The manager responsible for handling undo and redo operations. - internal virtual void HandleInput(Touch touch, UnRedoManager unredoManager) {} + internal virtual void HandleInput(Touch touch) {} /// /// Activates the tool, making it ready to receive input and perform its functionality. diff --git a/src/Tizen.NUI.PenWave/src/public/Tools/UnRedoManager.cs b/src/Tizen.NUI.PenWave/src/public/Tools/UnRedoManager.cs index 9eb3f5c9627..02603d7c4bd 100644 --- a/src/Tizen.NUI.PenWave/src/public/Tools/UnRedoManager.cs +++ b/src/Tizen.NUI.PenWave/src/public/Tools/UnRedoManager.cs @@ -25,35 +25,34 @@ namespace Tizen.NUI.PenWave /// The UnRedoManager class manages undo and redo operations for commands. /// [EditorBrowsable(EditorBrowsableState.Never)] - public class UnRedoManager + internal class UnRedoManager { // Stacks to store undo and redo commands - private readonly Stack undoStack = new Stack(); - private readonly Stack redoStack = new Stack(); + private uint undoStack = 0; + private uint redoStack = 0; /// /// Executes a command and clears the redo stack. /// /// The command to be executed. [EditorBrowsable(EditorBrowsableState.Never)] - public void Execute(ICommand command) + internal void RegisterUndo() { - command.Execute(); - undoStack.Push(command); - redoStack.Clear(); // Clear redo stack after executing a new command + undoStack++; // Push command to undo stack + redoStack = 0; // Clear redo stack after executing a new command } /// /// Undoes the last executed command and pushes it to the redo stack. /// [EditorBrowsable(EditorBrowsableState.Never)] - public void Undo() + internal void Undo() { - if (undoStack.Count > 0) + if (undoStack > 0) { - ICommand command = undoStack.Pop(); + undoStack--; // Pop command from undo stack PenWave.Instance.Undo(); - redoStack.Push(command); + redoStack++; // Push command to redo stack } } @@ -61,13 +60,13 @@ public void Undo() /// Redoes the last undone command and pushes it to the undo stack. /// [EditorBrowsable(EditorBrowsableState.Never)] - public void Redo() + internal void Redo() { - if (redoStack.Count > 0) + if (redoStack > 0) { - ICommand command = redoStack.Pop(); + redoStack--; // Pop command from redo stack PenWave.Instance.Redo(); - undoStack.Push(command); + undoStack++; // Push command to undo stack } } @@ -75,12 +74,12 @@ public void Redo() /// Determines whether an undo operation is possible. /// [EditorBrowsable(EditorBrowsableState.Never)] - public bool CanUndo => undoStack.Count > 0; + internal bool CanUndo => undoStack > 0; /// /// Determines whether a redo operation is possible. /// [EditorBrowsable(EditorBrowsableState.Never)] - public bool CanRedo => redoStack.Count > 0; + internal bool CanRedo => redoStack > 0; } } diff --git a/test/Tizen.NUI.PenWave.Sample/src/PenWaveSample.cs b/test/Tizen.NUI.PenWave.Sample/src/PenWaveSample.cs index 1497ffd5cf7..7feb49395fb 100644 --- a/test/Tizen.NUI.PenWave.Sample/src/PenWaveSample.cs +++ b/test/Tizen.NUI.PenWave.Sample/src/PenWaveSample.cs @@ -19,6 +19,21 @@ class Program : NUIApplication private PenWaveCanvas canvasView; private ImageView thumbnailView; + public class TestTool : ToolBase + { + public override void Activate() + { + } + + public override void Deactivate() + { + } + + // public override void HandleInput(Touch touch) + // { + // } + } + public Program(ThemeOptions option, WindowData windowData) : base(option, windowData) {