123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- import { _IProperty, _ITime } from "../Data/CommonDataType";
- import Tool_Tween from "./Tool/Tool_Tween";
- import Tool2D from "./Tool2D";
- import Tool3D from "./Tool3D";
- /**
- * 工具类
- */
- export default class Tool {
- /**缓动动画工具类 */
- private static tween: Tool_Tween;
- /**2D工具类 */
- private static tool2D: Tool2D;
- /**3D工具类 */
- private static tool3D: Tool3D;
- private constructor () {
- }
- /**缓动动画工具 */
- public static get Tween (): Tool_Tween {
- if (this.tween == null) {
- this.tween = new Tool_Tween();
- }
- return this.tween;
- }
- /**2D工具 */
- public static get Tool2D (): Tool2D {
- if (this.tool2D == null) {
- this.tool2D = new Tool2D();
- }
- return this.tool2D;
- }
- /**3D工具 */
- public static get Tool3D (): Tool3D {
- if (this.tool3D == null) {
- this.tool3D = new Tool3D();
- }
- return this.tool3D;
- }
- //////////////////////////////////////////////// Log ////////////////////////////////////////////////
- public static isLog: boolean = true;
- public static log (...args): void {
- if (this.isLog == true) {
- console.log(...args);
- }
- }
- public static error (...args): void {
- if (this.isLog == true) {
- console.error(...args);
- }
- }
- //////////////////////////////////////////////// Data ////////////////////////////////////////////////
- /**数据比对覆盖,只对inData存在的值进行覆盖 */
- public static dataProofreadCover (inData: any, outData: any): any {
- if (inData != null && outData != null) {
- for (let index in inData) {
- if (outData[index] !== undefined) {
- if (typeof inData[index] == "object" && outData[index] != null) {
- this.dataProofreadCover(inData[index], outData[index])
- }
- else {
- inData[index] = outData[index];
- }
- }
- }
- }
- return inData;
- }
- /**数据补充覆盖,只对inData不存在的值进行补充 */
- public static dataSupplementCover (inData: any, outData: any): any {
- if (inData != null && outData != null) {
- for (let index in outData) {
- if (inData[index] == undefined) {
- if (typeof inData[index] == "object" && outData[index] != null) {
- inData[index] = Array.isArray(outData[index]) ? [] : {};
- this.dataSupplementCover(inData[index], outData[index])
- }
- else {
- inData[index] = outData[index];
- }
- }
- }
- }
- return inData;
- }
- /**数据完全覆盖,对inData进行完全覆盖 */
- public static dataAllCover (inData: any, outData: any): any {
- if (inData != null && outData != null) {
- for (let index in outData) {
- if (typeof outData[index] == "object" && outData[index] != null) {
- inData[index] = Array.isArray(outData[index]) ? [] : {};
- this.dataAllCover(inData[index], outData[index])
- }
- else {
- inData[index] = outData[index];
- }
- }
- }
- return inData;
- }
- /**寻找key值 */
- public static findKey (data: Object, value: any, compare = (a: any, b: any) => a === b): any {
- return Object.keys(data).find(k => compare(data[k], value));
- }
-
- //////////////////////////////////////////////// Math ////////////////////////////////////////////////
- /**随机数 */
- public static random (number: number): number {
- return Math.round(Math.random()*number+0.4-0.4);
- }
- /**随机数组 */
- public static randomArray (sort: Array<number>, count: number = 10000): Array<number> {
- let func = (sort: Array<number>) => {
- let temp = [];
- let length = sort.length;
- for (let j = 0; j < length; j++) {
- let random = Math.round(Math.random()*(sort.length-1));
- temp[j] = sort[random];
- sort.splice(random, 1);
- }
- sort = temp;
- return sort;
- }
- for (let i = 0; i < count; i++) {
- sort = func(sort);
- sort = func(sort);
- }
- return sort;
- }
-
- /**数值限制 */
- public static clamp (min: number, max: number, data: number): number {
- if (data < min) {
- data = min;
- }
- else if (data > max) {
- data = max;
- }
- return data;
- }
- /**数值限制 */
- public static clampProperty (inData: _IProperty, outData: _IProperty, data: _IProperty, isClamp: boolean = true): _IProperty {
- if ((inData.max != NaN && outData.max != NaN) && (inData.max != null && outData.max != null)) {
- data.max = inData.max + outData.max;
- }
- if ((inData.min != NaN && outData.min != NaN) && (inData.min != null && outData.min != null)) {
- data.min = inData.min + outData.min;
- }
- if ((inData.value != NaN && outData.value != NaN) && (inData.value != null && outData.value != null)) {
- data.value = inData.value + outData.value;
- if (isClamp == true) {
- data.value = this.clamp(data.min, data.max, data.value);
- }
- }
- return data;
- }
- /**修改属性 */
- public static setPropertyValue (property: _IProperty, value: number): _IProperty {
- property.value = this.clamp(property.min, property.max, property.value+value);
- return property;
- }
- /**科学计数法 */
- public static formatter (value: number, fix?: number): string {
- let bits = ["K","M","B","T","aa","ab","ac","ad","ae","af","bb","bc","bd"];
- if (value >= 1000) {
- for (let i = bits.length; i > 0; i--) {
- if (value >= Math.pow(1000,i)) {
- let j = (value/Math.pow(1000,i)).toFixed(fix);
- j += bits[i-1];
- return `${j}`;
- }
- }
- }
- return value+"";
- }
- /**获得夹角 */
- public static getAngle (px: number, py: number, mx: number, my: number): number {
- var x = Math.abs(px-mx);
- var y = Math.abs(py-my);
- var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
- var cos = y/z;
- var radina = Math.acos(cos);//用反三角函数求弧度
- var angle = Math.floor(180/(Math.PI/radina));//将弧度转换成角度
- if(mx>px&&my>py){//鼠标在第四象限
- angle = 180 - angle;
- }
- if(mx==px&&my>py){//鼠标在y轴负方向上
- angle = 180;
- }
- if(mx>px&&my==py){//鼠标在x轴正方向上
- angle = 90;
- }
- if(mx<px&&my>py){//鼠标在第三象限
- angle = 180+angle;
- }
- if(mx<px&&my==py){//鼠标在x轴负方向
- angle = 270;
- }
- if(mx<px&&my<py){//鼠标在第二象限
- angle = 360 - angle;
- }
- return angle;
- }
- /**角度转向量 */
- public static getVector (angle: number): cc.Vec2 {
- // 将传入的角度转为弧度
- let radian = Math.PI / 180 * angle;
- // 邻边 / 斜边
- let cos = Math.cos(radian);
- // 对边 / 斜边
- let sin = Math.sin(radian);
- // 结合在一起并归一化
- let vec = cc.v2(cos, sin).normalize();
- return vec;
- }
- /**贝塞尔曲线 */
- public static bezier3 (a1, a2, a3, t) {
- return ((1 - t) * (1 - t)) * a1 + 2 * t * (1 - t) * a2 + t * t * a3;
- }
- //////////////////////////////////////////////// String ////////////////////////////////////////////////
- /**是否有中文 */
- public static isChinese (string: string): boolean {
- let reg= /[\u4e00-\u9fa5]+/;
- if (!reg.test(string)) {
- return false
- }
- return true;
- }
- /**将数字转换为汉字 */
- public static convertNumberToChinese (data: number): string {
- let Chinese: string = "";
- let list_1: Array<string> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- let list_2: Array<string> = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
- let list_3: Array<string> = ["十", "百", "千", "万", "十万", "百万", "千万", "亿"];
- let temp: Array<string> = (""+data).split("").reverse();
- let zore: boolean = false;
- for (let i: number = 0; i < temp.length; i++) {
- let index: number = list_1.indexOf(temp[i]);
- if (i == 0) {
- if (index != 0) {
- Chinese = list_2[index];
- }
- }
- else {
- if (index != 0) {
- Chinese = list_3[i-1] + Chinese;
- Chinese = list_2[index] + Chinese;
-
- zore = false;
- }
- else if (zore == false) {
- Chinese = list_2[index] + Chinese;
- zore = true;
- }
- }
- }
- if (Chinese.substring(0, 2) == "一十") {
- Chinese = Chinese.replace("一十", "十");
- }
- if (Chinese[Chinese.length-1] == list_2[0]) {
- Chinese = Chinese.substring(0, Chinese.length-1);
- }
- return Chinese;
- }
- /**将数字转换为真实日期 */
- public static converNumberToDate (time: number): string {
- let _time: _ITime = this.converNumberToTime(time);
- let _month = _time.month < 10?('0'+_time.month):_time.month;
- let _day = _time.day < 10?('0'+_time.day):_time.day;
- let _hour = (_time.hour) < 10?('0'+_time.hour):_time.hour;
- let _minute = (_time.minute) < 10?('0'+_time.minute):_time.minute;
- let _second = (_time.second) < 10?('0'+_time.second):_time.second;
- return _time.year+"-"+_month+"-"+_day+" "+_hour+":"+_minute+":"+_second;
- }
- /**将数字转换为时间 */
- public static converNumberToTime (time: number): _ITime {
- let d = new Date(Math.round(time)*1000);
- let year = d.getFullYear();
- let month = (d.getMonth() + 1);
- let day = d.getDate();
- let hour = d.getHours();
- let minute = d.getMinutes();
- let seconds = d.getSeconds();
- return {
- year: year,
- month: month,
- day: day,
- hour: hour,
- minute: minute,
- second: seconds,
- };
- }
- /**将数字转换为时间文本 */
- public static converNumberToTimeText (time: number, isChinese: boolean = false, isClearZero: boolean = false): string {
- time = time<0? 0:time;
- let t = Math.ceil(time);
- let h = Math.floor(t / 3600);
- let m = Math.floor(t % 3600 / 60);
- let s = Math.floor(t % 60);
- let hs: string = (h>=10)? h+"":("0"+h);
- let ms: string = (m>=10)? m+"":("0"+m);
- let ss: string = (s>=10)? s+"":("0"+s);
- let str: string = "";
-
- if (h > 0) {
- if (isClearZero == true) {
- if (hs.charAt(0) == "0") {
- hs = hs.substring(1, hs.length);;
- }
- }
- if (isChinese == true) {
- str = hs + "时" + ms + "分" + ss + "秒";
- }
- else {
- str = hs + ":" + ms + ":" + ss;
- }
- }
- else {
- if (isClearZero == true) {
- if (ms.charAt(0) == "0") {
- ms = ms.substring(1, ms.length);
- }
- }
- if (isChinese == true) {
- str = ms + "分" + ss +"秒";
- }
- else {
- str = ms + ":" + ss;
- }
- }
- return str;
- }
- }
|