fix #41, add Redo

This commit is contained in:
tibbi 2018-01-24 14:28:45 +01:00
parent 805946dd88
commit 084e61dfd9
4 changed files with 38 additions and 3 deletions

View File

@ -64,6 +64,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
color_picker.setOnClickListener { pickColor() }
undo.setOnClickListener { my_canvas.undo() }
eraser.setOnClickListener { eraserClicked() }
redo.setOnClickListener { my_canvas.redo() }
checkIntents()
checkWhatsNewDialog()
@ -342,6 +343,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
val contrastColor = pickedColor.getContrastColor()
undo.applyColorFilter(contrastColor)
eraser.applyColorFilter(contrastColor)
redo.applyColorFilter(contrastColor)
my_canvas.updateBackgroundColor(pickedColor)
suggestedFileExtension = PNG
}
@ -358,6 +360,10 @@ class MainActivity : SimpleActivity(), CanvasListener {
undo.beVisibleIf(visible)
}
override fun toggleRedoVisibility(visible: Boolean) {
redo.beVisibleIf(visible)
}
private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
my_canvas.setStrokeWidth(progress.toFloat())

View File

@ -2,4 +2,6 @@ package com.simplemobiletools.draw.interfaces
interface CanvasListener {
fun toggleUndoVisibility(visible: Boolean)
fun toggleRedoVisibility(visible: Boolean)
}

View File

@ -27,9 +27,9 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
var mBackgroundBitmap: Bitmap? = null
var mListener: CanvasListener? = null
// allow undoing Clear
var mLastPaths = LinkedHashMap<MyPath, PaintOptions>()
var mLastBackgroundBitmap: Bitmap? = null
var mUndonePaths = LinkedHashMap<MyPath, PaintOptions>()
private var mPaint = Paint()
private var mPath = MyPath()
@ -67,16 +67,32 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
return
}
if (mPaths.isEmpty())
if (mPaths.isEmpty()) {
return
}
val lastPath = mPaths.values.lastOrNull()
val lastKey = mPaths.keys.lastOrNull()
mPaths.remove(lastKey)
if (lastPath != null && lastKey != null) {
mUndonePaths[lastKey] = lastPath
mListener?.toggleRedoVisibility(true)
}
pathsUpdated()
invalidate()
}
fun redo() {
val lastKey = mUndonePaths.keys.last()
addPath(lastKey, mUndonePaths.values.last())
mUndonePaths.remove(lastKey)
if (mUndonePaths.isEmpty()) {
mListener?.toggleRedoVisibility(false)
}
invalidate()
}
fun toggleEraser(isEraserOn: Boolean) {
mIsEraserOn = isEraserOn
mPaintOptions.isEraser = isEraserOn
@ -146,7 +162,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
}
fun addPath(path: MyPath, options: PaintOptions) {
mPaths.put(path, options)
mPaths[path] = options
pathsUpdated()
}
@ -248,6 +264,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
mStartX = x
mStartY = y
actionDown(x, y)
mUndonePaths.clear()
mListener?.toggleRedoVisibility(false)
}
MotionEvent.ACTION_MOVE -> actionMove(x, y)
MotionEvent.ACTION_UP -> actionUp()

View File

@ -11,6 +11,15 @@
android:layout_height="match_parent"
android:background="@android:color/white"/>
<ImageView
android:id="@+id/redo"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:layout_toLeftOf="@+id/eraser"
android:padding="@dimen/medium_margin"
android:src="@drawable/ic_redo"
android:visibility="gone"/>
<ImageView
android:id="@+id/eraser"
android:layout_width="@dimen/normal_icon_size"