@@ -263,63 +263,42 @@ data class RawSubscription(
263263 private val xExp by lazy { getExpression(x) }
264264 private val yExp by lazy { getExpression(y) }
265265
266+ private val xArr by lazy { arrayOf(leftExp, rightExp, xExp) }
267+ private val yArr by lazy { arrayOf(topExp, bottomExp, yExp) }
268+
266269 val isValid by lazy {
267- arrayOf(
268- leftExp to topExp,
269- leftExp to bottomExp,
270- rightExp to topExp,
271- rightExp to bottomExp,
272- xExp to yExp,
273- ).any {
274- it.first != null && it.second != null
275- }
270+ xArr.any { it != null } && yArr.any { it != null }
276271 }
277272
278273 /* *
279274 * return (x, y)
280275 */
281276 fun calc (rect : Rect ): Pair <Float , Float >? {
282277 if (! isValid) return null
283- arrayOf(
284- leftExp,
285- topExp,
286- rightExp,
287- bottomExp,
288- xExp,
289- yExp,
290- ).forEach { exp ->
291- if (exp != null ) {
292- setVariables(exp, rect)
293- }
294- }
278+ xArr.forEach { setVariables(it, rect) }
279+ yArr.forEach { setVariables(it, rect) }
295280 try {
296- if (leftExp != null ) {
297- if (topExp != null ) {
298- return (rect.left + leftExp!! .evaluate()
299- .toFloat()) to (rect.top + topExp!! .evaluate().toFloat())
300- }
301- if (bottomExp != null ) {
302- return (rect.left + leftExp!! .evaluate()
303- .toFloat()) to (rect.bottom - bottomExp!! .evaluate().toFloat())
304- }
305- } else if (rightExp != null ) {
306- if (topExp != null ) {
307- return (rect.right - rightExp!! .evaluate()
308- .toFloat()) to (rect.top + topExp!! .evaluate().toFloat())
309- }
310- if (bottomExp != null ) {
311- return (rect.right - rightExp!! .evaluate()
312- .toFloat()) to (rect.bottom - bottomExp!! .evaluate().toFloat())
313- }
314- } else if (xExp != null ) {
315- if (yExp != null ) {
316- return xExp!! .evaluate().toFloat() to yExp!! .evaluate().toFloat()
317- }
281+ val x0 = xArr.find { it != null }!! .evaluate().toFloat()
282+ val y0 = yArr.find { it != null }!! .evaluate().toFloat()
283+ val x = when {
284+ leftExp != null -> rect.left + x0
285+ rightExp != null -> rect.right - x0
286+ xExp != null -> x0
287+ else -> null
288+ }
289+ val y = when {
290+ topExp != null -> rect.top + y0
291+ bottomExp != null -> rect.bottom - y0
292+ yExp != null -> y0
293+ else -> null
294+ }
295+ if (x != null && y != null ) {
296+ return x to y
318297 }
319298 } catch (e: Exception ) {
320299 // 可能存在 1/0 导致错误
321300 e.printStackTrace()
322- LogUtils .d(e)
301+ LogUtils .d(" Position.calc " , e)
323302 toast(e.message ? : e.stackTraceToString())
324303 }
325304 return null
@@ -652,7 +631,8 @@ data class RawSubscription(
652631 " random"
653632 )
654633
655- private fun setVariables (exp : Expression , rect : Rect ) {
634+ private fun setVariables (exp : Expression ? , rect : Rect ) {
635+ if (exp == null ) return
656636 exp.setVariable(" left" , rect.left.toDouble())
657637 exp.setVariable(" top" , rect.top.toDouble())
658638 exp.setVariable(" right" , rect.right.toDouble())
0 commit comments