瀏覽代碼

蒙版画法修改 添加顺时针判断 并转为逆时针

haojianlong 5 年之前
父節點
當前提交
de647d0222
共有 3 個文件被更改,包括 36 次插入8 次删除
  1. 1 1
      package.json
  2. 7 5
      src/divideFloorScene.ts
  3. 28 2
      src/items/SceneMarkItem.ts

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
     "name": "cad-engine",
-    "version": "2.0.165",
+    "version": "2.0.174",
     "description": "上格云 CAD图形引擎。",
     "main": "lib/index.js",
     "types": "lib/index.d.js",

+ 7 - 5
src/divideFloorScene.ts

@@ -50,7 +50,10 @@ export class DivideFloorScene extends FloorScene {
         super();
     } // Constructor
 
-    /** 清除蒙版    */
+    /**
+     * 清除蒙版
+     *
+     */
     clearSceneMark(): void {
         if (this.grabItem) {
             this.removeItem(this.grabItem);
@@ -66,8 +69,7 @@ export class DivideFloorScene extends FloorScene {
      * @return  boolean
      */
     onMouseDown(event: SMouseEvent): boolean {
-        console.log(arguments)
-        if (this.isMarking) {
+        if (this.isMarking && event.buttons == 1) {
             if (this.grabItem) {
                 this.grabItem.onMouseDown(event);
             } else {
@@ -80,7 +82,7 @@ export class DivideFloorScene extends FloorScene {
             super.onMouseDown(event);
         }
         return false;
-    } // Function onClick()
+    } // Function onMouseDown()
 
     /***
      * 键盘按下事件
@@ -95,5 +97,5 @@ export class DivideFloorScene extends FloorScene {
         } else {
             super.onKeyUp(event);
         }
-    }
+    } // Function onKeyUp()
 } // Class DivideFloorScene

+ 28 - 2
src/items/SceneMarkItem.ts

@@ -62,6 +62,7 @@ export class SceneMarkItem extends SGraphyItem {
      * @return	boolean
      */
     onMouseDown(event: SMouseEvent): boolean {
+        console.log(arguments);
         if (!this.closeFlag && event.buttons == 1) {
             let p = new SPoint(event.x, event.y);
             this.lastPoint.x = p.x;
@@ -93,7 +94,7 @@ export class SceneMarkItem extends SGraphyItem {
         if (event.keyCode == 13 && this.outLine.length >= 3) {
             this.createMask();
         }
-    } // Function onKeydown()
+    } // Function onKeyUp()
 
     /**
      * 创建蒙版
@@ -107,10 +108,35 @@ export class SceneMarkItem extends SGraphyItem {
         this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
         this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
         this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
-        this.mask.polygon(this.outLine);
+
+        let antiArr = this.outLine;
+        if(this.isAntiClockWise(this.outLine)){
+            antiArr = this.outLine.reverse();
+        }
+        this.mask.polygon(antiArr);
     } // Function createMask()
 
     /**
+     * 判断多边形点是否逆时针排列
+     *
+     * @param   pArr    点数组
+     * @return  boolean 是-true 否-false
+     */
+    private isAntiClockWise(pArr: SPoint[]): boolean {
+        let newArr = pArr.concat([]);
+        let rightPoint = newArr[0],
+            index = 0;
+        for (let i = 1; i < newArr.length; i++) {
+            if (newArr[i].x > rightPoint.x) {
+                index = i;
+                rightPoint = newArr[i];
+            }
+        }
+        index = index == 0 ? newArr.length - 1 : index - 1;
+        return rightPoint.y > newArr[index].y;
+    } // Function isAntiClockWise
+
+    /**
      * Item绘制操作
      *
      * @param   painter       painter对象