custom-hot-zone.component.ts 19.8 KB
Newer Older
1 2 3 4
import { Component, ElementRef, EventEmitter, HostListener, Input, OnChanges, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { EditorItem, HotZoneImageItem, Label, MySprite } from './Unit';
limingzhe's avatar
limingzhe committed
5 6 7 8 9 10 11 12 13 14 15
import TWEEN from '@tweenjs/tween.js';


@Component({
  selector: 'app-custom-hot-zone',
  templateUrl: './custom-hot-zone.component.html',
  styleUrls: ['./custom-hot-zone.component.scss']
})
export class CustomHotZoneComponent implements OnInit, OnDestroy, OnChanges {


16 17 18 19 20 21 22 23 24 25 26 27 28


  _bgItem = null;

  @Input()
  set bgItem(v) {
    this._bgItem = v;
    this.init();
  }
  get bgItem() {
    return this._bgItem;
  }

李维's avatar
李维 committed
29 30 31 32
  @Input() imgItemArr = null;
  @Input() hotZoneItemArr = null;
  @Input() hotZoneArr = null;
  @Output() save = new EventEmitter();
limingzhe's avatar
limingzhe committed
33

34 35 36 37 38
  @ViewChild('canvas') canvas: ElementRef;
  @ViewChild('wrap') wrap: ElementRef;
  @HostListener('window:resize', ['$event'])
  onResize(event) {
    this.g_winResizeEventStream.next();
limingzhe's avatar
limingzhe committed
39 40
  }

41 42 43 44
  g_winResizeEventStream = new Subject();
  firstTouch = true;
   canvasWidth = 960;
  canvasHeight = 155;
limingzhe's avatar
limingzhe committed
45

46 47
  canvasBaseW = 960;
  canvasBaseH = 155;
limingzhe's avatar
limingzhe committed
48
  mapScale = 1;
49

limingzhe's avatar
limingzhe committed
50 51 52 53
  ctx;
  mx;
  my; // 点击坐标

limingzhe's avatar
limingzhe committed
54 55 56

  // 资源
  // rawImages = new Map(res);
57 58 59 60 61 62

  // 声音
  bgAudio = new Audio();
  images = new Map();
  animationId: any;
  // winResizeEventStream = new Subject();
limingzhe's avatar
limingzhe committed
63 64 65 66 67 68 69 70 71 72
  canvasLeft;
  canvasTop;
  renderArr;
  imgArr = [];
  oldPos;
  curItem;
  bg: MySprite;
  changeSizeFlag = false;
  changeTopSizeFlag = false;
  changeRightSizeFlag = false;
73 74
  hotZoneChanged = false;
  scale = 1;
limingzhe's avatar
limingzhe committed
75

76
  constructor(private el: ElementRef) {
limingzhe's avatar
limingzhe committed
77 78 79 80
  }


  ngOnInit() {
81
    if(!this.bgItem.url){
李维's avatar
李维 committed
82
      this.bgItem.url = ""
83
    }
limingzhe's avatar
limingzhe committed
84 85 86 87 88 89 90 91 92 93 94 95
    this.initListener();
    this.update();
  }

  ngOnDestroy() {
    window.cancelAnimationFrame(this.animationId);
  }

  ngOnChanges() {

  }

李维's avatar
李维 committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109
  checkDisabel(items, val, index) {
    let valIndex_1 = items.indexOf(-1)
    let valIndex_2 = items.indexOf(-2)
    if(valIndex_1 == index || valIndex_2 == index) { 
      return false
    } else {
      if(valIndex_1 == -1 && valIndex_2 == -1 ) {
        return false
      } else {
        return true
      }
    }
  }

limingzhe's avatar
limingzhe committed
110 111
  onBackgroundUploadSuccess(e) {
    this.bgItem.url = e.url;
112 113 114
    this.refreshBackground(() => {
      this.autoSave()
    });
limingzhe's avatar
limingzhe committed
115 116
  }

limingzhe's avatar
limingzhe committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  refreshBackground(callBack = null) {
    if (!this.bg) {
      this.bg = new MySprite(this.ctx);
      this.renderArr.push(this.bg);
    }
    const bg = this.bg;
    if (this.bgItem.url) {
      bg.load(this.bgItem.url).then(() => {
        const rate1 = this.canvasWidth / bg.width;
        const rate2 = this.canvasHeight / bg.height;
        const rate = Math.min(rate1, rate2);
        bg.setScaleXY(rate);
        bg.x = this.canvasWidth / 2;
        bg.y = this.canvasHeight / 2;
        if (callBack) {
          callBack();
        }
134 135 136
      }).catch((error) => {
        console.log(error)
      })
limingzhe's avatar
limingzhe committed
137 138 139 140 141 142
    }
  }

  addBtnClick() {
    const item = this.getHotZoneItem();
    this.hotZoneArr.push(item);
limingzhe's avatar
limingzhe committed
143
    this.refreshHotZoneId();
李维's avatar
李维 committed
144
    this.refreshIndexNumber()
145
    this.autoSave()
limingzhe's avatar
limingzhe committed
146 147
  }

148 149
  handleMoveItemUp(event, index) {
    if (index != 0) {
李维's avatar
李维 committed
150
      this.bgItem.questions[index] = this.bgItem.questions.splice(index - 1, 1, this.bgItem.questions[index])[0];
151
    } else {
李维's avatar
李维 committed
152
      this.bgItem.questions.push(this.bgItem.questions.shift());
153 154 155
    }
    this.autoSave()
  }
limingzhe's avatar
limingzhe committed
156

157
  handleMoveItemDown(event, index) {
李维's avatar
李维 committed
158 159
    if (index != this.bgItem.questions.length - 1) {
      this.bgItem.questions[index] = this.bgItem.questions.splice(index + 1, 1, this.bgItem.questions[index])[0];
160
    } else {
李维's avatar
李维 committed
161
      this.bgItem.questions.unshift(this.bgItem.questions.splice(index, 1)[0]);
162 163 164
    }
    this.autoSave()
  }
limingzhe's avatar
limingzhe committed
165

166 167 168 169 170 171 172 173
  onImgUploadSuccessByImg(e, item) {
    item.pic_url = e.url;
    item.image_url = e.url;
    item.initNew((w, h) => {
      item.setScaleXY(Math.min(100 / w, 150 / h))
    })
    this.refreshImage(item);
    this.autoSave()
limingzhe's avatar
limingzhe committed
174 175
  }

李维's avatar
李维 committed
176 177 178
  onAudioUploadSuccess(e, item, key) {
    if(key) {
      item[key] = e.url;
李维's avatar
李维 committed
179 180
    } else {
      item.audio_url = e.url;
李维's avatar
李维 committed
181 182
    }
    
183
    this.autoSave()
limingzhe's avatar
limingzhe committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197
  }

  refreshImage(img) {
    this.hideAllLineDash();
    img.picItem = this.getPicItem(img);
    this.refreshImageId();
  }

  refreshHotZoneId() {
    for (let i = 0; i < this.hotZoneArr.length; i++) {
      this.hotZoneArr[i].index = i;
    }
  }

198 199 200
  refreshHotZoneText(item) {
    item.initNew()
  }
limingzhe's avatar
limingzhe committed
201 202 203 204 205 206 207 208 209 210

  refreshImageId() {
    for (let i = 0; i < this.imgArr.length; i++) {
      this.imgArr[i].id = i;
      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.text = 'Image-' + (i + 1);
      }
    }
  }

211
  getHotZoneItem(saveData = null, newRect?) {
limingzhe's avatar
limingzhe committed
212 213
    const itemW = 200;
    const itemH = 200;
214
    const item = new HotZoneImageItem(this.ctx);
limingzhe's avatar
limingzhe committed
215
    if (saveData) {
216 217 218 219 220 221 222
      item.init(saveData.media.image_url, saveData.media.text, saveData.media.type, (w, h) => {
        const saveRect = saveData.rect;
        item.scaleX = (saveData.rect.width / saveData.scale) / w;
        item.scaleY = (saveData.rect.height / saveData.scale) / h;
        item.x = saveRect.x / saveData.scale + newRect.x
        item.y = saveRect.y / saveData.scale + newRect.y
      });
李维's avatar
李维 committed
223
      item.image_url = saveData.media.image_url
224 225
      // item.text = saveData.media.text
      // item.type = saveData.media.type
李维's avatar
李维 committed
226
      item.audio_url = saveData.media.audio_url?saveData.media.audio_url:""
李维's avatar
李维 committed
227
      // item.image_url = "assets/play/bg_70_40.png"
李维's avatar
李维 committed
228
      // item.text = ""
229 230
      item.type = "Image"
    } else {
李维's avatar
李维 committed
231
      item.init(`assets/play/hotZooCard/bg_70_40_${this.hotZoneItemArr.length+1}.png`, null, "Image", () => {
李维's avatar
李维 committed
232
        // item.image_url = "assets/play/bg_70_40.png"
233 234 235
        item.x = this.canvasWidth / 2 - 35;
        item.y = this.canvasHeight / 2 - 20;
      }, () => this.autoSave());
limingzhe's avatar
limingzhe committed
236
    }
李维's avatar
李维 committed
237
    item.text = item.text?item.text:""
238 239
    item.anchorX = 0.5;
    item.anchorY = 0.5;
limingzhe's avatar
limingzhe committed
240 241 242 243

    return item;
  }

李维's avatar
李维 committed
244 245 246 247 248 249 250 251
  refreshIndexNumber() {
    this.hotZoneArr.forEach((element: HotZoneImageItem, index) => {
      element.init(`assets/play/hotZooCard/bg_70_40_${index+1}.png`, element.text, "Image", ()=>{
      
      })
    }, () => this.autoSave());
    // this.autoSave()
  }
limingzhe's avatar
limingzhe committed
252 253 254

  getPicItem(img, saveData = null) {
    const item = new EditorItem(this.ctx);
limingzhe's avatar
limingzhe committed
255
    item.load(img.pic_url).then(img => {
limingzhe's avatar
limingzhe committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
      let maxW, maxH;
      if (this.bg) {
        maxW = this.bg.width * this.bg.scaleX;
        maxH = this.bg.height * this.bg.scaleY;
      } else {
        maxW = this.canvasWidth;
        maxH = this.canvasHeight;
      }
      let scaleX = maxW / 3 / item.width;
      let scaleY = maxH / 3 / item.height;
      if (item.height * scaleX < this.canvasHeight) {
        item.setScaleXY(scaleX);
      } else {
        item.setScaleXY(scaleY);
      }
      item.x = this.canvasWidth / 2;
      item.y = this.canvasHeight / 2;
      if (saveData) {
        const saveRect = saveData.rect;
        item.setScaleXY(saveRect.width / item.width);
limingzhe's avatar
limingzhe committed
276
        item.x = saveRect.x + saveRect.width / 2;
limingzhe's avatar
limingzhe committed
277 278 279 280
        item.y = saveRect.y + saveRect.height / 2;
      } else {
        item.showLineDash();
      }
281 282
      this.autoSave()
    })
limingzhe's avatar
limingzhe committed
283 284 285 286
    return item;
  }


李维's avatar
李维 committed
287 288
  onAudioUploadSuccessByImg(e, item) {
    item.audio_url = e.url;
289
    this.autoSave()
limingzhe's avatar
limingzhe committed
290 291 292 293 294
  }

  deleteItem(e, i) {
    this.hotZoneArr.splice(i, 1);
    this.refreshHotZoneId();
李维's avatar
李维 committed
295
    this.refreshIndexNumber()
296
    this.autoSave()
limingzhe's avatar
limingzhe committed
297
  }
李维's avatar
李维 committed
298 299 300 301 302 303
  
  deleteQuestionItem(e, i) {
    this.bgItem.questions.splice(i, 1);
    // this.refreshHotZoneId();
    this.autoSave()
  }
limingzhe's avatar
limingzhe committed
304 305 306 307 308 309 310 311 312

  init() {
    this.initData();
    this.initCtx();
    this.initItem();
  }

  initItem() {
    if (!this.bgItem) {
李维's avatar
李维 committed
313
      this.bgItem = { };
limingzhe's avatar
limingzhe committed
314
    } else {
李维's avatar
李维 committed
315 316 317
      if(!this.bgItem.questions) {
        this.bgItem.questions = []
      }
limingzhe's avatar
limingzhe committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      this.refreshBackground(() => {
        if (!this.hotZoneItemArr) {
          this.hotZoneItemArr = [];
        } else {
          this.initHotZoneArr();
        }
      });
    }
  }

  initHotZoneArr() {
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
333
      curBgRect = { x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight };
limingzhe's avatar
limingzhe committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347
    }
    let oldBgRect = this.bgItem.rect;
    if (!oldBgRect) {
      oldBgRect = curBgRect;
    }
    const rate = curBgRect.width / oldBgRect.width;
    this.hotZoneArr = [];
    const arr = this.hotZoneItemArr.concat();
    for (let i = 0; i < arr.length; i++) {
      const data = JSON.parse(JSON.stringify(arr[i]));
      data.rect.x *= rate;
      data.rect.y *= rate;
      data.rect.width *= rate;
      data.rect.height *= rate;
348 349 350 351
      // data.rect.x += curBgRect.x;
      // data.rect.y += curBgRect.y;
      const item = this.getHotZoneItem(data, { x: curBgRect.x, y: curBgRect.y });
      // c//onsole.log("初始化存储数据", data)
limingzhe's avatar
limingzhe committed
352 353 354 355 356
      this.hotZoneArr.push(item);
    }
    this.refreshHotZoneId();
  }

