Function to creates buttons - supercollider

I'm trying to create a function to create a button (so keep the "clean" code).
Here is the code:
(
Window.closeAll;
~w = Window.new(
name: "Xylophone",
resizable: true,
border: true,
server: s,
scroll: false);
~w.alwaysOnTop = true;
/**
* Function that creates a button.
*/
createButtonFunc = {
|
l = 20, t = 20, w = 40, h = 190, // button position
nameNote = "note", // button name
freqs // frequency to play
|
Button(
parent: ~w, // the parent view
bounds: Rect(left: l, top: t, width: w, height: h)
)
.states_([[nameNote, Color.black, Color.fromHexString("#FF0000")]])
.action_({Synth("xyl", [\freqs, freqs])});
}
)
(
SynthDef("xyl", {
|
out = 0, // the index of the bus to write out to
freqs = #[410], // array of filter frequencies
rings = #[0.8] // array of 60 dB decay times in seconds for the filters
|
...
)
The error is: ERROR: Variable 'createButtonFunc' not defined.
Why?
Sorry but I'm a beginner.
Thanks!

Probably a little late to answer this, but I hope this might help someone else with the same question.
The reason you're getting that error is because you're using a variable name before you're declared it.
In other words, if you try to evaluate
variableName
on its own, you'll always get an error, because the interpreter can't match that name to anything else it knows. To solve this, you can use a global interpreter variable (a-z), an environment variable (like ~createButtonFunc), or declare var createButtonFunc earlier on in your code. Note that this last one means you won't be able to access that variable name after interpreting that chunk, which may or may not be a good thing. If you want to be able to access it later, I think it makes the most sense to write ~createButtonFunc.
By the way, you can just use w instead of ~w; single-letter variable names are global by default, and that's the idiomatic usage.
-Brian

Related

How to check that a countdown label has the wanted values with Cypress?

I have an slider that controls the amount of time a countdown will have.
The countdown is showed in a label to the user. So, if I set it to 5, the label will show 5, then 4, then 3 until 0, the initial value is -.
I want to test this with Cypress. But I am not sure which is the correct way to do it without creating a flacky test.
Is there a recommended way to "watch" and specific item changes?
I too am looking for a good way to listen for element changes. But I think in your scenario maybe you can get away through the countdown timer with something like this:
const count = 5;
const countdownTime = 1000;
let expectedCurrentNumber = count - 1;
do {
cy.contains('countdownLabel', expectedCurrentNumber, { timeout: countdownTime });
} while (--expectedCurrentNumber > 0);
cy.contains('countdownLabel', '-', { timeout: countdownTime });

Using random in JSP5

Does someone know how I can fix this problem?
var Arguments [2003,1002,3932,10203,2030,1828912,12912]
var Argument = Arguments[Math.floor(Math.random() * Arguments.length)]
Because of these variables, a random argument is always chosen at the beginning. However, if you are in the program and switch from page to page, it will keep the random value it had at the beginning. I want him to be constantly creating new values.
In the draw function I call the argument like this:
text(Argument, 300,180);
If I understand your question and the comments correctly, you need re-assign Argument to be a different random value each time you switch pages, such as this line every time it switchs:
Argument = Arguments[Math.floor(Math.random() * Arguments.length)];
You don't put var there every time. Give it a value in setup(), then re-assign it when you need another value.
Unless, of course, I completely missed your problem.
In p5.js there is a helpful function called random
so you could use it like this
var Arguments [2003,1002,3932,10203,2030,1828912,12912]
var Argument = Arguments[random(0, Arguments.length - 1)]
you can read more about it here
Make argument into a function.
var Arguments [2003,1002,3932,10203,2030,1828912,12912];
var Argument = function(){
return Arguments[ Math.floor( Math.random() * Arguments.length ) ];
}
text(Argument(), 300,180);
If you make a random selection in setup() it will only happen once in your whole script. Also, Don't choose variable names that might already be used by the system (I suspect "arguments" might be.
let args = [2003,1002,3932,10203,2030,1828912,12912];
let a;
function setup() {
createCanvas(400, 400);
a = random(args);
}
function draw() {
background(220);
textSize(30);
text(a,50,70);
}
function mousePressed() {
a = random(args);
}

Kendo NumericTextBox with step and rounding

I want to setup kendoNumericTextBox to allow user input any integer number and set step to 1000. But when user enters any value and use spinner, it should update to next multiple of step.
For example:
enter 123, press spin up, value will be 1000
enter 1234, press spin up, value vill be 2000
Is it possible or only way is to handle spin event and modify value from there?
UPDATE:
Ok, guys, thnx for help.
I have this spin handler now and it seems to be working as expected.
function onSpin(e)
{
var currentvalue = kendo.parseInt(this.value());
if (currentvalue > 0)
{
this.value(Math.floor(currentvalue / this.step()) * this.step());
}
if (currentvalue < 0)
{
this.value(Math.ceil(currentvalue / this.step()) * this.step());
}
}
I have provided a dojo below with a potential solution for you:
https://dojo.telerik.com/UQohUjaP/2
I have created a function that will work on the spin and on change value so that it will step the value up/down on the value that you set e.g. 1000
the function is fairly simple and for brevity I have taken out the log statements here:
function onChange() {
var currentvalue = kendo.parseInt(this.value());
if (currentvalue > 0) {
currentvalue = Math.ceil(currentvalue / this.step()) * this.step();
} else if (currentvalue < 0) {
currentvalue = Math.floor(currentvalue / this.step()) * this.step();
} else {
currentvalue = 0;
}
this.value(currentvalue);
}
Unfortunately there doesn't seem to be a simple way of figuring out if the value has gone up or down so I am basically checking to see if the value is greater than 1 or less than 1 and then calculating the ceiling or the floor for the value and then stepping it in the right direction. in order to cater for zero we have a special condition which just sets the value to 0 assuming that is a valid value in your scenario
As you say, it's possible by listening to the spin event:
$("#numerictextbox").kendoNumericTextBox({
min: 0,
spin: function(e) {
var isUp = e.sender._key === 38,
isDown = e.sender._key === 40;
var m = Math.trunc(this.value()/1000),
value = isUp ? m + 1 : m;
this.value(value * 1000);
}
});
I doubt there's something out of the box, because your needs seem somewhat unusual.

AdjustsFontSizeToFitWidth && numberOfLines = 0 doesn't work together as expected

let nameBox = UILabel(x: 0, y: 0, w: sideSize, h: sideSize*2/4)
nameBox.text = skillName
nameBox.textAlignment = .Center
nameBox.numberOfLines = 0
nameBox.adjustsFontSizeToFitWidth = true
nameBox.addBorderLeft(size: 1, color: UIColor.blackColor())
nameBox.addBorderTop(size: 1, color: UIColor.blackColor())
nameBox.addBorderRight(size: 1, color: UIColor.blackColor())
container.addSubview(nameBox)
This is the code I have and its output is below.
As you can see it has modified everything perfectly except for Communication & Lumberjack. Why is it, and how can I solve it?
Looks like you need to set the lineBreakMode of the label to .ByWordWrapping.
Another thought is that you really want the font to be smaller. Perhaps this is prevented by the minimumScaleFactor property.
From the docs for adjustsFontSizeToFitWidth:
The default value for this property is false. If you change it to true, you should also set an appropriate minimum font size by modifying the minimumFontSize property.
This seems to be a documentation bug. minimumFontSize is deprecated.

jqGrid - grouping + custom summarytype function

I have a jqGrid on my page that has grouping and contains several columns (A,B,C,D). How do I make the summary (i.e. total row) for column D within each group be the following: sum(D)/Sum(A) * 100?
I know I can define a custom function for the summaryType such as: summaryType: mysum but I am not quite sure, how I can use this to do the above formula? Note, columns A,B, and C all have summaryType set as follows: summaryType: 'sum' so they get calculated using the built in summing functionality. I am also wondering if I can somehow pull this off on the loadComplete event of the jqGrid?
I managed to achieve this by doing the following:
I define global variables for the current value of column A and Column C, and the current group by
var tColA = 0.0;
var tColD = 0.0;
var tCurrentGroupBy;
Then in my function, I do the following:
function mysum(a, b, c) {
if (tCurrentGroupBy!= c.GroupByCol) {
tColA= 0.0;
tColD = 0.0;
tCurrentGroupBy= c.GroupByCol;
tColA= tColA + parseFloat(c.ColA);
tColD = tColD + parseFloat(c.ColD);
}
else {
tColA= tColA + parseFloat(c.ColA);
tColD = tColD + parseFloat(c.ColD);
}
return ((tColD / tColA) * 100);
}
not sure if there is a better way to do this, but this seems to work. Are there any other suggestions? thanks.
A little bit late, but...
I have a column D that shows the individual percentage of column C with respect to column B. So I've implemented a formatter that adds the ' %' sign to D column. Column A is the identifier.
But the formatter function is applied to the summary row, too. So the formatter detects if it is the summary row, and if it is, do the calculations and return the value.
function percentage_format(cellValue, options, rowObject) {
var a=$(rowObject).attr('A');
if(!a || a='')
cellValue=Math.round($(rowObject).attr('C')*10000/$(rowObject).attr('B'))/100;
return "<span originalValue='"+cellValue+"'>"+cellValue+" %"+"</span>";
}
function unformatCampo(cellValue, options, cellObject) {
return $(cellObject.html()).attr("originalValue");
}
I hope this still helps.
Place this formatter function on each cell involved in the percent calculation.
In this example, cell a,cell b and cell c need the formatter function.
The function is in a and b to grap the value and on c to calulate the percent using the saved values.
It’s important to note 3 things with the formatter event:
1.The cell value is modifiable. Even though it’s a formatting event the value may be changed.
2.The event is fired in the order or the column model
3.Athough, rwdat is row data it is only the column model for the cell being formatter, not the whole row. That’s why the function needs to be on each cell to get all the values.
<script>
var cnt = 0;
var a = 0.0;
var b= 0.0;
function linePercent(cellval, opts, rwdat, act)
{
// rowed==”” means it’s a summary line(total)
if (opts.rowId =="")
{
// save the 2 values used in the calculation
if (rwdat.nm == "a"){totexec=cellval;}
if (rwdat.nm == "b"){planamt=cellval;}
// when the calculated cell is formatted, perform the calculation and set cellval
if (rwdat.nm == "c"){cellval=(a/b)*100;}
}
// call formatter to format cellval
return $.fn.fmatter('number', cellval,opts, rwdat, act);
};
</script>
Do it on the server. It'll be far simpler, you'll have complete control.

Resources