summaryrefslogtreecommitdiff
path: root/Runtime/IMGUI/IMGUIUtils.cpp
blob: 2ce0596d16d01e4a6110a0053f273d21cbcdd8c6 (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
#include "UnityPrefix.h"
#include "Runtime/IMGUI/IMGUIUtils.h"
#include "Runtime/IMGUI/TextMeshGenerator2.h"

const float s_TabWidth = 16;
extern float s_GUIStyleIconSizeX;

namespace IMGUI
{
	InputEvent::Type GetEventType (const GUIState &state, const InputEvent &event)
	{
		InputEvent::Type type = event.type;
		if (!state.m_OnGUIState.m_Enabled)
		{
			if (type == InputEvent::kRepaint || type == InputEvent::kLayout || type == InputEvent::kUsed)
				return type;
			return InputEvent::kIgnore;
		}
		
		if (state.m_CanvasGUIState.m_GUIClipState.GetEnabled())
			return type;
		
		if (type == InputEvent::kMouseDown || type == InputEvent::kMouseUp || type == InputEvent::kDragPerform || type == InputEvent::kDragUpdated)
			return InputEvent::kIgnore;
		
		return type;
	}
	
	
	InputEvent::Type GetEventTypeForControl (const GUIState &state, const InputEvent &event, int controlID)
	{
		InputEvent::Type m_Type = event.type;
	
		// if we have no hot control, just return the usual
		if (GetHotControl(state) == 0)
			return GetEventType (state, event);
		switch (m_Type)
		{
			// Mouse events follow GUIUtility.hotControl
			case InputEvent::kMouseDown:
			case InputEvent::kMouseUp:
			case InputEvent::kMouseMove:
			case InputEvent::kMouseDrag:
				if (!GetEnabled(state))
					return InputEvent::kIgnore;
				if (GetGUIClipEnabled(state) || HasMouseControl (state, controlID))
					return m_Type;
				return InputEvent::kIgnore;
			
			// Key events follow keyboard control
			case InputEvent::kKeyDown:
			case InputEvent::kKeyUp:
			case InputEvent::kScrollWheel: // Not sure about scrollwheel. For now we map it to behave like keyboard events.

				if (!GetEnabled(state))
					return InputEvent::kIgnore;
				if (GetGUIClipEnabled(state) || HasMouseControl (state, controlID) || GetKeyboardControl(state) == controlID)
					return m_Type;
				return InputEvent::kIgnore;
				
			// Repaint, Layout, Used, DragUpdated, DragPerform, Ignore
			default:
				return m_Type;
		}
	}

	
	TextMeshGenerator2* GetGenerator (const Rectf &contentRect, const GUIContent &content, Font& font, TextAnchor alignment, bool wordWrap, bool richText, ColorRGBA32 color, int fontSize, int fontStyle, ImagePosition imagePosition)
	{	
		if (!wordWrap)
			return &TextMeshGenerator2::Get (content.m_Text, &font, alignment, kAuto, 0.0f, s_TabWidth, 1.0f, richText, true, color, fontSize, fontStyle);
		
		Texture *image = content.m_Image;	
		float textWidth = contentRect.Width();
		switch (imagePosition) {
			case kImageLeft:
				// Todo: Subtract width of the icon
				if (image != NULL)
				{
					Vector2f imageSize = Vector2f (image->GetDataWidth(), image->GetDataHeight());
					
					//float imageScale = clamp (min (contentRect.Width() / imageSize.x, contentRect.Height() / imageSize.y), 0.0f, 1.0f);
					//contentRect.width -=  Roundf (imageSize.x * imageScale);
					
					if (s_GUIStyleIconSizeX == 0)
						textWidth -= Roundf (imageSize.x * clamp (std::min (contentRect.Width() / imageSize.x, contentRect.Height() / imageSize.y), 0.0f, 1.0f));
					else
						textWidth -= s_GUIStyleIconSizeX;
				}
				break;
			case kImageOnly:
				return NULL;
			case kImageAbove:
			case kTextOnly:
				// These two require no special handling
				break;
		}
		
		return &TextMeshGenerator2::Get (content.m_Text, &font, alignment, kAuto, textWidth, s_TabWidth, 1.0f, richText, true, color, fontSize, fontStyle);
	}
}	// namespace IMGUI