Restrict swatch to bounds of SL picker

This commit is contained in:
Moez Bhatti
2018-03-24 01:57:33 -04:00
parent dcff7e8c6c
commit 65d57f2afd
3 changed files with 29 additions and 26 deletions
@@ -34,4 +34,8 @@ fun Int.forEach(action: (Int) -> Unit) {
for (index in 0 until this) { for (index in 0 until this) {
action(index) action(index)
} }
}
fun Float.within(min: Float, max: Float): Float {
return Math.min(max, Math.max(min, this))
} }
@@ -25,7 +25,9 @@ import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkThemedActivity import common.base.QkThemedActivity
import common.util.extensions.dpToPx
import common.util.extensions.setBackgroundTint import common.util.extensions.setBackgroundTint
import common.util.extensions.within
import injection.appComponent import injection.appComponent
import kotlinx.android.synthetic.main.theme_picker_activity.* import kotlinx.android.synthetic.main.theme_picker_activity.*
import kotlinx.android.synthetic.main.theme_picker_hsl.* import kotlinx.android.synthetic.main.theme_picker_hsl.*
@@ -40,6 +42,8 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
@Inject lateinit var themeAdapter: ThemeAdapter @Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter @Inject lateinit var themePagerAdapter: ThemePagerAdapter
private val swatchPadding by lazy { 18.dpToPx(this) }
init { init {
appComponent.inject(this) appComponent.inject(this)
} }
@@ -70,22 +74,17 @@ class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePicke
hue.setOnTouchListener { _, event -> hue.setOnTouchListener { _, event ->
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
dX = swatch.x - event.rawX dX = event.x - event.rawX
dY = swatch.y - event.rawY dY = event.y - event.rawY
swatch.animate()
.x(event.rawX + dX)
.y(event.rawY + dY)
.setDuration(0)
.start()
} }
MotionEvent.ACTION_MOVE -> MotionEvent.ACTION_MOVE -> {
swatch.animate() val min = hue.x - swatch.width / 2
.x(event.rawX + dX) val max = min + hue.width
.y(event.rawY + dY)
.setDuration(0) swatch.x = (event.rawX + dX + swatchPadding).within(min, max)
.start() swatch.y = (event.rawY + dY + swatchPadding).within(min, max)
}
else -> return@setOnTouchListener false else -> return@setOnTouchListener false
} }
@@ -31,23 +31,23 @@
<FrameLayout <FrameLayout
android:id="@+id/swatch" android:id="@+id/swatch"
android:layout_width="wrap_content" android:layout_width="12dp"
android:layout_height="wrap_content" android:layout_height="12dp"
android:padding="18dp"> android:background="@drawable/circle"
android:padding="1dp"
<View android:layout_margin="18dp"
android:layout_width="12dp" app:layout_constraintBottom_toBottomOf="@id/hue"
android:layout_height="12dp" app:layout_constraintEnd_toEndOf="@id/hue"
android:layout_gravity="center" app:layout_constraintStart_toStartOf="@id/hue"
android:background="@drawable/circle" /> app:layout_constraintTop_toTopOf="@id/hue">
<View <View
android:id="@+id/swatchPreview" android:id="@+id/swatchPreview"
android:layout_width="10dp" android:layout_width="match_parent"
android:layout_height="10dp" android:layout_height="match_parent"
android:layout_gravity="center" android:layout_gravity="center"
android:background="@drawable/circle" android:background="@drawable/circle"
tools:backgroundTint="@color/tools_theme" /> android:backgroundTint="@color/tools_theme" />
</FrameLayout> </FrameLayout>