Qmaze Rules

From QmazeWiki
Jump to: navigation, search

Using Qmaze rules to call functions

Use the following script to catch the x3d commands given in the qmaze builder rules section.
The rules must be written as:

   x3d(object).action = value

action: what action needs to be executed? e.g. changeColor.
object: what object is the subject of change?
value: what value needs to be given to the function?


Place this piece of code in your outputscript:

   $(document).ready(function(){
       $(qmaze.events).on(qmaze.events.CONSTRAINT, function(e, constraints) {
           if (typeof constraints !== "undefined") {
               var res = constraints.split(";");
               for (var i = 0; i < res.length; i++) {
                   var regex = new RegExp(/(x3d\((.*?)\)\.(\w+)(\s+)?\=['"]?(.*)['"]?)/gi);
                   while (result = regex.exec(res[i])) {
                   	var object = result[2],
                       method = result[3],
                       value = unescape(result[5]).replace('\, );
                       console.log('qmaze:'+method+'='+value);
                       eval(method + '="' + value + '";');
                       window["my"+method](object, value);
                   }
               }
           }
       });
       var lazyLoadingConstraints = $('input[id$="hfLazyLoading"]').val();
       $(qmaze.events).trigger(qmaze.events.CONSTRAINT, [lazyLoadingConstraints]);
   });


In your output script you need to declare all the functions called by the qmaze rules.
The functions always need to begin with ' my ' as seen below.

   function mysetColor(obj, val) {
   obj: is the object in the qmaze rules
   val: is the value given in the qmaze rules
   the following example is for looking up an object in the scene (v3d)
   var obj = app.scene.getObjectByName(obj);
   }


Custom decompiler function

--ADVANCED

You'd only need this decompiler function if you want to make and call custom functions that need more then one variable.

To run the custom decompiler you need to Rule in Qmaze similar to how you would call a function.
The action needs to be 'decompiler' and the value can be a string of different variables with values.
First put in a variable name, secondly the value, separated by a ':'
Make sure you always put in 'function:' with your function name
The function with the name you put in here will be called when the decompiler is done

example of a Qmaze Rule:

   x3d(object).decompiler = function:testFunc, filename:'x.gltf', name:'test', posX:3, posY:1, posZ:0, rotX:0, rotY:0, rotZ:0

Include the next code in your Outputscript:

   function mydecompiler(obj, val) {
   	var myMap = new Map();
       val = val.replace(/\s/g, ); //remove all spaces
       val = val.replace(/'/g, ); //remove all '
       var res1 = val.split(',');
       for(var i = 0; i+1 <= res1.length; i++) {
       	var res2 = res1[i].split(':');
           myMap.set(res2[0], res2[1]);
       }
       console.log(myMap);
       window[myMap.get("function")](myMap);
   }

The following function will be run when the decompiler is done:

   function testFunc(myMap){
   	myMap.forEach(function(value,key){
       	console.log(key + ' = ' + value);
       });
   }

Currently this function prints a list in the console of all the different variable names and values
This can be changed into whatever you require

More information on how the map function in javascript works can be found here: Map


TO DO: make an example function