-
Notifications
You must be signed in to change notification settings - Fork 3
/
tutorialWindow.ts
220 lines (184 loc) · 7.67 KB
/
tutorialWindow.ts
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
namespace microcode {
const MAX_NUMBER_OF_TUTORIAL_TIPS_ON_SCREEN: number = 3
/**
* A single paragraph that contains an important point for the user.
* Keywords may be passed - each keyword will be coloured.
*/
type TutorialTip = {
text: string,
keywords?: string[],
keywordColors?: number[],
}
/**
* Owned via composition by the live-data-viewer and the recordingConfigSelection.
* Displays a scrollable sub-window filled with custom & coloured text - specified by the TutorialTip
* The owner should restate their control schema: UP, DOWN, A, B are used by the TutorialWindow.
*/
export class TutorialWindow extends Scene {
private tutorialTextTips: TutorialTip[];
private numberOfTips: number
private tutorialTextIndexOffset: number;
private nextScene: Scene;
private backFn: () => void;
constructor(
app: App,
opts: {tips: TutorialTip[], backFn: () => void},
nextScene: Scene,
){
super(app, "tutorialWindow")
this.tutorialTextTips = opts.tips;
this.numberOfTips = opts.tips.length;
this.tutorialTextIndexOffset = 0;
this.nextScene = nextScene;
this.backFn = opts.backFn;
basic.pause(5)
}
/* override */ startup() {
super.startup()
control.onEvent(
ControllerButtonEvent.Pressed,
controller.A.id,
() => {
this.app.popScene()
this.app.pushScene(this.nextScene)
}
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.B.id,
() => this.backFn()
)
// Unbind:
control.onEvent(ControllerButtonEvent.Pressed, controller.left.id, () => {})
control.onEvent(ControllerButtonEvent.Pressed, controller.right.id, () => {})
control.onEvent(
ControllerButtonEvent.Pressed,
controller.up.id,
() => this.tutorialTextIndexOffset = Math.max(this.tutorialTextIndexOffset - 1, 0)
)
control.onEvent(
ControllerButtonEvent.Pressed,
controller.down.id,
() => this.tutorialTextIndexOffset = Math.min(this.tutorialTextIndexOffset + 1, this.tutorialTextTips.length - MAX_NUMBER_OF_TUTORIAL_TIPS_ON_SCREEN)
)
}
draw() {
Screen.fillRect(
Screen.LEFT_EDGE,
Screen.TOP_EDGE,
Screen.WIDTH,
Screen.HEIGHT,
0xC
)
const headerX = Screen.HALF_WIDTH
const headerY = Screen.HALF_HEIGHT - 60 + 8
//-------------
// Draw Window:
//-------------
const subWindowRightEdge = Screen.HALF_WIDTH + 70;
const subWindowTopEdge = Screen.HALF_HEIGHT - 60
const subWindowWidth = 140
const subWindowHeight = 120
// Sub-window:
// Outline:
screen().fillRect(
Screen.HALF_WIDTH - 70,
subWindowTopEdge,
subWindowWidth,
subWindowHeight,
15 // Black
)
screen().fillRect(
Screen.HALF_WIDTH - 70 + 3,
subWindowTopEdge + 3,
subWindowWidth - 6,
subWindowHeight - 6,
3 // Pink
)
//------------
// Draw Title:
//------------
const tutorialTextLength = ("Tutorial".length * font.charWidth)
screen().print(
"Tutorial",
headerX - (tutorialTextLength / 2),
headerY,
15 // Black
)
// Underline the title:
screen().fillRect(
headerX - (tutorialTextLength / 2) - 3,
subWindowTopEdge + 16,
tutorialTextLength + 4,
1,
15 // Black
)
//-----------
// Scrollbar:
//-----------
screen().fillRect(
subWindowRightEdge - 8,
subWindowTopEdge + 10 + (this.tutorialTextIndexOffset / (this.numberOfTips - MAX_NUMBER_OF_TUTORIAL_TIPS_ON_SCREEN)) * (subWindowHeight - 20),
3,
6,
15 // Black
)
//---------------------
// Write Tutorial Tips:
//---------------------
// Some tutorials have coloured keywords, the text is printed in all black first, then the keyword is printed ontop:
let tutorialTextYOffset = 25
const tipsOnScreen = Math.min(this.tutorialTextTips.length, this.tutorialTextIndexOffset + MAX_NUMBER_OF_TUTORIAL_TIPS_ON_SCREEN)
this.tutorialTextTips.slice(this.tutorialTextIndexOffset, tipsOnScreen).forEach((tip) => {
screen().print(
tip.text,
headerX - 55,
tutorialTextYOffset,
15 // Black
)
// Keyword highlighting:
if (tip.keywords != null) {
for (let id = 0; id < tip.keywords.length; id++) {
let keyword = tip.keywords[id]
const keywordIndex = tip.text.indexOf(keyword)
const stringBeforeKeyword = tip.text.split(keyword, keywordIndex)[0]
const newlinesBeforeKeyword = stringBeforeKeyword.split("\n", keywordIndex)
// Find the position of the last newline before the keyword:
let newlineBeforeKeywordIndex = 0
for (let i = keywordIndex; i > 0; i--) {
if (stringBeforeKeyword.charAt(i) == "\n") {
newlineBeforeKeywordIndex = i
break
}
}
// Qty of characters between the last newline before the keyword is the xOffset:
let xOffset = (keywordIndex - newlineBeforeKeywordIndex) * font.charWidth
// Account for newline char:
if (newlineBeforeKeywordIndex != 0) {
xOffset -= 1 * font.charWidth
}
// Number of newlines before the keyword are pushed infront:
for (let _ = 0; _ < newlinesBeforeKeyword.length - 1; _++) {
keyword = "\n" + keyword
}
// Print them directly ontop of the word in black, but with the specified colouring:
screen().print(
keyword,
headerX - 55 + xOffset,
tutorialTextYOffset,
tip.keywordColors[id],
)
}
}
// Bullet point:
screen().fillCircle(
headerX - 61,
tutorialTextYOffset + 4,
2,
15 // Black
)
tutorialTextYOffset += (tip.text.split("\n").length * font.charHeight * 1.33) + 3 // .match() and matchAll() are not present; .split() is memory inefficient
})
}
}
}