Skip to content

Commit

Permalink
Convert menu item handling to the new vector-of-config method
Browse files Browse the repository at this point in the history
This matches removes the need for end-conversion to config right before the menus are shown.
  • Loading branch information
Vultraz committed Apr 21, 2017
1 parent 8b8065c commit 7d2d100
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 175 deletions.
8 changes: 4 additions & 4 deletions src/controller_base.cpp
Expand Up @@ -288,17 +288,17 @@ void controller_base::play_slice(bool is_delay_enabled)
}
}

void controller_base::show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& disp)
void controller_base::show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp)
{
hotkey::command_executor * cmd_exec = get_hotkey_command_executor();
if (!cmd_exec) {
return;
}

std::vector<std::string> items = items_arg;
std::vector<std::string>::iterator i = items.begin();
std::vector<config> items = items_arg;
std::vector<config>::iterator i = items.begin();
while(i != items.end()) {
const hotkey::hotkey_command& command = hotkey::get_hotkey_command(*i);
const hotkey::hotkey_command& command = hotkey::get_hotkey_command((*i)["id"]);
if(!cmd_exec->can_execute_command(command)
|| (context_menu && !in_context_menu(command.id))) {
i = items.erase(i);
Expand Down
2 changes: 1 addition & 1 deletion src/controller_base.hpp
Expand Up @@ -139,7 +139,7 @@ class controller_base : public video2::draw_layering
*/
virtual void process_keyup_event(const SDL_Event& event);

virtual void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& disp);
virtual void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp);
virtual void execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu);

