1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #include "ui/controls/enumslider.h"
21 
22 #include "common/stringutils.h"
23 
24 namespace Ui
25 {
26 
CEnumSlider()27 CEnumSlider::CEnumSlider() : CSlider()
28 {
29 }
30 
SetPossibleValues(const std::vector<float> & values)31 void CEnumSlider::SetPossibleValues(const std::vector<float>& values)
32 {
33     m_values = values;
34 }
35 
SetPossibleValues(const std::map<float,std::string> & values)36 void CEnumSlider::SetPossibleValues(const std::map<float, std::string>& values)
37 {
38     m_values.clear();
39     m_labels.clear();
40     for (auto it = values.begin(); it != values.end(); ++it)
41     {
42         m_values.push_back(it->first);
43         m_labels.push_back(it->second);
44     }
45 }
46 
SetVisibleValue(float value)47 void CEnumSlider::SetVisibleValue(float value)
48 {
49     for (unsigned int i = 0; i < m_values.size(); i++)
50     {
51         if (value == m_values[i])
52         {
53             m_visibleValue = static_cast<float>(i) / (m_values.size()-1);
54         }
55     }
56 }
57 
GetVisibleValueIndex()58 unsigned int CEnumSlider::GetVisibleValueIndex()
59 {
60     return round(m_visibleValue * (m_values.size()-1));
61 }
62 
GetVisibleValue()63 float CEnumSlider::GetVisibleValue()
64 {
65     return m_values[GetVisibleValueIndex()];
66 }
67 
GetLabel()68 std::string CEnumSlider::GetLabel()
69 {
70     unsigned int value = GetVisibleValueIndex();
71     if (value < m_labels.size())
72     {
73         return m_labels.at(value);
74     }
75     return StrUtils::ToString<int>(GetVisibleValue());
76 }
77 
78 }
79