File: dataview.bs

package info (click to toggle)
storm-lang 0.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,836 kB
  • sloc: ansic: 261,420; cpp: 138,870; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (449 lines) | stat: -rw-r--r-- 8,964 bytes parent folder | download | duplicates (4)
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
use ui;
use graphics;
use core:geometry;
use progvis:data;


/**
 * A view of some data.
 */
class DataView extends WithBorder {}


/**
 * Unknown data.
 */
class UnknownView extends DataView {
	private Text text;
	private Data? data;

	init(Data? data) {
		init {
			text("?", dataFont);
			data = data;
		}

		size = text.size + dataBorder*2;
	}

	void draw(Graphics g, Bool active) : override {
		super:draw(g, active);
		g.draw(text, dataFg, pos + dataBorder);
	}

	void drawLinks(Graphics g, Bool active) : override {}

	Data? srcData() : override { data; }

	void update(Workspace:Update ws) : override {
		if (data)
			ws.setVisual(data, this);
	}
}

/**
 * Padding element.
 *
 * Zero-size element that can be used as a target for pointers. One example where this is useful is
 * for pointers to the end of an array in C/C++.
 */
class PadView extends Drawable {
	private Pad data;

	init(Pad data) {
		init { data = data; }
	}

	void draw(Graphics g, Bool active) : override {}
	void drawLinks(Graphics g, Bool active) : override {}
	Data? srcData() : override { data; }
	void update(Workspace:Update ws) : override {
		ws.setVisual(data, this);
	}
	Size computeSize() : override { Size(); }
}

/**
 * View of some atomic data.
 *
 * Highlights reads and writes as appropriate.
 */
class AtomView extends DataView {
	// Status of the memory.
	MemAccess access;

	// Update.
	void update(Workspace:Update ws) : override {
		if (x = srcData as Atom)
			access = x.access;
	}

	// Draw. We draw the entire background ourselves, otherwise we would draw over the borders drawn
	// by DataView.
	void draw(Graphics g, Bool active) : override {
		Rect r = rect;

		if (access.read > 0 & access.write > 0) {
			Float center = r.center.x;
			g.fill(Rect(r.p0, Point(center, r.p1.y)), readColor);
			g.fill(Rect(Point(center, r.p0.y), r.p1), writeColor);
		} else if (access.read > 0) {
			g.fill(r, readColor);
		} else if (access.write > 0) {
			g.fill(r, writeColor);
		} else {
			g.fill(r, dataBg);
		}

		if (access.error) {
			g.line(r.p0, r.p1, dataError);
			g.line(Point(r.p0.x, r.p1.y), Point(r.p1.x, r.p0.y), dataError);
		}

		g.draw(r, dataFg);
	}

	// Read color.
	private Brush readColor() {
		if (access.read > 1)
			dataReadBg;
		else
			dataReadFaint;
	}

	// Write color.
	private Brush writeColor() {
		if (access.write > 1)
			dataWriteBg;
		else
			dataWriteFaint;
	}
}

/**
 * Primitive of some sort.
 */
class PrimitiveView extends AtomView {
	init(Primitive data) {
		init {
			data = data;
		}
	}

	private Primitive data;
	private Str lastValue;
	private Text? text;

	Data? srcData() : override { data; }

	void update(Workspace:Update ws) : override {
		ws.setVisual(data, this);
		super:update(ws);
		update();
	}

	private void update() {
		if (lastValue != data.value) {
			lastValue = data.value;
			text = Text(lastValue, dataFont);

			invalidateSize();
		}
	}

	void draw(Graphics g, Bool active) : override {
		super:draw(g, active);
		if (text) {
			Size margin = dataMargin;
			g.draw(text, dataFg, Point(pos.x + size.w - text.size.w - margin.w, pos.y + margin.h));
		}
	}

	void drawLinks(Graphics g, Bool active) : override {}

	protected Size computeSize() : override {
		if (text)
			text.size + dataMargin*2;
		else
			dataMargin*2;
	}
}

// Arrow for all pointers.
package Path ptrArrow on Render = {
	Path p;
	p.start(Point(0, 0));
	p.line(Point(-6, -4));
	p.line(Point(-6, 4));
	p.close();
	p;
};

/**
 * View of a pointer.
 */
class PointerView extends AtomView {
	init(Pointer data) {
		init {
			data = data;
		}

		size = ptrSize + dataMargin*2;
	}

	private Pointer data;
	Drawable? target;
	private Path line;

	Data? srcData() : override { data; }

	void update(Workspace:Update ws) : override {
		ws.setVisual(data, this);
		super:update(ws);
		if (to = data.to) {
			target = ws.find(this, to);
		} else {
			target = null;
		}
	}

	void draw(Graphics g, Bool active) : override {
		super:draw(g, active);
		if (target.empty) {
			Rect r = rect;
			g.line(rect.p0, rect.p1, activeLink);
		}
	}

	void drawLinks(Graphics g, Bool active) : override {
		if (target) {
			Point to = target.pos;

			Brush color = if (active) { activeLink; } else { inactiveLink; };
			Rect r = rect;
			Float dy = abs(r.center.y - to.y);
			Float shift = max(0.2*dy, 40);
			line.clear();
			line.start(r.center);
			line.bezier(to - Point(shift, 0), to);
			g.draw(line, color);

			g.push();
			g.transform(translate(to));
			g.fill(ptrArrow, color);
			g.pop();
		}
	}
}