virtual bool in_context_menu(hotkey::HOTKEY_COMMAND command) const;
Expand Down
5 changes: 3 additions & 2 deletions src/display.cpp
Expand Up @@ -1722,8 +1722,9 @@ void display::enable_menu(const std::string& item, bool enable)
for(std::vector<theme::menu>::const_iterator menu = theme_.menus().begin();
menu != theme_.menus().end(); ++menu) {

std::vector<std::string>::const_iterator hasitem =
std::find(menu->items().begin(), menu->items().end(), item);
const auto hasitem = std::find_if(menu->items().begin(), menu->items().end(),
[&item](const config& c) { return c["id"].str() == item; }
);

if(hasitem != menu->items().end()) {
const size_t index = menu - theme_.menus().begin();
Expand Down
46 changes: 23 additions & 23 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -39,6 +39,7 @@
#include "resources.hpp"
#include "reports.hpp"

#include "config_assign.hpp"
#include "desktop/clipboard.hpp"
#include "floating_label.hpp"
#include "game_board.hpp"
Expand Down Expand Up @@ -655,8 +656,8 @@ bool editor_controller::execute_command(const hotkey::hotkey_command& cmd, int i
//TODO mark the map as changed
sound::play_music_once(music_tracks_[index].id());
context_manager_->get_map_context().add_to_playlist(music_tracks_[index]);
std::vector<std::string> items;
items.push_back("editor-playlist");
std::vector<config> items;
items.emplace_back(config_of("id", "editor-playlist"));
std::shared_ptr<gui::button> b = gui_->find_menu_button("menu-playlist");
show_menu(items, b->location().x +1, b->location().y + b->height() +1, false, *gui_);
return true;
Expand Down Expand Up @@ -1005,87 +1006,86 @@ void editor_controller::show_help()
help::show_help(gui_->video(), "..editor");
}

void editor_controller::show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& disp)
void editor_controller::show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp)
{
if (context_menu) {
if (!context_manager_->get_map().on_board_with_border(gui().hex_clicked_on(xloc, yloc))) {
return;
}
}

std::vector<std::string> items;
std::vector<std::string>::const_iterator i = items_arg.begin();
std::vector<config> items;
std::vector<config>::const_iterator i = items_arg.begin();
while(i != items_arg.end())
{

const hotkey::hotkey_command& command = hotkey::get_hotkey_command(*i);
const hotkey::hotkey_command& command = hotkey::get_hotkey_command((*i)["id"]);

if ( ( can_execute_command(command)
&& (!context_menu || in_context_menu(command.id)) )
|| command.id == hotkey::HOTKEY_NULL) {
items.push_back(*i);
items.emplace_back(config_of("id", (*i)["id"]));
}
++i;
}
if (!items.empty() && items.front() == "EDITOR-LOAD-MRU-PLACEHOLDER") {
if (!items.empty() && items.front()["id"] == "EDITOR-LOAD-MRU-PLACEHOLDER") {
active_menu_ = editor::LOAD_MRU;
context_manager_->expand_load_mru_menu(items);
}
if (!items.empty() && items.front() == "editor-switch-map") {
if (!items.empty() && items.front()["id"] == "editor-switch-map") {
active_menu_ = editor::MAP;
context_manager_->expand_open_maps_menu(items);
}
if (!items.empty() && items.front() == "editor-palette-groups") {
if (!items.empty() && items.front()["id"] == "editor-palette-groups") {
active_menu_ = editor::PALETTE;
toolkit_->get_palette_manager()->active_palette().expand_palette_groups_menu(items);
}
if (!items.empty() && items.front() == "editor-switch-side") {
if (!items.empty() && items.front()["id"] == "editor-switch-side") {
active_menu_ = editor::SIDE;
context_manager_->expand_sides_menu(items);
}
if (!items.empty() && items.front() == "editor-switch-area") {
if (!items.empty() && items.front()["id"] == "editor-switch-area") {
active_menu_ = editor::AREA;
context_manager_->expand_areas_menu(items);
}
if (!items.empty() && items.front() == "editor-switch-time") {
if (!items.empty() && items.front()["id"] == "editor-switch-time") {
active_menu_ = editor::TIME;
context_manager_->expand_time_menu(items);
}
if (!items.empty() && items.front() == "editor-assign-local-time") {
if (!items.empty() && items.front()["id"] == "editor-assign-local-time") {
active_menu_ = editor::LOCAL_TIME;
context_manager_->expand_local_time_menu(items);
}
if (!items.empty() && items.front() == "menu-unit-facings") {
if (!items.empty() && items.front()["id"] == "menu-unit-facings") {
active_menu_ = editor::UNIT_FACING;
items.erase(items.begin());
for (int dir = 0; dir != map_location::NDIRECTIONS; dir++)
items.push_back(map_location::write_translated_direction(map_location::DIRECTION(dir)));
items.emplace_back(config_of("label", map_location::write_translated_direction(map_location::DIRECTION(dir))));
}
if (!items.empty() && items.front() == "editor-playlist") {
if (!items.empty() && items.front()["id"] == "editor-playlist") {
active_menu_ = editor::MUSIC;
items.erase(items.begin());
for (const sound::music_track& track : music_tracks_) {
items.push_back(track.title().empty() ? track.id() : track.title());
items.emplace_back(config_of("label", track.title().empty() ? track.id() : track.title()));
}
}
if (!items.empty() && items.front() == "editor-assign-schedule") {
if (!items.empty() && items.front()["id"] == "editor-assign-schedule") {
active_menu_ = editor::SCHEDULE;

items.erase(items.begin());

for (tods_map::iterator iter = tods_.begin();
iter != tods_.end(); ++iter) {
items.push_back(iter->second.first);
items.emplace_back(config_of("label", iter->second.first));
}
}
if (!items.empty() && items.front() == "editor-assign-local-schedule") {
if (!items.empty() && items.front()["id"] == "editor-assign-local-schedule") {
active_menu_ = editor::LOCAL_SCHEDULE;

items.erase(items.begin());

for (tods_map::iterator iter = tods_.begin();
iter != tods_.end(); ++iter) {
items.push_back(iter->second.first);
items.emplace_back(config_of("label", iter->second.first));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/editor/controller/editor_controller.hpp
Expand Up @@ -112,7 +112,7 @@ class editor_controller : public controller_base,
bool execute_command(const hotkey::hotkey_command& command, int index = -1, bool press=true) override;

/** controller_base override */
void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& disp) override;
void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp) override;

void show_help() override;
void status_table() override;
Expand Down
72 changes: 36 additions & 36 deletions src/editor/map/context_manager.cpp
Expand Up @@ -16,6 +16,7 @@
#include "resources.hpp"
#include "team.hpp"

#include "config_assign.hpp"
#include "display.hpp"
#include "editor/map/context_manager.hpp"
#include "editor/map/map_context.hpp"
Expand Down Expand Up @@ -44,8 +45,6 @@

#include "terrain/translation.hpp"

#include "wml_separators.hpp"

#include <memory>

namespace editor {
Expand Down Expand Up @@ -294,15 +293,15 @@ void context_manager::new_scenario_dialog()
}
}

void context_manager::expand_open_maps_menu(std::vector<std::string>& items)
void context_manager::expand_open_maps_menu(std::vector<config>& items)
{
for(unsigned int i = 0; i < items.size(); ++i) {
if(items[i] != "editor-switch-map") {
if(items[i]["id"] != "editor-switch-map") {
continue;
}

items.erase(items.begin() + i);
std::vector<std::string> contexts;
std::vector<config> contexts;
for(size_t mci = 0; mci < map_contexts_.size(); ++mci) {
std::string filename = map_contexts_[mci]->get_filename();
bool changed = map_contexts_[mci]->modified();
Expand All @@ -319,26 +318,26 @@ void context_manager::expand_open_maps_menu(std::vector<std::string>& items)
if(map_contexts_[mci]->is_embedded()) {
label += " (E)";
}
contexts.push_back(label);
contexts.emplace_back(config_of("label", label));
}
items.insert(items.begin() + i, contexts.begin(), contexts.end());
break;
}
}

void context_manager::expand_load_mru_menu(std::vector<std::string>& items)
void context_manager::expand_load_mru_menu(std::vector<config>& items)
{
std::vector<std::string> mru = preferences::editor::recent_files();

for(unsigned int i = 0; i < items.size(); ++i) {
if(items[i] != "EDITOR-LOAD-MRU-PLACEHOLDER") {
if(items[i]["id"] != "EDITOR-LOAD-MRU-PLACEHOLDER") {
continue;
}

items.erase(items.begin() + i);

if(mru.empty()) {
items.insert(items.begin() + i, _("No Recent Files"));
items.insert(items.begin() + i, config_of("label", _("No Recent Files")));
continue;
}

Expand All @@ -349,26 +348,31 @@ void context_manager::expand_load_mru_menu(std::vector<std::string>& items)
path = filesystem::base_name(path);
}

items.insert(items.begin() + i, mru.begin(), mru.end());
std::vector<config> temp;
std::transform(mru.begin(), mru.end(), std::back_inserter(temp), [](const std::string& str) {
return config_of("label", str);
});

items.insert(items.begin() + i, temp.begin(), temp.end());
break;
}

}

void context_manager::expand_areas_menu(std::vector<std::string>& items)
void context_manager::expand_areas_menu(std::vector<config>& items)
{
tod_manager* tod = get_map_context().get_time_manager();
if(!tod) {
return;
}

for(unsigned int i = 0; i < items.size(); ++i) {
if(items[i] != "editor-switch-area") {
if(items[i]["id"] != "editor-switch-area") {
continue;
}

items.erase(items.begin() + i);
std::vector<std::string> area_entries;
std::vector<config> area_entries;

std::vector<std::string> area_ids = tod->get_area_ids();

Expand All @@ -384,23 +388,23 @@ void context_manager::expand_areas_menu(std::vector<std::string>& items)
label << " [*]";
}

area_entries.push_back(label.str());
area_entries.emplace_back(config_of("label", label.str()));
}

items.insert(items.begin() + i, area_entries.begin(), area_entries.end());
break;
}
}

void context_manager::expand_sides_menu(std::vector<std::string>& items)
void context_manager::expand_sides_menu(std::vector<config>& items)
{
for(unsigned int i = 0; i < items.size(); ++i) {
if(items[i] != "editor-switch-side") {
if(items[i]["id"] != "editor-switch-side") {
continue;
}

items.erase(items.begin() + i);
std::vector<std::string> contexts;
std::vector<config> contexts;

for(size_t mci = 0; mci < get_map_context().get_teams().size(); ++mci) {

Expand All @@ -409,61 +413,57 @@ void context_manager::expand_sides_menu(std::vector<std::string>& items)
std::stringstream label;
label << "[" << mci+1 << "] ";
label << (teamname.empty() ? _("(New Side)") : teamname);
contexts.push_back(label.str());
contexts.emplace_back(config_of("label", label.str()));
}

items.insert(items.begin() + i, contexts.begin(), contexts.end());
break;
}
}

void context_manager::expand_time_menu(std::vector<std::string>& items)
void context_manager::expand_time_menu(std::vector<config>& items)
{
for(unsigned int i = 0; i < items.size(); ++i) {
if(items[i] != "editor-switch-time") {
if(items[i]["id"] != "editor-switch-time") {
continue;
}

items.erase(items.begin() + i);
std::vector<std::string> times;
std::vector<config> times;

tod_manager* tod_m = get_map_context().get_time_manager();

assert(tod_m != nullptr);

for(const time_of_day& time : tod_m->times()) {

std::stringstream label;
if(!time.image.empty())
label << IMAGE_PREFIX << time.image << IMG_TEXT_SEPARATOR;
label << time.name;
times.push_back(label.str());
times.emplace_back(config_of
("details", time.name) // Use 'details' field here since the image will take the first column
("image", time.image)
);
}

items.insert(items.begin() + i, times.begin(), times.end());
break;
}
}

void context_manager::expand_local_time_menu(std::vector<std::string>& items)
void context_manager::expand_local_time_menu(std::vector<config>& items)
{
for(unsigned int i = 0; i < items.size(); ++i) {
if(items[i] != "editor-assign-local-time") {
if(items[i]["id"] != "editor-assign-local-time") {
continue;
}

items.erase(items.begin() + i);
std::vector<std::string> times;
std::vector<config> times;

tod_manager* tod_m = get_map_context().get_time_manager();

for(const time_of_day& time : tod_m->times(get_map_context().get_active_area())) {

std::stringstream label;
if(!time.image.empty())
label << IMAGE_PREFIX << time.image << IMG_TEXT_SEPARATOR;
label << time.name;
times.push_back(label.str());
times.emplace_back(config_of
("details", time.name) // Use 'details' field here since the image will take the first column
("image", time.image)
);
}

items.insert(items.begin() + i, times.begin(), times.end());
Expand Down

0 comments on commit 7d2d100

Please sign in to comment.