#ifdef _WINDOWS
#include <windows.h>
#endif
#include <GL/gl.h>

#include "jlmouse.h"


JLMouse::JLMouse() {

	_x = _y = _last_x = _last_y = 0;
        for (int i = 0; i < NUMBUTTONS; i++)
          _mousedown[i] = 0;
	_ortho_x1 = _ortho_x2 = _ortho_y1 = _ortho_y2 = 0;
}

JLMouse::~JLMouse() {
}

JLMouse& JLMouse::CopyFrom(const JLMouse&) {

	cerr << "Don't copy mice..." << endl;
	exit(-1);

	return *this;
}

void JLMouse::Update(int x, int y) {

	_last_x = _x;
	_last_y = _y;

	_x = x;
	_y = y;
}

void JLMouse::GetFloatXYGivenViewport(float &x, float &y, int xsize, int ysize) const {
  x = _x;
  y = _y;
  
  float ox1, ox2, oy1, oy2;
  
  if ((_ortho_x2 - _ortho_x1) && (_ortho_y2 - _ortho_y1)) {
    ox1 = _ortho_x1;
    ox2 = _ortho_x2;
    oy1 = _ortho_y1;
    oy2 = _ortho_y2;
  }
  else {
    ox1 = 0;
    ox2 = xsize-1;
    oy1 = 0;
    oy2 = ysize-1;
  }
  
  x /= (xsize - 1);
  y /= (ysize - 1);
  
  x *= (ox2 - ox1);
  y *= (oy2 - oy1);
  
  x += ox1;
  y += oy1;
  
};

void JLMouse::GetFloatXY(float &x, float &y) const {

	GLint vp[4];

	glGetIntegerv(GL_VIEWPORT, vp);

	GetFloatXYGivenViewport (x, y, vp[2], vp[3]);

}
