keybindingelement.c 7.6 KB

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