李维's avatar
李维 committed
357 358 359
  addQustion() {
    this.bgItem.questions.push({
      number: 2,
李维's avatar
李维 committed
360 361
      combin: [0, 0],
      _combin: [0, 0]
李维's avatar
李维 committed
362
    })
李维's avatar
李维 committed
363
    this.autoSave()
李维's avatar
李维 committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  }

  handleCombinLength(item) {
    if(Number(item.number)>5){
      return
    }

    if( Number(item.number) > item.combin.length ) {
      let num = Number(item.number) - item.combin.length
      for(var index=0; index<num; index++) {
        item.combin.push(0)
      }
    } else {
      item.combin = item.combin.splice(0, Number(item.number))
    }
    this.autoSave()
    console.log(item.combin)
  }

limingzhe's avatar
limingzhe committed
383 384 385 386 387
  initImgArr() {
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
388
      curBgRect = { x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight };
limingzhe's avatar
limingzhe committed
389 390 391 392 393 394 395 396 397 398 399 400 401
    }

    let oldBgRect = this.bgItem.rect;
    if (!oldBgRect) {
      oldBgRect = curBgRect;
    }

    const rate = curBgRect.width / oldBgRect.width;
    this.imgArr = [];
    const arr = this.imgItemArr.concat();
    for (let i = 0; i < arr.length; i++) {

      const data = JSON.parse(JSON.stringify(arr[i]));
402
      const img = { pic_url: data.pic_url };
limingzhe's avatar
limingzhe committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

      data.rect.x *= rate;
      data.rect.y *= rate;
      data.rect.width *= rate;
      data.rect.height *= rate;

      data.rect.x += curBgRect.x;
      data.rect.y += curBgRect.y;

      img['picItem'] = this.getPicItem(img, data);
      img['audio_url'] = arr[i].audio_url;
      this.imgArr.push(img);
    }
    this.refreshImageId();
  }

