// -*- C++ -*-
// $RCSfile: materialcmd.C,v $
// $Revision: 1.4 $
// $Author: langer $
// $Date: 2000/10/30 19:23:27 $

/* This software was produced by NIST, an agency of the U.S. government,
 * and by statute is not subject to copyright in the United States.
 * Recipients of this software assume all responsibilities associated
 * with its operation, modification and maintenance. However, to
 * facilitate maintenance we ask that before distributing modifed
 * versions of this software, you first contact the authors at
 * oof_manager@ctcms.nist.gov. 
 */

#ifndef MATERIALCMD_C
#define MATERIALCMD_C

#include "goof.h"
#include "cell_coordinate.h"
#include "materialcmd.h"
#include "pixelgroups.h"

template <class TYPE>
MaterialCmd<TYPE>::MaterialCmd()
  : CommandM(TYPE::tag(), "create " + TYPE::tag() + " material")
{
  TYPE::AddArgs(this); // puts static default params in arg list
}

template <class TYPE>  
void MaterialCmd<TYPE>::func() {
  CharString invocation = garcon()->get_command() + " " + garcon()->get_args();
  if(garcon()->get_command(1) == "default") {
    if(defaultmaterial) delete defaultmaterial;
    defaultmaterial = new TYPE;
    defaultmaterial->parameters = invocation;
  }
  else {
    if(!goofinit()) return;
    // assign material properties to selected elements

    // Allocate one instance of a new Material, and check to see if
    // it's the same as a previously allocated material. If it is,
    // delete the new one and use the old one instead. It's necessary
    // to do this so that distinct materials can be distinguished
    // easily.
    Material *newmaterial = new TYPE; // small memory leak if this is kept!
    int match = 0;		// is this material the same as a previous one?
    int i;
    for(i=0; i<Material::allmaterials.capacity() && !match; i++) {
      if(*newmaterial == *allmaterials[i]) {
	delete newmaterial;
	newmaterial = allmaterials[i];
	match = 1;
      }
    }
    if(!match) {
      newmaterial->parameters = invocation;
      newmaterial->set_index(allmaterials.capacity());
      allmaterials.grow(1, newmaterial);
    }
    // make all selected pixels point to the new Material
    for(i=0; i<current_goof->selected().size(); i++) {
      Cell_coordinate which = current_goof->selected()[i];
      current_goof->assign_material(which, newmaterial);
    }
    current_goof->redraw();
  }
}

template <class TYPE>  
CommandM *MaterialCmd<TYPE>::clone() const {
  MaterialCmd<TYPE> *mc = new MaterialCmd<TYPE>;
  return mc;
}

//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//=\\=//

// include template definitions

#include "groups.C"

#endif
