Skip to content

Commit

Permalink
Add buttons to start/stop the simulation, move game_menu to game_viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Organic-Code committed Oct 11, 2023
1 parent 66b9530 commit 0ae643a
Show file tree
Hide file tree
Showing 14 changed files with 1,148 additions and 94 deletions.
861 changes: 861 additions & 0 deletions external/IconFontCppHeaders/IconsFontAwesome5.h

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions resources/configured_resources/lang/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@
[[log.entry]]
id = "view.view.bad_state"
fmt = "unknown view state: {state}"
[[log.entry]]
id = "view.view.FA_load_failed"
fmt = "failed to load font awesome from folder \"{folder}\""
[[log.entry]]
id = "view.view.FA_load_success"
fmt = "successfully loaded font awesome"

[[log.entry]]
id = "view.configurator.config_save_failed"
fmt = "Could not save the new configuration"
Expand Down Expand Up @@ -376,6 +383,19 @@
[[gui.entry]]
id = "view.in_game_menu.campaign_editor"
fmt = "Campaign editor"
[[gui.entry]]
id = "view.in_game_menu.return_to_main_menu"
fmt = "Return to main menu"

[[gui.entry]]
id = "view.game_viewer.play"
fmt = ""
[[gui.entry]]
id = "view.game_viewer.pause"
fmt = ""
[[gui.entry]]
id = "view.game_viewer.step"
fmt = ""

[[gui.entry]]
id = "file_explorer.ok_button"
Expand Down
10 changes: 10 additions & 0 deletions resources/configured_resources/lang/fr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@
[[log.entry]]
id = "view.view.bad_state"
fmt = "État de la vue inconnu : {state}"
[[log.entry]]
id = "view.view.FA_load_failed"
fmt = "Échec de chargement de Font Awesome depuis \"{folder}\""
[[log.entry]]
id = "view.view.FA_load_success"
fmt = "Font Awesome chargée avec succès"

[[log.entry]]
id = "view.configurator.config_save_failed"
fmt = "Échec de sauvegarde de la configuration"
Expand Down Expand Up @@ -375,6 +382,9 @@
[[gui.entry]]
id = "view.in_game_menu.campaign_editor"
fmt = "Éditeur de campagne"
[[gui.entry]]
id = "view.in_game_menu.return_to_main_menu"
fmt = "Menu principal"

[[gui.entry]]
id = "file_explorer.ok_button"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Font Awesome Free License
-------------------------

Font Awesome Free is free, open source, and GPL friendly. You can use it for
commercial projects, open source projects, or really almost whatever you want.
Full Font Awesome Free license: https://fontawesome.com/license/free.

# Icons: CC BY 4.0 License (https://creativecommons.org/licenses/by/4.0/)
In the Font Awesome Free download, the CC BY 4.0 license applies to all icons
packaged as SVG and JS file types.

# Fonts: SIL OFL 1.1 License (https://scripts.sil.org/OFL)
In the Font Awesome Free download, the SIL OLF license applies to all icons
packaged as web and desktop font files.

# Code: MIT License (https://opensource.org/licenses/MIT)
In the Font Awesome Free download, the MIT license applies to all non-font and
non-icon files.

# Attribution
Attribution is required by MIT, SIL OLF, and CC BY licenses. Downloaded Font
Awesome Free files already contain embedded comments with sufficient
attribution, so you shouldn't need to do anything additional when using these
files normally.

We've kept attribution comments terse, so we ask that you do not actively work
to remove them from files, especially code. They're a great way for folks to
learn about Font Awesome.

# Brand Icons
All brand icons are trademarks of their respective owners. The use of these
trademarks does not indicate endorsement of the trademark holder by Font
Awesome, nor vice versa. **Please do not use brand logos for any purpose except
to represent the company, product, or service to which they refer.**
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions src/state_holder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ class access<view::game_menu> {
return holder.adapter();
}

static ImTerm::terminal<terminal_commands> &terminal(holder &holder) noexcept {
return holder.terminal();
}

friend view::game_menu;
};

