Skip to content

Commit

Permalink
PenWave
Browse files Browse the repository at this point in the history
  • Loading branch information
JoogabYun committed Dec 3, 2024
1 parent 3ff2757 commit f490821
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 130 deletions.
39 changes: 32 additions & 7 deletions src/Tizen.NUI.PenWave/src/public/Canvas/PenWaveCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,39 @@ public ToolBase Tool
}
}

/// <summary>
/// Notifies that the canvas has started an action.
/// </summary>
private void NotifyActionStarted(object sender, EventArgs e)
{
ActionStarted?.Invoke(this, EventArgs.Empty);
}

/// <summary>
/// Notifies that the canvas has finished an action.
/// </summary>
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();
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Tizen.NUI.PenWave/src/public/Picker/PWToolPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class PWToolPicker : View
/// The ToolChanged event. It is triggered when the selected tool changes.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public event Action<ToolBase> ToolChanged;
public event EventHandler ToolChanged;

/// <summary>
/// Creates a new instance of PWToolPicker.
Expand Down Expand Up @@ -332,7 +332,7 @@ private void SetTool(ToolBase tool)
{
canvasView.Tool = tool;
ShowToolSettings(tool);
ToolChanged?.Invoke(tool);
ToolChanged?.Invoke(this, EventArgs.Empty);
}
}

Expand Down
53 changes: 0 additions & 53 deletions src/Tizen.NUI.PenWave/src/public/Tools/Command.cs

This file was deleted.

18 changes: 12 additions & 6 deletions src/Tizen.NUI.PenWave/src/public/Tools/Eraser/EraserTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// The type of eraser tool.
/// </summary>
Expand Down Expand Up @@ -82,7 +85,7 @@ public override void Deactivate()
/// Handle input events.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal override void HandleInput(Touch touch, UnRedoManager unredoManager)
internal override void HandleInput(Touch touch)
{
if (touch == null || touch.GetPointCount() == 0) return;

Expand All @@ -106,15 +109,15 @@ 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;
}
}

// 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();
}
Expand All @@ -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;
}
}

}
}

35 changes: 0 additions & 35 deletions src/Tizen.NUI.PenWave/src/public/Tools/ICommand.cs

This file was deleted.

18 changes: 11 additions & 7 deletions src/Tizen.NUI.PenWave/src/public/Tools/Pencil/PencilTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public override void Deactivate()
/// <summary>
/// Handles input from the user.
/// </summary>
internal override void HandleInput(Touch touch, UnRedoManager unredoManager)
internal override void HandleInput(Touch touch)
{
if (touch == null || touch.GetPointCount() == 0) return;

Expand All @@ -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;
}
Expand All @@ -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.
Expand All @@ -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();
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override void Deactivate()
/// </summary>
/// <param name="touch"></param>
/// <param name="unredoManager"></param>
internal override void HandleInput(Touch touch, UnRedoManager unredoManager)
internal override void HandleInput(Touch touch)
{
if (touch == null || touch.GetPointCount() == 0) return;

Expand Down
3 changes: 1 addition & 2 deletions src/Tizen.NUI.PenWave/src/public/Tools/Toolbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public abstract class ToolBase
/// Handles input events such as touch events and updates the state of the tool accordingly.
/// </summary>
/// <param name="touch">The touch event data.</param>
/// <param name="unredoManager">The manager responsible for handling undo and redo operations.</param>
internal virtual void HandleInput(Touch touch, UnRedoManager unredoManager) {}
internal virtual void HandleInput(Touch touch) {}

/// <summary>
/// Activates the tool, making it ready to receive input and perform its functionality.
Expand Down
33 changes: 16 additions & 17 deletions src/Tizen.NUI.PenWave/src/public/Tools/UnRedoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,61 @@ namespace Tizen.NUI.PenWave
/// The UnRedoManager class manages undo and redo operations for commands.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class UnRedoManager
internal class UnRedoManager
{
// Stacks to store undo and redo commands
private readonly Stack<ICommand> undoStack = new Stack<ICommand>();
private readonly Stack<ICommand> redoStack = new Stack<ICommand>();
private uint undoStack = 0;
private uint redoStack = 0;

/// <summary>
/// Executes a command and clears the redo stack.
/// </summary>
/// <param name="command">The command to be executed.</param>
[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
}

/// <summary>
/// Undoes the last executed command and pushes it to the redo stack.
/// </summary>
[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
}
}

/// <summary>
/// Redoes the last undone command and pushes it to the undo stack.
/// </summary>
[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
}
}

/// <summary>
/// Determines whether an undo operation is possible.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool CanUndo => undoStack.Count > 0;
internal bool CanUndo => undoStack > 0;

/// <summary>
/// Determines whether a redo operation is possible.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool CanRedo => redoStack.Count > 0;
internal bool CanRedo => redoStack > 0;
}
}
Loading

0 comments on commit f490821

Please sign in to comment.