@leafer-in/robot 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-in/robot
package/dist/robot.cjs ADDED
@@ -0,0 +1,187 @@
1
+ 'use strict';
2
+
3
+ var draw = require('@leafer-ui/draw');
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol */
20
+
21
+
22
+ function __decorate(decorators, target, key, desc) {
23
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
26
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
27
+ }
28
+
29
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
30
+ var e = new Error(message);
31
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
32
+ };
33
+
34
+ class RobotData extends draw.UIData {
35
+ setRobot(value) {
36
+ this._robot = value;
37
+ this.__leaf.__updateRobot();
38
+ }
39
+ setAction(value) {
40
+ this._action = value;
41
+ this.__leaf.__updateAction();
42
+ }
43
+ }
44
+
45
+ exports.Robot = class Robot extends draw.UI {
46
+ get __tag() { return 'Robot'; }
47
+ get nowFrame() { return this.robotFrames && this.robotFrames[this.now]; }
48
+ constructor(data) {
49
+ super(data);
50
+ this.__.__drawAfterFill = true;
51
+ }
52
+ play() {
53
+ this.running = true;
54
+ }
55
+ pause() {
56
+ this.running = false;
57
+ }
58
+ stop() {
59
+ this.pause();
60
+ }
61
+ __updateRobot() {
62
+ const { robot } = this;
63
+ this.robotFrames = [];
64
+ if (!robot)
65
+ return;
66
+ let start = 0;
67
+ if (robot instanceof Array)
68
+ robot.forEach(frame => this.__loadRobot(frame, start, start += frame.total || 1));
69
+ else
70
+ this.__loadRobot(robot, 0, robot.total);
71
+ }
72
+ __updateAction() {
73
+ const action = this.actions[this.action];
74
+ this.stop();
75
+ if (this.__timer)
76
+ clearTimeout(this.__timer);
77
+ if (action === undefined)
78
+ return;
79
+ if (typeof action === 'number') {
80
+ this.now = action;
81
+ }
82
+ else if (action instanceof Array) {
83
+ const { length } = action;
84
+ if (length > 1) {
85
+ const start = this.now = action[0], end = action[action.length - 1];
86
+ this.play();
87
+ this.__runAction(start, end);
88
+ }
89
+ else if (length)
90
+ this.now = action[0];
91
+ }
92
+ }
93
+ __loadRobot(frame, start, end) {
94
+ for (let i = start; i < end; i++)
95
+ this.robotFrames.push(undefined);
96
+ const image = draw.ImageManager.get(frame);
97
+ if (image.ready)
98
+ this.__createFrames(image, frame, start, end);
99
+ else
100
+ image.load(() => this.__createFrames(image, frame, start, end));
101
+ }
102
+ __createFrames(image, frame, start, end) {
103
+ const { offset, size, total } = frame;
104
+ const { width, height } = size && (typeof size === 'number' ? { width: size, height: size } : size) || (total > 1 ? this : image);
105
+ let x = offset ? offset.x : 0, y = offset ? offset.y : 0;
106
+ for (let i = start; i < end; i++) {
107
+ this.robotFrames[i] = { view: image.view, x, y, width, height };
108
+ if (x + width >= image.width)
109
+ x = 0, y += height;
110
+ else
111
+ x += width;
112
+ }
113
+ this.__updateRobotBounds();
114
+ this.forceRender();
115
+ this.emitEvent(new draw.ImageEvent(draw.ImageEvent.LOADED, { image }));
116
+ }
117
+ __runAction(start, end) {
118
+ this.__timer = setTimeout(() => {
119
+ if (this.running) {
120
+ if (this.now === end)
121
+ this.now = start;
122
+ else
123
+ this.now++;
124
+ this.__updateRobotBounds();
125
+ }
126
+ this.__runAction(start, end);
127
+ }, 1000 / this.FPS);
128
+ }
129
+ __updateRobotBounds() {
130
+ const { nowFrame } = this;
131
+ if (nowFrame) {
132
+ const data = this.__;
133
+ const width = nowFrame.width / data.pixelRatio;
134
+ const height = nowFrame.height / data.pixelRatio;
135
+ if (data.width !== width || data.height !== height)
136
+ this.forceUpdate('width');
137
+ data.__naturalWidth = width;
138
+ data.__naturalHeight = height;
139
+ }
140
+ }
141
+ __drawAfterFill(canvas, _options) {
142
+ const { nowFrame } = this;
143
+ if (nowFrame) {
144
+ const { width, height, cornerRadius } = this.__;
145
+ if (cornerRadius || this.pathInputed) {
146
+ canvas.save();
147
+ canvas.clip();
148
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height);
149
+ canvas.restore();
150
+ }
151
+ else {
152
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height);
153
+ }
154
+ }
155
+ }
156
+ destroy() {
157
+ if (this.robotFrames)
158
+ this.robotFrames = null;
159
+ super.destroy();
160
+ }
161
+ };
162
+ __decorate([
163
+ draw.dataProcessor(RobotData)
164
+ ], exports.Robot.prototype, "__", void 0);
165
+ __decorate([
166
+ draw.boundsType()
167
+ ], exports.Robot.prototype, "robot", void 0);
168
+ __decorate([
169
+ draw.dataType()
170
+ ], exports.Robot.prototype, "actions", void 0);
171
+ __decorate([
172
+ draw.dataType('')
173
+ ], exports.Robot.prototype, "action", void 0);
174
+ __decorate([
175
+ draw.surfaceType(0)
176
+ ], exports.Robot.prototype, "now", void 0);
177
+ __decorate([
178
+ draw.dataType(12)
179
+ ], exports.Robot.prototype, "FPS", void 0);
180
+ __decorate([
181
+ draw.dataType(true)
182
+ ], exports.Robot.prototype, "loop", void 0);
183
+ exports.Robot = __decorate([
184
+ draw.registerUI()
185
+ ], exports.Robot);
186
+
187
+ exports.RobotData = RobotData;
@@ -0,0 +1,185 @@
1
+ import { UIData, dataProcessor, boundsType, dataType, surfaceType, registerUI, UI, ImageManager, ImageEvent } from '@leafer-ui/draw';
2
+
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+ /* global Reflect, Promise, SuppressedError, Symbol */
18
+
19
+
20
+ function __decorate(decorators, target, key, desc) {
21
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
22
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
23
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
24
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
25
+ }
26
+
27
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
28
+ var e = new Error(message);
29
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
30
+ };
31
+
32
+ class RobotData extends UIData {
33
+ setRobot(value) {
34
+ this._robot = value;
35
+ this.__leaf.__updateRobot();
36
+ }
37
+ setAction(value) {
38
+ this._action = value;
39
+ this.__leaf.__updateAction();
40
+ }
41
+ }
42
+
43
+ let Robot = class Robot extends UI {
44
+ get __tag() { return 'Robot'; }
45
+ get nowFrame() { return this.robotFrames && this.robotFrames[this.now]; }
46
+ constructor(data) {
47
+ super(data);
48
+ this.__.__drawAfterFill = true;
49
+ }
50
+ play() {
51
+ this.running = true;
52
+ }
53
+ pause() {
54
+ this.running = false;
55
+ }
56
+ stop() {
57
+ this.pause();
58
+ }
59
+ __updateRobot() {
60
+ const { robot } = this;
61
+ this.robotFrames = [];
62
+ if (!robot)
63
+ return;
64
+ let start = 0;
65
+ if (robot instanceof Array)
66
+ robot.forEach(frame => this.__loadRobot(frame, start, start += frame.total || 1));
67
+ else
68
+ this.__loadRobot(robot, 0, robot.total);
69
+ }
70
+ __updateAction() {
71
+ const action = this.actions[this.action];
72
+ this.stop();
73
+ if (this.__timer)
74
+ clearTimeout(this.__timer);
75
+ if (action === undefined)
76
+ return;
77
+ if (typeof action === 'number') {
78
+ this.now = action;
79
+ }
80
+ else if (action instanceof Array) {
81
+ const { length } = action;
82
+ if (length > 1) {
83
+ const start = this.now = action[0], end = action[action.length - 1];
84
+ this.play();
85
+ this.__runAction(start, end);
86
+ }
87
+ else if (length)
88
+ this.now = action[0];
89
+ }
90
+ }
91
+ __loadRobot(frame, start, end) {
92
+ for (let i = start; i < end; i++)
93
+ this.robotFrames.push(undefined);
94
+ const image = ImageManager.get(frame);
95
+ if (image.ready)
96
+ this.__createFrames(image, frame, start, end);
97
+ else
98
+ image.load(() => this.__createFrames(image, frame, start, end));
99
+ }
100
+ __createFrames(image, frame, start, end) {
101
+ const { offset, size, total } = frame;
102
+ const { width, height } = size && (typeof size === 'number' ? { width: size, height: size } : size) || (total > 1 ? this : image);
103
+ let x = offset ? offset.x : 0, y = offset ? offset.y : 0;
104
+ for (let i = start; i < end; i++) {
105
+ this.robotFrames[i] = { view: image.view, x, y, width, height };
106
+ if (x + width >= image.width)
107
+ x = 0, y += height;
108
+ else
109
+ x += width;
110
+ }
111
+ this.__updateRobotBounds();
112
+ this.forceRender();
113
+ this.emitEvent(new ImageEvent(ImageEvent.LOADED, { image }));
114
+ }
115
+ __runAction(start, end) {
116
+ this.__timer = setTimeout(() => {
117
+ if (this.running) {
118
+ if (this.now === end)
119
+ this.now = start;
120
+ else
121
+ this.now++;
122
+ this.__updateRobotBounds();
123
+ }
124
+ this.__runAction(start, end);
125
+ }, 1000 / this.FPS);
126
+ }
127
+ __updateRobotBounds() {
128
+ const { nowFrame } = this;
129
+ if (nowFrame) {
130
+ const data = this.__;
131
+ const width = nowFrame.width / data.pixelRatio;
132
+ const height = nowFrame.height / data.pixelRatio;
133
+ if (data.width !== width || data.height !== height)
134
+ this.forceUpdate('width');
135
+ data.__naturalWidth = width;
136
+ data.__naturalHeight = height;
137
+ }
138
+ }
139
+ __drawAfterFill(canvas, _options) {
140
+ const { nowFrame } = this;
141
+ if (nowFrame) {
142
+ const { width, height, cornerRadius } = this.__;
143
+ if (cornerRadius || this.pathInputed) {
144
+ canvas.save();
145
+ canvas.clip();
146
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height);
147
+ canvas.restore();
148
+ }
149
+ else {
150
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height);
151
+ }
152
+ }
153
+ }
154
+ destroy() {
155
+ if (this.robotFrames)
156
+ this.robotFrames = null;
157
+ super.destroy();
158
+ }
159
+ };
160
+ __decorate([
161
+ dataProcessor(RobotData)
162
+ ], Robot.prototype, "__", void 0);
163
+ __decorate([
164
+ boundsType()
165
+ ], Robot.prototype, "robot", void 0);
166
+ __decorate([
167
+ dataType()
168
+ ], Robot.prototype, "actions", void 0);
169
+ __decorate([
170
+ dataType('')
171
+ ], Robot.prototype, "action", void 0);
172
+ __decorate([
173
+ surfaceType(0)
174
+ ], Robot.prototype, "now", void 0);
175
+ __decorate([
176
+ dataType(12)
177
+ ], Robot.prototype, "FPS", void 0);
178
+ __decorate([
179
+ dataType(true)
180
+ ], Robot.prototype, "loop", void 0);
181
+ Robot = __decorate([
182
+ registerUI()
183
+ ], Robot);
184
+
185
+ export { Robot, RobotData };
@@ -0,0 +1 @@
1
+ import{UIData as t,dataProcessor as e,boundsType as o,dataType as i,surfaceType as s,registerUI as r,UI as h,ImageManager as n,ImageEvent as a}from"@leafer-ui/draw";function _(t,e,o,i){var s,r=arguments.length,h=r<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,o):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)h=Reflect.decorate(t,e,o,i);else for(var n=t.length-1;n>=0;n--)(s=t[n])&&(h=(r<3?s(h):r>3?s(e,o,h):s(e,o))||h);return r>3&&h&&Object.defineProperty(e,o,h),h}"function"==typeof SuppressedError&&SuppressedError;class d extends t{setRobot(t){this._robot=t,this.__leaf.__updateRobot()}setAction(t){this._action=t,this.__leaf.__updateAction()}}let p=class extends h{get __tag(){return"Robot"}get nowFrame(){return this.robotFrames&&this.robotFrames[this.now]}constructor(t){super(t),this.__.__drawAfterFill=!0}play(){this.running=!0}pause(){this.running=!1}stop(){this.pause()}__updateRobot(){const{robot:t}=this;if(this.robotFrames=[],!t)return;let e=0;t instanceof Array?t.forEach((t=>this.__loadRobot(t,e,e+=t.total||1))):this.__loadRobot(t,0,t.total)}__updateAction(){const t=this.actions[this.action];if(this.stop(),this.__timer&&clearTimeout(this.__timer),void 0!==t)if("number"==typeof t)this.now=t;else if(t instanceof Array){const{length:e}=t;if(e>1){const e=this.now=t[0],o=t[t.length-1];this.play(),this.__runAction(e,o)}else e&&(this.now=t[0])}}__loadRobot(t,e,o){for(let t=e;t<o;t++)this.robotFrames.push(void 0);const i=n.get(t);i.ready?this.__createFrames(i,t,e,o):i.load((()=>this.__createFrames(i,t,e,o)))}__createFrames(t,e,o,i){const{offset:s,size:r,total:h}=e,{width:n,height:_}=r&&("number"==typeof r?{width:r,height:r}:r)||(h>1?this:t);let d=s?s.x:0,p=s?s.y:0;for(let e=o;e<i;e++)this.robotFrames[e]={view:t.view,x:d,y:p,width:n,height:_},d+n>=t.width?(d=0,p+=_):d+=n;this.__updateRobotBounds(),this.forceRender(),this.emitEvent(new a(a.LOADED,{image:t}))}__runAction(t,e){this.__timer=setTimeout((()=>{this.running&&(this.now===e?this.now=t:this.now++,this.__updateRobotBounds()),this.__runAction(t,e)}),1e3/this.FPS)}__updateRobotBounds(){const{nowFrame:t}=this;if(t){const e=this.__,o=t.width/e.pixelRatio,i=t.height/e.pixelRatio;e.width===o&&e.height===i||this.forceUpdate("width"),e.__naturalWidth=o,e.__naturalHeight=i}}__drawAfterFill(t,e){const{nowFrame:o}=this;if(o){const{width:e,height:i,cornerRadius:s}=this.__;s||this.pathInputed?(t.save(),t.clip(),t.drawImage(o.view,o.x,o.y,o.width,o.height,0,0,e,i),t.restore()):t.drawImage(o.view,o.x,o.y,o.width,o.height,0,0,e,i)}}destroy(){this.robotFrames&&(this.robotFrames=null),super.destroy()}};_([e(d)],p.prototype,"__",void 0),_([o()],p.prototype,"robot",void 0),_([i()],p.prototype,"actions",void 0),_([i("")],p.prototype,"action",void 0),_([s(0)],p.prototype,"now",void 0),_([i(12)],p.prototype,"FPS",void 0),_([i(!0)],p.prototype,"loop",void 0),p=_([r()],p);export{p as Robot,d as RobotData};
package/dist/robot.js ADDED
@@ -0,0 +1,191 @@
1
+ this.LeaferIN = this.LeaferIN || {};
2
+ this.LeaferIN.Robot = (function (exports, draw) {
3
+ 'use strict';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol */
20
+
21
+
22
+ function __decorate(decorators, target, key, desc) {
23
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
26
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
27
+ }
28
+
29
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
30
+ var e = new Error(message);
31
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
32
+ };
33
+
34
+ class RobotData extends draw.UIData {
35
+ setRobot(value) {
36
+ this._robot = value;
37
+ this.__leaf.__updateRobot();
38
+ }
39
+ setAction(value) {
40
+ this._action = value;
41
+ this.__leaf.__updateAction();
42
+ }
43
+ }
44
+
45
+ exports.Robot = class Robot extends draw.UI {
46
+ get __tag() { return 'Robot'; }
47
+ get nowFrame() { return this.robotFrames && this.robotFrames[this.now]; }
48
+ constructor(data) {
49
+ super(data);
50
+ this.__.__drawAfterFill = true;
51
+ }
52
+ play() {
53
+ this.running = true;
54
+ }
55
+ pause() {
56
+ this.running = false;
57
+ }
58
+ stop() {
59
+ this.pause();
60
+ }
61
+ __updateRobot() {
62
+ const { robot } = this;
63
+ this.robotFrames = [];
64
+ if (!robot)
65
+ return;
66
+ let start = 0;
67
+ if (robot instanceof Array)
68
+ robot.forEach(frame => this.__loadRobot(frame, start, start += frame.total || 1));
69
+ else
70
+ this.__loadRobot(robot, 0, robot.total);
71
+ }
72
+ __updateAction() {
73
+ const action = this.actions[this.action];
74
+ this.stop();
75
+ if (this.__timer)
76
+ clearTimeout(this.__timer);
77
+ if (action === undefined)
78
+ return;
79
+ if (typeof action === 'number') {
80
+ this.now = action;
81
+ }
82
+ else if (action instanceof Array) {
83
+ const { length } = action;
84
+ if (length > 1) {
85
+ const start = this.now = action[0], end = action[action.length - 1];
86
+ this.play();
87
+ this.__runAction(start, end);
88
+ }
89
+ else if (length)
90
+ this.now = action[0];
91
+ }
92
+ }
93
+ __loadRobot(frame, start, end) {
94
+ for (let i = start; i < end; i++)
95
+ this.robotFrames.push(undefined);
96
+ const image = draw.ImageManager.get(frame);
97
+ if (image.ready)
98
+ this.__createFrames(image, frame, start, end);
99
+ else
100
+ image.load(() => this.__createFrames(image, frame, start, end));
101
+ }
102
+ __createFrames(image, frame, start, end) {
103
+ const { offset, size, total } = frame;
104
+ const { width, height } = size && (typeof size === 'number' ? { width: size, height: size } : size) || (total > 1 ? this : image);
105
+ let x = offset ? offset.x : 0, y = offset ? offset.y : 0;
106
+ for (let i = start; i < end; i++) {
107
+ this.robotFrames[i] = { view: image.view, x, y, width, height };
108
+ if (x + width >= image.width)
109
+ x = 0, y += height;
110
+ else
111
+ x += width;
112
+ }
113
+ this.__updateRobotBounds();
114
+ this.forceRender();
115
+ this.emitEvent(new draw.ImageEvent(draw.ImageEvent.LOADED, { image }));
116
+ }
117
+ __runAction(start, end) {
118
+ this.__timer = setTimeout(() => {
119
+ if (this.running) {
120
+ if (this.now === end)
121
+ this.now = start;
122
+ else
123
+ this.now++;
124
+ this.__updateRobotBounds();
125
+ }
126
+ this.__runAction(start, end);
127
+ }, 1000 / this.FPS);
128
+ }
129
+ __updateRobotBounds() {
130
+ const { nowFrame } = this;
131
+ if (nowFrame) {
132
+ const data = this.__;
133
+ const width = nowFrame.width / data.pixelRatio;
134
+ const height = nowFrame.height / data.pixelRatio;
135
+ if (data.width !== width || data.height !== height)
136
+ this.forceUpdate('width');
137
+ data.__naturalWidth = width;
138
+ data.__naturalHeight = height;
139
+ }
140
+ }
141
+ __drawAfterFill(canvas, _options) {
142
+ const { nowFrame } = this;
143
+ if (nowFrame) {
144
+ const { width, height, cornerRadius } = this.__;
145
+ if (cornerRadius || this.pathInputed) {
146
+ canvas.save();
147
+ canvas.clip();
148
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height);
149
+ canvas.restore();
150
+ }
151
+ else {
152
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height);
153
+ }
154
+ }
155
+ }
156
+ destroy() {
157
+ if (this.robotFrames)
158
+ this.robotFrames = null;
159
+ super.destroy();
160
+ }
161
+ };
162
+ __decorate([
163
+ draw.dataProcessor(RobotData)
164
+ ], exports.Robot.prototype, "__", void 0);
165
+ __decorate([
166
+ draw.boundsType()
167
+ ], exports.Robot.prototype, "robot", void 0);
168
+ __decorate([
169
+ draw.dataType()
170
+ ], exports.Robot.prototype, "actions", void 0);
171
+ __decorate([
172
+ draw.dataType('')
173
+ ], exports.Robot.prototype, "action", void 0);
174
+ __decorate([
175
+ draw.surfaceType(0)
176
+ ], exports.Robot.prototype, "now", void 0);
177
+ __decorate([
178
+ draw.dataType(12)
179
+ ], exports.Robot.prototype, "FPS", void 0);
180
+ __decorate([
181
+ draw.dataType(true)
182
+ ], exports.Robot.prototype, "loop", void 0);
183
+ exports.Robot = __decorate([
184
+ draw.registerUI()
185
+ ], exports.Robot);
186
+
187
+ exports.RobotData = RobotData;
188
+
189
+ return exports;
190
+
191
+ })({}, LeaferUI);
@@ -0,0 +1 @@
1
+ "use strict";var t=require("@leafer-ui/draw");function o(t,o,e,i){var s,r=arguments.length,a=r<3?o:null===i?i=Object.getOwnPropertyDescriptor(o,e):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,o,e,i);else for(var n=t.length-1;n>=0;n--)(s=t[n])&&(a=(r<3?s(a):r>3?s(o,e,a):s(o,e))||a);return r>3&&a&&Object.defineProperty(o,e,a),a}"function"==typeof SuppressedError&&SuppressedError;class e extends t.UIData{setRobot(t){this._robot=t,this.__leaf.__updateRobot()}setAction(t){this._action=t,this.__leaf.__updateAction()}}exports.Robot=class extends t.UI{get __tag(){return"Robot"}get nowFrame(){return this.robotFrames&&this.robotFrames[this.now]}constructor(t){super(t),this.__.__drawAfterFill=!0}play(){this.running=!0}pause(){this.running=!1}stop(){this.pause()}__updateRobot(){const{robot:t}=this;if(this.robotFrames=[],!t)return;let o=0;t instanceof Array?t.forEach((t=>this.__loadRobot(t,o,o+=t.total||1))):this.__loadRobot(t,0,t.total)}__updateAction(){const t=this.actions[this.action];if(this.stop(),this.__timer&&clearTimeout(this.__timer),void 0!==t)if("number"==typeof t)this.now=t;else if(t instanceof Array){const{length:o}=t;if(o>1){const o=this.now=t[0],e=t[t.length-1];this.play(),this.__runAction(o,e)}else o&&(this.now=t[0])}}__loadRobot(o,e,i){for(let t=e;t<i;t++)this.robotFrames.push(void 0);const s=t.ImageManager.get(o);s.ready?this.__createFrames(s,o,e,i):s.load((()=>this.__createFrames(s,o,e,i)))}__createFrames(o,e,i,s){const{offset:r,size:a,total:n}=e,{width:h,height:p}=a&&("number"==typeof a?{width:a,height:a}:a)||(n>1?this:o);let _=r?r.x:0,d=r?r.y:0;for(let t=i;t<s;t++)this.robotFrames[t]={view:o.view,x:_,y:d,width:h,height:p},_+h>=o.width?(_=0,d+=p):_+=h;this.__updateRobotBounds(),this.forceRender(),this.emitEvent(new t.ImageEvent(t.ImageEvent.LOADED,{image:o}))}__runAction(t,o){this.__timer=setTimeout((()=>{this.running&&(this.now===o?this.now=t:this.now++,this.__updateRobotBounds()),this.__runAction(t,o)}),1e3/this.FPS)}__updateRobotBounds(){const{nowFrame:t}=this;if(t){const o=this.__,e=t.width/o.pixelRatio,i=t.height/o.pixelRatio;o.width===e&&o.height===i||this.forceUpdate("width"),o.__naturalWidth=e,o.__naturalHeight=i}}__drawAfterFill(t,o){const{nowFrame:e}=this;if(e){const{width:o,height:i,cornerRadius:s}=this.__;s||this.pathInputed?(t.save(),t.clip(),t.drawImage(e.view,e.x,e.y,e.width,e.height,0,0,o,i),t.restore()):t.drawImage(e.view,e.x,e.y,e.width,e.height,0,0,o,i)}}destroy(){this.robotFrames&&(this.robotFrames=null),super.destroy()}},o([t.dataProcessor(e)],exports.Robot.prototype,"__",void 0),o([t.boundsType()],exports.Robot.prototype,"robot",void 0),o([t.dataType()],exports.Robot.prototype,"actions",void 0),o([t.dataType("")],exports.Robot.prototype,"action",void 0),o([t.surfaceType(0)],exports.Robot.prototype,"now",void 0),o([t.dataType(12)],exports.Robot.prototype,"FPS",void 0),o([t.dataType(!0)],exports.Robot.prototype,"loop",void 0),exports.Robot=o([t.registerUI()],exports.Robot),exports.RobotData=e;
@@ -0,0 +1 @@
1
+ this.LeaferIN=this.LeaferIN||{},this.LeaferIN.Robot=function(t,o){"use strict";function e(t,o,e,i){var s,r=arguments.length,a=r<3?o:null===i?i=Object.getOwnPropertyDescriptor(o,e):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,o,e,i);else for(var n=t.length-1;n>=0;n--)(s=t[n])&&(a=(r<3?s(a):r>3?s(o,e,a):s(o,e))||a);return r>3&&a&&Object.defineProperty(o,e,a),a}"function"==typeof SuppressedError&&SuppressedError;class i extends o.UIData{setRobot(t){this._robot=t,this.__leaf.__updateRobot()}setAction(t){this._action=t,this.__leaf.__updateAction()}}return t.Robot=class extends o.UI{get __tag(){return"Robot"}get nowFrame(){return this.robotFrames&&this.robotFrames[this.now]}constructor(t){super(t),this.__.__drawAfterFill=!0}play(){this.running=!0}pause(){this.running=!1}stop(){this.pause()}__updateRobot(){const{robot:t}=this;if(this.robotFrames=[],!t)return;let o=0;t instanceof Array?t.forEach((t=>this.__loadRobot(t,o,o+=t.total||1))):this.__loadRobot(t,0,t.total)}__updateAction(){const t=this.actions[this.action];if(this.stop(),this.__timer&&clearTimeout(this.__timer),void 0!==t)if("number"==typeof t)this.now=t;else if(t instanceof Array){const{length:o}=t;if(o>1){const o=this.now=t[0],e=t[t.length-1];this.play(),this.__runAction(o,e)}else o&&(this.now=t[0])}}__loadRobot(t,e,i){for(let t=e;t<i;t++)this.robotFrames.push(void 0);const s=o.ImageManager.get(t);s.ready?this.__createFrames(s,t,e,i):s.load((()=>this.__createFrames(s,t,e,i)))}__createFrames(t,e,i,s){const{offset:r,size:a,total:n}=e,{width:h,height:_}=a&&("number"==typeof a?{width:a,height:a}:a)||(n>1?this:t);let d=r?r.x:0,p=r?r.y:0;for(let o=i;o<s;o++)this.robotFrames[o]={view:t.view,x:d,y:p,width:h,height:_},d+h>=t.width?(d=0,p+=_):d+=h;this.__updateRobotBounds(),this.forceRender(),this.emitEvent(new o.ImageEvent(o.ImageEvent.LOADED,{image:t}))}__runAction(t,o){this.__timer=setTimeout((()=>{this.running&&(this.now===o?this.now=t:this.now++,this.__updateRobotBounds()),this.__runAction(t,o)}),1e3/this.FPS)}__updateRobotBounds(){const{nowFrame:t}=this;if(t){const o=this.__,e=t.width/o.pixelRatio,i=t.height/o.pixelRatio;o.width===e&&o.height===i||this.forceUpdate("width"),o.__naturalWidth=e,o.__naturalHeight=i}}__drawAfterFill(t,o){const{nowFrame:e}=this;if(e){const{width:o,height:i,cornerRadius:s}=this.__;s||this.pathInputed?(t.save(),t.clip(),t.drawImage(e.view,e.x,e.y,e.width,e.height,0,0,o,i),t.restore()):t.drawImage(e.view,e.x,e.y,e.width,e.height,0,0,o,i)}}destroy(){this.robotFrames&&(this.robotFrames=null),super.destroy()}},e([o.dataProcessor(i)],t.Robot.prototype,"__",void 0),e([o.boundsType()],t.Robot.prototype,"robot",void 0),e([o.dataType()],t.Robot.prototype,"actions",void 0),e([o.dataType("")],t.Robot.prototype,"action",void 0),e([o.surfaceType(0)],t.Robot.prototype,"now",void 0),e([o.dataType(12)],t.Robot.prototype,"FPS",void 0),e([o.dataType(!0)],t.Robot.prototype,"loop",void 0),t.Robot=e([o.registerUI()],t.Robot),t.RobotData=i,t}({},LeaferUI);
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@leafer-in/robot",
3
+ "version": "1.0.3",
4
+ "description": "@leafer-in/robot",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "dist/robot.esm.js",
9
+ "exports": {
10
+ "import": "./dist/robot.esm.js",
11
+ "require": "./dist/robot.cjs",
12
+ "types": "./types/index.d.ts"
13
+ },
14
+ "types": "types/index.d.ts",
15
+ "unpkg": "dist/robot.js",
16
+ "jsdelivr": "dist/robot.js",
17
+ "files": [
18
+ "src",
19
+ "types",
20
+ "dist"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/leaferjs/in.git"
25
+ },
26
+ "homepage": "https://github.com/leaferjs/in/tree/main/packages/robot",
27
+ "bugs": "https://github.com/leaferjs/in/issues",
28
+ "keywords": [
29
+ "leafer robot",
30
+ "leafer-robot",
31
+ "leafer-in",
32
+ "robot",
33
+ "leafer-ui",
34
+ "leaferjs"
35
+ ],
36
+ "dependencies": {
37
+ "@leafer-ui/draw": "^1.0.3",
38
+ "@leafer-ui/interface": "^1.0.3",
39
+ "@leafer-in/interface": "^1.0.3"
40
+ }
41
+ }
package/src/Robot.ts ADDED
@@ -0,0 +1,169 @@
1
+ import { ILeaferCanvas, IRenderOptions, ILeaferImage, IRobot, IRobotData, IRobotInputData, IRobotKeyframe, IRobotActions, IRobotActionName, IRobotComputedKeyframe } from '@leafer-ui/interface'
2
+ import { UI, registerUI, dataProcessor, ImageEvent, surfaceType, ImageManager, dataType, boundsType } from '@leafer-ui/draw'
3
+
4
+ import { RobotData } from './data/RobotData'
5
+
6
+
7
+ @registerUI()
8
+ export class Robot extends UI implements IRobot {
9
+
10
+ public get __tag() { return 'Robot' }
11
+
12
+ public running: boolean
13
+
14
+ public get nowFrame(): IRobotComputedKeyframe { return this.robotFrames && this.robotFrames[this.now] }
15
+
16
+ public robotFrames?: IRobotComputedKeyframe[]
17
+
18
+
19
+ @dataProcessor(RobotData)
20
+ declare public __: IRobotData
21
+
22
+ @boundsType()
23
+ public robot?: IRobotKeyframe | IRobotKeyframe[]
24
+
25
+ @dataType()
26
+ public actions?: IRobotActions
27
+
28
+ @dataType('')
29
+ public action?: IRobotActionName
30
+
31
+ @surfaceType(0)
32
+ public now?: number
33
+
34
+ @dataType(12)
35
+ public FPS?: number
36
+
37
+ @dataType(true)
38
+ public loop?: boolean
39
+
40
+
41
+ protected __timer: any
42
+
43
+
44
+ constructor(data?: IRobotInputData) {
45
+ super(data)
46
+ this.__.__drawAfterFill = true
47
+ }
48
+
49
+
50
+ public play(): void {
51
+ this.running = true
52
+ }
53
+
54
+ public pause(): void {
55
+ this.running = false
56
+ }
57
+
58
+ public stop(): void {
59
+ this.pause()
60
+ }
61
+
62
+
63
+ public __updateRobot(): void {
64
+ const { robot } = this
65
+ this.robotFrames = []
66
+ if (!robot) return
67
+
68
+ let start = 0
69
+ if (robot instanceof Array) robot.forEach(frame => this.__loadRobot(frame, start, start += frame.total || 1))
70
+ else this.__loadRobot(robot, 0, robot.total)
71
+ }
72
+
73
+ public __updateAction(): void {
74
+ const action = this.actions[this.action]
75
+
76
+ this.stop()
77
+
78
+ if (this.__timer) clearTimeout(this.__timer)
79
+ if (action === undefined) return
80
+
81
+ if (typeof action === 'number') {
82
+
83
+ this.now = action
84
+
85
+ } else if (action instanceof Array) {
86
+
87
+ const { length } = action
88
+ if (length > 1) {
89
+ const start = this.now = action[0], end = action[action.length - 1]
90
+ this.play()
91
+ this.__runAction(start, end)
92
+ } else if (length) this.now = action[0]
93
+
94
+ }
95
+ }
96
+
97
+
98
+ protected __loadRobot(frame: IRobotKeyframe, start: number, end: number): void {
99
+ for (let i = start; i < end; i++) this.robotFrames.push(undefined)
100
+
101
+ const image = ImageManager.get(frame)
102
+ if (image.ready) this.__createFrames(image, frame, start, end)
103
+ else image.load(() => this.__createFrames(image, frame, start, end))
104
+ }
105
+
106
+ protected __createFrames(image: ILeaferImage, frame: IRobotKeyframe, start: number, end: number): void {
107
+ const { offset, size, total } = frame
108
+ const { width, height } = size && (typeof size === 'number' ? { width: size, height: size } : size) || (total > 1 ? this : image)
109
+
110
+ let x = offset ? offset.x : 0, y = offset ? offset.y : 0
111
+
112
+ for (let i = start; i < end; i++) {
113
+ this.robotFrames[i] = { view: image.view, x, y, width, height }
114
+ if (x + width >= image.width) x = 0, y += height
115
+ else x += width
116
+ }
117
+
118
+ this.__updateRobotBounds()
119
+ this.forceRender()
120
+ this.emitEvent(new ImageEvent(ImageEvent.LOADED, { image }))
121
+ }
122
+
123
+
124
+ protected __runAction(start: number, end: number): void {
125
+ this.__timer = setTimeout(() => {
126
+ if (this.running) {
127
+ if (this.now === end) this.now = start
128
+ else this.now++
129
+ this.__updateRobotBounds()
130
+ }
131
+
132
+ this.__runAction(start, end)
133
+ }, 1000 / this.FPS)
134
+ }
135
+
136
+ protected __updateRobotBounds(): void {
137
+ const { nowFrame } = this
138
+ if (nowFrame) {
139
+ const data = this.__
140
+ const width = nowFrame.width / data.pixelRatio
141
+ const height = nowFrame.height / data.pixelRatio
142
+ if (data.width !== width || data.height !== height) this.forceUpdate('width')
143
+
144
+ data.__naturalWidth = width
145
+ data.__naturalHeight = height
146
+ }
147
+ }
148
+
149
+ public __drawAfterFill(canvas: ILeaferCanvas, _options: IRenderOptions): void {
150
+ const { nowFrame } = this
151
+ if (nowFrame) {
152
+ const { width, height, cornerRadius } = this.__
153
+ if (cornerRadius || this.pathInputed) {
154
+ canvas.save()
155
+ canvas.clip()
156
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height)
157
+ canvas.restore()
158
+ } else {
159
+ canvas.drawImage(nowFrame.view, nowFrame.x, nowFrame.y, nowFrame.width, nowFrame.height, 0, 0, width, height)
160
+ }
161
+ }
162
+ }
163
+
164
+ public destroy(): void {
165
+ if (this.robotFrames) this.robotFrames = null
166
+ super.destroy()
167
+ }
168
+
169
+ }
@@ -0,0 +1,20 @@
1
+ import { IRobot, IRobotActionName, IRobotKeyframe } from '@leafer-ui/interface'
2
+ import { UIData } from '@leafer-ui/draw'
3
+
4
+
5
+ export class RobotData extends UIData {
6
+
7
+ protected _robot: IRobotKeyframe | IRobotKeyframe[]
8
+ protected _action: IRobotActionName
9
+
10
+ protected setRobot(value: IRobotKeyframe | IRobotKeyframe[]): void {
11
+ this._robot = value;
12
+ (this.__leaf as IRobot).__updateRobot()
13
+ }
14
+
15
+ protected setAction(value: IRobotActionName): void {
16
+ this._action = value;
17
+ (this.__leaf as IRobot).__updateAction()
18
+ }
19
+
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { Robot } from './Robot'
2
+ export { RobotData } from './data/RobotData'
@@ -0,0 +1,38 @@
1
+ import { IRobot, IRobotComputedKeyframe, IRobotData, IRobotKeyframe, IRobotActions, IRobotActionName, IRobotInputData, ILeaferImage, ILeaferCanvas, IRenderOptions } from '@leafer-ui/interface';
2
+ import { UI, UIData } from '@leafer-ui/draw';
3
+
4
+ declare class Robot extends UI implements IRobot {
5
+ get __tag(): string;
6
+ running: boolean;
7
+ get nowFrame(): IRobotComputedKeyframe;
8
+ robotFrames?: IRobotComputedKeyframe[];
9
+ __: IRobotData;
10
+ robot?: IRobotKeyframe | IRobotKeyframe[];
11
+ actions?: IRobotActions;
12
+ action?: IRobotActionName;
13
+ now?: number;
14
+ FPS?: number;
15
+ loop?: boolean;
16
+ protected __timer: any;
17
+ constructor(data?: IRobotInputData);
18
+ play(): void;
19
+ pause(): void;
20
+ stop(): void;
21
+ __updateRobot(): void;
22
+ __updateAction(): void;
23
+ protected __loadRobot(frame: IRobotKeyframe, start: number, end: number): void;
24
+ protected __createFrames(image: ILeaferImage, frame: IRobotKeyframe, start: number, end: number): void;
25
+ protected __runAction(start: number, end: number): void;
26
+ protected __updateRobotBounds(): void;
27
+ __drawAfterFill(canvas: ILeaferCanvas, _options: IRenderOptions): void;
28
+ destroy(): void;
29
+ }
30
+
31
+ declare class RobotData extends UIData {
32
+ protected _robot: IRobotKeyframe | IRobotKeyframe[];
33
+ protected _action: IRobotActionName;
34
+ protected setRobot(value: IRobotKeyframe | IRobotKeyframe[]): void;
35
+ protected setAction(value: IRobotActionName): void;
36
+ }
37
+
38
+ export { Robot, RobotData };