GridLayout from IdentityDictionary

hey, im currently working on an object prototype and storing keys and knobs in an IdentityDictionary. I would like to create a GridLayout or VLayout / HLayout with these.

The following code does create the desired result of an 4 row x 3 column knob matrix.

(
var knobs = Array.fill(12, { Knob.new() });
var layout = GridLayout.rows(*knobs.clump(3));
var window = Window.new().layout = layout;
window.front;
)

grafik

however if i create a sorted array from the IdentityDictionary:

~dict = ~modMatrix.paramViews;
-> IdentityDictionary[ (mod1_1 -> a Knob), (mod2_3 -> a Knob), (mod1_0 -> a Knob), (mod1_3 -> a Knob), (mod2_1 -> a Knob), 
  (mod0_1 -> a Knob), (mod0_2 -> a Knob), (mod0_3 -> a Knob), (mod0_0 -> a Knob), (mod2_2 -> a Knob), 
  (mod2_0 -> a Knob), (mod1_2 -> a Knob) ]

~array = ~dict.asSortedArray;
-> [ [ mod0_0, a Knob ], [ mod0_1, a Knob ], [ mod0_2, a Knob ], [ mod0_3, a Knob ], [ mod1_0, a Knob ], [ mod1_1, a Knob ], [ mod1_2, a Knob ], [ mod1_3, a Knob ], [ mod2_0, a Knob ], [ mod2_1, a Knob ], [ mod2_2, a Knob ], [ mod2_3, a Knob ] ]

and collect the knobs:

~knobs = 3.collect{ |n|
	4.collect{ |k|
		var knob;
		knob = ~array[n+k][1];
	};
}.flat;

-> [ a Knob, a Knob, a Knob, a Knob, a Knob, a Knob, a Knob, a Knob, a Knob, a Knob, a Knob, a Knob ]

and then run:

(
var layout = GridLayout.rows(*~knobs.clump(3));
var window = Window.new().layout = layout;
window.front;
)

i get a really different result:
grafik

and when i evaluate it again, even no knobs at all:
grafik

Could somebody help me out with that? many thanks :slight_smile:

What is ~modMatrix?

And some more characters

~modMatrix is a ProtoDef / Environment with some methods, one is ~makeParamViews which puts keys and corresponding knobs in an Identitydictionary called paramViews to be able to fetch and update them later on.

I have figured out that, If i use knobs = self.paramViews.values; instead of the .asSortedArray dictionary method i get the desired 4 row x 3 column knob matrix.

But the knobs arent sorted, which you can see if i update the first column of knobs [\mod0_0, \mod0_1, \mod0_2, \mod0_3]; with a random number:

grafik

(
~parameterChanged = { |key, val|
	var spec = ~modMatrix.params[key].value;
	~modMatrix.paramViews[key].value_(spec.unmap(val));
};

~nodeProxyChanged = { |...args|
	args.pairsDo{ |paramKey, val|
		~parameterChanged.(paramKey, val)
	};
};

{
	~nodeProxyChanged.(
		\mod0_0, rrand(0.0, 1.0),
		\mod0_1, rrand(0.0, 1.0),
		\mod0_2, rrand(0.0, 1.0),
		\mod0_3, rrand(0.0, 1.0),
	);

}.defer;
)

i think its just about figuring out the correct method for getting an ordered array of the knobs from the IdentityDictionary, but have tried for some hours now and couldnt figure it out, so every help is very much appreciated :slight_smile:

probably there is a better way, but using Order and .sortedKeyValuesDo does work:

self.knobs = Order[];

self.paramViews.sortedKeysValuesDo{ |key, knob|
	self.knobs.add(knob);
};

layout = GridLayout.columns(*self.knobs.clump(self.numInputs));

here correctly updating [\mod0_0, \mod0_1, \mod0_2, \mod0_3]; with a random number:
grafik

I have also found:

d = Dictionary[\a -> 5, \b -> 7, \c -> 1, \d -> 0];
d.order;
d.atAll(d.order);

i guess thats it :slight_smile: