Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.9 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose: QWERTY keyboard text entry method for Steam controller
  4. //=============================================================================//
  5. #ifndef PANORAMA_TEXTINPUT_DUALTOUCH_H
  6. #define PANORAMA_TEXTINPUT_DUALTOUCH_H
  7. #include "panorama/textinput/textinput.h"
  8. #include "panorama/controls/panel2d.h"
  9. #include "panorama/controls/label.h"
  10. #include "panorama/input/iuiinput.h"
  11. #include "mathlib/beziercurve.h"
  12. #include "tier1/utlptr.h"
  13. #include "panorama/uischeduleddel.h"
  14. namespace panorama
  15. {
  16. // Forward declaration
  17. class CTextInputDualTouch;
  18. class CTextEntry;
  19. class ITextInputSuggest;
  20. class CLabel;
  21. // this weights the frequency of words typed without any possible typos above
  22. // autocorrected words, as otherwise if you legitimately type 'test' it'll
  23. // auto-correct into 'rest'
  24. static const float k_flExactWordFrequencyWeight = 1.2f;
  25. class CTextInputDualTouch : public panorama::CTextInputHandler
  26. {
  27. DECLARE_PANEL2D( CTextInputDualTouch, panorama::CPanel2D );
  28. public:
  29. // Constructor
  30. CTextInputDualTouch( panorama::IUIWindow *pParent, const CTextInputHandlerSettings &settings, ITextInputControl *pTextControl );
  31. CTextInputDualTouch( panorama::CPanel2D *parent, const CTextInputHandlerSettings &settings, ITextInputControl *pTextControl );
  32. // Destructor
  33. ~CTextInputDualTouch();
  34. // CTextInputHandler overrides
  35. virtual void OpenHandler() OVERRIDE;
  36. virtual void CloseHandlerImpl( bool bCommitText ) OVERRIDE;
  37. virtual ETextInputHandlerType_t GetType() OVERRIDE;
  38. virtual ITextInputControl *GetControlInterface() OVERRIDE;
  39. virtual void SuggestWord( const wchar_t *pwch, int ich ) OVERRIDE;
  40. virtual void SetYButtonAction( const char *pchLabel, IUIEvent *pEvent ) OVERRIDE;
  41. static void GetSupportedLanguages( CUtlVector<ELanguage> &vecLangs );
  42. private:
  43. static const int k_DualTouchRowCount = 4;
  44. static const int k_DualTouchColumnCount = 11;
  45. static const int k_SuggestionCount = 4;
  46. enum EDualTouchModifier_t
  47. {
  48. k_EDualTouchModifierNone = 0,
  49. k_EDualTouchModifierShift = 1,
  50. k_EDualTouchModifierAlt = 2,
  51. k_EDualTouchModifierCount = 3,
  52. };
  53. class CTouchPad
  54. {
  55. public:
  56. bool Initialize( CTextInputDualTouch *pParent, const char *pointerID, const char *padID,
  57. IUIEngine::EHapticFeedbackPosition eHapticsPosition );
  58. void UpdatePointerState( bool bPointersEnabled, uint32 nTextureID );
  59. void OnTouch( void );
  60. void OnRelease( void );
  61. bool OnMove( float touchX, float touchY );
  62. void OnButtonDown( void );
  63. SteamPadPointer_t m_renderPointerState;
  64. CPanel2D *m_pPointerPanel;
  65. CPanel2D *m_pPadPanel;
  66. CPanel2D *m_pHoverKey; // what key are we currently hovering over?
  67. CPanel2D *m_pLastHoverKey; // what was the last key we were hovering over? this will either match m_pHoverKey or have the last value m_pHoverKey had if its currently nullptr
  68. CUtlVector< IUIPanel * > m_vecTouchKeys;
  69. bool m_bFingerOnPad;
  70. float m_hoverX;
  71. float m_hoverY;
  72. CTextInputDualTouch *m_pTextInputDualTouch;
  73. IUIEngine::EHapticFeedbackPosition m_eHapticsPosition;
  74. };
  75. void Initialize( const CTextInputHandlerSettings &settings, ITextInputControl *pTextControl );
  76. void SetMode( ETextInputMode_t mode );
  77. // CPanel2D overrides
  78. virtual bool OnGamePadUp( const panorama::GamePadData_t &code ) OVERRIDE;
  79. virtual bool OnGamePadDown( const panorama::GamePadData_t &code ) OVERRIDE;
  80. virtual bool OnGamePadAnalog( const panorama::GamePadData_t &code ) OVERRIDE;
  81. virtual bool OnKeyTyped( const KeyData_t &unichar ) OVERRIDE;
  82. virtual bool OnKeyDown( const KeyData_t &code ) OVERRIDE;
  83. virtual bool OnKeyUp( const KeyData_t &code ) OVERRIDE;
  84. void UpdateSteamPadPointers( void );
  85. bool OnPropertyTransitionEnd( const CPanelPtr< IUIPanel > &pPanel, CStyleSymbol prop );
  86. bool OnRemoveStyleFromLinkedKeys( CPanelPtr<CPanel2D> pPanel, const char *pszStyle );
  87. bool TouchPadClicked( CTouchPad* pTouchPad );
  88. // Listen for focus lost
  89. bool HandleInputFocusLost( const panorama::CPanelPtr< panorama::IUIPanel > &ptrPanel );
  90. bool OnActiveControllerTypeChanged( EActiveControllerType eActiveControllerType );
  91. bool BConvertNextSpaceToPeriod( void );
  92. bool TypeWchar( uchar16 wch, const char *pUTFf8Char = NULL );
  93. bool TypeKeyDown( panorama::KeyCode eCode );
  94. bool SwitchLanguage( void );
  95. bool LoadInputConfigurationFile( ELanguage language );
  96. bool LoadInputConfigurationFile( char const *szConfigFile, const char *szConfigRootDir );
  97. bool LoadConfigurationBuffer( char const *pszIncoming );
  98. void SetModifierKeyState( EDualTouchModifier_t modifier, bool bIsButtonPressEvent );
  99. EDualTouchModifier_t CalculateDesiredModifierState() const;
  100. void ApplyCurrentModifierLayout();
  101. void TouchKeyClicked( CPanel2D *pTouchKey, CTouchPad *pTouchPad );
  102. // Process scheduled key repeat
  103. void ScheduleKeyRepeats( panorama::GamePadCode eCode );
  104. void CancelOutstandingRepeats() { ScheduleKeyRepeats( XK_NULL ); }
  105. void ScheduledKeyRepeatFunction();
  106. // auto-suggestion
  107. void ClearSuggestionVisual( void )
  108. {
  109. for ( int i = 0; i < k_SuggestionCount; i++ )
  110. {
  111. m_pSuggestionLabels[i]->SetText( "" );
  112. }
  113. }
  114. void ClearSuggestionState( bool bFlush = true )
  115. {
  116. // flush what we have into the buffer
  117. if ( bFlush && m_vecPossibleWordsBeingTyped.Count() )
  118. {
  119. CStrAutoEncode s( m_vecPossibleWordsBeingTyped[0] );
  120. m_pTextInputControl->InsertCharactersAtCursor( s.ToWString(), V_wcslen( s.ToWString() ) );
  121. }
  122. m_vecPossibleWordsBeingTyped.Purge();
  123. ClearSuggestionVisual();
  124. UpdateTextPreview();
  125. }
  126. void ProcessSuggestions( void );
  127. void ApplySuggestion( int iSuggestion );
  128. void UpdateTextPreview( void );
  129. bool PerformBackspace( void );
  130. void CursorMove( const panorama::GamePadData_t &code );
  131. void DisableCursorMode( void );
  132. private:
  133. void ChangeTouchkeyStyle( CPanel2D *pTouchKey, const char *pchStyle, bool bAddStyle );
  134. public:
  135. ITextInputControl *m_pTextInputControl; // control interface for moving text input between a control and daisy wheel
  136. CPanel2D *m_pBodyContainer;
  137. IUIEvent *m_pYbuttonAction; // the action to fire if the Y button is hit
  138. CLabel *m_pYButtonText; // label for ybutton text
  139. CLabel *m_pLang;
  140. CLabel *m_pSuggestionLabels[k_SuggestionCount];
  141. CUtlVector< CUtlString > m_vecPossibleWordsBeingTyped;
  142. ELanguage m_language; // currently loaded language
  143. ITextInputSuggest *m_pSuggest; // suggestion engine
  144. CTextEntry *m_pTextPreview;
  145. bool m_bDoubleSpaceToDotSpace;
  146. bool m_bOnlySpacesEnteredSinceBackspace;
  147. ETextInputMode_t m_mode;
  148. bool m_bAutoComplete;
  149. bool m_bDisplaySuggestions;
  150. bool m_bHidePreviewField;
  151. bool m_bAutoCaps;
  152. // This controls the direct-rendered steampad crosshairs; we can only
  153. // have them up while we're not animating around, otherwise use higher-latency
  154. // panel crosshairs
  155. bool m_bSteamPadPointersEnabled;
  156. IImageSource *m_pSteamPadPointerImage;
  157. CTouchPad m_leftTouchPad;
  158. CTouchPad m_rightTouchPad;
  159. CUtlVector< CTouchPad * > m_vecTouchPads;
  160. uchar32 m_keyLayout[k_DualTouchColumnCount][k_DualTouchRowCount][k_EDualTouchModifierCount];
  161. EDualTouchModifier_t m_currentModifier;
  162. int m_iCharactersTypedSinceModifierStateChanged;
  163. // Tracking key repeats
  164. CCubicBezierCurve< Vector2D > m_repeatCurve; // Curve for key repeats
  165. double m_repeatStartTime; // Time when the key was initially pressed
  166. double m_repeatNextTime; // Time when the key will repeat next
  167. panorama::GamePadCode m_repeatGamePadCode; // Which key was pressed (low level, for key-up tracking)
  168. uint32 m_repeatCounter; // How many key repeats have happened
  169. panorama::CUIScheduledDel m_repeatFunction; // Scheduled function triggering key repeats
  170. bool m_bCursorMode;
  171. CPanel2D *m_pCursorKey;
  172. bool m_bUseTouchPads;
  173. bool m_bModifierKeysHeld[ k_EDualTouchModifierCount ];
  174. };
  175. } // namespace panorama
  176. #endif // PANORAMA_TEXTINPUT_DUALTOUCH_H