Skip to content

Commit 084e61d

Browse files
committed
fix #41, add Redo
1 parent 805946d commit 084e61d

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
6464
color_picker.setOnClickListener { pickColor() }
6565
undo.setOnClickListener { my_canvas.undo() }
6666
eraser.setOnClickListener { eraserClicked() }
67+
redo.setOnClickListener { my_canvas.redo() }
6768

6869
checkIntents()
6970
checkWhatsNewDialog()
@@ -342,6 +343,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
342343
val contrastColor = pickedColor.getContrastColor()
343344
undo.applyColorFilter(contrastColor)
344345
eraser.applyColorFilter(contrastColor)
346+
redo.applyColorFilter(contrastColor)
345347
my_canvas.updateBackgroundColor(pickedColor)
346348
suggestedFileExtension = PNG
347349
}
@@ -358,6 +360,10 @@ class MainActivity : SimpleActivity(), CanvasListener {
358360
undo.beVisibleIf(visible)
359361
}
360362

363+
override fun toggleRedoVisibility(visible: Boolean) {
364+
redo.beVisibleIf(visible)
365+
}
366+
361367
private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
362368
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
363369
my_canvas.setStrokeWidth(progress.toFloat())

app/src/main/kotlin/com/simplemobiletools/draw/interfaces/CanvasListener.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ package com.simplemobiletools.draw.interfaces
22

33
interface CanvasListener {
44
fun toggleUndoVisibility(visible: Boolean)
5+
6+
fun toggleRedoVisibility(visible: Boolean)
57
}

app/src/main/kotlin/com/simplemobiletools/draw/views/MyCanvas.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
2727
var mBackgroundBitmap: Bitmap? = null
2828
var mListener: CanvasListener? = null
2929

30-
// allow undoing Clear
3130
var mLastPaths = LinkedHashMap<MyPath, PaintOptions>()
3231
var mLastBackgroundBitmap: Bitmap? = null
32+
var mUndonePaths = LinkedHashMap<MyPath, PaintOptions>()
3333

3434
private var mPaint = Paint()
3535
private var mPath = MyPath()
@@ -67,16 +67,32 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
6767
return
6868
}
6969

70-
if (mPaths.isEmpty())
70+
if (mPaths.isEmpty()) {
7171
return
72+
}
7273

74+
val lastPath = mPaths.values.lastOrNull()
7375
val lastKey = mPaths.keys.lastOrNull()
7476

7577
mPaths.remove(lastKey)
78+
if (lastPath != null && lastKey != null) {
79+
mUndonePaths[lastKey] = lastPath
80+
mListener?.toggleRedoVisibility(true)
81+
}
7682
pathsUpdated()
7783
invalidate()
7884
}
7985

86+
fun redo() {
87+
val lastKey = mUndonePaths.keys.last()
88+
addPath(lastKey, mUndonePaths.values.last())
89+
mUndonePaths.remove(lastKey)
90+
if (mUndonePaths.isEmpty()) {
91+
mListener?.toggleRedoVisibility(false)
92+
}
93+
invalidate()
94+
}
95+
8096
fun toggleEraser(isEraserOn: Boolean) {
8197
mIsEraserOn = isEraserOn
8298
mPaintOptions.isEraser = isEraserOn
@@ -146,7 +162,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
146162
}
147163

148164
fun addPath(path: MyPath, options: PaintOptions) {
149-
mPaths.put(path, options)
165+
mPaths[path] = options
150166
pathsUpdated()
151167
}
152168

@@ -248,6 +264,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
248264
mStartX = x
249265
mStartY = y
250266
actionDown(x, y)
267+
mUndonePaths.clear()
268+
mListener?.toggleRedoVisibility(false)
251269
}
252270
MotionEvent.ACTION_MOVE -> actionMove(x, y)
253271
MotionEvent.ACTION_UP -> actionUp()

app/src/main/res/layout/activity_main.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
android:layout_height="match_parent"
1212
android:background="@android:color/white"/>
1313

14+
<ImageView
15+
android:id="@+id/redo"
16+
android:layout_width="@dimen/normal_icon_size"
17+
android:layout_height="@dimen/normal_icon_size"
18+
android:layout_toLeftOf="@+id/eraser"
19+
android:padding="@dimen/medium_margin"
20+
android:src="@drawable/ic_redo"
21+
android:visibility="gone"/>
22+
1423
<ImageView
1524
android:id="@+id/eraser"
1625
android:layout_width="@dimen/normal_icon_size"

0 commit comments

Comments
 (0)