#include <buttonpanel02.h>


buttonpanel02::buttonpanel02(zprmouse* zm_)
  : zm(zm_)
{
  ratio_center = point2<double>(0.9,0.8);
  ratio_centerdelta = 0.012;
  ratio_radius = 0.008;

/*
  color[0] = point3<uint>(239,151,255);
  color[1] = point3<uint>(127,255,0);
  color[2] = point3<uint>(240,192,10);
  color[3] = point3<uint>(239,254,240);
*/
  color[0] = point3<uint>(255,0,0);
  color[1] = point3<uint>(0,255,0);
  color[2] = point3<uint>(0,0,255);
  color[3] = point3<uint>(127,127,127);

  update();
}

void buttonpanel02::update()
{
  assert(zm);
  if (zm==0)
    return;

  center.x = zm->zz.width * ratio_center.x; 
  center.y = zm->zz.height * ratio_center.y; 

  centerdelta = zm->zz.width * ratio_centerdelta;

  radius = zm->zz.width * ratio_radius;

}


void buttonpanel02::process
(
  bool & isclicked, 
  uint& index, 
  point2<int> const & p
) 
{
  assert(centerdelta>0);
  assert(radius>0);

  isclicked=false;

  assert(zm);
  // Pushing the button triggers two calls, one
  //   down and the other up.  This rejects up.
  if (zm->zz.mouseLeft==false)
    return;


  // Bounding box rejects most cases. 
  if (p.x < (center.x - centerdelta - radius))
    return;
  if (p.x > (center.x + centerdelta + radius))
    return;
  if (p.y < (center.y - centerdelta - radius))
    return;
  if (p.y > (center.y + centerdelta + radius))
    return;

  point2<double> current(p.x,p.y);
  double radius2 = radius*radius;

  for (uint i=0; i<4; ++i)
  {
    point2<double> b0(centerpos(i).x,centerpos(i).y);
    if ((b0-current).dot() < radius2)
    {
      isclicked=true;
      index=i;
      return;
    }
  }

}

void buttonpanel02::drawcircle
(
  point2<int> const & c,
  point3<uint> const & color
) const
{
  int x1=c.x;
  int y1=c.y;

  glPushAttrib(GL_CURRENT_BIT);
  glPushAttrib(GL_LIGHTING_BIT);
  glDisable(GL_LIGHTING);
  glColor3ub(color.x,color.y,color.z);

  doublec rad=3.141592*2.0/360.0;

  glBegin(GL_TRIANGLE_FAN);
  glVertex2f(x1,y1);
  for (double angle=0; angle<=360; angle+=2)
  {
    point2<double> x2(x1+ sin(rad*angle)*radius, y1 + cos(rad*angle)*radius);
//    cout << SHOW(x2) << " ";
    glVertex2d(x2.x,x2.y);
  }
  glEnd();

//cout << endl;

  glPopAttrib();
  glPopAttrib();
}

void buttonpanel02::draw()
{
  assert(zm);

  // Code taken from tutorial
  //   http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0,zm->zz.width,zm->zz.height,0,0,1);
  glDisable(GL_DEPTH_TEST);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  glTranslatef(0.375,0.375,0);

/*
  radius=5;
  int x1=320;
  int y1=240;

  drawcircle(x1,y1,point3<uint>(255,0,0)); 
*/

  drawcircle(centerpos(0),color[0]);
  drawcircle(centerpos(1),color[1]);
  drawcircle(centerpos(2),color[2]);
  drawcircle(centerpos(3),color[3]);

  // Returning to previous state.
  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);

  glEnable(GL_DEPTH_TEST);
} 


point2<int> buttonpanel02::centerpos(uintc i) const
{
  assert(i<4);

  if (i==0)
    return point2<int>(center.x,center.y-centerdelta);
  if (i==1)
    return point2<int>(center.x+centerdelta,center.y);
  if (i==2)
    return point2<int>(center.x,center.y+centerdelta);

  return point2<int>(center.x-centerdelta,center.y);
}






