1/*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const { GLFrame } = require('./engine/GLFrame');
17const { Scr } = require('./engine/XDefine');
18const { XTools } = require('./engine/XTools');
19const { XSelect } = require('./engine/control/XSelect');
20const { X2DFast } = require('./engine/graphics/X2DFast');
21const { IrViewer } = require('./ir/IrViewer');
22const { LogParser } = require('./ir/LogParser');
23
24class MainEditor {
25  constructor() {
26    XTools.LoadConfig();
27
28    this.filePoint_ = '';
29    this.files_ = [];
30    this.viewer_ = {};
31    LogParser.Load('test.txt', this.onLoad.bind(this));
32
33    this.selectFile_ = new XSelect(this.files_, this.filePoint_);
34    this.selectFile_.registCallback(this.changeFile.bind(this));
35
36    GLFrame.gi().pCallbackDropfile = this.onDrop.bind(this);
37  }
38  changeFile(name) {
39    this.filePoint_ = name;
40  }
41  onLoad(fn, result) {
42    try {
43      let irv = new IrViewer(fn, result);
44      if (this.files_.indexOf(fn) < 0) {
45        this.files_.push(fn);
46        this.selectFile_.resetList(this.files_, fn);
47        this.changeFile(fn);
48      }
49      this.viewer_[fn] = irv;
50    }
51    catch (e) {
52      XTools.PROC_TO = 0;
53      console.log(e);
54      alert('读取' + fn + '失败');
55      return;
56    }
57  }
58  onDrop(files, x, y) {
59    if (files.length === 1) {
60      let reader = new FileReader();
61      reader.readAsDataURL(files[0]);
62      reader.onload = (e) => {
63        let ret = atob(e.target.result.split(',')[1]);
64        this.onLoad(files[0].name, ret);
65      };
66    }
67  }
68  static pInstance_ = null;
69  static gi() {
70    if (MainEditor.pInstance_ === null) {
71      MainEditor.pInstance_ = new MainEditor();
72    }
73    return MainEditor.pInstance_;
74  }
75
76  onDraw() {
77    if (this.selectFile_.list_.length <= 0) {
78      X2DFast.gi().drawText('拖入log文件', 30, Scr.logicw / 2, Scr.logich / 2, 1, 1, 0, -2, -2, 0xff000000);
79      return;
80    }
81
82    for (let v in this.viewer_) {
83      if (this.filePoint_ === v) {
84        this.viewer_[v].onDraw();
85      }
86    }
87
88    this.selectFile_.move(Scr.logicw - 200 - 10, 10, 200, 20);
89    this.selectFile_.draw();
90    if (XTools.PROC_TO > 0 && XTools.PROC_TO < 100) {
91      X2DFast.gi().fillRect(0, Scr.logich - 5, XTools.PROC_TO * Scr.logicw / 100, 5, 0xffff0000);
92    }
93  }
94
95  onTouch(msg, x, y) {
96    if (this.selectFile_.list_.length <= 0) {
97      return true;
98    }
99    if (this.selectFile_.onTouch(msg, x, y)) {
100      return true;
101    }
102    for (let v in this.viewer_) {
103      if (this.filePoint_ === v) {
104        if (this.viewer_[v].onTouch(msg, x, y)) {
105          return true;
106        }
107      }
108    }
109    return false;
110  }
111
112  onKey(k) {
113    for (let v in this.viewer_) {
114      if (this.filePoint_ === v) {
115        if (this.viewer_[v].onKey(k)) {
116          return true;
117        }
118      }
119    }
120    return true;
121  }
122}
123
124module.exports = {
125  MainEditor
126};