custom-hot-zone.component.ts 39.2 KB
Newer Older
范雪寒's avatar
范雪寒 committed
1
import {
liujiaxin's avatar
liujiaxin committed
2 3
  ApplicationRef,
  ChangeDetectorRef,
范雪寒's avatar
范雪寒 committed
4 5 6 7 8 9 10 11 12 13 14 15 16
  Component,
  ElementRef,
  EventEmitter,
  HostListener,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Output,
  ViewChild
} from '@angular/core';
import {Subject} from 'rxjs';
import {debounceTime} from 'rxjs/operators';
liujiaxin's avatar
liujiaxin committed
17
import {DragItem, EditorItem, HotZoneImg, HotZoneItem, HotZoneLabel, Label, MySprite, removeItemFromArr, ShapeRect, ShapeRectNew} from './Unit';
范雪寒's avatar
范雪寒 committed
18
import TWEEN from '@tweenjs/tween.js';
liujiaxin's avatar
liujiaxin committed
19 20 21
import {getMinScale} from '../../play/Unit';
import {tar} from 'compressing';
import {NzMessageService} from 'ng-zorro-antd';
范雪寒's avatar
范雪寒 committed
22 23 24 25 26 27 28 29 30


@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 {

liujiaxin's avatar
liujiaxin committed
31 32 33
  @ViewChild('canvas', {static: true}) canvas: ElementRef;
  @ViewChild('wrap', {static: true}) wrap: ElementRef;

范雪寒's avatar
范雪寒 committed
34 35 36 37 38 39 40 41 42 43 44 45

  @Input()
  hotZoneItemArr = null;
  @Input()
  hotZoneArr = null;
  @Input()
  isHasRect = true;
  @Input()
  isHasPic = true;
  @Input()
  isHasText = true;
  @Input()
liujiaxin's avatar
liujiaxin committed
46 47 48 49 50 51 52 53 54
  isHasAudio = true;
  @Input()
  isHasAnima = false;
  @Input()
  customTypeGroupArr; // [{name:string, rect:boolean, pic:boolean, text:boolean, audio:boolean, anima:boolean, animaNames:['name1', ..]}, ...]
  @Input()
  // hotZoneFontObj;
  @Input()
  isCopyData = false;
limingzhe's avatar
limingzhe committed
55 56 57

  @Input()
  isRotate = false;
liujiaxin's avatar
liujiaxin committed
58 59 60 61 62 63 64
  
  hotZoneFontObj;
  // hotZoneFontObj = {
  //   size: 50,
  //   name: 'ahronbd-1',
  //   color: '#8f3758'
  // }
范雪寒's avatar
范雪寒 committed
65 66 67
  @Input()
  defaultItemType = 'text';
  @Input()
liujiaxin's avatar
liujiaxin committed
68 69 70 71 72 73
  hotZoneImgSize = 0;


  @Output()
  save = new EventEmitter();

范雪寒's avatar
范雪寒 committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  saveDisabled = true;

  canvasWidth = 1280;
  canvasHeight = 720;
  canvasBaseW = 1280;
  // @HostListener('window:resize', ['$event'])
  canvasBaseH = 720;
  mapScale = 1;
  ctx;
  mx;
  my; // 点击坐标
  // 声音
  bgAudio = new Audio();
  images = new Map();
  animationId: any;


  // 资源
  // rawImages = new Map(res);
  winResizeEventStream = new Subject();
  canvasLeft;
  canvasTop;
  renderArr;
  imgArr = [];
  oldPos;
  radioValue;
  curItem;
  bg: MySprite;
  changeSizeFlag = false;
  changeTopSizeFlag = false;
  changeRightSizeFlag = false;
limingzhe's avatar
limingzhe committed
106
  // animaPanelVisible = false;
liujiaxin's avatar
liujiaxin committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  uploadUrl;
  uploadData;
  skeJsonData = {};
  texJsonData = {};
  texPngData = {};
  animaName = '';
  curDragonBoneIndex;
  curDragonBoneGIdx;
  pasteData = '';

  isSkeJsonLoading = false;
  isTexJsonLoading = false;
  isTexPngLoading = false;

  isAnimaSmall = false;

limingzhe's avatar
limingzhe committed
124

liujiaxin's avatar
liujiaxin committed
125 126 127 128 129 130 131 132
  savePropertyArr = [
    'id',
    'gIdx',
    'selectType',
    'labelText',
    'posX',
    'posY',
    'mathLabel',
limingzhe's avatar
limingzhe committed
133 134 135 136
    'skeJsonData',
    'texJsonData',
    'texPngData',
    'animType',
liujiaxin's avatar
liujiaxin committed
137 138 139 140 141 142 143 144 145 146 147
  ]


  constructor(private nzMessageService: NzMessageService, private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef) {
    this.uploadUrl = (<any> window).courseware.uploadUrl();
    this.uploadData = (<any> window).courseware.uploadData();

    window['air'].getUploadCallback = (url, data) => {
      this.uploadUrl = url;
      this.uploadData = data;
    };
范雪寒's avatar
范雪寒 committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  }

  _bgItem = null;

  get bgItem() {
    return this._bgItem;
  }

  @Input()
  set bgItem(v) {
    this._bgItem = v;

    this.init();
  }

limingzhe's avatar
limingzhe committed
163 164 165



范雪寒's avatar
范雪寒 committed
166
  onResize(event) {
limingzhe's avatar
limingzhe committed
167
    // this.winResizeEventStream.next();
范雪寒's avatar
范雪寒 committed
168 169 170 171 172
  }


  ngOnInit() {

liujiaxin's avatar
liujiaxin committed
173

范雪寒's avatar
范雪寒 committed
174 175
    this.initListener();

limingzhe's avatar
limingzhe committed
176
    this.init();
范雪寒's avatar
范雪寒 committed
177 178
    this.update();

liujiaxin's avatar
liujiaxin committed
179 180
    this.refresh();

范雪寒's avatar
范雪寒 committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  }

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


  ngOnChanges() {

  }


  onBackgroundUploadSuccess(e) {
    console.log('e: ', e);
    this.bgItem.url = e.url;
limingzhe's avatar
limingzhe committed
196 197 198

    this.init();
    // this.refreshBackground();
范雪寒's avatar
范雪寒 committed
199 200 201 202 203
  }

  onItemImgUploadSuccess(e, item) {
    item.pic_url = e.url;
    this.loadHotZonePic(item.pic, e.url);
liujiaxin's avatar
liujiaxin committed
204
    this.refresh();
范雪寒's avatar
范雪寒 committed
205 206 207 208
  }

  onItemAudioUploadSuccess(e, item) {
    item.audio_url = e.url;
liujiaxin's avatar
liujiaxin committed
209
    this.refresh();
范雪寒's avatar
范雪寒 committed
210 211 212 213 214 215 216 217 218 219
  }


  refreshBackground(callBack = null) {

    if (!this.bg) {
      this.bg = new MySprite(this.ctx);
      this.renderArr.push(this.bg);
    }

liujiaxin's avatar
liujiaxin committed
220 221 222 223
    if (!this.bgItem.url) {
      this.bgItem.url = 'http://teach.cdn.ireadabc.com/8ebb1858564340ea0936b83e3280ad7d.jpg';
    }

范雪寒's avatar
范雪寒 committed
224
    const bg = this.bg;
liujiaxin's avatar
liujiaxin committed
225
    // if (this.bgItem.url) {
范雪寒's avatar
范雪寒 committed
226

liujiaxin's avatar
liujiaxin committed
227 228 229
    bg.load(this.bgItem.url).then(() => {
      const rate1 = this.canvasWidth / bg.width;
      const rate2 = this.canvasHeight / bg.height;
范雪寒's avatar
范雪寒 committed
230

liujiaxin's avatar
liujiaxin committed
231 232 233 234 235
      const rate = Math.min(rate1, rate2);
      bg.setScaleXY(rate);
      
      bg.x = this.canvasWidth / 2;
      bg.y = this.canvasHeight / 2;
范雪寒's avatar
范雪寒 committed
236

liujiaxin's avatar
liujiaxin committed
237
      bg.removeChildren();
范雪寒's avatar
范雪寒 committed
238

liujiaxin's avatar
liujiaxin committed
239 240 241 242 243 244 245 246 247
      const bgBorder = new ShapeRectNew(this.ctx);
      bgBorder.setSize(bg.width, bg.height, 0);
      bgBorder.fillColor = '#ff0000';
      bgBorder.fill = false;
      bgBorder.stroke = true;
      bgBorder.x = -bg.width / 2;
      bgBorder.y = -bg.height / 2;
      bgBorder.lineWidth = 0.5;
      bg.addChild(bgBorder);
范雪寒's avatar
范雪寒 committed
248

liujiaxin's avatar
liujiaxin committed
249 250 251 252 253 254 255 256 257

      if (callBack) {
        callBack();
      }

      this.refresh();

    });
    // }
范雪寒's avatar
范雪寒 committed
258 259 260 261

  }


liujiaxin's avatar
liujiaxin committed
262
  addBtnClick(data=null) {
范雪寒's avatar
范雪寒 committed
263 264 265
    // this.imgArr.push({});
    // this.hotZoneArr.push({});

liujiaxin's avatar
liujiaxin committed
266
    const item = this.getHotZoneItem(data);
范雪寒's avatar
范雪寒 committed
267 268
    this.hotZoneArr.push(item);

liujiaxin's avatar
liujiaxin committed
269 270 271 272 273 274

    if (this.customTypeGroupArr) {
      this.refreshCustomItem(item);
    } else {
      this.refreshItem(item);
    }
范雪寒's avatar
范雪寒 committed
275 276 277 278 279 280 281 282 283 284

    this.refreshHotZoneId();

  }

  deleteBtnClick(index) {

    const item = this.hotZoneArr.splice(index, 1)[0];
    removeItemFromArr(this.renderArr, item.pic);
    removeItemFromArr(this.renderArr, item.textLabel);
liujiaxin's avatar
liujiaxin committed
285
    removeItemFromArr(this.renderArr, item.drag);
范雪寒's avatar
范雪寒 committed
286 287 288 289 290 291 292 293

    this.refreshHotZoneId();

  }

  onImgUploadSuccessByImg(e, img) {
    img.pic_url = e.url;
    this.refreshImage(img);
liujiaxin's avatar
liujiaxin committed
294
    this.refresh();
范雪寒's avatar
范雪寒 committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  }

  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;

      if (this.hotZoneArr[i]) {
        this.hotZoneArr[i].title = 'item-' + (i + 1);

      }
    }
