2015-02-03 00:44:06 +01:00
|
|
|
/* Copyright (C) 2015 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_CINPUT
|
|
|
|
#define INCLUDED_CINPUT
|
2004-10-14 23:01:13 +02:00
|
|
|
|
|
|
|
#include "GUI.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text field where you can input and edit the text.
|
|
|
|
*
|
|
|
|
* It doesn't use IGUITextOwner, because we don't need
|
|
|
|
* any other features than word-wrapping, and we need to be
|
2004-11-25 00:39:52 +01:00
|
|
|
* able to rapidly change the string.
|
|
|
|
*
|
2004-10-14 23:01:13 +02:00
|
|
|
* @see IGUIObject
|
|
|
|
*/
|
|
|
|
class CInput : public IGUIScrollBarOwner
|
|
|
|
{
|
|
|
|
GUI_OBJECT(CInput)
|
|
|
|
|
2005-02-05 08:25:16 +01:00
|
|
|
protected: // forwards
|
|
|
|
struct SRow;
|
|
|
|
|
2004-10-14 23:01:13 +02:00
|
|
|
public:
|
|
|
|
CInput();
|
|
|
|
virtual ~CInput();
|
|
|
|
|
2011-04-28 22:42:11 +02:00
|
|
|
/**
|
|
|
|
* @see IGUIObject#ResetStates()
|
|
|
|
*/
|
2004-10-14 23:01:13 +02:00
|
|
|
virtual void ResetStates() { IGUIScrollBarOwner::ResetStates(); }
|
|
|
|
|
2005-02-05 08:25:16 +01:00
|
|
|
// Check where the mouse is hovering, and get the appropriate text position.
|
|
|
|
// return is the text-position index.
|
2015-08-21 19:08:41 +02:00
|
|
|
int GetMouseHoveringTextPosition() const;
|
2005-02-05 08:25:16 +01:00
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
// Same as above, but only on one row in X, and a given value, not the mouse's.
|
|
|
|
// wanted is filled with x if the row didn't extend as far as the mouse pos.
|
|
|
|
int GetXTextPosition(const std::list<SRow>::const_iterator& c, const float& x, float& wanted) const;
|
2005-02-05 08:25:16 +01:00
|
|
|
|
2004-10-14 23:01:13 +02:00
|
|
|
protected:
|
|
|
|
/**
|
2011-04-28 22:42:11 +02:00
|
|
|
* @see IGUIObject#HandleMessage()
|
2004-10-14 23:01:13 +02:00
|
|
|
*/
|
2015-08-21 19:08:41 +02:00
|
|
|
virtual void HandleMessage(SGUIMessage& Message);
|
2004-10-14 23:01:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle events manually to catch keyboard inputting.
|
|
|
|
*/
|
2006-08-26 23:52:18 +02:00
|
|
|
virtual InReaction ManuallyHandleEvent(const SDL_Event_* ev);
|
2004-10-14 23:01:13 +02:00
|
|
|
|
2011-06-23 12:12:43 +02:00
|
|
|
/**
|
|
|
|
* Handle hotkey events (called by ManuallyHandleEvent)
|
|
|
|
*/
|
|
|
|
virtual InReaction ManuallyHandleHotkeyEvent(const SDL_Event_* ev);
|
|
|
|
|
2011-02-18 23:49:24 +01:00
|
|
|
/**
|
|
|
|
* @see IGUIObject#UpdateCachedSize()
|
|
|
|
*/
|
|
|
|
virtual void UpdateCachedSize();
|
2004-10-14 23:01:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the Text
|
|
|
|
*/
|
|
|
|
virtual void Draw();
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/**
|
|
|
|
* Calculate m_CharacterPosition
|
|
|
|
* the main task for this function is to perfom word-wrapping
|
|
|
|
* You input from which character it has been changed, because
|
|
|
|
* if we add a character to the very last end, we don't want
|
|
|
|
* process everything all over again! Also notice you can
|
|
|
|
* specify a 'to' also, it will only be used though if a '\n'
|
|
|
|
* appears, because then the word-wrapping won't change after
|
|
|
|
* that.
|
|
|
|
*/
|
2015-08-21 19:08:41 +02:00
|
|
|
void UpdateText(int from = 0, int to_before = -1, int to_after = -1);
|
2004-11-25 00:39:52 +01:00
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/**
|
|
|
|
* Delete the current selection. Also places the pointer at the
|
|
|
|
* crack between the two segments kept.
|
|
|
|
*/
|
2005-02-05 08:25:16 +01:00
|
|
|
void DeleteCurSelection();
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/**
|
|
|
|
* Is text selected? It can be denote two ways, m_iBufferPos_Tail
|
|
|
|
* being -1 or the same as m_iBufferPos. This makes for clearer
|
|
|
|
* code.
|
|
|
|
*/
|
2005-02-05 08:25:16 +01:00
|
|
|
bool SelectingText() const;
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Get area of where text can be drawn.
|
2005-02-05 08:25:16 +01:00
|
|
|
float GetTextAreaWidth();
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Called every time the auto-scrolling should be checked.
|
2005-02-05 08:25:16 +01:00
|
|
|
void UpdateAutoScroll();
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Clear composed IME input when supported (SDL2 only).
|
2014-10-04 01:33:41 +02:00
|
|
|
void ClearComposedText();
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Updates the buffer (cursor) position exposed to JS.
|
|
|
|
void UpdateBufferPositionSetting();
|
2004-10-14 23:01:13 +02:00
|
|
|
protected:
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Cursor position
|
|
|
|
int m_iBufferPos;
|
|
|
|
/// Cursor position we started to select from. (-1 if not selecting)
|
|
|
|
/// (NB: Can be larger than m_iBufferPos if selecting from back to front.)
|
|
|
|
int m_iBufferPos_Tail;
|
|
|
|
|
|
|
|
/// If we're composing text with an IME
|
2014-09-22 21:00:20 +02:00
|
|
|
bool m_ComposingText;
|
2015-02-03 00:44:06 +01:00
|
|
|
/// The length and position of the current IME composition
|
2014-09-22 21:00:20 +02:00
|
|
|
int m_iComposedLength, m_iComposedPos;
|
2015-02-03 00:44:06 +01:00
|
|
|
/// The position to insert committed text
|
2014-10-04 01:33:41 +02:00
|
|
|
int m_iInsertPos;
|
2014-09-22 21:00:20 +02:00
|
|
|
|
2004-11-25 00:39:52 +01:00
|
|
|
// the outer vector is lines, and the inner is X positions
|
|
|
|
// in a row. So that we can determine where characters are
|
|
|
|
// placed. It's important because we need to know where the
|
|
|
|
// pointer should be placed when the input control is pressed.
|
|
|
|
struct SRow
|
|
|
|
{
|
2015-02-03 00:44:06 +01:00
|
|
|
int m_ListStart; /// Where does the Row starts
|
|
|
|
std::vector<float> m_ListOfX; /// List of X values for each character.
|
2004-11-25 00:39:52 +01:00
|
|
|
};
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/**
|
|
|
|
* List of rows to ease changing its size, so iterators stay valid.
|
|
|
|
* For one-liners only one row is used.
|
|
|
|
*/
|
2015-08-21 19:08:41 +02:00
|
|
|
std::list<SRow> m_CharacterPositions;
|
2005-02-05 08:25:16 +01:00
|
|
|
|
|
|
|
// *** Things for a multi-lined input control *** //
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/**
|
|
|
|
* When you change row with up/down, and the row you jump to does
|
|
|
|
* not have anything at that X position, then it will keep the
|
|
|
|
* m_WantedX position in mind when switching to the next row.
|
|
|
|
* It will keep on being used until it reach a row which meets the
|
|
|
|
* requirements.
|
|
|
|
* 0.0f means not in use.
|
|
|
|
*/
|
2005-02-05 08:25:16 +01:00
|
|
|
float m_WantedX;
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/**
|
|
|
|
* If we are in the process of selecting a larger selection of text
|
|
|
|
* using the mouse click (hold) and drag, this is true.
|
|
|
|
*/
|
2005-02-05 08:25:16 +01:00
|
|
|
bool m_SelectingText;
|
|
|
|
|
|
|
|
// *** Things for one-line input control *** //
|
|
|
|
float m_HorizontalScroll;
|
2013-01-12 02:20:01 +01:00
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Used to store the previous time for flashing the cursor.
|
2013-01-12 02:20:01 +01:00
|
|
|
double m_PrevTime;
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// Cursor blink rate in seconds, if greater than 0.0.
|
2013-01-12 02:20:01 +01:00
|
|
|
double m_CursorBlinkRate;
|
|
|
|
|
2015-02-03 00:44:06 +01:00
|
|
|
/// If the cursor should be drawn or not.
|
2013-01-12 02:20:01 +01:00
|
|
|
bool m_CursorVisState;
|
2004-10-14 23:01:13 +02:00
|
|
|
};
|
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
#endif // INCLUDED_CINPUT
|