This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathTutorialBufferLayer.tsx
567 lines (483 loc) · 18.7 KB
/
TutorialBufferLayer.tsx
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* TutorialBufferLayer.tsx
*
* Layer that handles the top-level rendering of the tutorial UI,
* including the nested `NeovimEditor`, description, goals, etc.
*/
import * as React from "react"
import * as Oni from "oni-api"
import { Event, IEvent } from "oni-types"
import styled from "styled-components"
import { NeovimEditor } from "./../../../Editor/NeovimEditor"
import { getInstance as getPluginManagerInstance } from "./../../../Plugins/PluginManager"
import { getInstance as getColorsInstance } from "./../../Colors"
import { getInstance as getCompletionProvidersInstance } from "./../../Completion"
import { configuration } from "./../../Configuration"
import { getInstance as getDiagnosticsInstance } from "./../../Diagnostics"
import { getInstance as getLanguageManagerInstance } from "./../../Language"
import { getInstance as getMenuManagerInstance } from "./../../Menu"
import { getInstance as getOverlayInstance } from "./../../Overlay"
import { getInstance as getSnippetManagerInstance } from "./../../Snippets"
import { getThemeManagerInstance } from "./../../Themes"
import { getInstance as getTokenColorsInstance } from "./../../TokenColors"
import { windowManager } from "./../../WindowManager"
import { getInstance as getWorkspaceInstance } from "./../../Workspace"
import { Bold, withProps } from "./../../../UI/components/common"
import { FlipCard } from "./../../../UI/components/FlipCard"
import { StatusBar } from "./../../../UI/components/StatusBar"
import { ITutorialState, TutorialGameplayManager } from "./TutorialGameplayManager"
import { TutorialManager } from "./TutorialManager"
import { CompletionView } from "./CompletionView"
import { GameplayBufferLayer } from "./GameplayBufferLayer"
import { GoalView } from "./GoalView"
import { getInstance, Vector } from "./../../Particles"
export interface IGameplayCompletionInfo {
completed: boolean
keyPresses: number
timeInMilliseconds: number
}
const DefaultCompletionInfo = {
completed: false,
keyPresses: -1,
timeInMilliseconds: 0,
}
export interface IGameplayStateChangedEvent {
tutorialState: ITutorialState
completionInfo: IGameplayCompletionInfo
mode: string
}
export class GameTracker {
private _startTime: Date
private _keyPresses: number
public start(): void {
this._startTime = new Date()
this._keyPresses = 0
}
public addKeyPress(pressCount: number) {
this._keyPresses += pressCount
}
public end(): IGameplayCompletionInfo {
return {
completed: true,
timeInMilliseconds: new Date().getTime() - this._startTime.getTime(),
keyPresses: this._keyPresses,
}
}
}
export class TutorialBufferLayer implements Oni.BufferLayer {
private _editor: NeovimEditor
private _tutorialGameplayManager: TutorialGameplayManager
private _initPromise: Promise<void>
private _lastStage = -1
private _hasAddedLayer: boolean = false
private _currentTutorialId: string
private _lastTutorialState: ITutorialState
private _completionInfo: IGameplayCompletionInfo = DefaultCompletionInfo
private _element: HTMLElement
private _notes: JSX.Element[] = []
private _gameTracker: GameTracker = new GameTracker()
private _onStateChangedEvent: Event<ITutorialBufferLayerState> = new Event<
ITutorialBufferLayerState
>("TutorialBufferLayer::onStateChangedEvent")
public get id(): string {
return "oni.layer.tutorial"
}
public get friendlyName(): string {
return "Tutorial"
}
constructor(private _tutorialManager: TutorialManager) {
// TODO: Streamline dependences for NeovimEditor, so it's easier just to spin one up..
this._editor = new NeovimEditor(
getColorsInstance(),
getCompletionProvidersInstance(),
configuration,
getDiagnosticsInstance(),
getLanguageManagerInstance(),
getMenuManagerInstance(),
getOverlayInstance(),
getPluginManagerInstance(),
getSnippetManagerInstance(),
getThemeManagerInstance(),
getTokenColorsInstance(),
getWorkspaceInstance(),
)
this._editor.autoFocus = false
this._editor.onNeovimQuit.subscribe(() => {
// TODO:
// Maybe add an achievement for 'quitting vim'?
// Close current buffer / tab?
alert("quit!")
})
this._initPromise = this._editor.init([], {
loadInitVim: false,
})
this._tutorialGameplayManager = new TutorialGameplayManager(this._editor)
this._tutorialGameplayManager.onStateChanged.subscribe(state => {
this._lastTutorialState = state
this._onStateChangedEvent.dispatch({
tutorialState: state,
completionInfo: this._completionInfo,
mode: this._editor.mode,
notes: this._notes,
})
if (state.activeGoalIndex !== this._lastStage) {
this._lastStage = state.activeGoalIndex
if (this._element) {
const cursor = this._element.getElementsByClassName("cursor")
if (cursor.length > 0) {
const cursorElement = cursor[0]
const position = cursorElement.getBoundingClientRect()
this._spawnParticles("white", { x: position.left, y: position.top })
}
}
}
})
this._tutorialGameplayManager.onCompleted.subscribe(() => {
this._completionInfo = this._gameTracker.end()
this._onStateChangedEvent.dispatch({
tutorialState: this._lastTutorialState,
completionInfo: this._completionInfo,
mode: "normal",
notes: this._notes,
})
this._tutorialManager.notifyTutorialCompleted(this._currentTutorialId, {
time: this._completionInfo.timeInMilliseconds,
keyPresses: this._completionInfo.keyPresses,
})
if (this._element) {
const bounds = this._element.getBoundingClientRect()
const blue = "rgb(97, 175, 239)"
for (let i = 0; i < 8; i++) {
this._spawnParticles(
blue,
{
x: bounds.left + Math.random() * bounds.width,
y: bounds.top + Math.random() * bounds.height,
},
{ x: 300, y: 150 },
)
}
}
})
}
public handleInput(key: string): boolean {
if (this._completionInfo.completed) {
const nextTutorial = this._tutorialManager.getNextTutorialId(this._currentTutorialId)
if (key === "<space>") {
this.startTutorial(this._currentTutorialId)
} else if (nextTutorial && key === "<enter>") {
this.startTutorial(nextTutorial)
} else {
// No tutorial left - we'll pass through
return false
}
} else {
this._editor.input(key)
this._gameTracker.addKeyPress(1)
}
return true
}
public render(context: Oni.BufferLayerRenderContext): JSX.Element {
return (
<TutorialBufferLayerView
editor={this._editor}
renderContext={context}
onStateChangedEvent={this._onStateChangedEvent}
innerRef={elem => (this._element = elem)}
onWillUnmount={() => this.stop()}
/>
)
}
public async startTutorial(tutorialId: string): Promise<void> {
await this._initPromise
this._completionInfo = DefaultCompletionInfo
this._currentTutorialId = tutorialId
const tutorial = this._tutorialManager.getTutorial(tutorialId)
if (!this._hasAddedLayer) {
this._editor.activeBuffer.addLayer(
new GameplayBufferLayer(this._tutorialGameplayManager),
)
this._hasAddedLayer = true
}
this._notes = tutorial.notes || []
await this._editor.activeBuffer.setCursorPosition(0, 0)
await this._editor.neovim.command("stopinsert")
this._tutorialGameplayManager.start(tutorial, this._editor.activeBuffer)
this._gameTracker.start()
windowManager.focusSplit("oni.window.0")
}
public stop(): void {
this._tutorialGameplayManager.stop()
}
private _spawnParticles(
color: string,
position: Vector,
velocityVariance: Vector = { x: 100, y: 50 },
): void {
const particles = getInstance()
if (!particles || !this._element) {
return
}
particles.createParticles(25, {
Position: position,
PositionVariance: { x: 10, y: 10 },
Velocity: { x: 0, y: -150 },
VelocityVariance: { x: 100, y: 50 },
Color: color,
StartOpacity: 1,
EndOpacity: 0,
Time: 1,
})
}
}
export interface ITutorialBufferLayerViewProps {
renderContext: Oni.BufferLayerRenderContext
editor: NeovimEditor
onStateChangedEvent: IEvent<IGameplayStateChangedEvent>
innerRef: (elem: HTMLElement) => void
onWillUnmount: () => void
}
export interface ITutorialBufferLayerState {
tutorialState: ITutorialState
completionInfo: IGameplayCompletionInfo
mode: string
notes: JSX.Element[]
}
const TutorialWrapper = withProps<{}>(styled.div)`
position: relative;
width: 100%;
height: 100%;
background-color: ${p => p.theme["editor.background"]};
color: ${p => p.theme["editor.foreground"]};
overflow: auto;
pointer-events: all;
display: flex;
flex-direction: row;
`
const TutorialContentsWrapper = styled.div`
flex: 1 1 auto;
min-width: 600px;
max-width: 1000px;
margin-left: 2em;
display: flex;
flex-direction: column;
`
const TutorialNotesWrapper = styled.div`
flex: 0 0 auto;
width: 250px;
border-left: 1px solid rgba(255, 255, 255, 0.2);
margin: 3em 0em;
display: flex;
flex-direction: column;
`
const TutorialSectionWrapper = styled.div`
width: 75%;
max-width: 1000px;
flex: 0 0 auto;
`
const MainTutorialSectionWrapper = styled.div`
flex: 1 1 auto;
width: 100%;
height: 100%;
min-height: 275px;
display: flex;
align-items: center;
`
const PrimaryHeader = styled.div`
padding-top: 1em;
font-size: 2em;
`
const SubHeader = styled.div`
font-size: 1.6em;
`
const SectionHeader = styled.div`
font-size: 1.1em;
font-weight: bold;
`
const Section = styled.div`
padding-top: 1em;
padding-bottom: 2em;
`
const DescriptionWrapper = styled.div`
display: none;
@media (min-height: 800px) {
display: block;
}
`
export interface IModeStatusBarItemProps {
mode: string
}
const ModeStatusBarItem = withProps<IModeStatusBarItemProps>(styled.div)`
background-color: ${p => p.theme["highlight.mode." + p.mode + ".background"]};
color: ${p => p.theme["highlight.mode." + p.mode + ".foreground"]};
text-transform: uppercase;
height: 2em;
line-height: 2em;
padding: 0px 4px;
`
export class TutorialBufferLayerView extends React.PureComponent<
ITutorialBufferLayerViewProps,
ITutorialBufferLayerState
> {
constructor(props: ITutorialBufferLayerViewProps) {
super(props)
this.state = {
tutorialState: {
goals: [],
activeGoalIndex: -1,
metadata: null,
},
completionInfo: {
completed: false,
keyPresses: -1,
timeInMilliseconds: -1,
},
mode: "normal",
notes: [],
}
}
public componentDidMount(): void {
this.props.onStateChangedEvent.subscribe(newState => {
this.setState({
...newState,
})
})
}
public componentWillUnmount(): void {
this.props.onWillUnmount()
}
public render(): JSX.Element {
if (!this.state.tutorialState || !this.state.tutorialState.metadata) {
return null
}
const title = this.state.tutorialState.metadata.name
const description = this.state.tutorialState.metadata.description
const activeIndex = this.state.tutorialState.activeGoalIndex
const goalsWithIndex = this.state.tutorialState.goals
.map((goal, idx) => ({
goalIndex: idx,
goal,
}))
.filter(gi => !!gi.goal)
let postActiveIndex = goalsWithIndex.findIndex(f => f.goalIndex === activeIndex)
if (this.state.completionInfo.completed) {
postActiveIndex = goalsWithIndex.length
}
const goalsToDisplay = goalsWithIndex.map((goal, postIndex) => {
const isCompleted = postActiveIndex > postIndex
let visible = false
if (postActiveIndex === 0) {
visible = postIndex < 3
} else if (postActiveIndex > goalsWithIndex.length - 3) {
visible = goalsWithIndex.length - postIndex <= 3
} else {
visible = Math.abs(postIndex - postActiveIndex) < 2
}
return (
<GoalView
completed={isCompleted}
description={goal.goal}
active={goal.goalIndex === activeIndex}
visible={visible}
/>
)
})
const isFlipped = this.state.completionInfo.completed
return (
<TutorialWrapper>
<TutorialContentsWrapper>
<TutorialSectionWrapper>
<PrimaryHeader>Tutorial</PrimaryHeader>
<SubHeader>{title}</SubHeader>
</TutorialSectionWrapper>
<MainTutorialSectionWrapper>
<div
style={{
width: "75%",
height: "90%",
boxShadow: "3px 7px 10px 7px rgba(0, 0, 0, 0.2)",
}}
ref={this.props.innerRef}
>
<FlipCard
isFlipped={isFlipped}
front={
<div
style={{
width: "100%",
height: "100%",
display: "flex",
flexDirection: "column",
}}
>
<div
style={{
width: "100%",
height: "100%",
flex: "1 1 auto",
}}
>
{this.props.editor.render()}
</div>
<div style={{ flex: "0 0 auto" }}>
<StatusBar
items={[
{
alignment: 0,
contents: <div />,
id: "tutorial.null",
priority: 0,
},
{
alignment: 1,
contents: (
<ModeStatusBarItem
mode={this.state.mode}
>
{this.state.mode}
</ModeStatusBarItem>
),
id: "tutorial.mode",
priority: 0,
},
]}
fontFamily={configuration.getValue("ui.fontFamily")}
fontSize={configuration.getValue("ui.fontSize")}
enabled={!isFlipped}
/>
</div>
</div>
}
back={
isFlipped ? (
<CompletionView
keyStrokes={this.state.completionInfo.keyPresses}
time={this.state.completionInfo.timeInMilliseconds}
/>
) : null
}
/>
</div>
</MainTutorialSectionWrapper>
<TutorialSectionWrapper>
<DescriptionWrapper>
<SectionHeader>Description:</SectionHeader>
<Section>{description}</Section>
</DescriptionWrapper>
<SectionHeader>Goals:</SectionHeader>
<Section style={{ height: "200px" }}>
<div>{goalsToDisplay}</div>
</Section>
<Section />
</TutorialSectionWrapper>
</TutorialContentsWrapper>
<TutorialNotesWrapper>
<div style={{ textAlign: "center" }}>
<Bold>Notes:</Bold>
</div>
<div>{this.state.notes}</div>
</TutorialNotesWrapper>
</TutorialWrapper>
)
}
}