Skip to content

Commit

Permalink
use ParamView in EnvirGui and NdefParamGui
Browse files Browse the repository at this point in the history
  • Loading branch information
adc committed Nov 18, 2015
1 parent 9a7058d commit 62a8dd1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 299 deletions.
11 changes: 7 additions & 4 deletions HelpSource/Classes/ParamView.schelp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ code::
w = Window("test", Rect(20, 820, 400, 100)).front;
w.addFlowLayout;
~pv = ParamView(w, Rect(20, 20, 360, 20));
~pv2 = ParamView(w, Rect(20, 20, 360, 40));

// not working properly yet
~pv.bounds_(Rect(20, 20, 360, 40));
~pv.bounds_(Rect(4, 4, 300, 40));

~pv.dump
~pv.viewType_(0); // EZNumber
Expand All @@ -27,10 +28,12 @@ w.addFlowLayout;

~pv.value_(200);
~pv.value_(2000);
// switch to EZRanger
// switches to EZRanger
~pv.value_([200, 2000]);
~pv.value_([20, 200, 2000]); // 3 numbers -> EZText
~pv.value_(\blonk); // anything else -> EZText
// 3 numbers -> switches to EZText
~pv.value_([20, 200, 2000]);
// anything else -> EZText
~pv.value_(\blonk);
~pv.action = { |pv| pv.value.postcs };

::
Expand Down
248 changes: 43 additions & 205 deletions SCClassLibrary/JITLib/GUI/EnvirGui.sc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// needs a defer for making new EZs after a clearField...
// get rid of this later if possible.

