deprecate SCViewHolder-layRight
[supercollider.git] / SCClassLibrary / Common / GUI / Base / EZRangerSC.sc
blobcc055fc197b9d27f3cba4097ea94ce10e4d27bae
1 EZRanger : EZGui {
3         var <rangeSlider, <hiBox,<loBox, <unitView, <>controlSpec,
4                  <>action,<lo, <hi, popUp=false, numSize,numberWidth,unitWidth;
5         var <>round = 0.001;
7         *new { arg parent, bounds, label, controlSpec, action, initVal,
8                         initAction=false, labelWidth=60, numberWidth=45,
9                         unitWidth=0, labelHeight=20,  layout=\horz, gap,margin;
11                 ^super.new.init(parent, bounds, label, controlSpec, action,
12                         initVal, initAction, labelWidth, numberWidth,
13                                 unitWidth, labelHeight, layout, gap, margin)
14         }
16         init { arg parentView, bounds, label, argControlSpec, argAction, initVal,
17                         initAction, labelWidth, argNumberWidth,argUnitWidth,
18                         labelHeight, argLayout, argGap,argMargin;
20                 var labelBounds, hiBounds,loBounds, unitBounds,rangerBounds;
21                 var numberStep;
23                 // Set Margin and Gap
24                 this.prMakeMarginGap(parentView, argMargin, argGap);
26                 unitWidth = argUnitWidth;
27                 numberWidth = argNumberWidth;
28                 layout=argLayout;
30                 bounds.isNil.if{bounds = 350@20};
32                 // if no parent, then pop up window
33                 # view,bounds = this.prMakeView( parentView,bounds);
35                 labelSize=labelWidth@labelHeight;
36                 numSize = numberWidth@labelHeight;
38                 // calcualate bounds
39                 # labelBounds,hiBounds,loBounds,rangerBounds, unitBounds
40                                 = this.prSubViewBounds(innerBounds, label.notNil, unitWidth>0);
42                 label.notNil.if{ //only add a label if desired
43                         labelView = GUI.staticText.new(view, labelBounds);
44                         labelView.string = label;
45                 };
47                 (unitWidth>0).if{ //only add a unitLabel if desired
48                         unitView = GUI.staticText.new(view, unitBounds);
49                 };
52                 loBox = GUI.numberBox.new(view, loBounds);
53                 rangeSlider = GUI.rangeSlider.new(view, rangerBounds);
54                 hiBox = GUI.numberBox.new(view, hiBounds);
56                 controlSpec = argControlSpec.asSpec;
57                 (unitWidth>0).if{unitView.string = " "++controlSpec.units.asString};
58                 action = argAction;
60                 loBox.action_({ |box| this.lo_(box.value).doAction; });
61                 rangeSlider.action_({ |sl|
62                                 this.lo_(controlSpec.map(sl.lo));
63                                 this.hi_(controlSpec.map(sl.hi));
64                                 this.doAction;
65                         });
66                 hiBox.action_({ |box| this.hi_(box.value).doAction; });
68                 if (initVal.notNil) { this.value_(initVal) };
69                 if (initAction) { this.doAction };
72 //              if (controlSpec.step != 0) {
73 //                      rangeSlider.step = (controlSpec.step / (controlSpec.maxval - controlSpec.minval));
74 //              };
76                 numberStep = controlSpec.step;
77                 if (numberStep == 0) {
78                         numberStep = controlSpec.guessNumberStep;
79                 }{
80                         // controlSpec wants a step, so zooming in with alt is disabled.
81                         hiBox.alt_scale = 1.0;
82                         loBox.alt_scale = 1.0;
83                         rangeSlider.alt_scale = 1.0;
84                 };
86                 hiBox.step=numberStep;
87                 loBox.step=numberStep;
88                 hiBox.scroll_step=numberStep;
89                 loBox.scroll_step=numberStep;
91                 rangeSlider.receiveDragHandler = { arg slider;
92                         slider.valueAction = controlSpec.unmap(GUI.view.currentDrag);
93                 };
95                 rangeSlider.beginDragAction = { arg slider;
96                         controlSpec.map(slider.value)
97                 };
99                 this.prSetViewParams;
101         }
103         doAction { action.value(this); }
105         set { arg label, spec, argAction, initVal, initAction = false;
106                 labelView.notNil.if { labelView.string = label.asString };
107                 spec.notNil.if { controlSpec = spec.asSpec };
108                 argAction.notNil.if { action = argAction };
110                 initVal = initVal ? this.value ? controlSpec.default.dup(2);
112                 if (initAction) {
113                         this.valueAction_(initVal);
114                 }{
115                         this.value_(initVal);
116                 };
117         }
119         value { ^[lo, hi] }
120         value_ { |vals| this.lo_(vals[0]).hi_(vals[1]) }
121         valueAction_ { |vals| this.value_(vals).doAction }
123         lo_ { |val|
124                 lo = controlSpec.constrain(val);
125                 loBox.value_(lo.round(round));
126                 rangeSlider.lo_(controlSpec.unmap(lo));
127         }
129         hi_ { |val|
130                 hi = controlSpec.constrain(val);
131                 hiBox.value_(hi.round(round));
132                 rangeSlider.hi_(controlSpec.unmap(hi));
133         }
134         setColors{arg stringBackground, stringColor, sliderColor,  numBackground,numStringColor,
135                          numNormalColor, numTypingColor, knobColor,background ;
137                         stringBackground.notNil.if{
138                                 labelView.notNil.if{labelView.background_(stringBackground)};
139                                 unitView.notNil.if{unitView.background_(stringBackground)};};
140                         stringColor.notNil.if{
141                                 labelView.notNil.if{labelView.stringColor_(stringColor)};
142                                 unitView.notNil.if{unitView.stringColor_(stringColor)};};
143                         sliderColor.notNil.if{
144                                 rangeSlider.background_(sliderColor);};
145                         numBackground.notNil.if{
146                                 hiBox.background_(numBackground);
147                                 loBox.background_(numBackground);};
148                         numNormalColor.notNil.if{
149                                 hiBox.normalColor_(numNormalColor);
150                                 loBox.normalColor_(numNormalColor);};
151                         numTypingColor.notNil.if{
152                                 hiBox.typingColor_(numTypingColor);
153                                 loBox.typingColor_(numTypingColor);};
154                         numStringColor.notNil.if{
155                                 hiBox.stringColor_(numStringColor);
156                                 loBox.stringColor_(numStringColor);};
157                         knobColor.notNil.if{
158                                 rangeSlider.knobColor_(knobColor);};
159                         background.notNil.if{
160                                 view.background=background;};
161                         hiBox.refresh;
162         }
164         font_{ arg font;
166                         labelView.notNil.if{labelView.font=font};
167                         unitView.notNil.if{unitView.font=font};
168                         hiBox.font=font;
169                         loBox.font=font;
170         }
173         prSetViewParams{
175                 switch (layout,
176                 \line2, {
177                         labelView.notNil.if{
178                                 labelView.resize_(2);
179                                 unitView.notNil.if{unitView.resize_(3)};
180                                 hiBox.resize_(3);
181                                 loBox.resize_(3);
182                                 }{
183                                 unitView.notNil.if{
184                                         unitView.resize_(2).align_(\left)};
185                                 hiBox.resize_(1);
186                                 loBox.resize_(1);
187                                 };
188                         rangeSlider.resize_(5);
189                         popUp.if{view.resize_(2)};
190                 },
191                 \vert, {
192                         labelView.notNil.if{labelView.resize_(2)};
193                         unitView.notNil.if{unitView.resize_(8)};
194                         loBox.resize_(8);
195                         hiBox.resize_(2);
196                         rangeSlider.resize_(5);
197                         popUp.if{view.resize_(4)};
198                 },
199                 \horz, {
200                         labelView.notNil.if{labelView.resize_(4).align_(\right)};
201                         unitView.notNil.if{unitView.resize_(6)};
202                         loBox.resize_(4);
203                         hiBox.resize_(6);
204                         rangeSlider.resize_(5);
205                         popUp.if{view.resize_(2)};
206                 });
207         }
208         prSubViewBounds{arg rect, hasLabel, hasUnit;
209                 var hiBounds,loBounds,labelBounds,rangerBounds;
210                 var unitBounds, gap1, gap2, gap3,gap4, tmp, labelH, unitH;
211                 gap1 = gap.copy; // use copy to make sure these are unique
212                 gap2 = gap.copy; // since you later set the .x or .y
213                 gap3 = gap.copy;
214                 gap4 = gap.copy;
215                 labelH=labelSize.y;//  needed for \vert
216                 unitH=labelSize.y; //  needed for \vert
218                 switch (layout,
219                         \line2, {
220                                 hasLabel.not.if{ gap2 = 0@0; labelSize.x = 0 ;};
221                                 hasUnit.not.if{ gap4 = 0@0; unitWidth = 0};
222                                 hasLabel.if{ // with label
223                                         unitBounds = (unitWidth@labelSize.y)
224                                                 .asRect.left_(rect.width-unitWidth);// view to right
225                                         hiBounds = (numSize.x@labelSize.y)
226                                                 .asRect.left_(rect.width-unitBounds.width-numberWidth-gap4.x); // view to right
227                                         loBounds = (numSize.x@labelSize.y)
228                                                 .asRect.left_(rect.width-unitBounds.width-numberWidth
229                                                         -gap3.x-hiBounds.width-gap4.x); // view to right
230                                         labelBounds = (labelSize.x@labelSize.y)
231                                                 .asRect.width_(loBounds.left-gap2.x); //adjust width
232                                 }{ // no label
233                                 labelBounds = (0@labelSize.y).asRect; //just a dummy
234                                 loBounds = (numberWidth@labelSize.y).asRect; //view to left
235                                 hiBounds = (numberWidth@labelSize.y).asRect.moveTo(loBounds.width+gap3.x,0); //view to left
236                                 (unitWidth>0).if{
237                                         unitBounds = Rect.newSides (hiBounds.right+gap4.x,
238                                         0,rect.width, labelSize.y); //adjust width
239                                                 }{
240                                         unitBounds = Rect (0, 0,0,0); //no unitView
241                                                 };
243                                 };
244                                 rangerBounds = Rect(
245                                                 0,
246                                                 labelSize.y+gap1.y,
247                                                 rect.width,
248                                                 rect.height-numSize.y-gap1.y;
249                                                 );
250                                 },
252                          \vert, {
253                                 hasLabel.not.if{labelH=0; gap1.y=0};
254                                 labelBounds = (rect.width@labelH).asRect; // to top
255                                 hasUnit.not.if{unitH=0; gap4 = 0@0;};
256                                 unitBounds = (rect.width@unitH)
257                                         .asRect.top_(rect.height-unitH); // to bottom
258                                 hiBounds = (rect.width@labelSize.y)
259                                         .asRect.top_(labelBounds.height+gap1.y); // to bottom
260                                 loBounds = (rect.width@labelSize.y)
261                                         .asRect.top_(rect.height-unitBounds.height-numSize.y-gap4.y); // to bottom
262                                 rangerBounds = Rect.newSides(
263                                         0,
264                                         hiBounds.bottom+gap2.y,
265                                         rect.width,
266                                         loBounds.top-gap3.y
267                                         );
268                                 },
270                          \horz, {
271                                 hasLabel.not.if{ gap1 = 0@0; labelSize.x = 0 ;};
272                                 hasUnit.not.if{ gap4 = 0@0; unitWidth = 0};
273                                 labelSize.y=rect.height;
274                                 labelBounds = (labelSize.x@labelSize.y).asRect;
275                                 unitBounds = (unitWidth@labelSize.y).asRect.left_(rect.width-unitWidth);
276                                 loBounds = (numSize.x@labelSize.y).asRect
277                                         .left_(labelBounds.width+gap1.x);
278                                 hiBounds = (numSize.x@labelSize.y).asRect
279                                         .left_(rect.width-unitBounds.width-numSize.x-gap4.x);
280                                 rangerBounds  =  Rect.newSides(
281                                         loBounds.right+gap2.x , 0,
282                                         rect.width-hiBounds.width-unitBounds.width
283                                                 -gap3.x-gap4.x,
284                                         labelSize.y);
286                         });
289                 ^[labelBounds, hiBounds, loBounds, rangerBounds, unitBounds]
290                         .collect{arg v; v.moveBy(margin.x,margin.y)}
291         }