File: JITGui.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (207 lines) | stat: -rw-r--r-- 4,743 bytes parent folder | download | duplicates (6)
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
/***
	Abstract superclass for
	TdefGui,PdefGui,
	TdefAllGui, PdefAllGui
	EnvirGui,

	MonitorGui,
	NPGui, NdefGui,
	ProxyMixer (ProxySpaceGui),
	ParamGui,
	and possibly others to follow.

	common traits:
	an observed object (a proxy, monitor, space, envir, ...)
	a skipjack for watching it;
	a getState method for getting all the observed state
	a prevState instvar for keeping the old state around;
	a checkUpdate method for comparing old and new state,
		and updating the gui elements that have changed;

	parent, minimum bounds for its numItems, actual bounds;

	if the object has settings, make an EnvirGui or ParamGui
	ParamGui can do sliders or knobs ...

	Simpler ones: EnvirGui, TaskProxyGui;

***/

JITGui {
	var <object, <numItems, <parent, <bounds, <zone, <minSize, <defPos, <skin, <font;
	var <nameView, <csView;
	var <prevState, <skipjack, <scroller;
	var <config, <hasWindow = false;

	*initClass {
		Class.initClassTree(GUI);

		GUI.skins.put(\jit, (
			fontSpecs: 		["Helvetica", 12],
			fontColor: 		Color.black,
			background: 	Color(0.8, 0.85, 0.7, 0.5),
			foreground:		Color.grey(0.95),
			onColor:		Color(0.5, 1, 0.5),
			onColor2:   	Color(1.0, 0.5, 0.5),
			offColor:		Color.grey(0.8, 0.5),
			hiliteColor:	Color.green(1.0, 0.5),
			gap:			0 @ 0,
			margin: 		2@2,
			buttonHeight:	18,
			headHeight: 	24

		)
		);
	}

	*new { |object, numItems = (0), parent, bounds, makeSkip = true, options = #[]|
		^super.newCopyArgs(nil, numItems, parent, bounds)
			.init(makeSkip, options)
			.object_(object);
	}

	init { |makeSkip, options|

		skin = GUI.skins.jit;
		font = Font(*skin.fontSpecs);
		prevState = ();

		// calc bounds - at least minbounds
		this.setDefaults(options);
		this.calcBounds(options);

		if (parent.isNil) { this.makeWindow };
		this.makeZone;

			// then put all the other gui items on it
		this.makeViews(options);
			// and start watching
		if (makeSkip) { this.makeSkip };
	}

	accepts { |obj| ^true	}

	object_ { |obj|
		if (this.accepts(obj)) {
			object = obj;
			this.checkUpdate;
		} {
			"% : object % not accepted!".format(this.class, obj).warn;
		}
	}

	name_ { |name|
		if (hasWindow) { parent.name_(this.winName(name)) };
		if (nameView.notNil) {
			try { nameView.object_(object) };
			nameView.string_(name);
		};
	}

	hasName {
		^nameView.notNil and: { nameView.string.notNil }
	}

	getName { ^if (object.respondsTo(\key)) { object.key } { 'anon' } }
	winName { |name| ^this.class.name ++ $_ ++ (name ?? { this.getName }) }

	calcBounds {
		var defBounds;
		if(bounds.isKindOf(Rect)) {
			bounds.setExtent(max(bounds.width, minSize.x), max(bounds.height, minSize.y));
			^this
		};

		defBounds = Rect.fromPoints(defPos, defPos + minSize + (skin.margin + skin.margin));
		if (bounds.isNil) {
			bounds = defBounds;
			^this
		};

		if (bounds.isKindOf(Point)) {
			bounds = defBounds.setExtent(max(bounds.x, minSize.x), max(bounds.y, minSize.y));
		}
	}

	makeWindow {
		parent = Window(this.winName, bounds.copy.resizeBy(10, 10)).front;
		parent.addFlowLayout;
		hasWindow = true;
	}

	makeZone {
		zone = CompositeView(parent, bounds).background_(skin.background);
		zone.addFlowLayout(skin.margin, skin.gap);
		zone.resize_(2);
		zone.background_(skin.foreground);
	}

	moveTo { |h, v|
		if (hasWindow) { parent.bounds = parent.bounds.moveTo(h, v); }
	}

	close {
		if (hasWindow) { parent.close }
	}

	makeSkip {
		skipjack = SkipJack({
			this.checkUpdate
		},
		0.1,
		{ parent.isNil or: { parent.isClosed } },
		this.getName
		);
	}


		// these methods should be overridden in subclasses:
	setDefaults { |options|
		if (parent.isNil) {
			defPos = 10@260
		} {
			defPos = skin.margin;
		};
		minSize = 250 @ (numItems * skin.buttonHeight + skin.headHeight);
		//	"minSize: %\n".postf(minSize);
	}

	makeViews {
		var lineheight = max(
			skin.buttonHeight * numItems + skin.headHeight,
			zone.bounds.height)  - (skin.margin.y * 2);

		nameView = DragBoth(zone, Rect(0,0, 60, lineheight))
			.font_(font)
			.align_(\center)
			.receiveDragHandler_({ arg obj; this.object = View.currentDrag });

		csView = EZText(zone,
			Rect(0,0, bounds.width - 65, lineheight),
			nil, { |ez| object = ez.value; })
			.font_(font);
		csView.bounds;
	}

	getState {
		// get all the state I need to know of the object I am watching
		^(object: object)
	}

	checkUpdate {
		var newState = this.getState;

		// compare newState and prevState, update gui items as needed
		if (newState == prevState) { ^this };

		if (newState[\object] != prevState[\object]) {
			this.name_(this.getName);
			if (csView.textField.hasFocus.not) { csView.value_(object) };
		};
		prevState = newState;
	}

	makeScroller {
		// if numItems is exceeded, shift items along by some number with an EZScroller
	}
}