jit.gl.sketch current command list length?
hi
I'm using "cmd_replace" from javascript to adjust graphics in jit.gl.sketch. I've created the sketch object with new JitterObject
so it isn't inside a max box and doesn't have any outlets. At the moment I painstakingly and error-pronely count the commands I send to sketch and record indices of commands I would like to replace later. Isn't there a way to know which index a command just inserted has? the most convenient convention would have been for command funcitons to return the index, like (using javascript syntax):
var sketch = new JitterObject("jit.gl.sketch","context");
...
...
var line_idx = sketch.line(x,y);
...
...
sketch.cmd_replace(line_idx, "line", new_x, new_y);
but less beautiful but easier to add api could do it as well:
var sketch = new JitterObject("jit.gl.sketch","context");
...
...
var line_idx = sketch.cmdlist_len; // next added command will have this index
sketch.line(x,y);
...
...
sketch.cmd_replace(line_idx, "line", new_x, new_y);
or maybe there is a way to do this already and I just don't know?
is there a chance that something along these lines be added?
For now I'm gonna make my own javascript utility wrapper implementing this convention, but it will only work in javascript, so won't be useful for anyone using jitter otherwise.
So, here it is:
function add_epilogue(object,method_name,epilogue) {
if (!object.old)
object.old = {};
if (!object.old[method_name])
object.old[method_name] = object[method_name];
object[method_name] = function() {
object.old[method_name].apply(object,arguments);
return epilogue.apply(object,arguments);
}
}
var cmd_adders = [ "circle", "drawmatrix", "drawobject", "cube", "cylinder",
"beginstroke", "framecircle", "frameellipse", "framequad", "frametri",
"ellipse", "endstroke", "glalphafunc", "glbegin", "glbindprogram",
"glbindtexture", "glblendfunc", "glclear", "glclearcolor", "glcleardepth",
"glclipplane", "glcolor", "glcolormask", "glcolormaterial", "glcullface",
"gldepthmask", "gldepthrange", "gldisable", "gldrawpixels", "gledgeflag",
"glenable", "glend", "glfinish", "glflush", "glfog", "glfrustum", "glhint",
"glget", "gllight", "gllightmodel", "gllinestipple", "gllinewidth",
"glloadidentity", "glloadmatrix", "gllogicop", "glmaterial", "glmatrixmode",
"glmultmatrix", "glnormal", "glortho", "glpixeltransfer", "glpixelzoom",
"glpointsize", "glpolygonmode", "glpolygonoffset", "glpopattrib",
"glpopmatrix", "glpushattrib", "glpushmatrix", "glrasterpos", "glreadpixels",
"glrect", "glrotate", "glscale", "glscissor", "glshademodel",
"glunbindprogram", "gltexcoord", "gltexenv", "gltexgen", "gltexparameter",
"gltranslate", "glvertex", "glviewport", "glulookat", "gluortho2d",
"gluperspective", "glutessbegincontour", "glutessbeginpolygon",
"glutessedgeflag", "glutessendcontour", "glutessendpolygon", "glutessnormal",
"glutessmatrix", "glutessproperty", "glutessvertex", "move", "moveto",
"line", "lineto", "linesegment", "plane", "point", "quad", "tri",
"shapeorient", "shapeprim", "shapeslice", "sphere", "roundedplane", "torus",
"strokeparam", "strokepoint" ];
function wrap_sketch(sketch) {
sketch.cmds_cnt = 0;
add_epilogue(sketch,"reset",function() {
this.cmds_cnt = 0;
return null;
});
for(var i in cmd_adders) {
add_epilogue(sketch,cmd_adders[i],function() {
return this.cmds_cnt++;
});
}
}
I hope I listed all the methods that add commands to the list. The only one that this wrapper can't handle is the cmd_insert since calling it invalidates all the saved indexes after inserted command. Now I can do something like:
var c = [ [1,0,0], [0,1,0], [0,0,1] ];
var r = [ 0.5, 0.3, 0.1 ];
var a = [ 0, 0, 0 ];
var line_idx = [];
function build_viz() {
with(sketch) {
reset();
glclearcolor(1,1,1);
glclear();
var _a = 0;
for(var i = 0; i
_a += a[i];
glcolor(c[i]);
framecircle(r[i]);
// here
line_idx[i] = line( r[i] * Math.cos(_a), r[i] * Math.sin(_a) );
//
}
}
refresh();
}
function angle(k, ak) {
a[k] = ak * Math.PI / 180;
var _a = 0;
for(var i=0; i
_a += a[i];
// and using it here
sketch.cmd_replace(line_idx[i],"line",r[i]*Math.cos(_a),r[i]*Math.sin(_a));
//
}
refresh();
}
(not very nice global variable names there, but this is just an example :-))