|
@@ -24,12 +24,13 @@
|
|
|
package com.sybotan.android.graphy
|
|
|
|
|
|
import android.graphics.Canvas
|
|
|
+import android.graphics.Matrix
|
|
|
import android.graphics.PointF
|
|
|
import android.graphics.RectF
|
|
|
import android.util.Log
|
|
|
-import android.view.MotionEvent
|
|
|
import com.sybotan.android.graphy.enums.SGraphyItemFlag
|
|
|
import com.sybotan.android.graphy.listeners.SGraphyItemPosListener
|
|
|
+import com.sybotan.android.graphy.utils.MatrixTools
|
|
|
import org.jetbrains.anko.doAsync
|
|
|
import java.util.*
|
|
|
|
|
@@ -57,10 +58,20 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param e 事件参数
|
|
|
* @return 子对象MotionEvent
|
|
|
*/
|
|
|
- fun toChildMotionEvent(child: SGraphyItem, e: MotionEvent): MotionEvent {
|
|
|
- val se = MotionEvent.obtain(e)
|
|
|
- se.setLocation((e.x - child.pos.x) / child.scale.x, (e.y - child.pos.y) / child.scale.y)
|
|
|
- return se
|
|
|
+ fun toChildMotionEvent(child: SGraphyItem, e: SMotionEvent): SMotionEvent {
|
|
|
+ val ce = SMotionEvent(e)
|
|
|
+ ce.matrix.postTranslate(child.pos.x, child.pos.y);
|
|
|
+ ce.matrix.postScale(child.scale.x, child.scale.y);
|
|
|
+ ce.matrix.postRotate(child.rotate,0f, 0f);
|
|
|
+ // 不跟随缩放
|
|
|
+ if (!child.isTransform) {
|
|
|
+ ce.matrix.postScale(child._inverseScale, child._inverseScale);
|
|
|
+ }
|
|
|
+// se.setLocation((e.x - child.pos.x) / child.scale.x, (e.y - child.pos.y) / child.scale.y)
|
|
|
+ val matrixTransform = MatrixTools.matrixTransform(ce.matrix, e.x, e.y)
|
|
|
+ ce.x = matrixTransform.x
|
|
|
+ ce.y = matrixTransform.y
|
|
|
+ return ce
|
|
|
} // Function toSceneMotionEvent()
|
|
|
} // companion object
|
|
|
|
|
@@ -124,7 +135,12 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
}
|
|
|
/** 缩放比例 */
|
|
|
var scale = PointF(1f, 1f)
|
|
|
-
|
|
|
+ /** 旋转 */
|
|
|
+ var rotate = 0.0f
|
|
|
+ /** 是否进行变形 */
|
|
|
+ var isTransform = true;
|
|
|
+ /** 放缩反比例 */
|
|
|
+ var _inverseScale = 1f;
|
|
|
/** item标志 */
|
|
|
var flags: EnumSet<SGraphyItemFlag> = EnumSet.noneOf(SGraphyItemFlag::class.java)
|
|
|
|
|
@@ -362,7 +378,6 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
if (!item.isVisible) { // 如果对象不可见
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
try {
|
|
|
// 保存画布状态
|
|
|
canvas.save()
|
|
@@ -403,7 +418,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param e 按下手势事件。
|
|
|
* @return 如果事件被处理返回 true , 否则返回 false 。
|
|
|
*/
|
|
|
- open fun onDown(e: MotionEvent): Boolean {
|
|
|
+ open fun onDown(e: SMotionEvent): Boolean {
|
|
|
for (item in children) {
|
|
|
if (!item.isVisible) { // 如果对象不可见
|
|
|
continue
|
|
@@ -429,7 +444,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param e 释放手势事件。
|
|
|
* @return 如果事件被处理返回 true , 否则返回 false 。
|
|
|
*/
|
|
|
- open fun onSingleTapUp(e: MotionEvent): Boolean {
|
|
|
+ open fun onSingleTapUp(e: SMotionEvent): Boolean {
|
|
|
Log.d(TAG, "onSingleTapUp: releaseItem")
|
|
|
releaseItem()
|
|
|
|
|
@@ -453,7 +468,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param e 手势事件。
|
|
|
* @return 如果事件被处理返回 true , 否则返回 false 。
|
|
|
*/
|
|
|
- open fun onShowPress(e: MotionEvent): Boolean {
|
|
|
+ open fun onShowPress(e: SMotionEvent): Boolean {
|
|
|
Log.d(TAG, "onShowPress: releaseItem")
|
|
|
for (item in children) {
|
|
|
if (!item.isVisible) { // 如果对象不可见
|
|
@@ -475,7 +490,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param e 长按手势事件。
|
|
|
* @return 如果事件被处理返回 true , 否则返回 false 。
|
|
|
*/
|
|
|
- open fun onLongPress(e: MotionEvent): Boolean {
|
|
|
+ open fun onLongPress(e: SMotionEvent): Boolean {
|
|
|
Log.d(TAG, "onLongPress: releaseItem")
|
|
|
releaseItem()
|
|
|
|
|
@@ -502,7 +517,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param distanceY Y轴上的移动距离,单位:像素
|
|
|
* @return 如果事件被处理返回 true , 否则返回 false 。
|
|
|
*/
|
|
|
- open fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
|
|
|
+ open fun onScroll(e1: SMotionEvent, e2: SMotionEvent, distanceX: Float, distanceY: Float): Boolean {
|
|
|
if (flags.contains(SGraphyItemFlag.ItemIsMovable) && currentMoveItem === this) {
|
|
|
moveTo(pos.x - distanceX * scale.x, pos.y - distanceY * scale.y)
|
|
|
return true
|
|
@@ -539,7 +554,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
* @param velocityY Y轴上的移动速度,像素/秒
|
|
|
* @return 如果事件被处理返回 true , 否则返回 false 。
|
|
|
*/
|
|
|
- open fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
|
|
|
+ open fun onFling(e1: SMotionEvent, e2: SMotionEvent, velocityX: Float, velocityY: Float): Boolean {
|
|
|
Log.d(TAG, "onFling: releaseItem")
|
|
|
releaseItem()
|
|
|
|
|
@@ -581,4 +596,26 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
|
|
|
scene!!.grabItem = null
|
|
|
return
|
|
|
} // Function grabItem
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 场景对象到 item 对象的转换矩阵
|
|
|
+ *
|
|
|
+ * @return 转换矩阵
|
|
|
+ */
|
|
|
+ fun scene2itemMattrix() : Matrix{
|
|
|
+ val m = Matrix()
|
|
|
+ val list = this.itemPath()
|
|
|
+ for ( item in list) {
|
|
|
+ m.postTranslate(item.pos.x, item.pos.y);
|
|
|
+ m.postScale(item.scale.x, item.scale.y);
|
|
|
+ m.postRotate(item.rotate)
|
|
|
+ // 如果不进行变形处理,则取消 painter 的变型操作
|
|
|
+ if (!item.isTransform){
|
|
|
+ m.postScale(item._inverseScale, item._inverseScale);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return m
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
} // Class SGraphyItem
|