liujiaxin's avatar
liujiaxin committed
317
    this.refresh();
范雪寒's avatar
范雪寒 committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  }


  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);
      }
    }

  }

  getHotZoneItem(saveData = null) {

    const itemW = 200;
    const itemH = 200;
    const item = new HotZoneItem(this.ctx);
    item.setSize(itemW, itemH);
    item.anchorX = 0.5;
    item.anchorY = 0.5;

liujiaxin's avatar
liujiaxin committed
341

范雪寒's avatar
范雪寒 committed
342 343 344
    item.x = this.canvasWidth / 2;
    item.y = this.canvasHeight / 2;

liujiaxin's avatar
liujiaxin committed
345 346 347 348 349 350 351 352
    item.itemType = this.getDefaultItemType();
    item.gIdx = '0';


    item['id'] = this.createItemId() // new Date().getTime().toString();
    item['data'] = saveData;

    console.log('item.x: ', item.x);
范雪寒's avatar
范雪寒 committed
353 354 355 356 357 358 359 360

    if (saveData) {

      const saveRect = saveData.rect;
      item.scaleX = saveRect.width / item.width;
      item.scaleY = saveRect.height / item.height;
      item.x = saveRect.x + saveRect.width / 2;
      item.y = saveRect.y + saveRect.height / 2;
liujiaxin's avatar
liujiaxin committed
361 362 363
      item.gIdx = saveData.gIdx;

      item['id'] = saveData.id;
limingzhe's avatar
limingzhe committed
364 365 366
      // item['skeJsonData'] = saveData.skeJsonData;
      // item['texJsonData'] = saveData.texJsonData;
      // item['texPngData'] = saveData.texPngData;
liujiaxin's avatar
liujiaxin committed
367 368 369 370 371 372 373 374 375 376


      item['actionData_' + item.gIdx] = saveData['actionData_' + item.gIdx];

      this.savePropertyArr.forEach((key) => {
        if (saveData[key]) {
          item[key] = saveData[key];
        }
      })

范雪寒's avatar
范雪寒 committed
377 378 379

    }

liujiaxin's avatar
liujiaxin committed
380 381
    console.log('item.x:~~ ', item.x);

范雪寒's avatar
范雪寒 committed
382 383
    item.showLineDash();

liujiaxin's avatar
liujiaxin committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    // const pic = new HotZoneImg(this.ctx);
  

    this.setItemPic(item, saveData);

    this.setItemLabel(item, saveData);

    this.setItemAnima(item, saveData);

    this.setItemDrag(item, saveData);


    return item;
  }

  setItemPic(item, saveData) {

    console.log('in setItemPic ', saveData);
    const pic = new EditorItem(this.ctx);
范雪寒's avatar
范雪寒 committed
403 404
    pic.visible = false;
    item['pic'] = pic;
liujiaxin's avatar
liujiaxin committed
405 406 407 408 409 410 411 412 413 414 415 416
    if (saveData) {

      let picUrl = saveData.pic_url;
      const actionData = saveData['actionData_' + item.gIdx];
      if (actionData && actionData.type == 'pic') {
        picUrl = actionData.pic_url;
      }

      console.log('saveData: ', saveData);
      console.log('picUrl: ', picUrl);

      if (picUrl) {
limingzhe's avatar
limingzhe committed
417
        this.loadHotZonePic(pic, picUrl, saveData);
liujiaxin's avatar
liujiaxin committed
418
      }
范雪寒's avatar
范雪寒 committed
419
    }
liujiaxin's avatar
liujiaxin committed
420

范雪寒's avatar
范雪寒 committed
421 422 423
    pic.x = item.x;
    pic.y = item.y;
    this.renderArr.push(pic);
liujiaxin's avatar
liujiaxin committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
  }


  setItemDrag(item, saveData) {

    console.log('in setItemDrag ', saveData);
    const dragItem = new DragItem(this.ctx);
    dragItem.init();
    dragItem.item = item;
    item['drag'] = dragItem;

    dragItem.visible = false;

    dragItem.x = item.x;
    dragItem.y = item.y;
    this.renderArr.push(dragItem);

    if (saveData) {
      if (saveData.dragDot) {
        dragItem.x = saveData.dragDot.x / saveData.mapScale * this.mapScale;
        dragItem.y = saveData.dragDot.y / saveData.mapScale * this.mapScale;
      }  
    }


    // console.log('item.itemType: ', item.itemType);
    // let w = item.width;
    // let h = item.height;
    // if (saveData) {
    //   switch (saveData.itemType) {
    //     case 'rect':
    //       w = saveData.rect.width;
    //       h = saveData.rect.height;
    //       break;
    //     case 'pic':
    //       w = saveData.imgSizeW * saveData.imgScale;
    //       h = saveData.imgSizeH * saveData.imgScale;;
    //       break;
    //     case 'text':
    //       w = saveData.rect.width;
    //       h = saveData.rect.height;
    //       break;
    //   }
    // }
    
    // dragItem.setSize(w, h);


   
  }

  setItemAnima(item, saveData) {
    console.log('in setItemAnima ', saveData);
    // const pic = new EditorItem(this.ctx);
    // pic.visible = false;
    // item['pic'] = pic;
    // if (saveData) {

    //   let picUrl = saveData.pic_url;
    //   const actionData = saveData['actionData_' + item.gIdx];
    //   if (actionData && actionData.type == 'pic') {
    //     picUrl = actionData.pic_url;
    //   }

    //   console.log('saveData: ', saveData);
    //   console.log('picUrl: ', picUrl);

    //   if (picUrl) {
    //     this.loadHotZonePic(pic, picUrl, saveData.imgScale);
    //   }
    // }

    // pic.x = item.x;
    // pic.y = item.y;
    // this.renderArr.push(pic);
  }


  setItemLabel(item, saveData) {
范雪寒's avatar
范雪寒 committed
503 504

    const textLabel = new HotZoneLabel(this.ctx);
liujiaxin's avatar
liujiaxin committed
505 506 507 508 509 510
    if (this.hotZoneFontObj) {
      textLabel.fontSize = this.hotZoneFontObj.size;
      textLabel.fontName = this.hotZoneFontObj.name;
      textLabel.fontColor = this.hotZoneFontObj.color;
    }

范雪寒's avatar
范雪寒 committed
511 512 513 514
    textLabel.textAlign = 'center';
    // textLabel.setOutline();
    item['textLabel'] = textLabel;
    textLabel.setScaleXY(this.mapScale);
liujiaxin's avatar
liujiaxin committed
515 516 517 518 519 520 521
    if (saveData) {

      if (saveData.text && this.hotZoneFontObj) {
        textLabel.text = saveData.text
      }

      this.setActionFont(textLabel, saveData['actionData_' + item.gIdx]);
范雪寒's avatar
范雪寒 committed
522
      textLabel.refreshSize();
liujiaxin's avatar
liujiaxin committed
523

范雪寒's avatar
范雪寒 committed
524 525 526 527
    }
    textLabel.x = item.x;
    textLabel.y = item.y;
    this.renderArr.push(textLabel);
liujiaxin's avatar
liujiaxin committed
528
  }
