Skip to content

Commit 1314e6c

Browse files
committed
Feat: pass selected tool to preview [3/3]
1 parent bb7589d commit 1314e6c

File tree

5 files changed

+115
-103
lines changed

5 files changed

+115
-103
lines changed

lib/components/canvas/canvas.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class Canvas extends StatelessWidget {
5656
} else if (currentTool is Eraser) {
5757
return 1;
5858
} else if (currentTool is Select) {
59-
return 3;
59+
return 1;
6060
} else if (currentTool is LaserPointer) {
61-
return 4;
61+
return 1;
6262
} else if (currentTool is Pen) {
6363
if ((currentTool as Pen).isPressureEnabled()) {
6464
return 2;
@@ -72,26 +72,26 @@ class Canvas extends StatelessWidget {
7272

7373
int getColor() {
7474
if (currentTool is Pen) {
75-
var color = (currentTool as Pen).color.toARGB32();
76-
print('color is $color');
77-
return color;
75+
return (currentTool as Pen).color.toARGB32();
7876
} else {
79-
return 0;
77+
return Colors.black.toARGB32();
8078
}
8179
}
8280
double getWidth() {
83-
if (currentTool is Highlighter) {
84-
return (currentTool as Pen).getSize() * currentScale * 2;
85-
} else if (currentTool is Pen) {
86-
return (currentTool as Pen).getSize() * currentScale;
81+
if (currentTool is Pen) {
82+
double baseSize = (currentTool as Pen).getSize() * currentScale;
83+
if ((currentTool as Pen).isPressureEnabled()) {
84+
return baseSize;
85+
} else {
86+
return baseSize * 2;
87+
}
8788
} else {
8889
return 3.0;
8990
}
9091
}
9192

9293
@override
9394
Widget build(BuildContext context) {
94-
print('currentTool: $currentTool');
9595
return Center(
9696
child: FittedBox(
9797
child: DecoratedBox(
@@ -111,10 +111,10 @@ class Canvas extends StatelessWidget {
111111
width: page.size.width,
112112
height: page.size.height,
113113
child: OnyxSdkPenArea(
114-
refreshDelay: const Duration(seconds: 1),
115-
strokeStyle: toolToOnyx(currentTool),
116-
strokeColor: getColor(),
117-
strokeWidth: getWidth(),
114+
refreshDelay: const Duration(seconds: 1),
115+
strokeStyle: toolToOnyx(currentTool),
116+
strokeColor: getColor(),
117+
strokeWidth: getWidth(),
118118
child: InnerCanvas(
119119
key: page.innerCanvasKey,
120120
pageIndex: pageIndex,

packages/onyxsdk_pen/android/src/main/kotlin/com/example/onyxsdk_pen/OnyxsdkPenArea.kt

Lines changed: 89 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@ import android.graphics.Canvas
55
import android.graphics.Color
66
import android.graphics.Paint
77
import android.graphics.Rect
8-
import android.util.Log
98
import android.view.SurfaceView
109
import android.view.View
1110
import com.onyx.android.sdk.data.note.TouchPoint
1211
import com.onyx.android.sdk.pen.RawInputCallback
1312
import com.onyx.android.sdk.pen.TouchHelper
1413
import com.onyx.android.sdk.pen.data.TouchPointList
15-
import com.onyx.android.sdk.pen.NeoBrushPen
16-
import com.onyx.android.sdk.pen.NeoCharcoalPenV2
17-
import com.onyx.android.sdk.pen.NeoMarkerPen
18-
import com.onyx.android.sdk.pen.NeoFountainPen
19-
import com.onyx.android.sdk.pen.NeoPen
20-
import com.onyx.android.sdk.api.device.epd.EpdController
2114
import io.flutter.plugin.platform.PlatformView
2215
import io.flutter.plugin.common.MethodChannel
2316
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
@@ -29,72 +22,88 @@ import java.util.TimerTask
2922
import androidx.annotation.NonNull
3023

3124
internal class OnyxsdkPenArea(context: Context, messenger: BinaryMessenger, id: Int, creationParams: Map<String?, Any?>?) : PlatformView, MethodCallHandler {
25+
3226
enum class StrokeStyle(val Id: Int) {
33-
FountainPen(0),
34-
Pen(1),
35-
Brush(2),
36-
Pencil(3),
37-
Marker(4)
27+
FountainPen(0),
28+
Pen(1),
29+
Brush(2),
30+
Pencil(3),
31+
Marker(4)
3832
}
33+
3934
private val channel: MethodChannel = MethodChannel(messenger, "onyxsdk_pen_area")
35+
4036
companion object {
4137
private val pointsToRedraw = 20
42-
4338
}
39+
4440
private var strokeWidth = 0.0f
45-
var strokeColor = Color.BLACK
46-
var strokeStyle = StrokeStyle.FountainPen
41+
private var strokeColor = Color.BLACK
42+
private var strokeStyle = StrokeStyle.FountainPen
4743

4844
private fun updateStroke(paramsRef: Map<String, Any>?) {
49-
/*
50-
Flutter ints are variable width
51-
Personally, I think it is utterly dumb. I hope there is a way to fix
52-
int size in flutter (why would you need 64 bits to store srgb??),
53-
but unless or until there isn't, this stupidity should be performed
54-
*/
55-
val pureColor = (paramsRef?.get("strokeColor") as? Long)?.toInt()
56-
?: paramsRef?.get("strokeColor") as? Int
57-
?: Color.BLACK
58-
/*
59-
ONYXSDK Pen saturates the colors. Whatever it deems as "light" (luma < 128?)
60-
gets automagically turned to white. Transparency is also accounted apparently.
61-
Unless this is some case-by-case situation, if resulting color, assuming
62-
being drawn on absolutely white background, with a fully opaque brush
63-
is lighter than a certain unknown arbitrary threshold, it turns white,
64-
otherwise, is saturated to max.
65-
These thoughts are, however, purely observational. Nothing in documentation
66-
says this and no binary artifacts have been decompiled to come to these
67-
conclusions. Thus, feel free to correct my incompetence if I turn out wrong
68-
*/
69-
var dest = FloatArray(3)
70-
Color.RGBToHSV(
71-
Color.red(pureColor),
72-
Color.green(pureColor),
73-
Color.blue(pureColor),
74-
dest
75-
)
76-
dest[1] = 1.0f /* saturation */
77-
/*
78-
I suppose rounding value is reasonable, but I want color to show
79-
So clamp to black if it's visually indistinguishable from black
80-
and to colorful otherwise. E.g. brown will look red instead of black
81-
*/
82-
if (dest[2] < 0.2) {
83-
dest[2] = 0.0f
84-
} else {
85-
dest[2] = 1.0f
86-
}
87-
strokeColor = Color.HSVToColor(dest)
88-
strokeWidth = (paramsRef?.get("strokeWidth") as? Double ?: 3.0).toFloat()
89-
strokeStyle = when (paramsRef?.get("strokeStyle") as? Int ?: 0) {
90-
0 -> StrokeStyle.FountainPen
91-
1 -> StrokeStyle.Pen
92-
2 -> StrokeStyle.Brush
93-
3 -> StrokeStyle.Pencil
94-
4 -> StrokeStyle.Marker
95-
else -> StrokeStyle.Pen
96-
}
97-
Log.d("SABERSTROKEUPDATE", "color: %d, width: %f, style: %s".format(strokeColor, strokeWidth, strokeStyle.name))
45+
/*
46+
Flutter ints are variable width
47+
Personally, I think it is utterly dumb. I hope there is a way to fix
48+
int size in flutter (why would you need 64 bits to store srgb??),
49+
but unless or until there isn't, this stupidity should be performed
50+
*/
51+
val pureColor = (paramsRef?.get("strokeColor") as? Long)?.toInt()
52+
?: paramsRef?.get("strokeColor") as? Int
53+
?: Color.BLACK
54+
/*
55+
ONYXSDK Pen saturates the colors. Whatever it deems as "light" (luma < 128?)
56+
gets automagically turned to white. Transparency is also accounted apparently.
57+
Unless this is some case-by-case situation, if resulting color, assuming
58+
being drawn on absolutely white background, with a fully opaque brush
59+
is lighter than a certain unknown arbitrary threshold, it turns white,
60+
otherwise, is saturated to max.
61+
These thoughts are, however, purely observational. Nothing in documentation
62+
says this and no binary artifacts have been decompiled to come to these
63+
conclusions. Thus, feel free to correct my incompetence if I turn out wrong
64+
*/
65+
var dest = FloatArray(3)
66+
Color.RGBToHSV(
67+
Color.red(pureColor),
68+
Color.green(pureColor),
69+
Color.blue(pureColor),
70+
dest
71+
)
72+
/*
73+
Saturation
74+
I suppose clamping is a sound idea, but the extremes to which the lib
75+
takes them are unacceptable. Make it white if visually indistinguishable
76+
from white and colorful otherwise.
77+
*/
78+
if (dest[1] < 0.05) {
79+
dest[1] = 0.0f
80+
} else {
81+
dest[1] = 1.0f
82+
}
83+
/*
84+
Value
85+
I want color to show
86+
So clamp to black if it's visually indistinguishable from black
87+
and to colorful otherwise. E.g. brown will look red instead of black
88+
*/
89+
if (dest[2] < 0.2) {
90+
dest[2] = 0.0f
91+
} else {
92+
dest[2] = 1.0f
93+
}
94+
strokeColor = Color.HSVToColor(dest)
95+
strokeWidth = (paramsRef?.get("strokeWidth") as? Double ?: 3.0).toFloat()
96+
strokeStyle = when (paramsRef?.get("strokeStyle") as? Int ?: 0) {
97+
0 -> StrokeStyle.FountainPen
98+
1 -> StrokeStyle.Pen
99+
2 -> StrokeStyle.Brush
100+
3 -> StrokeStyle.Pencil
101+
4 -> StrokeStyle.Marker
102+
else -> StrokeStyle.Pen
103+
}
104+
touchHelper.setStrokeStyle(strokeStyleToOnyx(strokeStyle))
105+
touchHelper.setStrokeWidth(strokeWidth)
106+
touchHelper.setStrokeColor(strokeColor)
98107
}
99108

100109

@@ -106,7 +115,6 @@ internal class OnyxsdkPenArea(context: Context, messenger: BinaryMessenger, id:
106115
}
107116

108117
private val paint: Paint = Paint()
109-
private val deviceMaxPressure = EpdController.getMaxTouchPressure()
110118
private var pointsSinceLastRedraw = 0
111119

112120
private val currentStroke: ArrayList<TouchPoint> = ArrayList()
@@ -116,10 +124,6 @@ internal class OnyxsdkPenArea(context: Context, messenger: BinaryMessenger, id:
116124

117125
private val callback: RawInputCallback = object: RawInputCallback() {
118126
fun reset() {
119-
touchHelper.setStrokeStyle(strokeStyleToOnyx(strokeStyle))
120-
touchHelper.setStrokeWidth(strokeWidth)
121-
touchHelper.setStrokeColor(strokeColor)
122-
123127
currentStroke.clear()
124128
pointsSinceLastRedraw = 0
125129
drawPreview()
@@ -184,21 +188,21 @@ internal class OnyxsdkPenArea(context: Context, messenger: BinaryMessenger, id:
184188
}
185189

186190
private fun strokeStyleToOnyx(style: StrokeStyle): Int {
187-
return when (style) {
188-
StrokeStyle.FountainPen -> TouchHelper.STROKE_STYLE_FOUNTAIN
189-
StrokeStyle.Pen -> TouchHelper.STROKE_STYLE_PENCIL
190-
StrokeStyle.Brush -> TouchHelper.STROKE_STYLE_NEO_BRUSH
191-
StrokeStyle.Pencil -> TouchHelper.STROKE_STYLE_CHARCOAL_V2
192-
StrokeStyle.Marker -> TouchHelper.STROKE_STYLE_MARKER
193-
}
191+
return when (style) {
192+
StrokeStyle.FountainPen -> TouchHelper.STROKE_STYLE_FOUNTAIN
193+
StrokeStyle.Pen -> TouchHelper.STROKE_STYLE_PENCIL
194+
StrokeStyle.Brush -> TouchHelper.STROKE_STYLE_NEO_BRUSH
195+
StrokeStyle.Pencil -> TouchHelper.STROKE_STYLE_CHARCOAL
196+
StrokeStyle.Marker -> TouchHelper.STROKE_STYLE_MARKER
197+
}
194198
}
195199

196200
fun drawPreview() {
197-
currentStroke.clear()
201+
currentStroke.clear()
198202
}
199203

200204
init {
201-
channel.setMethodCallHandler(this)
205+
channel.setMethodCallHandler(this)
202206
view.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
203207
val limit = Rect()
204208
val exclude = emptyList<Rect>()
@@ -220,13 +224,13 @@ internal class OnyxsdkPenArea(context: Context, messenger: BinaryMessenger, id:
220224
}
221225

222226
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
223-
if (call.method == "updateStroke") {
224-
val params = call.arguments<Map<String, Any>?>()
225-
updateStroke(params)
226-
result.success(null)
227-
} else {
228-
result.notImplemented()
229-
}
227+
if (call.method == "updateStroke") {
228+
val params = call.arguments<Map<String, Any>?>()
229+
updateStroke(params)
230+
result.success(null)
231+
} else {
232+
result.notImplemented()
233+
}
230234
}
231235

232236
override fun dispose() {

packages/onyxsdk_pen/android/src/main/kotlin/com/example/onyxsdk_pen/OnyxsdkPenPlugin.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class OnyxsdkPenPlugin: FlutterPlugin, MethodCallHandler {
1717
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
1818
/// when the Flutter Engine is detached from the Activity
1919
private lateinit var channel : MethodChannel
20-
private lateinit var channelInstance : MethodChannel
2120

2221
override fun onAttachedToEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
2322
channel = MethodChannel(binding.binaryMessenger, "onyxsdk_pen")
@@ -29,7 +28,10 @@ class OnyxsdkPenPlugin: FlutterPlugin, MethodCallHandler {
2928

3029
binding
3130
.platformViewRegistry
32-
.registerViewFactory("onyxsdk_pen_area", OnyxsdkPenAreaFactory(binding.binaryMessenger))
31+
.registerViewFactory(
32+
"onyxsdk_pen_area",
33+
OnyxsdkPenAreaFactory(binding.binaryMessenger)
34+
)
3335
}
3436

3537
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {

packages/onyxsdk_pen/lib/onyxsdk_pen_area.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class _OnyxSdkPenAreaState extends State<OnyxSdkPenArea> {
9494
@override
9595
Widget build(BuildContext context) {
9696
if (!isOnyxDevice) return widget.child;
97-
creationParams.forEach((k, v) => print('$k: $v'));
9897
return Stack(
9998
fit: StackFit.expand,
10099
children: [

packages/onyxsdk_pen_dummy/lib/onyxsdk_pen_area.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ class OnyxSdkPenArea extends StatelessWidget {
44
const OnyxSdkPenArea({
55
super.key,
66
this.refreshDelay = const Duration(seconds: 1),
7+
this.strokeStyle = 0,
8+
this.strokeColor = 0,
9+
this.strokeWidth = 3.0,
10+
711
required this.child,
812
});
913

1014
final Duration refreshDelay;
15+
final int strokeStyle;
16+
final int strokeColor;
17+
final double strokeWidth;
1118

1219
final Widget child;
1320

0 commit comments

Comments
 (0)