李维's avatar
李维 committed
419 420 421 422 423 424 425 426 427 428 429 430 431
  getQuestionWord(combin) {
    let str = ""
    combin.forEach(element => {
      if(element == -1) {
        str += this.bgItem.wordLeft_text?this.bgItem.wordLeft_text:"";
      } else if(element == -2) {
        str += this.bgItem.wordRight_text?this.bgItem.wordRight_text:"";
      } else {
        str += this.hotZoneArr[element] && this.hotZoneArr[element].text?this.hotZoneArr[element].text:"";
      }
    });
    return str
  }
limingzhe's avatar
limingzhe committed
432 433 434

  initData() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
435 436
    this.canvasHeight = this.wrap.nativeElement.clientWidth * (155 / 960);
    this.scale = 960 * 0.95 / this.wrap.nativeElement.clientWidth
limingzhe's avatar
limingzhe committed
437 438 439 440 441 442 443 444 445 446 447 448
    this.mapScale = this.canvasWidth / this.canvasBaseW;
    this.renderArr = [];
    this.bg = null;
    this.imgArr = [];
    this.hotZoneArr = [];
  }

  initCtx() {
    this.ctx = this.canvas.nativeElement.getContext('2d');
    this.canvas.nativeElement.width = this.canvasWidth;
    this.canvas.nativeElement.height = this.canvasHeight;
  }
