This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 459
Drawing View #740
Merged
Merged
Drawing View #740
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
8292f3b
DrawingView (#468)
VladislavAntonyuk 9b2c001
fixed codestyle on Android renderer
pictos 6132b34
GTK renderer adjustments
pictos caae16f
iOS renderer adjustments
pictos 5ff9aa0
MacOS renderer adjustments
pictos 0b884b4
TIzen rendere adjustments
pictos 648438c
UWP renderer adjustments
pictos 2a30395
WPF tests
pictos 6cce0f3
Merge branch 'develop' of https://github.com/xamarin/XamarinCommunity…
pictos e37af72
Fixed merge conflict
pictos c0addd2
Fixed wpf stackoverflow exception
pictos 9d427d9
fixed iOS leaks
pictos f41d645
revert toast change
pictos 02e1fc4
removed wpf implementations
pictos 15fe5df
Merge branch 'develop' of https://github.com/xamarin/XamarinCommunity…
pictos 4f22164
root sample page to WelcomePage
pictos 42a36c8
removed DrawingView ctor from MainWindow
pictos d47a7a4
removed drawing instructions from readme
pictos a203fdc
Merge branch 'develop' into pj/drawingWork
pictos b987997
Update README.md
pictos 8035c2d
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 496444d
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler cb02457
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 3c54c11
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 1561449
Merge branch 'develop' into pj/drawingWork
jsuarezruiz 865e5eb
Merge branch 'develop' into pj/drawingWork
jsuarezruiz 80360fb
Merge branch 'develop' into pj/drawingWork
pictos 873cdd5
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 6d4dcfe
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 2f3fc5c
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 782a749
Merge branch 'develop' into pj/drawingWork
TheCodeTraveler 8f6c43d
Merge branch 'develop' into pj/drawingWork
jsuarezruiz 30e60a6
Merge branch 'develop' into pj/drawingWork
pictos 0462801
Drawing View - Fix nullable, add doc to the public method (#1293)
VladislavAntonyuk dc46d32
Merge branch 'develop' into pj/drawingWork
pictos 7248f21
return null instead of exception (#1339)
VladislavAntonyuk c59a709
Merge branch 'develop' into pj/drawingWork
pictos 0acada1
Merge branch 'develop' into pj/drawingWork
pictos feef70b
Merge branch 'develop' into pj/drawingWork
jfversluis 7a2996a
Merge branch 'develop' into pj/drawingWork
jfversluis dfabca1
Merge branch 'develop' into pj/drawingWork
jfversluis 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System.Collections.Specialized; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Windows.Controls; | ||
using System.Windows.Ink; | ||
using System.Windows.Input; | ||
using Xamarin.CommunityToolkit.Sample.WPF; | ||
using Xamarin.CommunityToolkit.UI.Views; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Platform.WPF; | ||
|
||
[assembly: ExportRenderer(typeof(DrawingView), typeof(DrawingViewRenderer))] | ||
namespace Xamarin.CommunityToolkit.Sample.WPF | ||
{ | ||
public class DrawingViewRenderer : ViewRenderer<DrawingView, InkCanvas> | ||
{ | ||
InkCanvas? canvas; | ||
|
||
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | ||
{ | ||
base.OnElementPropertyChanged(sender, e); | ||
if (e.PropertyName == DrawingView.PointsProperty.PropertyName) | ||
{ | ||
canvas!.Strokes.StrokesChanged -= OnStrokesChanged; | ||
canvas.Strokes.Clear(); | ||
LoadPoints(); | ||
canvas.Strokes.StrokesChanged += OnStrokesChanged; | ||
} | ||
} | ||
|
||
protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e) | ||
{ | ||
base.OnElementChanged(e); | ||
if (Control == null && Element != null) | ||
{ | ||
canvas = new InkCanvas | ||
{ | ||
DefaultDrawingAttributes = | ||
{ | ||
Color = Element.LineColor.ToMediaColor(), | ||
Width = Element.LineWidth, | ||
Height = Element.LineWidth | ||
}, | ||
Background = Element.BackgroundColor.ToBrush() | ||
}; | ||
Element.Points.CollectionChanged += OnCollectionChanged; | ||
SetNativeControl(canvas); | ||
|
||
canvas.Strokes.StrokesChanged += OnStrokesChanged; | ||
Control!.PreviewMouseDown += OnPreviewMouseDown; | ||
} | ||
|
||
if (e.OldElement != null) | ||
{ | ||
canvas!.Strokes.StrokesChanged -= OnStrokesChanged; | ||
Element!.Points.CollectionChanged -= OnCollectionChanged; | ||
if (Control != null) | ||
Control.PreviewMouseDown -= OnPreviewMouseDown; | ||
} | ||
} | ||
|
||
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) => LoadPoints(); | ||
|
||
void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) | ||
{ | ||
canvas!.Strokes.Clear(); | ||
Element.Points.Clear(); | ||
} | ||
|
||
void OnStrokesChanged(object sender, StrokeCollectionChangedEventArgs e) | ||
{ | ||
Element.Points.CollectionChanged -= OnCollectionChanged; | ||
if (e.Added.Count > 0) | ||
{ | ||
var points = e.Added.First().StylusPoints.Select(point => new Point(point.X, point.Y)); | ||
Element.Points.Clear(); | ||
foreach (var point in points) | ||
Element.Points.Add(point); | ||
|
||
if (Element.Points.Count > 0) | ||
{ | ||
if (Element.DrawingCompletedCommand.CanExecute(null)) | ||
Element.DrawingCompletedCommand.Execute(Element.Points); | ||
} | ||
|
||
if (Element.ClearOnFinish) | ||
Element.Points.Clear(); | ||
} | ||
Element.Points.CollectionChanged += OnCollectionChanged; | ||
} | ||
|
||
void LoadPoints() | ||
{ | ||
var stylusPoints = Element?.Points.Select(point => new StylusPoint(point.X, point.Y)).ToList(); | ||
if (stylusPoints is { Count: > 0 }) | ||
{ | ||
var stroke = new Stroke(new StylusPointCollection(stylusPoints), canvas!.DefaultDrawingAttributes); | ||
canvas.Strokes.Add(stroke); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,4 +183,4 @@ | |
<ItemGroup> | ||
<BundleResource Include="Resources\triangle.png" /> | ||
</ItemGroup> | ||
</Project> | ||
</Project> |
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,67 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<pages:BasePage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:views="clr-namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit" | ||
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.DrawingViewPage" | ||
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages" | ||
xmlns:viewsVodel="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Views;assembly=Xamarin.CommunityToolkit.Sample"> | ||
|
||
<pages:BasePage.BindingContext> | ||
<viewsVodel:DrawingViewViewModel/> | ||
</pages:BasePage.BindingContext> | ||
|
||
<ScrollView> | ||
<StackLayout> | ||
<Switch x:Name="ClearOnFinish" /> | ||
<Button | ||
BackgroundColor="White" | ||
Clicked="LoadPointsButtonClicked" | ||
Text="Load points" | ||
TextColor="Black" /> | ||
<Button | ||
BackgroundColor="White" | ||
Command="{Binding SetPointsCommand}" | ||
Text="Load points ViewModel" | ||
TextColor="Black" /> | ||
<Label | ||
x:Name="HiddenLabel" | ||
HorizontalOptions="CenterAndExpand" | ||
IsVisible="False" | ||
Text="Draw & GO" /> | ||
<Button | ||
BackgroundColor="White" | ||
Clicked="DisplayHiddenLabelButtonClicked" | ||
Text="Display hidden label" | ||
TextColor="Black" /> | ||
<Button | ||
BackgroundColor="White" | ||
Command="{Binding GetPointsCommand}" | ||
Text="Get points ViewModel" | ||
TextColor="Black" /> | ||
<Button | ||
BackgroundColor="White" | ||
Clicked="GetCurrentDrawingViewImageClicked" | ||
Text="Display current gesture pad image" | ||
TextColor="Black" /> | ||
<Button | ||
BackgroundColor="White" | ||
Clicked="GetImageClicked" | ||
Text="Display image" | ||
TextColor="Black" /> | ||
<Image | ||
x:Name="GestureImage" | ||
HeightRequest="100" | ||
WidthRequest="100" /> | ||
<views:DrawingView | ||
x:Name="DrawingViewControl" | ||
Points="{Binding MacroPoints, Mode=TwoWay}" | ||
BackgroundColor="DarkGray" | ||
ClearOnFinish="{Binding Source={x:Reference ClearOnFinish}, Path=IsToggled}" | ||
HorizontalOptions="FillAndExpand" | ||
LineColor="Red" | ||
LineWidth="10" | ||
VerticalOptions="FillAndExpand" /> | ||
<Editor x:Name="Logs" AutoSize="TextChanges" /> | ||
</StackLayout> | ||
</ScrollView> | ||
</pages:BasePage> |
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,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Xamarin.Forms; | ||
using Xamarin.CommunityToolkit.UI.Views; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace Xamarin.CommunityToolkit.Sample.Pages.Views | ||
{ | ||
public partial class DrawingViewPage : BasePage | ||
{ | ||
public DrawingViewPage() | ||
{ | ||
InitializeComponent(); | ||
DrawingViewControl.Points = GeneratePoints(200); | ||
DrawingViewControl.DrawingCompletedCommand = new Command<ObservableCollection<Point>>(points => | ||
{ | ||
Logs.Text += "GestureCompletedCommand executed" + Environment.NewLine; | ||
DrawImage(points); | ||
}); | ||
} | ||
|
||
void LoadPointsButtonClicked(object sender, EventArgs e) => DrawingViewControl.Points = GeneratePoints(50); | ||
|
||
void DisplayHiddenLabelButtonClicked(object sender, EventArgs e) => HiddenLabel.IsVisible = !HiddenLabel.IsVisible; | ||
|
||
void GetCurrentDrawingViewImageClicked(object sender, EventArgs e) | ||
{ | ||
var stream = DrawingViewControl.GetImageStream(GestureImage.Width, GestureImage.Height); | ||
GestureImage.Source = ImageSource.FromStream(() => stream); | ||
} | ||
|
||
void GetImageClicked(object sender, EventArgs e) | ||
{ | ||
var points = GeneratePoints(100); | ||
DrawImage(points); | ||
} | ||
|
||
static ObservableCollection<Point> GeneratePoints(int count) | ||
{ | ||
var points = new ObservableCollection<Point>(); | ||
for (var i = 0; i < count; i++) | ||
{ | ||
points.Add(new Point(i, i)); | ||
} | ||
|
||
return points; | ||
} | ||
|
||
void DrawImage(IEnumerable<Point> points) | ||
{ | ||
var stream = DrawingView.GetImageStream(points, new Size(GestureImage.Width, GestureImage.Height), 10, | ||
Color.White, Color.Black); | ||
GestureImage.Source = ImageSource.FromStream(() => stream); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