summaryrefslogtreecommitdiff
path: root/Runtime/BaseClasses/Cursor.h
blob: b0072fb1ce420502905d39bee739cae0594078a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once

#include "UnityPrefix.h"
#include "Runtime/Graphics/Texture2D.h"
#include "Runtime/Math/Vector2.h"

#include <string>

#define PLATFORM_SUPPORTS_HARDWARE_CURSORS (!UNITY_PEPPER && ((UNITY_WIN && !UNITY_WINRT) || UNITY_OSX || UNITY_LINUX || UNITY_FLASH))

enum CursorMode
{
	kAutoHardwareCursor = 0,
	kHardwareCursorOff = 1
};

#if UNITY_OSX
	#ifdef __OBJC__
		@class NSCursor;
	#else
		typedef struct objc_object NSCursor;
	#endif
#endif

namespace Cursors
{

template <typename T>
struct UnityCursor
{
	UnityCursor ()
	{
		hCursor = NULL;
		sCursor = NULL;
	}
	T hCursor;
	PPtr<Texture2D> sCursor;
	Vector2f hotspot;

	typedef T HCursorType;
};

template <typename T>
struct CursorManager
{
	T m_DefaultCursor;
	T m_CurrentCursor;

	bool m_UsingBuiltinDefaultCursor;

	typedef std::map<TextureID, T > CursorCache;
	CursorCache m_CursorCache;

	Texture2D* GetSoftwareCursor ()
	{
		return m_CurrentCursor.sCursor;
	}

	Vector2f GetCursorHotspot ()
	{
		return m_CurrentCursor.hotspot;
	}

	typename T::HCursorType GetHardwareCursor ()
	{
		return m_CurrentCursor.hCursor;
	}

	static CursorManager<T>* s_CursorManager;
	static CursorManager<T>& Instance ()
	{
		if (s_CursorManager == NULL)
		{
			s_CursorManager = new CursorManager<T>();
		}

		return *s_CursorManager;
	}

	static void Cleanup ()
	{
		delete s_CursorManager;
		s_CursorManager = NULL;
	}
};

void SetCursor (Texture2D* texture, Vector2f hotSpot, CursorMode forceHardware);
void RenderSoftwareCursor ();
Texture2D* GetSoftwareCursor ();
Vector2f GetCursorHotspot ();
void InitializeCursors (Texture2D* defaultCursorTexture, Vector2f defaultCursorHotSpot);
void CleanupCursors ();

#if UNITY_WIN
void ResetCursor ();
// returns true if the event is 'handled' WM_SETCURSOR in this case
bool HandleMouseCursor (UINT message, LPARAM lParam);
HCURSOR GetHardwareCursor ();
#endif

#if UNITY_OSX
void ResetCursor ();
NSCursor* GetHardwareCursor ();
#endif
};