GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — scratch-render/src/SpineManager.js at develop · Gandi-IDE/scratch-render

Expand file tree

/

SpineManager.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
217 lines (184 loc) · 7.23 KB
/

SpineManager.js

Copy path

File metadata and controls

217 lines (184 loc) · 7.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const Spine = require('./spine_runtime/spine-webgl');
const RenderConstants = require('./RenderConstants');
// const GetValue = require('../utils/object/GetValue');
class SpineManager {
constructor (scratchRender, gl, assetHost) {
this.scratchRender = scratchRender;
this.gl = gl;
this.renderer = new Spine.SceneRenderer(gl.canvas, gl);
this.timeKeeper = new Spine.TimeKeeper();
this.assetManager = new Spine.AssetManager(gl, assetHost);
// this.skeletonAsset = null;
this.drawDebug = false;
this.jsonCache = {};
this.atlasCache = {};
this.onNativeSizeChanged = this.onNativeSizeChanged.bind(this);
this.scratchRender.on(RenderConstants.Events.NativeSizeChanged, this.onNativeSizeChanged);
// this.onResize(this.scratchRender.getNativeSize());
this.runningSkeleton = new Set();
this.frameCall = null;
}
isDirty () {
return this.runningSkeleton.size > 0;
}
onNativeSizeChanged (event) {
this.onResize(event.newSize);
}
getNativeSize () {
return this._size;
}
onResize (nativeSize) {
const [width, height] = nativeSize;
// tw: do not resize if new size === old size
if (this._size && this._size[0] === width && this._size[1] === height) {
return;
}
this._size = nativeSize;
// tw: use native size for Drawable positioning logic
// this._rotationCenter[0] = this._nativeSize[0] / 2;
// this._rotationCenter[1] = this._nativeSize[1] / 2;
const {renderer} = this;
// const viewportWidth = renderer.canvas.width;
// const viewportHeight = renderer.canvas.height;
const viewportWidth = width;
const viewportHeight = height;
renderer.camera.position.x = viewportWidth / 2;
renderer.camera.position.y = viewportHeight / 2;
renderer.camera.setViewport(viewportWidth, viewportHeight);
console.log('onResize viewport', viewportWidth, viewportHeight);
}
isMultiSkeleton (skeletonJSONObj) {
const skeletonObjectKeys = ['skeleton', 'bones', 'skins', 'animations'];
return skeletonObjectKeys.find(key => !skeletonJSONObj[key]);
}
getAtlasFileByJSONFile (jsonFile) {
return this.atlasCache[jsonFile];
}
loadAtlasAndJson (atlasKey, jsonFile) {
const loadTextureAtlas = new Promise((resolve, reject) => {
const success = (path, asset) => {
// TODO - cache atlas
resolve();
};
const error = (path, message) => {
console.error('loadSpineAsset error', path);
console.error(' ', message);
reject(message);
};
this.assetManager.loadTextureAtlas(atlasKey, success, error);
}).catch(e => {
console.log(e);
});
const loadJson = new Promise((resolve, reject) => {
const success = (path, asset) => {
this.atlasCache[jsonFile] = atlasKey;
resolve(this.assetManager.require(jsonFile));
};
const error = (path, message) => {
console.error('loadSpineAsset error', path);
console.error(' ', message);
reject(message);
};
this.assetManager.loadJson(jsonFile, success, error);
}).catch(e => {
console.log(e);
});
return Promise.all([loadTextureAtlas, loadJson]);
}
getSkeletonNamesForJSON (jsonFile) {
if (!jsonFile) return null;
let skeletonJSONObj = this.jsonCache[jsonFile];
if (!skeletonJSONObj) {
skeletonJSONObj = this.assetManager.require(jsonFile);
this.jsonCache[jsonFile] = skeletonJSONObj;
}
if (!skeletonJSONObj) {
console.warn(`Gandi: skeleton json file not loaded - ${jsonFile}`);
return null;
}
if (this.isMultiSkeleton(skeletonJSONObj)) {
return Object.keys(skeletonJSONObj);
}
return [jsonFile];
}
async createSkeleton (atlasKey, jsonFile, skeletonName) {
await this.loadAtlasAndJson(atlasKey, jsonFile, skeletonName);
const atlas = this.assetManager.require(atlasKey);
if (!atlas) {
console.warn(`Gandi: skeleton atlas data not loaded - ${atlasKey}`);
return null;
}
let skeletonJSONObj = this.jsonCache[jsonFile];
if (!skeletonJSONObj) {
skeletonJSONObj = this.assetManager.require(jsonFile);
}
if (!skeletonJSONObj) {
console.warn(`Gandi: skeleton json file not loaded - ${jsonFile}`);
return null;
}
const atlasLoader = new Spine.AtlasAttachmentLoader(atlas);
const skeletonLoader = new Spine.SkeletonJson(atlasLoader);
if (!skeletonLoader) {
console.warn(`Spine: No skeleton data for: ${atlasKey}`);
return null;
}
const isMultiSkeleton = this.isMultiSkeleton(skeletonJSONObj);
let dataJSON = isMultiSkeleton ? skeletonJSONObj[skeletonName] : skeletonJSONObj;
if (!dataJSON) {
console.warn(`Spine: No skeleton data for: ${skeletonName}, try load first valid one in JSON`);
if (isMultiSkeleton) {
for (const key in skeletonJSONObj) {
if (Object.hasOwnProperty.call(skeletonJSONObj, key)) {
dataJSON = skeletonJSONObj[key];
break;
}
}
}
}
const skeletonData = skeletonLoader.readSkeletonData(dataJSON);
const skeleton = new Spine.Skeleton(skeletonData);
return {
skeletonData: skeletonData,
skeleton: skeleton,
preMultipliedAlpha: skeleton.preMultipliedAlpha
};
}
createAnimationState (skeleton) {
const stateData = new Spine.AnimationStateData(skeleton.data);
const state = new Spine.AnimationState(stateData);
return {stateData: stateData, state: state};
}
updateTime () {
this.timeKeeper.update();
}
rendererSkeleton (skeleton) {
this.renderer.begin();
this.renderer.drawSkeleton(skeleton, true);
if (this.drawDebug) {
console.log('rendererSkeleton');
this.renderer.drawSkeletonDebug(skeleton, true);
const {x, y, width, height} = skeleton.getBoundsRect();
// this.renderer.rectLine(false, x, y, x + width, y + height, 1, new Spine.Color(1, 1, 0, 1));
this.renderer.rect(false, x, y, width, height, new Spine.Color(0, 0, 1, 1));
}
this.renderer.end();
}
removeRunningSkeleton (skin) {
this.runningSkeleton.delete(skin);
}
addRunningSkeleton (skin) {
if (!this.runningSkeleton.has(skin)) {
this.runningSkeleton.add(skin);
}
}
stopRenderSkeleton () {
this.runningSkeleton.forEach(skin => {
skin.clearTracks();
});
this.runningSkeleton.clear();
}
// TODO: implement pause/resume
pauseRenderSkeleton () {}
resumeRenderSkeleton () {}
}
module.exports = SpineManager;
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.