OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Menu.cpp
Go to the documentation of this file.
1 
8 #include "imgui/imgui.h"
9 
10 #include "global.h"
11 #include "Console.h"
12 #include "Game.h"
13 #include "Log.h"
14 #include "RunTime.h"
15 #include "UI.h"
16 #include "TextureManager.h"
17 #include "system/Font.h"
18 #include "system/Window.h"
19 #include "utils/random.h"
20 #include "utils/strings.h"
21 #include "Menu.h"
22 
23 const glm::vec4 Menu::textColor(0.5f, 0.7f, 1.0f, 1.0f);
24 const glm::vec4 Menu::selectedColor(1.0f, 0.0f, 0.0f, 1.0f);
25 
26 bool Menu::visible = false;
27 Folder* Menu::mapFolder = nullptr;
28 std::vector<Script> Menu::scripts;
29 std::vector<Folder> Menu::paths;
30 std::vector<int> Menu::images;
31 
33  shutdown();
35 
36  std::vector<File> files;
37  mapFolder->findRecursiveFilesEndingWith(files, "tombpc.dat");
38  for (auto& f : files) {
39  scripts.emplace_back();
40  auto s = scripts.size() - 1;
41 
42  images.push_back(-1);
43  auto i = images.size() - 1;
44 
45  std::string path = convertPathDelimiter(removeLastPathElement(f.getPath()));
46  paths.emplace_back(path);
47  auto p = paths.size() - 1;
48 
49  if (scripts.at(s).load(f.getPath()) != 0) {
50  Log::get(LOG_ERROR) << "Invalid Script: \"" << f.getPath()
51  << "\"!" << Log::endl;
52  scripts.pop_back();
53  } else {
54  // Load one of the pictures of this level
55  std::vector<File> texFiles;
56  paths.at(p).findRecursiveFilesEndingWith(texFiles, ".pcx");
57  paths.at(p).findRecursiveFilesEndingWith(texFiles, ".bmp");
58  paths.at(p).findRecursiveFilesEndingWith(texFiles, ".png");
59  paths.at(p).findRecursiveFilesEndingWith(texFiles, ".tga");
60  paths.at(p).findRecursiveFilesEndingWith(texFiles, ".jpg");
61 
62  if (texFiles.size() > 0) {
63  int which = randomInteger(texFiles.size() - 1);
64  images.at(i) = TextureManager::loadImage(texFiles.at(which).getPath(),
66  }
67  }
68  }
69 
70  return 0;
71 }
72 
74  if (mapFolder != nullptr) {
75  delete mapFolder;
76  mapFolder = nullptr;
77  }
78 
79  scripts.clear();
80  paths.clear();
81  images.clear();
82 }
83 
84 void Menu::display() {
85  if (!visible)
86  return;
87 
88  glm::vec2 size = Window::getSize();
89  if (!ImGui::Begin("Menu", nullptr, ImVec2(size.x, size.y), -1.0f, ImGuiWindowFlags_NoTitleBar
92  ImGui::End();
93  return;
94  }
95 
97  ImGui::SetWindowSize(ImVec2(size.x, size.y));
98 
99  // Draw heading with version string
100  auto headingSize = ImGui::CalcTextSize(VERSION);
101  headingSize.x *= 3.0f; headingSize.y *= 3.0f;
103  ImGui::SameLine(0, (size.x / 2) - (headingSize.x / 2));
104  ImGui::TextColored(textColor, VERSION);
106 
107  static ImGuiFs::Dialog* dialog = nullptr;
108  const bool browseButtonPressed = ImGui::Button("Select Level File");
109  if (browseButtonPressed) {
110  if (dialog != nullptr)
111  delete dialog;
112  dialog = new ImGuiFs::Dialog();
113  }
114  if (dialog) {
115  dialog->chooseFileDialog(browseButtonPressed, RunTime::getPakDir().c_str(),
116  ".phd;.psx;.tub;.tr2", nullptr, ImVec2(400, 300), ImGui::GetMousePos());
117  std::string selectedFile = dialog->getChosenPath();
118  if (selectedFile.length() > 0) {
119  Game::loadLevel(selectedFile);
120  delete dialog;
121  dialog = nullptr;
122  }
123  }
124  ImGui::SameLine();
125  if (ImGui::Button("Toggle Console")) {
127  }
128  ImGui::SameLine();
129  if (ImGui::Button("Toggle Debug Window")) {
131  }
132 
134 
135  // Set up columns for game list
136  ImGui::Columns(3, "menuscripts");
137  static bool offsets = false;
138  if (!offsets) {
139  ImGui::SetColumnOffset(1, 140.0f);
140  ImGui::SetColumnOffset(2, size.x - 200.0f);
141  offsets = true;
142  }
143 
144  // List found games
145  for (int i = 0; i < scripts.size(); i++) {
146  ImGui::PushID(i);
147  Script& s = scripts.at(i);
148 
149  if (images.at(i) >= 0) {
151  ImGui::Image(bm, ImVec2(128, 128));
152  }
154 
155  ImGui::TextWrapped("Language: %s", s.getLanguage().c_str());
156  ImGui::TextWrapped("Description: %s", s.getDescription().c_str());
157 
158  if (ImGui::TreeNode("", "%d Levels", s.levelCount())) {
159  for (int l = 0; l < s.levelCount(); l++) {
160  ImGui::PushID(l);
161  ImGui::Bullet();
162  ImGui::TextWrapped("%s (%s)", s.getLevelName(l).c_str(), s.getLevelFilename(l).c_str());
163  ImGui::SameLine();
164  if (ImGui::Button("Play level")) {
165  std::vector<File> levelFiles;
166  paths.at(i).findRecursiveFilesEndingWith(levelFiles,
168  if (levelFiles.size() == 0) {
169  Log::get(LOG_ERROR) << "Could not find level \""
171  << "\"!" << Log::endl;
172  } else {
173  Game::loadLevel(levelFiles.at(0).getPath());
174  }
175  }
176  ImGui::PopID();
177  }
178  ImGui::TreePop();
179  }
180 
181  if (ImGui::TreeNode("", "%d Cut-Scenes", s.cutsceneCount())) {
182  for (int c = 0; c < s.cutsceneCount(); c++) {
183  ImGui::Bullet();
184  ImGui::TextWrapped("%s", s.getCutsceneFilename(c).c_str());
185  }
186  ImGui::TreePop();
187  }
188 
189  if (ImGui::TreeNode("", "%d Videos", s.videoCount())) {
190  for (int v = 0; v < s.videoCount(); v++) {
191  ImGui::Bullet();
192  ImGui::TextWrapped("%s", s.getVideoFilename(v).c_str());
193  }
194  ImGui::TreePop();
195  }
196 
197  if (ImGui::TreeNode("", "%d Pictures", s.pictureCount())) {
198  for (int p = 0; p < s.pictureCount(); p++) {
199  ImGui::Bullet();
200  ImGui::TextWrapped("%s", s.getPictureFilename(p).c_str());
201  }
202  ImGui::TreePop();
203  }
204 
205  if (ImGui::TreeNode("", "%d Titles", s.titleCount())) {
206  for (int t = 0; t < s.titleCount(); t++) {
207  ImGui::Bullet();
208  ImGui::TextWrapped("%s", s.getTitleFilename(t).c_str());
209  }
210  ImGui::TreePop();
211  }
212 
214  ImGui::TextWrapped("Real Gameplay not yet implemented!");
216 
217  if (i < (scripts.size() - 1))
219  ImGui::PopID();
220  }
221  ImGui::Columns(1);
222 
223  if (scripts.size() == 0) {
224  ImGui::TextWrapped("OpenRaiders built-in TombRaider-Script detection mechanism could not find any suitable TR2/TR3 script files (called \"TOMBPC.DAT\") in your PAK folder \"%s\"! Use the \"Select Level File\" Button to load a single level without starting the entire game.",
225  RunTime::getPakDir().c_str());
226  }
227 
228  ImGui::End();
229 }
230 
static void setVisible(bool v)
Definition: UI.h:30
unsigned int titleCount()
Definition: Script.cpp:261
const char * chooseFileDialog(bool dialogTriggerButton, const char *directory=NULL, const char *fileFilterExtensionString=NULL, const char *windowTitle=NULL, const ImVec2 &windowSize=ImVec2(-1,-1), const ImVec2 &windowPos=ImVec2(-1,-1), const float windowAlpha=0.875f)
std::string convertPathDelimiter(std::string s)
Definition: strings.cpp:60
static void display()
Definition: Menu.cpp:84
String handling utilities.
IMGUI_API void SetWindowPos(const ImVec2 &pos, ImGuiSetCond cond=0)
Definition: imgui.cpp:3510
int randomInteger(int max, int min=0)
Definition: random.cpp:20
IMGUI_API void NextColumn()
Definition: imgui.cpp:6575
std::string getTitleFilename(unsigned int i)
Definition: Script.cpp:265
IMGUI_API void SetColumnOffset(int column_index, float offset_x)
Definition: imgui.cpp:6636
static std::string getPakDir()
Definition: RunTime.h:26
IMGUI_API void TextWrapped(const char *fmt,...)
Definition: imgui.cpp:3832
IMGUI_API void End()
Definition: imgui.cpp:3137
static bool isVisible()
Definition: UI.h:31
IMGUI_API ImVec2 GetMousePos()
Definition: imgui.cpp:2499
static const glm::vec4 textColor
Definition: Menu.h:25
std::string getLevelFilename(unsigned int i)
Definition: Script.cpp:238
std::string getLevelName(unsigned int i)
Definition: Script.cpp:233
Main Menu.
Included everywhere.
static LogLevel & get(int level)
Definition: Log.cpp:14
static Folder * mapFolder
Definition: Menu.h:30
static int loadImage(std::string filename, TextureStorage s=TextureStorage::GAME, int slot=-1)
const char * getChosenPath() const
Definition: imgui.h:50
Global Logging Utility.
IMGUI_API void TextColored(const ImVec4 &col, const char *fmt,...)
Definition: imgui.cpp:3817
Windowing Interface.
Console Window.
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0), bool repeat_when_held=false)
Definition: imgui.cpp:4055
unsigned int pictureCount()
Definition: Script.cpp:243
IMGUI_API void SetWindowFontScale(float scale)
Definition: imgui.cpp:3697
static std::vector< Script > scripts
Definition: Menu.h:31
static bool isVisible()
Definition: Console.h:20
static BufferManager * getBufferManager(int tex, TextureStorage store)
IMGUI_API void PopID()
Definition: imgui.cpp:4588
void findRecursiveFilesEndingWith(std::vector< File > &found, std::string end, bool casesensitive=false)
IMGUI_API void Bullet()
Definition: imgui.cpp:4445
Font Interface.
IMGUI_API void TreePop()
Definition: imgui.cpp:6777
static const char endl
Definition: Log.h:35
Runtime Configuration Storage.
std::string removeLastPathElement(std::string s)
Definition: strings.cpp:46
IMGUI_API void Columns(int count=1, const char *id=NULL, bool border=true)
Definition: imgui.cpp:6674
IMGUI_API void SameLine(int column_x=0, int spacing_w=-1)
Definition: imgui.cpp:6551
unsigned int videoCount()
Definition: Script.cpp:270
std::string getLanguage()
Definition: Script.cpp:350
#define LOG_ERROR
Definition: Log.h:19
static std::vector< int > images
Definition: Menu.h:33
Definition: Folder.h:28
std::string getDescription()
Definition: Script.h:126
IMGUI_API ImVec2 CalcTextSize(const char *text, const char *text_end=NULL, bool hide_text_after_double_hash=false, float wrap_width=-1.0f)
Definition: imgui.cpp:2324
UI/Event Manager.
static int initialize()
Definition: Menu.cpp:32
Texture Registry.
IMGUI_API void SetWindowSize(const ImVec2 &size, ImGuiSetCond cond=0)
Definition: imgui.cpp:3550
IMGUI_API void Separator()
Definition: imgui.cpp:6448
IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &uv0=ImVec2(0, 0), const ImVec2 &uv1=ImVec2(1, 1), const ImVec4 &tint_col=ImVec4(1, 1, 1, 1), const ImVec4 &border_col=ImVec4(0, 0, 0, 0))
Definition: imgui.cpp:4163
unsigned int cutsceneCount()
Definition: Script.cpp:252
std::string getPictureFilename(unsigned int i)
Definition: Script.cpp:247
static void shutdown()
Definition: Menu.cpp:73
static int loadLevel(std::string level)
Definition: Game.cpp:41
unsigned int levelCount()
Definition: Script.cpp:229
Random number generation.
static const glm::vec4 selectedColor
Definition: Menu.h:26
static void setVisible(bool v)
Definition: Console.h:21
static bool visible
Definition: Menu.h:29
std::string getCutsceneFilename(unsigned int i)
Definition: Script.cpp:256
IMGUI_API bool Begin(const char *name="Debug", bool *p_opened=NULL, const ImVec2 &initial_size=ImVec2(0, 0), float bg_alpha=-1.0f, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:2735
Gameplay Handler.
std::string getLastPathElement(std::string s)
Definition: strings.cpp:53
std::string getVideoFilename(unsigned int i)
Definition: Script.cpp:274
static std::vector< Folder > paths
Definition: Menu.h:32
static glm::i32vec2 getSize()
Definition: Window.cpp:71
IMGUI_API bool TreeNode(const char *str_label_id)
Definition: imgui.cpp:4557
Game script loader.
Definition: Script.h:21
IMGUI_API void PushID(const char *str_id)
Definition: imgui.cpp:4569