Skip to content

Commit f4b375b

Browse files
authored
reducing size (#402)
* remove guts of Component * fix compile errors * a few more errors * remove destroy logic * more mods
1 parent 7ec4a40 commit f4b375b

File tree

7 files changed

+41
-243
lines changed

7 files changed

+41
-243
lines changed

button.ts

+10-59
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,13 @@ namespace microcode {
6767
return borderTop(style) + borderBottom(style)
6868
}
6969

70-
export class Button extends Component implements ISizable, IPlaceable {
70+
export class Button implements IComponent, ISizable, IPlaceable {
7171
private xfrm_: Affine
7272
private icon: Sprite
73-
//private text: TextSprite;
7473
private style: ButtonStyle
7574
private iconId: string | Image
7675
private _ariaId: string
7776
public onClick?: (button: Button) => void
78-
private bounds_: Bounds
7977

8078
//% blockCombine block="xfrm" callInDebugger
8179
public get xfrm() {
@@ -112,7 +110,12 @@ namespace microcode {
112110

113111
public get bounds() {
114112
// Returns bounds in local space
115-
return this.bounds_
113+
// This isn't quite right, but it's close enough for now
114+
return Bounds.GrowXY(
115+
this.icon.bounds,
116+
borderLeft(this.style),
117+
borderTop(this.style)
118+
)
116119
}
117120

118121
public get rootXfrm(): Affine {
@@ -132,7 +135,6 @@ namespace microcode {
132135
y: number
133136
onClick?: (button: Button) => void
134137
}) {
135-
super("button")
136138
this.xfrm_ = new Affine()
137139
this.xfrm.parent = opts.parent && opts.parent.xfrm
138140
this.style = opts.style || ButtonStyles.Transparent
@@ -144,16 +146,6 @@ namespace microcode {
144146
this.buildSprite()
145147
}
146148

147-
destroy() {
148-
if (this.icon) {
149-
this.icon.destroy()
150-
}
151-
//if (this.text) { this.text.destroy(); }
152-
this.icon = undefined
153-
//this.text = undefined;
154-
super.destroy()
155-
}
156-
157149
public getIcon() {
158150
return this.iconId
159151
}
@@ -171,10 +163,6 @@ namespace microcode {
171163
}
172164

173165
private buildSprite() {
174-
if (this.icon) {
175-
this.icon.destroy()
176-
}
177-
//if (this.text) { this.text.destroy(); }
178166
this.icon = new Sprite({
179167
parent: this,
180168
img:
@@ -183,15 +171,6 @@ namespace microcode {
183171
: this.iconId,
184172
})
185173
this.icon.xfrm.parent = this.xfrm
186-
//this.icon.xfrm.localPos.x = borderLeft(this.style);
187-
//this.icon.xfrm.localPos.y = borderTop(this.style);
188-
189-
// This isn't quite right, but it's close enough for now
190-
this.bounds_ = Bounds.GrowXY(
191-
this.icon.bounds,
192-
borderLeft(this.style),
193-
borderTop(this.style)
194-
)
195174
}
196175

197176
public occlusions(bounds: Bounds) {
@@ -224,46 +203,18 @@ namespace microcode {
224203
}
225204
}
226205

227-
hover(hov: boolean) {
228-
/*
229-
if (hov && this.text) { return; }
230-
if (!hov && !this.text) { return; }
231-
if (!this.label) { return; }
232-
if (!this.visible()) { return; }
233-
if (hov) {
234-
this.text = textsprite.create(this.label, 1, 15);
235-
this.text.setBorder(1, 15);
236-
this.text.x = this.x;
237-
this.text.y = this.y - this.height;
238-
this.text.z = this.icon.z;
239-
} else {
240-
this.text.destroy();
241-
this.text = undefined;
242-
}
243-
*/
244-
}
206+
hover(hov: boolean) { }
245207

246-
/* override */ update() {
247-
/*
248-
if (this.text) {
249-
this.text.x = this.x;
250-
this.text.y = this.y - this.height;
251-
}
252-
*/
253-
}
208+
update() { }
254209

255210
isOffScreenX(): boolean {
256211
return this.icon.isOffScreenX()
257212
}
258213

259-
/* override */ draw() {
214+
draw() {
260215
control.enablePerfCounter()
261216
this.drawStyle()
262217
this.drawIcon()
263-
//const iconbounds = Bounds.Translate(this.icon.bounds, this.icon.xfrm.worldPos);
264-
//iconbounds.drawRect(5);
265-
//const mybounds = Bounds.Translate(this.bounds, this.xfrm.worldPos);
266-
//mybounds.drawRect(14)
267218
}
268219

269220
private drawIcon() {

component.ts

+4-51
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,8 @@
11
namespace microcode {
2-
let id_sequence = 0
32

4-
export type ComponentHandler = (comp: Component) => void
5-
6-
export interface IKindable {
7-
kind: string
8-
}
9-
10-
export abstract class Component implements IKindable {
11-
private id_: number
12-
private data_: any
13-
private _destroyHandlers: ComponentHandler[]
14-
private _destroyed = false
15-
16-
//% blockCombine block="id" callInDebugger
17-
get id() {
18-
return this.id_
19-
}
20-
get data(): any {
21-
if (!this.data_) {
22-
this.data_ = {}
23-
}
24-
return this.data_
25-
}
26-
get destroyed(): boolean {
27-
return this._destroyed
28-
}
29-
30-
constructor(public kind: string) {
31-
this.id_ = id_sequence++
32-
}
33-
34-
onDestroy(handler: ComponentHandler) {
35-
this._destroyHandlers = this._destroyHandlers || []
36-
this._destroyHandlers.push(handler)
37-
}
38-
39-
/* virtual */ destroy() {
40-
if (this._destroyed) console.warn(`double destroy on ${this.id}`)
41-
this._destroyed = true
42-
const handlers = this._destroyHandlers || []
43-
this._destroyHandlers = undefined
44-
for (const handler of handlers) {
45-
handler(this)
46-
}
47-
this.data_ = undefined
48-
}
49-
50-
/* abstract */ update() {}
51-
/* abstract */ draw() {}
3+
export interface IComponent {
4+
update: () => void
5+
draw: () => void
526
}
537

548
export interface IPlaceable {
@@ -60,14 +14,13 @@ namespace microcode {
6014
height: number
6115
}
6216

63-
export class Placeable extends Component implements IPlaceable {
17+
export class Placeable implements IPlaceable {
6418
private xfrm_: Affine
6519
//% blockCombine block="xfrm" callInDebugger
6620
public get xfrm() {
6721
return this.xfrm_
6822
}
6923
constructor(parent?: IPlaceable) {
70-
super("placeable")
7124
this.xfrm_ = new Affine()
7225
this.xfrm_.parent = parent && parent.xfrm
7326
}

cursor.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace microcode {
1616
size: Bounds
1717
}
1818

19-
export class Cursor extends Component implements IPlaceable {
19+
export class Cursor implements IComponent, IPlaceable {
2020
xfrm: Affine
2121
navigator: INavigator
2222
cancelHandlerStack: CursorCancelHandler[]
@@ -29,7 +29,6 @@ namespace microcode {
2929
visible = true
3030

3131
constructor() {
32-
super("cursor")
3332
this.xfrm = new Affine()
3433
this.cancelHandlerStack = []
3534
this.moveDest = new Vec2()
@@ -105,14 +104,7 @@ namespace microcode {
105104
return false
106105
}
107106

108-
/* override */ destroy() {
109-
this.navigator = undefined
110-
super.destroy()
111-
}
112-
113-
/* override */ update() {
114-
super.update()
115-
107+
update() {
116108
const currTimeMs = control.millis()
117109
const elapsedTimeMs = currTimeMs - this.moveStartMs
118110

@@ -130,7 +122,7 @@ namespace microcode {
130122
this.cycle = currTimeMs % 1000 < 500 ? 1 : 0
131123
}
132124

133-
/* override */ draw() {
125+
draw() {
134126
control.enablePerfCounter()
135127
if (!this.visible) return
136128

0 commit comments

Comments
 (0)