Implement placeholder text for input fields and get rid of hack with mod filter

different font colour
no need to delete the text, when one wants to write there something
text displayed is not a value so field is technically empty
will disappear when user writes some character
will appear when user deletes all characters
we can get rid of text value "Filter" in mod selection screen and ugly
hack around it

Differential Revision: D2460
Comments by: vladislavbelov, elexis
This was SVN commit r24433.
This commit is contained in:
Angen 2020-12-21 09:04:12 +00:00
parent c72861b708
commit 37e08a4ffb
6 changed files with 65 additions and 2 deletions

View file

@ -71,6 +71,8 @@ ex_settings =
attribute maxwidth { xsd:decimal }? &
attribute multiline { bool }?&
attribute offset { pos }?&
attribute placeholder_text { text }?&
attribute placeholder_color { ccolor }?&
attribute readonly { bool }?&
attribute scrollbar { bool }?&
attribute scrollbar_style { text }?&

View file

@ -434,6 +434,14 @@
<ref name="ccolor"/>
</attribute>
</optional>
<optional>
<attribute name="placeholder_text"/>
</optional>
<optional>
<attribute name="placeholder_color">
<ref name="ccolor"/>
</attribute>
</optional>
<optional>
<attribute name="text_align">
<ref name="align"/>

View file

@ -104,7 +104,6 @@ function validateMods()
function initGUIFilters()
{
Engine.GetGUIObjectByName("negateFilter").checked = false;
Engine.GetGUIObjectByName("modGenericFilter").caption = translate("Filter");
displayModLists();
}
@ -251,7 +250,6 @@ function filterMod(folder)
let searchText = Engine.GetGUIObjectByName("modGenericFilter").caption;
if (searchText &&
searchText != translate("Filter") &&
folder.indexOf(searchText) == -1 &&
mod.name.indexOf(searchText) == -1 &&
mod.label.indexOf(searchText) == -1 &&

View file

@ -18,9 +18,11 @@
type="input"
style="ModernInput"
size="16 0 176 100%"
placeholder_color="gray"
>
<action on="Press">applyFilters();</action>
<action on="TextEdit">applyFilters();</action>
<translatableAttribute id="placeholder_text">Filter</translatableAttribute>
</object>
<!-- Checkboxes -->

View file

@ -52,6 +52,7 @@ CInput::CInput(CGUI& pGUI)
m_CursorVisState(true),
m_CursorBlinkRate(0.5),
m_ComposingText(),
m_GeneratedPlaceholderTextValid(false),
m_iComposedLength(),
m_iComposedPos(),
m_iInsertPos(),
@ -88,6 +89,8 @@ CInput::CInput(CGUI& pGUI)
RegisterSetting("sprite_selectarea", m_SpriteSelectArea);
RegisterSetting("textcolor", m_TextColor);
RegisterSetting("textcolor_selected", m_TextColorSelected);
RegisterSetting("placeholder_text", m_PlaceholderText);
RegisterSetting("placeholder_color", m_PlaceholderColor);
CFG_GET_VAL("gui.cursorblinkrate", m_CursorBlinkRate);
@ -598,6 +601,12 @@ void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode)
}
}
void CInput::SetupGeneratedPlaceholderText()
{
m_GeneratedPlaceholderText = CGUIText(m_pGUI, m_PlaceholderText, m_Font, 0, m_BufferZone, this);
m_GeneratedPlaceholderTextValid = true;
}
InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
{
bool shiftKeyPressed = g_keys[SDLK_RSHIFT] || g_keys[SDLK_LSHIFT];
@ -921,6 +930,15 @@ void CInput::HandleMessage(SGUIMessage& Message)
UpdateText();
}
if (Message.value == "placeholder_text" ||
Message.value == "size" ||
Message.value == "font" ||
Message.value == "z" ||
Message.value == "text_valign")
{
m_GeneratedPlaceholderTextValid = false;
}
UpdateAutoScroll();
break;
@ -1156,6 +1174,8 @@ void CInput::UpdateCachedSize()
GetScrollBar(0).SetZ(GetBufferedZ());
GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
}
m_GeneratedPlaceholderTextValid = false;
}
void CInput::Draw()
@ -1500,6 +1520,17 @@ void CInput::Draw()
glDisable(GL_SCISSOR_TEST);
tech->EndPass();
if (m_Caption.empty() && !m_PlaceholderText.GetRawString().empty())
DrawPlaceholderText(bz, cliparea);
}
void CInput::DrawPlaceholderText(float z, const CRect& clipping)
{
if (!m_GeneratedPlaceholderTextValid)
SetupGeneratedPlaceholderText();
m_GeneratedPlaceholderText.Draw(m_pGUI, m_PlaceholderColor, m_CachedActualSize.TopLeft(), z, clipping);
}
void CInput::UpdateText(int from, int to_before, int to_after)

View file

@ -21,6 +21,7 @@
#include "gui/CGUISprite.h"
#include "gui/ObjectBases/IGUIObject.h"
#include "gui/ObjectBases/IGUIScrollBarOwner.h"
#include "gui/SettingTypes/CGUIString.h"
#include "lib/external_libraries/libsdl.h"
#include <list>
@ -58,6 +59,9 @@ public:
int GetXTextPosition(const std::list<SRow>::const_iterator& c, const float& x, float& wanted) const;
protected:
void SetupGeneratedPlaceholderText();
/**
* @see IGUIObject#HandleMessage()
*/
@ -105,6 +109,15 @@ protected:
*/
void UpdateText(int from = 0, int to_before = -1, int to_after = -1);
/**
* Draws the text generated for placeholder.
*
* @param z Z value
* @param clipping Clipping rectangle, don't even add a parameter
* to get no clipping.
*/
virtual void DrawPlaceholderText(float z, const CRect& clipping = CRect());
/**
* Delete the current selection. Also places the pointer at the
* crack between the two segments kept.
@ -180,6 +193,13 @@ protected:
*/
bool m_SelectingText;
/**
* Whether the cached text is currently valid (if not then SetupText will be called by Draw)
*/
bool m_GeneratedPlaceholderTextValid;
CGUIText m_GeneratedPlaceholderText;
// *** Things for one-line input control *** //
float m_HorizontalScroll;
@ -200,6 +220,7 @@ protected:
i32 m_BufferPosition;
float m_BufferZone;
CStrW m_Caption;
CGUIString m_PlaceholderText;
i32 m_CellID;
CStrW m_Font;
CStrW m_MaskChar;
@ -213,6 +234,7 @@ protected:
CGUISpriteInstance m_SpriteSelectArea;
CGUIColor m_TextColor;
CGUIColor m_TextColorSelected;
CGUIColor m_PlaceholderColor;
};
#endif // INCLUDED_CINPUT