initial commit
[menupp.git] / src / level.hpp
blobf5da911ef5c51ce4b58131d79c1c2068b3a244f1
1 #ifndef MENUPP_LEVEL_HPP_
2 #define MENUPP_LEVEL_HPP_
4 #include "node.hpp"
6 namespace Menu
9 template<
10 typename Conf = LevelConf<>,
11 typename FRead = void(*)(typename Conf::Type),
12 typename FCallback = void(*)(typename Conf::Type)
14 class Level : public INode
16 NodeId _id;
17 const char* _label;
18 typename Conf::Type _value = Conf::dflt;
19 typename Conf::Type _value_save = Conf::dflt;
20 FCallback _fc;
21 FRead _fr;
23 public:
24 explicit Level(const char* label, FCallback fc, FRead fr)
25 : _label(label), _fc(fc), _fr(fr) {}
27 void set_id(NodeId id) override { _id = id; }
29 NodeId id() const override { return _id; }
31 void dump(std::ostream& os, size_t indent, size_t total_indent) const override
33 std::fill_n(std::ostream_iterator<char>(os), total_indent, ' ');
34 os << "Level" << _id << ": \"" << label() << "\"\n";
35 std::fill_n(std::ostream_iterator<char>(os), total_indent + indent, ' ');
36 os << _value << '\n';
39 NodeId move(Move m) override
41 NodeId ret = _id;
42 switch (m) {
43 case Move::Into:
44 _value_save = _value;
45 _fr(_value);
46 break;
47 case Move::Up:
48 _value = inc<Conf>(_value);
49 _fr(_value);
50 break;
51 case Move::Down:
52 _value = dec<Conf>(_value);
53 _fr(_value);
54 break;
55 case Move::Left:
56 _value = _value_save;
57 ret = _id.parent();
58 break;
59 case Move::Right:
60 _fc(_value);
61 ret = _id.parent();
62 break;
64 return ret;
67 INode* get(size_t) override { return nullptr; }
69 const char* label() const override { return _label; }
74 #endif // MENUPP_LEVEL_HPP_