osclang.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. this.initLang = function () {
  2. console.log("add operations to semantics")
  3. this.addOperationLang();
  4. }
  5. this.addOperationLang = function () {
  6. var self = this;
  7. this.semantics.addOperation('parse',
  8. {
  9. all: function (e, _, k) { return { "address": e.parse(), "params": k.parse() } },
  10. address: function (_, e) { return e.parse() },
  11. addr: function (e) { return e.parse() },
  12. props: function (e) { return e.parse() },
  13. props_single: function (e, k) {
  14. return { 'propName': e.parse(), 'propValue': k.parse() };
  15. },
  16. props_rgb: function (_, e) {
  17. return { 'propName': 'color', 'propValue': ['rgb(' + e.parse() + ')'] };
  18. },
  19. props_prop: function (e, k) {
  20. return { 'propName': e.parse(), 'propValue': k.parse() };
  21. },
  22. row: function (_l, e, k, _e) {
  23. let end = k.parse();
  24. if (end.length !== 0) {
  25. return e.parse() + ',' + k.parse();
  26. }
  27. return e.parse()
  28. },
  29. rep: function (_, e) { return e.parse() },
  30. col: function (e) { return e.parse() },
  31. colChar: function (e) { return e.parse() },
  32. number: function (_) { return parseFloat(this.sourceString) },
  33. propSingle: function (_) { return this.sourceString }
  34. });
  35. }
  36. this.getOSC = function (msg) {
  37. this.parseOSC(msg);
  38. }
  39. this.parseOSC = function (msg) {
  40. let str = msg.address + JSON.stringify(msg.args);
  41. var match = this.grammar.match(str, "all");
  42. if (match.succeeded()) {
  43. let res = this.semantics(match).parse();
  44. this.setPropsFromOSC(res);
  45. }
  46. }
  47. this.setPropsFromOSC = function (res) {
  48. //console.log(res);
  49. let address = '/' + res.address.join('/');
  50. let nodeID = vwf.find("", address);
  51. if (res.params.propValue.length == 1) {
  52. vwf_view.kernel.setProperty(nodeID, res.params.propName, [res.params.propValue[0]])
  53. }
  54. if (res.params.propValue.length >= 1) {
  55. vwf_view.kernel.setProperty(nodeID, res.params.propName, [res.params.propValue])
  56. }
  57. }