summaryrefslogtreecommitdiffhomepage
path: root/Tobii EyeX/samples/ActivatableButtons/ActivatableButtons.cpp
blob: 8cee1c30208f6e4a3eeb4c537c33c8d9cb9bb24d (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
/*
 * ActivatableButtons sample:
 * This is an example that demonstrates the Activatable behavior.
 * It features two buttons that can be clicked by looking at the button and pressing the space bar.
 *
 * Copyright 2013 Tobii Technology AB. All rights reserved.
 */

#include "stdafx.h"
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include "resource.h"
#include "EyeXHost.h"

#pragma comment (lib, "Gdiplus.lib")

// window messages used for notifications from the EyeXHost.
#define WM_EYEX_HOST_STATUS_CHANGED     WM_USER + 0
#define WM_REGION_GOT_ACTIVATION_FOCUS  WM_USER + 1
#define WM_REGION_ACTIVATED             WM_USER + 2

// constants.
static const Gdiplus::Color OriginalBackgroundColor(66, 173, 209);
static const Gdiplus::Color OtherBackgroundColor(89, 169, 108);
static const int ChangeButtonLeft = 50;
static const int ResetButtonLeft = 260;
static const int ButtonTop = 100;
static const int ButtonWidth = 160;
static const int ButtonHeight = 100;
static const TCHAR* g_szWindowClass = _T("ActivatableButtons");

// global variables.
static EyeXHost g_eyeXHost;
static HWND g_hWnd;
static HWND g_hChangeButton;
static HWND g_hResetButton;
static bool g_useOriginalColor = true;

// gets the bounds of a button in screen coordinates.
RECT GetScreenBounds(HWND hButton)
{
	POINT point = { 0, 0 };
	ClientToScreen(hButton, &point);

	RECT bounds;
	bounds.left = point.x;
	bounds.top = point.y; 
	bounds.right = bounds.left + ButtonWidth;
	bounds.bottom = bounds.top + ButtonHeight;

	return bounds;
}

// reports the buttons as activatable regions to the EyeX host.
void UpdateActivatableRegions()
{
	std::vector<EyeXHost::ActivatableRegion> regions;
   
	regions.push_back(EyeXHost::ActivatableRegion(IDC_CHANGE_COLOR_BUTTON, GetScreenBounds(g_hChangeButton)));
	regions.push_back(EyeXHost::ActivatableRegion(IDC_RESET_COLOR_BUTTON, GetScreenBounds(g_hResetButton)));

	g_eyeXHost.SetActivatableRegions(regions);
}

// event handler invoked when a "status changed" event has been received from the EyeX host.
void OnStatusChanged(bool engineConnectionIsFunctional)
{
	// update the window title to reflect the engine connection state.
	if (engineConnectionIsFunctional)
	{
		SetWindowText(g_hWnd, _T("ActivatableButtons - Use Ur Eyez! :)"));
	}
	else
	{
	    SetWindowText(g_hWnd, _T("ActivatableButtons - Mouse Only :("));
	}
}

// event handler invoked when a region has received the activation focus.
void OnRegionGotActivationFocus(UINT regionId)
{
	// set the keyboard focus to the corresponding button.
	switch(regionId)
	{
	case IDC_CHANGE_COLOR_BUTTON:
		SetFocus(g_hChangeButton);
		break;

	case IDC_RESET_COLOR_BUTTON:
		SetFocus(g_hResetButton);
		break;
	}
}

// event handler invoked when the "change color" button has been activated.
void OnChangeColorActivated()
{
	g_useOriginalColor = false;
	InvalidateRect(g_hWnd, 0, FALSE);
}

// event handler invoked when the "reset color" button has been activated.
void OnResetColorActivated()
{
	g_useOriginalColor = true;
	InvalidateRect(g_hWnd, 0, FALSE);
}

// event handler invoked when a region has received an activation event.
void OnRegionActivated(UINT regionId)
{
	// route the event to the corresponding button.
	switch(regionId)
	{
	case IDC_CHANGE_COLOR_BUTTON:
		OnChangeColorActivated();
		break;

	case IDC_RESET_COLOR_BUTTON:
		OnResetColorActivated();
		break;
	}
}

void OnDraw(HDC hdc)
{
	Gdiplus::Graphics graphics(hdc);

	RECT rect;
	GetClientRect(g_hWnd, &rect);

	Gdiplus::Color backgroundColor(OriginalBackgroundColor);
	if (!g_useOriginalColor)
	{
		backgroundColor = OtherBackgroundColor;
	}

	Gdiplus::SolidBrush backgroundBrush(backgroundColor);
	graphics.FillRectangle(&backgroundBrush, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);

	const TCHAR* message = _T("Click the buttons with your eyes! Look at a button and press the space bar to click. ")
		_T("(Make sure the window has input focus.)");
	Gdiplus::Font font(_T("Arial"), 10);
	Gdiplus::SolidBrush textBrush(Gdiplus::Color::Black);
	Gdiplus::RectF textRect((Gdiplus::REAL)10, 
							(Gdiplus::REAL)(ButtonTop + ButtonHeight + 20), 
							(Gdiplus::REAL)(rect.right - 20), 
							(Gdiplus::REAL)(rect.bottom - rect.top));
	Gdiplus::StringFormat textFormat;
	graphics.DrawString(message, (INT)_tcslen(message), &font, textRect, &textFormat, &textBrush);
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch (wmId)
		{
		case IDC_CHANGE_COLOR_BUTTON:
			OnChangeColorActivated();
			break;

		case IDC_RESET_COLOR_BUTTON:
			OnResetColorActivated();
			break;

		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;

	case WM_CREATE:
		{
			g_hChangeButton = CreateWindow(
				_T("BUTTON"), 
				_T("Change color"),
				WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 
				ChangeButtonLeft, ButtonTop, ButtonWidth, ButtonHeight, 
				hWnd, 
				(HMENU)IDC_CHANGE_COLOR_BUTTON, 
				GetModuleHandle(NULL), 
				NULL); 
			g_hResetButton = CreateWindow(
				_T("BUTTON"), 
				_T("Reset color"), 
				WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 
				ResetButtonLeft, ButtonTop, ButtonWidth, ButtonHeight, 
				hWnd, 
				(HMENU)IDC_RESET_COLOR_BUTTON, 
				GetModuleHandle(NULL), 
				NULL); 
		}
		break;

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		OnDraw(hdc);
		EndPaint(hWnd, &ps);
		break;

	case WM_ERASEBKGND:
		// no background erasing needed since our OnDraw method draws the entire window.
		return TRUE;

	case WM_EYEX_HOST_STATUS_CHANGED:
		OnStatusChanged(wParam != FALSE);
		break;

	case WM_REGION_GOT_ACTIVATION_FOCUS:
		OnRegionGotActivationFocus((UINT)wParam);
		break;

	case WM_REGION_ACTIVATED:
		OnRegionActivated((UINT)wParam);
		break;

	case WM_WINDOWPOSCHANGED:
		UpdateActivatableRegions();
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_KEYDOWN:
		// trigger an activation command when space is pressed.
		if (VK_SPACE == wParam)
		{
			g_eyeXHost.TriggerActivation();
		}
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ACTIVATABLEBUTTONS));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= 0;
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_ACTIVATABLEBUTTONS);
	wcex.lpszClassName	= g_szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   g_hWnd = CreateWindow(g_szWindowClass, _T(""), WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 490, 380, NULL, NULL, hInstance, NULL);

   if (!g_hWnd)
   {
      return FALSE;
   }

   ShowWindow(g_hWnd, nCmdShow);
   UpdateWindow(g_hWnd);

	// initialize the EyeX host and the activatable regions.
   g_eyeXHost.Init(g_hWnd, WM_EYEX_HOST_STATUS_CHANGED, WM_REGION_GOT_ACTIVATION_FOCUS, WM_REGION_ACTIVATED);
   UpdateActivatableRegions();

   return TRUE;
}

// Application entry point.
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// Initialize gdiplus
	ULONG_PTR gdiplusToken;
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	HACCEL hAccelTable;
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ACTIVATABLEBUTTONS));

	// Main message loop:
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	// Shutdown:
	Gdiplus::GdiplusShutdown(gdiplusToken);

	return (int) msg.wParam;
}