Skip to content

Commit 3f0b4f2

Browse files
update touch event functions
1 parent 23a35a6 commit 3f0b4f2

File tree

7 files changed

+180
-165
lines changed

7 files changed

+180
-165
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dependencies {
5151
implementation project(':colorpicker')
5252

5353
implementation 'com.github.SmartToolFactory:Compose-Colorful-Sliders:1.1.0'
54-
implementation 'com.github.SmartToolFactory:Compose-Extended-Gestures:2.0.0'
54+
implementation 'com.github.SmartToolFactory:Compose-Extended-Gestures:2.1.0'
5555
implementation 'com.github.SmartToolFactory:Compose-Extended-Colors:1.0.0-alpha07'
5656

5757
// Jetpack Compose

colorpicker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ dependencies {
4646

4747
implementation 'androidx.core:core-ktx:1.9.0'
4848

49-
implementation 'com.github.SmartToolFactory:Compose-Extended-Gestures:2.0.0'
49+
implementation 'com.github.SmartToolFactory:Compose-Extended-Gestures:2.1.0'
5050
implementation 'com.github.SmartToolFactory:Compose-Extended-Colors:1.0.0-alpha07'
5151
implementation 'com.github.SmartToolFactory:Compose-Screenshot:1.0.3'
5252
implementation 'com.github.SmartToolFactory:Compose-Color-Detector:1.0.0'

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/selector/SelectorCircleHueSaturation.kt

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import androidx.compose.ui.Modifier
88
import androidx.compose.ui.geometry.Offset
99
import androidx.compose.ui.graphics.Brush
1010
import androidx.compose.ui.graphics.Color
11+
import androidx.compose.ui.input.pointer.pointerInput
1112
import androidx.compose.ui.platform.LocalDensity
1213
import androidx.compose.ui.unit.Dp
1314
import com.smarttoolfactory.colorpicker.ui.gradientColorScaleHSVReversed
1415
import com.smarttoolfactory.colorpicker.util.calculateAngleFomLocalCoordinates
1516
import com.smarttoolfactory.colorpicker.util.calculateDistanceFromCenter
1617
import com.smarttoolfactory.colorpicker.util.calculatePositionFromAngleAndDistance
17-
import com.smarttoolfactory.gesture.pointerMotionEvents
18+
import com.smarttoolfactory.gesture.detectMotionEvents
1819

1920
/**
2021
* Circle Hue and Saturation picker for
@@ -63,48 +64,50 @@ fun SelectorCircleHueSaturationHSV(
6364

6465

6566
val canvasModifier = Modifier
66-
.pointerMotionEvents(
67-
onDown = {
68-
val position = it.position
69-
70-
// Distance from center to touch point
71-
val distance =
72-
calculateDistanceFromCenter(center, position).coerceAtMost(radius)
73-
74-
// if distance is between inner and outer radius then we touched valid area
75-
isTouched = (distance < radius)
76-
77-
if (isTouched) {
78-
val hueChange = calculateAngleFomLocalCoordinates(center, position)
79-
val saturationChange = (distance / radius).coerceIn(0f, 1f)
80-
onChange(hueChange, saturationChange)
81-
it.consume()
82-
}
83-
84-
},
85-
onMove = {
86-
if (isTouched) {
87-
67+
.pointerInput(Unit) {
68+
detectMotionEvents(
69+
onDown = {
8870
val position = it.position
89-
val hueChange = calculateAngleFomLocalCoordinates(center, position)
71+
72+
// Distance from center to touch point
9073
val distance =
9174
calculateDistanceFromCenter(center, position).coerceAtMost(radius)
9275

93-
val saturationChange = (distance / radius).coerceIn(0f, 1f)
94-
onChange(hueChange, saturationChange)
95-
it.consume()
96-
}
97-
98-
},
99-
onUp = {
100-
if (isTouched) {
101-
it.consume()
102-
}
103-
isTouched = false
104-
105-
},
106-
delayAfterDownInMillis = 20
107-
)
76+
// if distance is between inner and outer radius then we touched valid area
77+
isTouched = (distance < radius)
78+
79+
if (isTouched) {
80+
val hueChange = calculateAngleFomLocalCoordinates(center, position)
81+
val saturationChange = (distance / radius).coerceIn(0f, 1f)
82+
onChange(hueChange, saturationChange)
83+
it.consume()
84+
}
85+
86+
},
87+
onMove = {
88+
if (isTouched) {
89+
90+
val position = it.position
91+
val hueChange = calculateAngleFomLocalCoordinates(center, position)
92+
val distance =
93+
calculateDistanceFromCenter(center, position).coerceAtMost(radius)
94+
95+
val saturationChange = (distance / radius).coerceIn(0f, 1f)
96+
onChange(hueChange, saturationChange)
97+
it.consume()
98+
}
99+
100+
},
101+
onUp = {
102+
if (isTouched) {
103+
it.consume()
104+
}
105+
isTouched = false
106+
107+
},
108+
delayAfterDownInMillis = 20
109+
)
110+
}
108111

109112
SelectorCircleImpl(
110113
modifier = canvasModifier,

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/selector/SelectorDiamondSaturationLightness.kt

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import androidx.compose.ui.graphics.BlendMode
1515
import androidx.compose.ui.graphics.Brush
1616
import androidx.compose.ui.graphics.Color
1717
import androidx.compose.ui.graphics.Path
18+
import androidx.compose.ui.input.pointer.pointerInput
1819
import androidx.compose.ui.platform.LocalDensity
1920
import androidx.compose.ui.unit.Dp
2021
import androidx.compose.ui.unit.LayoutDirection
2122
import com.smarttoolfactory.colorpicker.model.ColorModel
2223
import com.smarttoolfactory.colorpicker.ui.GradientAngle
2324
import com.smarttoolfactory.colorpicker.ui.GradientOffset
2425
import com.smarttoolfactory.colorpicker.util.drawIntoLayer
25-
import com.smarttoolfactory.gesture.pointerMotionEvents
26+
import com.smarttoolfactory.gesture.detectMotionEvents
2627

2728

2829
/**
@@ -226,51 +227,53 @@ private fun SelectorDiamond(
226227
var isTouched by remember { mutableStateOf(false) }
227228

228229
val canvasModifier = Modifier
229-
.pointerMotionEvents(
230-
onDown = {
231-
val position = it.position
232-
val posX = position.x
233-
val posY = position.y
234-
235-
// Horizontal range for keeping x position in diamond bounds
236-
val range = getRangeForPositionInDiamond(length, posY)
237-
val start = range.start - selectionRadius
238-
val end = range.endInclusive + selectionRadius
239-
240-
isTouched = posX in start..end
241-
if (isTouched) {
242-
243-
val posXInPercent = (posX / length).coerceIn(0f, 1f)
244-
val posYInPercent = (posY / length).coerceIn(0f, 1f)
245-
246-
// Send x position as saturation and reverse of y position as lightness
247-
// lightness increases while going up but android drawing system is opposite
248-
onChange(posXInPercent, 1 - posYInPercent)
249-
}
250-
it.consume()
251-
252-
},
253-
onMove = {
254-
if (isTouched) {
255-
230+
.pointerInput(Unit) {
231+
detectMotionEvents(
232+
onDown = {
256233
val position = it.position
257-
val posX = position.x.coerceIn(0f, length)
258-
val posY = position.y.coerceIn(0f, length)
259-
260-
val posXInPercent = (posX / length).coerceIn(0f, 1f)
261-
val posYInPercent = (posY / length).coerceIn(0f, 1f)
262-
263-
// Send x position as saturation and reverse of y position as lightness
264-
// lightness increases while going up but android drawing system is opposite
265-
onChange(posXInPercent, 1 - posYInPercent)
234+
val posX = position.x
235+
val posY = position.y
236+
237+
// Horizontal range for keeping x position in diamond bounds
238+
val range = getRangeForPositionInDiamond(length, posY)
239+
val start = range.start - selectionRadius
240+
val end = range.endInclusive + selectionRadius
241+
242+
isTouched = posX in start..end
243+
if (isTouched) {
244+
245+
val posXInPercent = (posX / length).coerceIn(0f, 1f)
246+
val posYInPercent = (posY / length).coerceIn(0f, 1f)
247+
248+
// Send x position as saturation and reverse of y position as lightness
249+
// lightness increases while going up but android drawing system is opposite
250+
onChange(posXInPercent, 1 - posYInPercent)
251+
}
252+
it.consume()
253+
254+
},
255+
onMove = {
256+
if (isTouched) {
257+
258+
val position = it.position
259+
val posX = position.x.coerceIn(0f, length)
260+
val posY = position.y.coerceIn(0f, length)
261+
262+
val posXInPercent = (posX / length).coerceIn(0f, 1f)
263+
val posYInPercent = (posY / length).coerceIn(0f, 1f)
264+
265+
// Send x position as saturation and reverse of y position as lightness
266+
// lightness increases while going up but android drawing system is opposite
267+
onChange(posXInPercent, 1 - posYInPercent)
268+
}
269+
it.consume()
270+
},
271+
onUp = {
272+
isTouched = false
273+
it.consume()
266274
}
267-
it.consume()
268-
},
269-
onUp = {
270-
isTouched = false
271-
it.consume()
272-
}
273-
)
275+
)
276+
}
274277

275278
SelectorDiamondImpl(
276279
modifier = canvasModifier,

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/selector/SelectorRectHueLightnessValue.kt

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.runtime.*
77
import androidx.compose.ui.Modifier
88
import androidx.compose.ui.geometry.Offset
99
import androidx.compose.ui.graphics.Brush
10+
import androidx.compose.ui.input.pointer.pointerInput
1011
import androidx.compose.ui.platform.LocalDensity
1112
import androidx.compose.ui.unit.Dp
1213
import com.smarttoolfactory.colorpicker.ui.brush.transparentToBlackVerticalGradient
@@ -15,7 +16,7 @@ import com.smarttoolfactory.colorpicker.ui.brush.transparentToWhiteVerticalGradi
1516
import com.smarttoolfactory.colorpicker.ui.brush.whiteToTransparentToBlackVerticalGradient
1617
import com.smarttoolfactory.colorpicker.ui.gradientColorScaleHSL
1718
import com.smarttoolfactory.colorpicker.ui.gradientColorScaleHSV
18-
import com.smarttoolfactory.gesture.pointerMotionEvents
19+
import com.smarttoolfactory.gesture.detectMotionEvents
1920

2021
/**
2122
* Rectangle Hue and Value selector for
@@ -244,32 +245,34 @@ private fun SelectorRect(
244245
else width.coerceAtMost(height) * .04f
245246

246247
val canvasModifier = Modifier
247-
.pointerMotionEvents(
248-
onDown = {
249-
val position = it.position
250-
val hueChange = (position.x / width).coerceIn(0f, 1f) * 360f
251-
val propertyChange = if (selectorType == SelectorType.HSWithHSV) {
252-
(position.y / height).coerceIn(0f, 1f)
253-
} else {
254-
(1 - (position.y / height)).coerceIn(0f, 1f)
255-
}
256-
onChange(hueChange, propertyChange)
257-
it.consume()
248+
.pointerInput(Unit){
249+
detectMotionEvents(
250+
onDown = {
251+
val position = it.position
252+
val hueChange = (position.x / width).coerceIn(0f, 1f) * 360f
253+
val propertyChange = if (selectorType == SelectorType.HSWithHSV) {
254+
(position.y / height).coerceIn(0f, 1f)
255+
} else {
256+
(1 - (position.y / height)).coerceIn(0f, 1f)
257+
}
258+
onChange(hueChange, propertyChange)
259+
it.consume()
258260

259-
},
260-
onMove = {
261-
val position = it.position
262-
val hueChange = (position.x / width).coerceIn(0f, 1f) * 360f
263-
val propertyChange = if (selectorType == SelectorType.HSWithHSV) {
264-
(position.y / height).coerceIn(0f, 1f)
265-
} else {
266-
(1 - (position.y / height)).coerceIn(0f, 1f)
267-
}
268-
onChange(hueChange, propertyChange)
269-
it.consume()
270-
},
271-
delayAfterDownInMillis = 20
272-
)
261+
},
262+
onMove = {
263+
val position = it.position
264+
val hueChange = (position.x / width).coerceIn(0f, 1f) * 360f
265+
val propertyChange = if (selectorType == SelectorType.HSWithHSV) {
266+
(position.y / height).coerceIn(0f, 1f)
267+
} else {
268+
(1 - (position.y / height)).coerceIn(0f, 1f)
269+
}
270+
onChange(hueChange, propertyChange)
271+
it.consume()
272+
},
273+
delayAfterDownInMillis = 20
274+
)
275+
}
273276

274277
SelectorRectImpl(
275278
modifier = canvasModifier,

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/selector/SelectorRectSaturationLightnessValue.kt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.ui.Modifier
88
import androidx.compose.ui.geometry.Offset
99
import androidx.compose.ui.graphics.BlendMode
1010
import androidx.compose.ui.graphics.Brush
11+
import androidx.compose.ui.input.pointer.pointerInput
1112
import androidx.compose.ui.platform.LocalDensity
1213
import androidx.compose.ui.unit.Dp
1314
import com.smarttoolfactory.colorpicker.model.ColorModel
@@ -16,7 +17,7 @@ import com.smarttoolfactory.colorpicker.ui.brush.saturationHSLGradient
1617
import com.smarttoolfactory.colorpicker.ui.brush.saturationHSVGradient
1718
import com.smarttoolfactory.colorpicker.ui.brush.valueGradient
1819
import com.smarttoolfactory.colorpicker.util.drawBlendingRectGradient
19-
import com.smarttoolfactory.gesture.pointerMotionEvents
20+
import com.smarttoolfactory.gesture.detectMotionEvents
2021

2122
/**
2223
* Rectangle Saturation and Lightness selector for
@@ -131,24 +132,26 @@ private fun SelectorRect(
131132

132133
val canvasModifier = Modifier
133134
.fillMaxSize()
134-
.pointerMotionEvents(
135-
onDown = {
136-
val position = it.position
137-
val saturationChange = (position.x / width).coerceIn(0f, 1f)
138-
val valueChange = (1 - (position.y / height)).coerceIn(0f, 1f)
139-
onChange(saturationChange, valueChange)
140-
it.consume()
141-
142-
},
143-
onMove = {
144-
val position = it.position
145-
val saturationChange = (position.x / width).coerceIn(0f, 1f)
146-
val valueChange = (1 - (position.y / height)).coerceIn(0f, 1f)
147-
onChange(saturationChange, valueChange)
148-
it.consume()
149-
},
150-
delayAfterDownInMillis = 20
151-
)
135+
.pointerInput(Unit) {
136+
detectMotionEvents(
137+
onDown = {
138+
val position = it.position
139+
val saturationChange = (position.x / width).coerceIn(0f, 1f)
140+
val valueChange = (1 - (position.y / height)).coerceIn(0f, 1f)
141+
onChange(saturationChange, valueChange)
142+
it.consume()
143+
144+
},
145+
onMove = {
146+
val position = it.position
147+
val saturationChange = (position.x / width).coerceIn(0f, 1f)
148+
val valueChange = (1 - (position.y / height)).coerceIn(0f, 1f)
149+
onChange(saturationChange, valueChange)
150+
it.consume()
151+
},
152+
delayAfterDownInMillis = 20
153+
)
154+
}
152155

153156
SelectorRectImpl(
154157
modifier = canvasModifier,

0 commit comments

Comments
 (0)