
/**
 * MyGUIGroup allows objects to be grouped and moved / rotated, enabled/disabled, etc. together while still
 * receiving local mouse coordinates. This difers from a Panel object because it has no width, but its own
 * internal coordinate system. Objects placed in the group will be translated, rotated, and scaled before their
 * own draw methods are called. MyGUIGroup is very similar to MyGUI altho it inherits its basic properties from
 * MyGUIObject.
 */
public class MyGUIGroup extends MyGUIObject {
  // core variables (similar to MyGUI)
  private MyGUIObject[] elements;
  private int focusIndex = -1;
  private int numElements = 0;
  private boolean customMouse = false;
  private int tmouseX = 0, tmouseY = 0;
  
  // default constructor
  public MyGUIGroup(int x, int y) {
    super(x, y);
    elements = new MyGUIObject[25];
    this._style = new MyGUIStyle();    
  }
  // constructor where you can specify the size of the element array
  public MyGUIGroup(int x, int y, int size) {
    super(x, y);
    elements = new MyGUIObject[size];
    this._style = new MyGUIStyle();
  }
  // constructor where you can specify the size of the element array and set the style
  public MyGUIGroup(int x, int y, int size, MyGUIStyle style) {
    super(x, y);
    elements = new MyGUIObject[size];
    this._style = style;
  }
  // cross breed constructor where you can set the style, but the element array stays at the default
  public MyGUIGroup(int x, int y, MyGUIStyle style) {
    super(x, y);
    elements = new MyGUIObject[25];
    this._style = style;
  }
   
  /* Methods which affect the properties of the collection */
  // this flavour of add puts new element additions at the end of the elements array
  // returns the index of the new element, or null if there is no more space in array
  public int add(MyGUIObject newElement) {
    // See if there are any null entries to replace
    for(int i=0; i<numElements; i++) {
      if(elements[i] == null) {
        elements[i] = newElement;
        elements[i].setParent(this);
        elements[i].setID(i);
        return i;
      }
    }
    // If there were no null elements, then add to the end (providing there is space)
    if(numElements < elements.length) {
      // Assign new element to array and increment element counter
      int elementID = numElements;
      numElements++;
      elements[elementID] = newElement;
      elements[elementID].setParent(this);
      elements[elementID].setID(elementID);
      return elementID;
    }
    return -1;
  }  

  public boolean remove(int id) {
    if(id > 0 && id <= numElements) {
      elements[id] = null;
      return true;
    }
    return false;
  }

  // this function sets the style after initialisation
  public void setStyle(MyGUIStyle style) {
    this._style = style;
  }

  /**
   * This method overides the default MyGUIObject method setParent because it has children
   * that need to be updated when this (the parent object) is updated.
   */
  void setParent(MyGUIObject parent) {
    // Let local values
    this._parent = parent;
    this._root = parent._root;
    // Loop through children and pass on call to setParent
    for(int i=0; i<numElements; i++) {
      // but only if they exist (!= null)
      // and only if they are not disabled
      if(elements[i] != null)
        if(!elements[i]._disabled && elements[i]._visible )
          elements[i].setParent(this);
    }
  }
      
  // this function tells MyGUI that you want to use custom mouse coordinates
  public void useCustomMouse(boolean mode) {
    customMouse = mode;
  }
  
  // this function lets you specify custom mouse coordinates
  public void setMouseCoords(int x, int y) {
    tmouseX = x;
    tmouseY = y;
  }

  /* Methods which return values about the collection */
  /**
   * This method returns the MyGUIObject that current has focus
   * It will return null if no element is currently selected.
   */
  public MyGUIObject getFocused() {
    if(focusIndex != -1) {
      return elements[focusIndex];
    }
    return null;
  }
  
  /**
   * This method returns the index of the currently focused element. If no element
   * has focus then -1 will be returned. 0 is a valid index/id for an element.
   * You may need to check that id returned doesn't reference a group object that contains
   * other elments, because there may be another focused child inside it.
   */
  public int getFocusIndex() {
    return focusIndex;
  }

