/*
This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
MaterialsModel = require("js/models/materials-model").MaterialsModel,
NJUtils = require("js/lib/NJUtils").NJUtils,
World = require("js/lib/drawing/world").World,
Rectangle = require("js/lib/geom/rectangle").Rectangle,
ShapesController = require("js/controllers/elements/shapes-controller").ShapesController;
////////////////////////////////////////////////////////////////////////
//Exporting as MaterialsPopup
exports.MaterialsPopup = Montage.create(Component, {
////////////////////////////////////////////////////////////////////
okButton: {
value: null,
serializable: true
},
cancelButton: {
value: null,
serializable: true
},
saveAsButton: {
value: null,
serializable: true
},
resetButton: {
value: null,
serializable: true
},
materialsLibraryRef: {
value: null
},
materialTitle: {
value: null,
serializable: true
},
previewCanvas: {
value: null,
serializable: true
},
previewShape: {
value: null,
serializable: true
},
// Material Properties
_materialName: {
enumerable: true,
value: ""
},
_useSelection: { value: false, enumerable: true },
_whichMaterial: { value: "fill", enumerable: true },
_originalValues: {value: null, enumerable: true },
captureAction: {
value:function(event) {
switch(event._currentTarget.label)
{
case "Cancel":
// console.log("Cancel material edit");
this.revertToOriginalValues();
break;
case "OK":
// console.log("Committing material with the following values:");
for(var i=0, len=this.materialsProperties.childComponents.length; i< len; i++)
{
var childControl = this.materialsProperties.childComponents[i];
var childValue = childControl._control[childControl._prop];
if(typeof childValue === "object")
{
console.log(childControl.label + " is ");
console.dir(childValue);
}
else
{
console.log(childControl.label + " is " + childValue);
}
console.log("--------------");
}
break;
case "Save As...":
this.saveAs();
break;
case "Reset":
this.reset();
break;
}
// Notify Materials Library to close popup
NJevent("hideMaterialPopup");
}
},
saveAs:
{
value: function()
{
// console.log("Save As...");
var materialCopy = prompt("Save material as", this._materialName + "_Copy");
if (materialCopy)
{
this.materialsLibraryRef.duplicateMaterial(materialCopy);
}
}
},
reset:
{
value: function()
{
// console.log("Reset");
}
},
revertToOriginalValues:
{
value: function()
{
if (this._originalValues)
{
this._material.importJSON( this._originalValues );
if (this._useSelection)
{
var selection = this.application.ninja.selectedElements;
if (selection && (selection.length > 0))
{
var nObjs = selection.length;
for (var iObj=0; iObj 0))
{
var nObjs = selection.length;
for (var iObj=0; iObj 0))
{
var index = value.lastIndexOf( "/" );
if (index < 0) index = value.lastIndexOf( "\\" );
if (index >= 0)
{
value = value.substr( index+1 );
value = "assets/images/" + value;
}
rtnValue = value.slice(0);
}
break;
case "checkbox":
rtnValue = value;
break;
case "gradient":
rtnValue = value;
break;
default:
console.log( "unrecognized material control type: " + type );
break;
}
return rtnValue;
}
},
////////////////////////////////////////////////////////////////////
//
prepareForDraw: {
enumerable: false,
value: function() {
this.cancelButton.addEventListener("action", this, true);
this.okButton.addEventListener("action", this, true);
this.saveAsButton.addEventListener("action", this, true);
this.resetButton.addEventListener("action", this, true);
if (!this.previewCanvas.getAttribute( "data-RDGE-id" )) {
this.previewCanvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() );
}
}
},
////////////////////////////////////////////////////////////////////
//
didDraw: {
enumerable: false,
value: function() {
var world;
if(!this.previewShape) {
world = new World(this.previewCanvas, true);
this.previewShape = Object.create(Rectangle, {});
this.previewShape.init(world, 0, 0, 200, 100, 1, [0,0,0,1], [1,1,1,1],
0, 0, 0, 0, null, null, "Solid");
world.addObject(this.previewShape);
} else {
world = this.previewShape.getWorld();
}
this.materialTitle.innerHTML = this._materialName;
this.previewShape.setFillMaterial(this._material);
this.previewShape.buildBuffers();
world.restartRenderLoop();
}
},
//Garbage collection (Manual method)
destroy: {
enumerable: false,
value: function() {
// console.log("cleanup routines here");
this.previewShape.getWorld().stop();
}
},
loadMaterials:
{
enumerable: true,
value: function(materialID, useSelection, whichMaterial)
{
//TODO - Hack to force repetition to draw. Setting .length = 0 did not work.
this.materialsData = [];
var material;
this._materialName = materialID;
if (useSelection)
{
this._useSelection = true;
var selection = this.application.ninja.selectedElements;
if (selection && (selection.length > 0))
{
var canvas = selection[0];
var obj;
this._whichMaterial = whichMaterial;
if (canvas.elementModel && canvas.elementModel.shapeModel) obj = canvas.elementModel.shapeModel.GLGeomObj;
if (obj)
material = (whichMaterial === 'stroke') ? obj.getStrokeMaterial() : obj.getFillMaterial();
}
}
else
{
this._useSelection = false;
material = MaterialsModel.getMaterial( materialID );
}
if (material)
{
this._material = material;
this._originalValues = material.exportJSON();
if((materialID === "Linear Gradient") || (materialID === "Radial Gradient")) {
this.materialsData = this.getEditableProperties( material );
} else {
this.materialsData = this.getMaterialData( material );
}
}
else
{
this.materialsData = this[materialID];
}
this.needsDraw = true;
}
},
getEditableProperties: {
value: function(material) {
// declare the array to hold the results
var rtnArray = [],
obj,
colorObj = ShapesController.getMaterialColor(material.getName());
this._propNames = ["gradient"];
this._propValues = ["gradient"];
this._propTypes = ["gradient"];
this._propLabels = ["gradient"];
obj = this.createGradientData("gradient", colorObj);
rtnArray.push(obj);
return rtnArray;
}
},
getMaterialData:
{
value: function( material )
{
// declare the array to hold the results
var rtnArray = [];
var propNames = [], propValues = [], propTypes = [], propLabels = [];
this._propNames = propNames;
this._propValues = propValues;
this._propTypes = propTypes;
this._propLabels = propLabels;
material.getAllProperties( propNames, propValues, propTypes, propLabels);
var n = propNames.length;
for (var i=0; i 2)
{
obj["defaults"]["data"][2] =
{
"label": "Z",
"description": "Z value",
"controlType": "HotText",
"defaults":
{
"minValue": -1.e8,
"maxValue": 1.e8,
"value": value[2]
}
}
}
return obj;
}
},
createGradientData:
{
value: function( label, colorObj )
{
var obj = {
"label": label,
"description": "a gradient",
"controlType": "GradientPicker",
"defaults":
{
"_mode": colorObj.gradientMode,
"value": colorObj.color
}
};
return obj;
}
},
materialsProperties: {
serializable: true,
value: null
},
_materialsData: {
enumerable: true,
serializable: true,
value: []
},
materialsData: {
enumerable: true,
serializable: true,
get: function() {
return this._materialsData;
},
set: function(data) {
this._materialsData = data;
if(this.materialsProperties && data.length)
{
this.materialsProperties.needsDraw = true;
}
}
}
});