summaryrefslogtreecommitdiff
path: root/Runtime/Misc/InputEvent.cpp
blob: b27eb174ff2d25cf94c89cc17f4104e5e1a1d9b5 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "UnityPrefix.h"
#include "InputEvent.h"

InputInterface::~InputInterface () {
}
bool InputInterface::PerformDrag (InputEvent &event) {
	return false;
}
int InputInterface::UpdateDrag (InputEvent &event) {
	return 0;
}

InputEvent::~InputEvent(  )
{
	delete []commandString;
}

void InputEvent::Debug () const
{
#if ENABLE_NEW_EVENT_SYSTEM
	Vector2f mousePosition = touch.pos;
	Vector2f delta = touch.deltaPos;
#endif
	std::string buffer;
	const char *EventNames[] = {
		"kMouseDown", "kMouseUp", "kMouseMove", "kMouseDrag", "kKeyDown", "kKeyUp",
		"kScrollWheel", "kRepaint", "kLayout", "kDragUpdated", "kDragPerform", "kIgnore", "kUsed"
	};
	bool isMouse = false;
	bool isKeyb = false;
	switch (type) {
	case kKeyDown:
	case kKeyUp:
	case kScrollWheel:
		isKeyb = true;
		break;
	case kMouseDown:
	case kMouseUp:
	case kMouseMove:
	case kMouseDrag:
		isMouse = true;
	}
	buffer += Format("Event: %s\n", EventNames[type]);	
	if (isMouse) {
		buffer += Format("  Pos (%f, %f) \t   Delta: (%f, %f)\n", mousePosition.x, mousePosition.y, delta.x, delta.y);
		buffer += Format("  Button: %d", button);
		buffer += Format("  Click count: %d", clickCount);
	} else if (isKeyb) {
		AssertIf(clickCount != 0);
		if( character >= 32 && character <= 127 )
			buffer += Format("  character: '%c'\n", character);
		else
			buffer += Format("  character: code %d\n", character);
		buffer += Format("  keycode: '%d'\n", keycode);
	}
	buffer += "  Mod: ";
	if (modifiers & kShift) buffer += "shift ";
	if (modifiers & kControl) buffer += "ctrl ";
	if (modifiers & kAlt) buffer += "alt ";
	if (modifiers & kCommand) buffer += "cmd ";
	if (modifiers & kNumeric) buffer += "num ";
	if (modifiers & kCapsLock) buffer += "capslock ";
	if (modifiers & kFunctionKey) buffer += "fkey ";
	if (modifiers == 0) buffer += "none ";
	buffer += "\n";
	printf_console( "%s", buffer.c_str() );
}

void InputEvent::Init( )
{
#if !ENABLE_NEW_EVENT_SYSTEM
	mousePosition = Vector2f(0.0F, 0.0F);
	delta =  Vector2f(0.0F, 0.0F);
	#if UNITY_METRO
	touchType = kMouseTouch;
	#endif
#endif
	type = InputEvent::kIgnore;
	keycode = 0;
	character = 0;
	button = 0;
	clickCount = 0;
	pressure = 0;
	modifiers =  0;
	commandString = NULL;
}

InputEvent::InputEvent( const InputEvent& evt )
{
#if ENABLE_NEW_EVENT_SYSTEM
	touch = evt.touch;
#else
	mousePosition = evt.mousePosition;
	delta = evt.delta;
	#if UNITY_METRO
	touchType = evt.touchType;
	#endif
#endif
	type = evt.type;
	button = evt.button;
	modifiers = evt.modifiers;
	pressure = evt.pressure;
	clickCount = evt.clickCount;
	character = evt.character;
	keycode = evt.keycode;

	if (evt.commandString)
	{
		commandString = new char [strlen(evt.commandString) + 1];
		memcpy(commandString, evt.commandString, strlen(evt.commandString) + 1);
	}
	else
	{
		commandString = NULL;
	}
}


void InputEvent::operator = (const InputEvent& evt)
{
#if ENABLE_NEW_EVENT_SYSTEM
	touch = evt.touch;
#else
	mousePosition = evt.mousePosition;
	delta = evt.delta;
	#if UNITY_METRO
	touchType = evt.touchType;
	#endif
#endif
	type = evt.type;
	button = evt.button;
	modifiers = evt.modifiers;
	pressure = evt.pressure;
	clickCount = evt.clickCount;
	character = evt.character;
	keycode = evt.keycode;
	
	if (commandString)
	{
		delete[] commandString;
		commandString = NULL;
	}
	
	if (evt.commandString)
	{
		commandString = new char [strlen(evt.commandString) + 1];
		memcpy(commandString, evt.commandString, strlen(evt.commandString) + 1);
	}
}

InputEvent InputEvent::CommandStringEvent (const std::string& editorCommand, bool execute)
{
	InputEvent event;
	event.Init();
	if (execute)
		event.type = InputEvent::kExecuteCommand;
	else
		event.type = InputEvent::kValidateCommand;
	event.commandString = new char[editorCommand.size() + 1];
	memcpy(event.commandString, editorCommand.c_str(), editorCommand.size() + 1);
	return event;
}