123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- class KeybindingElementNew extends ScriptedWidgetEventHandler
- {
- protected KeybindingsContainer m_Container;
-
- protected Widget m_Root;
- protected TextWidget m_ElementName;
- protected TextWidget m_ElementModifier;
- protected ButtonWidget m_PrimaryBindButton;
- protected ButtonWidget m_AlternativeBindButton;
- protected Widget m_PrimaryClear;
- protected Widget m_AlternativeClear;
-
- protected int m_ElementIndex;
- protected bool m_IsEdited;
- protected bool m_IsAlternateEdited;
-
- protected ref array<int> m_CustomBind;
- protected ref array<int> m_CustomAlternateBind;
-
- protected ref Timer m_EntryTimer = new Timer( CALL_CATEGORY_GUI );
-
- void KeybindingElementNew( int key_index, Widget parent, KeybindingsContainer group )
- {
- m_Root = GetGame().GetWorkspace().CreateWidgets( GetLayoutName(), parent );
- m_ElementName = TextWidget.Cast( m_Root.FindAnyWidget( "setting_label" ) );
- m_ElementModifier = TextWidget.Cast( m_Root.FindAnyWidget( "modifier_label" ) );
- m_PrimaryBindButton = ButtonWidget.Cast( m_Root.FindAnyWidget( "primary_bind" ) );
- m_AlternativeBindButton = ButtonWidget.Cast( m_Root.FindAnyWidget( "alternative_bind" ) );
- m_PrimaryClear = m_Root.FindAnyWidget( "primary_clear" );
- m_AlternativeClear = m_Root.FindAnyWidget( "alternative_clear" );
-
- m_Container = group;
- m_ElementIndex = key_index;
-
- Reload();
- m_Root.SetHandler( this );
- }
-
- string GetLayoutName()
- {
- return "gui/layouts/new_ui/options/keybindings_selectors/keybinding_option.layout";
- }
-
- bool IsChanged()
- {
- return m_IsEdited;
- }
-
- bool IsAlternateChanged()
- {
- return m_IsAlternateEdited;
- }
-
- array<int> GetChangedBinds()
- {
- return m_CustomBind;
- }
-
- array<int> GetChangedAlternateBinds()
- {
- return m_CustomAlternateBind;
- }
-
- // assembly all related binding at widget element
- void SetElementTitle( ButtonWidget btnWidget, UAInput pInput, int iDeviceFlags )
- {
- string output;
- int a, i, countbind = 0;
-
- for( a = 0; a < pInput.AlternativeCount(); a++ )
- {
- pInput.SelectAlternative(a);
- if( pInput.IsCombo() )
- {
- if( pInput.BindingCount() > 0 )
- {
- if( pInput.Binding(0) != 0 && pInput.CheckBindDevice(0,iDeviceFlags) )
- {
- if( countbind > 0 )
- output += ", ";
-
- output += GetUApi().GetButtonName( pInput.Binding(0) );
- countbind++;
-
- for( i = 1; i < pInput.BindingCount(); i++ )
- {
- if( pInput.Binding(i) != 0 )
- {
- output += " + " + GetUApi().GetButtonName( pInput.Binding(i) );
- countbind++;
- }
- }
-
- }
- }
- }
- else
- {
- if( pInput.BindingCount() > 0 )
- {
- if( pInput.Binding(0) != 0 && pInput.CheckBindDevice(0,iDeviceFlags) )
- {
- if( countbind > 0 )
- output += ", ";
- output += GetUApi().GetButtonName( pInput.Binding(0) );
- countbind++;
- }
- }
- }
- }
- // nothing may be available - we do not want "null" or "0" in string
- if( countbind > 0 )
- btnWidget.SetText(output);
- else
- btnWidget.SetText("");
- }
-
- void Reload()
- {
- UAInput input = GetUApi().GetInputByID( m_ElementIndex );
- m_IsEdited = false;
- m_IsAlternateEdited = false;
- m_CustomBind = null;
- m_CustomAlternateBind = null;
- if( input.IsLimited() )
- {
- if( input.IsPressLimit() )
- {
- m_ElementModifier.SetText( "#keybind_press" );
- }
- if( input.IsReleaseLimit() )
- {
- m_ElementModifier.SetText( "#keybind_release" );
- }
- if( input.IsHoldLimit() )
- {
- m_ElementModifier.SetText( "#keybind_hold" );
- }
- if( input.IsHoldBeginLimit() )
- {
- m_ElementModifier.SetText( "#keybind_holdbegin" );
- }
- if( input.IsClickLimit() )
- {
- m_ElementModifier.SetText( "#keybind_click" );
- }
- if( input.IsDoubleClickLimit() )
- {
- m_ElementModifier.SetText( "#keybind_doubletap" );
- }
- }
- else
- {
- m_ElementModifier.SetText( "" );
- }
-
- string option_text;
- string name;
-
- GetGame().GetInput().GetActionDesc( m_ElementIndex, option_text );
- m_ElementName.SetText( option_text );
-
- // column #1 :: keyboard + mouse
- SetElementTitle(m_PrimaryBindButton, input, EUAINPUT_DEVICE_KEYBOARDMOUSE);
- // column #2 :: controller
- SetElementTitle(m_AlternativeBindButton, input, EUAINPUT_DEVICE_CONTROLLER);
- }
-
- void Reload( array<int> custom_binds, bool is_alternate )
- {
- string output;
- if( custom_binds.Count() > 1 )
- {
- if( custom_binds.Get( 0 ) != 0 )
- output = GetUApi().GetButtonName( custom_binds.Get( 0 ) );
- for( int i = 1; i < custom_binds.Count(); i++ )
- {
- if( custom_binds.Get( i ) != 0 )
- output += " + " + GetUApi().GetButtonName( custom_binds.Get( i ) );
- }
- }
- else if( custom_binds.Count() > 0 )
- {
- if( custom_binds.Get( 0 ) != 0 )
- output = GetUApi().GetButtonName( custom_binds.Get( 0 ) );
- }
-
- if( is_alternate )
- {
- m_CustomAlternateBind = custom_binds;
- m_IsAlternateEdited = true;
- m_AlternativeBindButton.SetText( output );
- }
- else
- {
- m_CustomBind = custom_binds;
- m_IsEdited = true;
- m_PrimaryBindButton.SetText( output );
- }
- }
-
- void StartEnteringKeybind()
- {
- m_Container.StartEnteringKeybind( m_ElementIndex );
- m_PrimaryBindButton.SetText( "#layout_keybinding_new_keybind" );
- }
-
- void CancelEnteringKeybind()
- {
- Reload();
- }
-
- void StartEnteringAlternateKeybind()
- {
- m_Container.StartEnteringAlternateKeybind( m_ElementIndex );
- m_AlternativeBindButton.SetText( "#layout_keybinding_new_keybind" );
- }
-
- void CancelEnteringAlternateKeybind()
- {
- Reload();
- }
-
- override bool OnClick( Widget w, int x, int y, int button )
- {
- if( !m_Container.IsEnteringKeyBind() )
- {
- if( w == m_PrimaryBindButton )
- {
- m_EntryTimer.Run( 0.01, this, "StartEnteringKeybind" );
- }
- if( w == m_AlternativeBindButton )
- {
- m_EntryTimer.Run( 0.01, this, "StartEnteringAlternateKeybind" );
- }
- }
- return false;
- }
-
- override bool OnMouseButtonUp( Widget w, int x, int y, int button )
- {
- if( w == m_PrimaryClear )
- {
- m_IsEdited = true;
- m_CustomBind = new array<int>;
- m_PrimaryBindButton.SetText( "" );
- m_Container.ClearKeybind( m_ElementIndex );
- }
- if( w == m_AlternativeClear )
- {
- m_IsAlternateEdited = true;
- m_CustomAlternateBind = new array<int>;
- m_AlternativeBindButton.SetText( "" );
- m_Container.ClearAlternativeKeybind( m_ElementIndex );
- }
- return false;
- }
-
- override bool OnMouseEnter( Widget w, int x, int y )
- {
- if( w == m_PrimaryBindButton || w == m_PrimaryClear )
- {
- m_PrimaryBindButton.SetColor( ARGBF( 1, 1, 0, 0 ) );
- m_PrimaryClear.Show( true );
- m_PrimaryClear.Update();
- m_AlternativeClear.Show( false );
- return true;
- }
- else if( w == m_AlternativeBindButton || w == m_AlternativeClear )
- {
- m_AlternativeBindButton.SetColor( ARGBF( 1, 1, 0, 0 ) );
- m_PrimaryClear.Show( false );
- m_AlternativeClear.Show( true );
- m_AlternativeClear.Update();
- return true;
- }
- else
- {
- m_PrimaryBindButton.SetColor( ARGBF( 0, 0, 0, 0 ) );
- m_AlternativeBindButton.SetColor( ARGBF( 1, 0, 0, 0 ) );
- m_PrimaryClear.Show( false );
- m_AlternativeClear.Show( false );
- }
- return false;
- }
-
- override bool OnMouseLeave( Widget w, Widget enterW, int x, int y )
- {
- if( w == m_PrimaryClear || w == m_PrimaryBindButton )
- {
- if( enterW != m_PrimaryClear && enterW != m_PrimaryBindButton )
- {
- m_PrimaryClear.Show( false );
- m_PrimaryBindButton.SetColor( ARGBF( 1, 0, 0, 0 ) );
- }
- }
- if( w == m_AlternativeClear || w == m_AlternativeBindButton )
- {
- if( enterW != m_AlternativeClear && enterW != m_AlternativeBindButton )
- {
- m_AlternativeClear.Show( false );
- m_AlternativeBindButton.SetColor( ARGBF( 1, 0, 0, 0 ) );
- }
- }
- return false;
- }
- }
|