#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "Controle.h"
#include "PotentiometreCirculaire.h"
#include "Couleur.h"
#include "ModuleFont.h"

#define UNREFERENCED_PARAMETER(P) (P)

PotentiometreCirculaire::PotentiometreCirculaire(void):Controle() {
  coulTexte = new Couleur();
  coulBouton = new Couleur();
  coulTexteBouton = new Couleur();
  rayon = 0;
  min = 0.0F;
  max = 0.0F;
  v = NULL;
  strcpy(titre,"");
}

PotentiometreCirculaire::PotentiometreCirculaire(int px,int py,int rayon,Couleur *coul,float min,float max,Couleur *coulB,const char *titre,Couleur *coulT,Couleur *coulTB,float *v):Controle(px,py,coul) {
  this->coulTexte = new Couleur(coulT);
  this->coulBouton = new Couleur(coulB);
  this->coulTexteBouton = new Couleur(coulTB);
  this->rayon = rayon;
  this->min = min;
  this->max = max;
  this->v = v;
  strcpy(this->titre,titre);
}

PotentiometreCirculaire::~PotentiometreCirculaire(void) {
  delete(this->coulTexte);
  delete(this->coulBouton);
  delete(this->coulTexteBouton);
}

void PotentiometreCirculaire::dessiner(void) {
  if ( !enabled )
    actif = 0;
  glEnable(GL_LIGHTING);
  glPushMatrix();
  glTranslatef(px,py,0.0F);
  setGLMaterial(GL_FRONT,GL_DIFFUSE,(float *) coul);
  { glPushMatrix();
    glutSolidSphere(rayon,36,36);
    glTranslatef(0.0F,0.0F,rayon*0.65F);
    glutSolidSphere(rayon/2,36,36);
    glPopMatrix(); }
  { glPushMatrix();
    glRotatef(-70.0F,0.0F,0.0F,1.0F);
    glTranslatef(rayon,0.0F,rayon);
    glScalef(1.7F,1.0F,1.0F);
    glutSolidSphere(6.0F,18,18);
    glPopMatrix(); }
  { glPushMatrix();
    glRotatef(250.0F,0.0F,0.0F,1.0F);
    glTranslatef(rayon,0.0F,rayon);
    glScalef(1.7F,1.0F,1.0F);
    glutSolidSphere(6.0F,18,18);
    glPopMatrix(); }
  float pos;
  { setGLMaterial(GL_FRONT,GL_DIFFUSE,(float *) coulBouton);
    glPushMatrix();
    pos = -70.0F+320*(*v-min)/(max-min);
    glRotatef(pos,0.0F,0.0F,1.0F);
    glTranslatef(rayon,0.0F,rayon+10.0F);
    glScalef(1.7F,1.0F,1.0F);
    glutSolidSphere(6.0F,18,18);
    glPopMatrix(); }
  glDisable(GL_LIGHTING);
  setGLColor((float *) coulTexte);
  { setFont(GLUT_BITMAP_8_BY_13,CENTER);
    bitmapStringOutput(0.0,-30-rayon,0.0F,titre); }
  { setFont(GLUT_BITMAP_8_BY_13,RIGHT);
    glPushMatrix();
    glRotatef(250.0F,0.0F,0.0F,1.0F);
    glTranslatef(rayon+17.0F,0.0F,0.0F);
    bitmapStringOutput(0.0F,0.0F,0.0F,"%5.2f",max);
    glPopMatrix(); }
  { setFont(GLUT_BITMAP_8_BY_13,LEFT);
    glPushMatrix();
    glRotatef(-70.0F,0.0F,0.0F,1.0F);
    glTranslatef(rayon+17.0F,0.0F,0.0F);
    bitmapStringOutput(0.0F,0.0F,0.0F,"%5.2f",min);
    glPopMatrix(); }
  setGLColor((float *) coulTexteBouton);
  { setFont(GLUT_BITMAP_8_BY_13,( pos < 90.0F ) ? LEFT : RIGHT);
    glPushMatrix();
    glRotatef(pos,0.0F,0.0F,1.0F);
    glTranslatef(rayon+17.0F,0.0F,0.0F);
    bitmapStringOutput(0.0F,0.0F,0.0F,"%5.2f",*v);
    glPopMatrix(); }
  glPopMatrix();
}

void PotentiometreCirculaire::sauve(void) {
  if ( v )
    save = *v;
}

void PotentiometreCirculaire::annule(void) {
  if ( v )
    *v = save;
}

static float carreDistance(float x,float y,float xx,float yy) {
  float dx = (x - xx);
  float dy = (y - yy);
  return(dx*dx+dy*dy);
}

int PotentiometreCirculaire::selection(int x,int y) {
  if ( enabled ) {
    float pos = 70.0F-320*(*v-min)/(max-min);
    float tx = px+rayon*(float) cos(pos/57.295779513);
    float ty = py-rayon*(float) sin(pos/57.295779513);
    if ( carreDistance(x,y,tx,ty) < 150 )
      return(1); }
  return(-1);
}

int PotentiometreCirculaire::gereModification(int x,int y,int dx,int dy) {
  UNREFERENCED_PARAMETER(dx);
  UNREFERENCED_PARAMETER(dy);
  float ang =(float) -atan2((double) py-y,x-px)*57.295779513F;
  if ( ang < -90.0F )
    ang = ang + 360.0F;
  *v =(ang + 70.0F)/320.0F*(max-min)+min;
  if ( *v < min )
    *v = min;
  if ( *v > max )
    *v = max;
  return(1);
}

void PotentiometreCirculaire::sauve(FILE *f) {
  if ( strcmp(name,"") ) {
    if ( v )
      fprintf(f,"  <%s valeur=\"%f\"></%s>\n",name,*v,name); }
}
