LCOV - code coverage report
Current view: top level - source/gui/ObjectTypes - CInput.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 2 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_CINPUT
      19             : #define INCLUDED_CINPUT
      20             : 
      21             : #include "gui/CGUISetting.h"
      22             : #include "gui/CGUISprite.h"
      23             : #include "gui/CGUIText.h"
      24             : #include "gui/ObjectBases/IGUIObject.h"
      25             : #include "gui/ObjectBases/IGUIScrollBarOwner.h"
      26             : #include "gui/SettingTypes/CGUIColor.h"
      27             : #include "gui/SettingTypes/CGUIString.h"
      28             : #include "maths/Rect.h"
      29             : #include "ps/CStr.h"
      30             : #include "ps/Input.h"
      31             : 
      32             : #include <SDL_keycode.h>
      33             : #include <list>
      34             : #include <vector>
      35             : 
      36             : class CCanvas2D;
      37             : class CGUI;
      38             : 
      39             : /**
      40             :  * Text field where you can input and edit the text.
      41             :  *
      42             :  * It doesn't use IGUITextOwner, because we don't need
      43             :  * any other features than word-wrapping, and we need to be
      44             :  * able to rapidly change the string.
      45             :  */
      46             : class CInput : public IGUIObject, public IGUIScrollBarOwner
      47             : {
      48           0 :     GUI_OBJECT(CInput)
      49             : 
      50             : protected: // forwards
      51             :     struct SRow;
      52             : 
      53             : public:
      54             :     CInput(CGUI& pGUI);
      55             : 
      56             :     /**
      57             :      * @see IGUIObject#ResetStates()
      58             :      */
      59             :     virtual void ResetStates();
      60             : 
      61             :     // Check where the mouse is hovering, and get the appropriate text position.
      62             :     //  return is the text-position index.
      63             :     int GetMouseHoveringTextPosition() const;
      64             : 
      65             :     // Same as above, but only on one row in X, and a given value, not the mouse's.
      66             :     //  wanted is filled with x if the row didn't extend as far as the mouse pos.
      67             :     int GetXTextPosition(const std::list<SRow>::const_iterator& c, const float& x, float& wanted) const;
      68             : 
      69             : protected:
      70             : 
      71             :     void SetupGeneratedPlaceholderText();
      72             : 
      73             :     /**
      74             :      * @see IGUIObject#HandleMessage()
      75             :      */
      76             :     virtual void HandleMessage(SGUIMessage& Message);
      77             : 
      78             :     /**
      79             :      * Handle events manually to catch keyboard inputting.
      80             :      */
      81             :     virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev);
      82             : 
      83             :     /**
      84             :      * Handle events manually to catch keys which change the text.
      85             :      */
      86             :     virtual void ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode);
      87             : 
      88             :     /**
      89             :      * Handle events manually to catch keys which don't change the text.
      90             :      */
      91             :     virtual void ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode);
      92             : 
      93             :     /**
      94             :      * Handle hotkey events (called by ManuallyHandleKeys)
      95             :      */
      96             :     virtual Input::Reaction ManuallyHandleHotkeyEvent(const SDL_Event& ev);
      97             : 
      98             :     /**
      99             :      * @see IGUIObject#HandleSizeChanged()
     100             :      */
     101             :     virtual void HandleSizeChanged();
     102             : 
     103             :     /**
     104             :      * Draws the Text
     105             :      */
     106             :     virtual void Draw(CCanvas2D& canvas);
     107             : 
     108             :     void DrawContent(CCanvas2D& canvas);
     109             : 
     110             :     /**
     111             :      * Calculate m_CharacterPosition
     112             :      * the main task for this function is to perfom word-wrapping
     113             :      * You input from which character it has been changed, because
     114             :      * if we add a character to the very last end, we don't want
     115             :      * process everything all over again! Also notice you can
     116             :      * specify a 'to' also, it will only be used though if a '\n'
     117             :      * appears, because then the word-wrapping won't change after
     118             :      * that.
     119             :      */
     120             :     void UpdateText(int from = 0, int to_before = -1, int to_after = -1);
     121             : 
     122             :     /**
     123             :      * Draws the text generated for placeholder.
     124             :      *
     125             :      * @param canvas Canvas to draw on.
     126             :      * @param clipping Clipping rectangle, don't even add a parameter
     127             :      *        to get no clipping.
     128             :      */
     129             :     virtual void DrawPlaceholderText(CCanvas2D& canvas, const CRect& clipping = CRect());
     130             : 
     131             :     /**
     132             :      * Delete the current selection. Also places the pointer at the
     133             :      * crack between the two segments kept.
     134             :      */
     135             :     void DeleteCurSelection();
     136             : 
     137             :     /**
     138             :      * Is text selected? It can be denote two ways, m_iBufferPos_Tail
     139             :      * being -1 or the same as m_iBufferPos. This makes for clearer
     140             :      * code.
     141             :      */
     142             :     bool SelectingText() const;
     143             : 
     144             :     /// Get area of where text can be drawn.
     145             :     float GetTextAreaWidth();
     146             : 
     147             :     /// Called every time the auto-scrolling should be checked.
     148             :     void UpdateAutoScroll();
     149             : 
     150             :     /// Clear composed IME input when supported (SDL2 only).
     151             :     void ClearComposedText();
     152             : 
     153             :     /// Updates the buffer (cursor) position exposed to JS.
     154             :     void UpdateBufferPositionSetting();
     155             : protected:
     156             :     /// Cursor position
     157             :     int m_iBufferPos;
     158             :     /// Cursor position we started to select from. (-1 if not selecting)
     159             :     /// (NB: Can be larger than m_iBufferPos if selecting from back to front.)
     160             :     int m_iBufferPos_Tail;
     161             : 
     162             :     /// If we're composing text with an IME
     163             :     bool m_ComposingText;
     164             :     /// The length and position of the current IME composition
     165             :     int m_iComposedLength, m_iComposedPos;
     166             :     /// The position to insert committed text
     167             :     int m_iInsertPos;
     168             : 
     169             :     // the outer vector is lines, and the inner is X positions
     170             :     //  in a row. So that we can determine where characters are
     171             :     //  placed. It's important because we need to know where the
     172             :     //  pointer should be placed when the input control is pressed.
     173           0 :     struct SRow
     174             :     {
     175             :         // Where the Row starts
     176             :         int m_ListStart;
     177             : 
     178             :         // List of X values for each character.
     179             :         std::vector<float> m_ListOfX;
     180             :     };
     181             : 
     182             :     /**
     183             :      * List of rows to ease changing its size, so iterators stay valid.
     184             :      * For one-liners only one row is used.
     185             :      */
     186             :     std::list<SRow> m_CharacterPositions;
     187             : 
     188             :     // *** Things for a multi-lined input control *** //
     189             : 
     190             :     /**
     191             :      * When you change row with up/down, and the row you jump to does
     192             :      * not have anything at that X position, then it will keep the
     193             :      * m_WantedX position in mind when switching to the next row.
     194             :      * It will keep on being used until it reach a row which meets the
     195             :      * requirements.
     196             :      * 0.0f means not in use.
     197             :      */
     198             :     float m_WantedX;
     199             : 
     200             :     /**
     201             :      * If we are in the process of selecting a larger selection of text
     202             :      * using the mouse click (hold) and drag, this is true.
     203             :      */
     204             :     bool m_SelectingText;
     205             : 
     206             :     /**
     207             :      * Whether the cached text is currently valid (if not then SetupText will be called by Draw)
     208             :      */
     209             :     bool m_GeneratedPlaceholderTextValid;
     210             : 
     211             :     CGUIText m_GeneratedPlaceholderText;
     212             : 
     213             :     // *** Things for one-line input control *** //
     214             :     float m_HorizontalScroll;
     215             : 
     216             :     /// Used to store the previous time for flashing the cursor.
     217             :     double m_PrevTime;
     218             : 
     219             :     /// Cursor blink rate in seconds, if greater than 0.0.
     220             :     double m_CursorBlinkRate;
     221             : 
     222             :     /// If the cursor should be drawn or not.
     223             :     bool m_CursorVisState;
     224             : 
     225             :     static const CStr EventNameTextEdit;
     226             :     static const CStr EventNamePress;
     227             :     static const CStr EventNameTab;
     228             : 
     229             :     CGUISimpleSetting<std::int32_t> m_BufferPosition;
     230             :     CGUISimpleSetting<float> m_BufferZone;
     231             :     CGUISimpleSetting<CStrW> m_Caption;
     232             :     CGUISimpleSetting<CGUIString> m_PlaceholderText;
     233             :     CGUISimpleSetting<CStrW> m_Font;
     234             :     CGUISimpleSetting<CStrW> m_MaskChar;
     235             :     CGUISimpleSetting<bool> m_Mask;
     236             :     CGUISimpleSetting<std::int32_t> m_MaxLength;
     237             :     CGUISimpleSetting<bool> m_MultiLine;
     238             :     CGUISimpleSetting<bool> m_Readonly;
     239             :     CGUISimpleSetting<bool> m_ScrollBar;
     240             :     CGUISimpleSetting<CStr> m_ScrollBarStyle;
     241             :     CGUISimpleSetting<CGUISpriteInstance> m_Sprite;
     242             :     CGUISimpleSetting<CGUISpriteInstance> m_SpriteOverlay;
     243             :     CGUISimpleSetting<CGUISpriteInstance> m_SpriteSelectArea;
     244             :     CGUISimpleSetting<CGUIColor> m_TextColor;
     245             :     CGUISimpleSetting<CGUIColor> m_TextColorSelected;
     246             :     CGUISimpleSetting<CGUIColor> m_PlaceholderColor;
     247             : };
     248             : 
     249             : #endif // INCLUDED_CINPUT

Generated by: LCOV version 1.16