449
  
limingzhe's avatar
limingzhe committed
450
  mapDown(event) {
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    this.oldPos = { x: this.mx, y: this.my };
    const arr = this.hotZoneArr;
    for (let i = arr.length - 1; i >= 0; i--) {
      const item = arr[i];
      if (item) {
        if (this.checkClickTarget(item)) {
          if (item.lineDashFlag && this.checkClickTarget(item.arrow)) {
            if(item.type == "Text"){
              return
            }
            this.changeItemSize(item);
          } else if (item.lineDashFlag && this.checkClickTarget(item.arrowTop)) {
            if(item.type == "Text"){
              return
            }
            this.changeItemTopSize(item);
          } else if (item.lineDashFlag && this.checkClickTarget(item.arrowRight)) {
            if(item.type == "Text"){
              return
            }
            this.changeItemRightSize(item);
          } else {
            this.changeCurItem(item);
          }
          this.hotZoneChanged = true;
          return;
        }
limingzhe's avatar
limingzhe committed
478 479 480 481 482 483

      }
    }
  }

  mapMove(event) {
484
    if (!this.curItem) { return; }
limingzhe's avatar
limingzhe committed
485 486 487 488 489 490 491 492 493 494

    if (this.changeSizeFlag) {
      this.changeSize();

    } else if (this.changeTopSizeFlag) {
      this.changeTopSize();

    } else if (this.changeRightSizeFlag) {
      this.changeRightSize();
    } else {
495 496
      let addX = this.mx - this.oldPos.x;
      let addY = this.my - this.oldPos.y;
limingzhe's avatar
limingzhe committed
497 498 499 500
      this.curItem.x += addX;
      this.curItem.y += addY;
    }

501
    this.oldPos = { x: this.mx, y: this.my };
limingzhe's avatar
limingzhe committed
502 503 504 505 506 507 508 509 510 511 512
  }

  mapUp(event) {
    this.curItem = null;
    this.changeSizeFlag = false;
    this.changeTopSizeFlag = false;
    this.changeRightSizeFlag = false;
  }

  changeSize() {
    const rect = this.curItem.getBoundingBox();
513 514 515
    let distance = this.my - rect.y;
    let lenW = this.mx - rect.x;
    let lenH = rect.height - distance
limingzhe's avatar
limingzhe committed
516
    let minLen = 20;
517 518 519
    let sx, sy;
    if (lenW < minLen) {
      lenW = minLen;
limingzhe's avatar
limingzhe committed
520
    }
521 522 523 524 525 526 527 528
    sx = lenW / this.curItem.width;
    if (lenH < minLen) {
      lenH = minLen;
    }
    sy = lenH / this.curItem.height;
    this.curItem.y = this.curItem.y + distance
    this.curItem.scaleX = sx;
    this.curItem.scaleY = sy;
limingzhe's avatar
limingzhe committed
529 530 531 532 533
  }


  changeTopSize() {
    const rect = this.curItem.getBoundingBox();
534 535
    let lenH = rect.y - this.my + rect.height;
    this.curItem.y = this.my
limingzhe's avatar
limingzhe committed
536 537
    let minLen = 20;
    let s;
limingzhe's avatar
limingzhe committed
538 539 540 541
    if (lenH < minLen) {
      lenH = minLen;
    }
    s = lenH / this.curItem.height;
542
    this.curItem.setScaleXY(s);
limingzhe's avatar
limingzhe committed
543 544 545 546
  }

  changeRightSize() {
    const rect = this.curItem.getBoundingBox();
547
    let lenW = this.mx - rect.x;
limingzhe's avatar
limingzhe committed
548 549 550 551 552 553
    let minLen = 20;
    let s;
    if (lenW < minLen) {
      lenW = minLen;
    }
    s = lenW / this.curItem.width;
554
    this.curItem.setScaleXY(s);
limingzhe's avatar
limingzhe committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
  }

  changeItemSize(item) {
    this.curItem = item;
    this.changeSizeFlag = true;
  }

  changeItemTopSize(item) {
    this.curItem = item;
    this.changeTopSizeFlag = true;
  }

  changeItemRightSize(item) {
    this.curItem = item;
    this.changeRightSizeFlag = true;
  }

  changeCurItem(item) {
    this.hideAllLineDash();
    this.curItem = item;
    this.curItem.showLineDash();
  }