Expand Down
33 changes: 21 additions & 12 deletions src/view/game/game_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ view::game_menu::user_request view::game_menu::show() {
const auto &res = utils::resource_manager::instance();
const auto &style = ImGui::GetStyle();

const std::string_view resume = res.gui_text_for("view.in_game_menu.resume");
const std::string_view load_dll = res.gui_text_for("view.in_game_menu.dll");
const std::string_view restart = res.gui_text_for("view.in_game_menu.restart");
const std::string_view settings = res.gui_text_for("view.in_game_menu.settings");
const std::string_view load_map = res.gui_text_for("view.in_game_menu.load_map");
const std::string_view import = res.gui_text_for("view.in_game_menu.import_maps");
const std::string_view credits = res.gui_text_for("view.in_game_menu.credits");
const std::string_view map_editor = res.gui_text_for("view.in_game_menu.map_editor");
const std::string_view campaign_editor = res.gui_text_for("view.in_game_menu.campaign_editor");
const std::string_view quit = res.gui_text_for("view.in_game_menu.quit");
const std::string_view resume = res.gui_text_for("view.in_game_menu.resume");
const std::string_view load_dll = res.gui_text_for("view.in_game_menu.dll");
const std::string_view restart = res.gui_text_for("view.in_game_menu.restart");
const std::string_view settings = res.gui_text_for("view.in_game_menu.settings");
const std::string_view load_map = res.gui_text_for("view.in_game_menu.load_map");
const std::string_view import = res.gui_text_for("view.in_game_menu.import_maps");
const std::string_view credits = res.gui_text_for("view.in_game_menu.credits");
const std::string_view map_editor = res.gui_text_for("view.in_game_menu.map_editor");
const std::string_view campaign_editor = res.gui_text_for("view.in_game_menu.campaign_editor");
const std::string_view back_to_main_menu = res.gui_text_for("view.in_game_menu.return_to_main_menu");
const std::string_view quit = res.gui_text_for("view.in_game_menu.quit");

ImVec2 max_text_size{0.f, 0.f};
auto update_sz = [&max_text_size](std::string_view str) {
Expand All @@ -63,12 +64,14 @@ view::game_menu::user_request view::game_menu::show() {
update_sz(credits);
update_sz(map_editor);
update_sz(campaign_editor);
update_sz(back_to_main_menu);
update_sz(quit);

float text_width = max_text_size.x + style.ItemInnerSpacing.x * 2;
ImGui::SetNextWindowSize(ImVec2{text_width + style.WindowPadding.x * 2, 0.f});
if (ImGui::BeginPopupModal(menu_window_name, nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize)) { // NOLINT(hicpp-signed-bitwise)
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoResize)) { // NOLINT(hicpp-signed-bitwise)

if (!::state::access<game_menu>::adapter(m_state).map_is_loaded()) {
using_style(disabled_button) {
Expand Down Expand Up @@ -115,7 +118,13 @@ view::game_menu::user_request view::game_menu::show() {
ImGui::Button(credits.data(), ImVec2{text_width, 0.f}); // TODO
ImGui::Button(map_editor.data(), ImVec2{text_width, 0.f}); // TODO
ImGui::Button(campaign_editor.data(), ImVec2{text_width, 0.f}); // TODO
};
}; // todo : move to main menu

if (ImGui::Button(back_to_main_menu.data(), ImVec2{text_width, 0.f})) {
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
return user_request::back_to_main_menu;
}

if (ImGui::Button(quit.data(), ImVec2{text_width, 0.f})) {
ImGui::CloseCurrentPopup();
Expand Down
3 changes: 2 additions & 1 deletion src/view/game/game_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class game_menu {
close_window,
restart,
load_dll,
load_map
load_map,
back_to_main_menu
};


Expand Down
129 changes: 124 additions & 5 deletions src/view/game/game_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

#include <IconFontCppHeaders/IconsFontAwesome5.h>

#include "game_viewer.hpp"
#include "state_holder.hpp"
#include "utils/logging.hpp"
#include "view/standalones/imgui_styles.hpp"

namespace {
const sf::Event::KeyEvent &key(const sf::Event &event) {
Expand Down Expand Up @@ -36,6 +40,7 @@ view::game_viewer::game_viewer(sf::RenderWindow &window, state::holder &state) n
: m_window{window}
, m_state{state}
, m_map{state}
, m_menu{state}
, m_fake_arg{state, state::access<game_viewer>::terminal(state), {}} {
m_map.set_render_window(window);
}
Expand All @@ -56,9 +61,15 @@ void view::game_viewer::restart() noexcept {
m_autostep_bot = false;
}

void view::game_viewer::show(bool show_debug_data) {
bool view::game_viewer::show(bool show_debug_data) {
m_window_size = m_window.getSize();
m_map.print(show_debug_data);
show_rightmost_bar();
if (m_showing_menu) {
display_menu();
}

return std::exchange(m_stay_in_game, true);
}

// todo split
Expand All @@ -72,14 +83,19 @@ void view::game_viewer::event(const sf::Event &event) {
terminal_commands::update_world(m_fake_arg);
}
break;
case sf::Keyboard::F4:
case sf::Keyboard::F3:
if (std::exchange(m_autostep_bot, !m_autostep_bot)) {
terminal_commands::stop_model(m_fake_arg);
}
else {
terminal_commands::run_model(m_fake_arg);
}
break;
case sf::Keyboard::Escape:
if (std::exchange(m_showing_menu, !m_showing_menu)) {
m_menu.close();
}
break;
default:
break;
}
Expand All @@ -92,9 +108,6 @@ void view::game_viewer::event(const sf::Event &event) {
break;
}

auto &terminal = state::access<game_viewer>::terminal(m_state);
terminal.set_width(m_window.getSize().x);

const sf::Event::SizeEvent sz = size(event);
const float x_ratio = static_cast<float>(sz.width) / static_cast<float>(m_window_size.x);
const float y_ratio = static_cast<float>(sz.height) / static_cast<float>(m_window_size.y);
Expand Down Expand Up @@ -185,3 +198,109 @@ void view::game_viewer::event(const sf::Event &event) {
break;
}
}

void view::game_viewer::display_menu() noexcept {
using fmt::operator""_a;

ImTerm::terminal<terminal_commands> &terminal = state::access<game_viewer>::terminal(m_state);

auto request = m_menu.show();
switch (request) {
case game_menu::user_request::none:
break;
case game_menu::user_request::close_menu:
m_showing_menu = false;
resume();
m_menu.close();
break;
case game_menu::user_request::close_window:
m_window.close();
break;
case game_menu::user_request::restart:
state::access<game_viewer>::adapter(m_state).load_map(m_state.current_map_path());
m_showing_menu = false;
restart();
m_menu.close();
break;
case game_menu::user_request::load_dll: {
terminal_commands::argument_type arg{m_state, terminal, {}};
arg.command_line.emplace_back();
arg.command_line.push_back(m_menu.path().generic_string());
terminal_commands::load_shared_library(arg);
break;
}
case game_menu::user_request::load_map: {
terminal_commands::argument_type arg{m_state, terminal, {}};
arg.command_line.emplace_back();
arg.command_line.push_back(m_menu.path().generic_string());
terminal_commands::load_map(arg);
break;
}
case game_menu::user_request::back_to_main_menu:
m_stay_in_game = false;
m_menu.close();
m_showing_menu = false;
break;
default:
utils::log::error("view.view.menu.unknown_request", "id"_a = static_cast<int>(request));
break;
}
}

void view::game_viewer::show_rightmost_bar() noexcept {
using fmt::operator""_a;

ImVec2 max_text_size{0.f, 0.f};
auto update_sz = [&max_text_size](std::string_view str) {
auto size = ImGui::CalcTextSize(str.data(), str.data() + str.size());
max_text_size.x = std::max(max_text_size.x, size.x);
max_text_size.y = std::max(max_text_size.y, size.y);
};

const std::string_view play = ICON_FA_PLAY;
const std::string_view pause = ICON_FA_PAUSE;
const std::string_view step = ICON_FA_PLAY_CIRCLE;
update_sz(play);
update_sz(pause);
update_sz(step);

const auto &style = ImGui::GetStyle();
const auto total_width = max_text_size.x + style.ItemInnerSpacing.x * 2 + style.ItemSpacing.x * 2;
const auto total_height = max_text_size.y * 3 + style.ItemInnerSpacing.y * 2 + style.ItemSpacing.y * 2;

ImGui::SetNextWindowSize(ImVec2{total_width + style.WindowPadding.x * 2, 0.f}, ImGuiCond_Always);
ImGui::SetNextWindowPos(
ImVec2{static_cast<float>(m_window_size.x) - total_width, (static_cast<float>(m_window_size.y) - total_height) / 2.f},
ImGuiCond_Always);

if (ImGui::Begin("view.game.game_viewer.show_rightmost_bar", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove // NOLINT(*-signed-bitwise)
| ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground)) {

if (m_autostep_bot) {
using_style(disabled_button) {
ImGui::Button(play.data());
};
if (ImGui::Button(pause.data())) {
terminal_commands::stop_model(m_fake_arg);
m_autostep_bot = false;
}
using_style(disabled_button) {
ImGui::Button(step.data());
};
}
else {
if (ImGui::Button(play.data())) {
terminal_commands::run_model(m_fake_arg);
m_autostep_bot = true;
}
using_style(disabled_button) {
ImGui::Button(pause.data());
};
if (ImGui::Button(step.data())) {
terminal_commands::update_world(m_fake_arg);
}
}
}
ImGui::End();
}
20 changes: 19 additions & 1 deletion src/view/game/game_viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NINJACLOWN_VIEW_GAME_VIEWER_HPP

#include "map_viewer.hpp"
#include "game_menu.hpp"

#include "terminal_commands.hpp"

Expand Down Expand Up @@ -36,7 +37,10 @@ class game_viewer {

void restart() noexcept;

void show(bool show_debug_data);
/**
* Returns false if return to main menu was requested.
*/
bool show(bool show_debug_data);

void reload_sprites() {
m_map.reload_sprites();
Expand All @@ -62,10 +66,24 @@ class game_viewer {
m_map.acquire_overmap()->hide(handle);
}

/**
* Displays the rightmost bar (play, pause, step, ... buttons).
*/
void show_rightmost_bar() noexcept;

/**
* Shows the escape menu.
*/
void display_menu() noexcept;

private:
sf::RenderWindow &m_window;
state::holder& m_state;
map_viewer m_map;
game_menu m_menu;
bool m_showing_menu{false};
bool m_stay_in_game{true};

terminal_commands::argument_type m_fake_arg; //! Mostly valid argument passed to the terminal by the view, instead of by ImTerm

// events related
Expand Down
Loading

0 comments on commit 0ae643a

Please sign in to comment.