Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CDropDown.cpp
Show First 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | if (!m_Open) | ||||
break; | break; | ||||
CPos mouse = m_pGUI->GetMousePos(); | CPos mouse = m_pGUI->GetMousePos(); | ||||
if (!GetListRect().PointInside(mouse)) | if (!GetListRect().PointInside(mouse)) | ||||
break; | break; | ||||
bool scrollbar; | bool scrollbar; | ||||
CGUIList* pList; | const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list"); | ||||
GUI<bool>::GetSetting(this, "scrollbar", scrollbar); | GUI<bool>::GetSetting(this, "scrollbar", scrollbar); | ||||
GUI<CGUIList>::GetSettingPointer(this, "list", pList); | |||||
float scroll = 0.f; | float scroll = 0.f; | ||||
if (scrollbar) | if (scrollbar) | ||||
scroll = GetScrollBar(0).GetPos(); | scroll = GetScrollBar(0).GetPos(); | ||||
CRect rect = GetListRect(); | CRect rect = GetListRect(); | ||||
mouse.y += scroll; | mouse.y += scroll; | ||||
int set = -1; | int set = -1; | ||||
for (int i = 0; i < (int)pList->m_Items.size(); ++i) | for (int i = 0; i < static_cast<int>(pList.m_Items.size()); ++i) | ||||
{ | { | ||||
if (mouse.y >= rect.top + m_ItemsYPositions[i] && | if (mouse.y >= rect.top + m_ItemsYPositions[i] && | ||||
mouse.y < rect.top + m_ItemsYPositions[i+1] && | mouse.y < rect.top + m_ItemsYPositions[i+1] && | ||||
// mouse is not over scroll-bar | // mouse is not over scroll-bar | ||||
(m_HideScrollBar || | (m_HideScrollBar || | ||||
mouse.x < GetScrollBar(0).GetOuterRect().left || | mouse.x < GetScrollBar(0).GetOuterRect().left || | ||||
mouse.x > GetScrollBar(0).GetOuterRect().right)) | mouse.x > GetScrollBar(0).GetOuterRect().right)) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | if (!enabled) | ||||
CStrW soundPath; | CStrW soundPath; | ||||
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty()) | if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty()) | ||||
g_SoundManager->PlayAsUI(soundPath.c_str(), false); | g_SoundManager->PlayAsUI(soundPath.c_str(), false); | ||||
break; | break; | ||||
} | } | ||||
if (!m_Open) | if (!m_Open) | ||||
{ | { | ||||
CGUIList* pList; | const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list"); | ||||
GUI<CGUIList>::GetSettingPointer(this, "list", pList); | if (pList.m_Items.empty()) | ||||
if (pList->m_Items.empty()) | |||||
return; | return; | ||||
m_Open = true; | m_Open = true; | ||||
GetScrollBar(0).SetZ(GetBufferedZ()); | GetScrollBar(0).SetZ(GetBufferedZ()); | ||||
GUI<int>::GetSetting(this, "selected", m_ElementHighlight); | GUI<int>::GetSetting(this, "selected", m_ElementHighlight); | ||||
// Start at the position of the selected item, if possible. | // Start at the position of the selected item, if possible. | ||||
GetScrollBar(0).SetPos(m_ItemsYPositions.empty() ? 0 : m_ItemsYPositions[m_ElementHighlight] - 60); | GetScrollBar(0).SetPos(m_ItemsYPositions.empty() ? 0 : m_ItemsYPositions[m_ElementHighlight] - 60); | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | default: | ||||
// maximal amount of characters is 100, which imo is far more than enough. | // maximal amount of characters is 100, which imo is far more than enough. | ||||
if (timer_Time() - m_TimeOfLastInput > 1.0 || m_InputBuffer.length() >= 100) | if (timer_Time() - m_TimeOfLastInput > 1.0 || m_InputBuffer.length() >= 100) | ||||
m_InputBuffer = szChar; | m_InputBuffer = szChar; | ||||
else | else | ||||
m_InputBuffer += szChar; | m_InputBuffer += szChar; | ||||
m_TimeOfLastInput = timer_Time(); | m_TimeOfLastInput = timer_Time(); | ||||
CGUIList* pList; | const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list"); | ||||
GUI<CGUIList>::GetSettingPointer(this, "list", pList); | |||||
// let's look for the closest element | // let's look for the closest element | ||||
// basically it's alphabetic order and "as many letters as we can get". | // basically it's alphabetic order and "as many letters as we can get". | ||||
int closest = -1; | int closest = -1; | ||||
int bestIndex = -1; | int bestIndex = -1; | ||||
int difference = 1250; | int difference = 1250; | ||||
for (int i = 0; i < (int)pList->m_Items.size(); ++i) | for (int i = 0; i < static_cast<int>(pList.m_Items.size()); ++i) | ||||
{ | { | ||||
int indexOfDifference = 0; | int indexOfDifference = 0; | ||||
int diff = 0; | int diff = 0; | ||||
for (size_t j = 0; j < m_InputBuffer.length(); ++j) | for (size_t j = 0; j < m_InputBuffer.length(); ++j) | ||||
{ | { | ||||
diff = std::abs((int)(pList->m_Items[i].GetRawString().LowerCase()[j]) - (int)m_InputBuffer[j]); | diff = std::abs(static_cast<int>(pList.m_Items[i].GetRawString().LowerCase()[j]) - static_cast<int>(m_InputBuffer[j])); | ||||
if (diff == 0) | if (diff == 0) | ||||
indexOfDifference = j+1; | indexOfDifference = j+1; | ||||
else | else | ||||
break; | break; | ||||
} | } | ||||
if (indexOfDifference > bestIndex || (indexOfDifference >= bestIndex && diff < difference)) | if (indexOfDifference > bestIndex || (indexOfDifference >= bestIndex && diff < difference)) | ||||
{ | { | ||||
bestIndex = indexOfDifference; | bestIndex = indexOfDifference; | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | |||||
void CDropDown::Draw() | void CDropDown::Draw() | ||||
{ | { | ||||
float bz = GetBufferedZ(); | float bz = GetBufferedZ(); | ||||
float dropdown_size, button_width; | float dropdown_size, button_width; | ||||
GUI<float>::GetSetting(this, "dropdown_size", dropdown_size); | GUI<float>::GetSetting(this, "dropdown_size", dropdown_size); | ||||
GUI<float>::GetSetting(this, "button_width", button_width); | GUI<float>::GetSetting(this, "button_width", button_width); | ||||
CGUISpriteInstance* sprite; | |||||
CGUISpriteInstance* sprite2; | |||||
CGUISpriteInstance* sprite2_second; | |||||
int cell_id, selected = 0; | int cell_id, selected = 0; | ||||
CGUIColor color; | CGUIColor color; | ||||
bool enabled; | bool enabled; | ||||
GUI<bool>::GetSetting(this, "enabled", enabled); | GUI<bool>::GetSetting(this, "enabled", enabled); | ||||
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2", sprite2); | CGUISpriteInstance& sprite = GUI<CGUISpriteInstance>::GetSetting(this, enabled ? "sprite" : "sprite_disabled"); | ||||
CGUISpriteInstance& sprite2 = GUI<CGUISpriteInstance>::GetSetting(this, "sprite2"); | |||||
GUI<int>::GetSetting(this, "cell_id", cell_id); | GUI<int>::GetSetting(this, "cell_id", cell_id); | ||||
GUI<int>::GetSetting(this, "selected", selected); | GUI<int>::GetSetting(this, "selected", selected); | ||||
GUI<CGUIColor>::GetSetting(this, enabled ? "textcolor_selected" : "textcolor_disabled", color); | GUI<CGUIColor>::GetSetting(this, enabled ? "textcolor_selected" : "textcolor_disabled", color); | ||||
GUI<CGUISpriteInstance>::GetSettingPointer(this, enabled ? "sprite" : "sprite_disabled", sprite); | m_pGUI->DrawSprite(sprite, cell_id, bz, m_CachedActualSize); | ||||
m_pGUI->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize); | |||||
if (button_width > 0.f) | if (button_width > 0.f) | ||||
{ | { | ||||
CRect rect(m_CachedActualSize.right-button_width, m_CachedActualSize.top, | CRect rect(m_CachedActualSize.right-button_width, m_CachedActualSize.top, | ||||
m_CachedActualSize.right, m_CachedActualSize.bottom); | m_CachedActualSize.right, m_CachedActualSize.bottom); | ||||
if (!enabled) | if (!enabled) | ||||
{ | { | ||||
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_disabled", sprite2_second); | CGUISpriteInstance& sprite2_second = GUI<CGUISpriteInstance>::GetSetting(this, "sprite2_disabled"); | ||||
m_pGUI->DrawSprite(*sprite2_second || *sprite2, cell_id, bz + 0.05f, rect); | m_pGUI->DrawSprite(sprite2_second || sprite2, cell_id, bz + 0.05f, rect); | ||||
} | } | ||||
else if (m_Open) | else if (m_Open) | ||||
{ | { | ||||
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_pressed", sprite2_second); | CGUISpriteInstance& sprite2_second = GUI<CGUISpriteInstance>::GetSetting(this, "sprite2_pressed"); | ||||
m_pGUI->DrawSprite(*sprite2_second || *sprite2, cell_id, bz + 0.05f, rect); | m_pGUI->DrawSprite(sprite2_second || sprite2, cell_id, bz + 0.05f, rect); | ||||
} | } | ||||
else if (m_MouseHovering) | else if (m_MouseHovering) | ||||
{ | { | ||||
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_over", sprite2_second); | CGUISpriteInstance& sprite2_second = GUI<CGUISpriteInstance>::GetSetting(this, "sprite2_over"); | ||||
m_pGUI->DrawSprite(*sprite2_second || *sprite2, cell_id, bz + 0.05f, rect); | m_pGUI->DrawSprite(sprite2_second || sprite2, cell_id, bz + 0.05f, rect); | ||||
} | } | ||||
else | else | ||||
m_pGUI->DrawSprite(*sprite2, cell_id, bz + 0.05f, rect); | m_pGUI->DrawSprite(sprite2, cell_id, bz + 0.05f, rect); | ||||
} | } | ||||
if (selected != -1) // TODO: Maybe check validity completely? | if (selected != -1) // TODO: Maybe check validity completely? | ||||
{ | { | ||||
CRect cliparea(m_CachedActualSize.left, m_CachedActualSize.top, | CRect cliparea(m_CachedActualSize.left, m_CachedActualSize.top, | ||||
m_CachedActualSize.right-button_width, m_CachedActualSize.bottom); | m_CachedActualSize.right-button_width, m_CachedActualSize.bottom); | ||||
CPos pos(m_CachedActualSize.left, m_CachedActualSize.top); | CPos pos(m_CachedActualSize.left, m_CachedActualSize.top); | ||||
DrawText(selected, color, pos, bz+0.1f, cliparea); | DrawText(selected, color, pos, bz+0.1f, cliparea); | ||||
} | } | ||||
bool* scrollbar = NULL; | // Temporarily disable scrollbar without sending a message | ||||
bool old; | bool& scrollbar = GUI<bool>::GetSetting(this, "scrollbar"); | ||||
GUI<bool>::GetSettingPointer(this, "scrollbar", scrollbar); | bool old = scrollbar; | ||||
old = *scrollbar; | |||||
if (m_Open) | if (m_Open) | ||||
{ | { | ||||
if (m_HideScrollBar) | if (m_HideScrollBar) | ||||
*scrollbar = false; | scrollbar = false; | ||||
DrawList(m_ElementHighlight, "sprite_list", "sprite_selectarea", "textcolor"); | DrawList(m_ElementHighlight, "sprite_list", "sprite_selectarea", "textcolor"); | ||||
if (m_HideScrollBar) | if (m_HideScrollBar) | ||||
*scrollbar = old; | scrollbar = old; | ||||
} | } | ||||
} | } | ||||
// When a dropdown list is opened, it needs to be visible above all the other | // When a dropdown list is opened, it needs to be visible above all the other | ||||
// controls on the page. The only way I can think of to do this is to increase | // controls on the page. The only way I can think of to do this is to increase | ||||
// its z value when opened, so that it's probably on top. | // its z value when opened, so that it's probably on top. | ||||
float CDropDown::GetBufferedZ() const | float CDropDown::GetBufferedZ() const | ||||
{ | { | ||||
float bz = CList::GetBufferedZ(); | float bz = CList::GetBufferedZ(); | ||||
if (m_Open) | if (m_Open) | ||||
return std::min(bz + 500.f, 1000.f); // TODO - don't use magic number for max z value | return std::min(bz + 500.f, 1000.f); // TODO - don't use magic number for max z value | ||||
else | else | ||||
return bz; | return bz; | ||||
} | } |
Wildfire Games · Phabricator