范雪寒's avatar
范雪寒 committed
529

liujiaxin's avatar
liujiaxin committed
530 531 532 533 534 535 536 537 538 539
  setActionFont(textLabel, actionData) {
    if (actionData && actionData.type == 'text') {

      textLabel.text = actionData.text;

      for (let i=0; i<actionData.changeOption.length; i++) {
        const op = actionData.changeOption[i];
        textLabel[op[0]] = op[1];
      }
    }
范雪寒's avatar
范雪寒 committed
540 541
  }

liujiaxin's avatar
liujiaxin committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  getDefaultItemType() {
    if (this.customTypeGroupArr) {
      const group = this.customTypeGroupArr[0];
      if (group.rect) {
        return 'rect';
      }
      if (group.pic) {
        return 'pic';
      }
      if (group.text) {
        return 'text';
      }
    } else {
      return this.defaultItemType;
    }
  }
范雪寒's avatar
范雪寒 committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

  getPicItem(img, saveData = null) {


    const item = new EditorItem(this.ctx);
    item.load(img.pic_url).then(img => {

      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);
        item.x = saveRect.x + saveRect.width / 2;
        item.y = saveRect.y + saveRect.height / 2;

      } else {
liujiaxin's avatar
liujiaxin committed
593
        // item.showLineDash();
范雪寒's avatar
范雪寒 committed
594 595
      }

liujiaxin's avatar
liujiaxin committed
596 597
      item.showLineDash();
      console.log('aaa');
范雪寒's avatar
范雪寒 committed
598 599 600 601 602 603 604
    });
    return item;
  }


  onAudioUploadSuccessByImg(e, img) {
    img.audio_url = e.url;
liujiaxin's avatar
liujiaxin committed
605
    this.refresh();
范雪寒's avatar
范雪寒 committed
606 607 608 609 610 611 612 613
  }

  deleteItem(e, i) {
    // this.imgArr.splice(i , 1);
    // this.refreshImageId();

    this.hotZoneArr.splice(i, 1);
    this.refreshHotZoneId();
liujiaxin's avatar
liujiaxin committed
614
    this.refresh();
范雪寒's avatar
范雪寒 committed
615
  }
liujiaxin's avatar
liujiaxin committed
616 617
  

范雪寒's avatar
范雪寒 committed
618

liujiaxin's avatar
liujiaxin committed
619 620
  // radioChange(e, item) {
  //   item.itemType = e;
范雪寒's avatar
范雪寒 committed
621

liujiaxin's avatar
liujiaxin committed
622 623 624 625
  //   this.refreshItem(item);
  //   this.refresh();
  //   // console.log(' in radioChange e: ', e);
  // }
范雪寒's avatar
范雪寒 committed
626

