-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathWhiteboard.kt
More file actions
631 lines (580 loc) · 21.8 KB
/
Whiteboard.kt
File metadata and controls
631 lines (580 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* Copyright (c) 2009 Andrew <andrewdubya@gmail.com>
* Copyright (c) 2009 Nicolas Raoul <nicolas.raoul@gmail.com>
* Copyright (c) 2009 Edu Zamora <edu.zasu@gmail.com>
* Copyright (c) 2021 Nicolai Weitkemper <kontakt@nicolaiweitkemper.de>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PathMeasure
import android.graphics.Point
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.Region
import android.net.Uri
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.CheckResult
import androidx.annotation.VisibleForTesting
import androidx.core.content.edit
import androidx.core.graphics.scale
import com.ichi2.anki.common.android.appContext
import com.ichi2.anki.common.annotations.NeedsTest
import com.ichi2.anki.common.time.Time
import com.ichi2.anki.common.time.getTimestamp
import com.ichi2.anki.compat.CompatHelper
import com.ichi2.anki.dialogs.WhiteBoardWidthDialog
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.settings.enums.NightTheme
import com.ichi2.anki.ui.windows.reviewer.whiteboard.showColorPickerDialog
import com.ichi2.themes.Themes.currentTheme
import com.ichi2.utils.DisplayUtils.getDisplayDimensions
import timber.log.Timber
import java.io.FileNotFoundException
import kotlin.math.abs
import kotlin.math.max
/**
* Whiteboard allowing the user to draw the card's answer on the touchscreen.
*/
@SuppressLint("ViewConstructor")
@NeedsTest("15176 ensure whiteboard drawing works")
class Whiteboard(
activity: AnkiActivity,
private val handleMultiTouch: Boolean,
inverted: Boolean,
) : View(activity, null) {
private val paint: Paint
private val undo = UndoList()
private lateinit var bitmap: Bitmap
private lateinit var canvas: Canvas
private val path: Path
private val bitmapPaint: Paint
private val ankiActivity: AnkiActivity = activity
private var x = 0f
private var y = 0f
private var secondFingerX0 = 0f
private var secondFingerY0 = 0f
private var secondFingerX = 0f
private var secondFingerY = 0f
private var secondFingerPointerId = 0
private var secondFingerWithinTapTolerance = false
var reviewerEraserModeIsToggledOn = false
var toggleStylus = false
var isCurrentlyDrawing = false
private set
@get:CheckResult
@get:VisibleForTesting
var foregroundColor = 0
private val colorPalette: LinearLayout
var onPaintColorChangeListener: OnPaintColorChangeListener? = null
private val currentStrokeWidth: Int
get() = ankiActivity.sharedPrefs().getInt("whiteBoardStrokeWidth", 6)
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.apply {
drawColor(0)
drawBitmap(bitmap, 0f, 0f, bitmapPaint)
drawPath(path, paint)
}
}
/** Handle motion events to draw using the touch screen or to interact with the flashcard behind
* the whiteboard by using a second finger.
*
* @param event The motion event.
* @return True if the event was handled, false otherwise
*/
fun handleTouchEvent(event: MotionEvent): Boolean = handleDrawEvent(event) || handleMultiTouchEvent(event)
/**
* Handle motion events to draw using the touch screen. Only simple touch events are processed,
* a multitouch event aborts to current stroke.
*
* @param event The motion event.
* @return True if the event was handled, false otherwise or when drawing was aborted due to
* detection of a multitouch event.
*/
private fun handleDrawEvent(event: MotionEvent): Boolean {
val x = event.x
val y = event.y
if (event.getToolType(event.actionIndex) == MotionEvent.TOOL_TYPE_ERASER || reviewerEraserModeIsToggledOn) {
eraseTouchedStroke(event)
return true
}
if (event.getToolType(event.actionIndex) != MotionEvent.TOOL_TYPE_STYLUS && toggleStylus) {
return false
}
return when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
if (event.buttonState == MotionEvent.BUTTON_STYLUS_PRIMARY) {
eraseTouchedStroke(event)
} else {
drawStart(x, y)
invalidate()
}
true
}
MotionEvent.ACTION_MOVE -> {
if (event.buttonState == MotionEvent.BUTTON_STYLUS_PRIMARY) {
eraseTouchedStroke(event)
return true
}
if (isCurrentlyDrawing) {
for (i in 0 until event.historySize) {
drawAlong(event.getHistoricalX(i), event.getHistoricalY(i))
}
drawAlong(x, y)
invalidate()
return true
}
false
}
MotionEvent.ACTION_UP -> {
if (isCurrentlyDrawing) {
drawFinish()
invalidate()
return true
}
false
}
MotionEvent.ACTION_POINTER_DOWN -> {
if (isCurrentlyDrawing) {
drawAbort()
}
false
}
211, 213 -> {
if (event.buttonState == MotionEvent.BUTTON_STYLUS_PRIMARY) {
eraseTouchedStroke(event)
}
true
}
else -> false
}
}
// Parse multitouch input to scroll the card behind the whiteboard or click on elements
private fun handleMultiTouchEvent(event: MotionEvent): Boolean =
if (handleMultiTouch && event.pointerCount == 2) {
when (event.actionMasked) {
MotionEvent.ACTION_POINTER_DOWN -> {
reinitializeSecondFinger(event)
true
}
MotionEvent.ACTION_MOVE -> trySecondFingerScroll(event)
MotionEvent.ACTION_POINTER_UP -> trySecondFingerClick(event)
else -> false
}
} else {
false
}
/**
* Erase touched stroke (= path or point)
* (by toggling the eraser action button on
* or by using the eraser button on the stylus pen
* or by using the digital eraser)
*/
private fun eraseTouchedStroke(event: MotionEvent) {
if (!undoEmpty()) {
val didErase = undo.erase(event.x.toInt(), event.y.toInt())
if (didErase) {
undo.apply()
if (undoEmpty()) {
ankiActivity.invalidateOptionsMenu()
}
}
}
}
/**
* Clear the whiteboard.
*/
fun clear() {
bitmap.eraseColor(0)
undo.clear()
invalidate()
ankiActivity.invalidateOptionsMenu()
}
/**
* Undo the last stroke
*/
fun undo() {
undo.pop()
undo.apply()
if (undoEmpty()) {
ankiActivity.invalidateOptionsMenu()
}
}
/** @return Whether there are strokes to undo
*/
fun undoEmpty(): Boolean = undo.empty()
private fun createBitmap(
w: Int,
h: Int,
) {
val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
this.bitmap = bitmap
canvas = Canvas(bitmap)
clear()
}
private fun createBitmap() {
// To fix issue #1336, just make the whiteboard big and square.
val p = displayDimensions
val bitmapSize = max(p.x, p.y)
createBitmap(bitmapSize, bitmapSize)
}
/**
* On rotating the device onSizeChanged() helps to stretch the previously created Bitmap rather
* than creating a new Bitmap which makes sure bitmap doesn't go out of screen.
*/
override fun onSizeChanged(
w: Int,
h: Int,
oldw: Int,
oldh: Int,
) {
super.onSizeChanged(w, h, oldw, oldh)
// createScaledBitmap requires a width and height > 0; #13972
if (w <= 0 || h <= 0) {
Timber.w("Width or height <= 0: w: $w h: $h Bitmap couldn't be created with the new size")
return
}
val scaledBitmap: Bitmap = bitmap.scale(w, h, filter = true)
bitmap = scaledBitmap
canvas = Canvas(bitmap)
}
private fun drawStart(
x: Float,
y: Float,
) {
isCurrentlyDrawing = true
path.reset()
path.moveTo(x, y)
this.x = x
this.y = y
}
private fun drawAlong(
x: Float,
y: Float,
) {
val dx = abs(x - this.x)
val dy = abs(y - this.y)
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
path.quadTo(this.x, this.y, (this.x + x) / 2, (this.y + y) / 2)
this.x = x
this.y = y
}
}
private fun drawFinish() {
isCurrentlyDrawing = false
val pm = PathMeasure(path, false)
path.lineTo(x, y)
val paint = Paint(paint)
val action = if (pm.length > 0) DrawPath(Path(path), paint) else DrawPoint(x, y, paint)
action.apply(canvas)
undo.add(action)
// kill the path so we don't double draw
path.reset()
if (undo.size() == 1) {
ankiActivity.invalidateOptionsMenu()
}
}
private fun drawAbort() {
drawFinish()
undo()
}
// call this with an ACTION_POINTER_DOWN event to start a new round of detecting drag or tap with
// a second finger
private fun reinitializeSecondFinger(event: MotionEvent) {
secondFingerWithinTapTolerance = true
secondFingerPointerId = event.getPointerId(event.actionIndex)
secondFingerX0 = event.getX(event.findPointerIndex(secondFingerPointerId))
secondFingerY0 = event.getY(event.findPointerIndex(secondFingerPointerId))
}
private fun updateSecondFinger(event: MotionEvent): Boolean {
val pointerIndex = event.findPointerIndex(secondFingerPointerId)
if (pointerIndex > -1) {
secondFingerX = event.getX(pointerIndex)
secondFingerY = event.getY(pointerIndex)
val dx = abs(secondFingerX0 - secondFingerX)
val dy = abs(secondFingerY0 - secondFingerY)
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
secondFingerWithinTapTolerance = false
}
return true
}
return false
}
// call this with an ACTION_POINTER_UP event to check whether it matches a tap of the second finger
// if so, forward a click action and return true
private fun trySecondFingerClick(event: MotionEvent): Boolean {
if (secondFingerPointerId == event.getPointerId(event.actionIndex)) {
updateSecondFinger(event)
if (secondFingerWithinTapTolerance && whiteboardMultiTouchMethods != null) {
whiteboardMultiTouchMethods!!.tapOnCurrentCard(secondFingerX.toInt(), secondFingerY.toInt())
return true
}
}
return false
}
// call this with an ACTION_MOVE event to check whether it is within the threshold for a tap of the second finger
// in this case perform a scroll action
private fun trySecondFingerScroll(event: MotionEvent): Boolean {
if (updateSecondFinger(event) && !secondFingerWithinTapTolerance) {
val dy = (secondFingerY0 - secondFingerY).toInt()
if (dy != 0 && whiteboardMultiTouchMethods != null) {
whiteboardMultiTouchMethods!!.scrollCurrentCardBy(dy)
secondFingerX0 = secondFingerX
secondFingerY0 = secondFingerY
}
return true
}
return false
}
fun onClick(view: View) {
when (view.id) {
R.id.pen_color_white -> {
penColor = Color.WHITE
}
R.id.pen_color_black -> {
penColor = Color.BLACK
}
R.id.pen_color_red -> {
val redPenColor = context.getColor(R.color.material_red_500)
penColor = redPenColor
}
R.id.pen_color_green -> {
val greenPenColor = context.getColor(R.color.material_green_500)
penColor = greenPenColor
}
R.id.pen_color_blue -> {
val bluePenColor = context.getColor(R.color.material_blue_500)
penColor = bluePenColor
}
R.id.pen_color_yellow -> {
val yellowPenColor = context.getColor(R.color.material_yellow_500)
penColor = yellowPenColor
}
R.id.pen_color_custom -> {
context.showColorPickerDialog(penColor) { penColor = it }
}
R.id.stroke_width -> {
handleWidthChangeDialog()
}
}
}
private fun handleWidthChangeDialog() {
val whiteBoardWidthDialog = WhiteBoardWidthDialog(ankiActivity, currentStrokeWidth)
whiteBoardWidthDialog.onStrokeWidthChanged { wbStrokeWidth: Int -> saveStrokeWidth(wbStrokeWidth) }
whiteBoardWidthDialog.showStrokeWidthDialog()
}
private fun saveStrokeWidth(wbStrokeWidth: Int) {
paint.strokeWidth = wbStrokeWidth.toFloat()
ankiActivity.sharedPrefs().edit {
putInt("whiteBoardStrokeWidth", wbStrokeWidth)
}
}
@get:VisibleForTesting
var penColor: Int
get() = paint.color
set(color) {
Timber.d("Setting pen color to %d", color)
paint.color = color
colorPalette.visibility = GONE
onPaintColorChangeListener?.onPaintColorChange(color)
}
/**
* Keep a list of all points and paths so that the last stroke can be undone
* pop() removes the last stroke from the list, and apply() redraws it to whiteboard.
*/
private inner class UndoList {
private val list: MutableList<WhiteboardAction> = ArrayList()
fun add(action: WhiteboardAction) {
list.add(action)
}
fun clear() {
list.clear()
}
fun size(): Int = list.size
fun pop() {
list.removeAt(list.size - 1)
}
fun apply() {
bitmap.eraseColor(0)
for (action in list) {
action.apply(canvas)
}
invalidate()
}
fun erase(
x: Int,
y: Int,
): Boolean {
var didErase = false
val clip = Region(0, 0, displayDimensions.x, displayDimensions.y)
val eraserPath = Path()
eraserPath.addRect((x - 10).toFloat(), (y - 10).toFloat(), (x + 10).toFloat(), (y + 10).toFloat(), Path.Direction.CW)
val eraserRegion = Region()
eraserRegion.setPath(eraserPath, clip)
// used inside the loop – created here to make things a little more efficient
val bounds = RectF()
var lineRegion = Region()
// we delete elements while iterating, so we need to use an iterator in order to avoid java.util.ConcurrentModificationException
val iterator = list.iterator()
while (iterator.hasNext()) {
val action = iterator.next()
val path = action.path
if (path != null) { // → line
val lineRegionSuccess = lineRegion.setPath(path, clip)
if (!lineRegionSuccess) {
// Small lines can be perfectly vertical/horizontal,
// thus giving us an empty region, which would make them undeletable.
// For this edge case, we create a Region ourselves.
path.computeBounds(bounds, true)
lineRegion =
Region(
Rect(
bounds.left.toInt(),
bounds.top.toInt(),
bounds.right.toInt() + 1,
bounds.bottom.toInt() + 1,
),
)
}
} else { // → point
val p = action.point
lineRegion = Region(p!!.x, p.y, p.x + 1, p.y + 1)
}
if (!lineRegion.quickReject(eraserRegion) && lineRegion.op(eraserRegion, Region.Op.INTERSECT)) {
iterator.remove()
didErase = true
}
}
return didErase
}
fun empty(): Boolean = list.isEmpty()
}
private interface WhiteboardAction {
fun apply(canvas: Canvas)
val path: Path?
val point: Point?
}
private class DrawPoint(
private val x: Float,
private val y: Float,
private val paint: Paint,
) : WhiteboardAction {
override fun apply(canvas: Canvas) {
canvas.drawPoint(x, y, paint)
}
override val path: Path?
get() = null
override val point: Point
get() = Point(x.toInt(), y.toInt())
}
private class DrawPath(
override val path: Path,
private val paint: Paint,
) : WhiteboardAction {
override fun apply(canvas: Canvas) {
canvas.drawPath(path, paint)
}
override val point: Point?
get() = null
}
@Throws(FileNotFoundException::class)
fun saveWhiteboard(time: Time?): Uri {
val bitmap = Bitmap.createBitmap(this.width, this.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
if (foregroundColor != Color.BLACK) {
canvas.drawColor(Color.BLACK)
} else {
canvas.drawColor(Color.WHITE)
}
draw(canvas)
val baseFileName = "Whiteboard" + getTimestamp(time!!)
return CompatHelper.compat.saveImage(context, bitmap, baseFileName, "jpg", Bitmap.CompressFormat.JPEG, 95)
}
fun interface OnPaintColorChangeListener {
fun onPaintColorChange(color: Int?)
}
companion object {
private const val TOUCH_TOLERANCE = 4f
private var whiteboardMultiTouchMethods: WhiteboardMultiTouchMethods? = null
fun createInstance(
context: AnkiActivity,
handleMultiTouch: Boolean,
whiteboardMultiTouchMethods: WhiteboardMultiTouchMethods?,
): Whiteboard {
val whiteboard = Whiteboard(context, handleMultiTouch, currentTheme is NightTheme)
Companion.whiteboardMultiTouchMethods = whiteboardMultiTouchMethods
val lp2 =
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
)
whiteboard.layoutParams = lp2
val fl = context.findViewById<FrameLayout>(R.id.whiteboard)
fl.addView(whiteboard)
whiteboard.isEnabled = true
return whiteboard
}
private val displayDimensions: Point
get() = getDisplayDimensions(appContext)
}
init {
val whitePenColorButton = activity.findViewById<Button>(R.id.pen_color_white)
val blackPenColorButton = activity.findViewById<Button>(R.id.pen_color_black)
if (!inverted) {
whitePenColorButton.visibility = GONE
blackPenColorButton.setOnClickListener { view: View -> onClick(view) }
foregroundColor = Color.BLACK
} else {
blackPenColorButton.visibility = GONE
whitePenColorButton.setOnClickListener { view: View -> onClick(view) }
foregroundColor = Color.WHITE
}
paint =
Paint().apply {
isAntiAlias = true
isDither = true
color = foregroundColor
style = Paint.Style.STROKE
strokeJoin = Paint.Join.ROUND
strokeCap = Paint.Cap.ROUND
strokeWidth = currentStrokeWidth.toFloat()
}
createBitmap()
path = Path()
bitmapPaint = Paint(Paint.DITHER_FLAG)
// selecting pen color to draw
colorPalette = activity.findViewById(R.id.whiteboard_editor)
activity.findViewById<View>(R.id.pen_color_red).setOnClickListener { view: View -> onClick(view) }
activity.findViewById<View>(R.id.pen_color_green).setOnClickListener { view: View -> onClick(view) }
activity.findViewById<View>(R.id.pen_color_blue).setOnClickListener { view: View -> onClick(view) }
activity.findViewById<View>(R.id.pen_color_yellow).setOnClickListener { view: View -> onClick(view) }
activity.findViewById<View>(R.id.pen_color_custom).apply {
setOnClickListener { view: View -> onClick(view) }
}
activity.findViewById<View>(R.id.stroke_width).apply {
setOnClickListener { view: View -> onClick(view) }
}
}
}