trekinput.cpp 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Forward declaration
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. TRef<IPopup> CreateInputMapPopup(
  8. Modeler* pmodeler,
  9. InputEngine* pinputEngine,
  10. TrekInput* ptrekInput,
  11. IEngineFont* pfont,
  12. Number* ptime
  13. );
  14. //////////////////////////////////////////////////////////////////////////////
  15. //
  16. // Trek Input
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. const int countAxis = 6;
  20. const int keyMax = 256;
  21. const int ModifierNone = 0;
  22. const int ModifierShift = 1;
  23. const int ModifierControl = 2;
  24. const int ModifierAlt = 4;
  25. const int ModifierAny = 8;
  26. class TrekInputImpl : public TrekInput {
  27. public:
  28. //
  29. // Input Engine
  30. //
  31. TRef<InputEngine> m_pinputEngine;
  32. TRef<JoystickImage> m_pjoystickImage;
  33. TArray<TRef<Number>, countAxis> m_ppnumberAxis;
  34. //
  35. // Joystick mapping
  36. //
  37. TArray<TRef<Boolean>, TK_Max> m_ppboolTrekKeyButtonDown;
  38. TArray<bool , TK_Max> m_boolTrekKeyButtonDown;
  39. TArray<TRef<ModifiableBoolean>, 4> m_ppboolHatButton;
  40. //
  41. // Keyboard mapping
  42. //
  43. bool m_bFocus;
  44. TArray<TrekKey, keyMax> m_KeyMap;
  45. TArray<TrekKey, keyMax> m_ShiftKeyMap;
  46. TArray<TrekKey, keyMax> m_ControlKeyMap;
  47. TArray<TrekKey, keyMax> m_AltKeyMap;
  48. TArray<TrekKey, keyMax> m_ControlShiftKeyMap;
  49. TArray<TrekKey, keyMax> m_ShiftAltKeyMap;
  50. TArray<TrekKey, keyMax> m_ControlAltKeyMap;
  51. TArray<TrekKey, keyMax> m_ControlShiftAltKeyMap;
  52. TArray<TRef<ModifiableBoolean>, keyMax> m_pboolKeyDown;
  53. TArray<TRef<Boolean>, TK_Max> m_pboolTrekKeyDown;
  54. TRef<Boolean> m_pboolNone;
  55. TRef<Boolean> m_pboolJustShift;
  56. TRef<Boolean> m_pboolJustControl;
  57. TRef<Boolean> m_pboolJustAlt;
  58. TRef<Boolean> m_pboolJustControlShift;
  59. TRef<Boolean> m_pboolJustShiftAlt;
  60. TRef<Boolean> m_pboolJustControlAlt;
  61. TRef<Boolean> m_pboolJustControlShiftAlt;
  62. //////////////////////////////////////////////////////////////////////////////
  63. //
  64. // Constructor
  65. //
  66. //////////////////////////////////////////////////////////////////////////////
  67. TrekInputImpl(
  68. HINSTANCE hInst,
  69. HWND hWnd,
  70. InputEngine* pinputEngine,
  71. JoystickImage* pjoystickImage
  72. ) :
  73. m_pinputEngine(pinputEngine),
  74. m_pjoystickImage(pjoystickImage)
  75. {
  76. InitializeCommandsMDL();
  77. //
  78. // Create the virtual hat buttons
  79. //
  80. for (int index = 0; index < 4; index++) {
  81. m_ppboolHatButton[index] = new ModifiableBoolean(false);
  82. }
  83. //
  84. // All the button states start up
  85. //
  86. ClearButtonStates();
  87. //
  88. // Initialize input mapping
  89. //
  90. if (!LoadMap(INPUTMAP_FILE)) {
  91. LoadMap(DEFAULTINPUTMAP_FILE);
  92. }
  93. Flush();
  94. }
  95. //////////////////////////////////////////////////////////////////////////////
  96. //
  97. // Methods
  98. //
  99. //////////////////////////////////////////////////////////////////////////////
  100. void Flush(void)
  101. {
  102. //
  103. // Do things this way because SetKeyboardState() doesn't appear
  104. // to work on Win'95...
  105. //
  106. for (int x = 0; x < 256; x++) {
  107. GetKeyState(x);
  108. }
  109. }
  110. int normalize (int val, int minval, int maxval);
  111. void InitializeCommandsMDL()
  112. {
  113. //
  114. // Trek Key definitions
  115. //
  116. TRef<INameSpace> pnsCommands =
  117. GetModeler()->CreateNameSpace(
  118. "commands",
  119. GetModeler()->GetNameSpace("model")
  120. );
  121. pnsCommands->AddMember("CommandHelp" , new Number((float)TK_Help ));
  122. pnsCommands->AddMember("CommandNextCommMsg" , new Number((float)TK_NextCommMsg ));
  123. pnsCommands->AddMember("CommandPrevCommMsg" , new Number((float)TK_PrevCommMsg ));
  124. pnsCommands->AddMember("CommandPrevWeapon" , new Number((float)TK_PrevWeapon ));
  125. pnsCommands->AddMember("CommandNextWeapon" , new Number((float)TK_NextWeapon ));
  126. pnsCommands->AddMember("CommandToggleGroupFire" , new Number((float)TK_ToggleGroupFire ));
  127. pnsCommands->AddMember("CommandPrevPart" , new Number((float)TK_PrevPart ));
  128. pnsCommands->AddMember("CommandNextPart" , new Number((float)TK_NextPart ));
  129. pnsCommands->AddMember("CommandMountPart" , new Number((float)TK_MountPart ));
  130. pnsCommands->AddMember("CommandReplacePart" , new Number((float)TK_ReplacePart ));
  131. pnsCommands->AddMember("CommandUnmountPart" , new Number((float)TK_UnmountPart ));
  132. pnsCommands->AddMember("CommandDropPart" , new Number((float)TK_DropPart ));
  133. pnsCommands->AddMember("CommandZoomOut" , new Number((float)TK_ZoomOut ));
  134. pnsCommands->AddMember("CommandZoomIn" , new Number((float)TK_ZoomIn ));
  135. pnsCommands->AddMember("CommandPitchUp" , new Number((float)TK_PitchUp ));
  136. pnsCommands->AddMember("CommandPitchDown" , new Number((float)TK_PitchDown ));
  137. pnsCommands->AddMember("CommandYawLeft" , new Number((float)TK_YawLeft ));
  138. pnsCommands->AddMember("CommandYawRight" , new Number((float)TK_YawRight ));
  139. pnsCommands->AddMember("CommandRollModifier" , new Number((float)TK_RollModifier ));
  140. pnsCommands->AddMember("CommandRollLeft" , new Number((float)TK_RollLeft ));
  141. pnsCommands->AddMember("CommandRollRight" , new Number((float)TK_RollRight ));
  142. pnsCommands->AddMember("CommandMainMenu" , new Number((float)TK_MainMenu ));
  143. pnsCommands->AddMember("CommandViewFrontLeft" , new Number((float)TK_ViewFrontLeft ));
  144. pnsCommands->AddMember("CommandViewFrontRight" , new Number((float)TK_ViewFrontRight ));
  145. pnsCommands->AddMember("CommandViewLeft" , new Number((float)TK_ViewLeft ));
  146. pnsCommands->AddMember("CommandViewRight" , new Number((float)TK_ViewRight ));
  147. pnsCommands->AddMember("CommandViewExternal" , new Number((float)TK_ViewExternal ));
  148. pnsCommands->AddMember("CommandViewFlyby" , new Number((float)TK_ViewFlyby ));
  149. pnsCommands->AddMember("CommandViewMissile" , new Number((float)TK_ViewMissile ));
  150. pnsCommands->AddMember("CommandViewTarget" , new Number((float)TK_ViewTarget ));
  151. pnsCommands->AddMember("CommandViewChase" , new Number((float)TK_ViewChase ));
  152. // pnsCommands->AddMember("CommandViewTurret" , new Number((float)TK_ViewTurret ));
  153. pnsCommands->AddMember("CommandViewBase" , new Number((float)TK_ViewBase ));
  154. pnsCommands->AddMember("CommandViewSector" , new Number((float)TK_ViewSector ));
  155. pnsCommands->AddMember("CommandViewCommand" , new Number((float)TK_ViewCommand ));
  156. pnsCommands->AddMember("CommandCycleChatMsgs" , new Number((float)TK_CycleChatMsgs ));
  157. pnsCommands->AddMember("CommandExecuteCommand" , new Number((float)TK_ExecuteCommand ));
  158. pnsCommands->AddMember("CommandAcceptCommand" , new Number((float)TK_AcceptCommand ));
  159. pnsCommands->AddMember("CommandRejectCommand" , new Number((float)TK_RejectCommand ));
  160. pnsCommands->AddMember("CommandClearCommand" , new Number((float)TK_ClearCommand ));
  161. pnsCommands->AddMember("CommandToggleAutoPilot" , new Number((float)TK_ToggleAutoPilot ));
  162. pnsCommands->AddMember("CommandThrustLeft" , new Number((float)TK_ThrustLeft ));
  163. pnsCommands->AddMember("CommandThrustRight" , new Number((float)TK_ThrustRight ));
  164. pnsCommands->AddMember("CommandThrustUp" , new Number((float)TK_ThrustUp ));
  165. pnsCommands->AddMember("CommandThrustDown" , new Number((float)TK_ThrustDown ));
  166. pnsCommands->AddMember("CommandThrustForward" , new Number((float)TK_ThrustForward ));
  167. pnsCommands->AddMember("CommandThrustBackward" , new Number((float)TK_ThrustBackward ));
  168. pnsCommands->AddMember("CommandFireBooster" , new Number((float)TK_FireBooster ));
  169. pnsCommands->AddMember("CommandFireWeapon" , new Number((float)TK_FireWeapon ));
  170. pnsCommands->AddMember("CommandFireMissile" , new Number((float)TK_FireMissile ));
  171. pnsCommands->AddMember("CommandLockVector" , new Number((float)TK_LockVector ));
  172. pnsCommands->AddMember("CommandThrottleUp" , new Number((float)TK_ThrottleUp ));
  173. pnsCommands->AddMember("CommandThrottleDown" , new Number((float)TK_ThrottleDown ));
  174. pnsCommands->AddMember("CommandThrottleZero" , new Number((float)TK_ThrottleZero ));
  175. pnsCommands->AddMember("CommandThrottle33" , new Number((float)TK_Throttle33 ));
  176. pnsCommands->AddMember("CommandThrottle66" , new Number((float)TK_Throttle66 ));
  177. pnsCommands->AddMember("CommandThrottleFull" , new Number((float)TK_ThrottleFull ));
  178. pnsCommands->AddMember("CommandToggleBooster" , new Number((float)TK_ToggleBooster ));
  179. pnsCommands->AddMember("CommandLODUp" , new Number((float)TK_LODUp ));
  180. pnsCommands->AddMember("CommandLODDown" , new Number((float)TK_LODDown ));
  181. pnsCommands->AddMember("CommandConModeCombat" , new Number((float)TK_ConModeCombat ));
  182. pnsCommands->AddMember("CommandConModeCommand" , new Number((float)TK_ConModeCommand ));
  183. pnsCommands->AddMember("CommandConModeNav" , new Number((float)TK_ConModeNav ));
  184. pnsCommands->AddMember("CommandConModeInventory" , new Number((float)TK_ConModeInventory ));
  185. pnsCommands->AddMember("CommandConModeInvest" , new Number((float)TK_ConModeInvest ));
  186. pnsCommands->AddMember("CommandConModeTeam" , new Number((float)TK_ConModeTeam ));
  187. pnsCommands->AddMember("CommandConModeGameState" , new Number((float)TK_ConModeGameState ));
  188. pnsCommands->AddMember("CommandViewRear" , new Number((float)TK_ViewRear ));
  189. pnsCommands->AddMember("CommandTarget" , new Number((float)TK_Target ));
  190. pnsCommands->AddMember("CommandTargetNearest" , new Number((float)TK_TargetNearest ));
  191. pnsCommands->AddMember("CommandTargetPrev" , new Number((float)TK_TargetPrev ));
  192. pnsCommands->AddMember("CommandTargetFriendly" , new Number((float)TK_TargetFriendly ));
  193. pnsCommands->AddMember("CommandTargetFriendlyNearest" , new Number((float)TK_TargetFriendlyNearest ));
  194. pnsCommands->AddMember("CommandTargetFriendlyPrev" , new Number((float)TK_TargetFriendlyPrev ));
  195. pnsCommands->AddMember("CommandTargetEnemy" , new Number((float)TK_TargetEnemy ));
  196. pnsCommands->AddMember("CommandTargetEnemyNearest" , new Number((float)TK_TargetEnemyNearest ));
  197. pnsCommands->AddMember("CommandTargetEnemyPrev" , new Number((float)TK_TargetEnemyPrev ));
  198. pnsCommands->AddMember("CommandTargetEnemyBase" , new Number((float)TK_TargetEnemyBase ));
  199. pnsCommands->AddMember("CommandTargetEnemyBaseNearest" , new Number((float)TK_TargetEnemyBaseNearest ));
  200. pnsCommands->AddMember("CommandTargetEnemyBasePrev" , new Number((float)TK_TargetEnemyBasePrev ));
  201. pnsCommands->AddMember("CommandTargetFriendlyBase" , new Number((float)TK_TargetFriendlyBase ));
  202. pnsCommands->AddMember("CommandTargetFriendlyBaseNearest", new Number((float)TK_TargetFriendlyBaseNearest));
  203. pnsCommands->AddMember("CommandTargetFriendlyBasePrev" , new Number((float)TK_TargetFriendlyBasePrev ));
  204. pnsCommands->AddMember("CommandTargetCommand" , new Number((float)TK_TargetCommand ));
  205. pnsCommands->AddMember("CommandTargetCenter" , new Number((float)TK_TargetCenter ));
  206. pnsCommands->AddMember("CommandTargetHostile" , new Number((float)TK_TargetHostile ));
  207. pnsCommands->AddMember("CommandSuicide" , new Number((float)TK_Suicide ));
  208. pnsCommands->AddMember("CommandToggleGrid" , new Number((float)TK_ToggleGrid ));
  209. pnsCommands->AddMember("CommandToggleCloak" , new Number((float)TK_ToggleCloak ));
  210. pnsCommands->AddMember("CommandDropMine" , new Number((float)TK_DropMine ));
  211. pnsCommands->AddMember("CommandRipcord" , new Number((float)TK_Ripcord ));
  212. pnsCommands->AddMember("CommandViewRearLeft" , new Number((float)TK_ViewRearLeft ));
  213. pnsCommands->AddMember("CommandViewRearRight" , new Number((float)TK_ViewRearRight ));
  214. pnsCommands->AddMember("CommandTargetSelf" , new Number((float)TK_TargetSelf ));
  215. pnsCommands->AddMember("CommandToggleCollisions" , new Number((float)TK_ToggleCollisions ));
  216. pnsCommands->AddMember("CommandOccupyNextTurret" , new Number((float)TK_OccupyNextTurret ));
  217. pnsCommands->AddMember("CommandTargetNothing" , new Number((float)TK_TargetNothing ));
  218. pnsCommands->AddMember("CommandMatchSpeed" , new Number((float)TK_MatchSpeed ));
  219. pnsCommands->AddMember("CommandChatPageUp" , new Number((float)TK_ChatPageUp ));
  220. pnsCommands->AddMember("CommandChatPageDown" , new Number((float)TK_ChatPageDown ));
  221. pnsCommands->AddMember("CommandViewExternalOrbit" , new Number((float)TK_ViewExternalOrbit ));
  222. pnsCommands->AddMember("CommandViewExternalStation" , new Number((float)TK_ViewExternalStation ));
  223. pnsCommands->AddMember("CommandDropChaff" , new Number((float)TK_DropChaff ));
  224. pnsCommands->AddMember("CommandToggleCommand" , new Number((float)TK_ToggleCommand ));
  225. pnsCommands->AddMember("CommandSwapWeapon1" , new Number((float)TK_SwapWeapon1 ));
  226. pnsCommands->AddMember("CommandSwapWeapon2" , new Number((float)TK_SwapWeapon2 ));
  227. pnsCommands->AddMember("CommandSwapWeapon3" , new Number((float)TK_SwapWeapon3 ));
  228. pnsCommands->AddMember("CommandSwapWeapon4" , new Number((float)TK_SwapWeapon4 ));
  229. pnsCommands->AddMember("CommandSwapTurret1" , new Number((float)TK_SwapTurret1 ));
  230. pnsCommands->AddMember("CommandSwapTurret2" , new Number((float)TK_SwapTurret2 ));
  231. pnsCommands->AddMember("CommandSwapTurret3" , new Number((float)TK_SwapTurret3 ));
  232. pnsCommands->AddMember("CommandSwapTurret4" , new Number((float)TK_SwapTurret4 ));
  233. pnsCommands->AddMember("CommandPromoteTurret1" , new Number((float)TK_PromoteTurret1 ));
  234. pnsCommands->AddMember("CommandPromoteTurret2" , new Number((float)TK_PromoteTurret2 ));
  235. pnsCommands->AddMember("CommandPromoteTurret3" , new Number((float)TK_PromoteTurret3 ));
  236. pnsCommands->AddMember("CommandPromoteTurret4" , new Number((float)TK_PromoteTurret4 ));
  237. pnsCommands->AddMember("CommandSwapMissile" , new Number((float)TK_SwapMissile ));
  238. pnsCommands->AddMember("CommandSwapChaff" , new Number((float)TK_SwapChaff ));
  239. pnsCommands->AddMember("CommandSwapShield" , new Number((float)TK_SwapShield ));
  240. pnsCommands->AddMember("CommandSwapCloak" , new Number((float)TK_SwapCloak ));
  241. pnsCommands->AddMember("CommandSwapAfterburner" , new Number((float)TK_SwapAfterburner ));
  242. pnsCommands->AddMember("CommandSwapMine" , new Number((float)TK_SwapMine ));
  243. pnsCommands->AddMember("CommandNextCargo" , new Number((float)TK_NextCargo ));
  244. pnsCommands->AddMember("CommandToggleWeapon1" , new Number((float)TK_ToggleWeapon1 ));
  245. pnsCommands->AddMember("CommandToggleWeapon2" , new Number((float)TK_ToggleWeapon2 ));
  246. pnsCommands->AddMember("CommandToggleWeapon3" , new Number((float)TK_ToggleWeapon3 ));
  247. pnsCommands->AddMember("CommandToggleWeapon4" , new Number((float)TK_ToggleWeapon4 ));
  248. pnsCommands->AddMember("CommandDropCargo" , new Number((float)TK_DropCargo ));
  249. pnsCommands->AddMember("CommandQuickChatMenu" , new Number((float)TK_QuickChatMenu ));
  250. pnsCommands->AddMember("CommandToggleLODSlider" , new Number((float)TK_ToggleLODSlider ));
  251. pnsCommands->AddMember("CommandStartChat" , new Number((float)TK_StartChat ));
  252. pnsCommands->AddMember("CommandReload" , new Number((float)TK_Reload ));
  253. pnsCommands->AddMember("CommandCycleRadar" , new Number((float)TK_CycleRadar ));
  254. pnsCommands->AddMember("CommandConModeMiniTeam" , new Number((float)TK_ConModeMiniTeam ));
  255. pnsCommands->AddMember("CommandConModeTeleport" , new Number((float)TK_ConModeTeleport ));
  256. pnsCommands->AddMember("CommandTargetEnemyBomber" , new Number((float)TK_TargetEnemyBomber ));
  257. pnsCommands->AddMember("CommandTargetEnemyFighter" , new Number((float)TK_TargetEnemyFighter ));
  258. pnsCommands->AddMember("CommandTargetEnemyConstructor" , new Number((float)TK_TargetEnemyConstructor ));
  259. pnsCommands->AddMember("CommandTargetEnemyMiner" , new Number((float)TK_TargetEnemyMiner ));
  260. pnsCommands->AddMember("CommandTargetEnemyMajorBase" , new Number((float)TK_TargetEnemyMajorBase ));
  261. pnsCommands->AddMember("CommandTargetFriendlyBomber" , new Number((float)TK_TargetFriendlyBomber ));
  262. pnsCommands->AddMember("CommandTargetFriendlyFighter" , new Number((float)TK_TargetFriendlyFighter ));
  263. pnsCommands->AddMember("CommandTargetFriendlyConstructor", new Number((float)TK_TargetFriendlyConstructor));
  264. pnsCommands->AddMember("CommandTargetFriendlyMiner" , new Number((float)TK_TargetFriendlyMiner ));
  265. pnsCommands->AddMember("CommandTargetFriendlyLifepod" , new Number((float)TK_TargetFriendlyLifepod ));
  266. pnsCommands->AddMember("CommandTargetFriendlyMajorBase" , new Number((float)TK_TargetFriendlyMajorBase ));
  267. pnsCommands->AddMember("CommandTargetFlag" , new Number((float)TK_TargetFlag ));
  268. pnsCommands->AddMember("CommandTargetArtifact" , new Number((float)TK_TargetArtifact ));
  269. pnsCommands->AddMember("CommandTargetEnemyBomberNearest" , new Number((float)TK_TargetEnemyBomberNearest ));
  270. pnsCommands->AddMember("CommandTargetEnemyFighterNearest" , new Number((float)TK_TargetEnemyFighterNearest ));
  271. pnsCommands->AddMember("CommandTargetEnemyConstructorNearest" , new Number((float)TK_TargetEnemyConstructorNearest ));
  272. pnsCommands->AddMember("CommandTargetEnemyMinerNearest" , new Number((float)TK_TargetEnemyMinerNearest ));
  273. pnsCommands->AddMember("CommandTargetEnemyMajorBaseNearest" , new Number((float)TK_TargetEnemyMajorBaseNearest ));
  274. pnsCommands->AddMember("CommandTargetFriendlyBomberNearest" , new Number((float)TK_TargetFriendlyBomberNearest ));
  275. pnsCommands->AddMember("CommandTargetFriendlyFighterNearest" , new Number((float)TK_TargetFriendlyFighterNearest ));
  276. pnsCommands->AddMember("CommandTargetFriendlyConstructorNearest", new Number((float)TK_TargetFriendlyConstructorNearest));
  277. pnsCommands->AddMember("CommandTargetFriendlyMinerNearest" , new Number((float)TK_TargetFriendlyMinerNearest ));
  278. pnsCommands->AddMember("CommandTargetFriendlyLifepodNearest" , new Number((float)TK_TargetFriendlyLifepodNearest ));
  279. pnsCommands->AddMember("CommandTargetFriendlyMajorBaseNearest" , new Number((float)TK_TargetFriendlyMajorBaseNearest ));
  280. pnsCommands->AddMember("CommandTargetFlagNearest" , new Number((float)TK_TargetFlagNearest ));
  281. pnsCommands->AddMember("CommandTargetArtifactNearest" , new Number((float)TK_TargetArtifactNearest ));
  282. pnsCommands->AddMember("CommandTargetEnemyBomberPrev" , new Number((float)TK_TargetEnemyBomberPrev ));
  283. pnsCommands->AddMember("CommandTargetEnemyFighterPrev" , new Number((float)TK_TargetEnemyFighterPrev ));
  284. pnsCommands->AddMember("CommandTargetEnemyConstructorPrev" , new Number((float)TK_TargetEnemyConstructorPrev ));
  285. pnsCommands->AddMember("CommandTargetEnemyMinerPrev" , new Number((float)TK_TargetEnemyMinerPrev ));
  286. pnsCommands->AddMember("CommandTargetEnemyMajorBasePrev" , new Number((float)TK_TargetEnemyMajorBasePrev ));
  287. pnsCommands->AddMember("CommandTargetFriendlyBomberPrev" , new Number((float)TK_TargetFriendlyBomberPrev ));
  288. pnsCommands->AddMember("CommandTargetFriendlyFighterPrev" , new Number((float)TK_TargetFriendlyFighterPrev ));
  289. pnsCommands->AddMember("CommandTargetFriendlyConstructorPrev", new Number((float)TK_TargetFriendlyConstructorPrev));
  290. pnsCommands->AddMember("CommandTargetFriendlyMinerPrev" , new Number((float)TK_TargetFriendlyMinerPrev ));
  291. pnsCommands->AddMember("CommandTargetFriendlyLifepodPrev" , new Number((float)TK_TargetFriendlyLifepodPrev ));
  292. pnsCommands->AddMember("CommandTargetFriendlyMajorBasePrev" , new Number((float)TK_TargetFriendlyMajorBasePrev ));
  293. pnsCommands->AddMember("CommandTargetFlagPrev" , new Number((float)TK_TargetFlagPrev ));
  294. pnsCommands->AddMember("CommandTargetArtifactPrev" , new Number((float)TK_TargetArtifactPrev ));
  295. pnsCommands->AddMember("CommandTargetWarp" , new Number((float)TK_TargetWarp ));
  296. pnsCommands->AddMember("CommandTargetWarpNearest" , new Number((float)TK_TargetWarpNearest ));
  297. pnsCommands->AddMember("CommandTargetWarpPrev" , new Number((float)TK_TargetWarpPrev ));
  298. pnsCommands->AddMember("CommandTrackCommandView" , new Number((float)TK_TrackCommandView ));
  299. pnsCommands->AddMember("CommandPauseTM" , new Number((float)TK_PauseTM ));
  300. pnsCommands->AddMember("CommandFinishTM" , new Number((float)TK_CommandFinishTM ));
  301. pnsCommands->AddMember("CommandToggleMouse" , new Number((float)TK_ToggleMouse ));
  302. pnsCommands->AddMember("CommandVoteYes" , new Number((float)TK_VoteYes ));
  303. pnsCommands->AddMember("CommandVoteNo" , new Number((float)TK_VoteNo ));
  304. // debug only
  305. //#ifdef _DEBUG_TRAINING
  306. pnsCommands->AddMember("CommandSkipTMGoal" , new Number((float)TK_SkipTMGoal ));
  307. pnsCommands->AddMember("CommandIncreaseTMClockSpeed" , new Number((float)TK_IncreaseTMClockSpeed ));
  308. pnsCommands->AddMember("CommandDecreaseTMClockSpeed" , new Number((float)TK_DecreaseTMClockSpeed ));
  309. pnsCommands->AddMember("CommandResetTMClockSpeed" , new Number((float)TK_ResetTMClockSpeed ));
  310. //#endif
  311. pnsCommands->AddMember("None" , new Number((float)ModifierNone ));
  312. pnsCommands->AddMember("Shift" , new Number((float)ModifierShift ));
  313. pnsCommands->AddMember("Control", new Number((float)ModifierControl));
  314. pnsCommands->AddMember("Alt" , new Number((float)ModifierAlt ));
  315. pnsCommands->AddMember("Any" , new Number((float)ModifierAny ));
  316. TRef<Boolean> pboolShift = GetKeyDown(VK_SHIFT );
  317. TRef<Boolean> pboolControl = GetKeyDown(VK_CONTROL);
  318. TRef<Boolean> pboolAlt = GetKeyDown(VK_MENU );
  319. TRef<Boolean> pboolNotShift = Not(pboolShift );
  320. TRef<Boolean> pboolNotControl = Not(pboolControl);
  321. TRef<Boolean> pboolNotAlt = Not(pboolAlt );
  322. TRef<Boolean> pboolNotControlShift = And(pboolNotControl, pboolNotShift);
  323. TRef<Boolean> pboolNotShiftAlt = And(pboolNotShift, pboolNotAlt );
  324. TRef<Boolean> pboolNotControlAlt = And(pboolNotControl, pboolNotAlt );
  325. TRef<Boolean> pboolControlShift = And(pboolControl, pboolShift);
  326. TRef<Boolean> pboolShiftAlt = And(pboolShift, pboolAlt );
  327. TRef<Boolean> pboolControlAlt = And(pboolControl, pboolAlt );
  328. m_pboolNone = And(pboolNotControlShift, pboolNotAlt );
  329. m_pboolJustShift = And(pboolShift, pboolNotControlAlt );
  330. m_pboolJustControl = And(pboolControl, pboolNotShiftAlt );
  331. m_pboolJustAlt = And(pboolAlt, pboolNotControlShift);
  332. m_pboolJustControlShift = And(pboolControlShift, pboolNotAlt );
  333. m_pboolJustShiftAlt = And(pboolShiftAlt, pboolNotControl );
  334. m_pboolJustControlAlt = And(pboolControlAlt, pboolNotShift );
  335. m_pboolJustControlShiftAlt = And(pboolControlShift, pboolAlt );
  336. }
  337. Boolean* GetKeyDown(int vk)
  338. {
  339. if (m_pboolKeyDown[vk] == NULL) {
  340. m_pboolKeyDown[vk] = new ModifiableBoolean(false);
  341. }
  342. return m_pboolKeyDown[vk];
  343. }
  344. ZString GetFPSString()
  345. {
  346. return
  347. ZString(m_pboolNone->GetValue() ? "N " : " " )
  348. + ZString(m_pboolJustControl->GetValue() ? "C " : " " )
  349. + ZString(m_pboolJustShift->GetValue() ? "S " : " " )
  350. + ZString(m_pboolJustAlt->GetValue() ? "A " : " " )
  351. + ZString(m_pboolJustControlShift->GetValue() ? "CS " : " " )
  352. + ZString(m_pboolJustShiftAlt->GetValue() ? "SA " : " " )
  353. + ZString(m_pboolJustControlAlt->GetValue() ? "CA " : " " )
  354. + ZString(m_pboolJustControlShiftAlt->GetValue() ? "CSA " : " ")
  355. ;
  356. }
  357. bool LoadMap(const ZString& str)
  358. {
  359. //
  360. // load the input map
  361. //
  362. TRef<INameSpace> pns = GetModeler()->GetNameSpace(str, false);
  363. if (pns == NULL) {
  364. return false;
  365. }
  366. //
  367. // Redetect the joysticks
  368. //
  369. m_pinputEngine->EnumerateJoysticks();
  370. //
  371. // Initialize Joystick
  372. //
  373. if (
  374. m_pinputEngine->GetJoystick(0)
  375. && m_pinputEngine->GetJoystick(0)->HasForceFeedback()
  376. ) {
  377. m_pinputEngine->GetJoystick(0)->CreateEffects();
  378. }
  379. //
  380. // Clear out the old input map
  381. //
  382. int index;
  383. for (index = 0; index < keyMax; index++) {
  384. m_KeyMap[index] = -1;
  385. m_ShiftKeyMap[index] = -1;
  386. m_ControlKeyMap[index] = -1;
  387. m_AltKeyMap[index] = -1;
  388. m_ControlShiftKeyMap[index] = -1;
  389. m_ShiftAltKeyMap[index] = -1;
  390. m_ControlAltKeyMap[index] = -1;
  391. m_ControlShiftAltKeyMap[index] = -1;
  392. }
  393. for (index = 0; index < TK_Max; index++) {
  394. m_pboolTrekKeyDown[index] = NULL;
  395. m_ppboolTrekKeyButtonDown[index] = NULL;
  396. }
  397. for (index = 0; index < m_ppnumberAxis.GetCount(); index++) {
  398. m_ppnumberAxis[index] = NULL;
  399. }
  400. //
  401. // keyboard mapping
  402. //
  403. {
  404. TRef<IObjectList> plist = pns->FindList("keyCommandMap");
  405. plist->GetFirst();
  406. while (plist->GetCurrent() != NULL) {
  407. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  408. int vk = (int)GetNumber(ppair->GetNth(0) );
  409. int modifiers = (int)GetNumber(ppair->GetNth(1) );
  410. int tk = (int)GetNumber(ppair->GetLastNth(2));
  411. TArray<TrekKey, keyMax>* pptk;
  412. if (modifiers & ModifierShift) {
  413. if (modifiers & ModifierControl) {
  414. if (modifiers & ModifierAlt) {
  415. pptk = &m_ControlShiftAltKeyMap;
  416. } else {
  417. pptk = &m_ControlShiftKeyMap;
  418. }
  419. } else {
  420. if (modifiers & ModifierAlt) {
  421. pptk = &m_ShiftAltKeyMap;
  422. } else {
  423. pptk = &m_ShiftKeyMap;
  424. }
  425. }
  426. } else {
  427. if (modifiers & ModifierControl) {
  428. if (modifiers & ModifierAlt) {
  429. pptk = &m_ControlAltKeyMap;
  430. } else {
  431. pptk = &m_ControlKeyMap;
  432. }
  433. } else {
  434. if (modifiers & ModifierAlt) {
  435. pptk = &m_AltKeyMap;
  436. } else {
  437. pptk = &m_KeyMap;
  438. }
  439. }
  440. }
  441. ZAssert((*pptk)[vk] == -1);
  442. (*pptk)[vk] = tk;
  443. //
  444. // Boolean inputs, the key
  445. //
  446. TRef<Boolean> pbool = GetKeyDown(vk);
  447. //
  448. // And with modifiers
  449. //
  450. if (modifiers != ModifierAny) {
  451. TRef<Boolean> pboolModifier;
  452. if (modifiers & ModifierShift) {
  453. if (modifiers & ModifierControl) {
  454. if (modifiers & ModifierAlt) {
  455. pboolModifier = m_pboolJustControlShiftAlt;
  456. } else {
  457. pboolModifier = m_pboolJustControlShift;
  458. }
  459. } else {
  460. if (modifiers & ModifierAlt) {
  461. pboolModifier = m_pboolJustShiftAlt;
  462. } else {
  463. pboolModifier = m_pboolJustShift;
  464. }
  465. }
  466. } else {
  467. if (modifiers & ModifierControl) {
  468. if (modifiers & ModifierAlt) {
  469. pboolModifier = m_pboolJustControlAlt;
  470. } else {
  471. pboolModifier = m_pboolJustControl;
  472. }
  473. } else {
  474. if (modifiers & ModifierAlt) {
  475. pboolModifier = m_pboolJustAlt;
  476. } else {
  477. pboolModifier = m_pboolNone;
  478. }
  479. }
  480. }
  481. pbool = And(pbool, pboolModifier);
  482. }
  483. //
  484. // Or all of the key combinations together
  485. //
  486. if (m_pboolTrekKeyDown[tk] == NULL) {
  487. m_pboolTrekKeyDown[tk] = pbool;
  488. } else {
  489. m_pboolTrekKeyDown[tk] =
  490. Or(
  491. pbool,
  492. m_pboolTrekKeyDown[tk]
  493. );
  494. }
  495. //
  496. // go to next mapping
  497. //
  498. plist->GetNext();
  499. }
  500. }
  501. //
  502. // mouse button mapping
  503. //
  504. {
  505. TRef<IObjectList> plist = pns->FindList("mouseButtonCommandMap");
  506. if (plist) {
  507. plist->GetFirst();
  508. while (plist->GetCurrent() != NULL) {
  509. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  510. int index = (int)GetNumber(ppair->GetFirst() );
  511. int tk = (int)GetNumber(ppair->GetSecond());
  512. //
  513. // Get the button
  514. //
  515. TRef<Boolean> pboolButton = m_pjoystickImage->IsDown(index);
  516. if (pboolButton) {
  517. //
  518. // Or all of the key combinations together
  519. //
  520. if (m_ppboolTrekKeyButtonDown[tk] == NULL) {
  521. m_ppboolTrekKeyButtonDown[tk] = pboolButton;
  522. } else {
  523. m_ppboolTrekKeyButtonDown[tk] =
  524. Or(
  525. pboolButton,
  526. m_ppboolTrekKeyButtonDown[tk]
  527. );
  528. }
  529. }
  530. plist->GetNext();
  531. }
  532. }
  533. }
  534. //
  535. // joystick button mapping
  536. //
  537. {
  538. TRef<IObjectList> plist = pns->FindList("buttonCommandMap");
  539. plist->GetFirst();
  540. while (plist->GetCurrent() != NULL) {
  541. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  542. int indexJoystick = (int)GetNumber(ppair->GetNth(0) );
  543. int indexButton = (int)GetNumber(ppair->GetNth(1) );
  544. int tk = (int)GetNumber(ppair->GetLastNth(2));
  545. //
  546. // Get the button
  547. //
  548. TRef<JoystickInputStream> pjoystick = m_pinputEngine->GetJoystick(indexJoystick);
  549. TRef<Boolean> pboolButton;
  550. if (indexButton < 0) {
  551. //
  552. // It's a virtual hat button
  553. //
  554. pboolButton = m_ppboolHatButton[-indexButton - 1];
  555. } else {
  556. //
  557. // A joystick button
  558. //
  559. if (pjoystick) {
  560. pboolButton = pjoystick->IsDown(indexButton);
  561. }
  562. }
  563. if (pboolButton) {
  564. //
  565. // Or all of the key combinations together
  566. //
  567. if (m_ppboolTrekKeyButtonDown[tk] == NULL) {
  568. m_ppboolTrekKeyButtonDown[tk] = pboolButton;
  569. } else {
  570. m_ppboolTrekKeyButtonDown[tk] =
  571. Or(
  572. pboolButton,
  573. m_ppboolTrekKeyButtonDown[tk]
  574. );
  575. }
  576. }
  577. plist->GetNext();
  578. }
  579. }
  580. //
  581. // joystick value mapping
  582. //
  583. {
  584. TRef<IObjectList> plist = pns->FindList("numericValuesNew");
  585. bool bNew = (plist != NULL);
  586. if (!bNew) {
  587. plist = pns->FindList("numericValues");
  588. }
  589. plist->GetFirst();
  590. for (int indexValueOutput = 0; indexValueOutput < m_ppnumberAxis.GetCount(); indexValueOutput++) {
  591. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  592. if (ppair) {
  593. int indexJoystick = (int)GetNumber(ppair->GetFirst() );
  594. int indexValueInput = (int)GetNumber(ppair->GetSecond());
  595. if (!bNew) {
  596. if (indexValueInput == 2) {
  597. indexValueInput = 3;
  598. } else if (indexValueInput == 3) {
  599. indexValueInput = 2;
  600. }
  601. }
  602. TRef<JoystickInputStream> pjoystick = m_pinputEngine->GetJoystick(indexJoystick);
  603. TRef<Number> pvalueInput;
  604. if (pjoystick) {
  605. pvalueInput = pjoystick->GetValue(indexValueInput);
  606. }
  607. if (pvalueInput) {
  608. m_ppnumberAxis[indexValueOutput] = pvalueInput;
  609. } else {
  610. m_ppnumberAxis[indexValueOutput] = NULL;
  611. }
  612. }
  613. plist->GetNext();
  614. }
  615. }
  616. //
  617. // Unload the map
  618. //
  619. GetModeler()->UnloadNameSpace(pns);
  620. return true;
  621. }
  622. void ClearButtonStates()
  623. {
  624. for (int index = 0; index < TK_Max; index++) {
  625. m_boolTrekKeyButtonDown[index] = false;
  626. }
  627. }
  628. //////////////////////////////////////////////////////////////////////////////
  629. //
  630. // TrekInput Methods
  631. //
  632. //////////////////////////////////////////////////////////////////////////////
  633. bool IsTrekKeyDown(TrekKey tk, bool bCheckKeyboard)
  634. {
  635. bool bDown = false;
  636. //
  637. // check joystick buttons
  638. //
  639. Boolean* pbool = m_ppboolTrekKeyButtonDown[tk];
  640. if (pbool) {
  641. bDown = pbool->GetValue();
  642. }
  643. //
  644. // check keys
  645. //
  646. if (bCheckKeyboard) {
  647. Boolean* pbool = m_pboolTrekKeyDown[tk];
  648. if (pbool) {
  649. bDown = bDown || pbool->GetValue();
  650. }
  651. }
  652. return bDown;
  653. }
  654. void SetFocus(bool bFocus)
  655. {
  656. if (m_bFocus != bFocus) {
  657. m_bFocus = bFocus;
  658. if (!m_bFocus) {
  659. for (int index = 0; index < keyMax; index++) {
  660. if (m_pboolKeyDown[index] != NULL) {
  661. m_pboolKeyDown[index]->SetValue(false);
  662. }
  663. }
  664. ClearButtonStates();
  665. }
  666. }
  667. }
  668. TrekKey TranslateKeyMessage(const KeyState& ks)
  669. {
  670. //
  671. // Translate to a trek key
  672. //
  673. if (ks.bShift) {
  674. if (ks.bControl) {
  675. if (ks.bAlt) {
  676. return m_ControlShiftAltKeyMap[ks.vk];
  677. } else {
  678. return m_ControlShiftKeyMap[ks.vk];
  679. }
  680. } else {
  681. if (ks.bAlt) {
  682. return m_ShiftAltKeyMap[ks.vk];
  683. } else {
  684. return m_ShiftKeyMap[ks.vk];
  685. }
  686. }
  687. } else {
  688. if (ks.bControl) {
  689. if (ks.bAlt) {
  690. return m_ControlAltKeyMap[ks.vk];
  691. } else {
  692. return m_ControlKeyMap[ks.vk];
  693. }
  694. } else {
  695. if (ks.bAlt) {
  696. return m_AltKeyMap[ks.vk];
  697. } else {
  698. return m_KeyMap[ks.vk];
  699. }
  700. }
  701. }
  702. return TK_NoKeyMapping;
  703. }
  704. TrekKey HandleKeyMessage(const KeyState& ks)
  705. {
  706. /*
  707. ZDebugOutput(
  708. "Key: "
  709. + ZString(ks.vk)
  710. + " Down: "
  711. + (ks.bDown ? "True " : "False ")
  712. + (ks.bShift ? "S" : "" )
  713. + (ks.bControl ? "C" : "" )
  714. + (ks.bAlt ? "A" : "" )
  715. + "\n"
  716. );
  717. */
  718. if (m_bFocus) {
  719. //
  720. // Update boolean value
  721. //
  722. if (m_pboolKeyDown[ks.vk] != NULL) {
  723. m_pboolKeyDown[ks.vk]->SetValue(ks.bDown);
  724. }
  725. //
  726. // Translate to a trek key
  727. //
  728. return TranslateKeyMessage(ks);
  729. }
  730. return TK_NoKeyMapping;
  731. }
  732. void PlayFFEffect(ForceEffectID effectID, LONG lDirection)
  733. {
  734. if (
  735. m_pinputEngine->GetJoystick(0)
  736. && m_pinputEngine->GetJoystick(0)->HasForceFeedback()
  737. ) {
  738. m_pinputEngine->GetJoystick(0)->PlayFFEffect(effectID, lDirection);
  739. }
  740. }
  741. float GetAxis(int index)
  742. {
  743. if (m_ppnumberAxis[index] != NULL) {
  744. return m_ppnumberAxis[index]->GetValue();
  745. } else {
  746. return 0;
  747. }
  748. }
  749. bool IsAxisValid(int index)
  750. {
  751. return m_ppnumberAxis[index] != NULL;
  752. }
  753. void GetButtonTrekKeys(TrekInputSite* psite)
  754. {
  755. //
  756. // Update hat buttons
  757. //
  758. if (m_ppnumberAxis[5] != NULL) {
  759. float value = m_ppnumberAxis[5]->GetValue();
  760. //ZDebugOutput("hat: " + ZString(value) + "\n");
  761. // 0 up = 0
  762. // 1 right = 0.5
  763. // 2 down = 1
  764. // 3 left = -0.5
  765. // center = -2.0
  766. for (int index = 0; index < 4; index++) {
  767. m_ppboolHatButton[index]->SetValue(0);
  768. }
  769. if (value > 0.75f) {
  770. m_ppboolHatButton[2]->SetValue(1);
  771. } else if (value > 0.25f) {
  772. m_ppboolHatButton[1]->SetValue(1);
  773. } else if (value > -0.25f) {
  774. m_ppboolHatButton[0]->SetValue(1);
  775. } else if (value > -0.75f) {
  776. m_ppboolHatButton[3]->SetValue(1);
  777. } else if (value > -1.25f) {
  778. m_ppboolHatButton[2]->SetValue(1);
  779. }
  780. }
  781. //
  782. // If any of the button states changes send a trek key
  783. //
  784. for (int index = 0; index < TK_Max; index++) {
  785. if (
  786. m_ppboolTrekKeyButtonDown[index] != NULL
  787. && m_ppboolTrekKeyButtonDown[index]->GetValue() != m_boolTrekKeyButtonDown[index]
  788. ) {
  789. m_boolTrekKeyButtonDown[index] = m_ppboolTrekKeyButtonDown[index]->GetValue();
  790. if (m_boolTrekKeyButtonDown[index]) {
  791. psite->OnTrekKey(index);
  792. }
  793. }
  794. }
  795. }
  796. TRef<IPopup> CreateInputMapPopup(Modeler* pmodeler, IEngineFont* pfont, Number* ptime)
  797. {
  798. return
  799. ::CreateInputMapPopup(
  800. pmodeler,
  801. m_pinputEngine,
  802. this,
  803. pfont,
  804. ptime
  805. );
  806. }
  807. };
  808. //////////////////////////////////////////////////////////////////////////////
  809. //
  810. //
  811. //
  812. //////////////////////////////////////////////////////////////////////////////
  813. TRef<TrekInput> CreateTrekInput(HINSTANCE hInst, HWND hwnd, InputEngine* pinputEngine, JoystickImage* pjoystickImage)
  814. {
  815. return new TrekInputImpl(hInst, hwnd, pinputEngine, pjoystickImage);
  816. }
  817. //////////////////////////////////////////////////////////////////////////////
  818. //
  819. //
  820. //
  821. //////////////////////////////////////////////////////////////////////////////
  822. PCC g_ppccAxisDescription[] =
  823. {
  824. "Yaw",
  825. "Pitch",
  826. "Roll",
  827. "Throttle",
  828. "Hat",
  829. "Hat as Buttons"
  830. };
  831. class KeyInfo {
  832. public:
  833. PCC m_pcc;
  834. };
  835. KeyInfo g_pkeyInfo[] =
  836. {
  837. { NULL }, // 00
  838. { NULL }, // 01
  839. { NULL }, // 02
  840. { NULL }, // 03
  841. { NULL }, // 04
  842. { NULL }, // 05
  843. { NULL }, // 06
  844. { NULL }, // 07
  845. { "Backspace" }, // 08
  846. { "Tab" }, // 09
  847. { NULL }, // 0a
  848. { NULL }, // 0b
  849. { NULL }, // 0c
  850. { "Enter" }, // 0d
  851. { NULL }, // 0e
  852. { NULL }, // 0f
  853. { NULL }, // 10
  854. { NULL }, // 11
  855. { NULL }, // 12
  856. { "Pause" }, // 13
  857. { "Caps Lock" }, // 14
  858. { NULL }, // 15
  859. { NULL }, // 16
  860. { NULL }, // 17
  861. { NULL }, // 18
  862. { NULL }, // 19
  863. { NULL }, // 1a
  864. { "Escape" }, // 1b
  865. { NULL }, // 1c
  866. { NULL }, // 1d
  867. { NULL }, // 1e
  868. { NULL }, // 1f
  869. { "Space" }, // 20
  870. { "Page Up" }, // 21
  871. { "Page Down" }, // 22
  872. { "End" }, // 23
  873. { "Home" }, // 24
  874. { "Left" }, // 25
  875. { "Up" }, // 26
  876. { "Right" }, // 27
  877. { "Down" }, // 28
  878. { NULL }, // 29
  879. { NULL }, // 2a
  880. { NULL }, // 2b
  881. { NULL }, // 2c
  882. { "Insert" }, // 2d
  883. { "Delete" }, // 2e
  884. { NULL }, // 2f
  885. { "0" }, // 30
  886. { "1" }, // 31
  887. { "2" }, // 32
  888. { "3" }, // 33
  889. { "4" }, // 34
  890. { "5" }, // 35
  891. { "6" }, // 36
  892. { "7" }, // 37
  893. { "8" }, // 38
  894. { "9" }, // 39
  895. { NULL }, // 3a
  896. { NULL }, // 3b
  897. { NULL }, // 3c
  898. { NULL }, // 3d
  899. { NULL }, // 3e
  900. { NULL }, // 3f
  901. { NULL }, // 40
  902. { "A" }, // 41
  903. { "B" }, // 42
  904. { "C" }, // 43
  905. { "D" }, // 44
  906. { "E" }, // 45
  907. { "F" }, // 46
  908. { "G" }, // 47
  909. { "H" }, // 48
  910. { "I" }, // 49
  911. { "J" }, // 4a
  912. { "K" }, // 4b
  913. { "L" }, // 4c
  914. { "M" }, // 4d
  915. { "N" }, // 4e
  916. { "O" }, // 4f
  917. { "P" }, // 50
  918. { "Q" }, // 51
  919. { "R" }, // 52
  920. { "S" }, // 53
  921. { "T" }, // 54
  922. { "U" }, // 55
  923. { "V" }, // 56
  924. { "W" }, // 57
  925. { "X" }, // 58
  926. { "Y" }, // 59
  927. { "Z" }, // 5a
  928. { NULL }, // 5b
  929. { NULL }, // 5c
  930. { NULL }, // 5d
  931. { NULL }, // 5e
  932. { NULL }, // 5f
  933. { "Num Pad 0" }, // 60
  934. { "Num Pad 1" }, // 61
  935. { "Num Pad 2" }, // 62
  936. { "Num Pad 3" }, // 63
  937. { "Num Pad 4" }, // 64
  938. { "Num Pad 5" }, // 65
  939. { "Num Pad 6" }, // 66
  940. { "Num Pad 7" }, // 67
  941. { "Num Pad 8" }, // 68
  942. { "Num Pad 9" }, // 69
  943. { "Num Pad Multiply" }, // 6a
  944. { "Num Pad Plus" }, // 6b
  945. { "Num Pad Enter" }, // 6c
  946. { "Num Pad Minus" }, // 6d
  947. { "Num Pad Decimal" }, // 6e
  948. { "Num Pad Divide" }, // 6f
  949. { "F1" }, // 70
  950. { "F2" }, // 71
  951. { "F3" }, // 72
  952. { "F4" }, // 73
  953. { "F5" }, // 74
  954. { "F6" }, // 75
  955. { "F7" }, // 76
  956. { "F8" }, // 77
  957. { "F9" }, // 78
  958. { "F10" }, // 79
  959. { "F11" }, // 7a
  960. { "F12" }, // 7b
  961. { NULL }, // 7c
  962. { NULL }, // 7d
  963. { NULL }, // 7e
  964. { NULL }, // 7f
  965. { NULL }, // 80
  966. { NULL }, // 81
  967. { NULL }, // 82
  968. { NULL }, // 83
  969. { NULL }, // 84
  970. { NULL }, // 85
  971. { NULL }, // 86
  972. { NULL }, // 87
  973. { NULL }, // 88
  974. { NULL }, // 89
  975. { NULL }, // 8a
  976. { NULL }, // 8b
  977. { NULL }, // 8c
  978. { NULL }, // 8d
  979. { NULL }, // 8e
  980. { NULL }, // 8f
  981. { "Num Lock" }, // 90
  982. { "Scroll Lock" }, // 91
  983. { NULL }, // 92
  984. { NULL }, // 93
  985. { NULL }, // 94
  986. { NULL }, // 95
  987. { NULL }, // 96
  988. { NULL }, // 97
  989. { NULL }, // 98
  990. { NULL }, // 99
  991. { NULL }, // 9a
  992. { NULL }, // 9b
  993. { NULL }, // 9c
  994. { NULL }, // 9d
  995. { NULL }, // 9e
  996. { NULL }, // 9f
  997. { NULL }, // a0
  998. { NULL }, // a1
  999. { NULL }, // a2
  1000. { NULL }, // a3
  1001. { NULL }, // a4
  1002. { NULL }, // a5
  1003. { NULL }, // a6
  1004. { NULL }, // a7
  1005. { NULL }, // a8
  1006. { NULL }, // a9
  1007. { NULL }, // aa
  1008. { NULL }, // ab
  1009. { NULL }, // ac
  1010. { NULL }, // ad
  1011. { NULL }, // ae
  1012. { NULL }, // af
  1013. { NULL }, // b0
  1014. { NULL }, // b1
  1015. { NULL }, // b2
  1016. { NULL }, // b3
  1017. { NULL }, // b4
  1018. { NULL }, // b5
  1019. { NULL }, // b6
  1020. { NULL }, // b7
  1021. { NULL }, // b8
  1022. { NULL }, // b9
  1023. { "SemiColon" }, // ba
  1024. { "Equals" }, // bb
  1025. { "Comma" }, // bc
  1026. { "Minus" }, // bd
  1027. { "Period" }, // be
  1028. { "Forward Slash" }, // bf
  1029. { "Tilde" }, // c0
  1030. { NULL }, // c1
  1031. { NULL }, // c2
  1032. { NULL }, // c3
  1033. { NULL }, // c4
  1034. { NULL }, // c5
  1035. { NULL }, // c6
  1036. { NULL }, // c7
  1037. { NULL }, // c8
  1038. { NULL }, // c9
  1039. { NULL }, // ca
  1040. { NULL }, // cb
  1041. { NULL }, // cc
  1042. { NULL }, // cd
  1043. { NULL }, // ce
  1044. { NULL }, // cf
  1045. { NULL }, // d0
  1046. { NULL }, // d1
  1047. { NULL }, // d2
  1048. { NULL }, // d3
  1049. { NULL }, // d4
  1050. { NULL }, // d5
  1051. { NULL }, // d6
  1052. { NULL }, // d7
  1053. { NULL }, // d8
  1054. { NULL }, // d9
  1055. { NULL }, // da
  1056. { "Left Square" }, // db
  1057. { "Back Slash" }, // dc
  1058. { "Right Square" }, // dd
  1059. { "Quote" }, // de
  1060. { NULL }, // df
  1061. { NULL }, // e0
  1062. { NULL }, // e1
  1063. { NULL }, // e2
  1064. { NULL }, // e3
  1065. { NULL }, // e4
  1066. { NULL }, // e5
  1067. { NULL }, // e6
  1068. { NULL }, // e7
  1069. { NULL }, // e8
  1070. { NULL }, // e9
  1071. { NULL }, // ea
  1072. { NULL }, // eb
  1073. { NULL }, // ec
  1074. { NULL }, // ed
  1075. { NULL }, // ee
  1076. { NULL }, // ef
  1077. { NULL }, // f0
  1078. { NULL }, // f1
  1079. { NULL }, // f2
  1080. { NULL }, // f3
  1081. { NULL }, // f4
  1082. { NULL }, // f5
  1083. { NULL }, // f6
  1084. { NULL }, // f7
  1085. { NULL }, // f8
  1086. { NULL }, // f9
  1087. { NULL }, // fa
  1088. { NULL }, // fb
  1089. { NULL }, // fc
  1090. { NULL }, // fd
  1091. { NULL }, // fe
  1092. { NULL } // ff
  1093. };
  1094. class CommandInfo {
  1095. public:
  1096. int m_tk;
  1097. PCC m_pcc;
  1098. };
  1099. CommandInfo g_pCommandInfo[] =
  1100. {
  1101. { TK_NoKeyMapping , "" },
  1102. { TK_NoKeyMapping , "Chat" },
  1103. { TK_NoKeyMapping , "" },
  1104. { TK_QuickChatMenu , "Quick Chat Menu" },
  1105. { TK_StartChat , "Start Chat" },
  1106. { TK_ChatPageUp , "Chat Page Up" },
  1107. { TK_ChatPageDown , "Chat Page Down" },
  1108. { TK_NoKeyMapping , "" },
  1109. { TK_NoKeyMapping , "Commands" },
  1110. { TK_NoKeyMapping , "" },
  1111. { TK_AcceptCommand , "Accept Command" },
  1112. { TK_ClearCommand , "Clear Command" },
  1113. { TK_RejectCommand , "Reject Command" },
  1114. { TK_ExecuteCommand , "Execute Command" },
  1115. { TK_VoteYes , "Vote Yes" },
  1116. { TK_VoteNo , "Vote No" },
  1117. { TK_NoKeyMapping , "" },
  1118. { TK_NoKeyMapping , "Weapons" },
  1119. { TK_NoKeyMapping , "" },
  1120. { TK_FireWeapon , "Fire Weapon" },
  1121. { TK_FireMissile , "Fire Missile" },
  1122. { TK_PrevWeapon , "Previous Weapon" },
  1123. { TK_NextWeapon , "Next Weapon" },
  1124. { TK_ToggleGroupFire , "Toggle Group Fire" },
  1125. { TK_Reload , "Reload" },
  1126. { TK_DropMine , "Drop Mine" },
  1127. { TK_DropChaff , "Drop Chaff" },
  1128. { TK_NoKeyMapping , "" },
  1129. { TK_NoKeyMapping , "Turning" },
  1130. { TK_NoKeyMapping , "" },
  1131. { TK_PitchUp , "Pitch Up" },
  1132. { TK_PitchDown , "Pitch Down" },
  1133. { TK_YawLeft , "Yaw Left" },
  1134. { TK_YawRight , "Yaw Right" },
  1135. { TK_RollLeft , "Roll Left" },
  1136. { TK_RollRight , "Roll Right" },
  1137. { TK_RollModifier , "Roll Modifier" },
  1138. { TK_ToggleMouse , "Toggle Mouse Control" },
  1139. { TK_NoKeyMapping , "" },
  1140. { TK_NoKeyMapping , "Thrust" },
  1141. { TK_NoKeyMapping , "" },
  1142. { TK_ThrustLeft , "Thrust Left" },
  1143. { TK_ThrustRight , "Thrust Right" },
  1144. { TK_ThrustUp , "Thrust Up" },
  1145. { TK_ThrustDown , "Thrust Down" },
  1146. { TK_ThrustForward , "Thrust Forward" },
  1147. { TK_ThrustBackward , "Thrust Backward" },
  1148. { TK_FireBooster , "Fire Booster" },
  1149. { TK_ThrottleUp , "Throttle Up" },
  1150. { TK_ThrottleDown , "Throttle Down" },
  1151. { TK_ThrottleZero , "Throttle Zero" },
  1152. { TK_Throttle33 , "Throttle 33%" },
  1153. { TK_Throttle66 , "Throttle 66%" },
  1154. { TK_ThrottleFull , "Throttle Full" },
  1155. { TK_LockVector , "Lock Vector" },
  1156. { TK_ToggleAutoPilot , "Toggle AutoPilot" },
  1157. { TK_MatchSpeed , "Match Speed" },
  1158. { TK_NoKeyMapping , "" },
  1159. { TK_NoKeyMapping , "Camera" },
  1160. { TK_NoKeyMapping , "" },
  1161. { TK_ZoomOut , "Zoom Out" },
  1162. { TK_ZoomIn , "Zoom In" },
  1163. { TK_ViewFrontLeft , "View Front Left" },
  1164. { TK_ViewFrontRight , "View Front Right" },
  1165. { TK_ViewLeft , "View Left" },
  1166. { TK_ViewRight , "View Right" },
  1167. { TK_ViewExternal , "View External" },
  1168. { TK_ViewFlyby , "View Flyby" },
  1169. { TK_ViewMissile , "View Missile" },
  1170. { TK_ViewTarget , "View Target" },
  1171. { TK_ViewChase , "View Chase" },
  1172. // { TK_ViewTurret , "View Turret" },
  1173. { TK_ViewBase , "View Base" },
  1174. { TK_ViewSector , "View Sector" },
  1175. { TK_ViewCommand , "View Command" },
  1176. { TK_ViewRear , "View Rear" },
  1177. { TK_ViewRearLeft , "View Rear Left" },
  1178. { TK_ViewRearRight , "View Rear Right" },
  1179. { TK_ViewExternalOrbit , "View External Orbit" },
  1180. { TK_ViewExternalStation , "View External Target" },
  1181. { TK_NoKeyMapping , "" },
  1182. { TK_NoKeyMapping , "UI Toggles" },
  1183. { TK_NoKeyMapping , "" },
  1184. { TK_ConModeGameState , "Switch To Game State" },
  1185. { TK_ConModeCombat , "Switch To Combat" },
  1186. { TK_ConModeCommand , "Switch To Command" },
  1187. { TK_ConModeInventory , "Switch To Inventory" },
  1188. { TK_ConModeInvest , "Switch To Invest" },
  1189. { TK_ConModeTeam , "Switch To Team" },
  1190. { TK_ConModeNav , "Switch To Nav" },
  1191. { TK_ConModeMiniTeam , "Switch To Mini Team" },
  1192. { TK_ConModeTeleport , "Switch To Teleport" },
  1193. { TK_ToggleGrid , "Toggle Grid" },
  1194. { TK_CycleRadar , "Cycle Radar Modes" },
  1195. { TK_NoKeyMapping , "" },
  1196. { TK_NoKeyMapping , "Inventory" },
  1197. { TK_NoKeyMapping , "" },
  1198. { TK_SwapWeapon1 , "Swap Weapon1" },
  1199. { TK_SwapWeapon2 , "Swap Weapon2" },
  1200. { TK_SwapWeapon3 , "Swap Weapon3" },
  1201. { TK_SwapWeapon4 , "Swap Weapon4" },
  1202. { TK_SwapTurret1 , "Swap Turret1" },
  1203. { TK_SwapTurret2 , "Swap Turret2" },
  1204. { TK_SwapTurret3 , "Swap Turret3" },
  1205. { TK_SwapTurret4 , "Swap Turret4" },
  1206. { TK_PromoteTurret1 , "Promote Turret1" },
  1207. { TK_PromoteTurret2 , "Promote Turret2" },
  1208. { TK_PromoteTurret3 , "Promote Turret3" },
  1209. { TK_PromoteTurret4 , "Promote Turret4" },
  1210. { TK_SwapMissile , "Swap Missile" },
  1211. { TK_SwapChaff , "Swap Chaff" },
  1212. { TK_SwapShield , "Swap Shield" },
  1213. { TK_SwapCloak , "Swap Cloak" },
  1214. { TK_SwapAfterburner , "Swap Afterburner" },
  1215. { TK_SwapMine , "Swap Mine" },
  1216. { TK_NextCargo , "Next Cargo" },
  1217. { TK_ToggleWeapon1 , "Toggle Weapon1" },
  1218. { TK_ToggleWeapon2 , "Toggle Weapon2" },
  1219. { TK_ToggleWeapon3 , "Toggle Weapon3" },
  1220. { TK_ToggleWeapon4 , "Toggle Weapon4" },
  1221. { TK_DropCargo , "Drop Cargo" },
  1222. { TK_NoKeyMapping , "" },
  1223. { TK_NoKeyMapping , "Targeting" },
  1224. { TK_NoKeyMapping , "" },
  1225. { TK_Target , "Target" },
  1226. { TK_TargetNothing , "Target Nothing" },
  1227. { TK_TargetSelf , "Target Self" },
  1228. { TK_TargetNearest , "Target Nearest" },
  1229. { TK_TargetPrev , "Target Previous" },
  1230. { TK_TargetCommand , "Target Command" },
  1231. { TK_TargetCenter , "Target Center" },
  1232. { TK_NoKeyMapping , "" },
  1233. { TK_TargetFlag , "Target Flag" },
  1234. { TK_TargetFlagNearest , "Target Nearest flag" },
  1235. { TK_TargetFlagPrev , "Target Previous flag" },
  1236. { TK_NoKeyMapping , "" },
  1237. { TK_TargetArtifact , "Target Artifact" },
  1238. { TK_TargetArtifactNearest , "Target Nearest artifact" },
  1239. { TK_TargetArtifactPrev , "Target Previous artifact" },
  1240. { TK_NoKeyMapping , "" },
  1241. { TK_TargetWarp , "Target Aleph" },
  1242. { TK_TargetWarpNearest , "Target Nearest Aleph" },
  1243. { TK_TargetWarpPrev , "Target Previous Alpeh" },
  1244. { TK_NoKeyMapping , "" },
  1245. { TK_TargetHostile , "Target Hostile" },
  1246. { TK_TargetEnemy , "Target Enemy" },
  1247. { TK_TargetEnemyNearest , "Target Enemy Nearest" },
  1248. { TK_TargetEnemyPrev , "Target Enemy Previous" },
  1249. { TK_TargetEnemyBase , "Target Enemy Base" },
  1250. { TK_TargetEnemyBaseNearest , "Target Enemy Base Nearest" },
  1251. { TK_TargetEnemyBasePrev , "Target Enemy Base Previous" },
  1252. { TK_TargetEnemyBomber , "Target Enemy Bomber" },
  1253. { TK_TargetEnemyFighter , "Target Enemy Fighter" },
  1254. { TK_TargetEnemyConstructor , "Target Enemy Constructor" },
  1255. { TK_TargetEnemyMiner , "Target Enemy Miner" },
  1256. { TK_TargetEnemyMajorBase , "Target Enemy Major Base" },
  1257. { TK_TargetEnemyBomberNearest , "Target Nearest Enemy Bomber" },
  1258. { TK_TargetEnemyFighterNearest , "Target Nearest Enemy Fighter" },
  1259. { TK_TargetEnemyConstructorNearest , "Target Nearest Enemy Constructor" },
  1260. { TK_TargetEnemyMinerNearest , "Target Nearest Enemy Miner" },
  1261. { TK_TargetEnemyMajorBaseNearest , "Target Nearest Enemy Major Base" },
  1262. { TK_TargetEnemyBomberPrev , "Target Previous Enemy Bomber" },
  1263. { TK_TargetEnemyFighterPrev , "Target Previous Enemy Fighter" },
  1264. { TK_TargetEnemyConstructorPrev , "Target Previous Enemy Constructor" },
  1265. { TK_TargetEnemyMinerPrev , "Target Previous Enemy Miner" },
  1266. { TK_TargetEnemyMajorBasePrev , "Target Previous Enemy Major Base" },
  1267. { TK_NoKeyMapping , "" },
  1268. { TK_TargetFriendly , "Target Friendly" },
  1269. { TK_TargetFriendlyNearest , "Target Friendly Nearest" },
  1270. { TK_TargetFriendlyPrev , "Target Friendly Previous" },
  1271. { TK_TargetFriendlyBase , "Target Friendly Base" },
  1272. { TK_TargetFriendlyBaseNearest , "Target Friendly Base Nearest" },
  1273. { TK_TargetFriendlyBasePrev , "Target Friendly Base Previous" },
  1274. { TK_TargetFriendlyBomber , "Target Friendly Bomber" },
  1275. { TK_TargetFriendlyFighter , "Target Friendly Fighter" },
  1276. { TK_TargetFriendlyConstructor , "Target Friendly Constructor" },
  1277. { TK_TargetFriendlyMiner , "Target Friendly Miner" },
  1278. { TK_TargetFriendlyLifepod , "Target Friendly Lifepod" },
  1279. { TK_TargetFriendlyMajorBase , "Target Friendly Major Base" },
  1280. { TK_TargetFriendlyBomberNearest , "Target Nearest Friendly Bomber" },
  1281. { TK_TargetFriendlyFighterNearest , "Target Nearest Friendly Fighter" },
  1282. { TK_TargetFriendlyConstructorNearest, "Target Nearest Friendly Constructor" },
  1283. { TK_TargetFriendlyMinerNearest , "Target Nearest Friendly Miner" },
  1284. { TK_TargetFriendlyLifepodNearest , "Target Nearest Friendly Lifepod" },
  1285. { TK_TargetFriendlyMajorBaseNearest , "Target Nearest Friendly Major Base" },
  1286. { TK_TargetFriendlyBomberPrev , "Target Previous Friendly Bomber" },
  1287. { TK_TargetFriendlyFighterPrev , "Target Previous Friendly Fighter" },
  1288. { TK_TargetFriendlyConstructorPrev , "Target Previous Friendly Constructor" },
  1289. { TK_TargetFriendlyMinerPrev , "Target Previous Friendly Miner" },
  1290. { TK_TargetFriendlyLifepodPrev , "Target Previous Friendly Lifepod" },
  1291. { TK_TargetFriendlyMajorBasePrev , "Target Previous Friendly Major Base" },
  1292. { TK_NoKeyMapping , "" },
  1293. { TK_NoKeyMapping , "Other" },
  1294. { TK_NoKeyMapping , "" },
  1295. { TK_Suicide , "Eject From Turret (Press 3 times)" },
  1296. { TK_ToggleCloak , "Toggle Cloak" },
  1297. { TK_Ripcord , "Ripcord (teleport)" },
  1298. { TK_PauseTM , "Pause (training mission only)" },
  1299. { TK_CommandFinishTM , "Finish goal (training mission only)" },
  1300. { TK_TrackCommandView , "Toggle command view target tracking" },
  1301. { TK_NoKeyMapping , "" },
  1302. { TK_NoKeyMapping , "Joystick" },
  1303. { TK_NoKeyMapping , "" },
  1304. };
  1305. const int g_nCommandInfo = ArrayCount(g_pCommandInfo);
  1306. //////////////////////////////////////////////////////////////////////////////
  1307. //
  1308. //
  1309. //
  1310. //////////////////////////////////////////////////////////////////////////////
  1311. class InputMapPopup :
  1312. public IPopup,
  1313. public EventTargetContainer<InputMapPopup>,
  1314. public List,
  1315. public ItemPainter
  1316. {
  1317. private:
  1318. //////////////////////////////////////////////////////////////////////////////
  1319. //
  1320. // Types
  1321. //
  1322. //////////////////////////////////////////////////////////////////////////////
  1323. class KeyMapping {
  1324. public:
  1325. int m_vk ;
  1326. int m_modifiers;
  1327. };
  1328. class ButtonMapping {
  1329. public:
  1330. int m_indexJoystick;
  1331. int m_indexButton ;
  1332. };
  1333. class CommandMap {
  1334. public:
  1335. TList<KeyMapping> m_listKeyMapping;
  1336. TList<ButtonMapping> m_listButtonMapping;
  1337. void Clear()
  1338. {
  1339. m_listKeyMapping.SetEmpty();
  1340. m_listButtonMapping.SetEmpty();
  1341. }
  1342. void AddKeyMapping(int vk, int modifiers)
  1343. {
  1344. m_listKeyMapping.PushFront();
  1345. m_listKeyMapping.GetFront().m_vk = vk;
  1346. if (modifiers == ModifierAny) {
  1347. m_listKeyMapping.GetFront().m_modifiers = ModifierNone;
  1348. } else {
  1349. m_listKeyMapping.GetFront().m_modifiers = modifiers;
  1350. }
  1351. }
  1352. void AddButtonMapping(int indexJoystick, int indexButton)
  1353. {
  1354. m_listButtonMapping.PushFront();
  1355. m_listButtonMapping.GetFront().m_indexJoystick = indexJoystick;
  1356. m_listButtonMapping.GetFront().m_indexButton = indexButton;
  1357. }
  1358. bool HasKeyMapping(int vk, int modifiers)
  1359. {
  1360. TList<KeyMapping>::Iterator iter(m_listKeyMapping);
  1361. while (!iter.End()) {
  1362. KeyMapping& map = iter.Value();
  1363. if (
  1364. map.m_vk == vk
  1365. && map.m_modifiers == modifiers
  1366. ) {
  1367. return true;
  1368. }
  1369. iter.Next();
  1370. }
  1371. return false;
  1372. }
  1373. void RemoveKeyMapping(int vk, int modifiers)
  1374. {
  1375. TList<KeyMapping>::Iterator iter(m_listKeyMapping);
  1376. while (!iter.End()) {
  1377. KeyMapping& map = iter.Value();
  1378. if (
  1379. map.m_vk == vk
  1380. && map.m_modifiers == modifiers
  1381. ) {
  1382. iter.Remove();
  1383. } else {
  1384. iter.Next();
  1385. }
  1386. }
  1387. }
  1388. bool HasButtonMapping(int indexJoystick, int indexButton)
  1389. {
  1390. TList<ButtonMapping>::Iterator iter(m_listButtonMapping);
  1391. while (!iter.End()) {
  1392. ButtonMapping& map = iter.Value();
  1393. if (
  1394. map.m_indexJoystick == indexJoystick
  1395. && map.m_indexButton == indexButton
  1396. ) {
  1397. return true;
  1398. }
  1399. iter.Next();
  1400. }
  1401. return false;
  1402. }
  1403. void RemoveButtonMapping(int indexJoystick, int indexButton)
  1404. {
  1405. TList<ButtonMapping>::Iterator iter(m_listButtonMapping);
  1406. while (!iter.End()) {
  1407. ButtonMapping& map = iter.Value();
  1408. if (
  1409. map.m_indexJoystick == indexJoystick
  1410. && map.m_indexButton == indexButton
  1411. ) {
  1412. iter.Remove();
  1413. } else {
  1414. iter.Next();
  1415. }
  1416. }
  1417. }
  1418. ZString GetString(InputEngine* pinputEngine, bool bInternational)
  1419. {
  1420. ZString str;
  1421. bool bFirst = true;
  1422. //
  1423. // Key mappings
  1424. //
  1425. {
  1426. TList<KeyMapping>::Iterator iter(m_listKeyMapping);
  1427. while (!iter.End()) {
  1428. KeyMapping& map = iter.Value();
  1429. if (bFirst) {
  1430. bFirst = false;
  1431. } else {
  1432. str += ", ";
  1433. }
  1434. if (map.m_modifiers & ModifierShift) {
  1435. str += "Shift+";
  1436. }
  1437. if (map.m_modifiers & ModifierControl) {
  1438. str += "Control+";
  1439. }
  1440. if (map.m_modifiers & ModifierAlt) {
  1441. str += "Alt+";
  1442. }
  1443. char buf[128];
  1444. UINT scan = ::MapVirtualKey(map.m_vk, 0) << 16;
  1445. if (scan & (1 << 32)) {
  1446. scan |= (1 << 24);
  1447. scan &= ~(1 << 23);
  1448. }
  1449. int length = 0;
  1450. if (bInternational) {
  1451. length = ::GetKeyNameText(scan, buf, 128);
  1452. }
  1453. if (length == 0) {
  1454. str += g_pkeyInfo[map.m_vk].m_pcc;
  1455. } else {
  1456. str += ZString(buf, length);
  1457. }
  1458. iter.Next();
  1459. }
  1460. }
  1461. //
  1462. // Button mappings
  1463. //
  1464. {
  1465. TList<ButtonMapping>::Iterator iter(m_listButtonMapping);
  1466. while (!iter.End()) {
  1467. ButtonMapping& map = iter.Value();
  1468. if (bFirst) {
  1469. bFirst = false;
  1470. } else {
  1471. str += ", ";
  1472. }
  1473. if (map.m_indexJoystick == -1) {
  1474. str += "Mouse Btn " + ZString(map.m_indexButton + 1);
  1475. } else {
  1476. str += "Joy " + ZString(map.m_indexJoystick);
  1477. switch(map.m_indexButton) {
  1478. case -1: str += " Hat Forward" ; break;
  1479. case -2: str += " Hat Right" ; break;
  1480. case -3: str += " Hat Backward" ; break;
  1481. case -4: str += " Hat Left " ; break;
  1482. default: str += " Btn " + ZString(map.m_indexButton + 1); break;
  1483. }
  1484. }
  1485. iter.Next();
  1486. }
  1487. }
  1488. //
  1489. // return the string
  1490. //
  1491. return str;
  1492. }
  1493. void FillModifierMap(TArray<bool, 256>& pbNoModifiers)
  1494. {
  1495. TList<KeyMapping>::Iterator iter(m_listKeyMapping);
  1496. while (!iter.End()) {
  1497. KeyMapping& map = iter.Value();
  1498. if (map.m_modifiers != ModifierNone) {
  1499. pbNoModifiers[map.m_vk] = false;
  1500. }
  1501. iter.Next();
  1502. }
  1503. }
  1504. void WriteKeyboardMapping(ZFile* pfile, int cmd, bool& bFirst, TArray<bool, 256>& pbNoModifiers)
  1505. {
  1506. {
  1507. TList<KeyMapping>::Iterator iter(m_listKeyMapping);
  1508. while (!iter.End()) {
  1509. KeyMapping& map = iter.Value();
  1510. if (bFirst) {
  1511. bFirst = false;
  1512. } else {
  1513. pfile->Write(ZString(",\n"));
  1514. }
  1515. int modifiers = map.m_modifiers;
  1516. if (
  1517. modifiers == ModifierNone
  1518. && pbNoModifiers[map.m_vk]
  1519. ) {
  1520. modifiers = ModifierAny;
  1521. }
  1522. pfile->Write(
  1523. " ("
  1524. + ZString(map.m_vk)
  1525. + ", "
  1526. + ZString(modifiers)
  1527. + ", "
  1528. + ZString(cmd)
  1529. + ")"
  1530. );
  1531. iter.Next();
  1532. }
  1533. }
  1534. }
  1535. void WriteMouseButtonMapping(ZFile* pfile, int cmd, bool& bFirst)
  1536. {
  1537. TList<ButtonMapping>::Iterator iter(m_listButtonMapping);
  1538. while (!iter.End()) {
  1539. ButtonMapping& map = iter.Value();
  1540. if (map.m_indexJoystick == -1) {
  1541. if (bFirst) {
  1542. bFirst = false;
  1543. } else {
  1544. pfile->Write(ZString(",\n"));
  1545. }
  1546. pfile->Write(
  1547. " ("
  1548. + ZString(map.m_indexButton)
  1549. + ", "
  1550. + ZString(cmd)
  1551. + ")"
  1552. );
  1553. }
  1554. iter.Next();
  1555. }
  1556. }
  1557. void WriteButtonMapping(ZFile* pfile, int cmd, bool& bFirst)
  1558. {
  1559. TList<ButtonMapping>::Iterator iter(m_listButtonMapping);
  1560. while (!iter.End()) {
  1561. ButtonMapping& map = iter.Value();
  1562. if (map.m_indexJoystick != -1) {
  1563. if (bFirst) {
  1564. bFirst = false;
  1565. } else {
  1566. pfile->Write(ZString(",\n"));
  1567. }
  1568. pfile->Write(
  1569. " ("
  1570. + ZString(map.m_indexJoystick)
  1571. + ", "
  1572. + ZString(map.m_indexButton)
  1573. + ", "
  1574. + ZString(cmd)
  1575. + ")"
  1576. );
  1577. }
  1578. iter.Next();
  1579. }
  1580. }
  1581. };
  1582. class Axis {
  1583. public:
  1584. int m_indexJoystick;
  1585. int m_indexValue;
  1586. };
  1587. class EvaluateImage : public WrapImage {
  1588. private:
  1589. InputMapPopup* m_ppopup;
  1590. public:
  1591. EvaluateImage(InputMapPopup* ppopup, Image* pimage, Number* ptime) :
  1592. WrapImage(pimage, ptime),
  1593. m_ppopup(ppopup)
  1594. {
  1595. }
  1596. void Evaluate()
  1597. {
  1598. m_ppopup->Evaluate();
  1599. }
  1600. //////////////////////////////////////////////////////////////////////////////
  1601. //
  1602. // Intercept all of the mouse input while editing
  1603. //
  1604. //////////////////////////////////////////////////////////////////////////////
  1605. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  1606. {
  1607. MouseResult result = WrapImage::HitTest(pprovider, point, bCaptured);
  1608. if (m_ppopup->IsEditing()) {
  1609. result.Set(MouseResultHit());
  1610. }
  1611. return result;
  1612. }
  1613. MouseResult Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  1614. {
  1615. if (m_ppopup->IsEditing()) {
  1616. if (bDown) {
  1617. m_ppopup->Button(button);
  1618. }
  1619. return MouseResult();
  1620. }
  1621. return WrapImage::Button(pprovider, point, button, bCaptured, bInside, bDown);
  1622. }
  1623. };
  1624. //////////////////////////////////////////////////////////////////////////////
  1625. //
  1626. // members
  1627. //
  1628. //////////////////////////////////////////////////////////////////////////////
  1629. CommandMap m_pcommandMap[TK_Max];
  1630. TArray<Axis, countAxis> m_paxis;
  1631. TVector<float> m_vvalue;
  1632. TArray<bool, 256> m_pbNoModifiers;
  1633. TRef<Modeler> m_pmodeler;
  1634. TRef<InputEngine> m_pinputEngine;
  1635. TRef<TrekInput> m_ptrekInput;
  1636. TRef<IEngineFont> m_pfont;
  1637. TRef<Number> m_ptime;
  1638. TRef<ModifiableString> m_pstringText;
  1639. TRef<Pane> m_ppane;
  1640. TRef<ButtonPane> m_pbuttonOK;
  1641. TRef<ButtonPane> m_pbuttonCancel;
  1642. TRef<ButtonPane> m_pbuttonRestore;
  1643. TRef<ButtonPane> m_pbuttonClose;
  1644. TRef<ListPane> m_plistPane;
  1645. TRef<EventSourceImpl> m_peventSource;
  1646. int m_column;
  1647. Color m_colorSelection;
  1648. Color m_colorText;
  1649. Color m_colorNoMapping;
  1650. bool m_bInternational;
  1651. bool m_bEditing;
  1652. bool m_bQuestion;
  1653. bool m_bButton;
  1654. bool m_bAxis;
  1655. int m_vkQuestion;
  1656. int m_modifiersQuestion;
  1657. int m_indexJoystickQuestion;
  1658. int m_indexButtonQuestion;
  1659. int m_indexAxisQuestion;
  1660. public:
  1661. //////////////////////////////////////////////////////////////////////////////
  1662. //
  1663. // constructor
  1664. //
  1665. //////////////////////////////////////////////////////////////////////////////
  1666. bool IsInternationalKeyboard()
  1667. {
  1668. HKL hkl = ::GetKeyboardLayout(0);
  1669. return (hkl != (HKL)0x04090409);
  1670. }
  1671. InputMapPopup(
  1672. Modeler* pmodeler,
  1673. InputEngine* pinputEngine,
  1674. TrekInput* ptrekInput,
  1675. IEngineFont* pfont,
  1676. Number* ptime
  1677. ) :
  1678. m_pmodeler(pmodeler),
  1679. m_pinputEngine(pinputEngine),
  1680. m_ptrekInput(ptrekInput),
  1681. m_pfont(pfont),
  1682. m_ptime(ptime),
  1683. m_peventSource(new EventSourceImpl()),
  1684. m_bEditing(false),
  1685. m_bQuestion(false),
  1686. m_bButton(false),
  1687. m_bAxis(false)
  1688. {
  1689. m_bInternational = IsInternationalKeyboard();
  1690. //
  1691. // Exports
  1692. //
  1693. TRef<INameSpace> pnsTeamScreenData = GetModeler()->CreateNameSpace("inputmappanedata");
  1694. pnsTeamScreenData->AddMember("text", m_pstringText = new ModifiableString(ZString()));
  1695. SetMessage();
  1696. //
  1697. // Imports
  1698. //
  1699. TRef<INameSpace> pns = pmodeler->GetNameSpace("inputmappane");
  1700. CastTo(m_ppane, pns->FindMember("inputMapPane"));
  1701. //
  1702. // buttons
  1703. //
  1704. CastTo(m_pbuttonOK, pns->FindMember("okButton"));
  1705. CastTo(m_pbuttonCancel, pns->FindMember("cancelButton"));
  1706. CastTo(m_pbuttonRestore, pns->FindMember("restoreButton"));
  1707. CastTo(m_pbuttonClose, pns->FindMember("closeButton"));
  1708. AddEventTarget(OnButtonOK, m_pbuttonOK->GetEventSource());
  1709. AddEventTarget(OnButtonCancel, m_pbuttonCancel->GetEventSource());
  1710. AddEventTarget(OnButtonRestore, m_pbuttonRestore->GetEventSource());
  1711. //AddEventTarget(OnButtonCancel, m_pbuttonClose->GetEventSource());
  1712. //
  1713. // details
  1714. //
  1715. m_column = (int)pns->FindNumber("mappingOffset");
  1716. m_colorText = pns->FindColor("textColor");
  1717. m_colorNoMapping = pns->FindColor("noMappingColor");
  1718. m_colorSelection = pns->FindColor("selectionColor");
  1719. //
  1720. // the list
  1721. //
  1722. CastTo(m_plistPane, (Pane*)pns->FindMember("listPane"));
  1723. AddEventTarget(OnDoubleClick, m_plistPane->GetDoubleClickEventSource());
  1724. m_plistPane->SetItemPainter(this);
  1725. m_plistPane->SetList(this);
  1726. //
  1727. // Unload the name space
  1728. //
  1729. pmodeler->UnloadNameSpace(pns);
  1730. //
  1731. // Load the current mapping
  1732. //
  1733. if (!LoadMap(INPUTMAP_FILE)) {
  1734. LoadMap(DEFAULTINPUTMAP_FILE);
  1735. }
  1736. }
  1737. //////////////////////////////////////////////////////////////////////////////
  1738. //
  1739. // methods
  1740. //
  1741. //////////////////////////////////////////////////////////////////////////////
  1742. void SetMessage(const ZString& str = ZString())
  1743. {
  1744. if (str.IsEmpty()) {
  1745. m_pstringText->SetValue("Press Enter to add a mapping or Delete to remove mapping.");
  1746. } else {
  1747. m_pstringText->SetValue(str);
  1748. }
  1749. }
  1750. void Changed()
  1751. {
  1752. m_peventSource->Trigger();
  1753. }
  1754. bool LoadMap(const ZString& str)
  1755. {
  1756. //
  1757. // load the input map
  1758. //
  1759. TRef<INameSpace> pns = GetModeler()->GetNameSpace(str, false);
  1760. if (pns == NULL) {
  1761. return false;
  1762. }
  1763. //
  1764. // Redetect the joysticks
  1765. //
  1766. m_pinputEngine->EnumerateJoysticks();
  1767. //
  1768. // clear the old input map
  1769. //
  1770. int index;
  1771. for (index = 0; index < TK_Max; index++) {
  1772. m_pcommandMap[index].Clear();
  1773. }
  1774. //
  1775. // keyboard mapping
  1776. //
  1777. {
  1778. TRef<IObjectList> plist = pns->FindList("keyCommandMap");
  1779. plist->GetFirst();
  1780. while (plist->GetCurrent() != NULL) {
  1781. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  1782. int vk = (int)GetNumber(ppair->GetNth(0) );
  1783. int modifiers = (int)GetNumber(ppair->GetNth(1) );
  1784. int tk = (int)GetNumber(ppair->GetLastNth(2));
  1785. m_pcommandMap[tk].AddKeyMapping(vk, modifiers);
  1786. plist->GetNext();
  1787. }
  1788. }
  1789. //
  1790. // mouse button mapping
  1791. //
  1792. {
  1793. TRef<IObjectList> plist = pns->FindList("mouseButtonCommandMap");
  1794. if (plist) {
  1795. plist->GetFirst();
  1796. while (plist->GetCurrent() != NULL) {
  1797. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  1798. int index = (int)GetNumber(ppair->GetFirst() );
  1799. int tk = (int)GetNumber(ppair->GetSecond());
  1800. m_pcommandMap[tk].AddButtonMapping(-1, index);
  1801. plist->GetNext();
  1802. }
  1803. }
  1804. }
  1805. //
  1806. // joystick button mapping
  1807. //
  1808. {
  1809. TRef<IObjectList> plist = pns->FindList("buttonCommandMap");
  1810. plist->GetFirst();
  1811. while (plist->GetCurrent() != NULL) {
  1812. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  1813. int indexJoystick = (int)GetNumber(ppair->GetNth(0) );
  1814. int indexButton = (int)GetNumber(ppair->GetNth(1) );
  1815. int tk = (int)GetNumber(ppair->GetLastNth(2));
  1816. m_pcommandMap[tk].AddButtonMapping(indexJoystick, indexButton);
  1817. plist->GetNext();
  1818. }
  1819. }
  1820. //
  1821. // joystick value mapping
  1822. //
  1823. {
  1824. TRef<IObjectList> plist = pns->FindList("numericValuesNew");
  1825. bool bNew = (plist != NULL);
  1826. if (!bNew) {
  1827. plist = pns->FindList("numericValues");
  1828. }
  1829. plist->GetFirst();
  1830. for (int indexValueOutput = 0; indexValueOutput < m_paxis.GetCount(); indexValueOutput++) {
  1831. TRef<IObjectPair> ppair; CastTo(ppair, plist->GetCurrent());
  1832. if (ppair) {
  1833. m_paxis[indexValueOutput].m_indexJoystick = (int)GetNumber(ppair->GetFirst() );
  1834. m_paxis[indexValueOutput].m_indexValue = (int)GetNumber(ppair->GetSecond());
  1835. if (!bNew) {
  1836. if (m_paxis[indexValueOutput].m_indexValue == 2) {
  1837. m_paxis[indexValueOutput].m_indexValue = 3;
  1838. } else if (m_paxis[indexValueOutput].m_indexValue == 3) {
  1839. m_paxis[indexValueOutput].m_indexValue = 2;
  1840. }
  1841. }
  1842. } else {
  1843. m_paxis[indexValueOutput].m_indexJoystick = -1;
  1844. m_paxis[indexValueOutput].m_indexValue = 0;
  1845. }
  1846. plist->GetNext();
  1847. }
  1848. }
  1849. //
  1850. // Unload the map
  1851. //
  1852. GetModeler()->UnloadNameSpace(pns);
  1853. return true;
  1854. }
  1855. void SaveMap(const ZString& str)
  1856. {
  1857. TRef<ZFile> pfile = new ZWriteFile(m_pmodeler->GetArtPath() + "/" + str + ".mdl");
  1858. pfile->Write(ZString("use \"commands\";\n"));
  1859. pfile->Write(ZString("use \"keycode\";\n" ));
  1860. //
  1861. // Calculate the modifier map
  1862. //
  1863. int index;
  1864. for (index = 0; index < 256; index++) {
  1865. m_pbNoModifiers[index] = true;
  1866. }
  1867. for (index = 0; index < TK_Max; index++) {
  1868. m_pcommandMap[index].FillModifierMap(m_pbNoModifiers);
  1869. }
  1870. //
  1871. // keyCommandMap
  1872. //
  1873. pfile->Write(ZString("keyCommandMap =\n" ));
  1874. pfile->Write(ZString(" [\n" ));
  1875. bool bFirst = true;
  1876. for (index = 0; index < TK_Max; index++) {
  1877. m_pcommandMap[index].WriteKeyboardMapping(pfile, index, bFirst, m_pbNoModifiers);
  1878. }
  1879. if (!bFirst) {
  1880. pfile->Write(ZString("\n"));
  1881. }
  1882. pfile->Write(ZString(" ];\n" ));
  1883. //
  1884. // mouseButtonCommandMap
  1885. //
  1886. pfile->Write(ZString("mouseButtonCommandMap =\n"));
  1887. pfile->Write(ZString(" [\n" ));
  1888. bFirst = true;
  1889. for (index = 0; index < TK_Max; index++) {
  1890. m_pcommandMap[index].WriteMouseButtonMapping(pfile, index, bFirst);
  1891. }
  1892. if (!bFirst) {
  1893. pfile->Write(ZString("\n"));
  1894. }
  1895. pfile->Write(ZString(" ];\n" ));
  1896. //
  1897. // buttonCommandMap
  1898. //
  1899. pfile->Write(ZString("buttonCommandMap =\n"));
  1900. pfile->Write(ZString(" [\n" ));
  1901. bFirst = true;
  1902. for (index = 0; index < TK_Max; index++) {
  1903. m_pcommandMap[index].WriteButtonMapping(pfile, index, bFirst);
  1904. }
  1905. if (!bFirst) {
  1906. pfile->Write(ZString("\n"));
  1907. }
  1908. pfile->Write(ZString(" ];\n" ));
  1909. //
  1910. // numericValues
  1911. //
  1912. pfile->Write(ZString("numericValuesNew =\n"));
  1913. pfile->Write(ZString(" [\n" ));
  1914. for (index = 0; index < m_paxis.GetCount(); index++) {
  1915. pfile->Write(
  1916. " ("
  1917. + ZString(m_paxis[index].m_indexJoystick)
  1918. + ", "
  1919. + ZString(m_paxis[index].m_indexValue)
  1920. + ")"
  1921. );
  1922. if (index != m_paxis.GetCount() - 1) {
  1923. pfile->Write(ZString(","));
  1924. }
  1925. pfile->Write(ZString("\n"));
  1926. }
  1927. pfile->Write(ZString(" ];\n" ));
  1928. }
  1929. void Close()
  1930. {
  1931. if (!m_ptrekInput->LoadMap(INPUTMAP_FILE)) {
  1932. m_ptrekInput->LoadMap(DEFAULTINPUTMAP_FILE);
  1933. }
  1934. m_plistPane->SetList(NULL);
  1935. m_plistPane->SetItemPainter(NULL);
  1936. ClosePopup(this);
  1937. }
  1938. bool OnButtonOK()
  1939. {
  1940. SaveMap(INPUTMAP_FILE);
  1941. Close();
  1942. return true;
  1943. }
  1944. bool OnButtonCancel()
  1945. {
  1946. Close();
  1947. return true;
  1948. }
  1949. bool OnButtonRestore()
  1950. {
  1951. LoadMap(DEFAULTINPUTMAP_FILE);
  1952. Changed();
  1953. return true;
  1954. }
  1955. bool OnDoubleClick()
  1956. {
  1957. EditSelection();
  1958. return true;
  1959. }
  1960. int GetSelectionIndex()
  1961. {
  1962. return GetIndex(m_plistPane->GetSelection());
  1963. }
  1964. bool IsAxisSelection()
  1965. {
  1966. return GetSelectionIndex() >= g_nCommandInfo;
  1967. }
  1968. int GetCommandMapIndex()
  1969. {
  1970. return g_pCommandInfo[GetSelectionIndex()].m_tk;
  1971. }
  1972. int GetAxisIndex()
  1973. {
  1974. return GetSelectionIndex() - g_nCommandInfo;
  1975. }
  1976. void DeleteMapping()
  1977. {
  1978. if (IsAxisSelection()) {
  1979. m_paxis[GetAxisIndex()].m_indexJoystick = -1;
  1980. m_paxis[GetAxisIndex()].m_indexValue = 0;
  1981. } else {
  1982. TrekKey tk = GetCommandMapIndex();
  1983. if (tk != TK_NoKeyMapping) {
  1984. m_pcommandMap[GetCommandMapIndex()].Clear();
  1985. }
  1986. }
  1987. Changed();
  1988. }
  1989. bool IsEditing()
  1990. {
  1991. return m_bEditing;
  1992. }
  1993. void EditSelection()
  1994. {
  1995. if (IsAxisSelection()) {
  1996. //
  1997. // Record the initial values of all the joystick axis
  1998. //
  1999. m_vvalue.SetCount(0);
  2000. int countJoystick = m_pinputEngine->GetJoystickCount();
  2001. for (int indexJoystick = 0; indexJoystick < countJoystick; indexJoystick++) {
  2002. JoystickInputStream* pjoystick = m_pinputEngine->GetJoystick(indexJoystick);
  2003. int count = pjoystick->GetValueCount();
  2004. for (int index = 0; index < count; index++) {
  2005. m_vvalue.PushEnd(pjoystick->GetValue(index)->GetValue());
  2006. }
  2007. }
  2008. m_bEditing = true;
  2009. SetMessage("Move desired joystick axis.");
  2010. } else {
  2011. TrekKey tk = GetCommandMapIndex();
  2012. m_bEditing = (tk != TK_NoKeyMapping);
  2013. if (m_bEditing) {
  2014. SetMessage("Type desired key sequence or button. Escape cancels.");
  2015. }
  2016. }
  2017. Changed();
  2018. }
  2019. void StopEditing()
  2020. {
  2021. m_bEditing = false;
  2022. m_bQuestion = false;
  2023. SetMessage();
  2024. Changed();
  2025. }
  2026. void RemoveKeyMappings(int vk, int modifiers)
  2027. {
  2028. for (int index = 0; index < TK_Max; index++) {
  2029. m_pcommandMap[index].RemoveKeyMapping(vk, modifiers);
  2030. }
  2031. }
  2032. bool AddKeyMapping(int vk, int modifiers)
  2033. {
  2034. //
  2035. // See if the key is already being used
  2036. //
  2037. for (int index = 0; index < TK_Max; index++) {
  2038. if (m_pcommandMap[index].HasKeyMapping(vk, modifiers)) {
  2039. if (index == GetCommandMapIndex()) {
  2040. return true;
  2041. }
  2042. //
  2043. // Key is being used
  2044. //
  2045. m_vkQuestion = vk;
  2046. m_modifiersQuestion = modifiers;
  2047. m_bQuestion = true;
  2048. m_bButton = false;
  2049. m_bAxis = false;
  2050. SetMessage("Key in use. Press Enter to lose old mapping. Escape cancels");
  2051. return false;
  2052. }
  2053. }
  2054. //
  2055. // Not being used add the mapping
  2056. //
  2057. m_pcommandMap[GetCommandMapIndex()].AddKeyMapping(vk, modifiers);
  2058. return true;
  2059. }
  2060. void RemoveButtonMappings(int indexJoystick, int indexButton)
  2061. {
  2062. for (int index = 0; index < TK_Max; index++) {
  2063. m_pcommandMap[index].RemoveButtonMapping(indexJoystick, indexButton);
  2064. }
  2065. }
  2066. bool AddButtonMapping(int indexJoystick, int indexButton)
  2067. {
  2068. //
  2069. // See if the button is already being used
  2070. //
  2071. for (int index = 0; index < TK_Max; index++) {
  2072. if (m_pcommandMap[index].HasButtonMapping(indexJoystick, indexButton)) {
  2073. if (index == GetCommandMapIndex()) {
  2074. return true;
  2075. }
  2076. //
  2077. // Button is being used
  2078. //
  2079. m_indexJoystickQuestion = indexJoystick;
  2080. m_indexButtonQuestion = indexButton;
  2081. m_bQuestion = true;
  2082. m_bButton = true;
  2083. m_bAxis = false;
  2084. SetMessage("Button in use. Press Enter to lose old mapping. Escape cancels");
  2085. return false;
  2086. }
  2087. }
  2088. //
  2089. // Not being used add the mapping
  2090. //
  2091. m_pcommandMap[GetCommandMapIndex()].AddButtonMapping(indexJoystick, indexButton);
  2092. return true;
  2093. }
  2094. void RemoveAxisMapping(int indexJoystick, int indexValue)
  2095. {
  2096. for (int index = 0; index < m_paxis.GetCount(); index++) {
  2097. if (
  2098. m_paxis[index].m_indexJoystick == indexJoystick
  2099. && m_paxis[index].m_indexValue == indexValue
  2100. ) {
  2101. m_paxis[index].m_indexJoystick = -1;
  2102. m_paxis[index].m_indexValue = 0;
  2103. }
  2104. }
  2105. }
  2106. bool AddAxisMapping(int indexJoystick, int indexValue)
  2107. {
  2108. //
  2109. // See if the axis is already being used
  2110. //
  2111. for (int index = 0; index < m_paxis.GetCount(); index++) {
  2112. if (
  2113. (m_paxis[index].m_indexJoystick == indexJoystick)
  2114. && (m_paxis[index].m_indexValue == indexValue)
  2115. ) {
  2116. if (index == GetAxisIndex()) {
  2117. return true;
  2118. }
  2119. //
  2120. // Axis is being used
  2121. //
  2122. m_indexJoystickQuestion = indexJoystick;
  2123. m_indexAxisQuestion = indexValue;
  2124. m_bQuestion = true;
  2125. m_bButton = false;
  2126. m_bAxis = true;
  2127. SetMessage("Axis in use. Press Enter to lose old mapping. Escape cancels");
  2128. return false;
  2129. }
  2130. }
  2131. //
  2132. // Not being used add the mapping
  2133. //
  2134. m_paxis[GetAxisIndex()].m_indexJoystick = indexJoystick;
  2135. m_paxis[GetAxisIndex()].m_indexValue = indexValue;
  2136. return true;
  2137. }
  2138. void Button(int index)
  2139. {
  2140. if (!IsAxisSelection()) {
  2141. if (AddButtonMapping(-1, index)) {
  2142. StopEditing();
  2143. }
  2144. }
  2145. }
  2146. //////////////////////////////////////////////////////////////////////////////
  2147. //
  2148. // Value methods
  2149. //
  2150. //////////////////////////////////////////////////////////////////////////////
  2151. void Evaluate()
  2152. {
  2153. if (m_bEditing) {
  2154. int countJoystick = m_pinputEngine->GetJoystickCount();
  2155. int vindex = 0;
  2156. for (int indexJoystick = 0; indexJoystick < countJoystick; indexJoystick++) {
  2157. JoystickInputStream* pjoystick = m_pinputEngine->GetJoystick(indexJoystick);
  2158. if (IsAxisSelection()) {
  2159. //
  2160. // axis: check the axis
  2161. //
  2162. int count = pjoystick->GetValueCount();
  2163. for (int index = 0; index < count; index++) {
  2164. float dvalue = m_vvalue[vindex] - pjoystick->GetValue(index)->GetValue();
  2165. vindex++;
  2166. if (dvalue < -0.9 || dvalue > 0.9) {
  2167. if (AddAxisMapping(indexJoystick, index)) {
  2168. StopEditing();
  2169. }
  2170. return;
  2171. }
  2172. }
  2173. } else {
  2174. //
  2175. // command: check the buttons
  2176. //
  2177. int count = pjoystick->GetButtonCount();
  2178. for (int index = 0; index < count; index++) {
  2179. if (pjoystick->IsDown(index)->GetValue()) {
  2180. if (AddButtonMapping(indexJoystick, index)) {
  2181. StopEditing();
  2182. }
  2183. return;
  2184. }
  2185. }
  2186. }
  2187. }
  2188. //
  2189. // Check the virtual buttons
  2190. //
  2191. if (!IsAxisSelection()) {
  2192. JoystickInputStream* pjoystick = m_pinputEngine->GetJoystick(m_paxis[5].m_indexJoystick);
  2193. if (pjoystick) {
  2194. Number* pvalue = pjoystick->GetValue(m_paxis[5].m_indexValue);
  2195. if (pvalue) {
  2196. float value = pvalue->GetValue();
  2197. if (value > 0.75f) {
  2198. if (AddButtonMapping(indexJoystick, -3)) {
  2199. StopEditing();
  2200. }
  2201. return;
  2202. } else if (value > 0.25f) {
  2203. if (AddButtonMapping(indexJoystick, -2)) {
  2204. StopEditing();
  2205. }
  2206. return;
  2207. } else if (value > -0.25f) {
  2208. if (AddButtonMapping(indexJoystick, -1)) {
  2209. StopEditing();
  2210. }
  2211. return;
  2212. } else if (value > -0.75f) {
  2213. if (AddButtonMapping(indexJoystick, -4)) {
  2214. StopEditing();
  2215. }
  2216. return;
  2217. } else if (value > -1.25f) {
  2218. if (AddButtonMapping(indexJoystick, -3)) {
  2219. StopEditing();
  2220. }
  2221. return;
  2222. }
  2223. }
  2224. }
  2225. }
  2226. }
  2227. }
  2228. //////////////////////////////////////////////////////////////////////////////
  2229. //
  2230. // List methods
  2231. //
  2232. //////////////////////////////////////////////////////////////////////////////
  2233. int GetCount()
  2234. {
  2235. return g_nCommandInfo + m_paxis.GetCount();
  2236. }
  2237. ItemID GetItem(int index)
  2238. {
  2239. return (ItemID)(index + 1);
  2240. }
  2241. int GetIndex(ItemID pitem)
  2242. {
  2243. return int(pitem) - 1;
  2244. }
  2245. ItemID GetNext(ItemID pitem)
  2246. {
  2247. int index = GetIndex(pitem);
  2248. if (index == GetCount() - 1) {
  2249. return 0;
  2250. } else {
  2251. return GetItem(index + 1);
  2252. }
  2253. }
  2254. IEventSource* GetChangedEvent()
  2255. {
  2256. return m_peventSource;
  2257. }
  2258. //////////////////////////////////////////////////////////////////////////////
  2259. //
  2260. // ItemPainter methods
  2261. //
  2262. //////////////////////////////////////////////////////////////////////////////
  2263. int GetXSize()
  2264. {
  2265. return m_plistPane->XSize();
  2266. }
  2267. int GetYSize()
  2268. {
  2269. return m_pfont->GetHeight();
  2270. }
  2271. void Paint(ItemID itemID, Surface* psurface, bool bSelected, bool bFocus)
  2272. {
  2273. ZString strCommand;
  2274. ZString strMapping;
  2275. int index = GetIndex(itemID);
  2276. bool bNoMapping = false;
  2277. if (index >= g_nCommandInfo) {
  2278. int indexAxis = index - g_nCommandInfo;
  2279. strCommand = g_ppccAxisDescription[indexAxis];
  2280. if (m_paxis[indexAxis].m_indexJoystick != -1) {
  2281. JoystickInputStream* pjoystick = m_pinputEngine->GetJoystick(m_paxis[indexAxis].m_indexJoystick);
  2282. TRef<Number> pvalueInput;
  2283. if (pjoystick) {
  2284. pvalueInput = pjoystick->GetValue(m_paxis[indexAxis].m_indexValue);
  2285. }
  2286. if (pvalueInput) {
  2287. strMapping =
  2288. pjoystick->GetShortDescription(m_paxis[indexAxis].m_indexJoystick)
  2289. + " "
  2290. + pjoystick->GetValueDescription(m_paxis[indexAxis].m_indexValue);
  2291. } else {
  2292. strMapping =
  2293. "Joy " + ZString(m_paxis[indexAxis].m_indexJoystick)
  2294. + " Value "
  2295. + ZString(m_paxis[indexAxis].m_indexValue);
  2296. }
  2297. }
  2298. } else {
  2299. strCommand = g_pCommandInfo[index].m_pcc;
  2300. TrekKey tk = g_pCommandInfo[index].m_tk;
  2301. if (tk != TK_NoKeyMapping) {
  2302. strMapping = m_pcommandMap[tk].GetString(m_pinputEngine, m_bInternational);
  2303. } else {
  2304. bNoMapping = true;
  2305. }
  2306. }
  2307. if (m_bEditing && index == GetSelectionIndex()) {
  2308. if (m_bQuestion) {
  2309. strMapping = "!!!";
  2310. } else {
  2311. strMapping = "???";
  2312. }
  2313. }
  2314. if (bSelected) {
  2315. psurface->FillRect(
  2316. WinRect(0, 0, GetXSize(), GetYSize()),
  2317. m_colorSelection
  2318. );
  2319. }
  2320. if (bNoMapping) {
  2321. psurface->DrawString(m_pfont, m_colorNoMapping, WinPoint(0, 0), strCommand);
  2322. } else {
  2323. psurface->DrawString(m_pfont, m_colorText, WinPoint(0, 0), strCommand);
  2324. }
  2325. psurface->DrawString(m_pfont, m_colorText, WinPoint(m_column, 0), strMapping);
  2326. }
  2327. //////////////////////////////////////////////////////////////////////////////
  2328. //
  2329. // IPopup methods
  2330. //
  2331. //////////////////////////////////////////////////////////////////////////////
  2332. Pane* GetPane()
  2333. {
  2334. return m_ppane;
  2335. }
  2336. TRef<Image> GetImage(Engine* pengine)
  2337. {
  2338. return
  2339. new EvaluateImage(
  2340. this,
  2341. CreatePaneImage(pengine, SurfaceType3D(), true, GetPane()),
  2342. m_ptime
  2343. );
  2344. }
  2345. //////////////////////////////////////////////////////////////////////////////
  2346. //
  2347. // IKeyboardInput methods
  2348. //
  2349. //////////////////////////////////////////////////////////////////////////////
  2350. bool OnKey(IInputProvider* pprovider, const KeyState& ks, bool& fForceTranslate)
  2351. {
  2352. if (ks.bDown) {
  2353. if (m_bQuestion) {
  2354. if (ks.vk == VK_ESCAPE) {
  2355. StopEditing();
  2356. } else if (ks.vk == VK_RETURN) {
  2357. if (m_bButton) {
  2358. RemoveButtonMappings(m_indexJoystickQuestion, m_indexButtonQuestion);
  2359. AddButtonMapping(m_indexJoystickQuestion, m_indexButtonQuestion);
  2360. } else if (m_bAxis) {
  2361. RemoveAxisMapping(m_indexJoystickQuestion, m_indexAxisQuestion);
  2362. AddAxisMapping(m_indexJoystickQuestion, m_indexAxisQuestion);
  2363. } else {
  2364. RemoveKeyMappings(m_vkQuestion, m_modifiersQuestion);
  2365. AddKeyMapping(m_vkQuestion, m_modifiersQuestion);
  2366. }
  2367. StopEditing();
  2368. }
  2369. } else if (m_bEditing) {
  2370. if (ks.vk == VK_ESCAPE) {
  2371. StopEditing();
  2372. } else if (
  2373. ks.vk != VK_MENU
  2374. && ks.vk != VK_CONTROL
  2375. && ks.vk != VK_SHIFT
  2376. ) {
  2377. if (!IsAxisSelection()) {
  2378. //
  2379. // Calculate the modifiers
  2380. //
  2381. int modifiers = 0;
  2382. if (ks.bAlt) {
  2383. modifiers |= ModifierAlt;
  2384. }
  2385. if (ks.bShift) {
  2386. modifiers |= ModifierShift;
  2387. }
  2388. if (ks.bControl) {
  2389. modifiers |= ModifierControl;
  2390. }
  2391. if (AddKeyMapping(ks.vk, modifiers)) {
  2392. StopEditing();
  2393. }
  2394. }
  2395. }
  2396. } else {
  2397. switch (ks.vk) {
  2398. case VK_ESCAPE:
  2399. OnButtonCancel();
  2400. break;
  2401. case VK_DELETE:
  2402. DeleteMapping();
  2403. break;
  2404. case VK_RETURN:
  2405. EditSelection();
  2406. break;
  2407. case VK_SPACE:
  2408. OnButtonOK();
  2409. break;
  2410. default:
  2411. m_plistPane->OnKey(pprovider, ks, fForceTranslate);
  2412. break;
  2413. }
  2414. }
  2415. }
  2416. return true;
  2417. }
  2418. };
  2419. TRef<IPopup> CreateInputMapPopup(
  2420. Modeler* pmodeler,
  2421. InputEngine* pinputEngine,
  2422. TrekInput* ptrekInput,
  2423. IEngineFont* pfont,
  2424. Number* ptime
  2425. ) {
  2426. return
  2427. new InputMapPopup(
  2428. pmodeler,
  2429. pinputEngine,
  2430. ptrekInput,
  2431. pfont,
  2432. ptime
  2433. );
  2434. }