Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CListMultiple.cpp
- This file was added.
/* 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 "CListMultiple.h" | |||||
#include "gui/CGUI.h" | |||||
#include "gui/CGUIColor.h" | |||||
#include "gui/CGUIScrollBarVertical.h" | |||||
#include "lib/external_libraries/libsdl.h" | |||||
#include "lib/timer.h" | |||||
#include "ps/Globals.h" | |||||
CListMultiple::CListMultiple(CGUI& pGUI) | |||||
: CList(pGUI), IGUIObject(pGUI), | |||||
m_AddMultiSelection(false), m_IsMultiSelecting(false) | |||||
{ | |||||
AddSetting<CIntList>("selected"); | |||||
SetSetting<CIntList>("selected", CIntList(), true); | |||||
} | |||||
CListMultiple::~CListMultiple() | |||||
{ | |||||
} | |||||
void CListMultiple::SelectionControl(int &select) | |||||
{ | |||||
m_IsMultiSelecting = g_keys[SDLK_LSHIFT] || g_keys[SDLK_RSHIFT]; | |||||
m_AddMultiSelection = g_keys[SDLK_LCTRL] || g_keys[SDLK_RCTRL]; | |||||
// Add current item to selection | |||||
if (m_AddMultiSelection) | |||||
{ | |||||
CIntList sel = GetSetting<CIntList>("selected"); | |||||
m_MultiSelectionFromItem = select; | |||||
m_SelectedItems = sel.m_Items; | |||||
bool add = true; | |||||
size_t pos = -1; | |||||
for (size_t it = 0; it < m_SelectedItems.size(); ++it) | |||||
if (m_SelectedItems.at(it) == select) | |||||
{ | |||||
add = false; | |||||
pos = it; | |||||
break; | |||||
} | |||||
if (add) | |||||
m_SelectedItems.push_back(select); | |||||
else | |||||
{ | |||||
m_SelectedItems.erase(m_SelectedItems.begin() + pos); | |||||
// Select last item in multiselection | |||||
if (m_SelectedItems.size() == 1) | |||||
select = m_SelectedItems.at(0); | |||||
} | |||||
sel.m_Items = m_SelectedItems; | |||||
SetSetting<CIntList>("selected", sel, true); | |||||
} | |||||
// Select everything from last clicked item to current | |||||
else if (m_IsMultiSelecting) | |||||
{ | |||||
if (m_MultiSelectionFromItem == select) | |||||
return; | |||||
CIntList sel = GetSetting<CIntList>("selected"); | |||||
m_SelectedItems = sel.m_Items; | |||||
m_SelectedItems.clear(); | |||||
for (size_t itemId = m_MultiSelectionFromItem; itemId < select + 1; ++itemId) | |||||
m_SelectedItems.push_back(itemId); | |||||
sel.m_Items = m_SelectedItems; | |||||
SetSetting<CIntList>( "selected", sel, true); | |||||
} | |||||
// Clear multi selection | |||||
else | |||||
{ | |||||
CIntList sel = GetSetting<CIntList>("selected"); | |||||
m_SelectedItems = sel.m_Items; | |||||
m_SelectedItems.clear(); | |||||
m_SelectedItems.push_back(select); | |||||
sel.m_Items = m_SelectedItems; | |||||
m_MultiSelectionFromItem = select; | |||||
SetSetting<CIntList>("selected", sel, true); | |||||
} | |||||
} | |||||
bool CListMultiple::IsEnabledDoubleClick() | |||||
{ | |||||
return !m_IsMultiSelecting && !m_AddMultiSelection; | |||||
} | |||||
void CListMultiple::DrawSelectedItems(const int selected, const bool scrollbar, const float scroll, CGUISpriteInstance& sprite_selectarea, const int cell_id, const float bz, CRect& rect) | |||||
{ | |||||
CIntList sel = GetSetting<CIntList>("selected"); | |||||
m_SelectedItems = sel.m_Items; | |||||
// Draw multi selection | |||||
if (!m_SelectedItems.empty()) | |||||
for (size_t selection = 0; selection < m_SelectedItems.size(); ++selection) | |||||
DrawSelection(m_SelectedItems[selection], scrollbar, scroll, sprite_selectarea, cell_id, bz, rect); | |||||
} | |||||
int CListMultiple::GetLastSelected() | |||||
{ | |||||
CIntList sel = GetSetting<CIntList>("selected"); | |||||
if (sel.m_Items.size() == 0) | |||||
return -1; | |||||
return sel.m_Items.at(sel.m_Items.size()-1); | |||||
} |
Wildfire Games · Phabricator