Skip to content

Commit

Permalink
add ParamView class to refactor EnvirGui views
Browse files Browse the repository at this point in the history
  • Loading branch information
adc committed Nov 18, 2015
1 parent 3aca41e commit 9a7058d
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
99 changes: 99 additions & 0 deletions HelpSource/Classes/ParamView.schelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
TITLE:: ParamView
summary:: show a parameter of a JITLib process
categories:: Libraries>JITLib
related:: Classes/JITGui, Overviews/JITLib

DESCRIPTION::
ParamView displays a parameter value, and switches representation as appropriate for value: A single number is shown by an EZSlider, a pair of numbers by an EZRanger, and anything else as code on an EZText.

First examples:
code::

w = Window("test", Rect(20, 820, 400, 100)).front;
w.addFlowLayout;
~pv = ParamView(w, Rect(20, 20, 360, 20));

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

~pv.dump
~pv.viewType_(0); // EZNumber
~pv.viewType_(1); // EZRanger
~pv.viewType_(2); // EZText

~pv.label_(\freq);
~pv.spec_(\freq); // needs spec for EZSlider


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

::

CLASSMETHODS::

METHOD:: new
create a new ParamView with
ARGUMENT:: parent
the parent window or view
ARGUMENT:: bounds
the bounds of the view
ARGUMENT:: label
a label to display
ARGUMENT:: spec
a controlspec for the value
ARGUMENT:: action
an action to do when the value changes
ARGUMENT:: initVal
an initial value
ARGUMENT:: initAction
a boolean whether to perform the action on init.


INSTANCEMETHODS::

METHOD:: label
get and set the view's label

METHOD:: spec
get and set the view's control spec

METHOD:: action
get and set the paramview's action

private:: zone, zones

METHOD:: ezviews, slider, ranger, textview
the 3 ezviews between which the ParamView switches

METHOD:: currview
the currently shown view of these

METHOD:: value
get and set value

METHOD:: valueAction
get and set value and do action

METHOD:: doAction
do the view's action

METHOD:: viewType
get and set the view's type:
0 is single number -> EZSlider,
1 is pair of numbers -> EZRanger,
2 is anything else

METHOD:: valueType
determine viewType for a given value


METHOD:: background
get and set background color

private:: init
86 changes: 86 additions & 0 deletions SCClassLibrary/JITLib/GUI/ParamView.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

ParamView {
var <value, <label, <>action, <spec;
var <zone, <zones, <slider, <ranger, <textview;
var <ezviews, <currview, <viewType;

*new { |parent, bounds, label, spec, action, initVal, initAction = false|
^super.new.init(parent, bounds, label, spec, action, initVal, initAction);
}

bounds { ^zone.bounds }
bounds_ { |bounds| zone.bounds_(bounds) }

background { ^currview.labelView.background }
background_ { |col|
ezviews.do { |ez| ez.labelView.background_(col) }
}

label_ { |alabel|
label = alabel;
ezviews.do { |ez| ez.labelView.string_(alabel);};
}


init { |parent, bounds, argLabel, argSpec, argAction, initVal, initAction, initType = 0|
var rect2 = bounds.moveTo(0,0);
zone = CompositeView(parent, bounds);
zones = 3.collect { |i| CompositeView(zone, rect2); };
zone.resize_(5);
zones.do(_.resize_(5));

label = argLabel ? "-";
spec = argSpec.asSpec;
action = argAction;

slider = EZSlider(zones[0], rect2, label, spec);
ranger = EZRanger(zones[1], rect2, label, spec);
textview = EZText(zones[2], rect2, label);

ezviews = [slider, ranger, textview];
ezviews.do { |ez|
ez.action_({ |ez| this.valueAction_(ez.value) });
};
if (initVal.notNil) { this.value(initVal) };
if (initAction) { this.doAction };
}

// types: 0 = slider, 1 = ranger, 2 = text
viewType_ { |newType = 0, force = false|
if (force or: { newType != viewType }) {
if (newType.inclusivelyBetween(0, 2)) {
zones.do { |z, i| z.visible_(newType == i) };
viewType = newType;
currview = ezviews[newType];
};
};
}

valueType { |newval|
^case
{ newval.isNumber } { 0 }
{ newval.isKindOf(Array) and:
{ newval.size == 2 and:
{ newval.every(_.isNumber) }
}
} { 1 }
{ 2 }
}

value_ { |val|
if (val != value) {
this.viewType_(this.valueType(val));
};
value = val;
currview.value_(value);
}

doAction { action.value(this) }
valueAction_ { |val| this.value_(val).doAction }

spec_ { |newspec|
spec = newspec.asSpec;
slider.controlSpec_(spec);
ranger.controlSpec_(spec);
}
}

0 comments on commit 9a7058d

Please sign in to comment.