actionMap.cc 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "input/actionMap.h"
  23. #include "platform/event.h"
  24. #include "console/console.h"
  25. #include "platform/platform.h"
  26. #include "platform/platformInput.h"
  27. #include "platform/platformAssert.h"
  28. #include "io/fileStream.h"
  29. #include "io/resource/resourceManager.h"
  30. // Script bindings.
  31. #include "actionMap_ScriptBinding.h"
  32. #define CONST_E 2.7182818284590452353602874f
  33. IMPLEMENT_CONOBJECT(ActionMap);
  34. // This is used for determing keys that have ascii codes for the foreign keyboards. IsAlpha doesn't work on foreign keys.
  35. #define dIsDecentChar(c) (((char(0xa0) <= (c)) && ((c) <= char(0xff))) || (( char(0x21) <= (c)) && ((c) <= char(0x7e))) || (( char(0x91) <= (c)) && ((c) <= char(0x92))))
  36. struct CodeMapping
  37. {
  38. const char* pDescription;
  39. U8 type;
  40. U32 code;
  41. };
  42. struct AsciiMapping
  43. {
  44. const char* pDescription;
  45. U16 asciiCode;
  46. };
  47. extern CodeMapping gVirtualMap[];
  48. extern AsciiMapping gAsciiMap[];
  49. //------------------------------------------------------------------------------
  50. //-------------------------------------- Action maps
  51. //
  52. Vector<ActionMap::BreakEntry> ActionMap::smBreakTable(__FILE__, __LINE__);
  53. //------------------------------------------------------------------------------
  54. ActionMap::ActionMap()
  55. {
  56. VECTOR_SET_ASSOCIATION(mDeviceMaps);
  57. }
  58. //------------------------------------------------------------------------------
  59. ActionMap::~ActionMap()
  60. {
  61. for (U32 i = 0; i < (U32)mDeviceMaps.size(); i++)
  62. delete mDeviceMaps[i];
  63. mDeviceMaps.clear();
  64. }
  65. //------------------------------------------------------------------------------
  66. ActionMap::DeviceMap::~DeviceMap()
  67. {
  68. for(U32 i = 0; i < (U32)nodeMap.size(); i++)
  69. {
  70. dFree(nodeMap[i].makeConsoleCommand);
  71. dFree(nodeMap[i].breakConsoleCommand);
  72. }
  73. }
  74. //------------------------------------------------------------------------------
  75. bool ActionMap::onAdd()
  76. {
  77. if (Parent::onAdd() == false)
  78. return false;
  79. Sim::getActionMapGroup()->addObject(this);
  80. return true;
  81. }
  82. //--------------------------------------------------------------------------
  83. void ActionMap::dumpActionMap(const char* fileName, const bool append) const
  84. {
  85. if (fileName != NULL) {
  86. // Dump the deletion, and creation script commands, followed by all the binds
  87. // to a script.
  88. FileStream iostrm;
  89. if ( !ResourceManager->openFileForWrite( iostrm, fileName, append ? FileStream::WriteAppend : FileStream::Write ) )
  90. {
  91. Con::errorf( "Unable to open file '%s' for writing.", fileName );
  92. return;
  93. }
  94. char lineBuffer[1024];
  95. if ( append )
  96. iostrm.setPosition( iostrm.getStreamSize() );
  97. else
  98. {
  99. // IMPORTANT -- do NOT change the following line, it identifies the file as an input map file
  100. dStrcpy( lineBuffer, "// Torque Input Map File\n" );
  101. iostrm.write( dStrlen( lineBuffer ), lineBuffer );
  102. }
  103. dSprintf(lineBuffer, 1023, "if (isObject(%s)) %s.delete();\n"
  104. "new ActionMap(%s);\n", getName(), getName(), getName());
  105. iostrm.write(dStrlen(lineBuffer), lineBuffer);
  106. // Dump all the binds to the console...
  107. for (S32 i = 0; i < mDeviceMaps.size(); i++) {
  108. const DeviceMap* pDevMap = mDeviceMaps[i];
  109. char devbuffer[32];
  110. getDeviceName(pDevMap->deviceType, pDevMap->deviceInst, devbuffer);
  111. for (S32 j = 0; j < pDevMap->nodeMap.size(); j++) {
  112. const Node& rNode = pDevMap->nodeMap[j];
  113. const char* pModifierString = getModifierString(rNode.modifiers);
  114. char objectbuffer[64];
  115. if (getKeyString(rNode.action, objectbuffer) == false)
  116. continue;
  117. const char* command = (rNode.flags & Node::BindCmd) ? "bindCmd" : "bind";
  118. dSprintf(lineBuffer, 1023, "%s.%s(%s, \"%s%s\"",
  119. getName(),
  120. command,
  121. devbuffer,
  122. pModifierString, objectbuffer);
  123. if (rNode.flags & (Node::HasScale|Node::HasDeadZone|Node::Ranged|Node::Inverted)) {
  124. char buff[10];
  125. U32 curr = 0;
  126. buff[curr++] = ',';
  127. buff[curr++] = ' ';
  128. if (rNode.flags & Node::HasScale)
  129. buff[curr++] = 'S';
  130. if (rNode.flags & Node::Ranged)
  131. buff[curr++] = 'R';
  132. if (rNode.flags & Node::HasDeadZone)
  133. buff[curr++] = 'D';
  134. if (rNode.flags & Node::Inverted)
  135. buff[curr++] = 'I';
  136. buff[curr] = '\0';
  137. dStrcat(lineBuffer, buff);
  138. }
  139. if (rNode.flags & Node::HasDeadZone) {
  140. char buff[64];
  141. dSprintf(buff, 63, ", \"%g %g\"", rNode.deadZoneBegin, rNode.deadZoneEnd);
  142. dStrcat(lineBuffer, buff);
  143. }
  144. if (rNode.flags & Node::HasScale) {
  145. char buff[64];
  146. dSprintf(buff, 63, ", %g", rNode.scaleFactor);
  147. dStrcat(lineBuffer, buff);
  148. }
  149. if (rNode.flags & Node::BindCmd) {
  150. if (rNode.makeConsoleCommand) {
  151. dStrcat(lineBuffer, ", \"");
  152. U32 pos = dStrlen(lineBuffer);
  153. expandEscape(lineBuffer + pos, rNode.makeConsoleCommand);
  154. dStrcat(lineBuffer, "\"");
  155. } else {
  156. dStrcat(lineBuffer, ", \"\"");
  157. }
  158. if (rNode.breakConsoleCommand) {
  159. dStrcat(lineBuffer, ", \"");
  160. U32 pos = dStrlen(lineBuffer);
  161. expandEscape(lineBuffer + pos, rNode.breakConsoleCommand);
  162. dStrcat(lineBuffer, "\"");
  163. }
  164. else
  165. dStrcat(lineBuffer, ", \"\"");
  166. } else {
  167. dStrcat(lineBuffer, ", ");
  168. dStrcat(lineBuffer, rNode.consoleFunction);
  169. }
  170. dStrcat(lineBuffer, ");\n");
  171. iostrm.write(dStrlen(lineBuffer), lineBuffer);
  172. }
  173. }
  174. iostrm.close();
  175. }
  176. else {
  177. // Dump all the binds to the console...
  178. for (S32 i = 0; i < mDeviceMaps.size(); i++) {
  179. const DeviceMap* pDevMap = mDeviceMaps[i];
  180. char devbuffer[32];
  181. getDeviceName(pDevMap->deviceType, pDevMap->deviceInst, devbuffer);
  182. for (S32 j = 0; j < pDevMap->nodeMap.size(); j++) {
  183. const Node& rNode = pDevMap->nodeMap[j];
  184. const char* pModifierString = getModifierString(rNode.modifiers);
  185. char keybuffer[64];
  186. if (getKeyString(rNode.action, keybuffer) == false)
  187. continue;
  188. const char* command = (rNode.flags & Node::BindCmd) ? "bindCmd" : "bind";
  189. char finalBuffer[1024];
  190. dSprintf(finalBuffer, 1023, "%s.%s(%s, \"%s%s\"",
  191. getName(),
  192. command,
  193. devbuffer,
  194. pModifierString, keybuffer);
  195. if (rNode.flags & (Node::HasScale|Node::HasDeadZone|Node::Ranged|Node::Inverted)) {
  196. char buff[10];
  197. U32 curr = 0;
  198. buff[curr++] = ',';
  199. buff[curr++] = ' ';
  200. if (rNode.flags & Node::HasScale)
  201. buff[curr++] = 'S';
  202. if (rNode.flags & Node::Ranged)
  203. buff[curr++] = 'R';
  204. if (rNode.flags & Node::HasDeadZone)
  205. buff[curr++] = 'D';
  206. if (rNode.flags & Node::Inverted)
  207. buff[curr++] = 'I';
  208. buff[curr] = '\0';
  209. dStrcat(finalBuffer, buff);
  210. }
  211. if (rNode.flags & Node::HasDeadZone) {
  212. char buff[64];
  213. dSprintf(buff, 63, ", \"%g %g\"", rNode.deadZoneBegin, rNode.deadZoneEnd);
  214. dStrcat(finalBuffer, buff);
  215. }
  216. if (rNode.flags & Node::HasScale) {
  217. char buff[64];
  218. dSprintf(buff, 63, ", %g", rNode.scaleFactor);
  219. dStrcat(finalBuffer, buff);
  220. }
  221. if (rNode.flags & Node::BindCmd) {
  222. if (rNode.makeConsoleCommand) {
  223. dStrcat(finalBuffer, ", \"");
  224. dStrcat(finalBuffer, rNode.makeConsoleCommand);
  225. dStrcat(finalBuffer, "\"");
  226. } else {
  227. dStrcat(finalBuffer, ", \"\"");
  228. }
  229. if (rNode.breakConsoleCommand) {
  230. dStrcat(finalBuffer, ", \"");
  231. dStrcat(finalBuffer, rNode.breakConsoleCommand);
  232. dStrcat(finalBuffer, "\"");
  233. }
  234. else
  235. dStrcat(finalBuffer, ", \"\"");
  236. } else {
  237. dStrcat(finalBuffer, ", ");
  238. dStrcat(finalBuffer, rNode.consoleFunction);
  239. }
  240. dStrcat(finalBuffer, ");");
  241. Con::printf(finalBuffer);
  242. }
  243. }
  244. }
  245. }
  246. //--------------------------------------------------------------------------
  247. bool ActionMap::createEventDescriptor(const char* pEventString, EventDescriptor* pDescriptor)
  248. {
  249. char copyBuffer[256];
  250. dStrcpy(copyBuffer, pEventString);
  251. // Do we have modifiers?
  252. char* pSpace = dStrchr(copyBuffer, ' ');
  253. char* pObjectString;
  254. if (pSpace != NULL) {
  255. // Yes. Parse them out...
  256. //
  257. pDescriptor->flags = 0;
  258. pObjectString = pSpace + 1;
  259. pSpace[0] = '\0';
  260. char* pModifier = dStrtok(copyBuffer, "-");
  261. while (pModifier != NULL) {
  262. if (dStricmp(pModifier, "shift") == 0) {
  263. pDescriptor->flags |= SI_SHIFT;
  264. } else if (dStricmp(pModifier, "lshift") == 0) {
  265. pDescriptor->flags |= SI_LSHIFT;
  266. } else if (dStricmp(pModifier, "rshift") == 0) {
  267. pDescriptor->flags |= SI_RSHIFT;
  268. } else if (dStricmp(pModifier, "ctrl") == 0) {
  269. pDescriptor->flags |= SI_CTRL;
  270. } else if (dStricmp(pModifier, "lctrl") == 0) {
  271. pDescriptor->flags |= SI_LCTRL;
  272. } else if (dStricmp(pModifier, "rctrl") == 0) {
  273. pDescriptor->flags |= SI_RCTRL;
  274. } else if (dStricmp(pModifier, "alt") == 0) {
  275. pDescriptor->flags |= SI_ALT;
  276. } else if (dStricmp(pModifier, "lalt") == 0) {
  277. pDescriptor->flags |= SI_LALT;
  278. } else if (dStricmp(pModifier, "ralt") == 0) {
  279. pDescriptor->flags |= SI_RALT;
  280. } else if (dStricmp(pModifier, "cmd") == 0) {
  281. pDescriptor->flags |= SI_ALT;
  282. } else if (dStricmp(pModifier, "opt") == 0) {
  283. pDescriptor->flags |= SI_MAC_OPT;
  284. } else if (dStricmp(pModifier, "lopt") == 0) {
  285. pDescriptor->flags |= SI_MAC_LOPT;
  286. } else if (dStricmp(pModifier, "ropt") == 0) {
  287. pDescriptor->flags |= SI_MAC_ROPT;
  288. }
  289. pModifier = dStrtok(NULL, "-");
  290. }
  291. } else {
  292. // No.
  293. pDescriptor->flags = 0;
  294. pObjectString = copyBuffer;
  295. }
  296. // Now we need to map the key string to the proper KEY code from event.h
  297. //
  298. AssertFatal(dStrlen(pObjectString) != 0, "Error, no key was specified!");
  299. if (dStrlen(pObjectString) == 1)
  300. {
  301. if (dIsDecentChar(*pObjectString)) // includes foreign chars
  302. {
  303. U16 asciiCode = (*pObjectString);
  304. // clear out the FF in upper 8bits for foreign keys??
  305. asciiCode &= 0xFF;
  306. U16 keyCode = Input::getKeyCode(asciiCode);
  307. if ( keyCode >= KEY_0 )
  308. {
  309. pDescriptor->eventType = SI_KEY;
  310. pDescriptor->eventCode = keyCode;
  311. return true;
  312. }
  313. else if (dIsalpha(*pObjectString) == true)
  314. {
  315. pDescriptor->eventType = SI_KEY;
  316. pDescriptor->eventCode = KEY_A+dTolower(*pObjectString)-'a';
  317. return true;
  318. }
  319. else if (dIsdigit(*pObjectString) == true)
  320. {
  321. pDescriptor->eventType = SI_KEY;
  322. pDescriptor->eventCode = KEY_0+(*pObjectString)-'0';
  323. return true;
  324. }
  325. }
  326. return false;
  327. }
  328. else
  329. {
  330. pDescriptor->eventCode = 0;
  331. // Gotta search through the Ascii table...
  332. for (U16 i = 0; gAsciiMap[i].asciiCode != 0xFFFF; i++)
  333. {
  334. if (dStricmp(pObjectString, gAsciiMap[i].pDescription) == 0)
  335. {
  336. U16 asciiCode = gAsciiMap[i].asciiCode;
  337. U16 keyCode = Input::getKeyCode(asciiCode);
  338. if ( keyCode >= KEY_0 )
  339. {
  340. pDescriptor->eventType = SI_KEY;
  341. pDescriptor->eventCode = keyCode;
  342. return(true);
  343. }
  344. else
  345. {
  346. break;
  347. }
  348. }
  349. }
  350. // Didn't find an ascii match. Check the virtual map table
  351. for (U32 j = 0; gVirtualMap[j].code != 0xFFFFFFFF; j++)
  352. {
  353. if (dStricmp(pObjectString, gVirtualMap[j].pDescription) == 0)
  354. {
  355. pDescriptor->eventType = gVirtualMap[j].type;
  356. pDescriptor->eventCode = gVirtualMap[j].code;
  357. return true;
  358. }
  359. }
  360. }
  361. return false;
  362. }
  363. //------------------------------------------------------------------------------
  364. ActionMap::Node* ActionMap::getNode(const U32 inDeviceType, const U32 inDeviceInst,
  365. const U32 inModifiers, const U32 inAction,SimObject* object /*= NULL*/)
  366. {
  367. // DMMTODO - Slow INITIAL implementation. Replace with a faster version...
  368. //
  369. DeviceMap* pDeviceMap = NULL;
  370. U32 i;
  371. for (i = 0; i < (U32)mDeviceMaps.size(); i++)
  372. {
  373. if (mDeviceMaps[i]->deviceType == inDeviceType &&
  374. mDeviceMaps[i]->deviceInst == inDeviceInst) {
  375. pDeviceMap = mDeviceMaps[i];
  376. break;
  377. }
  378. }
  379. if (pDeviceMap == NULL)
  380. {
  381. mDeviceMaps.increment();
  382. mDeviceMaps.last() = new DeviceMap;
  383. pDeviceMap = mDeviceMaps.last();
  384. pDeviceMap->deviceInst = inDeviceInst;
  385. pDeviceMap->deviceType = inDeviceType;
  386. }
  387. for (i = 0; i < (U32)pDeviceMap->nodeMap.size(); i++)
  388. {
  389. if (pDeviceMap->nodeMap[i].modifiers == inModifiers &&
  390. pDeviceMap->nodeMap[i].action == inAction &&
  391. ( (object != NULL) ? object == pDeviceMap->nodeMap[i].object : true )) // Check for an object match if the object exists
  392. {
  393. return &pDeviceMap->nodeMap[i];
  394. }
  395. }
  396. // If we're here, the node doesn't exist. create it.
  397. pDeviceMap->nodeMap.increment();
  398. Node* pRetNode = &pDeviceMap->nodeMap.last();
  399. pRetNode->modifiers = inModifiers;
  400. pRetNode->action = inAction;
  401. pRetNode->flags = 0;
  402. pRetNode->deadZoneBegin = 0.0;
  403. pRetNode->deadZoneEnd = 0.0;
  404. pRetNode->scaleFactor = 1.0;
  405. pRetNode->consoleFunction = NULL;
  406. pRetNode->makeConsoleCommand = NULL;
  407. pRetNode->breakConsoleCommand = NULL;
  408. //[neob, 5/7/2007 - #2975]
  409. pRetNode->object = 0;
  410. return pRetNode;
  411. }
  412. //------------------------------------------------------------------------------
  413. void ActionMap::removeNode(const U32 inDeviceType, const U32 inDeviceInst, const U32 inModifiers, const U32 inAction, SimObject* object /*= NULL*/)
  414. {
  415. // DMMTODO - Slow INITIAL implementation. Replace with a faster version...
  416. //
  417. DeviceMap* pDeviceMap = NULL;
  418. U32 i;
  419. for (i = 0; i < (U32)mDeviceMaps.size(); i++) {
  420. if (mDeviceMaps[i]->deviceType == inDeviceType &&
  421. mDeviceMaps[i]->deviceInst == inDeviceInst) {
  422. pDeviceMap = mDeviceMaps[i];
  423. break;
  424. }
  425. }
  426. if (pDeviceMap == NULL)
  427. return;
  428. U32 realMods = inModifiers;
  429. if (realMods & SI_SHIFT)
  430. realMods |= SI_SHIFT;
  431. if (realMods & SI_CTRL)
  432. realMods |= SI_CTRL;
  433. if (realMods & SI_ALT)
  434. realMods |= SI_ALT;
  435. if (realMods & SI_MAC_OPT)
  436. realMods |= SI_MAC_OPT;
  437. for (i = 0; i < (U32)pDeviceMap->nodeMap.size(); i++) {
  438. if (pDeviceMap->nodeMap[i].modifiers == realMods &&
  439. pDeviceMap->nodeMap[i].action == inAction &&
  440. ( (object != NULL) ? object == pDeviceMap->nodeMap[i].object : true ))
  441. {
  442. dFree(pDeviceMap->nodeMap[i].makeConsoleCommand);
  443. dFree(pDeviceMap->nodeMap[i].breakConsoleCommand);
  444. pDeviceMap->nodeMap.erase(i);
  445. }
  446. }
  447. }
  448. //------------------------------------------------------------------------------
  449. const ActionMap::Node* ActionMap::findNode(const U32 inDeviceType, const U32 inDeviceInst,
  450. const U32 inModifiers, const U32 inAction)
  451. {
  452. // DMMTODO - Slow INITIAL implementation. Replace with a faster version...
  453. //
  454. DeviceMap* pDeviceMap = NULL;
  455. U32 i;
  456. for (i = 0; i < (U32)mDeviceMaps.size(); i++)
  457. {
  458. if (mDeviceMaps[i]->deviceType == inDeviceType && mDeviceMaps[i]->deviceInst == inDeviceInst)
  459. {
  460. pDeviceMap = mDeviceMaps[i];
  461. break;
  462. }
  463. }
  464. if (pDeviceMap == NULL)
  465. return NULL;
  466. U32 realMods = inModifiers;
  467. if (realMods & SI_SHIFT)
  468. realMods |= SI_SHIFT;
  469. if (realMods & SI_CTRL)
  470. realMods |= SI_CTRL;
  471. if (realMods & SI_ALT)
  472. realMods |= SI_ALT;
  473. if (realMods & SI_MAC_OPT)
  474. realMods |= SI_MAC_OPT;
  475. for (i = 0; i < (U32)pDeviceMap->nodeMap.size(); i++)
  476. {
  477. if (pDeviceMap->nodeMap[i].action == KEY_ANYKEY && pDeviceMap->nodeMap[i].modifiers == realMods && dIsDecentChar(inAction))
  478. return &pDeviceMap->nodeMap[i];
  479. else if (inModifiers == 0 && pDeviceMap->nodeMap[i].modifiers == 0 && pDeviceMap->nodeMap[i].action == inAction)
  480. return &pDeviceMap->nodeMap[i];
  481. else if (pDeviceMap->nodeMap[i].modifiers == realMods && pDeviceMap->nodeMap[i].action == inAction)
  482. return &pDeviceMap->nodeMap[i];
  483. }
  484. return NULL;
  485. }
  486. //------------------------------------------------------------------------------
  487. bool ActionMap::findBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex )
  488. {
  489. devMapIndex = 0;
  490. nodeIndex = 0;
  491. return nextBoundNode( function, devMapIndex, nodeIndex );
  492. }
  493. bool ActionMap::nextBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex )
  494. {
  495. // Loop through all of the existing nodes to find the one mapped to the
  496. // given function:
  497. for ( U32 i = devMapIndex; i < (U32)mDeviceMaps.size(); i++ )
  498. {
  499. const DeviceMap* dvcMap = mDeviceMaps[i];
  500. for ( U32 j = nodeIndex; j < (U32)dvcMap->nodeMap.size(); j++ )
  501. {
  502. const Node* node = &dvcMap->nodeMap[j];
  503. if ( !( node->flags & Node::BindCmd ) && ( dStricmp( function, node->consoleFunction ) == 0 ) )
  504. {
  505. devMapIndex = i;
  506. nodeIndex = j;
  507. return( true );
  508. }
  509. }
  510. nodeIndex = 0;
  511. }
  512. return( false );
  513. }
  514. //------------------------------------------------------------------------------
  515. bool ActionMap::processUnbind(const char *device, const char *action, SimObject* object /*= NULL*/)
  516. {
  517. U32 deviceType;
  518. U32 deviceInst;
  519. if(!getDeviceTypeAndInstance(device, deviceType, deviceInst))
  520. return false;
  521. EventDescriptor eventDescriptor;
  522. if (!createEventDescriptor(action, &eventDescriptor))
  523. return false;
  524. removeNode(deviceType, deviceInst, eventDescriptor.flags,eventDescriptor.eventCode, object);
  525. return true;
  526. }
  527. //------------------------------------------------------------------------------
  528. // This function is for the use of the control remapper.
  529. // It will only check against the console function (since all remappable commands are
  530. // bound using bind and not bindCmd).
  531. //
  532. const char* ActionMap::getBinding( const char* command )
  533. {
  534. char* returnString = Con::getReturnBuffer( 1024 );
  535. returnString[0] = 0;
  536. char buffer[256];
  537. char deviceBuffer[32];
  538. char keyBuffer[64];
  539. U32 devMapIndex = 0, nodeIndex = 0;
  540. while ( nextBoundNode( command, devMapIndex, nodeIndex ) )
  541. {
  542. const DeviceMap* deviceMap = mDeviceMaps[devMapIndex];
  543. if ( getDeviceName( deviceMap->deviceType, deviceMap->deviceInst, deviceBuffer ) )
  544. {
  545. const Node* node = &deviceMap->nodeMap[nodeIndex];
  546. const char* modifierString = getModifierString( node->modifiers );
  547. if ( getKeyString( node->action, keyBuffer ) )
  548. {
  549. dSprintf( buffer, sizeof( buffer ), "%s\t%s%s", deviceBuffer, modifierString, keyBuffer );
  550. if ( returnString[0] )
  551. dStrcat( returnString, "\t" );
  552. dStrcat( returnString, buffer );
  553. }
  554. }
  555. ++nodeIndex;
  556. }
  557. return returnString;
  558. }
  559. //------------------------------------------------------------------------------
  560. // This function is for the use of the control remapper.
  561. // The intent of this function is to determine if the given event descriptor is already
  562. // bound in this action map. If so, this function returns the command it is bound to.
  563. // If not, it returns NULL.
  564. //
  565. const char* ActionMap::getCommand( const char* device, const char* action )
  566. {
  567. U32 deviceType;
  568. U32 deviceInst;
  569. if ( getDeviceTypeAndInstance( device, deviceType, deviceInst ) )
  570. {
  571. EventDescriptor eventDescriptor;
  572. if ( createEventDescriptor( action, &eventDescriptor ) )
  573. {
  574. const ActionMap::Node* mapNode = findNode( deviceType, deviceInst, eventDescriptor.flags, eventDescriptor.eventCode );
  575. if ( mapNode )
  576. {
  577. if ( mapNode->flags & Node::BindCmd )
  578. {
  579. S32 bufferLen = dStrlen( mapNode->makeConsoleCommand ) + dStrlen( mapNode->breakConsoleCommand ) + 2;
  580. char* returnString = Con::getReturnBuffer( bufferLen );
  581. dSprintf( returnString, bufferLen, "%s\t%s",
  582. ( mapNode->makeConsoleCommand ? mapNode->makeConsoleCommand : "" ),
  583. ( mapNode->breakConsoleCommand ? mapNode->breakConsoleCommand : "" ) );
  584. return( returnString );
  585. }
  586. else
  587. return( mapNode->consoleFunction );
  588. }
  589. }
  590. }
  591. return( "" );
  592. }
  593. //------------------------------------------------------------------------------
  594. // This function returns whether or not the mapping specified is inverted.
  595. // Obviously, this should only be used for axes.
  596. bool ActionMap::isInverted( const char* device, const char* action )
  597. {
  598. U32 deviceType;
  599. U32 deviceInst;
  600. if ( getDeviceTypeAndInstance( device, deviceType, deviceInst ) )
  601. {
  602. EventDescriptor eventDescriptor;
  603. if ( createEventDescriptor( action, &eventDescriptor ) )
  604. {
  605. const ActionMap::Node* mapNode = findNode( deviceType, deviceInst, eventDescriptor.flags, eventDescriptor.eventCode );
  606. if ( mapNode )
  607. return( mapNode->flags & Node::Inverted );
  608. }
  609. }
  610. Con::errorf( "The input event specified by %s %s is not in this action map!", device, action );
  611. return( false );
  612. }
  613. //------------------------------------------------------------------------------
  614. F32 ActionMap::getScale( const char* device, const char* action )
  615. {
  616. U32 deviceType;
  617. U32 deviceInst;
  618. if ( getDeviceTypeAndInstance( device, deviceType, deviceInst ) )
  619. {
  620. EventDescriptor eventDescriptor;
  621. if ( createEventDescriptor( action, &eventDescriptor ) )
  622. {
  623. const ActionMap::Node* mapNode = findNode( deviceType, deviceInst, eventDescriptor.flags, eventDescriptor.eventCode );
  624. if ( mapNode )
  625. {
  626. if ( mapNode->flags & Node::HasScale )
  627. return( mapNode->scaleFactor );
  628. else
  629. return( 1.0f );
  630. }
  631. }
  632. }
  633. Con::errorf( "The input event specified by %s %s is not in this action map!", device, action );
  634. return( 1.0f );
  635. }
  636. //------------------------------------------------------------------------------
  637. const char* ActionMap::getDeadZone( const char* device, const char* action )
  638. {
  639. U32 deviceType;
  640. U32 deviceInst;
  641. if ( getDeviceTypeAndInstance( device, deviceType, deviceInst ) )
  642. {
  643. EventDescriptor eventDescriptor;
  644. if ( createEventDescriptor( action, &eventDescriptor ) )
  645. {
  646. const ActionMap::Node* mapNode = findNode( deviceType, deviceInst, eventDescriptor.flags, eventDescriptor.eventCode );
  647. if ( mapNode )
  648. {
  649. if ( mapNode->flags & Node::HasDeadZone )
  650. {
  651. char buf[64];
  652. dSprintf( buf, sizeof( buf ), "%g %g", mapNode->deadZoneBegin, mapNode->deadZoneEnd );
  653. char* returnString = Con::getReturnBuffer( dStrlen( buf ) + 1 );
  654. dStrcpy( returnString, buf );
  655. return( returnString );
  656. }
  657. else
  658. return( "0 0" );
  659. }
  660. }
  661. }
  662. Con::errorf( "The input event specified by %s %s is not in this action map!", device, action );
  663. return( "" );
  664. }
  665. //------------------------------------------------------------------------------
  666. const char* ActionMap::buildActionString( const InputEvent* event )
  667. {
  668. const char* modifierString = getModifierString( event->modifier );
  669. char objectBuffer[64];
  670. if ( !getKeyString( event->objInst, objectBuffer ) )
  671. return( "" );
  672. U32 returnLen = dStrlen( modifierString ) + dStrlen( objectBuffer ) + 2;
  673. char* returnString = Con::getReturnBuffer( returnLen );
  674. dSprintf( returnString, returnLen - 1, "%s%s", modifierString, objectBuffer );
  675. return( returnString );
  676. }
  677. //------------------------------------------------------------------------------
  678. bool ActionMap::getDeviceTypeAndInstance(const char *pDeviceName, U32 &deviceType, U32 &deviceInstance)
  679. {
  680. U32 offset = 0;
  681. if (dStrnicmp(pDeviceName, "keyboard", dStrlen("keyboard")) == 0)
  682. {
  683. deviceType = KeyboardDeviceType;
  684. offset = dStrlen("keyboard");
  685. }
  686. else if (dStrnicmp(pDeviceName, "mouse", dStrlen("mouse")) == 0)
  687. {
  688. deviceType = MouseDeviceType;
  689. offset = dStrlen("mouse");
  690. }
  691. else if (dStrnicmp(pDeviceName, "joystick", dStrlen("joystick")) == 0)
  692. {
  693. deviceType = JoystickDeviceType;
  694. offset = dStrlen("joystick");
  695. }
  696. else if (dStrnicmp(pDeviceName, "accelerometer", dStrlen("accelerometer")) == 0)
  697. {
  698. deviceType = AccelerometerDeviceType;
  699. offset = dStrlen("accelerometer");
  700. }
  701. else if (dStrnicmp(pDeviceName, "gyroscope", dStrlen("gyroscope")) == 0)
  702. {
  703. deviceType = GyroscopeDeviceType;
  704. offset = dStrlen("gyroscope");
  705. }
  706. else if (dStrnicmp(pDeviceName, "touchdevice", dStrlen("touchdevice")) == 0)
  707. {
  708. deviceType = ScreenTouchDeviceType;
  709. offset = dStrlen("touchdevice");
  710. }
  711. else if (dStrnicmp(pDeviceName, "gamepad", dStrlen("gamepad")) == 0)
  712. {
  713. deviceType = GamepadDeviceType;
  714. offset = dStrlen("gamepad");
  715. }
  716. else
  717. return false;
  718. if (dStrlen(pDeviceName) > offset)
  719. {
  720. const char* pInst = pDeviceName + offset;
  721. S32 instNum = dAtoi(pInst);
  722. if (instNum < 0)
  723. deviceInstance = 0;
  724. else
  725. deviceInstance = instNum;
  726. }
  727. else
  728. deviceInstance = 0;
  729. return true;
  730. }
  731. //------------------------------------------------------------------------------
  732. bool ActionMap::getDeviceName(const U32 deviceType, const U32 deviceInstance, char* buffer)
  733. {
  734. switch (deviceType)
  735. {
  736. case KeyboardDeviceType:
  737. dStrcpy(buffer, "keyboard");
  738. break;
  739. case MouseDeviceType:
  740. dSprintf(buffer, 16, "mouse%d", deviceInstance);
  741. break;
  742. case JoystickDeviceType:
  743. dSprintf(buffer, 16, "joystick%d", deviceInstance);
  744. break;
  745. case AccelerometerDeviceType:
  746. dStrcpy(buffer, "accelerometer");
  747. break;
  748. case GyroscopeDeviceType:
  749. dStrcpy(buffer, "gyroscope");
  750. break;
  751. case ScreenTouchDeviceType:
  752. dStrcpy(buffer, "touchdevice");
  753. break;
  754. case GamepadDeviceType:
  755. dSprintf(buffer, 16, "gamepad%d", deviceInstance);
  756. break;
  757. default:
  758. Con::errorf( "ActionMap::getDeviceName: unknown device type specified, %d (inst: %d)", deviceType, deviceInstance);
  759. return false;
  760. }
  761. return true;
  762. }
  763. //------------------------------------------------------------------------------
  764. const char* ActionMap::getModifierString(const U32 modifiers)
  765. {
  766. U32 realModifiers = modifiers;
  767. if ( modifiers & SI_LSHIFT || modifiers & SI_RSHIFT )
  768. realModifiers |= SI_SHIFT;
  769. if ( modifiers & SI_LCTRL || modifiers & SI_RCTRL )
  770. realModifiers |= SI_CTRL;
  771. if ( modifiers & SI_LALT || modifiers & SI_RALT )
  772. realModifiers |= SI_ALT;
  773. if ( modifiers & SI_MAC_LOPT || modifiers & SI_MAC_ROPT )
  774. realModifiers |= SI_MAC_OPT;
  775. switch (realModifiers & (SI_SHIFT|SI_CTRL|SI_ALT|SI_MAC_OPT)) {
  776. #if defined(TORQUE_OS_OSX)
  777. // optional code, to output alt as cmd on mac.
  778. // interpreter sees them as the same...
  779. case (SI_SHIFT|SI_CTRL|SI_ALT):
  780. return "cmd-shift-ctrl ";
  781. case (SI_SHIFT|SI_ALT):
  782. return "cmd-shift ";
  783. case (SI_CTRL|SI_ALT):
  784. return "cmd-ctrl ";
  785. case (SI_ALT):
  786. return "cmd ";
  787. #else
  788. case (SI_SHIFT|SI_CTRL|SI_ALT):
  789. return "shift-ctrl-alt ";
  790. case (SI_SHIFT|SI_ALT):
  791. return "shift-alt ";
  792. case (SI_CTRL|SI_ALT):
  793. return "ctrl-alt ";
  794. case (SI_ALT):
  795. return "alt ";
  796. #endif
  797. case (SI_SHIFT|SI_CTRL):
  798. return "shift-ctrl ";
  799. case (SI_SHIFT):
  800. return "shift ";
  801. case (SI_CTRL):
  802. return "ctrl ";
  803. // plus new mac cases:
  804. case (SI_ALT|SI_SHIFT|SI_CTRL|SI_MAC_OPT):
  805. return "cmd-shift-ctrl-opt ";
  806. case (SI_ALT|SI_SHIFT|SI_MAC_OPT):
  807. return "cmd-shift-opt ";
  808. case (SI_ALT|SI_CTRL|SI_MAC_OPT):
  809. return "cmd-ctrl-opt ";
  810. case (SI_ALT|SI_MAC_OPT):
  811. return "cmd-opt ";
  812. case (SI_SHIFT|SI_CTRL|SI_MAC_OPT):
  813. return "shift-ctrl-opt ";
  814. case (SI_SHIFT|SI_MAC_OPT):
  815. return "shift-opt ";
  816. case (SI_CTRL|SI_MAC_OPT):
  817. return "ctrl-opt ";
  818. case (SI_MAC_OPT):
  819. return "opt ";
  820. case 0:
  821. return "";
  822. default:
  823. AssertFatal(false, "Error, should never reach the default case in getModifierString");
  824. return "";
  825. }
  826. }
  827. //------------------------------------------------------------------------------
  828. bool ActionMap::getKeyString(const U32 action, char* buffer)
  829. {
  830. U16 asciiCode = Input::getAscii(action, STATE_LOWER);
  831. // if (action >= KEY_A && action <= KEY_Z) {
  832. // buffer[0] = char(action - KEY_A + 'a');
  833. // buffer[1] = '\0';
  834. // return true;
  835. // } else if (action >= KEY_0 && action <= KEY_9) {
  836. // buffer[0] = char(action - KEY_0 + '0');
  837. // buffer[1] = '\0';
  838. if ( (asciiCode != 0) && dIsDecentChar((char)asciiCode))
  839. {
  840. for (U32 i = 0; gAsciiMap[i].asciiCode != 0xFFFF; i++) {
  841. if (gAsciiMap[i].asciiCode == asciiCode)
  842. {
  843. dStrcpy(buffer, gAsciiMap[i].pDescription);
  844. return true;
  845. }
  846. }
  847. // Must not have found a string for that ascii code just record the char
  848. buffer[0] = char(asciiCode);
  849. buffer[1] = '\0';
  850. return true;
  851. }
  852. else
  853. {
  854. if (action >= KEY_A && action <= KEY_Z)
  855. {
  856. buffer[0] = char(action - KEY_A + 'a');
  857. buffer[1] = '\0';
  858. return true;
  859. }
  860. else if (action >= KEY_0 && action <= KEY_9) {
  861. buffer[0] = char(action - KEY_0 + '0');
  862. buffer[1] = '\0';
  863. return true;
  864. }
  865. for (U32 i = 0; gVirtualMap[i].code != 0xFFFFFFFF; i++) {
  866. if (gVirtualMap[i].code == action) {
  867. dStrcpy(buffer, gVirtualMap[i].pDescription);
  868. return true;
  869. }
  870. }
  871. }
  872. Con::errorf( "ActionMap::getKeyString: no string for action %d", action );
  873. return false;
  874. }
  875. //--------------------------------------------------------------------------
  876. bool ActionMap::processBindCmd(const char *device, const char *action, const char *makeCmd, const char *breakCmd)
  877. {
  878. U32 deviceType;
  879. U32 deviceInst;
  880. if(!getDeviceTypeAndInstance(device, deviceType, deviceInst))
  881. {
  882. Con::printf("processBindCmd: unknown device: %s", device);
  883. return false;
  884. }
  885. // Ok, we now have the deviceType and instance. Create an event descriptor
  886. // for the bind...
  887. //
  888. EventDescriptor eventDescriptor;
  889. if (createEventDescriptor(action, &eventDescriptor) == false) {
  890. Con::printf("Could not create a description for binding: %s", action);
  891. return false;
  892. }
  893. // SI_POV == SI_MOVE, and the POV works fine with bindCmd, so we have to add these manually.
  894. if( ( eventDescriptor.eventCode == SI_XAXIS ) ||
  895. ( eventDescriptor.eventCode == SI_YAXIS ) ||
  896. ( eventDescriptor.eventCode == SI_ZAXIS ) ||
  897. ( eventDescriptor.eventCode == SI_RXAXIS ) ||
  898. ( eventDescriptor.eventCode == SI_RYAXIS ) ||
  899. ( eventDescriptor.eventCode == SI_RZAXIS ) ||
  900. ( eventDescriptor.eventCode == SI_SLIDER ) ||
  901. ( eventDescriptor.eventCode == SI_XPOV ) ||
  902. ( eventDescriptor.eventCode == SI_YPOV ) ||
  903. ( eventDescriptor.eventCode == SI_XPOV2 ) ||
  904. ( eventDescriptor.eventCode == SI_YPOV2 ) ||
  905. ( eventDescriptor.eventCode == SI_ACCELX ) ||
  906. ( eventDescriptor.eventCode == SI_ACCELY ) ||
  907. ( eventDescriptor.eventCode == SI_ACCELZ ) ||
  908. ( eventDescriptor.eventCode == SI_GRAVX ) ||
  909. ( eventDescriptor.eventCode == SI_GRAVY ) ||
  910. ( eventDescriptor.eventCode == SI_GRAVZ ) ||
  911. ( eventDescriptor.eventCode == SI_GYROX ) ||
  912. ( eventDescriptor.eventCode == SI_GYROY ) ||
  913. ( eventDescriptor.eventCode == SI_GYROZ ) ||
  914. ( eventDescriptor.eventCode == SI_YAW ) ||
  915. ( eventDescriptor.eventCode == SI_PITCH ) ||
  916. ( eventDescriptor.eventCode == SI_ROLL ) )
  917. {
  918. Con::warnf( "ActionMap::processBindCmd - Cannot use 'bindCmd' with a move event type. Use 'bind' instead." );
  919. return false;
  920. }
  921. // Create the full bind entry, and place it in the map
  922. //
  923. // DMMTODO
  924. Node* pBindNode = getNode(deviceType, deviceInst,
  925. eventDescriptor.flags,
  926. eventDescriptor.eventCode);
  927. pBindNode->flags = Node::BindCmd;
  928. pBindNode->deadZoneBegin = 0;
  929. pBindNode->deadZoneEnd = 0;
  930. pBindNode->scaleFactor = 1;
  931. if(makeCmd[0])
  932. pBindNode->makeConsoleCommand = dStrdup(makeCmd);
  933. else
  934. pBindNode->makeConsoleCommand = dStrdup("");
  935. if(breakCmd[0])
  936. pBindNode->breakConsoleCommand = dStrdup(breakCmd);
  937. else
  938. pBindNode->breakConsoleCommand = dStrdup("");
  939. return true;
  940. }
  941. //------------------------------------------------------------------------------
  942. bool ActionMap::processBind(const U32 argc, const char** argv, SimObject* object)
  943. {
  944. // Ok, the bind will come in the following format:
  945. // [device] [key or button] <[param spec] [param] ...> [fnName]
  946. //
  947. const char* pDeviceName = argv[0];
  948. const char* pEvent = argv[1];
  949. const char* pFnName = argv[argc - 1];
  950. // Determine the device
  951. U32 deviceType;
  952. U32 deviceInst;
  953. if(!getDeviceTypeAndInstance(argv[0], deviceType, deviceInst))
  954. {
  955. Con::printf("processBind: unknown device: %s", pDeviceName);
  956. return false;
  957. }
  958. // Ok, we now have the deviceType and instance. Create an event descriptor
  959. // for the bind...
  960. //
  961. EventDescriptor eventDescriptor;
  962. if (createEventDescriptor(pEvent, &eventDescriptor) == false) {
  963. Con::printf("Could not create a description for binding: %s", pEvent);
  964. return false;
  965. }
  966. // Event has now been described, and device determined. we need now to extract
  967. // any modifiers that the action map will apply to incoming events before
  968. // calling the bound function...
  969. //
  970. // DMMTODO
  971. U32 assignedFlags = 0;
  972. F32 deadZoneBegin = 0.0f;
  973. F32 deadZoneEnd = 0.0f;
  974. F32 scaleFactor = 1.0f;
  975. if (argc != 3) {
  976. // We have the following: "[DSIR]" [deadZone] [scale]
  977. //
  978. const char* pSpec = argv[2];
  979. for (U32 i = 0; pSpec[i] != '\0'; i++) {
  980. switch (pSpec[i]) {
  981. case 'r': case 'R':
  982. assignedFlags |= Node::HasScale;
  983. break;
  984. case 's': case 'S':
  985. assignedFlags |= Node::HasScale;
  986. break;
  987. case 'd': case 'D':
  988. assignedFlags |= Node::HasDeadZone;
  989. break;
  990. case 'i': case 'I':
  991. assignedFlags |= Node::Inverted;
  992. break;
  993. case 'n': case 'N':
  994. assignedFlags |= Node::NonLinear;
  995. break;
  996. default:
  997. AssertFatal(false, avar("Misunderstood specifier in bind (spec string: %s)",
  998. pSpec));
  999. }
  1000. }
  1001. // Ok, we have the flags. Scan the dead zone and scale, if any.
  1002. //
  1003. U32 curArg = 3;
  1004. if (assignedFlags & Node::HasDeadZone) {
  1005. dSscanf(argv[curArg], "%g %g", &deadZoneBegin, &deadZoneEnd);
  1006. curArg++;
  1007. }
  1008. if (assignedFlags & Node::HasScale) {
  1009. scaleFactor = dAtof(argv[curArg]);
  1010. curArg++;
  1011. }
  1012. if (curArg != (argc - 1)) {
  1013. AssertFatal(curArg == (argc - 1), "error in bind spec somewhere...");
  1014. Con::printf("Improperly specified bind for key: %s", argv[2]);
  1015. return false;
  1016. }
  1017. }
  1018. // Ensure that the console function is properly specified?
  1019. //
  1020. // DMMTODO
  1021. // Create the full bind entry, and place it in the map
  1022. //
  1023. // DMMTODO
  1024. Node* pBindNode = getNode(deviceType, deviceInst,
  1025. eventDescriptor.flags,
  1026. eventDescriptor.eventCode, object);
  1027. pBindNode->flags = assignedFlags;
  1028. pBindNode->deadZoneBegin = deadZoneBegin;
  1029. pBindNode->deadZoneEnd = deadZoneEnd;
  1030. pBindNode->scaleFactor = scaleFactor;
  1031. pBindNode->object = object;
  1032. pBindNode->consoleFunction = StringTable->insert(pFnName);
  1033. return true;
  1034. }
  1035. //------------------------------------------------------------------------------
  1036. bool ActionMap::processGesture(const InputEvent* pEvent)
  1037. {
  1038. static const char *argv[6];
  1039. char buffer[64];
  1040. const Node* pNode = findNode( pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objType );
  1041. if (pNode == NULL)
  1042. {
  1043. // Check to see if we clear the modifiers, do we find an action?
  1044. if (pEvent->modifier != 0)
  1045. pNode = findNode(pEvent->deviceType, pEvent->deviceInst, 0, pEvent->objInst);
  1046. if (pNode == NULL)
  1047. return false;
  1048. }
  1049. // "Do nothing" bind:
  1050. if ( !pNode->consoleFunction[0] )
  1051. return( true );
  1052. // Function
  1053. argv[0] = pNode->consoleFunction;
  1054. switch(pEvent->objType)
  1055. {
  1056. case SI_CIRCLE_GESTURE:
  1057. // ID
  1058. argv[1] = Con::getIntArg(pEvent->iValue);
  1059. // Progress
  1060. argv[2] = Con::getFloatArg(pEvent->fValues[0]);
  1061. // Radius
  1062. argv[3] = Con::getFloatArg(pEvent->fValues[1]);
  1063. // Direction (1 clockwise, 0 counter-clockwise)
  1064. argv[4] = Con::getFloatArg(pEvent->fValues[2]);
  1065. // State
  1066. argv[5] = Con::getFloatArg(pEvent->fValues[3]);
  1067. if (pNode->object)
  1068. Con::executef(pNode->object, 6, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
  1069. else
  1070. Con::execute(6, argv);
  1071. break;
  1072. case SI_SWIPE_GESTURE:
  1073. // ID
  1074. argv[1] = Con::getIntArg(pEvent->iValue);
  1075. // State
  1076. argv[2] = Con::getFloatArg(pEvent->fValues[0]);
  1077. // Direction
  1078. dSprintf(buffer, sizeof(buffer), "%f %f %f", pEvent->fValues[1], pEvent->fValues[2], pEvent->fValues[3]);
  1079. argv[3] = buffer;
  1080. // Speed
  1081. argv[4] = Con::getFloatArg(pEvent->fValues[4]);
  1082. if (pNode->object)
  1083. Con::executef(pNode->object, 5, argv[0], argv[1], argv[2], argv[3], argv[4]);
  1084. else
  1085. Con::execute(5, argv);
  1086. break;
  1087. case SI_KEYTAP_GESTURE:
  1088. case SI_SCREENTAP_GESTURE:
  1089. // ID
  1090. argv[1] = Con::getIntArg(pEvent->iValue);
  1091. // Position
  1092. dSprintf(buffer, sizeof(buffer), "%f %f %f", pEvent->fValues[0], pEvent->fValues[1], pEvent->fValues[2]);
  1093. argv[2] = buffer;
  1094. // Direction
  1095. dSprintf(buffer, sizeof(buffer), "%f %f %f", pEvent->fValues[3], pEvent->fValues[4], pEvent->fValues[5]);
  1096. argv[3] = buffer;
  1097. if (pNode->object)
  1098. Con::executef(pNode->object, 4, argv[0], argv[1], argv[2], argv[3]);
  1099. else
  1100. Con::execute(5, argv);
  1101. break;
  1102. case SI_PINCH_GESTURE:
  1103. case SI_SCALE_GESTURE:
  1104. default:
  1105. return true;
  1106. }
  1107. return true;
  1108. }
  1109. //------------------------------------------------------------------------------
  1110. bool ActionMap::processTouch(const InputEvent* pEvent)
  1111. {
  1112. static const char *argv[4];
  1113. const Node* pNode = findNode(pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objInst);
  1114. if (pNode == NULL)
  1115. {
  1116. // Check to see if we clear the modifiers, do we find an action?
  1117. if (pEvent->modifier != 0)
  1118. pNode = findNode(pEvent->deviceType, pEvent->deviceInst, 0, pEvent->objInst);
  1119. if (pNode == NULL)
  1120. return false;
  1121. }
  1122. // "Do nothing" bind:
  1123. if ( !pNode->consoleFunction[0] )
  1124. return( true );
  1125. // Ok, we're all set up, call the function.
  1126. argv[0] = pNode->consoleFunction;
  1127. argv[1] = pEvent->fingerIDs;
  1128. argv[2] = pEvent->fingersX;
  1129. argv[3] = pEvent->fingersY;
  1130. if (pNode->object)
  1131. Con::executef(pNode->object, 4, argv[0], argv[1], argv[2], argv[3]);
  1132. else
  1133. Con::execute(4, argv);
  1134. return true;
  1135. }
  1136. //------------------------------------------------------------------------------
  1137. bool ActionMap::processButton(const InputEvent* pEvent)
  1138. {
  1139. static const char *argv[2];
  1140. const Node* pNode = findNode(pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objInst);
  1141. if (pNode == NULL)
  1142. {
  1143. // Check to see if we clear the modifiers, do we find an action?
  1144. if (pEvent->modifier != 0)
  1145. pNode = findNode(pEvent->deviceType, pEvent->deviceInst, 0, pEvent->objInst);
  1146. if (pNode == NULL)
  1147. return false;
  1148. }
  1149. // Whadda ya know, we have this bound. Set up, and call the console
  1150. // function associated with it...
  1151. //
  1152. F32 value = pEvent->fValues[0];
  1153. if (pNode->flags & Node::Ranged)
  1154. {
  1155. value = (value * 2.0f) - 1.0f;
  1156. if (pNode->flags & Node::Inverted)
  1157. value *= -1.0f;
  1158. }
  1159. else
  1160. {
  1161. if (pNode->flags & Node::Inverted)
  1162. value = 1.0f - value;
  1163. }
  1164. if (pNode->flags & Node::HasScale)
  1165. value *= pNode->scaleFactor;
  1166. if (pNode->flags & Node::HasDeadZone)
  1167. {
  1168. if (value >= pNode->deadZoneBegin && value <= pNode->deadZoneEnd)
  1169. value = 0.0f;
  1170. else
  1171. {
  1172. if( value > 0 )
  1173. value = ( value - pNode->deadZoneBegin ) * ( 1.f / ( 1.f - pNode->deadZoneBegin ) );
  1174. else
  1175. value = ( value + pNode->deadZoneBegin ) * ( 1.f / ( 1.f - pNode->deadZoneBegin ) );
  1176. }
  1177. }
  1178. if( pNode->flags & Node::NonLinear )
  1179. value = ( value < 0.f ? -1.f : 1.f ) * mPow( mFabs( value ), CONST_E );
  1180. // Ok, we're all set up, call the function.
  1181. if(pNode->flags & Node::BindCmd)
  1182. {
  1183. // it's a bind command
  1184. if(pNode->makeConsoleCommand)
  1185. Con::evaluate(pNode->makeConsoleCommand);
  1186. }
  1187. else if ( pNode->consoleFunction[0] )
  1188. {
  1189. argv[0] = pNode->consoleFunction;
  1190. argv[1] = Con::getFloatArg(value);
  1191. if (pNode->object)
  1192. Con::executef(pNode->object, 2, argv[0], argv[1]);
  1193. else
  1194. Con::execute(2, argv);
  1195. }
  1196. // [neo, 5/13/2007 - #3109]
  1197. // The execs/evaluate above could have called reentrant script code which made calls to
  1198. // bindCmd() etc, channging the node map underneath us. If enough nodes were added then
  1199. // the node map vector would realloc, with the result that pNode would then be pointing
  1200. // at garbage and cause a crash when passed to enterBreakEvent() below. So we just look
  1201. // it up again to be safe. This is not needed in the other cases below as we return right
  1202. // after the execs and don't use pNode again.
  1203. pNode = findNode( pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objInst );
  1204. if (pNode == NULL)
  1205. {
  1206. if (pEvent->modifier != 0)
  1207. {
  1208. // Check to see if we clear the modifiers, do we find an action?
  1209. pNode = findNode(pEvent->deviceType, pEvent->deviceInst, 0, pEvent->objInst);
  1210. if (pNode == NULL)
  1211. {
  1212. // We already called any bound methods/functions so our job is done
  1213. return true;
  1214. }
  1215. }
  1216. }
  1217. // And enter the break into the table if this is a make event...
  1218. enterBreakEvent(pEvent, pNode);
  1219. return true;
  1220. }
  1221. //------------------------------------------------------------------------------
  1222. bool ActionMap::processMove(const InputEvent* pEvent)
  1223. {
  1224. static const char *argv[4];
  1225. if (pEvent->deviceType == MouseDeviceType)
  1226. {
  1227. const Node* pNode = findNode(pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objType);
  1228. if (pNode == NULL)
  1229. {
  1230. // Check to see if we clear the modifiers, do we find an action?
  1231. if (pEvent->modifier != 0)
  1232. pNode = findNode(pEvent->deviceType, pEvent->deviceInst, 0, pEvent->objType);
  1233. if (pNode == NULL)
  1234. return false;
  1235. }
  1236. // "Do nothing" bind:
  1237. if ( !pNode->consoleFunction[0] )
  1238. return( true );
  1239. // Whadda ya know, we have this bound. Set up, and call the console
  1240. // function associated with it. Mouse events ignore range and dead
  1241. // zone params.
  1242. //
  1243. F32 value = pEvent->fValues[0];
  1244. if (pNode->flags & Node::Inverted)
  1245. value *= -1.0f;
  1246. if (pNode->flags & Node::HasScale)
  1247. value *= pNode->scaleFactor;
  1248. // Ok, we're all set up, call the function.
  1249. argv[0] = pNode->consoleFunction;
  1250. argv[1] = Con::getFloatArg(value);
  1251. if (pNode->object)
  1252. Con::executef(pNode->object, 2, argv[0], argv[1]);
  1253. else
  1254. Con::execute(2, argv);
  1255. return true;
  1256. }
  1257. else if ( (pEvent->objType == XI_POS || pEvent->objType == XI_FLOAT || pEvent->objType == XI_ROT || pEvent->objType == XI_INT) )
  1258. {
  1259. const Node* pNode = findNode(pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objInst);
  1260. if( pNode == NULL )
  1261. return false;
  1262. // Ok, we're all set up, call the function.
  1263. argv[0] = pNode->consoleFunction;
  1264. S32 argc = 1;
  1265. if (pEvent->objType == XI_INT)
  1266. {
  1267. // Handle the integer as some sort of motion such as a
  1268. // single component to an absolute position
  1269. argv[1] = Con::getIntArg( pEvent->iValue );
  1270. argc += 1;
  1271. }
  1272. else if (pEvent->objType == XI_FLOAT)
  1273. {
  1274. // Handle float as some sort of motion such as a
  1275. // single component to an absolute position
  1276. argv[1] = Con::getFloatArg( pEvent->fValues[0] );
  1277. argc += 1;
  1278. }
  1279. else if (pEvent->objType == XI_POS)
  1280. {
  1281. // Handle Point3F type position
  1282. argv[1] = Con::getFloatArg( pEvent->fValues[0] );
  1283. argv[2] = Con::getFloatArg( pEvent->fValues[1] );
  1284. argv[3] = Con::getFloatArg( pEvent->fValues[2] );
  1285. argc += 3;
  1286. }
  1287. if (pNode->object)
  1288. {
  1289. Con::execute(pNode->object, argc, argv);
  1290. }
  1291. else
  1292. {
  1293. Con::execute(argc, argv);
  1294. }
  1295. return true;
  1296. }
  1297. else if ( pEvent->deviceType == JoystickDeviceType )
  1298. {
  1299. // Joystick events...
  1300. const Node* pNode = findNode( pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objType );
  1301. if( pNode == NULL )
  1302. return false;
  1303. // "Do nothing" bind:
  1304. if ( !pNode->consoleFunction[0] )
  1305. return( true );
  1306. // Whadda ya know, we have this bound. Set up, and call the console
  1307. // function associated with it. Joystick move events are the same as mouse
  1308. // move events except that they don't ignore dead zone.
  1309. //
  1310. F32 value = pEvent->fValues[0];
  1311. if ( pNode->flags & Node::Inverted )
  1312. value *= -1.0f;
  1313. if ( pNode->flags & Node::HasScale )
  1314. value *= pNode->scaleFactor;
  1315. if ( pNode->flags & Node::HasDeadZone )
  1316. {
  1317. if ( value >= pNode->deadZoneBegin && value <= pNode->deadZoneEnd )
  1318. {
  1319. value = 0.0f;
  1320. }
  1321. else
  1322. {
  1323. if( value > 0 )
  1324. value = ( value - pNode->deadZoneBegin ) * ( 1.f / ( 1.f - pNode->deadZoneBegin ) );
  1325. else
  1326. value = ( value + pNode->deadZoneBegin ) * ( 1.f / ( 1.f - pNode->deadZoneBegin ) );
  1327. }
  1328. }
  1329. if( pNode->flags & Node::NonLinear )
  1330. value = ( value < 0.f ? -1.f : 1.f ) * mPow( mFabs( value ), CONST_E );
  1331. // Ok, we're all set up, call the function.
  1332. argv[0] = pNode->consoleFunction;
  1333. argv[1] = Con::getFloatArg( value );
  1334. if (pNode->object)
  1335. Con::executef(pNode->object, 2, argv[0], argv[1]);
  1336. else
  1337. Con::execute(2, argv);
  1338. return true;
  1339. }
  1340. else if ( pEvent->deviceType == GamepadDeviceType )
  1341. {
  1342. // Joystick events...
  1343. const Node* pNode = findNode( pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objInst );
  1344. if( pNode == NULL )
  1345. return false;
  1346. // "Do nothing" bind:
  1347. if ( !pNode->consoleFunction[0] )
  1348. return( true );
  1349. // Whadda ya know, we have this bound. Set up, and call the console
  1350. // function associated with it. Joystick move events are the same as mouse
  1351. // move events except that they don't ignore dead zone.
  1352. //
  1353. F32 value = pEvent->fValues[0];
  1354. if ( pNode->flags & Node::Inverted )
  1355. value *= -1.0f;
  1356. if ( pNode->flags & Node::HasScale )
  1357. value *= pNode->scaleFactor;
  1358. if ( pNode->flags & Node::HasDeadZone )
  1359. {
  1360. if ( value >= pNode->deadZoneBegin && value <= pNode->deadZoneEnd )
  1361. {
  1362. value = 0.0f;
  1363. }
  1364. else
  1365. {
  1366. if( value > 0 )
  1367. value = ( value - pNode->deadZoneBegin ) * ( 1.f / ( 1.f - pNode->deadZoneBegin ) );
  1368. else
  1369. value = ( value + pNode->deadZoneBegin ) * ( 1.f / ( 1.f - pNode->deadZoneBegin ) );
  1370. }
  1371. }
  1372. if( pNode->flags & Node::NonLinear )
  1373. value = ( value < 0.f ? -1.f : 1.f ) * mPow( mFabs( value ), CONST_E );
  1374. // Ok, we're all set up, call the function.
  1375. argv[0] = pNode->consoleFunction;
  1376. argv[1] = Con::getFloatArg( value );
  1377. if (pNode->object)
  1378. Con::executef(pNode->object, 2, argv[0], argv[1]);
  1379. else
  1380. Con::execute(2, argv);
  1381. return true;
  1382. }
  1383. return false;
  1384. }
  1385. //------------------------------------------------------------------------------
  1386. bool ActionMap::processMotion(const InputEvent* pEvent)
  1387. {
  1388. static const char *argv[2];
  1389. // iOS Accelerometer, Gyroscope and DeviceMotion processing
  1390. // Currently, this is identical to the joystick handling.
  1391. // This was copied over into its own section because this will
  1392. // give us a dedicated section to tweak processing based on iOS specific
  1393. // devices. No point in trying to mangle joystick code any further
  1394. const Node* pNode = findNode( pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objType );
  1395. if ( pNode == NULL )
  1396. {
  1397. // Check to see if we clear the modifiers, do we find an action?
  1398. if (pEvent->modifier != 0)
  1399. pNode = findNode( pEvent->deviceType, pEvent->deviceInst, 0, pEvent->objType );
  1400. if ( pNode == NULL )
  1401. return false;
  1402. }
  1403. // "Do nothing" bind:
  1404. if ( !pNode->consoleFunction[0] )
  1405. return( true );
  1406. F32 value = pEvent->fValues[0];
  1407. if ( pNode->flags & Node::Inverted )
  1408. value *= -1.0f;
  1409. if ( pNode->flags & Node::HasScale )
  1410. value *= pNode->scaleFactor;
  1411. if ( pNode->flags & Node::HasDeadZone )
  1412. {
  1413. if ( value >= pNode->deadZoneBegin && value <= pNode->deadZoneEnd )
  1414. value = 0.0f;
  1415. }
  1416. // Ok, we're all set up, call the function.
  1417. argv[0] = pNode->consoleFunction;
  1418. argv[1] = Con::getFloatArg( value );
  1419. if (pNode->object)
  1420. Con::executef(pNode->object, 2, argv[0], argv[1]);
  1421. else
  1422. Con::execute(2, argv);
  1423. return true;
  1424. }
  1425. //------------------------------------------------------------------------------
  1426. bool ActionMap::processXInput(const InputEvent* pEvent)
  1427. {
  1428. static const char *argv[2];
  1429. if ((pEvent->objType == XI_FLOAT || pEvent->objType == XI_INT))
  1430. {
  1431. const Node* pNode = findNode( pEvent->deviceType, pEvent->deviceInst, pEvent->modifier, pEvent->objType );
  1432. if (pNode == NULL )
  1433. return false;
  1434. // Ok, we're all set up, call the function.
  1435. argv[0] = pNode->consoleFunction;
  1436. S32 argc = 1;
  1437. if (pEvent->objType == XI_INT)
  1438. {
  1439. // Handle the integer as some sort of motion such as a
  1440. // single component to an absolute position
  1441. argv[1] = Con::getIntArg( pEvent->iValue );
  1442. argc += 1;
  1443. }
  1444. else if (pEvent->objType == XI_FLOAT)
  1445. {
  1446. // Handle float as some sort of motion such as a
  1447. // single component to an absolute position
  1448. argv[1] = Con::getFloatArg( pEvent->fValues[0] );
  1449. argc += 1;
  1450. }
  1451. if (pNode->object)
  1452. {
  1453. Con::execute(pNode->object, argc, argv);
  1454. }
  1455. else
  1456. {
  1457. Con::execute(argc, argv);
  1458. }
  1459. }
  1460. return true;
  1461. }
  1462. //------------------------------------------------------------------------------
  1463. bool ActionMap::processAction(const InputEvent* pEvent)
  1464. {
  1465. switch(pEvent->action)
  1466. {
  1467. case SI_GESTURE:
  1468. return processGesture(pEvent);
  1469. break;
  1470. case SI_TOUCH:
  1471. return processTouch(pEvent);
  1472. break;
  1473. case SI_MAKE:
  1474. return processButton(pEvent);
  1475. break;
  1476. case SI_MOVE:
  1477. return processMove(pEvent);
  1478. break;
  1479. case SI_MOTION:
  1480. return processMotion(pEvent);
  1481. break;
  1482. case SI_BREAK:
  1483. return checkBreakTable(pEvent);
  1484. break;
  1485. case SI_VALUE:
  1486. return processXInput(pEvent);
  1487. }
  1488. return false;
  1489. }
  1490. //------------------------------------------------------------------------------
  1491. void ActionMap::enterBreakEvent(const InputEvent* pEvent, const Node* pNode)
  1492. {
  1493. // There aren't likely to be many breaks outstanding at any one given time,
  1494. // so a simple linear search is probably sufficient. Note that the break table
  1495. // is static to the class, all breaks are directed to the action map that received
  1496. // the make.
  1497. //
  1498. S32 entry = -1;
  1499. for (U32 i = 0; i < (U32)smBreakTable.size(); i++) {
  1500. if (smBreakTable[i].deviceType == U32(pEvent->deviceType) &&
  1501. smBreakTable[i].deviceInst == U32(pEvent->deviceInst) &&
  1502. smBreakTable[i].objInst == U32(pEvent->objInst)) {
  1503. // Match.
  1504. entry = i;
  1505. break;
  1506. }
  1507. }
  1508. if (entry == -1) {
  1509. smBreakTable.increment();
  1510. entry = smBreakTable.size() - 1;
  1511. smBreakTable[entry].deviceType = pEvent->deviceType;
  1512. smBreakTable[entry].deviceInst = pEvent->deviceInst;
  1513. smBreakTable[entry].objInst = pEvent->objInst;
  1514. }
  1515. // Ok, we now have the entry, and know that the device desc. and the objInst match.
  1516. // Copy out the node information...
  1517. //
  1518. smBreakTable[entry].object = pNode->object;
  1519. // [neo, 5/7/2007 - #2975]
  1520. // object above can be deleted in between a make/break and so object will point
  1521. // to turfed memory and crash. To keep things simple we just store id as well so
  1522. // we can look it up to validate object ref.
  1523. smBreakTable[entry].objectId = pNode->object ? pNode->object->getId() : 0;
  1524. smBreakTable[entry].consoleFunction = pNode->consoleFunction;
  1525. if(pNode->breakConsoleCommand)
  1526. smBreakTable[entry].breakConsoleCommand = dStrdup(pNode->breakConsoleCommand);
  1527. else
  1528. smBreakTable[entry].breakConsoleCommand = NULL;
  1529. smBreakTable[entry].flags = pNode->flags;
  1530. smBreakTable[entry].deadZoneBegin = pNode->deadZoneBegin;
  1531. smBreakTable[entry].deadZoneEnd = pNode->deadZoneEnd;
  1532. smBreakTable[entry].scaleFactor = pNode->scaleFactor;
  1533. }
  1534. //------------------------------------------------------------------------------
  1535. bool ActionMap::checkBreakTable(const InputEvent* pEvent)
  1536. {
  1537. for (U32 i = 0; i < (U32)smBreakTable.size(); i++) {
  1538. if (smBreakTable[i].deviceType == U32(pEvent->deviceType) &&
  1539. smBreakTable[i].deviceInst == U32(pEvent->deviceInst) &&
  1540. smBreakTable[i].objInst == U32(pEvent->objInst)) {
  1541. // Match. Issue the break event...
  1542. //
  1543. F32 value = pEvent->fValues[0];
  1544. if (smBreakTable[i].flags & Node::Ranged) {
  1545. value = (value * 2.0f) - 1.0f;
  1546. if (smBreakTable[i].flags & Node::Inverted)
  1547. value *= -1.0f;
  1548. } else {
  1549. if (smBreakTable[i].flags & Node::Inverted)
  1550. value = 1.0f - value;
  1551. }
  1552. if (smBreakTable[i].flags & Node::HasScale)
  1553. value *= smBreakTable[i].scaleFactor;
  1554. if (smBreakTable[i].flags & Node::HasDeadZone)
  1555. if (value >= smBreakTable[i].deadZoneBegin &&
  1556. value <= smBreakTable[i].deadZoneEnd)
  1557. value = 0.0f;
  1558. // Ok, we're all set up, call the function.
  1559. if(smBreakTable[i].consoleFunction)
  1560. {
  1561. if ( smBreakTable[i].consoleFunction[0] )
  1562. {
  1563. static const char *argv[2];
  1564. argv[0] = smBreakTable[i].consoleFunction;
  1565. argv[1] = Con::getFloatArg(value);
  1566. if( smBreakTable[i].object )
  1567. {
  1568. // [neo, 5/7/2007 - #2975]
  1569. // object above can be deleted in between a make/break and so object will point
  1570. // to turfed memory and crash. To keep things simple we just store id as well so
  1571. // we can look it up to validate object ref.
  1572. if( smBreakTable[i].objectId > 0 && Sim::findObject( smBreakTable[i].objectId ) )
  1573. Con::executef(smBreakTable[i].object, 2, argv[0], argv[1]);
  1574. }
  1575. else
  1576. Con::execute(2, argv);
  1577. }
  1578. }
  1579. else if(smBreakTable[i].breakConsoleCommand)
  1580. {
  1581. Con::evaluate(smBreakTable[i].breakConsoleCommand);
  1582. dFree(smBreakTable[i].breakConsoleCommand);
  1583. }
  1584. smBreakTable.erase(i);
  1585. return true;
  1586. }
  1587. }
  1588. return false;
  1589. }
  1590. //------------------------------------------------------------------------------
  1591. bool ActionMap::handleEvent(const InputEvent* pEvent)
  1592. {
  1593. // Interate through the ActionMapSet until we get a map that
  1594. // handles the event or we run out of maps...
  1595. //
  1596. SimSet* pActionMapSet = Sim::getActiveActionMapSet();
  1597. AssertFatal(pActionMapSet && pActionMapSet->size() != 0,
  1598. "error, no ActiveMapSet or no global action map...");
  1599. for (SimSet::iterator itr = pActionMapSet->end() - 1;
  1600. itr > pActionMapSet->begin(); itr--) {
  1601. ActionMap* pMap = static_cast<ActionMap*>(*itr);
  1602. if (pMap->processAction(pEvent) == true)
  1603. return true;
  1604. }
  1605. return false;
  1606. }
  1607. //------------------------------------------------------------------------------
  1608. bool ActionMap::handleEventGlobal(const InputEvent* pEvent)
  1609. {
  1610. // Interate through the ActionMapSet until we get a map that
  1611. // handles the event or we run out of maps...
  1612. //
  1613. SimSet* pActionMapSet = Sim::getActiveActionMapSet();
  1614. AssertFatal(pActionMapSet && pActionMapSet->size() != 0,
  1615. "error, no ActiveMapSet or no global action map...");
  1616. return ((ActionMap*)pActionMapSet->first())->processAction(pEvent);
  1617. }
  1618. //------------------------------------------------------------------------------
  1619. //-------------------------------------- Key code to string mapping
  1620. // TODO: Add most obvious aliases...
  1621. //
  1622. CodeMapping gVirtualMap[] =
  1623. {
  1624. //-------------------------------------- KEYBOARD EVENTS
  1625. //
  1626. { "backspace", SI_KEY, KEY_BACKSPACE },
  1627. { "tab", SI_KEY, KEY_TAB },
  1628. { "return", SI_KEY, KEY_RETURN },
  1629. { "enter", SI_KEY, KEY_RETURN },
  1630. { "shift", SI_KEY, KEY_SHIFT },
  1631. { "ctrl", SI_KEY, KEY_CONTROL },
  1632. { "alt", SI_KEY, KEY_ALT },
  1633. { "pause", SI_KEY, KEY_PAUSE },
  1634. { "capslock", SI_KEY, KEY_CAPSLOCK },
  1635. { "escape", SI_KEY, KEY_ESCAPE },
  1636. { "space", SI_KEY, KEY_SPACE },
  1637. { "pagedown", SI_KEY, KEY_PAGE_DOWN },
  1638. { "pageup", SI_KEY, KEY_PAGE_UP },
  1639. { "end", SI_KEY, KEY_END },
  1640. { "home", SI_KEY, KEY_HOME },
  1641. { "left", SI_KEY, KEY_LEFT },
  1642. { "up", SI_KEY, KEY_UP },
  1643. { "right", SI_KEY, KEY_RIGHT },
  1644. { "down", SI_KEY, KEY_DOWN },
  1645. { "print", SI_KEY, KEY_PRINT },
  1646. { "insert", SI_KEY, KEY_INSERT },
  1647. { "delete", SI_KEY, KEY_DELETE },
  1648. { "help", SI_KEY, KEY_HELP },
  1649. { "win_lwindow", SI_KEY, KEY_WIN_LWINDOW },
  1650. { "win_rwindow", SI_KEY, KEY_WIN_RWINDOW },
  1651. { "win_apps", SI_KEY, KEY_WIN_APPS },
  1652. { "cmd", SI_KEY, KEY_ALT },
  1653. { "opt", SI_KEY, KEY_MAC_OPT },
  1654. { "lopt", SI_KEY, KEY_MAC_LOPT },
  1655. { "ropt", SI_KEY, KEY_MAC_ROPT },
  1656. { "numpad0", SI_KEY, KEY_NUMPAD0 },
  1657. { "numpad1", SI_KEY, KEY_NUMPAD1 },
  1658. { "numpad2", SI_KEY, KEY_NUMPAD2 },
  1659. { "numpad3", SI_KEY, KEY_NUMPAD3 },
  1660. { "numpad4", SI_KEY, KEY_NUMPAD4 },
  1661. { "numpad5", SI_KEY, KEY_NUMPAD5 },
  1662. { "numpad6", SI_KEY, KEY_NUMPAD6 },
  1663. { "numpad7", SI_KEY, KEY_NUMPAD7 },
  1664. { "numpad8", SI_KEY, KEY_NUMPAD8 },
  1665. { "numpad9", SI_KEY, KEY_NUMPAD9 },
  1666. { "numpadmult", SI_KEY, KEY_MULTIPLY },
  1667. { "numpadadd", SI_KEY, KEY_ADD },
  1668. { "numpadsep", SI_KEY, KEY_SEPARATOR },
  1669. { "numpadminus", SI_KEY, KEY_SUBTRACT },
  1670. { "numpaddecimal", SI_KEY, KEY_DECIMAL },
  1671. { "numpaddivide", SI_KEY, KEY_DIVIDE },
  1672. { "numpadenter", SI_KEY, KEY_NUMPADENTER },
  1673. { "f1", SI_KEY, KEY_F1 },
  1674. { "f2", SI_KEY, KEY_F2 },
  1675. { "f3", SI_KEY, KEY_F3 },
  1676. { "f4", SI_KEY, KEY_F4 },
  1677. { "f5", SI_KEY, KEY_F5 },
  1678. { "f6", SI_KEY, KEY_F6 },
  1679. { "f7", SI_KEY, KEY_F7 },
  1680. { "f8", SI_KEY, KEY_F8 },
  1681. { "f9", SI_KEY, KEY_F9 },
  1682. { "f10", SI_KEY, KEY_F10 },
  1683. { "f11", SI_KEY, KEY_F11 },
  1684. { "f12", SI_KEY, KEY_F12 },
  1685. { "f13", SI_KEY, KEY_F13 },
  1686. { "f14", SI_KEY, KEY_F14 },
  1687. { "f15", SI_KEY, KEY_F15 },
  1688. { "f16", SI_KEY, KEY_F16 },
  1689. { "f17", SI_KEY, KEY_F17 },
  1690. { "f18", SI_KEY, KEY_F18 },
  1691. { "f19", SI_KEY, KEY_F19 },
  1692. { "f20", SI_KEY, KEY_F20 },
  1693. { "f21", SI_KEY, KEY_F21 },
  1694. { "f22", SI_KEY, KEY_F22 },
  1695. { "f23", SI_KEY, KEY_F23 },
  1696. { "f24", SI_KEY, KEY_F24 },
  1697. { "numlock", SI_KEY, KEY_NUMLOCK },
  1698. { "scrolllock", SI_KEY, KEY_SCROLLLOCK },
  1699. { "lshift", SI_KEY, KEY_LSHIFT },
  1700. { "rshift", SI_KEY, KEY_RSHIFT },
  1701. { "lcontrol", SI_KEY, KEY_LCONTROL },
  1702. { "rcontrol", SI_KEY, KEY_RCONTROL },
  1703. { "lalt", SI_KEY, KEY_LALT },
  1704. { "ralt", SI_KEY, KEY_RALT },
  1705. { "tilde", SI_KEY, KEY_TILDE },
  1706. { "minus", SI_KEY, KEY_MINUS },
  1707. { "equals", SI_KEY, KEY_EQUALS },
  1708. { "lbracket", SI_KEY, KEY_LBRACKET },
  1709. { "rbracket", SI_KEY, KEY_RBRACKET },
  1710. { "backslash", SI_KEY, KEY_BACKSLASH },
  1711. { "semicolon", SI_KEY, KEY_SEMICOLON },
  1712. { "apostrophe", SI_KEY, KEY_APOSTROPHE },
  1713. { "comma", SI_KEY, KEY_COMMA },
  1714. { "period", SI_KEY, KEY_PERIOD },
  1715. { "slash", SI_KEY, KEY_SLASH },
  1716. { "lessthan", SI_KEY, KEY_OEM_102 },
  1717. //-------------------------------------- BUTTON EVENTS
  1718. // Joystick/Mouse buttons
  1719. { "button0", SI_BUTTON, KEY_BUTTON0 },
  1720. { "button1", SI_BUTTON, KEY_BUTTON1 },
  1721. { "button2", SI_BUTTON, KEY_BUTTON2 },
  1722. { "button3", SI_BUTTON, KEY_BUTTON3 },
  1723. { "button4", SI_BUTTON, KEY_BUTTON4 },
  1724. { "button5", SI_BUTTON, KEY_BUTTON5 },
  1725. { "button6", SI_BUTTON, KEY_BUTTON6 },
  1726. { "button7", SI_BUTTON, KEY_BUTTON7 },
  1727. { "button8", SI_BUTTON, KEY_BUTTON8 },
  1728. { "button9", SI_BUTTON, KEY_BUTTON9 },
  1729. { "button10", SI_BUTTON, KEY_BUTTON10 },
  1730. { "button11", SI_BUTTON, KEY_BUTTON11 },
  1731. { "button12", SI_BUTTON, KEY_BUTTON12 },
  1732. { "button13", SI_BUTTON, KEY_BUTTON13 },
  1733. { "button14", SI_BUTTON, KEY_BUTTON14 },
  1734. { "button15", SI_BUTTON, KEY_BUTTON15 },
  1735. { "button16", SI_BUTTON, KEY_BUTTON16 },
  1736. { "button17", SI_BUTTON, KEY_BUTTON17 },
  1737. { "button18", SI_BUTTON, KEY_BUTTON18 },
  1738. { "button19", SI_BUTTON, KEY_BUTTON19 },
  1739. { "button20", SI_BUTTON, KEY_BUTTON20 },
  1740. { "button21", SI_BUTTON, KEY_BUTTON21 },
  1741. { "button22", SI_BUTTON, KEY_BUTTON22 },
  1742. { "button23", SI_BUTTON, KEY_BUTTON23 },
  1743. { "button24", SI_BUTTON, KEY_BUTTON24 },
  1744. { "button25", SI_BUTTON, KEY_BUTTON25 },
  1745. { "button26", SI_BUTTON, KEY_BUTTON26 },
  1746. { "button27", SI_BUTTON, KEY_BUTTON27 },
  1747. { "button28", SI_BUTTON, KEY_BUTTON28 },
  1748. { "button29", SI_BUTTON, KEY_BUTTON29 },
  1749. { "button30", SI_BUTTON, KEY_BUTTON30 },
  1750. { "button31", SI_BUTTON, KEY_BUTTON31 },
  1751. //-------------------------------------- MOVE EVENTS
  1752. // Mouse/Joystick axes:
  1753. { "xaxis", SI_MOVE, SI_XAXIS },
  1754. { "yaxis", SI_MOVE, SI_YAXIS },
  1755. { "zaxis", SI_MOVE, SI_ZAXIS },
  1756. { "rxaxis", SI_MOVE, SI_RXAXIS },
  1757. { "ryaxis", SI_MOVE, SI_RYAXIS },
  1758. { "rzaxis", SI_MOVE, SI_RZAXIS },
  1759. { "slider", SI_MOVE, SI_SLIDER },
  1760. //-------------------------------------- POV EVENTS
  1761. // Joystick POV:
  1762. { "xpov", SI_POV, SI_XPOV },
  1763. { "ypov", SI_POV, SI_YPOV },
  1764. { "upov", SI_POV, SI_UPOV },
  1765. { "dpov", SI_POV, SI_DPOV },
  1766. { "lpov", SI_POV, SI_LPOV },
  1767. { "rpov", SI_POV, SI_RPOV },
  1768. { "xpov2", SI_POV, SI_XPOV2 },
  1769. { "ypov2", SI_POV, SI_YPOV2 },
  1770. { "upov2", SI_POV, SI_UPOV2 },
  1771. { "dpov2", SI_POV, SI_DPOV2 },
  1772. { "lpov2", SI_POV, SI_LPOV2 },
  1773. { "rpov2", SI_POV, SI_RPOV2 },
  1774. #if defined( TORQUE_OS_WIN32 ) || defined( TORQUE_OS_XENON )
  1775. //-------------------------------------- XINPUT EVENTS
  1776. // Controller connect / disconnect:
  1777. { "connect", XI_BUTTON, XI_CONNECT },
  1778. // L & R Thumbsticks:
  1779. { "thumblx", XI_AXIS, XI_THUMBLX },
  1780. { "thumbly", XI_AXIS, XI_THUMBLY },
  1781. { "thumbrx", XI_AXIS, XI_THUMBRX },
  1782. { "thumbry", XI_AXIS, XI_THUMBRY },
  1783. // L & R Triggers:
  1784. { "triggerl", XI_AXIS, XI_LEFT_TRIGGER },
  1785. { "triggerr", XI_AXIS, XI_RIGHT_TRIGGER },
  1786. // DPAD Buttons:
  1787. { "dpadu", XI_BUTTON, SI_UPOV },
  1788. { "dpadd", XI_BUTTON, SI_DPOV },
  1789. { "dpadl", XI_BUTTON, SI_LPOV },
  1790. { "dpadr", XI_BUTTON, SI_RPOV },
  1791. // START & BACK Buttons:
  1792. { "btn_start", XI_BUTTON, XI_START },
  1793. { "btn_back", XI_BUTTON, XI_BACK },
  1794. // L & R Thumbstick Buttons:
  1795. { "btn_lt", XI_BUTTON, XI_LEFT_THUMB },
  1796. { "btn_rt", XI_BUTTON, XI_RIGHT_THUMB },
  1797. // L & R Shoulder Buttons:
  1798. { "btn_l", XI_BUTTON, XI_LEFT_SHOULDER },
  1799. { "btn_r", XI_BUTTON, XI_RIGHT_SHOULDER },
  1800. // Primary buttons:
  1801. { "btn_a", XI_BUTTON, XI_A },
  1802. { "btn_b", XI_BUTTON, XI_B },
  1803. { "btn_x", XI_BUTTON, XI_X },
  1804. { "btn_y", XI_BUTTON, XI_Y },
  1805. #endif
  1806. //-------------------------------------- MOTION EVENTS
  1807. // Accelerometer/Gyroscope axes:
  1808. { "accelx", SI_MOTION, SI_ACCELX },
  1809. { "accely", SI_MOTION, SI_ACCELY },
  1810. { "accelz", SI_MOTION, SI_ACCELZ },
  1811. { "gravityx", SI_MOTION, SI_GRAVX },
  1812. { "gravityy", SI_MOTION, SI_GRAVY },
  1813. { "gravityz", SI_MOTION, SI_GRAVZ },
  1814. { "gyrox", SI_MOTION, SI_GYROX },
  1815. { "gyroy", SI_MOTION, SI_GYROY },
  1816. { "gyroz", SI_MOTION, SI_GYROZ },
  1817. { "yaw", SI_MOTION, SI_YAW },
  1818. { "pitch", SI_MOTION, SI_PITCH },
  1819. { "roll", SI_MOTION, SI_ROLL },
  1820. //-------------------------------------- TOUCH EVENTS
  1821. // Touch events:
  1822. { "touchdown", SI_TOUCH, SI_TOUCHDOWN },
  1823. { "touchup", SI_TOUCH, SI_TOUCHUP },
  1824. { "touchmove", SI_TOUCH, SI_TOUCHMOVE },
  1825. //-------------------------------------- GESTURE EVENTS
  1826. // Preset gesture events:
  1827. { "circleGesture", SI_GESTURE, SI_CIRCLE_GESTURE },
  1828. { "swipeGesture", SI_GESTURE, SI_SWIPE_GESTURE },
  1829. { "screenTapGesture", SI_GESTURE, SI_SCREENTAP_GESTURE },
  1830. { "keyTapGesture", SI_GESTURE, SI_KEYTAP_GESTURE },
  1831. { "pinchGesture", SI_GESTURE, SI_PINCH_GESTURE },
  1832. { "scaleGesture", SI_GESTURE, SI_SCALE_GESTURE },
  1833. //-------------------------------------- MISCELLANEOUS EVENTS
  1834. //
  1835. { "anykey", SI_KEY, KEY_ANYKEY },
  1836. { "nomatch", SI_UNKNOWN, 0xFFFFFFFF }
  1837. };
  1838. AsciiMapping gAsciiMap[] =
  1839. {
  1840. //--- KEYBOARD EVENTS
  1841. //
  1842. { "space", 0x0020 },
  1843. //{ "exclamation", 0x0021 },
  1844. { "doublequote", 0x0022 },
  1845. //{ "pound", 0x0023 },
  1846. //{ "ampersand", 0x0026 },
  1847. { "apostrophe", 0x0027 },
  1848. //{ "lparen", 0x0028 },
  1849. //{ "rparen", 0x0029 },
  1850. { "comma", 0x002c },
  1851. { "minus", 0x002d },
  1852. { "period", 0x002e },
  1853. //{ "slash", 0x002f },
  1854. //{ "colon", 0x003a },
  1855. //{ "semicolon", 0x003b },
  1856. //{ "lessthan", 0x003c },
  1857. //{ "equals", 0x003d },
  1858. //{ "morethan", 0x003e },
  1859. //{ "lbracket", 0x005b },
  1860. { "backslash", 0x005c },
  1861. //{ "rbracket", 0x005d },
  1862. //{ "circumflex", 0x005e },
  1863. //{ "underscore", 0x005f },
  1864. { "grave", 0x0060 },
  1865. //{ "tilde", 0x007e },
  1866. //{ "vertbar", 0x007c },
  1867. //{ "exclamdown", 0x00a1 },
  1868. //{ "cent", 0x00a2 },
  1869. //{ "sterling", 0x00a3 },
  1870. //{ "currency", 0x00a4 },
  1871. //{ "brokenbar", 0x00a6 },
  1872. //{ "ring", 0x00b0 },
  1873. //{ "plusminus", 0x00b1 },
  1874. { "super2", 0x00b2 },
  1875. { "super3", 0x00b3 },
  1876. { "acute", 0x00b4 },
  1877. //{ "mu", 0x00b5 },
  1878. //{ "ordmasculine", 0x00ba },
  1879. //{ "questiondown", 0x00bf },
  1880. //{ "gemandbls", 0x00df },
  1881. //{ "agrave", 0x00e0 },
  1882. //{ "aacute", 0x00e1 },
  1883. //{ "acircumflex", 0x00e2 },
  1884. //{ "atilde", 0x00e3 },
  1885. //{ "adieresis", 0x00e4 },
  1886. //{ "aring", 0x00e5 },
  1887. //{ "ae", 0x00e6 },
  1888. //{ "ccedille", 0x00e7 },
  1889. //{ "egrave", 0x00e8 },
  1890. //{ "eacute", 0x00e9 },
  1891. //{ "ecircumflex", 0x00ea },
  1892. //{ "edieresis", 0x00eb },
  1893. //{ "igrave", 0x00ec },
  1894. //{ "iacute", 0x00ed },
  1895. //{ "icircumflex", 0x00ee },
  1896. //{ "idieresis", 0x00ef },
  1897. //{ "ntilde", 0x00f1 },
  1898. //{ "ograve", 0x00f2 },
  1899. //{ "oacute", 0x00f3 },
  1900. //{ "ocircumflex", 0x00f4 },
  1901. //{ "otilde", 0x00f5 },
  1902. //{ "odieresis", 0x00f6 },
  1903. //{ "divide", 0x00f7 },
  1904. //{ "oslash", 0x00f8 },
  1905. //{ "ugrave", 0x00f9 },
  1906. //{ "uacute", 0x00fa },
  1907. //{ "ucircumflex", 0x00fb },
  1908. //{ "udieresis", 0x00fc },
  1909. //{ "ygrave", 0x00fd },
  1910. //{ "thorn", 0x00fe },
  1911. //{ "ydieresis", 0x00ff },
  1912. { "nomatch", 0xFFFF }
  1913. };
  1914. ////Device Event Types
  1915. //#define SI_UNKNOWN 0x01
  1916. //#define SI_BUTTON 0x02
  1917. //#define SI_POV 0x03
  1918. //#define SI_XPOV 0x04
  1919. //#define SI_YPOV 0x05
  1920. //#define SI_UPOV 0x06
  1921. //#define SI_DPOV 0x07
  1922. //#define SI_LPOV 0x08
  1923. //#define SI_RPOV 0x09
  1924. //#define SI_KEY 0x0A
  1925. //#define SI_XAXIS 0x0B
  1926. //#define SI_YAXIS 0x0C
  1927. //#define SI_ZAXIS 0x0D
  1928. //#define SI_RXAXIS 0x0E
  1929. //#define SI_RYAXIS 0x0F
  1930. //#define SI_RZAXIS 0x10
  1931. //#define SI_SLIDER 0x11