-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.mjs
246 lines (223 loc) · 6.73 KB
/
library.mjs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* global Log MM */
function asleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
class Scenes {
#scenario = []
#options = {}
#index = 0
#timer = null
#timerStarted = null
#pausedRemaining = 0
#updateCallback = () => { }
constructor({ scenario = [], defaults = {}, options = {}, updator = () => { } }) {
this.#updateCallback = updator
this.#options = options
this.#scenario = scenario.map((scene, index) => {
const enter = (scene.enter || []).map((role) => {
if (typeof role === 'string') {
return {
role,
...defaults.defaultEnter,
}
}
return {
...defaults.defaultEnter,
...role,
}
})
const exit = (scene.exit || []).map((role) => {
if (typeof role === 'string') {
return {
role,
...defaults.defaultExit,
}
}
return {
...defaults.defaultExit,
...role,
}
})
return {
name: scene.name || `scene_${index + 1}`,
activeIndicator: String(scene.activeIndicator || defaults.activeIndicator || index),
inactiveIndicator: String(scene.inactiveIndicator || defaults.inactiveIndicator || index),
enter,
exit,
life: scene.life ?? defaults.life,
next: (scene.next === false) ? false : (scene.next === 0) ? 0 : (scene.next || null),
previous: (scene.previous === false) ? false : (scene.previous === 0) ? 0 : (scene.previous || null),
}
})
}
get length() {
return this.#scenario.length
}
get indicators() {
return {
active: this.#scenario.map(scene => scene.activeIndicator),
inactive: this.#scenario.map(scene => scene.inactiveIndicator)
}
}
get index() {
return this.#index
}
#findSceneIndex(id) {
const found = this.#scenario.findIndex(scene => scene.name === id)
if (found >= 0) return found
const index = parseInt(id)
if (!isNaN(index) && index >= 0 && index < this.#scenario.length) return index
return null
}
async play(id) {
let result = {
status: false,
currentScene: null,
index: null,
message: "Scene not found"
}
const lockString = this.#options.lockString
const sceneIndex = this.#findSceneIndex(id) ?? this.#index
const scene = this.#scenario[ sceneIndex ]
if (!scene) return result
this.#index = sceneIndex
this.#pausedRemaining = 0
const exitAll = async function () {
const roles = scene.exit || []
if (roles.length < 1) return
for (const role of roles) {
const modules = MM.getModules().withClass(role.role).filter(module => !module.hidden)
for (const module of modules) {
MM.hideModule(module, role.duration, () => {}, {
lockString,
animate: role.animation,
})
await asleep(role.gap)
}
}
}
const enterAll = async function () {
const roles = scene.enter || []
if (roles.length < 1) return
for (const role of roles) {
const modules = MM.getModules().withClass(role.role).filter(module => module.hidden)
for (const module of modules) {
MM.showModule(module, role.duration, () => {}, {
lockString,
animate: role.animation,
})
await asleep(role.gap)
}
}
return true
}
Log.log("[SCENE] Scene transition starts:", scene.name)
await exitAll()
this.#updateCallback()
await enterAll()
Log.log("[SCENE] Scene will live:", scene.name, scene.life)
if (!isNaN(scene.life) && scene.life > 0) {
clearTimeout(this.#timer)
this.#timer = null
this.#timerStarted = new Date(Date.now())
this.#timer = setTimeout(() => {
clearTimeout(this.#timer)
this.next()
}, scene.life)
}
return {
status: true,
currentScene: scene,
index: sceneIndex,
message: "Scene Played"
}
}
async pause() {
const life = this.#scenario[ this.#index ].life
this.#pausedRemaining = life - ((this.#timerStarted) ? this.#timerStarted - new Date(Date.now()) : 0)
clearTimeout(this.#timer)
this.#timer = null
this.#timerStarted = null
let result = {
message: "Scene Paused",
status: true,
currentScene: this.#scenario[ this.#index ],
index: this.#index,
}
Log.log(result, `Remaining: ${this.#pausedRemaining}`)
return result
}
async resume() {
if (this.#pausedRemaining > 0) {
this.#timerStarted = new Date(Date.now())
clearTimeout(this.#timer)
this.#timer = setTimeout(() => {
clearTimeout(this.#timer)
this.next()
}, this.#pausedRemaining)
}
let result = {
message: "Scene Resumed",
status: true,
currentScene: this.#scenario[ this.#index ],
index: this.#index,
}
Log.log(result, `Resumed: ${this.#pausedRemaining}`)
return result
}
async next() {
const scene = this.getScene(this.#index)
if (!scene) return {
status: false,
currentScene: null,
index: this.#index,
message: "Something wrong. Invalid index:" + this.#index
}
const param = { scene: {...scene}, scenario: [...this.#scenario] }
const sn = (typeof scene.next === 'function') ? scene.next(param) : scene.next
const nextIndex = (sn === false)
? false
: (sn === 0)
? 0
: (sn)
? this.#findSceneIndex(sn)
: ((this.#index + 1) >= this.#scenario.length ? 0 : this.#index + 1)
if (nextIndex === false) return await this.current()
this.#index = nextIndex
return await this.play(this.#index)
}
async previous() {
const scene = this.getScene(this.#index)
if (!scene) return {
status: false,
currentScene: null,
index: this.#index,
message: "Something wrong. Invalid index:" + this.#index
}
const param = { scene: {...scene}, scenario: [...this.#scenario] }
const sp = (typeof scene.previous === 'function') ? scene.previous(param) : scene.previous
const prevIndex = (sp === false)
? false
: (sp === 0)
? 0
: (sp)
? this.#findSceneIndex(sp)
: ((this.#index - 1) < 0 ? this.#scenario.length - 1 : this.#index - 1)
if (prevIndex === false) return await this.current()
this.#index = prevIndex
return await this.play(this.#index)
}
async current() {
return {
status: true,
currentScene: this.#scenario[ this.#index ],
index: this.#index,
message: "Current Scene"
}
}
getScene(id) {
return this.#scenario.find(scene => scene.name === id) || this.#scenario[ id ] || null
}
}
Log.log('[Scenes]: Library loaded')
export { Scenes }