File: ags_controller.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (231 lines) | stat: -rw-r--r-- 7,084 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * of the License, or(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/plugins/ags_controller/ags_controller.h"
#include "ags/events.h"
#include "common/config-manager.h"
#include "common/system.h"

namespace AGS3 {
namespace Plugins {
namespace AGSController {

struct Controller : public IAGSScriptManagedObject {
public:

	int Dispose(void *address, bool force) override {
		return true;
	}

	const char *GetType() override {
		return "Controller";
	};

	int Serialize(void *address, char *buffer, int bufsize) override {
		return 0;
	}
};

Controller *ctrlInterface = nullptr;
ConReader *ctrlReader = nullptr;

Controller *getController() {
	if (!ctrlInterface)
		ctrlInterface = new Controller;

	return ctrlInterface;
}

ConReader *getConReader() {
	if (!ctrlReader)
		ctrlReader = new ConReader;

	return ctrlReader;
}

AGSController::~AGSController() {
	delete ctrlInterface;
	delete ctrlReader;
}

const char *AGSController::AGS_GetPluginName() {
	return "AGSController";
}

void AGSController::AGS_EngineStartup(IAGSEngine *engine) {
	PluginBase::AGS_EngineStartup(engine);

	SCRIPT_METHOD(ControllerCount, AGSController::ControllerCount);
	SCRIPT_METHOD(Controller::Open, AGSController::Controller_Open);
	SCRIPT_METHOD(Controller::Close, AGSController::Controller_Close);
	SCRIPT_METHOD(Controller::Plugged, AGSController::Controller_Plugged);
	SCRIPT_METHOD(Controller::GetAxis, AGSController::Controller_GetAxis);
	SCRIPT_METHOD(Controller::GetPOV, AGSController::Controller_GetPOV);
	SCRIPT_METHOD(Controller::IsButtonDown, AGSController::Controller_IsButtonDown);
	SCRIPT_METHOD(Controller::GetName^0, AGSController::Controller_GetName);
	SCRIPT_METHOD(Controller::Rumble, AGSController::Controller_Rumble);
	SCRIPT_METHOD(Controller::IsButtonDownOnce, AGSController::Controller_IsButtonDownOnce);
	SCRIPT_METHOD(Controller::PressAnyKey, AGSController::Controller_PressAnyKey);
	SCRIPT_METHOD(Controller::BatteryStatus, AGSController::Controller_BatteryStatus);
	SCRIPT_METHOD(ClickMouse, AGSController::ClickMouse);

	_engine->RequestEventHook(AGSE_PREGUIDRAW);
	_engine->AddManagedObjectReader("Controller", getConReader());
}

int64 AGSController::AGS_EngineOnEvent(int event, NumberPtr data) {
	if (event == AGSE_PREGUIDRAW) {
		Controller_Update();
	}

	return 0;
}

void AGSController::Controller_Update() {
	//	::AGS::g_events->pollEvents();
}

void ConReader::Unserialize(int key, const char *serializedData, int dataSize) {
	//	no implementation
}

void AGSController::ControllerCount(ScriptMethodParams &params) {

	// WORKAROUND: The current implementation doesn't work at all, in UIHY at least
	// Just disable the gamepad for now, also to prevent accessing the controller options
	// that crash everything
	if ((ConfMan.get("gameid") == "untilihaveyou")) {
		debug(0, "AGSController: Returning ControllerCount=0 to force-disable controller!");
		params._result = 0;
		return;
	}
	int joystickNum = ConfMan.getInt("joystick_num");
	params._result = (joystickNum == -1) ? 0 : 1;
}

void AGSController::Controller_Open(ScriptMethodParams &params) {
	Controller *newCtrl = new Controller();
	_engine->RegisterManagedObject(newCtrl, getController());
	params._result = newCtrl;
}

void AGSController::Controller_Close(ScriptMethodParams &params) {
	// No implementation needed
}

void AGSController::Controller_Plugged(ScriptMethodParams &params) {
	int joystickNum = ConfMan.getInt("joystick_num");
	params._result = joystickNum != -1;
}

void AGSController::Controller_GetAxis(ScriptMethodParams &params) {
	PARAMS1(int, axis);
	if (axis < 0 || axis > 31)
		params._result = false;
	else
		params._result = ::AGS::g_events->getJoystickAxis(axis);
}

void AGSController::Controller_GetPOV(ScriptMethodParams &params) {
	// Not supported
	debug(0, "AGSController: POV is not implemented");
	params._result = 0;
}

void AGSController::Controller_IsButtonDown(ScriptMethodParams &params) {
	PARAMS1(int, button);
	if (button < 0 || button > 31)
		params._result = false;
	else
		params._result = ::AGS::g_events->getJoystickButton(button);
}

void AGSController::Controller_GetName(ScriptMethodParams &params) {
	int joystickNum = ConfMan.getInt("joystick_num");
	params._result = (joystickNum != -1) ? _engine->CreateScriptString("Joystick")
										 : _engine->CreateScriptString("");
}

void AGSController::Controller_Rumble(ScriptMethodParams &params) {
	// Not supported
	debug(0, "AGSController: Rumble is not supported");
}

void AGSController::Controller_IsButtonDownOnce(ScriptMethodParams &params) {
	PARAMS1(int, button);
	if (button < 0 || button > 31)
		params._result = false;
	else
		params._result = ::AGS::g_events->getJoystickButtonOnce(button);
}

void AGSController::Controller_PressAnyKey(ScriptMethodParams &params) {
	params._result = -1;

	for (int index = 0; index < 32; ++index) {
		if (::AGS::g_events->getJoystickButton(index)) {
			params._result = index;
			break;
		}
	}
}

void AGSController::Controller_BatteryStatus(ScriptMethodParams &params) {
	// Not supported, so return -1 for "UNKNOWN"
	debug(0, "AGSController: Battery status is not supported");
	params._result = -1;
}

void AGSController::ClickMouse(ScriptMethodParams &params) {
	PARAMS1(int, button);

	// WORKAROUND: This method is used to fake mouseclicks in the game menus (for keyboard
	// and gamepad control) but the implementation using eventmanager doesn't seem to work
	// Just use PluginSimulateMouseClick, which matches AGSController original implementation
	if ((ConfMan.get("gameid") == "untilihaveyou")) {
		PluginSimulateMouseClick(button);
		return;
	}

	assert(button < 3);
	Common::EventType DOWN[3] = {
		Common::EVENT_LBUTTONDOWN, Common::EVENT_RBUTTONDOWN, Common::EVENT_MBUTTONDOWN
	};
	Common::EventType UP[3] = {
		Common::EVENT_LBUTTONUP, Common::EVENT_RBUTTONUP, Common::EVENT_MBUTTONUP
	};

	Common::Point mousePos = ::AGS::g_events->getMousePos();
	Common::Event down, up;
	down.type = DOWN[button];
	down.mouse.x = mousePos.x;
	down.mouse.y = mousePos.y;
	g_system->getEventManager()->pushEvent(down);

	up.type = UP[button];
	up.mouse.x = mousePos.x;
	up.mouse.y = mousePos.y;
	g_system->getEventManager()->pushEvent(up);
}

} // namespace AGSController
} // namespace Plugins
} // namespace AGS3