李维's avatar
李维 committed
578 579 580 581
  handlePartChange(e, QI, index) {
    let a = JSON.parse(JSON.stringify(this.bgItem.questions[QI].combin))
    a[index] = Number(e)
    this.bgItem.questions[QI].combin = a
李维's avatar
李维 committed
582
    this.autoSave()
李维's avatar
李维 committed
583 584
  }

limingzhe's avatar
limingzhe committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  hideAllLineDash() {
    for (let i = 0; i < this.imgArr.length; i++) {
      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.hideLineDash();
      }
    }
  }


  update() {
    this.animationId = window.requestAnimationFrame(this.update.bind(this));
    // 清除画布内容
    this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
    for (let i = 0; i < this.renderArr.length; i++) {
      this.renderArr[i].update(this);
    }
    this.updateArr(this.hotZoneArr);
    TWEEN.update();
  }

  updateArr(arr) {
    if (arr) {
      for (let i = 0; i < arr.length; i++) {
        arr[i].update();
      }
    }
  }

  renderAfterResize() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    this.init();
  }

  initListener() {
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    const element = this.canvas.nativeElement;
    this.g_winResizeEventStream.pipe(debounceTime(500)).subscribe(data => {
      this.renderAfterResize();
    });
    
    const addTouchListener = () => {
      element.addEventListener('touchstart', touchDownFunc);
      element.addEventListener('touchmove', touchMoveFunc);
      element.addEventListener('touchend', touchUpFunc);
      element.addEventListener('touchcancel', touchUpFunc);
    };
    const removeTouchListener = () => {
      element.removeEventListener('touchstart', touchDownFunc);
      element.removeEventListener('touchmove', touchMoveFunc);
      element.removeEventListener('touchend', touchUpFunc);
      element.removeEventListener('touchcancel', touchUpFunc);
    };

    const addMouseListener = () => {
      element.addEventListener('mousedown', mouseDownFunc);
      element.addEventListener('mousemove', mouseMoveFunc);
      element.addEventListener('mouseup', mouseUpFunc);
    };
    const removeMouseListener = () => {
      element.removeEventListener('mousedown', mouseDownFunc);
      element.removeEventListener('mousemove', mouseMoveFunc);
      element.removeEventListener('mouseup', mouseUpFunc);
    };

    const touchDownFunc = (e) => {
      if (this.firstTouch) {
        this.firstTouch = false;
        removeMouseListener();
      }
      setMxMyByTouch(e);
      this.mapDown(e);
    };
    const touchMoveFunc = (e) => {
      setMxMyByTouch(e);
      this.mapMove(e);
    };
    const touchUpFunc = (e) => {
      setMxMyByTouch(e);
      this.mapUp(e);
    };

    const mouseDownFunc = (e) => {
      if (this.firstTouch) {
        this.firstTouch = false;
        removeTouchListener();
      }
      setMxMyByMouse(e);
      this.mapDown(e);
    };
    const mouseMoveFunc = (e) => {
      setMxMyByMouse(e);
      this.mapMove(e);
    };
    const mouseUpFunc = (e) => {
      setMxMyByMouse(e);
      this.mapUp(e);
    };

    const setMxMyByTouch = event => {
      if (event.touches.length <= 0) {
        return;
      }
      if (this.canvasLeft == null) {
        setParentOffset();
      }
      this.mx = event.touches[0].pageX - this.canvasLeft;
      this.my = event.touches[0].pageY - this.canvasTop;
    };
limingzhe's avatar
limingzhe committed
693

694 695 696 697 698
    const setParentOffset = () => {
      const rect = this.canvas.nativeElement.getBoundingClientRect();
      this.canvasLeft = rect.left;
      this.canvasTop = rect.top;
    };
limingzhe's avatar
limingzhe committed
699

700 701 702 703
    const setMxMyByMouse = (event) => {
      this.mx = event.offsetX;
      this.my = event.offsetY;
    };
limingzhe's avatar
limingzhe committed
704

705 706
    addMouseListener();
    addTouchListener();
limingzhe's avatar
limingzhe committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

  }

  IsPC() {
    if (window['ELECTRON']) {
      return false; // 封装客户端标记
    }
    if (document.body.ontouchstart !== undefined) {
      return false;
    } else {
      return true;
    }
  }


  checkClickTarget(target) {
    const rect = target.getBoundingBox();
    if (this.checkPointInRect(this.mx, this.my, rect)) {
      return true;
    }
    return false;
  }

  checkPointInRect(x, y, rect) {
    if (x >= rect.x && x <= rect.x + rect.width) {
      if (y >= rect.y && y <= rect.y + rect.height) {
        return true;
      }
    }
    return false;
  }