liujiaxin's avatar
liujiaxin committed
627 628 629 630 631 632 633 634 635 636 637
  customRadioChange(e, item) {

    console.log('in customRadioChange', e);
    item.gIdx = -1;
    setTimeout(() => {
      item.gIdx = e;
      this.refreshCustomItem(item);
      this.refresh();
    }, 1);
    
    // const curGroup = this.customTypeGroupArr[e];
范雪寒's avatar
范雪寒 committed
638

liujiaxin's avatar
liujiaxin committed
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 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  }

  refreshCustomItem(item) {
    this.hideHotZoneItem(item);
    const group = this.customTypeGroupArr[item.gIdx];
    if (!group) {
      return;
    }

    if (group.text) {
      this.showItemLabel(item);
    }

    if (group.rect) {
      this.showItemRect(item, !group.isFixed);
    }

    if (group.pic && !group.anima) {
     this.showItemPic(item);
    }

    if (group.action) {
      if (group.action.type == 'pic') {
        this.showItemPic(item);
      }
      if (group.action.type == 'text') {
        this.showItemLabel(item);
      }
      if (group.action.type == 'anima') {
        this.showItemRect(item);
      }
    }

    item.drag.visible = group.drag;

    item.setAnimaStyle(group.animaSmall)
  

  }

  showItemDrag(item) {
    item.drag.visible = true;
    // item.dragBody.visible = true;
    // item.itemType = 'pic';
    // this.showArrowTop(item)
  }


  showItemPic(item) {
    item.pic.visible = true;
    item.itemType = 'pic';
    this.showArrowTop(item)
  }

  showItemLabel(item) {
    item.textLabel.visible = true;
    item.itemType = 'text';
    this.showArrowTop(item)
  }

  showItemRect(item, isShowArrowTop = true) {
    item.visible = true;
    item.itemType = 'rect';
    this.showArrowTop(item, isShowArrowTop)
  }

  showArrowTop(item, isShowArrowTop = false) {
    if (isShowArrowTop) {
      item.arrowTop.visible = true;
      item.arrowRight.visible = true;
    } else {
      item.arrowTop.visible = false;
      item.arrowRight.visible = false;
    }
  }

  hideHotZoneItem(item) {
    item.visible = false;
    item.pic.visible = false;
    item.textLabel.visible = false;
范雪寒's avatar
范雪寒 committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732
  }

  refreshItem(item) {
    switch (item.itemType) {
      case 'rect':
        this.setRectState(item);
        break;
      case 'pic':
        this.setPicState(item);
        break;
      case 'text':
        this.setTextState(item);
        break;
      default:
liujiaxin's avatar
liujiaxin committed
733
        this.setNoneState(item);
范雪寒's avatar
范雪寒 committed
734 735 736 737 738 739
    }
  }


  init() {

liujiaxin's avatar
liujiaxin committed
740
    console.log('init');
limingzhe's avatar
limingzhe committed
741 742 743 744 745
    // if (this.isInit) {
    //   return;
    // }
    // this.isInit = true;

范雪寒's avatar
范雪寒 committed
746 747 748 749 750 751
    this.initData();
    this.initCtx();
    this.initItem();
  }

  initItem() {
liujiaxin's avatar
liujiaxin committed
752 753 754 755

    this.changeDetectorRef.markForCheck();
    this.changeDetectorRef.detectChanges();

范雪寒's avatar
范雪寒 committed
756 757 758 759
    if (!this.bgItem) {
      this.bgItem = {};
    } else {
      this.refreshBackground(() => {
liujiaxin's avatar
liujiaxin committed
760 761
      
      
范雪寒's avatar
范雪寒 committed
762 763 764 765 766 767 768 769 770
        if (!this.hotZoneItemArr) {
          this.hotZoneItemArr = [];
        } else {
          this.initHotZoneArr();
        }

      });
    }

liujiaxin's avatar
liujiaxin committed
771
    this.refresh();
范雪寒's avatar
范雪寒 committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  }

  initHotZoneArr() {

    // console.log('this.hotZoneArr: ', this.hotZoneArr);
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
      curBgRect = {x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight};
    }

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

    const rate = curBgRect.width / oldBgRect.width;

    console.log('rate: ', rate);

    this.hotZoneArr = [];
    const arr = this.hotZoneItemArr.concat();
liujiaxin's avatar
liujiaxin committed
795
    console.log('this.hotZoneItemArr: ', this.hotZoneItemArr);
范雪寒's avatar
范雪寒 committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    for (let i = 0; i < arr.length; i++) {

      const data = JSON.parse(JSON.stringify(arr[i]));
      // const img = {pic_url: data.pic_url};

      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);

      const item = this.getHotZoneItem(data);
      item.audio_url = data.audio_url;
      item.pic_url = data.pic_url;
      item.text = data.text;
      item.itemType = data.itemType;

liujiaxin's avatar
liujiaxin committed
819 820 821 822 823 824 825 826
      if (this.customTypeGroupArr) {
        this.refreshCustomItem(item);
      } else {
        this.refreshItem(item);
      }


      console.log('1 item: ', item);
范雪寒's avatar
范雪寒 committed
827 828 829 830 831 832 833 834 835 836 837 838 839
      this.hotZoneArr.push(item);

    }

    this.refreshHotZoneId();

    // this.refreshImageId();

  }


  initData() {

limingzhe's avatar
limingzhe committed
840 841
    console.log(' in initData')

范雪寒's avatar
范雪寒 committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    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;
  }


  mapDown(event) {

    this.oldPos = {x: this.mx, y: this.my};

liujiaxin's avatar
liujiaxin committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    
    // 先检测拖拽点
    for (let i = this.hotZoneArr.length - 1; i >= 0; i--) {
      
      const item = this.hotZoneArr[i];

      if (item && item.drag && item.drag.visible) {

        if (this.checkClickTarget(item.drag)) {

          this.clickedDragPoint(item.drag);
          return;
        }
      }
    }
范雪寒's avatar
范雪寒 committed
878

liujiaxin's avatar
liujiaxin committed
879 880 881

    for (let i = this.hotZoneArr.length - 1; i >= 0; i--) {
      
范雪寒's avatar
范雪寒 committed
882
      const item = this.hotZoneArr[i];
liujiaxin's avatar
liujiaxin committed
883 884

      console.log('mapDown item: ', item);
范雪寒's avatar
范雪寒 committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
      let callback;
      let target;
      switch (item.itemType) {
        case 'rect':
          target = item;
          callback = this.clickedHotZoneRect.bind(this);
          break;
        case 'pic':
          target = item.pic;
          callback = this.clickedHotZonePic.bind(this);
          break;
        case 'text':
          target = item.textLabel;
          callback = this.clickedHotZoneText.bind(this);
          break;
      }

liujiaxin's avatar
liujiaxin committed
902
      if (target && this.checkClickTarget(target)) {
范雪寒's avatar
范雪寒 committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
        callback(target);
        return;
      }
    }


  }


  mapMove(event) {
    if (!this.curItem) {
      return;
    }

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

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

    } else if (this.changeRightSizeFlag) {
      this.changeRightSize();

    } else {

      const addX = this.mx - this.oldPos.x;
      const addY = this.my - this.oldPos.y;
      this.curItem.x += addX;
      this.curItem.y += addY;
liujiaxin's avatar
liujiaxin committed
932 933 934

      this.curItem.posX = this.curItem.x;
      this.curItem.posY = this.curItem.y;
范雪寒's avatar
范雪寒 committed
935 936 937 938
    }

    this.oldPos = {x: this.mx, y: this.my};

liujiaxin's avatar
liujiaxin committed
939
    // this.saveDisabled = false;
范雪寒's avatar
范雪寒 committed
940 941 942

  }

liujiaxin's avatar
liujiaxin committed
943
  mapUp(event=null) {
范雪寒's avatar
范雪寒 committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    this.curItem = null;
    this.changeSizeFlag = false;
    this.changeTopSizeFlag = false;
    this.changeRightSizeFlag = false;
  }


  changeSize() {
    const rect = this.curItem.getBoundingBox();

    let lenW = (this.mx - (rect.x + rect.width / 2)) * 2;
    let lenH = ((rect.y + rect.height / 2) - this.my) * 2;


    let minLen = 20;
    let s;
    if (lenW < lenH) {
      if (lenW < minLen) {
        lenW = minLen;
      }
      s = lenW / this.curItem.width;

    } else {
      if (lenH < minLen) {
        lenH = minLen;
      }
      s = lenH / this.curItem.height;
    }


    // console.log('s: ', s);
    this.curItem.setScaleXY(s);
    this.curItem.refreshLabelScale();
  }


  changeTopSize() {
    const rect = this.curItem.getBoundingBox();

    // let lenW = ( this.mx - (rect.x + rect.width / 2) ) * 2;
    let lenH = ((rect.y + rect.height / 2) - this.my) * 2;


    let minLen = 20;
    let s;
    // if (lenW < lenH) {
    //   if (lenW < minLen) {
    //     lenW = minLen;
    //   }
    //   s = lenW / this.curItem.width;
    //
    // } else {
    if (lenH < minLen) {
      lenH = minLen;
    }
    s = lenH / this.curItem.height;
    // }


    // console.log('s: ', s);
    this.curItem.scaleY = s;
    this.curItem.refreshLabelScale();
  }

  changeRightSize() {
    const rect = this.curItem.getBoundingBox();

    let lenW = (this.mx - (rect.x + rect.width / 2)) * 2;
    // let lenH = ( (rect.y + rect.height / 2) - this.my ) * 2;

    let minLen = 20;
    let s;
    if (lenW < minLen) {
      lenW = minLen;
    }
    s = lenW / this.curItem.width;

    this.curItem.scaleX = s;
    this.curItem.refreshLabelScale();
  }

  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) {

liujiaxin's avatar
liujiaxin committed
1042 1043
    console.log(' in changeCurItem', item)

范雪寒's avatar
范雪寒 committed
1044 1045 1046 1047
    this.hideAllLineDash();

    this.curItem = item;
    this.curItem.showLineDash();
liujiaxin's avatar
liujiaxin committed
1048

范雪寒's avatar
范雪寒 committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  }

  hideAllLineDash() {

    for (let i = 0; i < this.imgArr.length; i++) {
      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.hideLineDash();
      }
    }
  }


  update() {
    if (!this.ctx) {
      return;
    }


    this.animationId = window.requestAnimationFrame(this.update.bind(this));
    // 清除画布内容
    this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);

liujiaxin's avatar
liujiaxin committed
1071
  
范雪寒's avatar
范雪寒 committed
1072 1073 1074 1075 1076 1077 1078 1079

    // for (let i = 0; i < this.imgArr.length; i++) {
    //   const picItem = this.imgArr[i].picItem;
    //   if (picItem) {
    //     picItem.update(this);
    //   }
    // }

liujiaxin's avatar
liujiaxin committed
1080 1081 1082 1083
    for (let i = 0; i < this.renderArr.length; i++) {
      this.renderArr[i].update(this);
    }

范雪寒's avatar
范雪寒 committed
1084
    this.updateArr(this.hotZoneArr);
liujiaxin's avatar
liujiaxin committed
1085 1086 1087 1088 1089

   

    this.updatePos();

