summaryrefslogtreecommitdiff
path: root/Source/Plugins/InputPluginCommon/Src/ControllerEmu.cpp
blob: 6422f59eb4a88445ac700a3262a8f2f057b141ed (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
#include "ControllerEmu.h"

#if defined(HAVE_X11) && HAVE_X11
#include <X11/Xlib.h>
#endif

ControllerEmu::~ControllerEmu()
{
	// control groups
	std::vector<ControlGroup*>::const_iterator
		i = groups.begin(),
		e = groups.end();
	for ( ; i!=e; ++i )
		delete *i;
}

ControllerEmu::ControlGroup::~ControlGroup()
{
	// controls
	std::vector<Control*>::const_iterator
		ci = controls.begin(),
		ce = controls.end();
	for ( ; ci!=ce; ++ci )
		delete *ci;

	// settings
	std::vector<Setting*>::const_iterator
		si = settings.begin(),
		se = settings.end();
	for ( ; si!=se; ++si )
		delete *si;
}

ControllerEmu::Extension::~Extension()
{
	// attachments
	std::vector<ControllerEmu*>::const_iterator
		ai = attachments.begin(),
		ae = attachments.end();
	for ( ; ai!=ae; ++ai )
		delete *ai;
}
ControllerEmu::ControlGroup::Control::~Control()
{
	delete control_ref;
}

void ControllerEmu::UpdateReferences( ControllerInterface& devi )
{
	std::vector<ControlGroup*>::const_iterator
		i = groups.begin(),
		e = groups.end();
	for ( ; i!=e; ++i )
	{
		std::vector<ControlGroup::Control*>::const_iterator
			ci = (*i)->controls.begin(),
			ce = (*i)->controls.end();
		for ( ; ci!=ce; ++ci )
			devi.UpdateReference( (*ci)->control_ref );

		// extension
		if ( GROUP_TYPE_EXTENSION == (*i)->type )
		{
			std::vector<ControllerEmu*>::const_iterator
				ai = ((Extension*)*i)->attachments.begin(),
				ae = ((Extension*)*i)->attachments.end();
			for ( ; ai!=ae; ++ai )
				(*ai)->UpdateReferences( devi );
		}
	}
}

void ControllerEmu::UpdateDefaultDevice()
{
	std::vector<ControlGroup*>::const_iterator
		i = groups.begin(),
		e = groups.end();
	for ( ; i!=e; ++i )
	{
		std::vector<ControlGroup::Control*>::const_iterator
			ci = (*i)->controls.begin(),
			ce = (*i)->controls.end();
		for ( ; ci!=ce; ++ci )
			(*ci)->control_ref->device_qualifier = default_device;

		// extension
		if ( GROUP_TYPE_EXTENSION == (*i)->type )
		{
			std::vector<ControllerEmu*>::const_iterator
				ai = ((Extension*)*i)->attachments.begin(),
				ae = ((Extension*)*i)->attachments.end();
			for ( ; ai!=ae; ++ai )
			{
				(*ai)->default_device = default_device;
				(*ai)->UpdateDefaultDevice();
			}
		}
	}
}

void ControllerEmu::ControlGroup::LoadConfig( IniFile::Section& sec, const std::string& defdev, const std::string& base )
{
	std::string group( base + name ); group += "/";

	// settings
	std::vector<ControlGroup::Setting*>::const_iterator
		si = settings.begin(),
		se = settings.end();
	for ( ; si!=se; ++si )
		(*si)->value = sec.Get(group+(*si)->name, (*si)->default_value*100) / 100;

	// controls
	std::vector<ControlGroup::Control*>::const_iterator
		ci = controls.begin(),
		ce = controls.end();
	for ( ; ci!=ce; ++ci )
	{
		// control and dev qualifier
		(*ci)->control_ref->control_qualifier.name = sec[group + (*ci)->name];
		(*ci)->control_ref->device_qualifier.FromString( sec.Get( group+(*ci)->name+"/Device", defdev ) );

		// range
		(*ci)->control_ref->range = sec.Get( group+(*ci)->name+"/Range", 100.0f ) / 100;

		// input mode
		if ( (*ci)->control_ref->is_input )
			((ControllerInterface::InputReference*)((*ci)->control_ref))->mode
				= sec.Get( group+(*ci)->name+"/Mode", 0 );
	}

	// extensions
	if ( GROUP_TYPE_EXTENSION == type )
	{
		Extension* const ex = ((Extension*)this);

		ex->switch_extension = 0;
		unsigned int n = 0;
		const std::string extname = sec[ base + name ];

		std::vector<ControllerEmu*>::const_iterator
			ai = ((Extension*)this)->attachments.begin(),
			ae = ((Extension*)this)->attachments.end();
		for ( ; ai!=ae; ++ai,++n )
		{
			(*ai)->default_device.FromString( defdev );
			(*ai)->LoadConfig( sec, base + (*ai)->GetName() + "/" );

			if ( (*ai)->GetName() == extname )
				ex->switch_extension = n;
		}
	}
}

void ControllerEmu::LoadConfig( IniFile::Section& sec, const std::string& base )
{
	std::string defdev = default_device.ToString();
	if ( base.empty() )
	{
		defdev =  sec[ base + "Device" ];
		default_device.FromString( defdev );
	}

	std::vector<ControlGroup*>::const_iterator i = groups.begin(),
		e = groups.end();
	for ( ; i!=e; ++i )
		(*i)->LoadConfig( sec, defdev, base );
}

void ControllerEmu::ControlGroup::SaveConfig( IniFile::Section& sec, const std::string& defdev, const std::string& base )
{
	std::string group( base + name ); group += "/";

	// settings
	std::vector<ControlGroup::Setting*>::const_iterator
		si = settings.begin(),
		se = settings.end();
	for ( ; si!=se; ++si )
		sec.Set( group+(*si)->name, (*si)->value*100, (*si)->default_value*100 );

	// controls
	std::vector<ControlGroup::Control*>::const_iterator
		ci = controls.begin(),
		ce = controls.end();
	for ( ; ci!=ce; ++ci )
	{
		// control and dev qualifier
		sec.Set( group+(*ci)->name, (*ci)->control_ref->control_qualifier.name );
		sec.Set( group+(*ci)->name+"/Device", (*ci)->control_ref->device_qualifier.ToString(), defdev );

		// range
		sec.Set( group+(*ci)->name+"/Range", (*ci)->control_ref->range*100, 100 );

		// input mode
		if ( (*ci)->control_ref->is_input )
			sec.Set( group+(*ci)->name+"/Mode",
				((ControllerInterface::InputReference*)((*ci)->control_ref))->mode, (unsigned int)0 );
	}

	// extensions
	if ( GROUP_TYPE_EXTENSION == type )
	{
		Extension* const ext = ((Extension*)this);
		sec.Set( base + name, ext->attachments[ext->switch_extension]->GetName(), std::string("None") );

		std::vector<ControllerEmu*>::const_iterator
			ai = ((Extension*)this)->attachments.begin(),
			ae = ((Extension*)this)->attachments.end();
		for ( ; ai!=ae; ++ai )
			(*ai)->SaveConfig( sec, base + (*ai)->GetName() + "/" );
	}
}

void ControllerEmu::SaveConfig( IniFile::Section& sec, const std::string& base )
{
	const std::string defdev = default_device.ToString();
	if ( base.empty() )
		sec.Set( std::string(" ") + base + "Device", defdev );

	std::vector<ControlGroup*>::const_iterator i = groups.begin(),
		e = groups.end();
	for ( ; i!=e; ++i )
		(*i)->SaveConfig( sec, defdev, base );
}

ControllerEmu::AnalogStick::AnalogStick( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_STICK )
{
	for ( unsigned int i = 0; i < 4; ++i )
		controls.push_back( new Input( named_directions[i] ) );

	controls.push_back( new Input( "Modifier" ) );

	settings.push_back( new Setting("Dead Zone", 0, 1, 50 ) );
	settings.push_back( new Setting("Square Stick", 0 ) );

}

ControllerEmu::Buttons::Buttons( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_BUTTONS )
{
	settings.push_back( new Setting("Threshold", 0.5f ) );
}

ControllerEmu::MixedTriggers::MixedTriggers( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_MIXED_TRIGGERS )
{
	settings.push_back( new Setting("Threshold", 0.9f ) );
}

ControllerEmu::Triggers::Triggers( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_TRIGGERS )
{
	settings.push_back( new Setting("Dead Zone", 0, 1, 50 ) );
}

ControllerEmu::Force::Force( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_FORCE )
{
	controls.push_back( new Input( "Up" ) );
	controls.push_back( new Input( "Down" ) );
	controls.push_back( new Input( "Left" ) );
	controls.push_back( new Input( "Right" ) );
	controls.push_back( new Input( "Forward" ) );
	controls.push_back( new Input( "Backward" ) );
	controls.push_back( new Input( "Modifier" ) );

	settings.push_back( new Setting("Dead Zone", 0, 1, 50 ) );
}

ControllerEmu::Tilt::Tilt( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_TILT )
{
	//for ( unsigned int i = 0; i < 4; ++i )
		//controls.push_back( new Input( named_directions[i] ) );
	controls.push_back( new Input( "Forward" ) );
	controls.push_back( new Input( "Backward" ) );
	controls.push_back( new Input( "Left" ) );
	controls.push_back( new Input( "Right" ) );

	controls.push_back( new Input( "Modifier" ) );

	settings.push_back( new Setting("Dead Zone", 0, 1, 50 ) );
	settings.push_back( new Setting("Circle Stick", 0 ) );
}

ControllerEmu::Cursor::Cursor( const char* const _name, const SWiimoteInitialize* const _wiimote_initialize )
	: ControlGroup( _name, GROUP_TYPE_CURSOR )
	//, z(0)
	, wiimote_initialize(_wiimote_initialize)
{
	for ( unsigned int i = 0; i < 4; ++i )
		controls.push_back( new Input( named_directions[i] ) );
	controls.push_back( new Input( "Forward" ) );
	controls.push_back( new Input( "Hide" ) );

	settings.push_back( new Setting("Center", 0.5f ) );
	settings.push_back( new Setting("Width", 0.5f ) );
	settings.push_back( new Setting("Height", 0.5f ) );

}

void GetMousePos(float& x, float& y, const SWiimoteInitialize* const wiimote_initialize)
{
#if ( defined(_WIN32) || (defined(HAVE_X11) && HAVE_X11))
	unsigned int win_width = 2, win_height = 2;
#endif

#ifdef _WIN32
	// Get the cursor position for the entire screen
	POINT point = { 1, 1 };
	GetCursorPos(&point);
	// Get the cursor position relative to the upper left corner of the rendering window
	ScreenToClient(wiimote_initialize->hWnd, &point);

	// Get the size of the rendering window. (In my case Rect.top and Rect.left was zero.)
	RECT Rect;
	GetClientRect(wiimote_initialize->hWnd, &Rect);
	// Width and height is the size of the rendering window
	win_width = Rect.right - Rect.left;
	win_height = Rect.bottom - Rect.top;

#elif defined(HAVE_X11) && HAVE_X11
	int root_x, root_y;
	struct
	{
		int x, y;
	} point = { 1, 1 };

	Display* const wm_display = (Display*)wiimote_initialize->hWnd;
	Window glwin = *(Window *)wiimote_initialize->pXWindow;

	XWindowAttributes win_attribs;
	XGetWindowAttributes (wm_display, glwin, &win_attribs);
	win_width = win_attribs.width;
	win_height = win_attribs.height;
	Window root_dummy, child_win;
	unsigned int mask;
	XQueryPointer(wm_display, glwin, &root_dummy, &child_win, &root_x, &root_y, &point.x, &point.y, &mask);
#endif

#if ( defined(_WIN32) || (defined(HAVE_X11) && HAVE_X11))
	// Return the mouse position as a range from -1 to 1
	x = (float)point.x / (float)win_width * 2 - 1;
	y = (float)point.y / (float)win_height * 2 - 1;
#else
	x = 0;
	y = 0;
#endif

}