EnvirGui : JITGui {

var <valFields, <widgets, labelWidth;
var <paramViews, labelWidth;
var <keysRotation = 0, <specs;
var <protoBut, <parentBut, <knowBut, <docBut;
var <replaceKeys;
Expand Down Expand Up @@ -45,13 +43,11 @@ EnvirGui : JITGui {

this.makeOptionalViews(options);

valFields = {
CompositeView(zone, Rect(0, 0, bounds.width - 20, height))
.resize_(2)
paramViews = {
ParamView(zone, Rect(0, 0, bounds.width - 20, height))
.background_(skin.background);
}.dup(numItems);

widgets = nil.dup(numItems); // keep EZGui types here
paramViews.do { |pv| pv.zone.visible_(false); };

zone.decorator.reset.shift(zone.bounds.width - 16, 0);

Expand Down Expand Up @@ -149,26 +145,23 @@ EnvirGui : JITGui {
envir { ^object }

highlight { |index, prefix, color|
var widget = widgets[index];
var parName;
if (widget.notNil) {
widget.labelView.background_(color ? skin.onColor2);
parName = this.editKeys[index];
if (prefix.notNil and: parName.notNil) {
parName = prefix ++ parName;
};
widget.labelView.string_(parName);
var parView = paramViews[index];
var parName = this.editKeys[index];

parView.background_(color ? skin.onColor2);

if (prefix.notNil and: parName.notNil) {
parName = prefix ++ parName;
};
parView.label_(parName);
}

unhighlight { |index, prefix, color|
var widget = widgets[index];
var parName;
if (widget.notNil) {
widget.labelView.background_(skin.offColor);
parName = this.editKeys[index] ? "";
widget.labelView.string_(parName);
};
unhighlight { |index|
var parView = paramViews[index];
var parName = this.editKeys[index] ? "";

parView.background_(skin.offColor);
parView.label_(parName);
}

// also get specs as state that may have changed
Expand Down Expand Up @@ -202,7 +195,7 @@ EnvirGui : JITGui {

if (object.isNil) {
prevState = newState;
^this.clearFields(0);
^this.showFields(0);
};

if (newState[\overflow] > 0) {
Expand All @@ -218,7 +211,7 @@ EnvirGui : JITGui {
this.setByKeys(newKeys);
} {
this.setByKeys(newKeys);
if (newState[\overflow] == 0) { this.clearFields(newKeys.size) };
this.showFields(newKeys.size);
};

// "newState: %\n".postf(newState);
Expand All @@ -228,131 +221,52 @@ EnvirGui : JITGui {

updateButtons {
var flag = object.notNil;
if (protoBut.notNil) { protoBut.enabled_(flag and: { object.proto.notNil }) };
if (parentBut.notNil) { parentBut.enabled_(flag and: { object.parent.notNil }) };
if (knowBut.notNil) { knowBut.enabled_(flag).value_((flag and: { object.know }).binaryValue) };
if (protoBut.notNil) {
protoBut.enabled_(flag and: { object.proto.notNil })
};
if (parentBut.notNil) {
parentBut.enabled_(flag and: { object.parent.notNil })
};
if (knowBut.notNil) {
knowBut.enabled_(flag).value_((flag and: { object.know }).binaryValue)
};
if (docBut.notNil) { docBut.enabled_(flag) };
}

clearFields { |from = 0| (numItems - 1 .. from).do(this.clearField(_)) }
showFields { |num = 0|
paramViews.do { |pv, i|
var isInUse = i < num;
// [i, isInUse].postln;
pv.zone.visible_(isInUse).refresh;
}
}

setByKeys { |newKeys|
var prevEnvir = prevState[\object] ?? {()};
var newVal, oldVal, oldKey;


newKeys.do { |newKey, i|
var isSameKey;
var paramView = paramViews[i];

oldKey = prevState[\editKeys][i];
isSameKey = oldKey == newKey;
newVal = object[newKey];
oldVal = prevEnvir[newKey];
if (isSameKey.not or: { oldVal != newVal }) {
// "val for % has changed: %\n".postf(key, newval);
this.setField(i, newKey, newVal, false);
};
};
}

clearField { |index|
var area = valFields[index];
try {
area.children.copy.do { |view| view.remove };
area.background_(skin.background);
area.refresh;
widgets[index] = nil;
if (isSameKey.not) {
paramView.label_(newKey);
paramView.spec_(this.getSpec(newKey, newVal));
};
if (oldVal != newVal) { paramView.value_(newVal) };
};
}

setFunc { |key|
^{ |elem| object.put(key, elem.value) }
}

setToSlider { |index, key, value, sameKey|
var widget = widgets[index];
var area = valFields[index];
var keyToShow = this.showKeyFor(key);

// "setToSlider...".postln;

if (widget.isKindOf(EZSlider)) {
// "was slider already".postln;
if (sameKey.not) {
// "new key - reset widget ...".postln;
widget.set(
keyToShow,
this.getSpec(key, value),
this.setFunc(key),
value
);
this.colorizeArea(area, keyToShow != key); } {
// "old key, just set ...".postln;
widget.value = value;
};

^this
} {
// "make new slider!".postln;
this.clearField(index);
// don't know why, but defer seems needed:
{
widget = EZSlider(area, area.bounds.extent,
this.showKeyFor(key),
this.getSpec(key, value),
this.setFunc(key),
value,
labelWidth: labelWidth,
numberWidth: labelWidth
)
.font_(font);
widget.view.resize_(2);
widgets[index] = widget;

this.colorizeArea(area, keyToShow != key);

}.defer(0.03);

^this
};
}

setToRanger { |index, key, value, sameKey|
var widget = widgets[index];
var area = valFields[index];
var keyToShow = this.showKeyFor(key);

if (widget.isKindOf(EZRanger)) {
if (sameKey.not) {
widget.set(
keyToShow,
this.getSpec(key),
this.setFunc(key),
value
);
this.colorizeArea(area, keyToShow != key);
} {
widget.value = value;
};
} {
this.clearField(index);
{
widget = EZRanger(valFields[index], valFields[index].bounds.extent,
this.showKeyFor(key),
this.getSpec(key, value.maxItem),
this.setFunc(key),
value,
labelWidth: labelWidth,
numberWidth: labelWidth
).font_(font);

widget.view.resize_(2);
widgets[index] = widget;
this.colorizeArea(area, keyToShow != key);
}.defer(0.03);
^this
};
}

showKeyFor { |key| ^(replaceKeys[key] ? key) }

colorizeArea { |area, hilite = true|
Expand All @@ -363,82 +277,6 @@ EnvirGui : JITGui {
};
}

setToText { |index, key, value, sameKey = false|
var widget = widgets[index];
var area = valFields[index];
var keyToShow = this.showKeyFor(key);

// default: EZText
if (widget.isKindOf(EZText)) {
if (sameKey.not) { widget.labelView.string = keyToShow.asString };
widget.action = this.setFunc(key);
widget.value = value;
this.colorizeArea(area, keyToShow != key);

^this
} {
this.clearField(index);
{
widget = EZText(area,
area.bounds.extent,
this.showKeyFor(key),
this.setFunc(key),
value, false,
labelWidth, labelHeight: 18);
widget.font_(font);
widget.view.resize_(2);
widgets[index] = widget;

widget.value_(value);
this.colorizeArea(area, keyToShow != key);

}.defer(0.03);
};
}

setField { |index, key, value, sameKey = false|
var widget = widgets[index];
var area = valFields[index];

if (value.isKindOf(SimpleNumber) ) {
this.setToSlider(index, key, value, sameKey);
^this
};
// Ranger - only if spec exists and value is 2 numbers
if (useRanger
and: { value.size == 2
and: { key.asSpec.notNil
and: { value.every(_.isKindOf(SimpleNumber)) }
} }) {
this.setToRanger(index, key, value, sameKey);
^this
};
// default
this.setToText(index, key, value, sameKey);
}

findWidget { |key|
^widgets.reject(_.isNil).detect { |ez| ez.labelView.string.asSymbol == key };
}

updateSliderSpecs { |editKeys|

if (object.isNil) { specs.clear; ^this };

editKeys.do { |key, i|
var currValue = object[key];
var newSpec = this.getSpec(key, currValue);
var widge = widgets[i];
if (newSpec != widge.controlSpec) {
specs.put(key, newSpec);
if (widge.isKindOf(EZSlider) or: { widge.isKindOf(EZRanger) }) {
widge.controlSpec = newSpec;
widge.value_(currValue);
};
};
}
}

putSpec { |key, obj|
var widge, spec;
spec = obj.asSpec;
Expand Down
Loading

0 comments on commit 62a8dd1

Please sign in to comment.