范雪寒's avatar
范雪寒 committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111


    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() {

limingzhe's avatar
limingzhe committed
1112 1113 1114 1115 1116
    // this.winResizeEventStream
    //   .pipe(debounceTime(500))
    //   .subscribe(data => {
    //     this.renderAfterResize();
    //   });
范雪寒's avatar
范雪寒 committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

    if (this.IsPC()) {

      this.canvas.nativeElement.addEventListener('mousedown', (event) => {
        setMxMyByMouse(event);
        this.mapDown(event);
      });

      this.canvas.nativeElement.addEventListener('mousemove', (event) => {
        setMxMyByMouse(event);
        this.mapMove(event);
      });

      this.canvas.nativeElement.addEventListener('mouseup', (event) => {
        setMxMyByMouse(event);
        this.mapUp(event);
      });

      const setMxMyByMouse = (event) => {
        this.mx = event.offsetX;
        this.my = event.offsetY;
      };

    } else {

      this.canvas.nativeElement.addEventListener('touchstart', (event) => {
        setMxMyByTouch(event);
        this.mapDown(event);
      });

      this.canvas.nativeElement.addEventListener('touchmove', (event) => {
        setMxMyByTouch(event);
        this.mapMove(event);
      });

      this.canvas.nativeElement.addEventListener('touchend', (event) => {
        setMxMyByTouch(event);
        this.mapUp(event);
      });

      this.canvas.nativeElement.addEventListener('touchcancel', (event) => {
        setMxMyByTouch(event);
        this.mapUp(event);
      });


      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;
      };


      const setParentOffset = () => {

        const rect = this.canvas.nativeElement.getBoundingClientRect();
        this.canvasLeft = rect.left;
        this.canvasTop = rect.top;
      };
    }
  }

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


  checkClickTarget(target) {
liujiaxin's avatar
liujiaxin committed
1199 1200 1201
    if (!target || !target.visible) {
      return;
    }
范雪寒's avatar
范雪寒 committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    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;
  }


  saveClick() {
liujiaxin's avatar
liujiaxin committed
1220 1221 1222 1223 1224 1225 1226

    const sendData = this.getSendData();
    this.save.emit(sendData);
  }

  getSendData() {

范雪寒's avatar
范雪寒 committed
1227 1228
    const bgItem = this.bgItem;
    if (this.bg) {
liujiaxin's avatar
liujiaxin committed
1229 1230 1231 1232 1233 1234
      const rect = this.bg.getBoundingBox();
      bgItem['rect'] = rect;
      rect.x = Math.round(rect.x * 100) / 100;
      rect.y = Math.round(rect.y * 100) / 100;
      rect.width = Math.round(rect.width * 100) / 100;
      rect.height = Math.round(rect.height * 100) / 100;
范雪寒's avatar
范雪寒 committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    } else {
      bgItem['rect'] = {
        x: 0,
        y: 0,
        width: Math.round(this.canvasWidth * 100) / 100,
        height: Math.round(this.canvasHeight * 100) / 100
      };
    }


    const hotZoneItemArr = [];
    const hotZoneArr = this.hotZoneArr;
    for (let i = 0; i < hotZoneArr.length; i++) {

      const hotZoneItem = {
liujiaxin's avatar
liujiaxin committed
1250
        id: hotZoneArr[i].id,
范雪寒's avatar
范雪寒 committed
1251 1252 1253 1254 1255 1256 1257
        index: hotZoneArr[i].index,
        pic_url: hotZoneArr[i].pic_url,
        text: hotZoneArr[i].text,
        audio_url: hotZoneArr[i].audio_url,
        itemType: hotZoneArr[i].itemType,
        fontScale: hotZoneArr[i].textLabel ? hotZoneArr[i].textLabel.scaleX : 1,
        imgScale: hotZoneArr[i].pic ? hotZoneArr[i].pic.scaleX : 1,
liujiaxin's avatar
liujiaxin committed
1258 1259 1260
        imgSizeW: hotZoneArr[i].pic ? hotZoneArr[i].pic.width : 0,
        imgSizeH: hotZoneArr[i].pic ? hotZoneArr[i].pic.height : 0,
        mapScale: this.mapScale,
limingzhe's avatar
limingzhe committed
1261 1262 1263
        // skeJsonData: hotZoneArr[i].skeJsonData,
        // texJsonData: hotZoneArr[i].texJsonData,
        // texPngData: hotZoneArr[i].texPngData,
liujiaxin's avatar
liujiaxin committed
1264 1265
        dragDot: hotZoneArr[i].drag.getPosition(),
        gIdx: hotZoneArr[i].gIdx,
范雪寒's avatar
范雪寒 committed
1266
      };
liujiaxin's avatar
liujiaxin committed
1267

limingzhe's avatar
limingzhe committed
1268 1269 1270

      console.log("~~hotZoneArr[i]: ", hotZoneArr[i]);

liujiaxin's avatar
liujiaxin committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
      this.savePropertyArr.forEach((key) => {
        if (hotZoneArr[i][key]) {
          hotZoneItem[key] = hotZoneArr[i][key];
        }
      })

      hotZoneItem['actionData_' + hotZoneItem.gIdx] = hotZoneArr[i]['actionData_' + hotZoneArr[i].gIdx]


      if (this.hotZoneFontObj) {
        hotZoneItem['fontSize'] = this.hotZoneFontObj.size;
        hotZoneItem['fontName'] = this.hotZoneFontObj.name;
        hotZoneItem['ontColor'] = this.hotZoneFontObj.color;
      }


      if (hotZoneArr[i].itemType == 'pic') {
        hotZoneItem['rect'] = hotZoneArr[i].pic.getBoundingBox();
      } else if (hotZoneArr[i].itemType == 'text') {
        hotZoneArr[i].textLabel.refreshSize();
        hotZoneItem['rect'] = hotZoneArr[i].textLabel.getLabelRect();
        // hotZoneItem['rect'].width =  hotZoneItem['rect'].width / hotZoneArr[i].textLabel.scaleX;
        // hotZoneItem['rect'].height = hotZoneArr[i].textLabel.height * hotZoneArr[i].textLabel.scaleY;

      } else {
        hotZoneItem['rect'] = hotZoneArr[i].getBoundingBox();
      }
      // hotZoneItem['rect'] = hotZoneArr[i].getBoundingBox();
范雪寒's avatar
范雪寒 committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
      hotZoneItem['rect'].x = Math.round((hotZoneItem['rect'].x - bgItem['rect'].x) * 100) / 100;
      hotZoneItem['rect'].y = Math.round((hotZoneItem['rect'].y - bgItem['rect'].y) * 100) / 100;
      hotZoneItem['rect'].width = Math.round((hotZoneItem['rect'].width) * 100) / 100;
      hotZoneItem['rect'].height = Math.round((hotZoneItem['rect'].height) * 100) / 100;


      hotZoneItemArr.push(hotZoneItem);
    }

    console.log('hotZoneItemArr: ', hotZoneItemArr);

liujiaxin's avatar
liujiaxin committed
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    return {bgItem, hotZoneItemArr};
  }

  saveText(item) {
    if (item.itemType == 'text') {
      item.textLabel.text = item.text;
    }
  }

  saveItemPos(item) {
  
    console.log('item.posX: ', item.posX)
    console.log('item.posY: ', item.posY)

      item.x = Number(item.posX || 0)
      item.y = Number(item.posY || 0)
  

 
limingzhe's avatar
limingzhe committed
1329
    this.changeCurItem(item);
liujiaxin's avatar
liujiaxin committed
1330 1331 1332



limingzhe's avatar
limingzhe committed
1333 1334
    this.curItem.x = item.posX || 0;
    this.curItem.y = item.posY || 0;
liujiaxin's avatar
liujiaxin committed
1335

limingzhe's avatar
limingzhe committed
1336
    this.mapUp();
liujiaxin's avatar
liujiaxin committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370


  }

  onSaveCustomAction(e, item) {
  
    const data = e;
    item['actionData_' + item.gIdx] = data;

    if (data.type == 'pic') {
      let picUrl = data.pic_url;
      if (picUrl) {
        this.loadHotZonePic(item.pic, picUrl);
      }
    }
   
    if (data.type == 'text') {
      this.setActionFont(item.textLabel, data);
      item.textLabel.refreshSize();
    }
   
    // if (data.type == 'anima') {
    //   this.setActionAnima(item.anima, data);
    // }
    
    // this.loadHotZonePic(item.pic, e.url);
    this.refresh()
  }
 
  setActionAnima() {

  }
  

limingzhe's avatar
limingzhe committed
1371
  // setAnimaBtnClick(index) {
liujiaxin's avatar
liujiaxin committed
1372

limingzhe's avatar
limingzhe committed
1373 1374
  //   console.log('aaaa')
  //   this.isAnimaSmall = false;
liujiaxin's avatar
liujiaxin committed
1375

limingzhe's avatar
limingzhe committed
1376
  //   this.setCurDragonBone(index);
liujiaxin's avatar
liujiaxin committed
1377

limingzhe's avatar
limingzhe committed
1378 1379
  //   // this.curDragonBoneIndex = index;
  //   // this.curDragonBoneGIdx = Number(this.hotZoneArr[index].gIdx);
liujiaxin's avatar
liujiaxin committed
1380

limingzhe's avatar
limingzhe committed
1381
  //   // const {skeJsonData, texJsonData, texPngData} = this.hotZoneArr[index];
liujiaxin's avatar
liujiaxin committed
1382

limingzhe's avatar
limingzhe committed
1383 1384 1385
  //   // this.skeJsonData = skeJsonData || {};
  //   // this.texJsonData = texJsonData || {};
  //   // this.texPngData = texPngData || {};
liujiaxin's avatar
liujiaxin committed
1386

limingzhe's avatar
limingzhe committed
1387
  //   // this.animaPanelVisible = true;
