-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscene.ts
More file actions
160 lines (139 loc) · 4.9 KB
/
scene.ts
File metadata and controls
160 lines (139 loc) · 4.9 KB
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
namespace user_interface_base {
const INPUT_PRIORITY = 10
const UPDATE_PRIORITY = 20
const RENDER_PRIORITY = 30
const SCREEN_PRIORITY = 100
/**
* Top-level abstraction drawn to the screen.
* Extended by CursorScene when you want to have a GUI with Button support.
* Useful if you: don't want buttons, aren't making a GUI, or to make a more complex GUI.
* CursorScene overwrites a number of these abstract methods.
*/
export abstract class Scene implements IComponent {
private xfrm_: Affine
private color_: number
private backgroundCaptured_ = false
//% blockCombine block="xfrm" callInDebugger
public get xfrm() {
return this.xfrm_
}
//% blockCombine block="color" callInDebugger
public get backgroundColor() {
return this.color_
}
public set backgroundColor(v) {
this.color_ = v
}
constructor(public app?: AppInterface, public name?: string) {
this.xfrm_ = new Affine()
this.color_ = 12
}
/* abstract */ startup() {
if (Options.menuProfiling) {
context.onEvent(
ControllerButtonEvent.Pressed,
controller.menu.id,
() => {
control.heapSnapshot()
}
)
}
}
/* abstract */ shutdown() { }
/* override */ activate() {
profile()
}
/* override */ deactivate() {
profile()
}
/* abstract */ update() { }
/* abstract */ draw() { }
protected handleClick(x: number, y: number) { }
protected handleMove(x: number, y: number) { }
protected handleWheel(dx: number, dy: number) { }
get backgroundCaptured() {
return !!this.backgroundCaptured_
}
/**
* Captures the current screen image as background image. You must call releaseBackground to resume usual rendering.
*/
captureBackground() {
this.backgroundCaptured_ = true
}
releaseBackground() {
this.backgroundCaptured_ = false
}
__init() {
context.eventContext().registerFrameHandler(INPUT_PRIORITY, () => {
control.enablePerfCounter()
const dtms = (context.eventContext().deltaTime * 1000) | 0
controller.left.__update(dtms)
controller.right.__update(dtms)
controller.up.__update(dtms)
controller.down.__update(dtms)
})
// Setup frame callbacks.
context.eventContext().registerFrameHandler(UPDATE_PRIORITY, () => {
control.enablePerfCounter()
this.update()
})
context.eventContext().registerFrameHandler(RENDER_PRIORITY, () => {
control.enablePerfCounter()
// perf: render directly on the background image buffer
this.draw()
if (Options.fps)
Screen.image.print(context.EventContext.lastStats, 1, 1, 15)
if (screen() !== Screen.image)
screen().drawBitmap(Screen.image, 0, 0)
})
context.eventContext().registerFrameHandler(SCREEN_PRIORITY, () => {
control.enablePerfCounter()
control.__screen.stop()
control.__screen.update()
})
}
}
/**
* This is an wrapper around a stack of scenes.
* You can .push() and .pop() scenes to navigator your application.
* In a microbit App this SceneManager is setup in app.ts,
* which has a wrapper around SceneManager methods.
* The app object is normally passed as a reference between scenes,
* so that they can access this SceneManager.
*/
export class SceneManager {
scenes: Scene[]
constructor() {
this.scenes = []
}
public pushScene(scene: Scene) {
const currScene = this.currScene()
if (currScene) {
currScene.deactivate()
}
context.pushEventContext()
this.scenes.push(scene)
scene.startup()
scene.activate()
scene.__init()
}
public popScene() {
const prevScene = this.scenes.pop()
if (prevScene) {
prevScene.deactivate()
prevScene.shutdown()
context.popEventContext()
}
const currScene = this.currScene()
if (currScene) {
currScene.activate()
}
}
private currScene(): Scene {
if (this.scenes.length) {
return this.scenes[this.scenes.length - 1]
}
return undefined
}
}
}