keybindingelementnew.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. class KeybindingElementNew extends ScriptedWidgetEventHandler
  2. {
  3. protected KeybindingsContainer m_Container;
  4. protected Widget m_Root;
  5. protected TextWidget m_ElementName;
  6. protected TextWidget m_ElementModifier;
  7. protected ButtonWidget m_PrimaryBindButton;
  8. protected ButtonWidget m_AlternativeBindButton;
  9. protected Widget m_PrimaryClear;
  10. protected Widget m_AlternativeClear;
  11. protected int m_ElementIndex;
  12. protected bool m_IsEdited;
  13. protected bool m_IsAlternateEdited;
  14. protected ref array<int> m_CustomBind;
  15. protected ref array<int> m_CustomAlternateBind;
  16. protected ref Timer m_EntryTimer = new Timer( CALL_CATEGORY_GUI );
  17. void KeybindingElementNew( int key_index, Widget parent, KeybindingsContainer group )
  18. {
  19. m_Root = GetGame().GetWorkspace().CreateWidgets( GetLayoutName(), parent );
  20. m_ElementName = TextWidget.Cast( m_Root.FindAnyWidget( "setting_label" ) );
  21. m_ElementModifier = TextWidget.Cast( m_Root.FindAnyWidget( "modifier_label" ) );
  22. m_PrimaryBindButton = ButtonWidget.Cast( m_Root.FindAnyWidget( "primary_bind" ) );
  23. m_AlternativeBindButton = ButtonWidget.Cast( m_Root.FindAnyWidget( "alternative_bind" ) );
  24. m_PrimaryClear = m_Root.FindAnyWidget( "primary_clear" );
  25. m_AlternativeClear = m_Root.FindAnyWidget( "alternative_clear" );
  26. m_Container = group;
  27. m_ElementIndex = key_index;
  28. Reload();
  29. m_Root.SetHandler( this );
  30. }
  31. string GetLayoutName()
  32. {
  33. return "gui/layouts/new_ui/options/keybindings_selectors/keybinding_option.layout";
  34. }
  35. bool IsChanged()
  36. {
  37. return m_IsEdited;
  38. }
  39. bool IsAlternateChanged()
  40. {
  41. return m_IsAlternateEdited;
  42. }
  43. array<int> GetChangedBinds()
  44. {
  45. return m_CustomBind;
  46. }
  47. array<int> GetChangedAlternateBinds()
  48. {
  49. return m_CustomAlternateBind;
  50. }
  51. // assembly all related binding at widget element
  52. void SetElementTitle( ButtonWidget btnWidget, UAInput pInput, int iDeviceFlags )
  53. {
  54. string output;
  55. int a, i, countbind = 0;
  56. for( a = 0; a < pInput.AlternativeCount(); a++ )
  57. {
  58. pInput.SelectAlternative(a);
  59. if( pInput.IsCombo() )
  60. {
  61. if( pInput.BindingCount() > 0 )
  62. {
  63. if( pInput.Binding(0) != 0 && pInput.CheckBindDevice(0,iDeviceFlags) )
  64. {
  65. if( countbind > 0 )
  66. output += ", ";
  67. output += GetUApi().GetButtonName( pInput.Binding(0) );
  68. countbind++;
  69. for( i = 1; i < pInput.BindingCount(); i++ )
  70. {
  71. if( pInput.Binding(i) != 0 )
  72. {
  73. output += " + " + GetUApi().GetButtonName( pInput.Binding(i) );
  74. countbind++;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. else
  81. {
  82. if( pInput.BindingCount() > 0 )
  83. {
  84. if( pInput.Binding(0) != 0 && pInput.CheckBindDevice(0,iDeviceFlags) )
  85. {
  86. if( countbind > 0 )
  87. output += ", ";
  88. output += GetUApi().GetButtonName( pInput.Binding(0) );
  89. countbind++;
  90. }
  91. }
  92. }
  93. }
  94. // nothing may be available - we do not want "null" or "0" in string
  95. if( countbind > 0 )
  96. btnWidget.SetText(output);
  97. else
  98. btnWidget.SetText("");
  99. }
  100. void Reload()
  101. {
  102. UAInput input = GetUApi().GetInputByID( m_ElementIndex );
  103. m_IsEdited = false;
  104. m_IsAlternateEdited = false;
  105. m_CustomBind = null;
  106. m_CustomAlternateBind = null;
  107. if( input.IsLimited() )
  108. {
  109. if( input.IsPressLimit() )
  110. {
  111. m_ElementModifier.SetText( "#keybind_press" );
  112. }
  113. if( input.IsReleaseLimit() )
  114. {
  115. m_ElementModifier.SetText( "#keybind_release" );
  116. }
  117. if( input.IsHoldLimit() )
  118. {
  119. m_ElementModifier.SetText( "#keybind_hold" );
  120. }
  121. if( input.IsHoldBeginLimit() )
  122. {
  123. m_ElementModifier.SetText( "#keybind_holdbegin" );
  124. }
  125. if( input.IsClickLimit() )
  126. {
  127. m_ElementModifier.SetText( "#keybind_click" );
  128. }
  129. if( input.IsDoubleClickLimit() )
  130. {
  131. m_ElementModifier.SetText( "#keybind_doubletap" );
  132. }
  133. }
  134. else
  135. {
  136. m_ElementModifier.SetText( "" );
  137. }
  138. string option_text;
  139. string name;
  140. GetGame().GetInput().GetActionDesc( m_ElementIndex, option_text );
  141. m_ElementName.SetText( option_text );
  142. // column #1 :: keyboard + mouse
  143. SetElementTitle(m_PrimaryBindButton, input, EUAINPUT_DEVICE_KEYBOARDMOUSE);
  144. // column #2 :: controller
  145. SetElementTitle(m_AlternativeBindButton, input, EUAINPUT_DEVICE_CONTROLLER);
  146. }
  147. void Reload( array<int> custom_binds, bool is_alternate )
  148. {
  149. string output;
  150. if( custom_binds.Count() > 1 )
  151. {
  152. if( custom_binds.Get( 0 ) != 0 )
  153. output = GetUApi().GetButtonName( custom_binds.Get( 0 ) );
  154. for( int i = 1; i < custom_binds.Count(); i++ )
  155. {
  156. if( custom_binds.Get( i ) != 0 )
  157. output += " + " + GetUApi().GetButtonName( custom_binds.Get( i ) );
  158. }
  159. }
  160. else if( custom_binds.Count() > 0 )
  161. {
  162. if( custom_binds.Get( 0 ) != 0 )
  163. output = GetUApi().GetButtonName( custom_binds.Get( 0 ) );
  164. }
  165. if( is_alternate )
  166. {
  167. m_CustomAlternateBind = custom_binds;
  168. m_IsAlternateEdited = true;
  169. m_AlternativeBindButton.SetText( output );
  170. }
  171. else
  172. {
  173. m_CustomBind = custom_binds;
  174. m_IsEdited = true;
  175. m_PrimaryBindButton.SetText( output );
  176. }
  177. }
  178. void StartEnteringKeybind()
  179. {
  180. m_Container.StartEnteringKeybind( m_ElementIndex );
  181. m_PrimaryBindButton.SetText( "#layout_keybinding_new_keybind" );
  182. }
  183. void CancelEnteringKeybind()
  184. {
  185. Reload();
  186. }
  187. void StartEnteringAlternateKeybind()
  188. {
  189. m_Container.StartEnteringAlternateKeybind( m_ElementIndex );
  190. m_AlternativeBindButton.SetText( "#layout_keybinding_new_keybind" );
  191. }
  192. void CancelEnteringAlternateKeybind()
  193. {
  194. Reload();
  195. }
  196. override bool OnClick( Widget w, int x, int y, int button )
  197. {
  198. if( !m_Container.IsEnteringKeyBind() )
  199. {
  200. if( w == m_PrimaryBindButton )
  201. {
  202. m_EntryTimer.Run( 0.01, this, "StartEnteringKeybind" );
  203. }
  204. if( w == m_AlternativeBindButton )
  205. {
  206. m_EntryTimer.Run( 0.01, this, "StartEnteringAlternateKeybind" );
  207. }
  208. }
  209. return false;
  210. }
  211. override bool OnMouseButtonUp( Widget w, int x, int y, int button )
  212. {
  213. if( w == m_PrimaryClear )
  214. {
  215. m_IsEdited = true;
  216. m_CustomBind = new array<int>;
  217. m_PrimaryBindButton.SetText( "" );
  218. m_Container.ClearKeybind( m_ElementIndex );
  219. }
  220. if( w == m_AlternativeClear )
  221. {
  222. m_IsAlternateEdited = true;
  223. m_CustomAlternateBind = new array<int>;
  224. m_AlternativeBindButton.SetText( "" );
  225. m_Container.ClearAlternativeKeybind( m_ElementIndex );
  226. }
  227. return false;
  228. }
  229. override bool OnMouseEnter( Widget w, int x, int y )
  230. {
  231. if( w == m_PrimaryBindButton || w == m_PrimaryClear )
  232. {
  233. m_PrimaryBindButton.SetColor( ARGBF( 1, 1, 0, 0 ) );
  234. m_PrimaryClear.Show( true );
  235. m_PrimaryClear.Update();
  236. m_AlternativeClear.Show( false );
  237. return true;
  238. }
  239. else if( w == m_AlternativeBindButton || w == m_AlternativeClear )
  240. {
  241. m_AlternativeBindButton.SetColor( ARGBF( 1, 1, 0, 0 ) );
  242. m_PrimaryClear.Show( false );
  243. m_AlternativeClear.Show( true );
  244. m_AlternativeClear.Update();
  245. return true;
  246. }
  247. else
  248. {
  249. m_PrimaryBindButton.SetColor( ARGBF( 0, 0, 0, 0 ) );
  250. m_AlternativeBindButton.SetColor( ARGBF( 1, 0, 0, 0 ) );
  251. m_PrimaryClear.Show( false );
  252. m_AlternativeClear.Show( false );
  253. }
  254. return false;
  255. }
  256. override bool OnMouseLeave( Widget w, Widget enterW, int x, int y )
  257. {
  258. if( w == m_PrimaryClear || w == m_PrimaryBindButton )
  259. {
  260. if( enterW != m_PrimaryClear && enterW != m_PrimaryBindButton )
  261. {
  262. m_PrimaryClear.Show( false );
  263. m_PrimaryBindButton.SetColor( ARGBF( 1, 0, 0, 0 ) );
  264. }
  265. }
  266. if( w == m_AlternativeClear || w == m_AlternativeBindButton )
  267. {
  268. if( enterW != m_AlternativeClear && enterW != m_AlternativeBindButton )
  269. {
  270. m_AlternativeClear.Show( false );
  271. m_AlternativeBindButton.SetColor( ARGBF( 1, 0, 0, 0 ) );
  272. }
  273. }
  274. return false;
  275. }
  276. }