actionMap.cc 75 KB

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