  // get by specified index
  public MyGUIObject get(int index) {
    if(index < numElements && index > 0) {
      return elements[focusIndex];
    }
    return null;
  }

  // is empty?
  public boolean isEmpty() {
    if(numElements == 0) {
      return true;
    }
    return false;
  }

  // is full? check if there are no null spaces
  public boolean isFull() {
    // quick check, space at end of array?
    if(numElements != elements.length) {
      return false;
    }
    // longer check for null spaces
    for(int i=0; i<numElements; i++) {
      if(elements[i] == null) return false;
    }
    // If numElements == elements.length and there are no null entries:
    return true;
  }  
      
  /**
   * Note when this method is called an element in MyGUIGroup is given focus
   * Focus is a state necessary for a GUI element to perform actions. Elements with higher
   * indexes will get focus priority over lower indexes. Higher index elements
   * are also drawn last and so will appear on-top of other objects.
   * The focus needs to be decided prior to passing mousePressed as mousePressed may rely
   * on the state of focusIndex before it can work properly.
   */
  public void mousePressed() {
    // clear focus (will be set again below)
    focusIndex = -1;
    // loop through collection of objects and pass on call to mousePressed
    for(int i=numElements-1; i>=0; i--) {
      // keep track of which element has focus
      if(elements[i] != null) {
        // If the element is underneath the cursor, and the element is not disabled, and it is visible.
        if(elements[i].checkForHit() && !(elements[i]._disabled || !elements[i]._visible)) {
          // match found
          focusIndex = i;
          // kill loop
          i = -1;
        }
      }
    }
    // loop again after focusIndex has been decided
    for(int i=0; i<numElements; i++) {
      // pass on mousePressed command to elements in array
      // but only if they exist (!= null)
      // and only if they are not disabled
      if(elements[i] != null)
        if(!elements[i]._disabled && elements[i]._visible)
          elements[i].mousePressed();
    }
  }

  public void mouseReleased() {
    // Loop through collection of objects and pass on call to mouseReleased
    for(int i=0; i<numElements; i++) {
      // but only if they exist (!= null)
      // and only if they are not disabled
      if(elements[i] != null)
        if(!elements[i]._disabled && elements[i]._visible )
          elements[i].mouseReleased();
    }
  }

  public void mouseDragged() {
    // Loop through collection of objects and pass on call to mouseDragged
    for(int i=0; i<numElements; i++) {
      // but only if they exist (!= null)
      // and only if they are not disabled
      if(elements[i] != null)
        if(!elements[i]._disabled && elements[i]._visible)
          elements[i].mouseDragged();
    }
  }

  /* Methods for handling key events */
  public void keyPressed(KeyEvent e) {
    
  }
  public void keyReleased(KeyEvent e) {
    
  }

  /* Draw */
  // Draw has been separated into two logical methods
  // void drawStates() will actually update all the draw methods of components
  // void draw() has a logic switch that prevents it from calling drawStates() if in
  // lightWeightMode. When in lightWeightMode, components will only be updated if 
  // a mouse, or key event is detected.
  public void draw() {
    // The whole coordinate system needs moving, rotating and scaling
    pushMatrix();
    translate(_x, _y);
    rotate(radians(_rotation));
    scale(_scale);
    // If a custom mouse is not being used, then copy them over
    updateLocalMouse();
    // Loop through collection of objects and call their draw methods
    for(int i=0; i<numElements; i++) {
      // Check element isn't the focus object (i != focusindex)
      // and check there is an element at this position (!= null)
      if(i != focusIndex && elements[i] != null)
        // Check if object is visible
        if(elements[i]._visible)
          elements[i].draw();
    }
    if(focusIndex >= 0 && focusIndex < numElements) {
      if(elements[focusIndex] != null) {
        if(elements[focusIndex]._visible)
          elements[focusIndex].draw();
      }
    }
    // reset matrix, end of group draw
    popMatrix();
  }
}
