inpututils.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. class InputUtils
  2. {
  3. //useful for console preset differentiation
  4. static const string PRESET_OLD = "#STR_UAPRESET_0";
  5. static const string PRESET_NEW = "#STR_UAPRESET_1";
  6. static const int VARIANT_OLD = 0;
  7. static const int VARIANT_NEW = 1;
  8. static int m_CurrentPresetIDConsole = -1;
  9. //
  10. static protected ref map<int, ref array<int>> m_InputActionSortingMap; //<sorting_idx,<input_ID>>
  11. static protected ref array<int> m_UnsortedInputActions;
  12. static const float ICON_SCALE_NORMAL = 1.21;
  13. static const float ICON_SCALE_TOOLBAR = 1.81;
  14. static string GetButtonNameFromInput(string pInputName, int pInputDeviceType)
  15. {
  16. UAInput inp = GetUApi().GetInputByName(pInputName);
  17. for (int i = 0; i < inp.AlternativeCount(); i++)
  18. {
  19. inp.SelectAlternative(i);
  20. if (inp.CheckBindDevice(0, pInputDeviceType))
  21. {
  22. return GetUApi().GetButtonName(inp.GetBindKey(0));
  23. }
  24. }
  25. return "";
  26. }
  27. //! returns a map of button names, combo or not
  28. static map<int,ref TStringArray> GetComboButtonNamesFromInput(string pInputName, int pInputDeviceType) //TODO: revisit to include same input device alternatives too?
  29. {
  30. UAInput inp = GetUApi().GetInputByName(pInputName);
  31. TStringArray buttons;
  32. map<int,ref TStringArray> output;//<alternativeIDX<button_array>>
  33. for (int i = 0; i < inp.AlternativeCount(); i++)
  34. {
  35. inp.SelectAlternative(i);
  36. if (inp.BindingCount() > 0 && inp.Binding(0) != 0 && inp.CheckBindDevice(0,pInputDeviceType))
  37. {
  38. buttons = new TStringArray;
  39. if (inp.IsCombo())
  40. {
  41. buttons.Insert(GetUApi().GetButtonName( inp.Binding(0) ));
  42. for (int j = 1; j < inp.BindingCount(); j++)
  43. {
  44. if (inp.Binding(j) != 0 && inp.CheckBindDevice(j,pInputDeviceType))
  45. {
  46. buttons.Insert(GetUApi().GetButtonName( inp.Binding(j) ));
  47. }
  48. }
  49. //return buttons;
  50. }
  51. else
  52. {
  53. buttons.Insert(GetUApi().GetButtonName(inp.GetBindKey(0)));
  54. //return buttons;
  55. }
  56. if (buttons.Count() > 0)
  57. {
  58. if (!output)
  59. {
  60. output = new map<int,ref TStringArray>;
  61. }
  62. output.Insert(i,buttons);
  63. }
  64. }
  65. }
  66. return output;
  67. }
  68. static array<string> GetButtonIconPathFromInput(notnull UAInput pInput, int pInputDeviceType = EUAINPUT_DEVICE_CONTROLLER)
  69. {
  70. array<string> buttonIcons = new array<string>();
  71. for (int i = 0; i < pInput.AlternativeCount(); i++)
  72. {
  73. pInput.SelectAlternative(i);
  74. bool done = false;
  75. for (int bk = 0; bk < pInput.BindKeyCount(); bk++)
  76. {
  77. if (pInput.CheckBindDevice(0, pInputDeviceType))
  78. {
  79. buttonIcons.Insert(GetUApi().GetButtonIcon(pInput.Binding(bk)));
  80. if (bk == pInput.BindKeyCount() - 1)
  81. {
  82. done = true;
  83. }
  84. }
  85. }
  86. if (done)
  87. {
  88. buttonIcons.Invert();
  89. return buttonIcons;
  90. }
  91. }
  92. return buttonIcons;
  93. }
  94. static array<string> GetButtonIconPathFromInput(string pInputName, int pInputDeviceType)
  95. {
  96. UAInput inp = GetUApi().GetInputByName(pInputName);
  97. array<string> buttonIcons = new array<string>();
  98. buttonIcons = InputUtils.GetButtonIconPathFromInput(inp, pInputDeviceType);
  99. return buttonIcons;
  100. }
  101. static void GetImagesetAndIconFromInputAction(notnull UAInput pInput, int pInputDeviceType, out array<string> pImageSet, out array<string> pIconName)
  102. {
  103. array<string> buttons = InputUtils.GetButtonIconPathFromInput(pInput, pInputDeviceType);
  104. if (buttons.Count() == 0 )
  105. {
  106. return;
  107. }
  108. for (int i = 0; i < buttons.Count(); i++)
  109. {
  110. array<string> parts = new array<string>;
  111. buttons.Get(i).Split(":", parts);
  112. if (parts.Count() < 2)
  113. {
  114. return;
  115. }
  116. pImageSet.Insert(parts[1].SubstringInverted(parts[1], parts[1].Length() - 6, parts[1].Length()));
  117. //! Asia vs Europe/N.America controller layout
  118. #ifdef PLATFORM_PS4
  119. if (GetGame().GetInput().GetEnterButton() == GamepadButton.B)
  120. {
  121. //! switch confirm button
  122. if (parts[2] == "cross")
  123. {
  124. parts[2] = "circle";
  125. }
  126. //! switch back button
  127. if (parts[2] == "circle")
  128. {
  129. parts[2] = "cross";
  130. }
  131. }
  132. #endif
  133. pIconName.Insert(parts[2]);
  134. }
  135. }
  136. static void GetImagesetAndIconFromInputAction(string pInputAction, int pInputDeviceType, out array<string> pImageSet, out array<string> pIconName)
  137. {
  138. UAInput inp = GetUApi().GetInputByName(pInputAction);
  139. InputUtils.GetImagesetAndIconFromInputAction(inp, pInputDeviceType, pImageSet, pIconName);
  140. }
  141. static string GetRichtextButtonIconFromInputAction(notnull UAInput pInput, string pLocalizedDescription, int pInputDeviceType = EUAINPUT_DEVICE_CONTROLLER, float pScale = ICON_SCALE_NORMAL, bool pVertical = false)
  142. {
  143. array<string> imageSets = new array<string>();
  144. array<string> iconNames = new array<string>();
  145. InputUtils.GetImagesetAndIconFromInputAction(pInput, pInputDeviceType, imageSets, iconNames);
  146. if (imageSets.Count() == 0)
  147. {
  148. return "";
  149. }
  150. string result = string.Format("<image set=\"%1\" name=\"%2\" scale=\"%3\" />", imageSets.Get(0), iconNames.Get(0), pScale);
  151. string divider = " ";
  152. if (pVertical)
  153. {
  154. divider = string.Format("\n%1\n", divider);
  155. }
  156. if (imageSets.Count() > 1)
  157. {
  158. //! first element is already taken
  159. for (int i = 1; i < imageSets.Count(); i++)
  160. {
  161. result = string.Format("%1%2<image set=\"%3\" name=\"%4\" scale=\"%5\" />", result, divider, imageSets.Get(i), iconNames.Get(i), pScale);
  162. }
  163. }
  164. return string.Format("%1 %2", result, pLocalizedDescription);
  165. }
  166. static string GetRichtextButtonIconFromInputAction(string pInputAction, string pLocalizedDescription, int pInputDeviceType = EUAINPUT_DEVICE_CONTROLLER, float pScale = ICON_SCALE_NORMAL, bool pVertical = false)
  167. {
  168. UAInput inp = GetUApi().GetInputByName(pInputAction);
  169. array<string> imageSets = new array<string>();
  170. array<string> iconNames = new array<string>();
  171. InputUtils.GetImagesetAndIconFromInputAction(inp, pInputDeviceType, imageSets, iconNames);
  172. return InputUtils.GetRichtextButtonIconFromInputAction(inp, pLocalizedDescription, pInputDeviceType, pScale, pVertical);
  173. }
  174. static void UpdateConsolePresetID()
  175. {
  176. string profile_name;
  177. GetGame().GetInput().GetProfileName(GetUApi().PresetCurrent(), profile_name);
  178. if (profile_name == PRESET_OLD)
  179. {
  180. m_CurrentPresetIDConsole = VARIANT_OLD;
  181. }
  182. else
  183. {
  184. m_CurrentPresetIDConsole = VARIANT_NEW;
  185. }
  186. }
  187. static int GetConsolePresetID()
  188. {
  189. return m_CurrentPresetIDConsole;
  190. }
  191. static map<int, ref array<int>> GetInputActionSortingMap()
  192. {
  193. return m_InputActionSortingMap;
  194. }
  195. static array<int> GetUnsortedInputActions()
  196. {
  197. return m_UnsortedInputActions;
  198. }
  199. static bool InitInputMetadata()
  200. {
  201. if (!m_InputActionSortingMap)
  202. {
  203. m_InputActionSortingMap = new map<int, ref array<int>>;
  204. TIntArray sorted_actions = new TIntArray;
  205. if (!m_UnsortedInputActions)
  206. {
  207. m_UnsortedInputActions = new array<int>;
  208. }
  209. GetUApi().GetActiveInputs(m_UnsortedInputActions);
  210. UAInput inp;
  211. for (int i = 0; i < GetUApi().SortingCount(); i++)
  212. {
  213. int input_id;
  214. array<int> sorting_content = new array<int>;
  215. for (int j = 0; j < m_UnsortedInputActions.Count(); j++)
  216. {
  217. input_id = m_UnsortedInputActions[j];
  218. inp = GetUApi().GetInputByID(input_id);
  219. if (inp.HasSorting(i))
  220. {
  221. sorting_content.Insert(input_id);
  222. sorted_actions.Insert(input_id);
  223. }
  224. }
  225. if (sorting_content.Count() > 0)
  226. {
  227. sorting_content.Sort();
  228. m_InputActionSortingMap.Insert(i,sorting_content);
  229. }
  230. }
  231. //remove sorted used inputs
  232. int count = sorted_actions.Count();
  233. for (i = 0; i < count; i++)
  234. {
  235. m_UnsortedInputActions.RemoveItem(sorted_actions[i]);
  236. }
  237. return true;
  238. }
  239. return false;
  240. }
  241. }