liujiaxin's avatar
liujiaxin committed
1388

limingzhe's avatar
limingzhe committed
1389 1390
  //   // this.refresh();
  // }
liujiaxin's avatar
liujiaxin committed
1391 1392

  
limingzhe's avatar
limingzhe committed
1393
  // setAnimaSmallBtnClick(index) {
liujiaxin's avatar
liujiaxin committed
1394 1395


limingzhe's avatar
limingzhe committed
1396 1397
  //   console.log('bbb')
  //   this.isAnimaSmall = true;
liujiaxin's avatar
liujiaxin committed
1398

limingzhe's avatar
limingzhe committed
1399
  //   this.setCurDragonBone(index);
liujiaxin's avatar
liujiaxin committed
1400

limingzhe's avatar
limingzhe committed
1401
  // }
liujiaxin's avatar
liujiaxin committed
1402

limingzhe's avatar
limingzhe committed
1403 1404 1405
  // setCurDragonBone(index) {
  //   this.curDragonBoneIndex = index;
  //   this.curDragonBoneGIdx = Number(this.hotZoneArr[index].gIdx);
liujiaxin's avatar
liujiaxin committed
1406

limingzhe's avatar
limingzhe committed
1407
  //   const {skeJsonData, texJsonData, texPngData} = this.hotZoneArr[index];
liujiaxin's avatar
liujiaxin committed
1408

limingzhe's avatar
limingzhe committed
1409 1410 1411
  //   this.skeJsonData = skeJsonData || {};
  //   this.texJsonData = texJsonData || {};
  //   this.texPngData = texPngData || {};
liujiaxin's avatar
liujiaxin committed
1412

limingzhe's avatar
limingzhe committed
1413
  //   this.animaPanelVisible = true;
liujiaxin's avatar
liujiaxin committed
1414

limingzhe's avatar
limingzhe committed
1415 1416
  //   this.refresh();
  // }
liujiaxin's avatar
liujiaxin committed
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441

  setItemSizeByAnima(jsonData, item) {
    console.log('json: ', jsonData);

    const request = new XMLHttpRequest();
    //通过url获取文件,第二个选项是文件所在的具体地址
    request.open('GET', jsonData.url, true);
    request.send(null);
    request.onreadystatechange = ()=> {
    if (request.readyState === 4 && request.status === 200) {
        var type = request.getResponseHeader('Content-Type');
              if (type.indexOf("text") !== 1) {
                  //返回一个文件内容的对象

                  const data = JSON.parse(request.responseText);
                  console.log('request.responseText;', data)
                  const animaSize = data.armature[0].canvas;
                  item.width = animaSize.width;
                  item.height = animaSize.height;
                  // return request.responseText;
              }
          }
    }
  }

limingzhe's avatar
limingzhe committed
1442 1443 1444 1445
  // animaPanelCancel() {
  //   this.animaPanelVisible = false;
  //   this.refresh();
  // }
liujiaxin's avatar
liujiaxin committed
1446

limingzhe's avatar
limingzhe committed
1447
  // animaPanelOk() {
liujiaxin's avatar
liujiaxin committed
1448

limingzhe's avatar
limingzhe committed
1449 1450 1451
  //   this.setItemDragonBoneData(this.hotZoneArr[this.curDragonBoneIndex]);
  //   console.log('this.hotZoneArr: ', this.hotZoneArr);
  //   this.animaPanelVisible = false;
liujiaxin's avatar
liujiaxin committed
1452

limingzhe's avatar
limingzhe committed
1453 1454 1455
  //   if (this.isAnimaSmall) {
  //     this.setItemSizeByAnima(this.skeJsonData, this.hotZoneArr[this.curDragonBoneIndex]);
  //   }
liujiaxin's avatar
liujiaxin committed
1456

limingzhe's avatar
limingzhe committed
1457 1458
  //   this.refresh();
  // }
liujiaxin's avatar
liujiaxin committed
1459

limingzhe's avatar
limingzhe committed
1460 1461 1462 1463 1464
  // setItemDragonBoneData(item) {
  //   item['skeJsonData'] = this.skeJsonData;
  //   item['texJsonData'] = this.texJsonData;
  //   item['texPngData'] = this.texPngData;
  // }
liujiaxin's avatar
liujiaxin committed
1465

limingzhe's avatar
limingzhe committed
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  // skeJsonHandleChange(e) {
  //   console.log('e: ', e);
  //   switch (e.type) {
  //     case 'start':
  //       this.isSkeJsonLoading = true;
  //       break;

  //     case 'success':
  //       this.skeJsonData['url'] = e.file.response.url;
  //       this.skeJsonData['name'] = e.file.name;
  //       this.nzMessageService.success('上传成功');
  //       this.isSkeJsonLoading = false;
  //       break;

  //     case 'progress':
  //       break;
  //   }
  // }
liujiaxin's avatar
liujiaxin committed
1484

limingzhe's avatar
limingzhe committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
  // texJsonHandleChange(e) {
  //   console.log('e: ', e);
  //   switch (e.type) {
  //     case 'start':
  //       this.isTexJsonLoading = true;
  //       break;

  //     case 'success':
  //       this.texJsonData['url'] = e.file.response.url;
  //       this.texJsonData['name'] = e.file.name;
  //       this.nzMessageService.success('上传成功');
  //       this.isTexJsonLoading = false;
  //       break;

  //     case 'progress':
  //       break;
  //   }
  // }
liujiaxin's avatar
liujiaxin committed
1503

limingzhe's avatar
limingzhe committed
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
  // texPngHandleChange(e) {
  //   console.log('e: ', e);
  //   switch (e.type) {
  //     case 'start':
  //       this.isTexPngLoading = true;
  //       break;

  //     case 'success':
  //       this.texPngData['url'] = e.file.response.url;
  //       this.texPngData['name'] = e.file.name;
  //       this.nzMessageService.success('上传成功');
  //       this.isTexPngLoading = false;
  //       break;

  //     case 'progress':
  //       break;
  //   }
  // }
