# A-Frame & VWF & OSC simple scene
# Copyright 2017 Krestianstvo.org project
---
extends: http://vwf.example.com/aframe/ascene.vwf
properties:
  assets: "assets.json"
children:
  fog:
    extends: http://vwf.example.com/aframe/aSceneFogComponent.vwf
    type: "component"
    properties:
      fogType: "linear"
      fogColor: "#ECECEC"
      far: 20
      near: 0
  myLight:
    extends: http://vwf.example.com/aframe/alight.vwf
    properties:
      type: "point"
      color: "white"
      position: "0 10 5"
      rotation: "0 0 0"
  groundPlane:
    extends: http://vwf.example.com/aframe/aplane.vwf
    properties:
        height: 50
        width: 50
        rotation: [-90, 0, 0]
    children:
      material:
        extends: http://vwf.example.com/aframe/aMaterialComponent.vwf
        properties:
          wireframe: false
          src: "#bg2"
          repeat: "10 10"
  spaceText:
    extends: http://vwf.example.com/aframe/atext.vwf
    properties:
      value: "Virtual World Framework & A-Frame & OSC"
      color: "#2b5d83"
      position: [-2, 2.5, -3]
  cube:
    extends: http://vwf.example.com/aframe/abox.vwf
    properties:
      position: "0 1 -3"
      rotation: "0 0 0"
      depth: 1
      height: 1
      width: 1
    children:
      material:
        extends: http://vwf.example.com/aframe/aMaterialComponent.vwf
        properties:
          color: "#3c7249"
      sphere:
        extends: http://vwf.example.com/aframe/asphere.vwf
        properties:
          position: "0 2 0"
          rotation: "0 0 0"
          radius: 0.4
        children:
          material:
            extends: http://vwf.example.com/aframe/aMaterialComponent.vwf
            properties:
              color: "#3c7249"
  newSky:
    extends: http://vwf.example.com/aframe/aentity.vwf
    children:
      skyshader:
        extends: http://vwf.example.com/aframe/app-skyshader-component.vwf
  oscLang:
    extends: http://vwf.example.com/ohm/node.vwf
    properties:
      osc: true
      grammar:
      semantics:
      ohmLang: |
                parseOSC {
                            all = address ":" props
                            address = ("/" addr)*
                            addr = ~("/") propSingle
                            props 
                                = propSingle row  -- single
                                | "rgb" row       -- rgb
                                | propSingle number -- prop
                            row = "[" col rep "]"
                            rep = ("," col)*
                            col = colChar*
                            colChar = ~("[" | "," | "]") number
                            propSingle = ~("rgb") letter*
                            number  (a number)
                                = digit* "." digit+  -- fract
                                | digit+             -- whole
                            }
    methods:
      initLang:
        body: |
              console.log("add operations to semantics")
              this.addOperationLang();
      addOperationLang:
        body: |
              var self = this;
              this.semantics.addOperation('parse',
              {
                  all: function(e, _, k){ return {"address": e.parse(), "params": k.parse()} },
                  address: function(_, e){ return e.parse()},
                  addr: function(e) {return e.parse() },
                  props: function(e) {return e.parse()},
                  props_single: function( e, k){
                          return {'propName': e.parse(), 'propValue': k.parse()};
                  },
                  props_rgb: function(_, e)
                  {
                      return {'propName': 'color', 'propValue': ['rgb('+ e.parse() + ')']};
                  },
                  props_prop: function(e, k){
                          return {'propName': e.parse(), 'propValue': k.parse()};
                  },
                  row: function(_l, e, k, _e){
                         let end = k.parse();
                          if (end.length !== 0) {
                           return e.parse() +','+ k.parse();
                         }
                         return e.parse()    
                  },
                  rep: function(_, e){ return e.parse()},
                  col: function(e) {return e.parse()},
                  colChar: function(e) {return e.parse()},
                  number: function(_) {return parseFloat(this.sourceString)},
                  propSingle: function(_){return this.sourceString}
              });
      getOSC:
        parameters:
          - msg
        body: |
              this.parseOSC(msg);
      parseOSC:
        parameters:
          - msg
        body: |
                let str = msg.address + JSON.stringify(msg.args);
                var match = this.grammar.match(str, "all");
                if (match.succeeded())
                {
                    let res = this.semantics(match).parse();
                    this.setPropsFromOSC(res);
                }
      setPropsFromOSC:
        parameters:
          - res
        body: |
              //console.log(res);
              let address = '/'+res.address.join('/');
              let nodeID = vwf.find("", address);
              if (res.params.propValue.length == 1) {
                  vwf_view.kernel.setProperty(nodeID, res.params.propName, [res.params.propValue[0]])
              }
              if (res.params.propValue.length >= 1) {
                  vwf_view.kernel.setProperty(nodeID, res.params.propName, [res.params.propValue])
              }