123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
- // Secretary of Defense (Personnel & Readiness).
- //
- // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- // in compliance with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software distributed under the License
- // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- // or implied. See the License for the specific language governing permissions and limitations under
- // the License.
- /// vwf/model/test.js is a dummy driver used for tests.
- ///
- /// @module vwf/model/test
- /// @requires vwf/model
- define( [ "module",
- "vwf/model",
- "vwf/utility",
- "vwf/utility/color",
- "mil-sym/cws",
- "jquery" ],
- function( module, model, utility, Color, cws, $ ) {
- var modelDriver;
- return model.load( module, {
- // == Module Definition ====================================================================
- // -- initialize ---------------------------------------------------------------------------
- initialize: function( options ) {
-
- modelDriver = this;
- this.arguments = Array.prototype.slice.call( arguments );
- if ( options === undefined ) { options = {}; }
- this.state = {
- "nodes": {},
- "prototypes": {},
- "createNode": function( nodeType, nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback ) {
- return {
- "nodeType": nodeType,
- "parentID": nodeID,
- "ID": childID,
- "extendsID": childExtendsID,
- "implementsIDs": childImplementsIDs,
- "source": childSource,
- "type": childType,
- "name": childName
- };
- }
- };
- // turns on logger debugger console messages
- this.debug = {
- "creation": false,
- "initializing": false,
- "parenting": false,
- "deleting": false,
- "properties": false,
- "setting": false,
- "getting": false,
- "prototypes": false
- };
- },
- creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback ) {
- // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
- // the scene or a prototype. In either of those cases, save the uri of the new node
- var childURI = ( nodeID === 0 ? childIndex : undefined );
- var appID = this.kernel.application();
- if ( this.debug.creation ) {
- this.logger.infox( "creatingNode", nodeID, childID, childExtendsID, childImplementsIDs, childSource, childType, childName );
- }
- // If the node being created is a prototype, construct it and add it to the array of prototypes,
- // and then return
- var prototypeID = utility.ifPrototypeGetId( appID, this.state.prototypes, nodeID, childID );
- if ( prototypeID !== undefined ) {
-
- if ( this.debug.prototypes ) {
- this.logger.infox( "prototype: ", prototypeID );
- }
- this.state.prototypes[ prototypeID ] = {
- parentID: nodeID,
- ID: childID,
- extendsID: childExtendsID,
- implementsID: childImplementsIDs,
- source: childSource,
- type: childType,
- uri: childURI,
- name: childName,
- };
- return;
- }
- var protos = getPrototypes( childExtendsID );
- var node = this.state.nodes[ childID ];
- if ( node === undefined ) {
- if ( isUnitNode( protos ) ) {
- this.state.nodes[ childID ] = node = this.state.createNode( "unit", nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback );
-
- // additional members for the unit components
- node.symbolID = undefined;
- node.modifiers = {};
- node.image = undefined;
- node.description = undefined;
- node.tagName = undefined;
- node.fullName = undefined;
- node.echelon = undefined;
- node.affiliation = undefined;
- } else if ( isModifierNode( protos ) ) {
- this.state.nodes[ childID ] = node = this.state.createNode( "modifier", nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback );
- }
- }
- },
- initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName ) {
- if ( this.debug.initializing ) {
- this.logger.infox( "initializingNode", nodeID, childID, childExtendsID, childImplementsIDs, childSource, childType, childName );
- }
- },
- deletingNode: function( nodeID ) {
- if ( this.debug.deleting ) {
- this.logger.infox( "deletingNode", nodeID );
- }
- if ( this.state.nodes[ nodeID ] !== undefined ) {
- delete this.state.nodes[ nodeID ];
- }
- },
- // addingChild: function( nodeID, childID, childName ) {
- // if ( this.debug.parenting ) {
- // this.logger.infox( "addingChild", nodeID, childID, childName );
- // }
- // },
- // movingChild: function( nodeID, childID, childName ) {
- // if ( this.debug.parenting ) {
- // this.logger.infox( "movingChild", nodeID, childID, childName );
- // }
- // },
- // removingChild: function( nodeID, childID, childName ) {
- // if ( this.debug.parenting ) {
- // this.logger.infox( "removingChild", nodeID, childID, childName );
- // }
- // },
- // -- creatingProperty ---------------------------------------------------------------------
- creatingProperty: function( nodeID, propertyName, propertyValue ) {
- if ( this.debug.properties ) {
- this.logger.infox( "C === creatingProperty ", nodeID, propertyName, propertyValue );
- }
- return this.settingProperty( nodeID, propertyName, propertyValue );
- },
- // -- initializingProperty -----------------------------------------------------------------
- initializingProperty: function( nodeID, propertyName, propertyValue ) {
- if ( this.debug.properties ) {
- this.logger.infox( " I === initializingProperty ", nodeID, propertyName, propertyValue );
- }
- return this.settingProperty( nodeID, propertyName, propertyValue );
- },
- // -- settingProperty ----------------------------------------------------------------------
- settingProperty: function( nodeID, propertyName, propertyValue ) {
- if ( this.debug.properties || this.debug.setting ) {
- this.logger.infox( " S === settingProperty ", nodeID, propertyName, propertyValue );
- }
- var node = this.state.nodes[ nodeID ];
- var value = undefined;
- var renderImage = false;
- if ( node !== undefined && ( utility.validObject( propertyValue ) ) ) {
- if ( node.nodeType === "unit" ) {
-
- switch ( propertyName ) {
- case "symbolID":
- value = node.symbolID = propertyValue;
- if ( node.echelon !== undefined ) {
- node.symbolID = cws.addEchelonToSymbolId( node.symbolID, node.echelon );
- }
- if ( node.affiliation !== undefined ) {
- node.symbolID = cws.addAffiliationToSymbolId( node.symbolID, node.affiliation );
- }
- renderImage = true;
- break;
- case "image":
- value = node.image = propertyValue;
- break;
- case "description":
- value = node.description = propertyValue;
- break;
- case "tagName":
- value = node.tagName = propertyValue;
- break;
- case "fullName":
- value = node.fullName = propertyValue;
- break;
- case "echelon":
- if ( node.echelon !== propertyValue ) {
- switch( propertyValue ) {
-
- case "team":
- case "crew":
- case "squad":
- case "section":
- case "platoon":
- case "detachment":
- case "company":
- case "battery":
- case "troop":
- case "battalion":
- case "squadron":
- case "regiment":
- case "group":
- case "brigade":
- case "division":
- case "corps":
- case "mef":
- case "army":
- case "army group":
- case "front":
- case "region":
- case "null":
- case "none":
- if ( node.symbolID !== undefined ) {
- node.symbolID = cws.addEchelonToSymbolId( node.symbolID, propertyValue );
- }
- node.echelon = propertyValue;
- renderImage = true;
- break;
- default:
- this.logger.warnx( "incorrect echelon property value: " + propertyValue );
- break;
- }
- }
- break;
- case "affiliation":
- if ( node.affiliation !== propertyValue ) {
- switch( propertyValue ) {
- case "unknown":
- case "neutral":
- case "hostile":
- case "friendly":
- if ( node.symbolID !== undefined ) {
- node.symbolID = cws.addAffiliationToSymbolId( node.symbolID, propertyValue );
- }
- node.affiliation = propertyValue;
- renderImage = true;
- break;
- default:
- this.logger.warnx( "incorrect affiliation property value: " + propertyValue );
- break;
- }
- }
- break;
- }
- } else if ( node.nodeType === "modifier" ) {
-
- var unit = this.state.nodes[ node.parentID ];
- var msa = armyc2.c2sd.renderer.utilities.MilStdAttributes;
- var mu = armyc2.c2sd.renderer.utilities.ModifiersUnits;
- if ( unit === undefined ) {
- return undefined;
- }
-
- // Render image if modifier is valid
- renderImage = setModifier( unit, propertyName, propertyValue );
-
- }
- }
- if ( node !== undefined ) {
- // if node is defined then it is either a unit or a modifier
- // if nodeType is a modifier, then the parent is the unit
- var unitNode = node.nodeType === "modifier" ? this.state.nodes[ node.parentID ] : node;
- if ( unitNode && renderImage ) {
- render( unitNode );
- }
- }
- return value;
- },
- // -- gettingProperty ----------------------------------------------------------------------
- gettingProperty: function( nodeID, propertyName ) {
- if ( this.debug.properties || this.debug.getting ) {
- this.logger.infox( " G === gettingProperty ", nodeID, propertyName );
- }
- var node = this.state.nodes[ nodeID ];
- var value = undefined;
- //this driver has no representation of this node, so there is nothing to do.
- if( node === undefined ) return value;
- if ( node.nodeType === "unit" ) {
-
- switch ( propertyName ) {
- case "symbolID":
- value = node.symbolID;
- break;
- case "image":
- value = node.image;
- break;
- case "description":
- value = node.description;
- break;
- case "fullName":
- value = node.fullName;
- break;
- case "tagName":
- value = node.tagName;
- break;
- }
- } else if ( node.nodeType === "modifier" ) {
-
- var unit = this.state.nodes[ node.parentID ];
- var msa = armyc2.c2sd.renderer.utilities.MilStdAttributes;
- var mu = armyc2.c2sd.renderer.utilities.ModifiersUnits;
- if ( unit === undefined ) {
- return undefined;
- }
- value = getModifier( unit, propertyName );
-
- }
- return value;
- },
- // -- callingMethod --------------------------------------------------------------------------
- callingMethod: function( nodeID, methodName /* [, parameter1, parameter2, ... ] */ ) { // TODO: parameters
-
- var node = this.state.nodes[ nodeID ];
- var value = undefined;
- switch( methodName ) {
- case "render":
- value = render( node );
- break;
- }
- return value;
- },
- // TODO: creatingEvent, deltetingEvent, firingEvent
- // -- executing ------------------------------------------------------------------------------
- // executing: function( nodeID, scriptText, scriptType ) {
- // return undefined;
- // },
- // == ticking =============================================================================
- // ticking: function( vwfTime ) {
-
- // }
- } );
- function getPrototypes( extendsID ) {
- var prototypes = [];
- var id = extendsID;
- while ( id !== undefined ) {
- prototypes.push( id );
- id = modelDriver.kernel.prototype( id );
- }
-
- return prototypes;
- }
- function isUnitNode( prototypes ) {
- var found = false;
- if ( prototypes ) {
- for ( var i = 0; i < prototypes.length && !found; i++ ) {
- found = ( prototypes[i] == "http://vwf.example.com/mil-sym/unit.vwf" );
- }
- }
- return found;
- }
- function isModifierNode( prototypes ) {
- var found = false;
- if ( prototypes ) {
- for ( var i = 0; i < prototypes.length && !found; i++ ) {
- found = ( prototypes[i] == "http://vwf.example.com/mil-sym/modifier.vwf" );
- }
- }
- return found;
- }
- function render( node ) {
- if ( node === undefined ) {
- return;
- }
- var value = undefined;
-
- if ( node !== undefined && node.nodeType === "unit" && node.symbolID !== undefined ) {
- var iconRender = armyc2.c2sd.renderer.MilStdIconRenderer;
- var img = iconRender.Render( node.symbolID, node.modifiers );
-
- var centerPt = img.getCenterPoint();
- var imgSize = img.getImageBounds();
- var symbolBounds = img.getSymbolBounds();
- value = node.image = img.toDataUrl();
- // we should really use the event, but we're having troubles
- // getting the events to replicate, this should be switched
- // back to the event when the replication is fixed. handleRender
- // can then be completely removed
- //modelDriver.kernel.fireEvent( node.ID, "imageRendered", [ node.image, imgSize, centerPt, symbolBounds ] );
-
- modelDriver.kernel.callMethod( node.ID, "handleRender", [ node.image, imgSize, centerPt, symbolBounds ] );
- }
- return value;
- }
- function setModifier( unit, modifierAlias, modifierValue ) {
-
- var modObj = cws.modifierByAlias( modifierAlias );
- var msa = armyc2.c2sd.renderer.utilities.MilStdAttributes;
- var mu = armyc2.c2sd.renderer.utilities.ModifiersUnits;
- var modifierSet = false;
-
- if ( modObj !== undefined ) {
-
- var modifierActualName;
-
- switch ( modObj.type ) {
- case "ModifiersUnits":
- modifierActualName = mu[ modObj.modifier ];
- break;
-
- case "MilStdAttributes":
- modifierActualName = msa[ modObj.modifier ];
- break;
-
- default:
- modelDriver.logger.errorx( "setModifier", "Unknown type (", modObj.type, ") specified." );
- return modifierSet;
- }
-
- if ( modifierValue === "" ) {
- if ( unit.modifiers[ modifierActualName ] !== undefined ) {
- delete unit.modifiers[ modifierActualName ];
- modifierSet = true;
- }
- } else {
- switch ( modObj.valueType ) {
- case "Boolean":
- unit.modifiers[ modifierActualName ] = Boolean(modifierValue);
- break;
-
- case "Number":
- unit.modifiers[ modifierActualName ] = Number(modifierValue);
- break;
-
- case "Array":
- case "Text":
- default:
- unit.modifiers[ modifierActualName ] = modifierValue;
- break;
- }
- modifierSet = true;
- }
- }
-
- return modifierSet;
- }
-
- function getModifier( unit, modifierAlias ) {
-
- var modObj = cws.modifierByAlias( modifierAlias );
- var msa = armyc2.c2sd.renderer.utilities.MilStdAttributes;
- var mu = armyc2.c2sd.renderer.utilities.ModifiersUnits;
- var value = undefined;
-
- if ( modObj !== undefined ) {
-
- var modifierActualName;
-
- switch ( modObj.type ) {
- case "ModifiersUnits":
- modifierActualName = mu[ modObj.modifier ];
- break;
-
- case "MilStdAttributes":
- modifierActualName = msa[ modObj.modifier ];
- break;
-
- default:
- modelDriver.logger.errorx( "getModifier", "Unknown type (", modObj.type, ") specified." );
- return value;
- }
- value = unit.modifiers[ modifierActualName ];
- }
-
- return value;
- }
-
- } );
|