summaryrefslogtreecommitdiff
path: root/common/keyboard.cpp
blob: da2cf88e94c79bbefe7a6e89abacbd71ddd4a1da (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// Code to handle user-defined keyboard shortcuts.
//

#include <stdio.h>
#include "system.h"
#include "keyboard.h"
#include "file.h"
#include "str.h"

// ============================================================================
// Globals.

LC_KEYBOARD_COMMAND DefaultKeyboardShortcuts[] = 
{
	{ LC_FILE_NEW, "New Project", LC_KEYMOD1_CONTROL, LC_KEY_N, 0 },
	{ LC_FILE_OPEN, "Open Project", LC_KEYMOD1_CONTROL, LC_KEY_O, 0 },
	{ LC_FILE_MERGE, "Merge Project", 0, 0, 0 },
	{ LC_FILE_SAVE, "Save Project", LC_KEYMOD1_CONTROL, LC_KEY_S, 0 },
	{ LC_FILE_SAVEAS, "Save Project As", 0, 0, 0 },
	{ LC_FILE_PICTURE, "Save Picture", 0, 0, 0 },
	{ LC_FILE_3DS, "Export 3D Studio", 0, 0, 0 },
	{ LC_FILE_HTML, "Export HTML", 0, 0, 0 },
	{ LC_FILE_POVRAY, "Export POV-Ray", 0, 0, 0 },
	{ LC_FILE_WAVEFRONT, "Export Wavefront", 0, 0, 0 },
	{ LC_FILE_PROPERTIES, "Project Properties", 0, 0, 0 },
//	{ LC_FILE_TERRAIN, "Terrain Editor", 0, 0, 0 },
	{ LC_FILE_LIBRARY, "Piece Library Manager", 0, 0, 0 },
//	{ LC_FILE_RECENT, "Open Recent File", 0, 0, 0 },
	{ LC_EDIT_UNDO, "Undo", LC_KEYMOD1_CONTROL, LC_KEY_Z, 0 },
	{ LC_EDIT_REDO, "Redo", LC_KEYMOD1_CONTROL, LC_KEY_Y, 0 },
	{ LC_EDIT_CUT, "Cut", LC_KEYMOD1_CONTROL, LC_KEY_X, 0 },
	{ LC_EDIT_COPY, "Copy", LC_KEYMOD1_CONTROL, LC_KEY_C, 0 },
	{ LC_EDIT_PASTE, "Paste", LC_KEYMOD1_CONTROL, LC_KEY_V, 0 },
	{ LC_EDIT_SELECT_ALL, "Select All", LC_KEYMOD1_CONTROL, LC_KEY_A, 0 },
	{ LC_EDIT_SELECT_NONE, "Select None", 0, 0, 0 },
	{ LC_EDIT_SELECT_INVERT, "Select Invert", 0, 0, 0 },
	{ LC_EDIT_SELECT_BYNAME, "Select By Name", 0, 0, 0 },
	{ LC_PIECE_INSERT, "Piece Insert", 0, LC_KEY_INSERT, 0 },
	{ LC_PIECE_DELETE, "Piece Delete", 0, LC_KEY_DELETE, 0 },
//	{ LC_PIECE_MINIFIG, "Minifig Wizard", 0, 0, 0 },
	{ LC_PIECE_ARRAY, "Piece Array", 0, 0, 0 },
//	{ LC_PIECE_COPYKEYS, "", 0, 0, 0 },
	{ LC_PIECE_GROUP, "Piece Group", LC_KEYMOD1_CONTROL, LC_KEY_G, 0 },
	{ LC_PIECE_UNGROUP, "Piece Ungroup", LC_KEYMOD1_CONTROL, LC_KEY_U, 0 },
	{ LC_PIECE_GROUP_ADD, "Group Add Piece", 0, 0, 0 },
	{ LC_PIECE_GROUP_REMOVE, "Group Remove Piece", 0, 0, 0 },
	{ LC_PIECE_GROUP_EDIT, "Group Edit", 0, 0, 0 },
	{ LC_PIECE_HIDE_SELECTED, "Hide Selection", LC_KEYMOD1_CONTROL, LC_KEY_H, 0 },
	{ LC_PIECE_HIDE_UNSELECTED, "Unhide Selection", 0, 0, 0 },
	{ LC_PIECE_UNHIDE_ALL, "Unhide All", 0, 0, 0 },
	{ LC_PIECE_PREVIOUS, "Piece Previous Step", 0, 0, 0 },
	{ LC_PIECE_NEXT, "Piece Next Step", 0, 0, 0 },
	{ LC_VIEW_PREFERENCES, "Preferences", 0, 0, 0 },
//	{ LC_VIEW_ZOOM, "", 0, 0, 0 },
	{ LC_VIEW_ZOOMIN, "Zoom In", 0, 0, 0 },
	{ LC_VIEW_ZOOMOUT, "Zoom Out", 0, 0, 0 },
	{ LC_VIEW_ZOOMEXTENTS, "Zoom Extents", 0, 0, 0 },
//	{ LC_VIEW_VIEWPORTS, "", 0, 0, 0 },
	{ LC_VIEW_STEP_NEXT, "Step Next", 0, 0, 0 },
	{ LC_VIEW_STEP_PREVIOUS, "Step Previous", 0, 0, 0 },
	{ LC_VIEW_STEP_FIRST, "Step First", 0, 0, 0 },
	{ LC_VIEW_STEP_LAST, "Step Last", 0, 0, 0 },
//	{ LC_VIEW_STEP_CHOOSE, "", 0, 0, 0 },
//	{ LC_VIEW_STEP_SET, "", 0, 0, 0 },
	{ LC_VIEW_STEP_INSERT, "Step Insert", 0, 0, 0 },
	{ LC_VIEW_STEP_DELETE, "Step Delete", 0, 0, 0 },
//	{ LC_VIEW_STOP, "", 0, 0, 0 },
//	{ LC_VIEW_PLAY, "", 0, 0, 0 },
//	{ LC_VIEW_CAMERA_MENU, "", 0, 0, 0 },
//	{ LC_VIEW_CAMERA_RESET, "", 0, 0, 0 },
//	{ LC_VIEW_AUTOPAN, "", 0, 0, 0 },
//	{ LC_HELP_ABOUT, "", 0, 0, 0 },
//	{ LC_TOOLBAR_ANIMATION, "", 0, 0, 0 },
//	{ LC_TOOLBAR_ADDKEYS, "", 0, 0, 0 },
//	{ LC_TOOLBAR_SNAPMENU, "", 0, 0, 0 },
//	{ LC_TOOLBAR_LOCKMENU, "", 0, 0, 0 },
//	{ LC_TOOLBAR_SNAPMOVEMENU, "", 0, 0, 0 },
//	{ LC_TOOLBAR_FASTRENDER, "", 0, 0, 0 },
//	{ LC_TOOLBAR_BACKGROUND, "", 0, 0, 0 },
};

const int KeyboardShortcutsCount = sizeof(DefaultKeyboardShortcuts)/sizeof(KeyboardShortcuts[0]);

LC_KEYBOARD_COMMAND KeyboardShortcuts[KeyboardShortcutsCount];

// ============================================================================
// Functions

bool SaveKeyboardShortcuts(const char* FileName)
{
	FileDisk f;

	if (!f.Open(FileName, "wt"))
		return false;

	for (int i = 0; i < KeyboardShortcutsCount; i++)
	{
		LC_KEYBOARD_COMMAND& Cmd = KeyboardShortcuts[i];
		String str;

		str = Cmd.Description;
		str += "=";

		if (Cmd.Key1)
		{
			if (Cmd.Modifiers & LC_KEYMOD1_SHIFT)
				str += "Shift+";

			if (Cmd.Modifiers & LC_KEYMOD1_CONTROL)
				str += "Ctrl+";

			str += "\"";
			str += GetKeyName(Cmd.Key1);
			str += "\"";
		}

		if (Cmd.Key2)
		{
			str += ",";

			if (Cmd.Modifiers & LC_KEYMOD2_SHIFT)
				str += "Shift+";

			if (Cmd.Modifiers & LC_KEYMOD2_CONTROL)
				str += "Ctrl+";

			str += "\"";
			str += GetKeyName(Cmd.Key2);
			str += "\"";
		}

		str += "\n";

		f.Write((const char*)str, str.GetLength());
	}

	return true;
}

bool LoadKeyboardShortcuts(const char* FileName)
{
	FileDisk f;
	int i;

	if (!f.Open(FileName, "rt"))
		return false;

	// Remove all existing shortcuts
	for (i = 0; i < KeyboardShortcutsCount; i++)
	{
		LC_KEYBOARD_COMMAND& Cmd = KeyboardShortcuts[i];

		Cmd.Key1 = 0;
		Cmd.Key2 = 0;
		Cmd.Modifiers = 0;
	}

	char Line[1024];
	while (f.ReadString(Line, 1024))
	{
		char* ptr = strchr(Line, '=');

		if (ptr == NULL)
			continue;

		*ptr = 0;
		ptr++;


		for (i = 0; i < KeyboardShortcutsCount; i++)
		{
			LC_KEYBOARD_COMMAND& Cmd = KeyboardShortcuts[i];

			if (strcmp(Line, Cmd.Description))
				continue;

			if (!strncmp(ptr, "Shift+", 6))
			{
				Cmd.Modifiers |= LC_KEYMOD1_SHIFT;
				ptr += 6;
			}

			if (!strncmp(ptr, "Ctrl+", 5))
			{
				Cmd.Modifiers |= LC_KEYMOD1_CONTROL;
				ptr += 5;
			}

			ptr++;
			char* ptr2 = strchr(ptr, '\"');

			if (ptr2 == NULL)
			{
				Cmd.Modifiers = 0;
				continue;
			}

			*ptr2 = 0;
			Cmd.Key1 = GetKeyFromName(ptr);

			ptr = ptr2 + 1;

			if (*ptr != ',')
				continue;
			ptr++;

			if (!strncmp(ptr, "Shift+", 6))
			{
				Cmd.Modifiers |= LC_KEYMOD2_SHIFT;
				ptr += 6;
			}

			if (!strncmp(ptr, "Ctrl+", 5))
			{
				Cmd.Modifiers |= LC_KEYMOD2_CONTROL;
				ptr += 5;
			}

			ptr++;
			ptr2 = strchr(ptr, '\"');

			if (ptr2 == NULL)
			{
				Cmd.Modifiers &= ~(LC_KEYMOD2_SHIFT | LC_KEYMOD2_CONTROL);
				continue;
			}

			*ptr2 = 0;
			Cmd.Key2 = GetKeyFromName(ptr);
		}
	}

	return true;
}

void ResetKeyboardShortcuts()
{
	memcpy(KeyboardShortcuts, DefaultKeyboardShortcuts, sizeof(KeyboardShortcuts));
}

void InitKeyboardShortcuts()
{
	const char* FileName = Sys_ProfileLoadString("Settings", "Keyboard", "");

	ResetKeyboardShortcuts();
	LoadKeyboardShortcuts(FileName);
}

typedef struct
{
	int Key;
	const char* Name;

} LC_KEYNAME_ENTRY;

static LC_KEYNAME_ENTRY KeyNames[] =
{
	{ LC_KEY_BACK,       "Backspace" },
	{ LC_KEY_TAB,        "Tab" },
	{ LC_KEY_RETURN,     "Return" },
	{ LC_KEY_PAUSE,      "Pause" },
	{ LC_KEY_CAPITAL,    "Caps" },
	{ LC_KEY_ESCAPE,     "Escape" },
	{ LC_KEY_SPACE,      "Space" },
	{ LC_KEY_PRIOR,      "Page Up" },
	{ LC_KEY_NEXT,       "Page Down" },
	{ LC_KEY_END,        "End" },
	{ LC_KEY_HOME,       "Home" },
	{ LC_KEY_LEFT,       "Left" },
	{ LC_KEY_UP,         "Up" },
	{ LC_KEY_RIGHT,      "Right" },
	{ LC_KEY_DOWN,       "Down" },
	{ LC_KEY_SELECT,     "Select" },
	{ LC_KEY_PRINT,      "Print" },
	{ LC_KEY_INSERT,     "Insert" },
	{ LC_KEY_DELETE,     "Delete" },
	{ LC_KEY_0,          "0" },
	{ LC_KEY_1,          "1" },
	{ LC_KEY_2,          "2" },
	{ LC_KEY_3,          "3" },
	{ LC_KEY_4,          "4" },
	{ LC_KEY_5,          "5" },
	{ LC_KEY_6,          "6" },
	{ LC_KEY_7,          "7" },
	{ LC_KEY_8,          "8" },
	{ LC_KEY_9,          "9" },
	{ LC_KEY_A,          "A" },
	{ LC_KEY_B,          "B" },
	{ LC_KEY_C,          "C" },
	{ LC_KEY_D,          "D" },
	{ LC_KEY_E,          "E" },
	{ LC_KEY_F,          "F" },
	{ LC_KEY_G,          "G" },
	{ LC_KEY_H,          "H" },
	{ LC_KEY_I,          "I" },
	{ LC_KEY_J,          "J" },
	{ LC_KEY_K,          "K" },
	{ LC_KEY_L,          "L" },
	{ LC_KEY_M,          "M" },
	{ LC_KEY_N,          "N" },
	{ LC_KEY_O,          "O" },
	{ LC_KEY_P,          "P" },
	{ LC_KEY_Q,          "Q" },
	{ LC_KEY_R,          "R" },
	{ LC_KEY_S,          "S" },
	{ LC_KEY_T,          "T" },
	{ LC_KEY_U,          "U" },
	{ LC_KEY_V,          "V" },
	{ LC_KEY_W,          "W" },
	{ LC_KEY_X,          "X" },
	{ LC_KEY_Y,          "Y" },
	{ LC_KEY_Z,          "Z" },
	{ LC_KEY_NUMPAD0,    "Numpad 0" },
	{ LC_KEY_NUMPAD1,    "Numpad 1" },
	{ LC_KEY_NUMPAD2,    "Numpad 2" },
	{ LC_KEY_NUMPAD3,    "Numpad 3" },
	{ LC_KEY_NUMPAD4,    "Numpad 4" },
	{ LC_KEY_NUMPAD5,    "Numpad 5" },
	{ LC_KEY_NUMPAD6,    "Numpad 6" },
	{ LC_KEY_NUMPAD7,    "Numpad 7" },
	{ LC_KEY_NUMPAD8,    "Numpad 8" },
	{ LC_KEY_NUMPAD9,    "Numpad 9" },
	{ LC_KEY_MULTIPLY,   "Numpad *" },
	{ LC_KEY_ADD,        "Numpad +" },
	{ LC_KEY_SUBTRACT,   "Numpad -" },
	{ LC_KEY_DECIMAL,    "Numpad ." },
	{ LC_KEY_DIVIDE,     "Numpad /" },
	{ LC_KEY_F1,         "F1" },
	{ LC_KEY_F2,         "F2" },
	{ LC_KEY_F3,         "F3" },
	{ LC_KEY_F4,         "F4" },
	{ LC_KEY_F5,         "F5" },
	{ LC_KEY_F6,         "F6" },
	{ LC_KEY_F7,         "F7" },
	{ LC_KEY_F8,         "F8" },
	{ LC_KEY_F9,         "F9" },
	{ LC_KEY_F10,        "F10" },
	{ LC_KEY_F11,        "F11" },
	{ LC_KEY_F12,        "F12" },
	{ LC_KEY_F13,        "F13" },
	{ LC_KEY_F14,        "F14" },
	{ LC_KEY_F15,        "F15" },
	{ LC_KEY_F16,        "F16" },
	{ LC_KEY_F17,        "F17" },
	{ LC_KEY_F18,        "F18" },
	{ LC_KEY_F19,        "F19" },
	{ LC_KEY_F20,        "F20" },
	{ LC_KEY_F21,        "F21" },
	{ LC_KEY_F22,        "F22" },
	{ LC_KEY_F23,        "F23" },
	{ LC_KEY_F24,        "F24" },
	{ LC_KEY_NUMLOCK,    "Num Lock" },
	{ LC_KEY_SCROLL,     "Scroll" }
};

// Returns a string with the name of the key.
const char* GetKeyName(char Key)
{
	int Count = sizeof(KeyNames)/sizeof(KeyNames[0]);

	for (int i = 0; i < Count; i++)
	{
		if (Key == KeyNames[i].Key)
			return KeyNames[i].Name;
	}

	return NULL;
}

char GetKeyFromName(const char* Name)
{
	int Count = sizeof(KeyNames)/sizeof(KeyNames[0]);

	for (int i = 0; i < Count; i++)
	{
		if (!strcmp(Name, KeyNames[i].Name))
			return KeyNames[i].Key;
	}

	return 0;
}