739 740
  autoSave() {
    // console.log("Auto save")
limingzhe's avatar
limingzhe committed
741 742 743 744
    const bgItem = this.bgItem;
    if (this.bg) {
      bgItem['rect'] = this.bg.getBoundingBox();
    } else {
745
      bgItem['rect'] = { x: 0, y: 0, width: Math.round(this.canvasWidth * 100) / 100, height: Math.round(this.canvasHeight * 100) / 100 };
limingzhe's avatar
limingzhe committed
746 747 748 749 750 751 752 753 754
    }

    const hotZoneItemArr = [];
    const hotZoneArr = this.hotZoneArr;
    for (let i = 0; i < hotZoneArr.length; i++) {
      const hotZoneItem = {
        index: hotZoneArr[i].index,
      };
      hotZoneItem['rect'] = hotZoneArr[i].getBoundingBox();
755 756 757 758
      const currentX = hotZoneItem['rect'].x
      const currentY = hotZoneItem['rect'].y
      hotZoneItem['media'] = {}
      hotZoneItem['scale'] = this.scale
李维's avatar
李维 committed
759 760 761 762
      hotZoneItem['rect'].x = (Math.round((currentX - bgItem['rect'].x) * 1000) / 1000) * this.scale;
      hotZoneItem['rect'].y = (Math.round((currentY - bgItem['rect'].y) * 1000) / 1000) * this.scale;
      hotZoneItem['rect'].width = (Math.round((hotZoneItem['rect'].width) * 1000) / 1000) * this.scale;
      hotZoneItem['rect'].height = (Math.round((hotZoneItem['rect'].height) * 1000) / 1000) * this.scale;
763 764 765
      hotZoneItem['media'].image_url = hotZoneArr[i].image_url
      hotZoneItem['media'].text = hotZoneArr[i].text
      hotZoneItem['media'].type = hotZoneArr[i].type
李维's avatar
李维 committed
766
      hotZoneItem['media'].audio_url = hotZoneArr[i].audio_url ? hotZoneArr[i].audio_url : ""
767
      // hotZoneItem['media'].card_audio_url = hotZoneArr[i].card_audio_url ? hotZoneArr[i].card_audio_url : ""
limingzhe's avatar
limingzhe committed
768 769
      hotZoneItemArr.push(hotZoneItem);
    }
770 771
    this.save.emit({ bgItem, hotZoneItemArr });
    this.hotZoneChanged = false;
limingzhe's avatar
limingzhe committed
772 773
  }

774 775 776
  saveClick() {
    // console.log("Saved")
    this.autoSave()
limingzhe's avatar
limingzhe committed
777
  }
limingzhe's avatar
limingzhe committed
778
}