//-----------------------------------------------------------------------------
// Project            	RenderKit
// Class		G3DSDLEventHandler
// Creator            	Philippe C.D. Robert
// Maintainer         	Philippe C.D. Robert
// Creation Date      	Sun Jul 16 23:23:46 CEST 2000
//
// Copyright (c) Philippe C.D. Robert
//
// The RenderKit is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the Free 
// Software Foundation
//
// $Id: G3DSDLEventHandler.m,v 1.12 2001/07/15 13:56:15 probert Exp $
//-----------------------------------------------------------------------------

#import "G3DSDLEventHandler.h"

NSString *G3DSDLEventHandlerException = @"G3DSDLEventHandlerException";

@implementation G3DSDLEventHandler

static G3DSDLEventHandler *_dfHandler = nil;
static BOOL isMouseActive = YES;
static unsigned buttonMask = 0;

static int screenCenterX;
static int screenCenterY;

//-----------------------------------------------------------------------------
// Class methods
//-----------------------------------------------------------------------------

+ (G3DSDLEventHandler *)defaultHandler
{
  if (!_dfHandler) {
    _dfHandler = [[G3DSDLEventHandler alloc] init];
  }
  return _dfHandler;
}

//-----------------------------------------------------------------------------
// Event Handling
//-----------------------------------------------------------------------------

- (BOOL)getEvent:(SDL_Event *)ev
{
  //~ Is this needed?
  // SDL_PumpEvents();

  return SDL_PollEvent(ev);
}

- (NSString *)nameWithKeyEvent:(SDLKey)ev
{
  return [NSString stringWithCString:SDL_GetKeyName(ev)];
}

- (void)getKeyState:(Uint8 *)kstate
{
  SDL_PumpEvents();

  kstate = SDL_GetKeyState(NULL);
}

- (SDLMod)modifierKeyState
{
  return SDL_GetModState();
}

- (BOOL)isKeyPressed:(SDLKey)ev
{
  Uint8 *keystate;
  
  SDL_PumpEvents();
  keystate = SDL_GetKeyState(NULL);

  return (keystate[ev]) ? YES : NO;
}

- (void)setKeyRepeatWithDelay:(int)del interval:(int)iv
{
  SDL_EnableKeyRepeat(del, iv);
}

//-----------------------------------------------------------------------------
// Mouse Event Handling
//-----------------------------------------------------------------------------

- (int)getStateAtX:(int *)x y:(int *)y
{
  Uint8 _m_state;

  if (isMouseActive == NO) {
    return -1;
  }

  SDL_PumpEvents();
  _m_state=SDL_GetMouseState(x,y);

  if (_m_state==SDL_BUTTON(1)) {
    return 1;
  }
  else if (_m_state==SDL_BUTTON(2)) {
    return 3;
  }
  else if (_m_state==SDL_BUTTON(3)) {
    return 2;
  }

  return 0;
}

- (int)getRelativeStateAtX:(int *)x y:(int *)y
{
  Uint8 _m_state;

  if (isMouseActive == NO) {
    return -1;
  }

  SDL_PumpEvents();
  _m_state=SDL_GetRelativeMouseState(x,y);

  if (_m_state==SDL_BUTTON(1)) {
    return 1;
  }
  else if (_m_state==SDL_BUTTON(2)) {
    return 3;
  }
  else if (_m_state==SDL_BUTTON(3)) {
    return 2;
  }

  return 0;
}

- (void)setMouseAtX:(int)x y:(int)y
{
  SDL_WarpMouse(x,y);
}

- (void)centerMouse
{
  if (isMouseActive) {
    SDL_Surface *s = SDL_GetVideoSurface();

    screenCenterX = (s->w / 2);
    screenCenterY = (s->h / 2);

    SDL_WarpMouse(screenCenterX,screenCenterY);
  }
}

- (BOOL)isMouseActive
{
  return isMouseActive;
}

- (void)activeMouse:(BOOL)yn
{
  if (yn && !isMouseActive) {
    SDL_WM_GrabInput(SDL_GRAB_ON);
    SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);

    buttonMask = 0; 
    isMouseActive = YES;

    [self centerMouse];       
  }
  else {
    SDL_WM_GrabInput(SDL_GRAB_OFF);
    SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
    isMouseActive = NO;
  }
}

- (void)showCursor
{
  SDL_ShowCursor(1);
  isMouseActive = YES;
}

- (void)hideCursor
{
  SDL_ShowCursor(0);
  isMouseActive = NO;
}

@end



