1
0
forked from mirrors/0ad

Use a new GetSetting returning a reference instead of GetSettingPointer when the setting is known to exist.

Differential Revision: https://code.wildfiregames.com/D2192
This was SVN commit r22693.
This commit is contained in:
elexis
2019-08-19 10:32:29 +00:00
parent 5cfef19a06
commit 3dfa23cd25
16 changed files with 259 additions and 318 deletions
+86 -97
View File
@@ -82,10 +82,8 @@ void CInput::UpdateBufferPositionSetting()
void CInput::ClearComposedText()
{
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
pCaption->erase(m_iInsertPos, m_iComposedLength);
CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
pCaption.erase(m_iInsertPos, m_iComposedLength);
m_iBufferPos = m_iInsertPos;
UpdateBufferPositionSetting();
m_iComposedLength = 0;
@@ -113,8 +111,7 @@ InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
return IN_PASS;
// Text has been committed, either single key presses or through an IME
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
std::wstring text = wstring_from_utf8(ev->ev.text.text);
m_WantedX = 0.0f;
@@ -128,10 +125,10 @@ InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
m_ComposingText = false;
}
if (m_iBufferPos == (int)pCaption->length())
pCaption->append(text);
if (m_iBufferPos == static_cast<int>(pCaption.length()))
pCaption.append(text);
else
pCaption->insert(m_iBufferPos, text);
pCaption.insert(m_iBufferPos, text);
UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1);
@@ -151,8 +148,7 @@ InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
// Text is being composed with an IME
// TODO: indicate this by e.g. underlining the uncommitted text
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
const char* rawText = ev->ev.edit.text;
int rawLength = strlen(rawText);
std::wstring wtext = wstring_from_utf8(rawText);
@@ -175,7 +171,7 @@ InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
m_ComposingText = ev->ev.edit.start != 0 || rawLength != 0;
if (m_ComposingText)
{
pCaption->insert(m_iInsertPos, wtext);
pCaption.insert(m_iInsertPos, wtext);
// The text buffer is limited to SDL_TEXTEDITINGEVENT_TEXT_SIZE bytes, yet start
// increases without limit, so don't let it advance beyond the composed text length
@@ -203,8 +199,7 @@ InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
// Since the GUI framework doesn't handle to set settings
// in Unicode (CStrW), we'll simply retrieve the actual
// pointer and edit that.
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
SDL_Keycode keyCode = ev->ev.key.keysym.sym;
ManuallyImmutableHandleKeyDownEvent(keyCode, pCaption);
@@ -220,7 +215,7 @@ InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
}
}
void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW* pCaption)
void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW& pCaption)
{
if (m_Readonly)
return;
@@ -246,14 +241,15 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW*
{
m_iBufferPos_Tail = -1;
if (pCaption->empty() || m_iBufferPos == 0)
if (pCaption.empty() || m_iBufferPos == 0)
break;
if (m_iBufferPos == (int)pCaption->length())
*pCaption = pCaption->Left((long)pCaption->length() - 1);
if (m_iBufferPos == static_cast<int>(pCaption.length()))
pCaption = pCaption.Left(static_cast<long>(pCaption.length()) - 1);
else
*pCaption = pCaption->Left(m_iBufferPos - 1) +
pCaption->Right((long)pCaption->length() - m_iBufferPos);
pCaption =
pCaption.Left(m_iBufferPos - 1) +
pCaption.Right(static_cast<long>(pCaption.length()) - m_iBufferPos);
--m_iBufferPos;
@@ -272,11 +268,12 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW*
DeleteCurSelection();
else
{
if (pCaption->empty() || m_iBufferPos == (int)pCaption->length())
if (pCaption.empty() || m_iBufferPos == static_cast<int>(pCaption.length()))
break;
*pCaption = pCaption->Left(m_iBufferPos) +
pCaption->Right((long)pCaption->length() - (m_iBufferPos + 1));
pCaption =
pCaption.Left(m_iBufferPos) +
pCaption.Right(static_cast<long>(pCaption.length()) - (m_iBufferPos + 1));
UpdateText(m_iBufferPos, m_iBufferPos + 1, m_iBufferPos);
}
@@ -311,7 +308,7 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW*
// check max length
int max_length;
GUI<int>::GetSetting(this, "max_length", max_length);
if (max_length != 0 && (int)pCaption->length() >= max_length)
if (max_length != 0 && static_cast<int>(pCaption.length()) >= max_length)
break;
m_WantedX = 0.0f;
@@ -320,11 +317,12 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW*
DeleteCurSelection();
m_iBufferPos_Tail = -1;
if (m_iBufferPos == (int)pCaption->length())
*pCaption += cooked;
if (m_iBufferPos == static_cast<int>(pCaption.length()))
pCaption += cooked;
else
*pCaption = pCaption->Left(m_iBufferPos) + cooked +
pCaption->Right((long)pCaption->length() - m_iBufferPos);
pCaption =
pCaption.Left(m_iBufferPos) + cooked +
pCaption.Right(static_cast<long>(pCaption.length()) - m_iBufferPos);
UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos + 1);
@@ -337,7 +335,7 @@ void CInput::ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW*
}
}
void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW* pCaption)
void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStrW& pCaption)
{
bool shiftKeyPressed = g_keys[SDLK_RSHIFT] || g_keys[SDLK_LSHIFT];
@@ -377,7 +375,7 @@ void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStr
m_iBufferPos_Tail = m_iBufferPos;
}
m_iBufferPos = (long)pCaption->length();
m_iBufferPos = static_cast<long>(pCaption.length());
m_WantedX = 0.0f;
UpdateAutoScroll();
@@ -440,7 +438,7 @@ void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStr
else if (!SelectingText())
m_iBufferPos_Tail = m_iBufferPos;
if (m_iBufferPos < (int)pCaption->length())
if (m_iBufferPos < static_cast<int>(pCaption.length()))
++m_iBufferPos;
}
else
@@ -573,8 +571,7 @@ void CInput::ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode, CStr
InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
{
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
bool shiftKeyPressed = g_keys[SDLK_RSHIFT] || g_keys[SDLK_LSHIFT];
@@ -593,11 +590,12 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
if (SelectingText())
DeleteCurSelection();
if (m_iBufferPos == (int)pCaption->length())
*pCaption += text;
if (m_iBufferPos == static_cast<int>(pCaption.length()))
pCaption += text;
else
*pCaption = pCaption->Left(m_iBufferPos) + text +
pCaption->Right((long) pCaption->length()-m_iBufferPos);
pCaption =
pCaption.Left(m_iBufferPos) + text +
pCaption.Right(static_cast<long>(pCaption.length()) - m_iBufferPos);
UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1);
@@ -635,7 +633,7 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
virtualTo = m_iBufferPos;
}
CStrW text = (pCaption->Left(virtualTo)).Right(virtualTo - virtualFrom);
CStrW text = (pCaption.Left(virtualTo)).Right(virtualTo - virtualFrom);
sys_clipboard_set(&text[0]);
@@ -659,10 +657,10 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
if (SelectingText())
DeleteCurSelection();
if (!pCaption->empty() && m_iBufferPos != 0)
if (!pCaption.empty() && m_iBufferPos != 0)
{
m_iBufferPos_Tail = m_iBufferPos;
CStrW searchString = pCaption->Left(m_iBufferPos);
CStrW searchString = pCaption.Left(m_iBufferPos);
// If we are starting in whitespace, adjust position until we get a non whitespace
while (m_iBufferPos > 0)
@@ -705,22 +703,22 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
if (SelectingText())
DeleteCurSelection();
if (!pCaption->empty() && m_iBufferPos < (int)pCaption->length())
if (!pCaption.empty() && m_iBufferPos < static_cast<int>(pCaption.length()))
{
// Delete the word to the right of the cursor
m_iBufferPos_Tail = m_iBufferPos;
// Delete chars to the right unit we hit whitespace
while (++m_iBufferPos < (int)pCaption->length())
while (++m_iBufferPos < static_cast<int>(pCaption.length()))
{
if (iswspace((*pCaption)[m_iBufferPos]) || iswpunct((*pCaption)[m_iBufferPos]))
if (iswspace(pCaption[m_iBufferPos]) || iswpunct(pCaption[m_iBufferPos]))
break;
}
// Eliminate any whitespace behind the word we just deleted
while (m_iBufferPos < (int)pCaption->length())
while (m_iBufferPos < static_cast<int>(pCaption.length()))
{
if (!iswspace((*pCaption)[m_iBufferPos]))
if (!iswspace(pCaption[m_iBufferPos]))
break;
++m_iBufferPos;
@@ -743,9 +741,9 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
else if (!SelectingText())
m_iBufferPos_Tail = m_iBufferPos;
if (!pCaption->empty() && m_iBufferPos != 0)
if (!pCaption.empty() && m_iBufferPos != 0)
{
CStrW searchString = pCaption->Left(m_iBufferPos);
CStrW searchString = pCaption.Left(m_iBufferPos);
// If we are starting in whitespace, adjust position until we get a non whitespace
while (m_iBufferPos > 0)
@@ -796,19 +794,19 @@ InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev)
else if (!SelectingText())
m_iBufferPos_Tail = m_iBufferPos;
if (!pCaption->empty() && m_iBufferPos < (int)pCaption->length())
if (!pCaption.empty() && m_iBufferPos < static_cast<int>(pCaption.length()))
{
// Select chars to the right until we hit whitespace
while (++m_iBufferPos < (int)pCaption->length())
while (++m_iBufferPos < static_cast<int>(pCaption.length()))
{
if (iswspace((*pCaption)[m_iBufferPos]) || iswpunct((*pCaption)[m_iBufferPos]))
if (iswspace(pCaption[m_iBufferPos]) || iswpunct(pCaption[m_iBufferPos]))
break;
}
// Also select any whitespace following the word we just selected
while (m_iBufferPos < (int)pCaption->length())
while (m_iBufferPos < static_cast<int>(pCaption.length()))
{
if (!iswspace((*pCaption)[m_iBufferPos]))
if (!iswspace(pCaption[m_iBufferPos]))
break;
++m_iBufferPos;
@@ -942,23 +940,22 @@ void CInput::HandleMessage(SGUIMessage& Message)
if (m_ComposingText)
break;
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
const CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
if (pCaption->empty())
if (pCaption.empty())
break;
m_iBufferPos = m_iBufferPos_Tail = GetMouseHoveringTextPosition();
if (m_iBufferPos >= (int)pCaption->length())
m_iBufferPos = m_iBufferPos_Tail = pCaption->length() - 1;
if (m_iBufferPos >= (int)pCaption.length())
m_iBufferPos = m_iBufferPos_Tail = pCaption.length() - 1;
// See if we are clicking over whitespace
if (iswspace((*pCaption)[m_iBufferPos]))
if (iswspace(pCaption[m_iBufferPos]))
{
// see if we are in a section of whitespace greater than one character
if ((m_iBufferPos + 1 < (int) pCaption->length() && iswspace((*pCaption)[m_iBufferPos + 1])) ||
(m_iBufferPos - 1 > 0 && iswspace((*pCaption)[m_iBufferPos - 1])))
if ((m_iBufferPos + 1 < (int) pCaption.length() && iswspace(pCaption[m_iBufferPos + 1])) ||
(m_iBufferPos - 1 > 0 && iswspace(pCaption[m_iBufferPos - 1])))
{
//
// We are clicking in an area with more than one whitespace character
@@ -968,7 +965,7 @@ void CInput::HandleMessage(SGUIMessage& Message)
// skip the whitespace
while (m_iBufferPos > 0)
{
if (!iswspace((*pCaption)[m_iBufferPos - 1]))
if (!iswspace(pCaption[m_iBufferPos - 1]))
break;
m_iBufferPos--;
@@ -976,52 +973,52 @@ void CInput::HandleMessage(SGUIMessage& Message)
// now go until we hit white space or punctuation
while (m_iBufferPos > 0)
{
if (iswspace((*pCaption)[m_iBufferPos - 1]))
if (iswspace(pCaption[m_iBufferPos - 1]))
break;
m_iBufferPos--;
if (iswpunct((*pCaption)[m_iBufferPos]))
if (iswpunct(pCaption[m_iBufferPos]))
break;
}
// [2] Then the right
// go right until we are not in whitespace
while (++m_iBufferPos_Tail < (int)pCaption->length())
while (++m_iBufferPos_Tail < static_cast<int>(pCaption.length()))
{
if (!iswspace((*pCaption)[m_iBufferPos_Tail]))
if (!iswspace(pCaption[m_iBufferPos_Tail]))
break;
}
if (m_iBufferPos_Tail == (int)pCaption->length())
if (m_iBufferPos_Tail == static_cast<int>(pCaption.length()))
break;
// now go to the right until we hit whitespace or punctuation
while (++m_iBufferPos_Tail < (int)pCaption->length())
while (++m_iBufferPos_Tail < static_cast<int>(pCaption.length()))
{
if (iswspace((*pCaption)[m_iBufferPos_Tail]) || iswpunct((*pCaption)[m_iBufferPos_Tail]))
if (iswspace(pCaption[m_iBufferPos_Tail]) || iswpunct(pCaption[m_iBufferPos_Tail]))
break;
}
}
else
{
// single whitespace so select word to the right
while (++m_iBufferPos_Tail < (int)pCaption->length())
while (++m_iBufferPos_Tail < static_cast<int>(pCaption.length()))
{
if (!iswspace((*pCaption)[m_iBufferPos_Tail]))
if (!iswspace(pCaption[m_iBufferPos_Tail]))
break;
}
if (m_iBufferPos_Tail == (int)pCaption->length())
if (m_iBufferPos_Tail == static_cast<int>(pCaption.length()))
break;
// Don't include the leading whitespace
m_iBufferPos = m_iBufferPos_Tail;
// now go to the right until we hit whitespace or punctuation
while (++m_iBufferPos_Tail < (int)pCaption->length())
while (++m_iBufferPos_Tail < static_cast<int>(pCaption.length()))
{
if (iswspace((*pCaption)[m_iBufferPos_Tail]) || iswpunct((*pCaption)[m_iBufferPos_Tail]))
if (iswspace(pCaption[m_iBufferPos_Tail]) || iswpunct(pCaption[m_iBufferPos_Tail]))
break;
}
}
@@ -1032,17 +1029,17 @@ void CInput::HandleMessage(SGUIMessage& Message)
// go until we hit white space or punctuation
while (m_iBufferPos > 0)
{
if (iswspace((*pCaption)[m_iBufferPos - 1]))
if (iswspace(pCaption[m_iBufferPos - 1]))
break;
m_iBufferPos--;
if (iswpunct((*pCaption)[m_iBufferPos]))
if (iswpunct(pCaption[m_iBufferPos]))
break;
}
// go to the right until we hit whitespace or punctuation
while (++m_iBufferPos_Tail < (int)pCaption->length())
if (iswspace((*pCaption)[m_iBufferPos_Tail]) || iswpunct((*pCaption)[m_iBufferPos_Tail]))
while (++m_iBufferPos_Tail < static_cast<int>(pCaption.length()))
if (iswspace(pCaption[m_iBufferPos_Tail]) || iswpunct(pCaption[m_iBufferPos_Tail]))
break;
}
UpdateAutoScroll();
@@ -1185,9 +1182,7 @@ void CInput::Draw()
GUI<CGUIColor>::GetSetting(this, "textcolor_selected", color_selected);
CStrIntern font_name(font_name_w.ToUTF8());
// Get pointer of caption, it might be very large, and we don't
// want to copy it continuously.
CStrW* pCaption = NULL;
const CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
wchar_t mask_char = L'*';
if (mask)
{
@@ -1196,19 +1191,14 @@ void CInput::Draw()
if (maskStr.length() > 0)
mask_char = maskStr[0];
}
else
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
CGUISpriteInstance* sprite = NULL;
CGUISpriteInstance* sprite_selectarea = NULL;
CGUISpriteInstance& sprite = GUI<CGUISpriteInstance>::GetSetting(this, "sprite");
CGUISpriteInstance& sprite_selectarea = GUI<CGUISpriteInstance>::GetSetting(this, "sprite_selectarea");
int cell_id;
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_selectarea", sprite_selectarea);
GUI<int>::GetSetting(this, "cell_id", cell_id);
GetGUI()->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize);
m_pGUI->DrawSprite(sprite, cell_id, bz, m_CachedActualSize);
float scroll = 0.f;
if (scrollbar && multiline)
@@ -1397,14 +1387,13 @@ void CInput::Draw()
rect.right = m_CachedActualSize.right;
}
if (sprite_selectarea)
GetGUI()->DrawSprite(*sprite_selectarea, cell_id, bz+0.05f, rect);
m_pGUI->DrawSprite(sprite_selectarea, cell_id, bz+0.05f, rect);
}
if (i < (int)it->m_ListOfX.size())
{
if (!mask)
x_pointer += (float)font.GetCharacterWidth((*pCaption)[it->m_ListStart + i]);
x_pointer += (float)font.GetCharacterWidth(pCaption[it->m_ListStart + i]);
else
x_pointer += (float)font.GetCharacterWidth(mask_char);
}
@@ -1490,7 +1479,7 @@ void CInput::Draw()
if (i != (int)it->m_ListOfX.size())
{
if (!mask)
textRenderer.PrintfAdvance(L"%lc", (*pCaption)[it->m_ListStart + i]);
textRenderer.PrintfAdvance(L"%lc", pCaption[it->m_ListStart + i]);
else
textRenderer.PrintfAdvance(L"%lc", mask_char);
}
@@ -1997,8 +1986,7 @@ int CInput::GetXTextPosition(const std::list<SRow>::const_iterator& current, con
void CInput::DeleteCurSelection()
{
CStrW* pCaption = nullptr;
GUI<CStrW>::GetSettingPointer(this, "caption", pCaption);
CStrW& pCaption = GUI<CStrW>::GetSetting(this, "caption");
int virtualFrom;
int virtualTo;
@@ -2014,8 +2002,9 @@ void CInput::DeleteCurSelection()
virtualTo = m_iBufferPos;
}
*pCaption = pCaption->Left(virtualFrom) +
pCaption->Right((long)pCaption->length() - virtualTo);
pCaption =
pCaption.Left(virtualFrom) +
pCaption.Right(static_cast<long>(pCaption.length()) - virtualTo);
UpdateText(virtualFrom, virtualTo, virtualFrom);