Skip to content
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

Feature drawing panel #1470

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Source/Fuse.Android/Fuse.Android.unoproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"Fuse.Launcher.Maps",
"Fuse.Launcher.Phone",
"Fuse.Launcher",
"Fuse.Audio"
"Fuse.Audio",
"Fuse.Controls.DrawingPanel"
],
"Projects": [
"../Fuse.Platform/Fuse.Platform.unoproj",
Expand Down
104 changes: 104 additions & 0 deletions Source/Fuse.Controls.DrawingPanel/Bitmap.uno
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Uno;
using Uno.Compiler.ExportTargetInterop;
using Uno.UX;
using Uno.Threading;
using Uno.Collections;
using Fuse.Resources.Exif;
using Fuse.Input;
using Fuse.Scripting;
using Fuse.Controls.Internal;
using Fuse.Controls.Native.iOS;
using Fuse.Controls.Native.Android;

namespace Fuse.Controls
{
extern(ANDROID) internal class Bitmap : IDisposable
{
public Java.Object Handle { get { return _handle; } }

public int2 PixelSize { get { return int2(GetWidth(_handle), GetHeight(_handle)); } }

Java.Object _handle;

Bitmap(byte[] bytes)
{
_handle = Load(Fuse.Android.Bindings.AndroidDeviceInterop.MakeBufferInputStream(bytes));
}

public Bitmap(Java.Object bitmap)
{
_handle = bitmap;
}

public void Dispose()
{
if (_handle != null)
{
Release(_handle);
_handle = null;
}
}

public static Bitmap FromJpegBytes(byte[] bytes)
{
return new Bitmap(bytes);
}

class SavePromise : Promise<string>
{
public void OnRejected(string msg) { Reject(new Exception(msg)); }
}

public Future<string> SaveJpeg()
{
var sp = new SavePromise();
SaveJpeg(_handle, sp.Resolve, sp.OnRejected);
return sp;
}

[Foreign(Language.Java)]
static void SaveJpeg(
Java.Object handle,
Action<string> onResolve,
Action<string> onReject)
@{
String filePath;
try {
filePath = com.fuse.camera.ImageStorageTools.createFilePath("jpeg", true);
java.io.FileOutputStream file = new java.io.FileOutputStream(filePath);
((android.graphics.Bitmap)handle).compress(
android.graphics.Bitmap.CompressFormat.JPEG,
100,
file);
file.close();
onResolve.run(filePath);
} catch(Exception e) {
onReject.run(e.getMessage());
}
@}

[Foreign(Language.Java)]
static int GetWidth(Java.Object handle)
@{
return ((android.graphics.Bitmap)handle).getWidth();
@}

[Foreign(Language.Java)]
static int GetHeight(Java.Object handle)
@{
return ((android.graphics.Bitmap)handle).getHeight();
@}

[Foreign(Language.Java)]
static void Release(Java.Object handle)
@{
((android.graphics.Bitmap)handle).recycle();
@}

[Foreign(Language.Java)]
static Java.Object Load(Java.Object buf)
@{
return android.graphics.BitmapFactory.decodeStream((java.io.InputStream)buf);
@}
}
}
64 changes: 64 additions & 0 deletions Source/Fuse.Controls.DrawingPanel/Drawing.uno
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Uno;
using Uno.UX;
using Uno.Threading;
using Uno.Collections;
using Fuse.Scripting;

namespace Fuse.Controls.Internal
{
internal struct Line
{
public readonly float2 From;
public readonly float2 To;
public readonly float Width;
public readonly float4 Color;

public Line(float2 from, float2 to, float width, float4 color)
{
From = from;
To = to;
Width = width;
Color = color;
}

public Line Scale(float scale)
{
return new Line(From * scale, To * scale, Width * scale, Color);
}
}

internal struct Circle
{
public readonly float2 Center;
public readonly float Radius;
public readonly float4 Color;

public Circle(float2 center, float radius, float4 color)
{
Center = center;
Radius = radius;
Color = color;
}

public Circle Scale(float scale)
{
return new Circle(Center * scale, Radius * scale, Color);
}
}

internal class Stroke
{
public readonly List<Line> Lines = new List<Line>();
public readonly List<Circle> Circles = new List<Circle>();

public Stroke Scale(float scale)
{
var stroke = new Stroke();
foreach (var line in Lines)
stroke.Lines.Add(line.Scale(scale));
foreach (var circle in Circles)
stroke.Circles.Add(circle.Scale(scale));
return stroke;
}
}
}
Loading