text: ﻿#include "mixer.lib.h"
  #include "mixer.lib.cursor.h"
  #include "mixer.lib.window.h"
  
  #ifdef MI_PLATFORM_WINDOWS
  #include <Windows.h>
  #include "mixer.lib.cursor.win32.h"
  #endif
  
  #include "miLib.h"
  
  extern miLib* g_lib;
  
  extern "C"
  {
  	MI_API miCursor* MI_C_DECL miCreateCursor(miCursorType ct, const wchar_t* fn)
  	{
  		miCursor* newCursor = 0;
  #ifdef MI_PLATFORM_WINDOWS
  		newCursor = miCreate<miCursorWin32>();
  		miCursorWin32* ptr = (miCursorWin32*)newCursor;
  		ptr->m_handle = (HCURSOR)::LoadImageW(GetModuleHandle(0), fn, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
  #else
  #error For Windows
  #endif
  		newCursor->OnCreate(ct);
  		return newCursor;
  	}
  
  	MI_API miCursor* MI_C_DECL miGetCursor(miCursorType ct)
  	{
  		return g_lib->m_cursors[(u32)ct];
  	}
  
  	MI_API void MI_C_DECL miResetCursor(miCursorType ct)
  	{
  		g_lib->m_cursors[(u32)ct] = g_lib->m_cursorsDefault[(u32)ct];
  	}
  
  	MI_API void MI_C_DECL miShowCursor(bool v)
  	{
  		if (v)
  		{
  			miGetCursor(miCursorType::Arrow)->Activate();
  		}
  		else
  		{
  #ifdef MI_PLATFORM_WINDOWS
  			::SetCursor(0); // hide
  #else
  #error Need to implement
  #endif
  		}
  	}
  
  	MI_API void MI_C_DECL miSetCursorClip(v4f* New, v4f* Old, miWindow* w)
  	{
  		v4f o = g_lib->m_cursorClip;
  		if (Old)
  		{
  			Old->x = g_lib->m_cursorClip.x;
  			Old->y = g_lib->m_cursorClip.y;
  			Old->z = g_lib->m_cursorClip.z;
  			Old->w = g_lib->m_cursorClip.w;
  		}
  
  		if (New)
  		{
  			assert(w);
  			g_lib->m_cursorClip.x = New->x;
  			g_lib->m_cursorClip.y = New->y;
  			g_lib->m_cursorClip.z = New->z;
  			g_lib->m_cursorClip.w = New->w;
  #ifdef MI_PLATFORM_WINDOWS
  			POINT p1;
  			p1.x = (LONG)g_lib->m_cursorClip.x;
  			p1.y = (LONG)g_lib->m_cursorClip.y;
  
  			POINT p2;
  			p2.x = (LONG)g_lib->m_cursorClip.z;
  			p2.y = (LONG)g_lib->m_cursorClip.w;
  
  			ClientToScreen((HWND)w->GetHandle(), &p1);
  			ClientToScreen((HWND)w->GetHandle(), &p2);
  
  			g_lib->m_cursorClip.x = (f32)p1.x;
  			g_lib->m_cursorClip.y = (f32)p1.y;
  			g_lib->m_cursorClip.z = (f32)p2.x;
  			g_lib->m_cursorClip.w = (f32)p2.y;
  #else
  #error Need to implement
  #endif
  		}
  
  #ifdef MI_PLATFORM_WINDOWS
  		if (!New)
  		{
  			RECT cclip;
  			//	GetWindowRect(GetDesktopWindow(), &cclip);
  			cclip.left = 0;
  			cclip.top = 0;
  			cclip.right = GetSystemMetrics(SM_CXMAXTRACK);
  			cclip.bottom = GetSystemMetrics(SM_CYMAXTRACK);
  
  			g_lib->m_cursorClip.x = (f32)cclip.left;
  			g_lib->m_cursorClip.y = (f32)cclip.top;
  			g_lib->m_cursorClip.z = (f32)cclip.right;
  			g_lib->m_cursorClip.w = (f32)cclip.bottom;
  		}
  
  		RECT cclip;
  		cclip.left = (LONG)g_lib->m_cursorClip.x;
  		cclip.top = (LONG)g_lib->m_cursorClip.y;
  		cclip.right = (LONG)g_lib->m_cursorClip.z;
  		cclip.bottom = (LONG)g_lib->m_cursorClip.w;
  		ClipCursor(&cclip);
  #else
  #error Need to implement
  #endif
  	}
  
  	MI_API void MI_C_DECL miSetCursor(miCursorType ct, miCursor* c)
  	{
  		g_lib->m_cursors[(u32)ct] = c;
  		c->Activate();
  	}
  
  	MI_API void MI_C_DECL miSetCursorDisableAutoChange(bool v)
  	{
  		g_lib->m_cursorDisableAutoChange = v;
  	}
  
  	MI_API bool MI_C_DECL miGetCursorDisableAutoChange()
  	{
  		return g_lib->m_cursorDisableAutoChange;
  	}
  }