Skip to content

Commit

Permalink
Add digital eraser support in the Whiteboard (ankidroid#14315)
Browse files Browse the repository at this point in the history
* Add digital eraser support in the Whiteboard.
* modified to make it easier to understand
* change priority for erase

---------

Co-authored-by: root <[email protected]>
  • Loading branch information
naong and saifsas authored Aug 28, 2023
1 parent e817d89 commit f2e4d15
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/anki/Whiteboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ class Whiteboard(activity: AnkiActivity, handleMultiTouch: Boolean, inverted: Bo
private fun handleDrawEvent(event: MotionEvent): Boolean {
val x = event.x
val y = event.y
if (event.getToolType(event.actionIndex) != MotionEvent.TOOL_TYPE_STYLUS && toggleStylus == true) {
if (event.getToolType(event.actionIndex) == MotionEvent.TOOL_TYPE_ERASER) {
stylusErase(event)
return true
}
if (event.getToolType(event.actionIndex) != MotionEvent.TOOL_TYPE_STYLUS && toggleStylus) {
return false
}
return when (event.actionMasked) {
Expand Down Expand Up @@ -145,14 +149,8 @@ class Whiteboard(activity: AnkiActivity, handleMultiTouch: Boolean, inverted: Bo
false
}
211, 213 -> {
if (event.buttonState == MotionEvent.BUTTON_STYLUS_PRIMARY && !undoEmpty()) {
val didErase = mUndo.erase(event.x.toInt(), event.y.toInt())
if (didErase) {
mUndo.apply()
if (undoEmpty()) {
mAnkiActivity.invalidateOptionsMenu()
}
}
if (event.buttonState == MotionEvent.BUTTON_STYLUS_PRIMARY) {
stylusErase(event)
}
true
}
Expand All @@ -177,6 +175,21 @@ class Whiteboard(activity: AnkiActivity, handleMultiTouch: Boolean, inverted: Bo
}
}

/**
* Erase with stylus pen.(By using the eraser button on the stylus pen or by using the digital eraser)
*/
private fun stylusErase(event: MotionEvent) {
if (!undoEmpty()) {
val didErase = mUndo.erase(event.x.toInt(), event.y.toInt())
if (didErase) {
mUndo.apply()
if (undoEmpty()) {
mAnkiActivity.invalidateOptionsMenu()
}
}
}
}

/**
* Clear the whiteboard.
*/
Expand Down

0 comments on commit f2e4d15

Please sign in to comment.