2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "modules/meta.hpp"
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
LEMONBUDDY_NS
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace modules {
|
|
|
|
struct menu_tree_item {
|
|
|
|
string exec;
|
|
|
|
label_t label;
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
struct menu_tree {
|
|
|
|
vector<unique_ptr<menu_tree_item>> items;
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
class menu_module : public static_module<menu_module> {
|
|
|
|
public:
|
|
|
|
using static_module::static_module;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
void setup() {
|
2016-10-11 09:28:14 -04:00
|
|
|
string default_format{TAG_LABEL_TOGGLE + string{" "} + TAG_MENU};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
m_formatter->add(DEFAULT_FORMAT, default_format, {TAG_LABEL_TOGGLE, TAG_MENU});
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
if (m_formatter->has(TAG_LABEL_TOGGLE)) {
|
|
|
|
m_labelopen = get_config_label(m_conf, name(), "label-open");
|
|
|
|
m_labelclose = get_optional_config_label(m_conf, name(), "label-close", "x");
|
|
|
|
}
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
m_labelseparator = get_optional_config_label(m_conf, name(), "label-separator", "");
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
if (!m_formatter->has(TAG_MENU))
|
|
|
|
return;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
while (true) {
|
|
|
|
string level_param{"menu-" + to_string(m_levels.size())};
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
if (m_conf.get<string>(name(), level_param + "-0", "").empty())
|
|
|
|
break;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
m_log.trace("%s: Creating menu level %i", name(), m_levels.size());
|
|
|
|
m_levels.emplace_back(make_unique<menu_tree>());
|
2016-06-29 05:06:33 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
while (true) {
|
|
|
|
string item_param{level_param + "-" + to_string(m_levels.back()->items.size())};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
if (m_conf.get<string>(name(), item_param, "").empty())
|
|
|
|
break;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
m_log.trace("%s: Creating menu level item %i", name(), m_levels.back()->items.size());
|
|
|
|
auto item = make_unique<menu_tree_item>();
|
|
|
|
item->label = get_config_label(m_conf, name(), item_param);
|
|
|
|
item->exec = m_conf.get<string>(name(), item_param + "-exec", EVENT_MENU_CLOSE);
|
|
|
|
m_levels.back()->items.emplace_back(std::move(item));
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
2016-06-29 05:06:33 -04:00
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool build(builder* builder, string tag) {
|
|
|
|
if (tag == TAG_LABEL_TOGGLE && m_level == -1) {
|
|
|
|
builder->cmd(mousebtn::LEFT, string(EVENT_MENU_OPEN) + "0");
|
|
|
|
builder->node(m_labelopen);
|
|
|
|
builder->cmd_close(true);
|
|
|
|
} else if (tag == TAG_LABEL_TOGGLE && m_level > -1) {
|
|
|
|
builder->cmd(mousebtn::LEFT, EVENT_MENU_CLOSE);
|
|
|
|
builder->node(m_labelclose);
|
|
|
|
builder->cmd_close(true);
|
|
|
|
} else if (tag == TAG_MENU && m_level > -1) {
|
2016-10-11 09:28:14 -04:00
|
|
|
for (auto&& item : m_levels[m_level]->items) {
|
|
|
|
if (item != m_levels[m_level]->items.front())
|
2016-06-14 23:32:35 -04:00
|
|
|
builder->space();
|
2016-10-11 09:28:14 -04:00
|
|
|
if (*m_labelseparator)
|
|
|
|
builder->node(m_labelseparator, true);
|
|
|
|
builder->cmd(mousebtn::LEFT, item->exec);
|
|
|
|
builder->node(item->label);
|
2016-06-14 23:32:35 -04:00
|
|
|
builder->cmd_close(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-10 12:05:58 -04:00
|
|
|
bool handle_event(string cmd) {
|
2016-10-11 09:28:14 -04:00
|
|
|
if (cmd.compare(0, 4, "menu") != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// broadcast update when leaving leaving the function
|
|
|
|
auto exit_handler = scope_util::make_exit_handler<>([this]() {
|
|
|
|
if (!m_threads.empty()) {
|
|
|
|
m_log.trace("%s: Cleaning up previous broadcast threads", name());
|
|
|
|
for (auto&& thread : m_threads)
|
|
|
|
if (thread.joinable())
|
|
|
|
thread.join();
|
|
|
|
m_threads.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_log.trace("%s: Dispatching broadcast thread", name());
|
|
|
|
m_threads.emplace_back(thread(&menu_module::broadcast, this));
|
|
|
|
});
|
|
|
|
|
2016-10-10 12:05:58 -04:00
|
|
|
if (cmd.compare(0, strlen(EVENT_MENU_OPEN), EVENT_MENU_OPEN) == 0) {
|
|
|
|
auto level = cmd.substr(strlen(EVENT_MENU_OPEN));
|
|
|
|
|
|
|
|
if (level.empty())
|
|
|
|
level = "0";
|
|
|
|
|
|
|
|
m_level = std::atoi(level.c_str());
|
2016-10-11 09:28:14 -04:00
|
|
|
m_log.info("%s: Opening menu level '%i'", name(), static_cast<int>(m_level));
|
2016-10-10 12:05:58 -04:00
|
|
|
|
2016-10-11 09:28:14 -04:00
|
|
|
if (static_cast<size_t>(m_level) >= m_levels.size()) {
|
|
|
|
m_log.warn("%s: Cannot open unexisting menu level '%i'", name(), level);
|
2016-10-10 12:05:58 -04:00
|
|
|
m_level = -1;
|
|
|
|
}
|
|
|
|
} else if (cmd == EVENT_MENU_CLOSE) {
|
2016-10-11 09:28:14 -04:00
|
|
|
m_log.info("%s: Closing menu tree", name());
|
2016-10-10 12:05:58 -04:00
|
|
|
m_level = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool receive_events() const {
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr auto TAG_LABEL_TOGGLE = "<label-toggle>";
|
|
|
|
static constexpr auto TAG_MENU = "<menu>";
|
|
|
|
|
2016-10-11 23:19:29 -04:00
|
|
|
static constexpr auto EVENT_MENU_OPEN = "menu-open-";
|
|
|
|
static constexpr auto EVENT_MENU_CLOSE = "menu-close";
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
label_t m_labelopen;
|
|
|
|
label_t m_labelclose;
|
2016-10-11 09:28:14 -04:00
|
|
|
label_t m_labelseparator;
|
|
|
|
|
|
|
|
vector<unique_ptr<menu_tree>> m_levels;
|
|
|
|
|
|
|
|
std::atomic<int> m_level{-1};
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|