fix #71, allow Undoing Clear

This commit is contained in:
tibbi 2017-11-20 23:12:29 +01:00
parent 0208a424e2
commit cd8fd8cb8f
3 changed files with 27 additions and 10 deletions

View File

@ -25,14 +25,14 @@ import com.simplemobiletools.draw.extensions.config
import com.simplemobiletools.draw.helpers.JPG import com.simplemobiletools.draw.helpers.JPG
import com.simplemobiletools.draw.helpers.PNG import com.simplemobiletools.draw.helpers.PNG
import com.simplemobiletools.draw.helpers.SVG import com.simplemobiletools.draw.helpers.SVG
import com.simplemobiletools.draw.interfaces.CanvasListener
import com.simplemobiletools.draw.models.Svg import com.simplemobiletools.draw.models.Svg
import com.simplemobiletools.draw.views.MyCanvas
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { class MainActivity : SimpleActivity(), CanvasListener {
private val FOLDER_NAME = "images" private val FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png" private val FILE_NAME = "simple-draw.png"
@ -329,8 +329,8 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
updateEraserState() updateEraserState()
} }
override fun pathsChanged(cnt: Int) { override fun toggleUndoVisibility(visible: Boolean) {
undo.beVisibleIf(cnt > 0) undo.beVisibleIf(visible)
} }
private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener { private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.draw.interfaces
interface CanvasListener {
fun toggleUndoVisibility(visible: Boolean)
}

View File

@ -13,6 +13,7 @@ import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.draw.R import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.interfaces.CanvasListener
import com.simplemobiletools.draw.models.MyParcelable import com.simplemobiletools.draw.models.MyParcelable
import com.simplemobiletools.draw.models.MyPath import com.simplemobiletools.draw.models.MyPath
import com.simplemobiletools.draw.models.PaintOptions import com.simplemobiletools.draw.models.PaintOptions
@ -24,7 +25,11 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
var mPaths = LinkedHashMap<MyPath, PaintOptions>() var mPaths = LinkedHashMap<MyPath, PaintOptions>()
var mBackgroundBitmap: Bitmap? = null var mBackgroundBitmap: Bitmap? = null
var mListener: PathsChangedListener? = null var mListener: CanvasListener? = null
// allow undoing Clear
var mLastPaths = LinkedHashMap<MyPath, PaintOptions>()
var mLastBackgroundBitmap: Bitmap? = null
private var mPaint = Paint() private var mPaint = Paint()
private var mPath = MyPath() private var mPath = MyPath()
@ -53,6 +58,15 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
fun undo() { fun undo() {
if (mPaths.isEmpty() && mLastPaths.isNotEmpty()) {
mPaths = mLastPaths.clone() as LinkedHashMap<MyPath, PaintOptions>
mBackgroundBitmap = mLastBackgroundBitmap
mLastPaths.clear()
pathsUpdated()
invalidate()
return
}
if (mPaths.isEmpty()) if (mPaths.isEmpty())
return return
@ -182,6 +196,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
fun clearCanvas() { fun clearCanvas() {
mLastPaths = mPaths.clone() as LinkedHashMap<MyPath, PaintOptions>
mLastBackgroundBitmap = mBackgroundBitmap
mBackgroundBitmap = null mBackgroundBitmap = null
mPath.reset() mPath.reset()
mPaths.clear() mPaths.clear()
@ -219,7 +235,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
private fun pathsUpdated() { private fun pathsUpdated() {
mListener?.pathsChanged(mPaths.size) mListener?.toggleUndoVisibility(mPaths.isNotEmpty() || mLastPaths.isNotEmpty())
} }
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean {
@ -240,10 +256,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
return true return true
} }
interface PathsChangedListener {
fun pathsChanged(cnt: Int)
}
public override fun onSaveInstanceState(): Parcelable { public override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState() val superState = super.onSaveInstanceState()
val savedState = MyParcelable(superState) val savedState = MyParcelable(superState)