[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sc-devel] Transparent backgrounds in Views




Am 01.12.2007 um 21:14 schrieb Stephan Wittwer:



For example to allow for 2 or more multisliderviews to be on top
of each other but the top one would have transparent background.



There is one problem at the moment:

Ordering of layers is different in cocoa versus swing.




/*
GUI.cocoa
GUI.swing
*/
(
var keys, colors, data;
var sortedKeys, activeKey, activate;
var activeKeyLast, activeKeyFirst; // cocoa v/s swing
var gui;
var win, dec, pb, mainContainer, msvContainer, buttonsContainer;
var makeMSVcontainer, makeSlider, makeSliders, calcWidths;
var buttons;
var infoButton;

keys = [\red, \green, \other, \yellow]; // ordered for buttons, printing
sortedKeys = keys.copy; // init

colors = IdentityDictionary [
	\red -> { Color.red },
	\green -> { Color.green },
	\other -> { Color(0.3, 0.3, 0.3) },
	\yellow -> { Color.yellow }
];

data = IdentityDictionary [
	\red -> [0, 0.25, 0.5, 0.1, 0],
	\green -> Array.fill(12, { 1.0.rand }),
	\other -> Array.geom(22, 1, 0.001.pow(21.reciprocal)),
	\yellow -> Array.rand(150, 0.0, 1.0) // not too many, please
];


activate = { |key|
	activeKey = key;
	("\nactive key : %\n").postf(activeKey);
};

activeKeyLast = { // cocoa
	sortedKeys.remove(activeKey);
	sortedKeys.add(activeKey);
};
activeKeyFirst = {  // swing
	sortedKeys.remove(activeKey);
	sortedKeys.addFirst(activeKey);
};

	// GUI
gui = GUI.current;	
win = gui.window.new("layered horizontal multi sliders");
pb = win.view.bounds;
win.view.decorator = dec = FlowLayout.new(pb, 20 @ 20);

mainContainer = gui.compositeView.new(win, pb.width - ( 2 * dec.margin.x ) @ 300);

makeMSVcontainer = {
	msvContainer = gui.compositeView.new(
		mainContainer,
mainContainer.bounds.insetAll(0, 0, 0, mainContainer.bounds.height * 0.2)
	);
};
makeMSVcontainer.value; // init

buttonsContainer = gui.hLayoutView.new(
	mainContainer,
mainContainer.bounds.insetAll(0, mainContainer.bounds.height * 0.8 + 4, 0, 0)
);

calcWidths = { |msv|
	var usedPx, ratio = 2, unit;
	usedPx = msv.bounds.width - 2;
	unit = usedPx / ((1 + ratio) * msv.value.size - 1);
	
		// b/c of cocoa
	if (unit >= 1.0){
		msv.indexThumbSize = ratio * unit;
		msv.gap = unit;
	}{
		msv.gap = 0;
		msv.indexThumbSize = usedPx / msv.value.size;
	};
};
makeSlider = { |color, datum|
	var msv;
	msv = gui.multiSliderView.new(msvContainer, msvContainer.bounds);
	msv.value = datum;
	calcWidths.value(msv);
	msv.fillColor = color;
	msv.strokeColor = Color.clear;
};

makeSliders = {
	sortedKeys.collect { |key, i| var color, datum, slider;
		color = gui.id.switch(
			\cocoa, {
				if (i === (keys.size - 1)) {
					colors[key].value }{
					colors[key].value.alpha_(0.4)
				}			
			},
			\swing, {
				if (i === 0){ colors[key].value }{ colors[key].value.alpha_(0.4) };
			}
		);
		datum = data[key];
		slider = makeSlider.value(color, datum);
		slider.mouseUpAction = { |view|
			data[key] = view.value; // update data
			("index %\n").postf(view.index);
		};
	};
};
makeSliders.value; // init

buttons = keys.collect { |key, i|
	var button, width;
	width =
		(buttonsContainer.bounds.width - ( (keys.size - 1) * 4)) / keys.size;
	button = gui.button.new(buttonsContainer, width);
	button.states = [["", Color.black, colors[key].value]];
};
	// buttons actions
buttons.do { |each, i|
	each.action = { |view|		
		if (keys[i] != activeKey) {
			activate.value(keys[i]);
			case { gui.id === 'cocoa' }{ activeKeyLast.value; }{
				gui.id === 'swing' }{ activeKeyFirst.value; };
			msvContainer.remove;
			makeMSVcontainer.value;
			makeSliders.value;
		};
	};
};

infoButton = gui.button.new(win, pb.width - (2 * dec.margin.x) @ 20);
infoButton.states = [["post data"]];
infoButton.action = { |view|
	keys.do { |key, i|
		Post << Char.nl << key << Char.tab << data[key] << Char.nl;
	};
};
win.front;
)