"use strict";
cc._RF.push(module, '2c0c3WgsglIe407Cu8vF6pN', 'NodeFitScreen');
// OP13/script/NodeFitScreen.js

"use strict";

var FitType = {
  Scale: 0,
  //等比缩放，一般用于背景图
  FullScreen: 1,
  //铺满全屏，拉伸
  FitMode: 2,
  //更改canvas适配策略，只能挂在canvas上
  FullHeight: 3,
  FullWidth: 4
};
var designSize = cc.size(1280, 720);
cc.Class({
  "extends": cc.Component,
  properties: {
    FitType: {
      type: cc.Enum(FitType),
      "default": 0
    }
  },
  onLoad: function onLoad() {
    cc.view.on('canvas-resize', this.refreshFit, this);
    this.refreshFit();
  },
  onDestroy: function onDestroy() {
    cc.view.off('canvas-resize', this.refreshFit, this);
  },
  refreshFit: function refreshFit() {
    if (this.FitType == FitType.Scale) {
      var winSize = cc.view.getVisibleSize();
      console.log(winSize);
      console.log(this.node.width, this.node.height);

      if (this.node.width >= winSize.width && this.node.height >= winSize.height) {
        return;
      }

      var scale1 = winSize.width / this.node.width;
      var scale2 = winSize.height / this.node.height;

      if (scale1 > scale2) {
        this.node.scale = scale1;
      } else {
        this.node.scale = scale2;
      }
    } else if (this.FitType == FitType.FullScreen) {
      var visiblesize = cc.view.getVisibleSize();
      this.node.width = visiblesize.width;
      this.node.height = visiblesize.height;
    } else if (this.FitType == FitType.FullHeight) {
      var _visiblesize = cc.view.getVisibleSize();

      var scale = _visiblesize.width / this.node.width;
      this.node.width = scale * this.node.width;
      this.node.height = scale * this.node.height;
    } else if (this.FitType == FitType.FullWidth) {
      var _visiblesize2 = cc.view.getVisibleSize();

      var _scale = _visiblesize2.height / this.node.height;

      this.node.width = _scale * this.node.width;
      this.node.height = _scale * this.node.height;
    } else {
      var wsize = cc.view.getFrameSize();
      var scaleW = wsize.width / designSize.width;
      var scaleH = wsize.height / designSize.height;
      var pCanvas = this.node.getComponent(cc.Canvas);

      if (pCanvas) {
        if (scaleW > scaleH) {
          //更宽了要fitHeight, 否则height就留黑边了;
          pCanvas.fitHeight = true;
          pCanvas.fitWidth = false;
        } else {
          //更高;
          pCanvas.fitHeight = false;
          pCanvas.fitWidth = true;
        }
      } else {
        console.error("fitmode模式只能用在canvas节点上");
      }
    }
  }
});

cc._RF.pop();