1/*
2 * Copyright (c) 2023 Huawei Device 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
16export class XTools {
17  constructor() { }
18  static MOUSE_POS = {
19    x: 0,
20    y: 0,
21  };
22  static InRect(xx, yy, x, y, w, h) {
23    if (xx < x) return false;
24    if (yy < y) return false;
25    if (xx > x + w) return false;
26    if (yy > y + h) return false;
27    return true;
28  }
29  static PROC_TO = 0;
30  static CONFIG = null;
31  static LoadConfig() {
32    const xhr = new XMLHttpRequest();
33    xhr.open('GET', 'config.json');
34    xhr.onreadystatechange = () => {
35      if (xhr.readyState === XMLHttpRequest.DONE) {
36        if (xhr.status === 200) {
37          try {
38            XTools.CONFIG = JSON.parse(xhr.responseText);
39            for (let k in XTools.CONFIG.NodeColor) {
40              XTools.CONFIG.NodeColor[k] = parseInt(XTools.CONFIG.NodeColor[k], 16);
41            }
42            for (let k in XTools.CONFIG.LineColor) {
43              XTools.CONFIG.LineColor[k] = parseInt(XTools.CONFIG.LineColor[k], 16);
44            }
45          } catch (e) {
46            alert('Config file error');
47          }
48        } else {
49          alert('Failed to load config file');
50        }
51      }
52    };
53    xhr.send();
54  }
55}
56export function fAngle(x, y) {
57  return (Math.atan2(-y, x) * 180) / Math.PI;
58}
59export function iDistance(x, y) {
60  return Math.sqrt(x * x + y * y);
61}
62
63export var timeMs = 0;
64export function freshTime() {
65  let t = new Date();
66  timeMs = t.getTime();
67}
68freshTime();
69export function TimeMS() {
70  let t = new Date();
71  return t.getTime();
72}
73export function RandInt(min = 0, max = 100) {
74  return Math.floor(Math.random() * (max - min)) + min;
75}
76
77export function GetURL() {
78  if ('undefined' != typeof wx) {
79    return 'https://7465-testegg-19e3c9-1301193145.tcb.qcloud.la/';
80  } else return '';
81}
82