How to create a jsui object that works in both patch and presentation mode?

lightspeed.johnny's icon

I am having trouble creating a jsui object that works correctly when the object has a different size in patch mode than in presentation mode.

I am using relative coordinates and sketch.screentoworld(x,y) to get click points. However, it seems that sketch.screentoworld() always uses the patch rectangle for it's transformation and not the presentation rectangle. So when clicking on the corners in patch mode, you get correct -1. to 1. range for the corners of the object, but when switching to presentation mode, the range seems to be related to the size of the patching rect, and not the new presentation rect.

How are we supposed to handle this situation?

Note: This is reproducible with the C74 example mgraphics-hittest patch.

nick rothwell | project cassiel's icon

I did report this shortcoming a few years ago. Only work-round is to use the same size for patch and presentation.

hollyhook's icon

my workaround: use a separate sketch for mouse input events.
creation of that sketch in some init function:

mysketch = new Sketch(box.rect[2] - box.rect[0], box.rect[3] - box.rect[1]);

and in any function you use mouse positions, e.g. button down

function onclick(x, y, button_down) {
    var w = mysketch.screentoworld(x, y);
...
}

for painting, i'm using the standard sketch

Baek Santarek's icon

Hollyhook, could you elaborate, please?

I've tried your workaround and it still doesn't work.

Sorry for the necro of an old thread but I have just bumped into this frustrating issue.

EDIT:

Nevermind.

I seem to have solved this by bypassing the native screentoworld function altogether and creating my own absolute-to-relative-coordinates-conversion function:

function ConvertToRelativeCoordinates(oldValue, oldMax){

post(oldValue);
post(oldMax);

var axis = axis;

var oldValue = oldValue;
var oldMin = 0;
var oldMax = oldMax;
var oldRange = (oldMax - oldMin);
var newMin = 1;
var newMax = -1;
var newRange = (newMax - newMin);  
var newValue = (((oldValue - oldMin) * newRange) / oldRange) + newMin
return newValue;
}

This way I am able to read and convert the absolute coordinates from this.box.rect both in patch mode and presentation mode. So far so good.