Skip to content

Commit 872cc1f

Browse files
author
therealbluepandabear
committed
Re-implemented the ability to long tap on your primary/secondary color to open the color picker. May contain bugs.
1 parent 3cd4dab commit 872cc1f

2 files changed

Lines changed: 67 additions & 19 deletions

File tree

app/src/main/java/com/therealbluepandabear/pixapencil/activities/canvas/oncreate/CanvasActivity+setupEventListeners.kt

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,31 @@ import com.therealbluepandabear.pixapencil.R
77
import com.therealbluepandabear.pixapencil.activities.canvas.*
88
import com.therealbluepandabear.pixapencil.fragments.colorpicker.ColorPickerFragment
99

10-
fun CanvasActivity.setupEventListeners() {
11-
binding.activityCanvasColorSwitcherView.setOnColorPickerTapped {
12-
// this 'if' is very important, as without it multiple fragments would be added to the backstack
13-
if (supportFragmentManager.backStackEntryCount == 0) {
14-
supportFragmentManager.commit {
15-
replace(
16-
R.id.activityCanvas_primaryFragmentHost,
17-
ColorPickerFragment.newInstance(
18-
paramOldColor = getSelectedColor(),
19-
paramColorPalette = null
20-
)
10+
private fun CanvasActivity.navigateToColorPicker() {
11+
if (supportFragmentManager.backStackEntryCount == 0) {
12+
supportFragmentManager.commit {
13+
replace(
14+
R.id.activityCanvas_primaryFragmentHost,
15+
ColorPickerFragment.newInstance(
16+
paramOldColor = getSelectedColor(),
17+
paramColorPalette = null
2118
)
22-
addToBackStack(null)
23-
}
19+
)
20+
addToBackStack(null)
2421
}
2522
}
23+
}
24+
25+
fun CanvasActivity.setupEventListeners() {
26+
binding.activityCanvasColorSwitcherView.setOnColorPickerTapped {
27+
navigateToColorPicker()
28+
}
29+
30+
binding.activityCanvasColorSwitcherView.setOnPrimaryColorLongTapped {
31+
navigateToColorPicker()
32+
}
33+
34+
binding.activityCanvasColorSwitcherView.setOnSecondaryColorLongTapped {
35+
navigateToColorPicker()
36+
}
2637
}

app/src/main/java/com/therealbluepandabear/pixapencil/customviews/colorswitcherview/ColorSwitcherView.kt

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.graphics.Shader.TileMode
77
import android.util.AttributeSet
88
import android.view.MotionEvent
99
import android.view.View
10+
import android.view.ViewConfiguration
1011
import androidx.core.content.ContextCompat
1112
import com.therealbluepandabear.pixapencil.R
1213

@@ -95,6 +96,10 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
9596

9697
private var onColorPickerTapped: () -> Unit = { }
9798

99+
private var onPrimaryColorLongTapped: () -> Unit = { }
100+
101+
private var onSecondaryColorLongTapped: () -> Unit = { }
102+
98103
private fun toDp(px: Int): Float {
99104
return px * resources.displayMetrics.density
100105
}
@@ -117,6 +122,14 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
117122
this.onColorPickerTapped = onColorPickerTapped
118123
}
119124

125+
fun setOnPrimaryColorLongTapped(onPrimaryColorLongTapped: () -> Unit) {
126+
this.onPrimaryColorLongTapped = onPrimaryColorLongTapped
127+
}
128+
129+
fun setOnSecondaryColorLongTapped(onSecondaryColorLongTapped: () -> Unit) {
130+
this.onSecondaryColorLongTapped = onSecondaryColorLongTapped
131+
}
132+
120133
fun setIsPrimarySelected(isPrimarySelected: Boolean) {
121134
this.isPrimarySelected = isPrimarySelected
122135
invalidate()
@@ -305,6 +318,14 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
305318
}
306319
}
307320

321+
private val longPressedRunnable = Runnable {
322+
if (isPrimarySelected) {
323+
onPrimaryColorLongTapped.invoke()
324+
} else {
325+
onSecondaryColorLongTapped.invoke()
326+
}
327+
}
328+
308329
@SuppressLint("ClickableViewAccessibility") /** I will un-suppress in future commits **/
309330
override fun onTouchEvent(event: MotionEvent): Boolean {
310331
val xOrY = if (!orientationLandscape) {
@@ -319,18 +340,34 @@ class ColorSwitcherView(context: Context, attributeSet: AttributeSet) : View(con
319340
event.x
320341
}
321342

322-
if ((xOrY in toDp(50)..toDp(100) - insetStroke) && isPrimarySelected
343+
if ((xOrY in toDp(50)..toDp(100) - insetStroke)
323344
&& inverse !in toDp(50)..toDp(70)) {
324-
isPrimarySelected = false
325-
invalidate()
326-
} else if ((xOrY !in toDp(50)..toDp(100) - insetStroke) && !isPrimarySelected
345+
// secondary color has been selected
346+
handler.postDelayed(longPressedRunnable, ViewConfiguration.getLongPressTimeout().toLong())
347+
348+
if (isPrimarySelected) {
349+
isPrimarySelected = false
350+
invalidate()
351+
}
352+
} else if ((xOrY !in toDp(50)..toDp(100) - insetStroke)
327353
&& inverse !in toDp(50)..toDp(70)) {
328-
isPrimarySelected = true
329-
invalidate()
354+
// primary color has been selected
355+
handler.postDelayed(longPressedRunnable, ViewConfiguration.getLongPressTimeout().toLong())
356+
357+
if (!isPrimarySelected) {
358+
isPrimarySelected = true
359+
invalidate()
360+
}
330361
} else if ((inverse in toDp(55)..toDp(70))) {
362+
// color picker has been selected
363+
331364
onColorPickerTapped.invoke()
332365
}
333366

367+
if (event.action == MotionEvent.ACTION_UP) {
368+
handler.removeCallbacks(longPressedRunnable)
369+
}
370+
334371
return true
335372
}
336373
}

0 commit comments

Comments
 (0)