Skip to content

Commit 7b16e70

Browse files
committed
feat: refactor drawer methods for improved calculations and uses
1 parent 93ccf01 commit 7b16e70

File tree

9 files changed

+123
-75
lines changed

9 files changed

+123
-75
lines changed

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/control/Control.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ abstract class Control(
379379
}
380380

381381
fun arcStrokeWidth(width: Float): DrawerBuilder {
382-
arcStrokeWidth = ArcDrawer.getStrokeWidth(width)
382+
arcStrokeWidth = ArcDrawer.clampStrokeWidth(width)
383383
return this
384384
}
385385

@@ -388,7 +388,7 @@ abstract class Control(
388388
fun arcStrokeWidth(width: Int) = arcStrokeWidth(width.toFloat())
389389

390390
fun arcSweepAngle(angle: Float): DrawerBuilder {
391-
arcSweepAngle = ArcDrawer.getSweepAngle(angle)
391+
arcSweepAngle = ArcDrawer.clampSweepAngle(angle)
392392
return this
393393
}
394394

@@ -397,7 +397,7 @@ abstract class Control(
397397
fun arcSweepAngle(angle: Int) = arcSweepAngle(angle.toFloat())
398398

399399
fun circleRadiusRatio(ratio: Float): DrawerBuilder {
400-
circleRadiusRatio = DrawerRadius.Ratio.getValidRatio(ratio)
400+
circleRadiusRatio = DrawerRadius.Ratio.clampRatio(ratio)
401401
circleRadius = null
402402
return this
403403
}

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/drawer/core/DrawerRadius.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ sealed class DrawerRadius {
5959
const val MAX_RADIUS_RATIO = 0.80f
6060

6161
/**
62-
* Checks if the [ratio] value meets the valid range.
62+
* Clamps the [ratio] value in the valid range.
6363
*
6464
* @param ratio The ratio value.
6565
*
6666
* @return A valid radius ratio in the range [MIN_RADIUS_RATIO] to [MAX_RADIUS_RATIO]
6767
*/
6868
@JvmStatic
6969
@FloatRange(from = MIN_RADIUS_RATIO.toDouble(), to = MAX_RADIUS_RATIO.toDouble())
70-
fun getValidRatio(ratio: Float): Float {
70+
fun clampRatio(ratio: Float): Float {
7171
return ratio.coerceIn(MIN_RADIUS_RATIO, MAX_RADIUS_RATIO)
7272
}
7373
}
7474

75-
val ratio = getValidRatio(ratio)
75+
val ratio = clampRatio(ratio)
7676

7777
override fun getValue(control: Control): Double = control.radius * ratio
7878
}

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/drawer/core/SimpleDrawer.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,20 @@ open class SimpleDrawer(
8989
}
9090

9191
/**
92-
* Gets the current direction of the control.
92+
* Calculates the current direction of the control.
9393
*
9494
* By default, it returns the control's direction.
9595
*
9696
* @param control The current control.
9797
*/
98-
protected open fun getCurrentDirection(control: Control): Control.Direction = control.direction
98+
protected open fun getDirection(control: Control): Control.Direction = control.direction
99+
100+
/**
101+
* Calculate the maximum distance from the center that can be reached.
102+
*
103+
* @param control The [Control] from where the drawer is used.
104+
*/
105+
protected open fun getMaxDistance(control: Control): Double = control.radius
99106

100107
override fun canDraw(control: Control): Boolean = true
101108

@@ -106,7 +113,7 @@ open class SimpleDrawer(
106113
mConfigured = true
107114
}
108115

109-
val direction = getCurrentDirection(control)
116+
val direction = getDirection(control)
110117

111118
if (!isValid(control)) {
112119
lastDirection = direction

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/drawer/drawable/DrawableDrawer.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@ open class DrawableDrawer(
262262
/**
263263
* Gets the scaled width dimension of the drawable.
264264
*/
265-
protected val width: Float
265+
protected open val width: Float
266266
get() = properties.drawable.intrinsicWidth * properties.scale
267267

268268
/**
269269
* Gets for the scaled height dimension of the drawable.
270270
*/
271-
protected val height: Float
271+
protected open val height: Float
272272
get() = properties.drawable.intrinsicHeight * properties.scale
273273

274274
/**
@@ -281,12 +281,7 @@ open class DrawableDrawer(
281281
*/
282282
protected val halfHeight: Float get() = height / 2f
283283

284-
285-
/**
286-
* Calculates the maximum distance from the center of the control
287-
* that the drawable can reach.
288-
* */
289-
protected open fun getMaxRadius(control: Control): Double {
284+
override fun getMaxDistance(control: Control): Double {
290285
return if (properties.isBounded)
291286
control.radius - maxOf(halfWidth, halfHeight)
292287
else control.radius
@@ -298,7 +293,7 @@ open class DrawableDrawer(
298293
* @param control The [Control] from where the drawer is used.
299294
*/
300295
protected open fun getPosition(control: Control): ImmutablePosition {
301-
val max = getMaxRadius(control)
296+
val max = getMaxDistance(control)
302297
return if (max <= 0) {
303298
withLogger("DrawableDrawer") {
304299
error(

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/drawer/shapes/arc/ArcDrawer.kt

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ open class ArcDrawer(
9090
*/
9191
protected open val arcCircle: Circle = Circle(1f, Position())
9292

93-
override fun configure() {
94-
properties.paint.apply {
95-
style = Paint.Style.STROKE
96-
color = properties.colors.primary
97-
strokeWidth = properties.strokeWidth
98-
}
99-
}
100-
10193
/**
10294
* @param colors The colors for the drawer.
10395
* @param strokeWidth The stroke width of the paint.
@@ -118,30 +110,38 @@ open class ArcDrawer(
118110
var isBounded: Boolean = true,
119111
) : ColorfulProperties(colors) {
120112

113+
init {
114+
paint.apply {
115+
style = Paint.Style.STROKE
116+
this.strokeWidth = clampStrokeWidth(strokeWidth)
117+
}
118+
}
119+
121120
/**
122121
* The sweep angle of the arc.
123122
* */
124-
var sweepAngle = getSweepAngle(sweepAngle)
123+
var sweepAngle = clampSweepAngle(sweepAngle)
125124
/**
126125
* Sets the sweep angle of the arc.
127126
*
128-
* @see [getSweepAngle]
127+
* @see [clampSweepAngle]
129128
* */
130129
set(value) {
131-
field = getSweepAngle(value)
130+
field = clampSweepAngle(value)
132131
}
133132

134133
/**
135134
* The width of the arc line
136135
* */
137-
var strokeWidth = getStrokeWidth(strokeWidth)
136+
var strokeWidth = clampStrokeWidth(strokeWidth)
138137
/**
139138
* Sets the width of the arc line
140139
*
141-
* @see [getStrokeWidth]
140+
* @see [clampStrokeWidth]
142141
* */
143142
set(value) {
144-
field = getStrokeWidth(value)
143+
field = clampStrokeWidth(value)
144+
paint.strokeWidth = field
145145
}
146146
}
147147

@@ -162,28 +162,28 @@ open class ArcDrawer(
162162
const val MIN_STROKE_WIDTH: Float = 5f
163163

164164
/**
165-
* Checks if the [sweepAngle] value meets the valid range.
165+
* Clamps the [sweepAngle] value in the valid range.
166166
*
167167
* @param sweepAngle The angle (degrees) value.
168168
*
169169
* @return A valid sexagesimal degree value in the range [MIN_SWEEP_ANGLE] to [MAX_SWEEP_ANGLE].
170170
*/
171171
@JvmStatic
172172
@FloatRange(from = MIN_SWEEP_ANGLE.toDouble(), to = MAX_SWEEP_ANGLE.toDouble())
173-
fun getSweepAngle(sweepAngle: Float): Float {
173+
fun clampSweepAngle(sweepAngle: Float): Float {
174174
return sweepAngle.coerceIn(MIN_SWEEP_ANGLE, MAX_SWEEP_ANGLE)
175175
}
176176

177177
/**
178-
* Checks if the [strokeWidth] value meets the valid range.
178+
* Clamps the [strokeWidth] value in the valid range.
179179
*
180180
* @param strokeWidth The angle value.
181181
*
182182
* @return A valid stroke width in the range [MIN_STROKE_WIDTH] to [strokeWidth].
183183
*/
184184
@JvmStatic
185185
@FloatRange(from = MIN_SWEEP_ANGLE.toDouble())
186-
fun getStrokeWidth(strokeWidth: Float): Float {
186+
fun clampStrokeWidth(strokeWidth: Float): Float {
187187
return strokeWidth.coerceAtLeast(MIN_STROKE_WIDTH)
188188
}
189189

@@ -309,12 +309,8 @@ open class ArcDrawer(
309309
}
310310
}
311311

312-
/**
313-
* Calculate the maximum distance from the center that can be reached.
314-
*
315-
* @param control The [Control] from where the drawer is used.
316-
*/
317-
protected open fun getDistance(control: Control): Double = control.radius.let {
312+
313+
override fun getMaxDistance(control: Control): Double = control.radius.let {
318314
if (properties.isBounded)
319315
it - properties.strokeWidth * 2
320316
else it
@@ -344,7 +340,7 @@ open class ArcDrawer(
344340
protected open fun drawShapes(canvas: Canvas, control: Control) {
345341
arcCircle.apply {
346342
setCenter(control.center)
347-
radius = getDistance(control)
343+
radius = getMaxDistance(control)
348344
}
349345

350346
val angle: Double = control.angle
@@ -412,7 +408,7 @@ open class ArcDrawer(
412408

413409
return RadialGradient(
414410
position.x, position.y,
415-
getDistance(control).toFloat(),
411+
getMaxDistance(control).toFloat(),
416412
intArrayOf(accent, primary),
417413
null,
418414
Shader.TileMode.CLAMP

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/drawer/shapes/arc/CircleArcDrawer.kt

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.yoimerdr.android.virtualjoystick.control.Control
55
import com.yoimerdr.android.virtualjoystick.drawer.core.ControlDrawer
66
import com.yoimerdr.android.virtualjoystick.drawer.shapes.circle.CircleDrawer.CircleProperties
77
import com.yoimerdr.android.virtualjoystick.drawer.core.DrawerRadius
8-
import com.yoimerdr.android.virtualjoystick.drawer.shapes.circle.CircleDrawer
98
import com.yoimerdr.android.virtualjoystick.theme.ColorsScheme
109

1110
/**
@@ -138,6 +137,80 @@ open class CircleArcDrawer protected constructor(
138137
DrawerRadius.Ratio(ratio),
139138
isBounded
140139
)
140+
141+
/**
142+
* Creates a [CircleArcDrawer] with a radius based on a ratio.
143+
* @param color The color for the drawer.
144+
* @param strokeWidth The stroke width of the paint.
145+
* @param sweepAngle The arc sweep angle.
146+
* @param ratio The ratio value.
147+
* @param isBounded Indicates whether the maximum distance is bounded.
148+
*
149+
* @see [DrawerRadius.Ratio]
150+
* */
151+
@JvmOverloads
152+
@JvmStatic
153+
fun withRatio(
154+
color: Int,
155+
strokeWidth: Float,
156+
sweepAngle: Float,
157+
ratio: Float,
158+
isBounded: Boolean = true,
159+
) = CircleArcDrawer(
160+
color,
161+
strokeWidth,
162+
sweepAngle,
163+
DrawerRadius.Ratio(ratio),
164+
isBounded
165+
)
166+
167+
/**
168+
* Creates a [CircleArcDrawer] with a radius value.
169+
* @param colors The colors for the drawer.
170+
* @param strokeWidth The stroke width of the paint.
171+
* @param sweepAngle The arc sweep angle.
172+
* @param radius The radius value.
173+
* @param isBounded Indicates whether the maximum distance is bounded.
174+
*
175+
* @see [DrawerRadius.Fixed]
176+
* */
177+
fun withRadius(
178+
colors: ColorsScheme,
179+
strokeWidth: Float,
180+
sweepAngle: Float,
181+
radius: Float,
182+
isBounded: Boolean = true,
183+
) = CircleArcDrawer(
184+
colors,
185+
strokeWidth,
186+
sweepAngle,
187+
DrawerRadius.Fixed(radius),
188+
isBounded
189+
)
190+
191+
/**
192+
* Creates a [CircleArcDrawer] with a radius value.
193+
* @param color The color for the drawer.
194+
* @param strokeWidth The stroke width of the paint.
195+
* @param sweepAngle The arc sweep angle.
196+
* @param radius The radius value.
197+
* @param isBounded Indicates whether the maximum distance is bounded.
198+
*
199+
* @see [DrawerRadius.Fixed]
200+
* */
201+
fun withRadius(
202+
color: Int,
203+
strokeWidth: Float,
204+
sweepAngle: Float,
205+
radius: Float,
206+
isBounded: Boolean = true,
207+
) = CircleArcDrawer(
208+
color,
209+
strokeWidth,
210+
sweepAngle,
211+
DrawerRadius.Fixed(radius),
212+
isBounded
213+
)
141214
}
142215

143216
private var mCircleDrawer: ControlDrawer? = circleDrawer
@@ -164,8 +237,8 @@ open class CircleArcDrawer protected constructor(
164237
protected open fun getCircleRadius(control: Control): Double =
165238
properties.circleProperties.radius.getValue(control)
166239

167-
override fun getDistance(control: Control): Double {
168-
val max = super.getDistance(control)
240+
override fun getMaxDistance(control: Control): Double {
241+
val max = super.getMaxDistance(control)
169242

170243
return (control.distance + getCircleRadius(control))
171244
.coerceAtMost(max)

virtualjoystick/src/main/java/com/yoimerdr/android/virtualjoystick/drawer/shapes/circle/CircleDrawer.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ open class CircleDrawer(
158158
*/
159159
protected open fun getCircleRadius(control: Control): Double = properties.radius.getValue(control)
160160

161-
/**
162-
* Gets the maximum distance to where the center position of the circle can be.
163-
*/
164-
protected open fun getMaxDistance(control: Control): Double = control.radius.let {
161+
override fun getMaxDistance(control: Control): Double = control.radius.let {
165162
if (properties.isBounded)
166163
it - getCircleRadius(control)
167164
else it

0 commit comments

Comments
 (0)