Add quicktune.{cpp,h}
[oweals/minetest.git] / src / quicktune.h
1 /*
2 Minetest-c55
3 Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef QUICKTUNE_HEADER
21 #define QUICKTUNE_HEADER
22
23 #include "irrlichttypes.h"
24 #include <string>
25 #include <map>
26 #include <vector>
27 #include "utility.h"
28
29 enum QuicktuneValueType{
30         QUICKTUNE_NONE,
31         QUICKTUNE_FLOAT
32 };
33 struct QuicktuneValue
34 {
35         QuicktuneValueType type;
36         union{
37                 struct{
38                         float current;
39                         float min;
40                         float max;
41                 } value_float;
42         };
43 };
44
45 std::vector<std::string> getQuicktuneNames();
46 //std::map<std::string, QuicktuneValue> getQuicktuneNames();
47 QuicktuneValue getQuicktuneValue(const std::string &name);
48 void setQuicktuneValue(const std::string &name, const QuicktuneValue &val);
49
50 class QuicktuneShortcutter
51 {
52 private:
53         std::vector<std::string> m_names;
54         u32 m_selected_i;
55         std::string m_message;
56 public:
57         std::string getMessage()
58         {
59                 std::string s = m_message;
60                 m_message = "";
61                 return s;
62         }
63         std::string getSelectedName()
64         {
65                 if(m_selected_i < m_names.size())
66                         return m_names[m_selected_i];
67                 return "";
68         }
69         void next()
70         {
71                 m_names = getQuicktuneNames();
72                 if(m_selected_i < m_names.size()-1)
73                         m_selected_i++;
74                 else
75                         m_selected_i = 0;
76                 m_message = std::string("Selected \"")+getSelectedName()+"\"";
77         }
78         void prev()
79         {
80                 m_names = getQuicktuneNames();
81                 if(m_selected_i > 0)
82                         m_selected_i--;
83                 else
84                         m_selected_i = m_names.size()-1;
85                 m_message = std::string("Selected \"")+getSelectedName()+"\"";
86         }
87         void inc()
88         {
89                 QuicktuneValue val = getQuicktuneValue(getSelectedName());
90                 switch(val.type){
91                 case QUICKTUNE_NONE:
92                         break;
93                 case QUICKTUNE_FLOAT:
94                         val.value_float.current += 0.05 * (val.value_float.max - val.value_float.min);
95                         if(val.value_float.current > val.value_float.max)
96                                 val.value_float.current = val.value_float.max;
97                         m_message = std::string("\"")+getSelectedName()
98                                         +"\" = "+ftos(val.value_float.current);
99                         break;
100                 default:
101                         m_message = std::string("\"")+getSelectedName()
102                                         +"\" has unknown value type";
103                 }
104                 setQuicktuneValue(getSelectedName(), val);
105         }
106         void dec()
107         {
108                 QuicktuneValue val = getQuicktuneValue(getSelectedName());
109                 switch(val.type){
110                 case QUICKTUNE_NONE:
111                         break;
112                 case QUICKTUNE_FLOAT:
113                         val.value_float.current -= 0.05 * (val.value_float.max - val.value_float.min);
114                         if(val.value_float.current < val.value_float.max)
115                                 val.value_float.current = val.value_float.max;
116                         m_message = std::string("\"")+getSelectedName()
117                                         +"\" = "+ftos(val.value_float.current);
118                         break;
119                 default:
120                         m_message = std::string("\"")+getSelectedName()
121                                         +"\" has unknown value type";
122                 }
123                 setQuicktuneValue(getSelectedName(), val);
124         }
125 };
126
127 void updateQuicktuneValue(const std::string &name, QuicktuneValue &val);
128
129 #ifndef NDEBUG
130
131 #define QUICKTUNE_FLOAT(var, min, max, name){\
132         QuicktuneValue qv;\
133         qv.type = QUICKTUNE_FLOAT;\
134         qv.value_float.current = var;\
135         qv.value_float.min = min;\
136         qv.value_float.min = max;\
137         updateQuicktune(name, qv);\
138         var = qv.value_float.current;\
139 }
140
141 #else // NDEBUG
142
143 #define QUICKTUNE_FLOAT(var, min, max, name){}
144
145 #endif
146
147 #endif
148