/**
 * Composite data.
 */
class CompositeView extends DataView {
	init(Composite data) {
		init {
			data = data;
		}
	}

	private Composite data;

	Data? srcData() : override { data; }

	void draw(Graphics g, Bool active) : override {
		super:draw(g, active);
		Float width = rect.size.w;
		Float xBorder = compositeBorder.w;
		Float xBase = pos.x + xBorder;

		for (v in content) {
			Float y = pos.y + v.yPos;

			if (v.line) {
				g.line(Point(pos.x, y), Point(pos.x + width, y), dataFg);
			}

			y += dataBorder.h;

			// Align the text a bit nicer on the y-axis, to line up the baselines...
			g.draw(v.name, dataFg, Point(xBase, y + dataBorder.h));
			v.data.pos = Point(xBase + width - v.data.size.w - xBorder*2, y);
			// v.data.pos = Point(xBase + namesWidth + dataWidth - v.data.size.w, y);
			v.data.draw(g, false);
		}
	}

	void drawLinks(Graphics g, Bool active) : override {
		for (v in content) {
			v.data.drawLinks(g, active);
		}
	}

	void update(Workspace:Update ws) {
		ws.setVisual(data, this);
		for (id, part in data.parts) {
			if (id >= content.count) {
				content << Variable(part.name, ws.create(part.value), part.first);
			} else {
				if (content[id].data.srcData !is part.value)
					content[id].data = ws.create(part.value);

				if (content[id].name.text != part.name)
					content[id].name = Text(part.name, variableFont);
			}
			content[id].data.update(ws);
		}
		while (content.count > data.parts.count)
			content.pop();

		invalidateSize();
	}

	Float maxNameWidth() { namesWidth; }
	Float maxDataWidth() { dataWidth; }

private:

	class Variable on Render {
		Text name;
		Drawable data;
		Bool line;
		Float yPos;

		init(Str name, Drawable data, Bool line) {
			init {
				name = Text(name, variableFont);
				data = data;
				line = line;
			}
		}
	}

	Variable[] content;
	Float namesWidth;
	Float dataWidth;
	Float height;

	Size computeSize() : override {
		if (content.any)
			content[0].line = false;

		// TODO: It would probably be nice to recurse into
		// the values that are themselves variables and render them
		// more compactly.
		namesWidth = 0;
		dataWidth = 0;
		height = 0;
		for (v in content) {
			var nameSz = v.name.size;
			var dataSz = v.data.size;

			if (v.line)
				height += dataBorder.h;

			v.yPos = height;

			namesWidth = max(namesWidth, nameSz.w + dataBorder.w);
			dataWidth = max(dataWidth, dataSz.w);
			height += max(nameSz.h, dataSz.h) + dataBorder.h;
		}

		height -= dataBorder.h;
		Size(namesWidth + dataWidth, height) + compositeBorder*2;
	}
}

/**
 * Array data.
 */
class ArrayView extends DataView {
	init(Array data) {
		init {
			data = data;
		}
	}

	private Array data;

	Data? srcData() : override { data; }

	void draw(Graphics g, Bool active) : override {
		super:draw(g, active);
		Float width = rect.size.w;
		Float xBorder = compositeBorder.w;
		Point at = pos + compositeBorder;

		for (v in content) {
			var nameSz = v.text.size;
			var dataSz = v.value.size;

			// Align the text a bit nicer on the y-axis, to line up the baselines...
			g.draw(v.text, dataFg, at + Point(0, dataBorder.h));

			v.value.pos = at + Point(width - dataSz.w - xBorder*2, 0);
			// v.value.pos = at + Point(namesWidth + dataWidth - dataSz.w, 0);
			v.value.draw(g, false);

			at.y += max(nameSz.h, dataSz.h) + dataBorder.h;
		}
	}

	void drawLinks(Graphics g, Bool active) : override {
		for (v in content) {
			v.value.drawLinks(g, active);
		}
	}

	void update(Workspace:Update ws) {
		ws.setVisual(data, this);

		for (id, elem in data.contents) {
			if (id >= content.count) {
				content << Element(id, ws.create(elem));
			}
			// Note: Types don't change.
			content[id].value.update(ws);
		}
		while (content.count > data.contents.count)
			content.pop();

		invalidateSize();
	}

private:

	class Element on Render {
		Text text;
		Drawable value;

		init(Nat id, Drawable value) {
			init() {
				text = Text(id.toS + ":", variableFont);
				value = value;
			}
		}
	}

	Element[] content;
	Float namesWidth;
	Float dataWidth;
	Float height;

	Size computeSize() : override {
		namesWidth = 0;
		dataWidth = 0;
		height = 0;
		for (v in content) {
			var nameSz = v.text.size;
			var dataSz = v.value.size;

			namesWidth = max(namesWidth, nameSz.w + dataBorder.w);
			dataWidth = max(dataWidth, dataSz.w);
			height += max(nameSz.h, dataSz.h) + dataBorder.h;
		}

		height -= dataBorder.h;
		Size(namesWidth + dataWidth, height) + compositeBorder*2;
	}
}