summaryrefslogtreecommitdiff
path: root/Runtime/IMGUI/GUIManager.h
blob: eb35c45d454d6520b561bdcc916d105a60be2615 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef GUIMANAGER_H
#define GUIMANAGER_H

#include "Configuration/UnityConfigure.h"
#include "Runtime/Mono/MonoBehaviour.h"
#include "Runtime/Misc/InputEvent.h"
#include "Runtime/Utilities/MemoryPool.h"
#include "Runtime/IMGUI/GUIState.h"
#include "Runtime/Misc/DeveloperConsole.h"

#include <deque>

struct InputEvent;
class GUIManager {
  public:
	GUIManager ();

	// Issue a repaint event - used by players... 
	// Mouse position & modifiers are read from the input manager.
	// TODO: implement Modifier keys
	void Repaint ();

	// Add/remove  - called from MonoBehaviour
	void AddGUIScript (MonoBehaviourListNode& beh);
	
	// Send an event to all in-game GUI scripts
	// e -				the event to send.
	// mask -			GOLayer mask to match.
	// frontToBack		Send event to the frontmost GUI script first (true for normal event processing, false for repaint)
	void DoGUIEvent (InputEvent &e, bool frontToBack);
	
	// Send an input event (keydown, mousemove, mouseup, etc) to the GUI scripts.
	void QueueEvent (InputEvent &ie);
	void QueueEventImmediate (InputEvent &ie);
	
	void SendQueuedEvents ();

	InputEvent GetLastInputEvent() { return m_LastEvent;}
	
	static bool AnyMouseButtonsDown();	

	// Did GUI code inside BeginWindows make it irrelevant for OnGUI code to be called at all? (e.g. mouseClick inside a GUI.window)
	// We used to do this by calling event.Use (), but that causes repaints, so instead this function is called that sets a var to track it
	static void SetDidGUIWindowsEatLastEvent (bool value);
	static bool GetDidGUIWindowsEatLastEvent ();
	
	#if UNITY_EDITOR
	// Clear all setting for entering / exiting playmode
	void Reset ();
	#endif
		
	static void ResetCursorFlash ();
	static float GetCursorFlashTime ();	

	#if UNITY_EDITOR
	void SetEditorGUIInfo (Vector2f guiPixelOffset);
	Vector2f GetGUIPixelOffset () { return m_GUIPixelOffset; }
	Vector2f GetEditorGUIInfo() const;
	#endif

	#if SUPPORT_REPRODUCE_LOG
	void WriteLog (std::ofstream& out);
	void ReadLog (std::ifstream& in);
	#endif
	
	struct GUIObjectWrapper
	{
	public:
		explicit GUIObjectWrapper(MonoBehaviour* beh);
	#if UNITY_HAS_DEVELOPER_CONSOLE
	
		explicit GUIObjectWrapper(DeveloperConsole* dev);

	#endif // UNITY_HAS_DEVELOPER_CONSOLE

		// Wrapped functions for GUI objects
		bool DoGUI(MonoBehaviour::GUILayoutType layoutType, int skin) const  {
			return do_gui_func(wrapped_ptr, layoutType, skin);
		}

		ObjectGUIState& GetObjectGUIState() const {
			return get_gui_state_func(wrapped_ptr);
		}

		// This avoids a plethora of pitfalls in situations,
		// where implicit conversions may happen
		typedef void * GUIObjectWrapper::*unspecified_bool_type;
		operator unspecified_bool_type() const  { // never throws
			return wrapped_ptr == 0? 0: &GUIObjectWrapper::wrapped_ptr;
		}

	private:
		typedef bool (*dogui_function_type)(void*, MonoBehaviour::GUILayoutType, int);
		typedef ObjectGUIState& (*get_gui_state_function_type)(void*);

		void* wrapped_ptr;

		dogui_function_type do_gui_func;
		get_gui_state_function_type get_gui_state_func;
	};

	struct SortedScript 
	{
		int depth;
		GUIObjectWrapper beh;
		SortedScript (int dep, GUIObjectWrapper b) : depth(dep), beh(b) {}
	};

	inline std::deque<InputEvent>& GetQueuedEvents() {return m_Events;}
	bool GetMouseUsed () const { return m_MouseUsed; }

	static GUIKeyboardState &GetMasterGUIState ();

private:
	typedef List<MonoBehaviourListNode> MonoBehaviourList;
	MonoBehaviourList m_GUIScripts;
	std::deque<InputEvent> m_Events;

	bool m_MouseUsed, m_DidGUIWindowsEatLastEvent;
	int m_mouseButtonsDown;
	Vector2f m_GUIPixelOffset;
	typedef std::list<SortedScript, memory_pool<SortedScript> > SortedScripts;
	SortedScripts m_SortedScripts;

	float m_LastInputEventTime;
	InputEvent m_LastEvent;
	GUIKeyboardState m_MasterState;
};

GUIManager &GetGUIManager ();

void InitGUIManager ();
void CleanupGUIManager ();

#endif