number box, no focus, hide cursor and slow drag with jsui

Denis's icon

Hello forum
I wasn't able to find what I needed so I did it myself. It's a number box that doesn't steal focus, hides the cursor during the dragging and reacts more slowly than normal number boxes. Here is my code that you hopefully can adapt to your needs. You also need to access mouse position from outside, which I did in a global js code, otherwise I didn't find the way to access the correct absolute/relative mouse position with nested subpatches.
Also note that the cursor position, even though it's hidden, is reset to initial click position every 30 pixels, to avoid drag blocking whenever you drag close to the screen border.
Hope it helps!

EDIT: find the zipped full patch down below
---------------------------
autowatch=1;
inlets=1;
outlets=1;

var g=new Global('myglobal'); // get mouse position stored in myglobal with g.mousex and g.mousey

var width=50;
var height=30;
var minimum=0;
var maximum=100;
var dragdistance=30; // increments / decrements when cursor moves more than 30 pixels
sketch.default2d();
sketch.glortho(0,width,0,height,-1,100.);
var bgcolor_normal = [1.,0.64,0.0,1.];
var bgcolor_clicked = [1.,0.4,0.0,1.];
var bgcolor =bgcolor_normal;
var color = [0.,0.,0.,1.];
var _fontsize = 24;
var _font = "Sans Serif";
var lasty=0;
var beforedragx=0; // store mouse position when click
var beforedragy=0;
var mousedown=0; // remember mouse button state
var currentnumber=0;
draw();

function up()
{ updatenumber(1); }

function down()
{ updatenumber(-1); }

function msg_int()
{
    currentnumber=arguments[0];
    checknumber();
}

function draw()
{
    with (sketch)
    {
        fontsize(_fontsize);
         font(_font);
        glclearcolor(bgcolor);
        glclear();
        textalign("left","bottom");
        moveto(-3,2);
        glcolor(color);
        text(currentnumber.toString());
    }
    refresh();
}

function updatenumber(delta)
{
    currentnumber+=delta;
    checknumber();
}

function checknumber()
{
if (currentnumber
currentnumber=minimum;
else if (currentnumber>maximum)
currentnumber=maximum;
    outlet(0,currentnumber);
    draw();
}

function ondrag(x, y, button, cmd, shift, capslock, option, ctrl)
{
if (!button)
{
max.pupdate(beforedragx,beforedragy);
max.showcursor();
bgcolor=bgcolor_normal;
draw();
}

var delta=lasty-y;
var delta2=0;
if (delta>0)        delta2=Math.floor(delta/dragdistance);
else        delta2=Math.ceil(delta/dragdistance);
if (delta2!=0)
{
updatenumber(delta2);
max.pupdate(beforedragx,beforedragy);
}
}
ondrag.local = 1; //private

function onclick(x,y,button)
{
bgcolor=bgcolor_clicked;
draw();
max.hidecursor();
beforedragx=g.mousex;
beforedragy=g.mousey;
lasty=y;
outlet(0,currentnumber);
}
onclick.local = 1; //private

function onresize()
{
    box.size(width,height);
    sketch.default2d();
    sketch.glortho(0,width,0,height,-1,100.);
    draw();
}
onresize.local = 1; //private

vichug's icon

hey,
please use copy compressed when posting a patch to forum (select everything ; rightclick -> copy compressed) otherwise the forum formatting will destroy everything 99% of the time

Jan M's icon

@VICHUG: we can't "copy compressed" JavaScript code :)
...but it can be attached as as file ... ;)

Happy New Year!

vichug's icon

oh that's only jacascript ! indeed ! my bad. (maybe the "code" tags would help ?.. i'm not sure if js is borked with forum formatting)
anyway yes happy new year

Jan M's icon

yea code tags help to keep formatting ... but if it is more than a few lines it's not too readable on moible devices. That't just how it is right now :)

Floating Point's icon

thanks for this;

i think this line:var g=new Global(‘myglobal’);

needs to be:var g=new Global("myglobal");

Floating Point's icon

just been having a look at this-- all sorts of weird stuff going on; did you test the code before posting?
arguments for pupdate not being seen, causing error messages in max window, number box very sticky, keeps increasing values when drag mouse down;
someone with more js experience than me might want to have a look (good coding exercise nevertheless)

Denis's icon

I only meant to show how to use pupdate and combinations but here is the full package with global code and jsui, hope it works better for you.

for those who will have a look, you will also see a date function to convert months into numbers. Nothing to do with the first subject but I didn't find how to get numeric date formats like "20140102", if somebody has a trick I'm happy to know.

specialnumber_jsui.zip
zip
Floating Point's icon

Oh OK thanks-- a good piece of code for me to study (I'm a js novice). re date formatting, isn't that just converting ints to strings and then concatenating?