#include "PowerSelector.h"

PowerSelector::PowerSelector()
{
}

PowerSelector::~PowerSelector()
{
}

void PowerSelector::clear()
{
  selected=-1;
  previousSelected=-1;
  indyWid=0;
  indyHei=0;
}

int PowerSelector::getSelected()
{
  return selected;
}

int PowerSelector::getPreviousSelected()
{
  return previousSelected;
}

//changes the selected item, sets the previously selected item.
void PowerSelector::setSelected(int sel)
{
  previousSelected=selected;
  selected=sel;
}

//get and set the dimensions of an individual box.
void PowerSelector::setIndividualBoxDimensions (uint16_t w, uint16_t h)
{
  indyWid=w;
  indyHei=h;
}

uint16_t PowerSelector::getIndividualBoxWidth ()
{
  return indyWid;
}

uint16_t PowerSelector::getIndividualBoxHeight ()
{
  return indyHei;
}

//If the mouse clicks at this position, return the index of the selected power that was hit.
//returns -1 if the mouse is outside of the hitzone.
int PowerSelector::mouseClickUpdate (int mouseX, int mouseY, int mouseButton)
{
  int width=indyWid * 4, height=indyHei * 4;
  int leftSide=posX-(indyWid * 2), topSide=posY-(indyHei * 3 / 2);

  if (!pointRectangleCollision(mouseX, mouseY, leftSide, leftSide+width, topSide, topSide+height))
    return -1;

  int xPick, yPick;
  xPick=(mouseX-leftSide)/indyWid;
  yPick=(mouseY-topSide)/indyHei;

  int selectedElement;

  if (xPick > 2)
    selectedElement=9;
  else
    selectedElement=(xPick+(yPick*3));
  return selectedElement;
}

//Setting and getting position
void PowerSelector::setPositionX (int p)
{ posX=p; }

int PowerSelector::getPositionX()
{ return posX; }

void PowerSelector::setPositionY (int p)
{ posY=p; }

int PowerSelector::getPositionY()
{ return posY; }
