Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JoogabYun committed Nov 4, 2024
1 parent 68c4f60 commit bab96a1
Show file tree
Hide file tree
Showing 23 changed files with 653 additions and 245 deletions.
41 changes: 41 additions & 0 deletions src/Tizen.NUI.PenWave/src/public/EventBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.Collections.Generic;

namespace Tizen.NUI.PenWave
{
public static class EventBus
{
private static readonly Dictionary<string, Action<object>> events = new();

public static void Subscribe(string eventName, Action<object> callback)
{
if (events.ContainsKey(eventName))
events[eventName] += callback;
else
events[eventName] = callback;
}

public static void Publish(string eventName, object eventArgs = null)
{
if (events.TryGetValue(eventName, out var action))
action.Invoke(eventArgs);
}
}
}
23 changes: 13 additions & 10 deletions src/Tizen.NUI.PenWave/src/public/PWCanvasView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,25 @@ public class PWCanvasView : DirectRenderingGLView

public PWCanvasView() : base(DirectRenderingGLView.ColorFormat.RGBA8888, DirectRenderingGLView.BackendMode.UnsafeDirectRendering)
{
pageManager = new PageManager();
renderer = new CanvasRenderer(PWEngine.CreateCanvas(-1, -1));
uiManager = new CanvasUIManager(this);
ToolManager = new ToolManager();
InitializeCanvas();
}

public PWCanvasView(string backgroundPath) : base(DirectRenderingGLView.ColorFormat.RGBA8888, DirectRenderingGLView.BackendMode.UnsafeDirectRendering)
{
pageManager = new PageManager();
renderer = new CanvasRenderer(PWEngine.CreateCanvasWithBackgroundImage(backgroundPath));
uiManager = new CanvasUIManager(this);
ToolManager = new ToolManager();
InitializeCanvas();
}


public static PWCanvasView CreateDefaultCanvas()
{
var canvasView = new PWCanvasView();

CanvasTool canvasTool = new CanvasTool();
PencilTool pencilTool = new PencilTool();
EraserTool eraserTool = new EraserTool();
SelectTool selectTool = new SelectTool();
var canvasTool = ToolFactory.CreateTool(ToolBase.ToolType.Canvas);
var pencilTool = ToolFactory.CreateTool(ToolBase.ToolType.Pencil);
var eraserTool = ToolFactory.CreateTool(ToolBase.ToolType.Eraser);
var selectTool = ToolFactory.CreateTool(ToolBase.ToolType.Select);

canvasView.ToolManager.RegisterTool(canvasTool);
canvasView.ToolManager.RegisterTool(pencilTool);
Expand All @@ -71,8 +66,16 @@ public static PWCanvasView CreateDefaultCanvas()
return canvasView;
}

private void InitializeManager()
{
pageManager = new PageManager();
uiManager = new CanvasUIManager(this);
ToolManager = new ToolManager();
}

private void InitializeCanvas()
{
InitializeManager();
this.RenderingMode = GLRenderingMode.Continuous;
this.RegisterGLCallbacks(renderer.InitializeGL, renderer.RenderFrame, renderer.TerminateGL);
this.SetGraphicsConfig(false, false, 0, GLESVersion.Version20);
Expand Down
53 changes: 53 additions & 0 deletions src/Tizen.NUI.PenWave/src/public/PopupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 Tizen.NUI;
using Tizen.NUI.BaseComponents;

namespace Tizen.NUI.PenWave
{
public class PopupManager
{
private View popupView;
private View parentView;

public PopupManager(View parentView)
{
this.parentView = parentView;
}

public void ShowPopup(View contentView)
{
if (popupView != null) HidePopup();

popupView = new View();
popupView.Add(contentView);
parentView.Add(popupView);
}

public void HidePopup()
{
if (popupView != null)
{
parentView.Remove(popupView);
popupView = null;
}
}
}

}
47 changes: 12 additions & 35 deletions src/Tizen.NUI.PenWave/src/public/Tools/Canvas/CanvasTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public class CanvasTool : ToolBase
new Color("#090E21"),
};

private Icon palettIcon;

public CanvasTool()
{

palettIcon = new PalettIcon();
AddIcon(palettIcon);
}

protected override void StartDrawing(Vector2 position, uint touchTime)
Expand All @@ -57,31 +60,15 @@ protected override void EndDrawing()
{
}

protected override void Deactivate()
protected override void OnIconSelected()
{
EndDrawing();
}
var colorIconsView = CreateColorIconsView();
PopupManager.ShowPopup(colorIconsView);

public override View GetUI()
{
View rootView = new View
{
// Layout = new LinearLayout()
// {
// LinearOrientation = LinearLayout.Orientation.Horizontal,
// },
Layout = new GridLayout { Columns = 1, RowSpacing = 4 }
};
var icon = new PalettIcon();
rootView.Add(icon);
icon.IconSelected += OnIconSelected;

MakePopup(rootView);

return rootView;
Tizen.Log.Error("NUI", $"canvasTool Icon\n");
}

