text: #include "Defs.h"
  #include "p2Log.h"
  
  #include "j1App.h"
  
  #include "j1Input.h"
  #include "j1Window.h"
  
  #include "SDL/include/SDL.h"
  
  #define MAX_KEYS 300
  
  j1Input::j1Input() : j1Module()
  {
  	name.assign("input");
  
  	keyboard = new KEY_STATE[MAX_KEYS];
  	memset(keyboard, KEY_IDLE, sizeof(KEY_STATE) * MAX_KEYS);
  	memset(mouseButtons, KEY_IDLE, sizeof(KEY_STATE) * NUM_MOUSE_BUTTONS);
  }
  
  // Destructor
  j1Input::~j1Input()
  {
  	delete[] keyboard;
  }
  
  // Called before render is available
  bool j1Input::Awake(pugi::xml_node& config)
  {
  	bool ret = true;
  
  	LOG("Init SDL input event system");
  
  	SDL_Init(0);
  
  	if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
  	{
  		LOG("SDL_EVENTS could not initialize! SDL_Error: %s\n", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }
  
  // Called before the first frame
  bool j1Input::Start()
  {
  	SDL_StopTextInput();
  
  	return true;
  }
  
  // Called each loop iteration
  bool j1Input::PreUpdate()
  {
  	bool ret = true;
  
  	isPressed = false;
  
  	static SDL_Event event;
  
  	const Uint8* keys = SDL_GetKeyboardState(NULL);
  
  	for (int i = 0; i < MAX_KEYS; ++i)
  	{
  		if (keys[i] == 1)
  		{
  			isPressed = true;
  
  			if (keyboard[i] == KEY_IDLE)
  				keyboard[i] = KEY_DOWN;
  			else
  				keyboard[i] = KEY_REPEAT;
  		}
  		else
  		{
  			if (keyboard[i] == KEY_REPEAT || keyboard[i] == KEY_DOWN)
  				keyboard[i] = KEY_UP;
  			else
  				keyboard[i] = KEY_IDLE;
  		}
  	}
  
  	for (int i = 0; i < NUM_MOUSE_BUTTONS; ++i)
  	{
  		if (mouseButtons[i] == KEY_DOWN)
  			mouseButtons[i] = KEY_REPEAT;
  
  		if (mouseButtons[i] == KEY_UP)
  			mouseButtons[i] = KEY_IDLE;
  	}
  
  	while (SDL_PollEvent(&event) != 0)
  	{
  		switch (event.type)
  		{
  		case SDL_QUIT:
  			windowEvents[WE_QUIT] = true;
  			break;
  
  		case SDL_WINDOWEVENT:
  			switch (event.window.event)
  			{
  				//case SDL_WINDOWEVENT_LEAVE:
  			case SDL_WINDOWEVENT_HIDDEN:
  			case SDL_WINDOWEVENT_MINIMIZED:
  			case SDL_WINDOWEVENT_FOCUS_LOST:
  				windowEvents[WE_HIDE] = true;
  				break;
  
  				//case SDL_WINDOWEVENT_ENTER:
  			case SDL_WINDOWEVENT_SHOWN:
  			case SDL_WINDOWEVENT_FOCUS_GAINED:
  			case SDL_WINDOWEVENT_MAXIMIZED:
  			case SDL_WINDOWEVENT_RESTORED:
  				windowEvents[WE_SHOW] = true;
  				break;
  			}
  			break;
  
  		case SDL_MOUSEBUTTONDOWN:
  			mouseButtons[event.button.button - 1] = KEY_DOWN;
  			//LOG("Mouse button %d down", event.button.button-1);
  			break;
  
  		case SDL_MOUSEBUTTONUP:
  			mouseButtons[event.button.button - 1] = KEY_UP;
  			//LOG("Mouse button %d up", event.button.button-1);
  			break;
  
  		case SDL_MOUSEMOTION:
  			int scale = App->win->GetScale();
  			mouseMotionX = event.motion.xrel / scale;
  			mouseMotionY = event.motion.yrel / scale;
  			mouseX = event.motion.x / scale;
  			mouseY = event.motion.y / scale;
  			//LOG("Mouse motion x %d y %d", mouseMotionX, mouseMotionY);
  			break;
  		}
  	}
  
  	return ret;
  }
  
  // Called before quitting
  bool j1Input::CleanUp()
  {
  	bool ret = true;
  
  	LOG("Quitting SDL event subsystem");
  
  	SDL_QuitSubSystem(SDL_INIT_EVENTS);
  
  	return ret;
  }
  
  // ---------
  bool j1Input::GetWindowEvent(j1EventWindow ev)
  {
  	return windowEvents[ev];
  }
  
  void j1Input::GetMousePosition(int& x, int& y)
  {
  	x = mouseX;
  	y = mouseY;
  }
  
  void j1Input::GetMouseMotion(int& x, int& y)
  {
  	x = mouseMotionX;
  	y = mouseMotionY;
  }
  
  bool j1Input::IsAnyKeyPressed()
  {
  	bool isAnyKeyPressed = isPressed;
  	isPressed = false;
  	return isAnyKeyPressed;
  }