liujiaxin's avatar
liujiaxin committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663

  copyItem(it) {

    const {hotZoneItemArr} = this.getSendData();
    let itemData;
    hotZoneItemArr.forEach((data) => {
      if (data.id == it.id) {
        itemData = data;
      }
    })


    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
      curBgRect = {x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight};
    }

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

    const rate = curBgRect.width / oldBgRect.width;

    console.log('rate: ', rate);


    const data = itemData
  
    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;


    const item = this.getHotZoneItem(data);
    item.audio_url = data.audio_url;
    item.pic_url = data.pic_url;
    item.text = data.text;
    item.itemType = data.itemType;

    this.hotZoneArr.push(item);

    if (this.customTypeGroupArr) {
      this.refreshCustomItem(item);
    } else {
      this.refreshItem(item);
    }

    this.refreshHotZoneId();

    item['id'] = this.createItemId();

  }

  createItemId() {
    return new Date().getTime().toString();
  }

  copyHotZoneData() {

    const {bgItem, hotZoneItemArr} = this.getSendData();

    // const hotZoneItemArrNew = [];
    // const tmpArr = JSON.parse(JSON.stringify(hotZoneItemArr));
    // tmpArr.forEach((item) => {
    //   if (item.gIdx === '0') {
    //     hotZoneItemArr.push(item);
    //   }
    // })

    const jsonData = {
      bgItem,
      hotZoneItemArr,
      isHasRect: this.isHasRect,
      isHasPic: this.isHasPic,
      isHasText: this.isHasText,
      isHasAudio: this.isHasAudio,
      isHasAnima: this.isHasAnima,
      hotZoneFontObj: this.hotZoneFontObj,
      defaultItemType: this.defaultItemType,
      hotZoneImgSize: this.hotZoneImgSize,
    };


    const oInput = document.createElement('input');
    oInput.value = JSON.stringify(jsonData);
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象
    document.execCommand('Copy'); // 执行浏览器复制命令

    document.body.removeChild(oInput);
    this.nzMessageService.success('复制成功');
    // alert('复制成功');


  }

  importData() {
    if (!this.pasteData) {
      return;
    }


    try {
      const data = JSON.parse(this.pasteData);
      console.log('data:', data);
      const {
        bgItem,
        hotZoneItemArr,
        isHasRect,
        isHasPic,
        isHasText,
        isHasAudio,
        isHasAnima,
        hotZoneFontObj,
        defaultItemType,
        hotZoneImgSize,
      } = data;

      this.hotZoneItemArr = hotZoneItemArr;
      this.isHasRect = isHasRect;
      this.isHasPic = isHasPic;
      this.isHasText = isHasText;
      this.isHasAudio = isHasAudio;
      this.isHasAnima = isHasAnima;
      this.hotZoneFontObj = hotZoneFontObj;
      this.defaultItemType = defaultItemType;
      this.hotZoneImgSize = hotZoneImgSize;

      this.bgItem = bgItem;

      this.pasteData = '';
    } catch (e) {
      console.log('err: ', e);
      this.nzMessageService.error('导入失败');
    }
范雪寒's avatar
范雪寒 committed
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
  }

  private updatePos() {
    this.hotZoneArr.forEach((item) => {
      let x, y;
      switch (item.itemType) {
        case 'rect':
          x = item.x;
          y = item.y;
          break;

        case 'pic':
          x = item.pic.x;
          y = item.pic.y;
          break;

        case 'text':
          x = item.textLabel.x;
          y = item.textLabel.y;
          break;
      }

liujiaxin's avatar
liujiaxin committed
1686 1687 1688 1689 1690 1691 1692 1693 1694
      if (x != undefined && y != undefined) {
        item.x = x;
        item.y = y;
        item.pic.x = x;
        item.pic.y = y;
        item.textLabel.x = x;
        item.textLabel.y = y;
      }

范雪寒's avatar
范雪寒 committed
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
    });
  }

  private setPicState(item: any) {
    item.visible = false;
    item.textLabel.visible = false;
    item.pic.visible = true;
  }

  private setRectState(item: any) {
    item.visible = true;
    item.textLabel.visible = false;
    item.pic.visible = false;
  }

  private setTextState(item: any) {
    item.visible = false;
    item.pic.visible = false;
    item.textLabel.visible = true;
  }

liujiaxin's avatar
liujiaxin committed
1716 1717 1718 1719 1720 1721
  private setNoneState(item: any) {
    item.visible = false;
    item.pic.visible = false;
    item.textLabel.visible = false;
  }

范雪寒's avatar
范雪寒 committed
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
  private clickedHotZoneRect(item: any) {
    if (this.checkClickTarget(item)) {

      if (item.lineDashFlag && this.checkClickTarget(item.arrow)) {
        this.changeItemSize(item);
      } else if (item.lineDashFlag && this.checkClickTarget(item.arrowTop)) {
        this.changeItemTopSize(item);
      } else if (item.lineDashFlag && this.checkClickTarget(item.arrowRight)) {
        this.changeItemRightSize(item);
      } else {
        this.changeCurItem(item);
      }
      return;
    }
  }

  private clickedHotZonePic(item: any) {
    if (this.checkClickTarget(item)) {
liujiaxin's avatar
liujiaxin committed
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

      if (item.lineDashFlag && this.checkClickTarget(item.arrow)) {
        this.changeItemSize(item);
        // } else if (item.lineDashFlag && this.checkClickTarget(item.arrowTop)) {
        //   this.changeItemTopSize(item);
        // } else if (item.lineDashFlag && this.checkClickTarget(item.arrowRight)) {
        //   this.changeItemRightSize(item);
      } else {
        this.changeCurItem(item);
      }

范雪寒's avatar
范雪寒 committed
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
      this.curItem = item;
    }
  }

  private clickedHotZoneText(item: any) {
    if (this.checkClickTarget(item)) {
      this.curItem = item;
    }
  }

liujiaxin's avatar
liujiaxin committed
1761 1762
  clickedDragPoint(item) {
    this.curItem = item;
范雪寒's avatar
范雪寒 committed
1763 1764
  }

limingzhe's avatar
limingzhe committed
1765
  private loadHotZonePic(pic: HotZoneImg, url, saveData = null) {
liujiaxin's avatar
liujiaxin committed
1766 1767 1768 1769 1770 1771

    let baseLen;
    if (this.hotZoneImgSize) {
      baseLen = this.hotZoneImgSize * this.mapScale;
    }

limingzhe's avatar
limingzhe committed
1772 1773
    
    let scale;
范雪寒's avatar
范雪寒 committed
1774
    pic.load(url).then(() => {
limingzhe's avatar
limingzhe committed
1775
      if (!saveData) {
liujiaxin's avatar
liujiaxin committed
1776 1777 1778 1779 1780
        if (baseLen) {
          scale = getMinScale(pic, baseLen);
        } else {
          scale = this.bg.scaleX;
        }
limingzhe's avatar
limingzhe committed
1781 1782 1783
      } else {
        scale = getMinScale(pic, saveData.rect.width);
      }
liujiaxin's avatar
liujiaxin committed
1784 1785

      pic.setScaleXY(scale);
范雪寒's avatar
范雪寒 committed
1786 1787
    });
  }
liujiaxin's avatar
liujiaxin committed
1788

limingzhe's avatar
limingzhe committed
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798



  saveAnim(e, item) {
    console.log(" in saveAnim e:", e);
    
    item.skeJsonData = e.skeJsonData;
    item.texJsonData = e.texJsonData;
    item.texPngData = e.texPngData;
    item.animType = e.animType;
liujiaxin's avatar
liujiaxin committed
1799
  }
limingzhe's avatar
limingzhe committed
1800 1801 1802 1803 1804

  
  // closeAfterPanel() {
  //   this.refresh();
  // }
liujiaxin's avatar
liujiaxin committed
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }


范雪寒's avatar
范雪寒 committed
1815
}