private void MakePopup(View rootView)
private View CreateColorIconsView()
{
var bgImage = new ImageView
{
Expand All @@ -91,34 +78,24 @@ private void MakePopup(View rootView)
Layout = new GridLayout { Columns = 1, RowSpacing = 4 }
};
AddIconsToView(bgImage, BgColors, color => new BackgroundColorIcon(color));
rootView.Add(bgImage);
bgImage.Position2D = new Position2D((int)palettIcon.ScreenPosition.X, (int)palettIcon.ScreenPosition.Y + 48);
return bgImage;
}

private void AddIconsToView<T>(View rootView, IEnumerable<T>items, Func<T, Icon> iconFactory)
{
var view = new View
{
// Layout = new LinearLayout()
// {
// LinearOrientation = LinearLayout.Orientation.Horizontal,
// },
Layout = new GridLayout { Columns = 4, ColumnSpacing = 8, RowSpacing = 8 },
};
foreach (var item in items)
{
var icon = iconFactory(item);
view.Add(icon);
// icon.IconSelected += OnIconSelected;
}

rootView.Add(view);
}

// protected override void OnIconSelected()
// {
// base.OnIconSelected();


// }

}
}
21 changes: 1 addition & 20 deletions src/Tizen.NUI.PenWave/src/public/Tools/Eraser/EraserTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum Mode

public EraserTool()
{

AddIcon(new EraserIcon());
}

protected override void StartDrawing(Vector2 position, uint touchTime)
Expand All @@ -55,24 +55,5 @@ protected override void EndDrawing()
PWEngine.StopErasing();
}

protected override void Deactivate()
{
EndDrawing();
}

public override View GetUI()
{
View rootView = new View
{
Layout = new LinearLayout()
{
LinearOrientation = LinearLayout.Orientation.Horizontal,
},
};
var icon = new EraserIcon();
rootView.Add(icon);
icon.IconSelected += OnIconSelected;
return rootView;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace Tizen.NUI.PenWave
{
public class BrushIcon : Icon
{
private PWEngine.BrushType mBrushType;
private PWEngine.BrushType brushType;
private readonly IBrushStrategy brushStrategy;

private static readonly Dictionary<PWEngine.BrushType, string> IconMap = new Dictionary<PWEngine.BrushType, string>
{
Expand All @@ -40,52 +41,12 @@ public class BrushIcon : Icon
{ PWEngine.BrushType.SharpBrush, "icon_sharp_brush" },
};

private static readonly Dictionary<PWEngine.BrushType, Action> BrushConfigs = new Dictionary<PWEngine.BrushType, Action>
{
{ PWEngine.BrushType.Stroke, () => PWEngine.SetStrokeType(0) },
{ PWEngine.BrushType.VarStroke, () => PWEngine.SetStrokeType(6) },
{ PWEngine.BrushType.VarStrokeInc, () => PWEngine.SetStrokeType(7) },
{ PWEngine.BrushType.SprayBrush, () =>
{
PWEngine.SetStrokeType(1);
PWEngine.SetBrushTexture(0);
PWEngine.SetBrushDistance(3.0f);
}
},
{ PWEngine.BrushType.DotBrush, () =>
{
PWEngine.SetStrokeType(1);
PWEngine.SetBrushTexture(1);
PWEngine.SetBrushDistance(2.0f);
}
},
{ PWEngine.BrushType.DashedLine, () =>
{
PWEngine.SetStrokeType(5);
PWEngine.SetDashArray("1 3");
}
},
{ PWEngine.BrushType.Highlighter, () =>
{
PWEngine.SetStrokeType(1);
PWEngine.SetBrushTexture(3);
PWEngine.SetBrushDistance(0.25f);
}
},
{ PWEngine.BrushType.SoftBrush, () =>
{
PWEngine.SetStrokeType(1);
PWEngine.SetBrushTexture(4);
PWEngine.SetBrushDistance(1.0f);
}
},
{ PWEngine.BrushType.SharpBrush, () => PWEngine.SetStrokeType(8) },
};

private ImageView mImgView;
public BrushIcon(PWEngine.BrushType brushType) : base()
{
mBrushType = brushType;
brushStrategy = BrushStrategyFactory.GetBrushStrategy(brushType);

this.brushType = brushType;

mImgView = new ImageView();
mImgView.Size2D = new Size2D(48, 48);
Expand All @@ -101,17 +62,14 @@ private string GetIconUrl(PWEngine.BrushType brushType)
}


public PWEngine.BrushType GetBrushType() => mBrushType;
public PWEngine.BrushType GetBrushType() => brushType;


public override bool IconClick(object sender, View.TouchEventArgs args)
{
if (base.IconClick(sender, args))
{
if (BrushConfigs.ContainsKey(GetBrushType()))
{
BrushConfigs[GetBrushType()].Invoke();
}
brushStrategy.ApplyBrushSettings();

}
return true;
Expand Down
Loading

0 comments on commit bab96a1

Please sign in to comment.