System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  "use strict";

  var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, Camera, RenderTexture, sys, UITransform, gfx, director, Texture2D, view, _cc, Util, _dec, _dec2, _class, _class2, _descriptor, _crd, cc, ccclass, property, scene2;

  function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }

  function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

  function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }

  function _reportPossibleCrUseOfUtil(extras) {
    _reporterNs.report("Util", "../Util", _context.meta, extras);
  }

  return {
    setters: [function (_unresolved_) {
      _reporterNs = _unresolved_;
    }, function (_cc2) {
      _cclegacy = _cc2.cclegacy;
      __checkObsolete__ = _cc2.__checkObsolete__;
      __checkObsoleteInNamespace__ = _cc2.__checkObsoleteInNamespace__;
      _decorator = _cc2._decorator;
      Component = _cc2.Component;
      Camera = _cc2.Camera;
      RenderTexture = _cc2.RenderTexture;
      sys = _cc2.sys;
      UITransform = _cc2.UITransform;
      gfx = _cc2.gfx;
      director = _cc2.director;
      Texture2D = _cc2.Texture2D;
      view = _cc2.view;
      _cc = _cc2;
    }, function (_unresolved_2) {
      Util = _unresolved_2.Util;
    }],
    execute: function () {
      _crd = true;

      _cclegacy._RF.push({}, "5a622pHY+lE4YKlmtKn3+Mo", "scene2", undefined);

      __checkObsolete__(['_decorator', 'Component', 'Node', 'Camera', 'RenderTexture', 'sys', 'UITransform', 'gfx', 'director', 'ImageAsset', 'Texture2D', 'SpriteFrame', 'Sprite', 'view', 'tween', 'Vec3', 'isValid']);

      cc = __checkObsoleteInNamespace__(_cc);
      ({
        ccclass,
        property
      } = _decorator);

      _export("scene2", scene2 = (_dec = ccclass('scene2'), _dec2 = property(Camera), _dec(_class = (_class2 = class scene2 extends Component {
        constructor() {
          super(...arguments);

          _initializerDefineProperty(this, "copyCamera", _descriptor, this);
        }

        onLoad() {
          //禁用动态合图
          cc.DynamicAtlasManager.instance.enabled = false;
          var node = cc.find("Canvas/node");
          var img = cc.find("Canvas/img");
          var img2 = cc.find("Canvas/img2");
          var button = cc.find("Canvas/Button");
          var uit = node.getComponent(cc.UITransform);
          var nodeWW = uit.width,
              nodeHH = uit.height;
          var rt;
          button.on(cc.Node.EventType.TOUCH_END, () => {
            var tKey = "draw";
            var dd = this.drawToRenderTexture2(node);
            rt = dd;
            var texture = new cc.Texture2D();
            console.time(tKey);
            var sp = img.getComponent(cc.Sprite);
            sp.spriteFrame = new cc.SpriteFrame();
            sp.spriteFrame.texture = dd;
            var uit = img.getComponent(cc.UITransform);
            uit.width = view.getVisibleSize().width;
            uit.height = view.getVisibleSize().height; // console.timeEnd(tKey)

            var buff = this.setOneImg(node, dd); // let imgA = new ImageAsset({
            //     _data:buff,
            //     _compressed: false,
            //     width: nodeWW,
            //     height: nodeHH,
            //     format: cc.Texture2D.PixelFormat.RGBA8888
            // })
            // let texture = new cc.Texture2D();
            // texture.setFilters(cc.Texture2D.Filter.NONE,cc.Texture2D.Filter.NONE)
            // texture.image = imgA;
            //包含 RGBA 四通道的 32 位整形像素格式：RGBA8888。 一字节8位

            texture.reset({
              width: nodeWW,
              height: nodeHH,
              format: Texture2D.PixelFormat.RGBA8888,
              mipmapLevel: 0
            });
            texture.uploadData(buff, 0, 0);
            texture.updateImage();
            console.timeEnd(tKey); // this.validateNow()

            var sp2 = img2.getComponent(cc.Sprite);
            sp2.spriteFrame = new cc.SpriteFrame();
            sp2.spriteFrame.texture = texture;
            var uit2 = img2.getComponent(cc.UITransform);
            uit2.width = nodeWW;
            uit2.height = nodeHH;
            setTimeout(() => {
              img.active = false;
              dd.destroy();
            }, 1000);
            window.ss = texture;
          }, this);
          var maskCon = cc.find("Canvas/maskCon2");
          maskCon.active = false;
          var maskbtn = cc.find("Canvas/maskbtn");
          maskbtn.on(cc.Node.EventType.TOUCH_END, () => {
            maskCon.active = true;
            var sp = maskCon.getComponent(cc.Sprite);
            sp.spriteFrame = new cc.SpriteFrame(); // sp.spriteFrame.texture = rt;

            sp.spriteFrame.texture = window.ss;
            var uit = maskCon.getComponent(cc.UITransform);
            uit.width = view.getVisibleSize().width;
            uit.height = view.getVisibleSize().height;
            uit.width = nodeWW;
            uit.height = nodeHH;
          }, this);
        }

        setOneImg(targetNode, renderTex, all) {
          var uit = targetNode.getComponent(UITransform);
          var width = uit.width,
              height = uit.height; // width = 720, height=1386;

          var worldPos = targetNode.getWorldPosition();
          var texBuffers = [];
          texBuffers[0] = new Uint8Array(width * height * 4);
          var region = new gfx.BufferTextureCopy();
          region.texOffset.x = Math.round(worldPos.x - width * uit.anchorPoint.x);
          region.texOffset.y = Math.round(worldPos.y - height * uit.anchorPoint.y); // region.texOffset.x = region.texOffset.y=0;

          region.texExtent.width = width;
          region.texExtent.height = height;
          director.root.device.copyTextureToBuffers(renderTex.getGFXTexture(), texBuffers, [region]); // renderTex._getGFXDevice().copyTextureToBuffers(renderTex.getGFXTexture(), texBuffers, [region]);

          var _buffer = texBuffers[0]; // this.saveAsImage(width, height, _buffer);

          return _buffer;
        }

        saveAsImage(width, height, arrayBuffer) {
          width = Math.round(width);
          height = Math.round(height);

          if (sys.isBrowser) {
            var _canvas = document.createElement('canvas');

            _canvas.width = width;
            _canvas.height = height;

            var ctx = _canvas.getContext('2d');

            var rowBytes = width * 4;

            for (var row = 0; row < height; row++) {
              var sRow = height - 1 - row;
              var imageData = ctx.createImageData(width, 1);
              var start = sRow * width * 4;

              for (var i = 0; i < rowBytes; i++) {
                imageData.data[i] = arrayBuffer[start + i];
              }

              ctx.putImageData(imageData, 0, row);
            } //这2个方法都可以


            this.eliFoTevas(_canvas, "", "aa.png", null, null);
          }
        }

        eliFoTevas(canvas, type, filePath, rect, encoderOptions) {
          // var base64 = toDataURL.call(this, type, rect, encoderOptions);
          var base64 = canvas.toDataURL("image/png");

          if (base64 == null) {
            return;
          }

          var href = base64.replace(/^data:image[^;]*/, "data:image/octet-stream");
          var aLink = document.createElement('a');
          aLink['download'] = filePath;
          aLink.href = href;
          var evt = document.createEvent('MouseEvents');
          evt.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
          aLink.dispatchEvent(evt);
        }

        update(deltaTime) {}

        changeLayer(node, layer) {
          node.children.map(item => {
            this.changeLayer(item, layer);
          });
          node.layer = layer;
        }

        drawToRenderTexture(node) {
          var nextID;

          var nextDraw = oldLayer => {
            if (nextID) clearTimeout(nextID);
            nextID = setTimeout(() => {
              this.copyCamera.targetTexture = null;
              this.changeLayer(node, oldLayer);
              nextID = null;
            }, 10000);
          };

          var oldLayer = node.layer;
          var drawLayer = cc.Layers.nameToLayer("drawNode") + 1;

          if (oldLayer != drawLayer) {
            this.changeLayer(node, drawLayer);
          }

          var uit = node.getComponent(cc.UITransform);
          var renderTex = new RenderTexture();
          renderTex.reset({
            width: view.getVisibleSize().width,
            height: view.getVisibleSize().height
          });
          this.copyCamera.targetTexture = renderTex;
          nextDraw(oldLayer);
          (_crd && Util === void 0 ? (_reportPossibleCrUseOfUtil({
            error: Error()
          }), Util) : Util).validateNow(); // this.copyCamera.targetTexture = null;
          // this.changeLayer(node, oldLayer);

          return renderTex;
        }

        drawToRenderTexture2(node) {
          var oldLayer = node.layer;
          var drawLayer = cc.Layers.nameToLayer("drawNode") + 1;

          if (oldLayer != drawLayer) {
            this.changeLayer(node, drawLayer);
          }

          var uit = node.getComponent(cc.UITransform);
          var renderTex = new RenderTexture();
          renderTex.reset({
            width: view.getVisibleSize().width,
            height: view.getVisibleSize().height
          });
          var node2 = new cc.Node('CaptureUtils');
          this.node.addChild(node2);
          var camera = node2.addComponent(cc.Camera);
          camera.projection = cc.Camera.ProjectionType.ORTHO;
          camera.near = 0;
          /* 默认1，必须改为0否则黑屏 */

          camera.visibility = cc.Layers.nameToLayer("drawNode") + 1;
          camera.orthoHeight = 640;
          camera.clearColor = camera.clearColor.fromHEX("00000000");
          camera.targetTexture = renderTex;
          (_crd && Util === void 0 ? (_reportPossibleCrUseOfUtil({
            error: Error()
          }), Util) : Util).validateNow();
          camera.targetTexture = null;
          node2.parent = null;
          this.changeLayer(node, oldLayer);
          return renderTex;
        }

      }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "copyCamera", [_dec2], {
        configurable: true,
        enumerable: true,
        writable: true,
        initializer: function initializer() {
          return null;
        }
      })), _class2)) || _class));

      _cclegacy._RF.pop();

      _crd = false;
    }
  };
});
//# sourceMappingURL=b469feb438213c1b8582a00eb5d2031f3f667d17.js.map