Loading [MathJax]/extensions/MathMenu.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LoadMenu.cxx
Go to the documentation of this file.
1 #include "LoadMenu.hxx"
2 
3 #include "imgui.h"
4 #include "imgui_impl_sdl.h"
5 #include "imgui_impl_sdlrenderer.h"
6 
7 #include "Settings.hxx"
8 #include "MapFunctions.hxx"
9 #include "Constants.hxx"
10 
11 #include <filesystem>
12 
13 #ifdef USE_AUDIO
14 #include "../services/AudioMixer.hxx"
15 #endif
16 
17 namespace fs = std::filesystem;
18 namespace ui = ImGui;
19 
21 {
22  // still do nothing
23 }
24 
25 void LoadMenu::draw() const
26 {
27  m_result = e_none;
28  m_filename.clear();
29 
30  ImVec2 windowSize(500, 500);
31  ImVec2 screenSize = ui::GetIO().DisplaySize;
32 
33  auto &uiManager = UIManager::instance();
34 
35  const auto &layout = uiManager.getLayouts()["LoadDialogButtons"];
36  ui::SetNextWindowPos(ImVec2((screenSize.x - windowSize.x) / 2, (screenSize.y - windowSize.y) / 2));
37  ui::SetNextWindowSize(windowSize);
38 
39  constexpr float btnSide = 40;
40  const ImVec2 buttonSize(windowSize.x - btnSide * 3, btnSide);
41  const ImVec2 buttonOffset((windowSize.x - buttonSize.x) / 2, buttonSize.y / 2);
42  const ImVec2 widgetSize((windowSize.x / 2) - 8, 20);
43 
44  ui::PushFont(layout.font);
45  ui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
46  ui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
47  ui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, buttonOffset.y));
48 
49  bool open = true;
50  ui::BeginCt("LoadDialog", &open,
51  ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar |
52  ImGuiWindowFlags_NoScrollWithMouse);
53 
54  // empty header
55  ui::PushItemWidth(widgetSize.x);
56 
57  // header label
58  auto textWidth = ImGui::CalcTextSize("Load city").x;
59  ImGui::SetCursorPosX((windowSize.x - textWidth) * 0.5f);
60  ui::LabelText("##loadcitylbl", "Load city");
61 
62  ImGui::BeginChild("##loadbuttons", {0.f, windowSize.y * 0.6f}, false, ImGuiWindowFlags_NoTitleBar);
63 
64  std::string path = CYTOPIA_DATA_DIR + "/saves";
65  for (const auto &entry : fs::directory_iterator(path))
66  {
67  if (!fs::is_regular_file(entry) || entry.path().extension() != ".cts")
68  continue;
69 
70  ui::Dummy({btnSide / 2.f, 0.f});
71  ui::SameLine();
72  if (ui::ButtonCt(entry.path().filename().string().c_str(), buttonSize))
73  {
74  m_filename = entry.path().filename().string();
76  }
77 
78  ui::SameLine();
79  ui::Dummy({btnSide / 2.f, 0.f});
80  ui::SameLine();
81  if (ui::ButtonCt("X", {btnSide, btnSide}))
82  {
83  m_filename = entry.path().filename().string();
85  }
86  }
87 
88  ui::EndChild();
89  ui::PopItemWidth();
90 
91  ImVec2 btnSize(windowSize.x / 4, btnSide);
92 
93  ui::SetCursorPosY(windowSize.y - btnSize.y * 2);
94  ui::Dummy({(windowSize.x - btnSize.x) / 2.f, 0.f});
95  ui::SameLine();
96  if (ui::ButtonCt("OK", btnSize))
97  {
98  m_result = e_close;
99  }
100 
101  ui::PopFont();
102  ui::PopStyleVar(3);
103 
104  ui::End();
105 }
106 
108 {
109  // still do nothing
110 }
LoadMenu::LoadMenu
LoadMenu()
Definition: LoadMenu.cxx:20
LoadMenu::e_load_file
@ e_load_file
Definition: LoadMenu.hxx:13
LoadMenu::m_filename
std::string m_filename
Definition: LoadMenu.hxx:25
MapFunctions.hxx
LoadMenu::e_close
@ e_close
Definition: LoadMenu.hxx:12
LoadMenu.hxx
LoadMenu::draw
void draw() const override
Definition: LoadMenu.cxx:25
LoadMenu::m_result
Result m_result
Definition: LoadMenu.hxx:26
Settings.hxx
LoadMenu::~LoadMenu
~LoadMenu()
Definition: LoadMenu.cxx:107
CYTOPIA_DATA_DIR
const std::string CYTOPIA_DATA_DIR
Definition: Constants.hxx:17
Singleton< UIManager >::instance
static UIManager & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
LoadMenu::e_none
@ e_none
Definition: LoadMenu.hxx:11
string
std::string string
Definition: AudioConfig.hxx:14
Constants.hxx
LoadMenu::e_delete_file
@ e_delete_file
Definition: LoadMenu.hxx:14