Files
0ad/source/gui/CList.cpp
T
elexis e1014aad3b Use CGUI& instead of CGUI* so that the 50+ users stop wondering whether or not that is null and can't even be mistaken to add redundant null checks preemptively.
Remove the remaining of such checks after d3e56f0f57 removed 21 of them
already.
2c47fbd66a initialized the pointer at construction time rather than at
random conditional events later.

Differential Revision: https://code.wildfiregames.com/D2205
Comments By: Vladislav, wraitii
Tested on: gcc 9.1.0, clang 8.0.1

This was SVN commit r22741.
2019-08-21 10:12:33 +00:00

525 lines
14 KiB
C++

/* Copyright (C) 2019 Wildfire Games.
* 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/>.
*/
#include "precompiled.h"
#include "CList.h"
#include "gui/CGUIColor.h"
#include "gui/CGUIScrollBarVertical.h"
#include "lib/external_libraries/libsdl.h"
#include "ps/CLogger.h"
#include "ps/Profile.h"
#include "soundmanager/ISoundManager.h"
CList::CList(CGUI& pGUI)
: IGUIObject(pGUI), IGUITextOwner(pGUI), IGUIScrollBarOwner(pGUI),
m_Modified(false), m_PrevSelectedItem(-1), m_LastItemClickTime(0)
{
// Add sprite_disabled! TODO
AddSetting<float>("buffer_zone");
AddSetting<CStrW>("font");
AddSetting<bool>("scrollbar");
AddSetting<CStr>("scrollbar_style");
AddSetting<CStrW>("sound_disabled");
AddSetting<CStrW>("sound_selected");
AddSetting<CGUISpriteInstance>("sprite");
AddSetting<CGUISpriteInstance>("sprite_selectarea");
AddSetting<int>( "cell_id");
AddSetting<EAlign>("text_align");
AddSetting<CGUIColor>("textcolor");
AddSetting<CGUIColor>("textcolor_selected");
AddSetting<int>( "selected"); // Index selected. -1 is none.
AddSetting<bool>("auto_scroll");
AddSetting<int>( "hovered");
AddSetting<CStrW>("tooltip");
AddSetting<CStr>("tooltip_style");
// Each list item has both a name (in 'list') and an associated data string (in 'list_data')
AddSetting<CGUIList>("list");
AddSetting<CGUIList>("list_data");
GUI<bool>::SetSetting(this, "scrollbar", false);
GUI<int>::SetSetting(this, "selected", -1);
GUI<int>::SetSetting(this, "hovered", -1);
GUI<bool>::SetSetting(this, "auto_scroll", false);
// Add scroll-bar
CGUIScrollBarVertical* bar = new CGUIScrollBarVertical(pGUI);
bar->SetRightAligned(true);
AddScrollBar(bar);
}
CList::~CList()
{
}
void CList::SetupText()
{
m_Modified = true;
const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list");
//ENSURE(m_GeneratedTexts.size()>=1);
m_ItemsYPositions.resize(pList.m_Items.size() + 1);
// Delete all generated texts. Some could probably be saved,
// but this is easier, and this function will never be called
// continuously, or even often, so it'll probably be okay.
m_GeneratedTexts.clear();
CStrW font;
if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
// Use the default if none is specified
// TODO Gee: (2004-08-14) Don't define standard like this. Do it with the default style.
font = L"default";
bool scrollbar;
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
float width = GetListRect().GetWidth();
// remove scrollbar if applicable
if (scrollbar && GetScrollBar(0).GetStyle())
width -= GetScrollBar(0).GetStyle()->m_Width;
float buffer_zone = 0.f;
GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
// Generate texts
float buffered_y = 0.f;
for (size_t i = 0; i < pList.m_Items.size(); ++i)
{
CGUIText* text;
if (!pList.m_Items[i].GetOriginalString().empty())
text = &AddText(pList.m_Items[i], font, width, buffer_zone, this);
else
{
// Minimum height of a space character of the current font size
CGUIString align_string;
align_string.SetValue(L" ");
text = &AddText(align_string, font, width, buffer_zone, this);
}
m_ItemsYPositions[i] = buffered_y;
buffered_y += text->GetSize().cy;
}
m_ItemsYPositions[pList.m_Items.size()] = buffered_y;
// Setup scrollbar
if (scrollbar)
{
GetScrollBar(0).SetScrollRange(m_ItemsYPositions.back());
GetScrollBar(0).SetScrollSpace(GetListRect().GetHeight());
CRect rect = GetListRect();
GetScrollBar(0).SetX(rect.right);
GetScrollBar(0).SetY(rect.top);
GetScrollBar(0).SetZ(GetBufferedZ());
GetScrollBar(0).SetLength(rect.bottom - rect.top);
}
}
void CList::HandleMessage(SGUIMessage& Message)
{
IGUIScrollBarOwner::HandleMessage(Message);
//IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
m_Modified = false;
switch (Message.type)
{
case GUIM_SETTINGS_UPDATED:
if (Message.value == "list")
SetupText();
// If selected is changed, call "SelectionChange"
if (Message.value == "selected")
{
// TODO: Check range
bool auto_scroll;
GUI<bool>::GetSetting(this, "auto_scroll", auto_scroll);
if (auto_scroll)
UpdateAutoScroll();
// TODO only works if lower-case, shouldn't it be made case sensitive instead?
ScriptEvent("selectionchange");
}
if (Message.value == "scrollbar")
SetupText();
// Update scrollbar
if (Message.value == "scrollbar_style")
{
CStr scrollbar_style;
GUI<CStr>::GetSetting(this, Message.value, scrollbar_style);
GetScrollBar(0).SetScrollBarStyle(scrollbar_style);
SetupText();
}
break;
case GUIM_MOUSE_PRESS_LEFT:
{
bool enabled;
GUI<bool>::GetSetting(this, "enabled", enabled);
if (!enabled)
{
CStrW soundPath;
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty())
g_SoundManager->PlayAsUI(soundPath.c_str(), false);
break;
}
int hovered = GetHoveredItem();
if (hovered == -1)
break;
GUI<int>::SetSetting(this, "selected", hovered);
UpdateAutoScroll();
CStrW soundPath;
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty())
g_SoundManager->PlayAsUI(soundPath.c_str(), false);
if (timer_Time() - m_LastItemClickTime < SELECT_DBLCLICK_RATE && hovered == m_PrevSelectedItem)
this->SendEvent(GUIM_MOUSE_DBLCLICK_LEFT_ITEM, "mouseleftdoubleclickitem");
else
this->SendEvent(GUIM_MOUSE_PRESS_LEFT_ITEM, "mouseleftclickitem");
m_LastItemClickTime = timer_Time();
m_PrevSelectedItem = hovered;
break;
}
case GUIM_MOUSE_LEAVE:
{
int hoveredSetting = -1;
GUI<int>::GetSetting(this, "hovered", hoveredSetting);
if (hoveredSetting == -1)
break;
GUI<int>::SetSetting(this, "hovered", -1);
ScriptEvent("hoverchange");
break;
}
case GUIM_MOUSE_OVER:
{
int hoveredSetting = -1;
GUI<int>::GetSetting(this, "hovered", hoveredSetting);
int hovered = GetHoveredItem();
if (hovered == hoveredSetting)
break;
GUI<int>::SetSetting(this, "hovered", hovered);
ScriptEvent("hoverchange");
break;
}
case GUIM_LOAD:
{
CStr scrollbar_style;
GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style);
GetScrollBar(0).SetScrollBarStyle(scrollbar_style);
break;
}
default:
break;
}
IGUITextOwner::HandleMessage(Message);
}
InReaction CList::ManuallyHandleEvent(const SDL_Event_* ev)
{
InReaction result = IN_PASS;
if (ev->ev.type == SDL_KEYDOWN)
{
int szChar = ev->ev.key.keysym.sym;
switch (szChar)
{
case SDLK_HOME:
SelectFirstElement();
UpdateAutoScroll();
result = IN_HANDLED;
break;
case SDLK_END:
SelectLastElement();
UpdateAutoScroll();
result = IN_HANDLED;
break;
case SDLK_UP:
SelectPrevElement();
UpdateAutoScroll();
result = IN_HANDLED;
break;
case SDLK_DOWN:
SelectNextElement();
UpdateAutoScroll();
result = IN_HANDLED;
break;
case SDLK_PAGEUP:
GetScrollBar(0).ScrollMinusPlenty();
result = IN_HANDLED;
break;
case SDLK_PAGEDOWN:
GetScrollBar(0).ScrollPlusPlenty();
result = IN_HANDLED;
break;
default: // Do nothing
result = IN_PASS;
}
}
return result;
}
void CList::Draw()
{
int selected;
GUI<int>::GetSetting(this, "selected", selected);
DrawList(selected, "sprite", "sprite_selectarea", "textcolor");
}
void CList::DrawList(const int& selected, const CStr& _sprite, const CStr& _sprite_selected, const CStr& _textcolor)
{
float bz = GetBufferedZ();
// First call draw on ScrollBarOwner
bool scrollbar;
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
if (scrollbar)
IGUIScrollBarOwner::Draw();
{
CRect rect = GetListRect();
CGUISpriteInstance& sprite = GUI<CGUISpriteInstance>::GetSetting(this, _sprite);
CGUISpriteInstance& sprite_selectarea = GUI<CGUISpriteInstance>::GetSetting(this, _sprite_selected);
const int cell_id = GUI<int>::GetSetting(this, "cell_id");
m_pGUI.DrawSprite(sprite, cell_id, bz, rect);
float scroll = 0.f;
if (scrollbar)
scroll = GetScrollBar(0).GetPos();
if (selected >= 0 && selected+1 < (int)m_ItemsYPositions.size())
{
// Get rectangle of selection:
CRect rect_sel(rect.left, rect.top + m_ItemsYPositions[selected] - scroll,
rect.right, rect.top + m_ItemsYPositions[selected+1] - scroll);
if (rect_sel.top <= rect.bottom &&
rect_sel.bottom >= rect.top)
{
if (rect_sel.bottom > rect.bottom)
rect_sel.bottom = rect.bottom;
if (rect_sel.top < rect.top)
rect_sel.top = rect.top;
if (scrollbar)
{
// Remove any overlapping area of the scrollbar.
if (rect_sel.right > GetScrollBar(0).GetOuterRect().left &&
rect_sel.right <= GetScrollBar(0).GetOuterRect().right)
rect_sel.right = GetScrollBar(0).GetOuterRect().left;
if (rect_sel.left >= GetScrollBar(0).GetOuterRect().left &&
rect_sel.left < GetScrollBar(0).GetOuterRect().right)
rect_sel.left = GetScrollBar(0).GetOuterRect().right;
}
m_pGUI.DrawSprite(sprite_selectarea, cell_id, bz+0.05f, rect_sel);
}
}
const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list");
const CGUIColor& color = GUI<CGUIColor>::GetSetting(this, _textcolor);
for (size_t i = 0; i < pList.m_Items.size(); ++i)
{
if (m_ItemsYPositions[i+1] - scroll < 0 ||
m_ItemsYPositions[i] - scroll > rect.GetHeight())
continue;
// Clipping area (we'll have to substract the scrollbar)
CRect cliparea = GetListRect();
if (scrollbar)
{
if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
cliparea.right <= GetScrollBar(0).GetOuterRect().right)
cliparea.right = GetScrollBar(0).GetOuterRect().left;
if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
cliparea.left < GetScrollBar(0).GetOuterRect().right)
cliparea.left = GetScrollBar(0).GetOuterRect().right;
}
DrawText(i, color, rect.TopLeft() - CPos(0.f, scroll - m_ItemsYPositions[i]), bz + 0.1f, cliparea);
}
}
}
void CList::AddItem(const CStrW& str, const CStrW& data)
{
CGUIString gui_string;
gui_string.SetValue(str);
// Do not send a settings-changed message
CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list");
pList.m_Items.push_back(gui_string);
CGUIString data_string;
data_string.SetValue(data);
CGUIList& pListData = GUI<CGUIList>::GetSetting(this, "list_data");
pListData.m_Items.push_back(data_string);
// TODO Temp
SetupText();
}
bool CList::HandleAdditionalChildren(const XMBElement& child, CXeromyces* pFile)
{
int elmt_item = pFile->GetElementID("item");
if (child.GetNodeName() == elmt_item)
{
AddItem(child.GetText().FromUTF8(), child.GetText().FromUTF8());
return true;
}
return false;
}
void CList::SelectNextElement()
{
int selected = GUI<int>::GetSetting(this, "selected");
const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list");
if (selected != static_cast<int>(pList.m_Items.size()) - 1)
{
++selected;
GUI<int>::SetSetting(this, "selected", selected);
CStrW soundPath;
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty())
g_SoundManager->PlayAsUI(soundPath.c_str(), false);
}
}
void CList::SelectPrevElement()
{
int selected = GUI<int>::GetSetting(this, "selected");
if (selected > 0)
{
--selected;
GUI<int>::SetSetting(this, "selected", selected);
CStrW soundPath;
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty())
g_SoundManager->PlayAsUI(soundPath.c_str(), false);
}
}
void CList::SelectFirstElement()
{
if (GUI<int>::GetSetting(this, "selected") >= 0)
GUI<int>::SetSetting(this, "selected", 0);
}
void CList::SelectLastElement()
{
const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list");
const int index = static_cast<int>(pList.m_Items.size()) - 1;
if (GUI<int>::GetSetting(this, "selected") != index)
GUI<int>::SetSetting(this, "selected", index);
}
void CList::UpdateAutoScroll()
{
const int selected = GUI<int>::GetSetting(this, "selected");
const bool scrollbar = GUI<bool>::GetSetting(this, "scrollbar");
// No scrollbar, no scrolling (at least it's not made to work properly).
if (!scrollbar || selected < 0 || static_cast<std::size_t>(selected) >= m_ItemsYPositions.size())
return;
float scroll = GetScrollBar(0).GetPos();
// Check upper boundary
if (m_ItemsYPositions[selected] < scroll)
{
GetScrollBar(0).SetPos(m_ItemsYPositions[selected]);
return; // this means, if it wants to align both up and down at the same time
// this will have precedence.
}
// Check lower boundary
CRect rect = GetListRect();
if (m_ItemsYPositions[selected+1]-rect.GetHeight() > scroll)
GetScrollBar(0).SetPos(m_ItemsYPositions[selected+1]-rect.GetHeight());
}
int CList::GetHoveredItem()
{
const bool scrollbar = GUI<bool>::GetSetting(this, "scrollbar");
const float scroll = scrollbar ? GetScrollBar(0).GetPos() : 0.f;
const CRect& rect = GetListRect();
CPos mouse = m_pGUI.GetMousePos();
mouse.y += scroll;
// Mouse is over scrollbar
if (scrollbar && GetScrollBar(0).IsVisible() &&
mouse.x >= GetScrollBar(0).GetOuterRect().left &&
mouse.x <= GetScrollBar(0).GetOuterRect().right)
return -1;
const CGUIList& pList = GUI<CGUIList>::GetSetting(this, "list");
for (size_t i = 0; i < pList.m_Items.size(); ++i)
if (mouse.y >= rect.top + m_ItemsYPositions[i] &&
mouse.y < rect.top + m_ItemsYPositions[i + 1])
return i;
return -1;
}