add icons and navigation
This commit is contained in:
134
main.cpp
134
main.cpp
@@ -29,6 +29,86 @@ struct AppState {
|
||||
std::filesystem::path assets_path;
|
||||
} g_app;
|
||||
|
||||
// Custom system interface to enable logging
|
||||
class LoggingSystemInterface : public Rml::SystemInterface {
|
||||
public:
|
||||
LoggingSystemInterface(Rml::SystemInterface* backend) : backend_(backend) {}
|
||||
|
||||
double GetElapsedTime() override { return backend_->GetElapsedTime(); }
|
||||
|
||||
bool LogMessage(Rml::Log::Type type, const Rml::String& message) override {
|
||||
const char* type_str = "";
|
||||
switch (type) {
|
||||
case Rml::Log::LT_ERROR: type_str = "ERROR"; break;
|
||||
case Rml::Log::LT_WARNING: type_str = "WARNING"; break;
|
||||
case Rml::Log::LT_INFO: type_str = "INFO"; break;
|
||||
case Rml::Log::LT_DEBUG: type_str = "DEBUG"; break;
|
||||
default: type_str = "LOG"; break;
|
||||
}
|
||||
std::println("[RmlUi {}] {}", type_str, message);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Forward JoinPath to fix Windows path issues
|
||||
void JoinPath(Rml::String& translated_path, const Rml::String& document_path, const Rml::String& path) override {
|
||||
// Fix paths where colon was converted to pipe (D| -> D:)
|
||||
std::string fixed_path = path;
|
||||
if (fixed_path.length() >= 2 && fixed_path[1] == '|') {
|
||||
fixed_path[1] = ':';
|
||||
}
|
||||
|
||||
std::string fixed_doc = document_path;
|
||||
if (fixed_doc.length() >= 2 && fixed_doc[1] == '|') {
|
||||
fixed_doc[1] = ':';
|
||||
}
|
||||
|
||||
// Use std::filesystem to join paths properly and normalize (resolve ..)
|
||||
std::filesystem::path doc_dir = std::filesystem::path(fixed_doc).parent_path();
|
||||
std::filesystem::path result = (doc_dir / fixed_path).lexically_normal();
|
||||
translated_path = result.generic_string();
|
||||
std::println("JoinPath: {} + {} -> {}", fixed_doc, fixed_path, translated_path);
|
||||
}
|
||||
|
||||
private:
|
||||
Rml::SystemInterface* backend_;
|
||||
};
|
||||
|
||||
static LoggingSystemInterface* g_logging_interface = nullptr;
|
||||
|
||||
// Custom file interface to fix Windows path issues (D| -> D:)
|
||||
class WindowsFileInterface : public Rml::FileInterface {
|
||||
public:
|
||||
Rml::FileHandle Open(const Rml::String& path) override {
|
||||
// Fix paths where colon was converted to pipe (D| -> D:)
|
||||
std::string fixed_path = path;
|
||||
if (fixed_path.length() >= 2 && fixed_path[1] == '|') {
|
||||
fixed_path[1] = ':';
|
||||
}
|
||||
std::println("FileInterface::Open: {} -> {}", path, fixed_path);
|
||||
|
||||
FILE* fp = fopen(fixed_path.c_str(), "rb");
|
||||
return reinterpret_cast<Rml::FileHandle>(fp);
|
||||
}
|
||||
|
||||
void Close(Rml::FileHandle file) override {
|
||||
fclose(reinterpret_cast<FILE*>(file));
|
||||
}
|
||||
|
||||
size_t Read(void* buffer, size_t size, Rml::FileHandle file) override {
|
||||
return fread(buffer, 1, size, reinterpret_cast<FILE*>(file));
|
||||
}
|
||||
|
||||
bool Seek(Rml::FileHandle file, long offset, int origin) override {
|
||||
return fseek(reinterpret_cast<FILE*>(file), offset, origin) == 0;
|
||||
}
|
||||
|
||||
size_t Tell(Rml::FileHandle file) override {
|
||||
return ftell(reinterpret_cast<FILE*>(file));
|
||||
}
|
||||
};
|
||||
|
||||
static WindowsFileInterface* g_file_interface = nullptr;
|
||||
|
||||
void load_fonts(const std::filesystem::path& dir)
|
||||
{
|
||||
for (const auto& file : std::filesystem::directory_iterator(dir))
|
||||
@@ -44,13 +124,15 @@ void load_fonts(const std::filesystem::path& dir)
|
||||
// Path is relative to assets folder
|
||||
int lua_loadScreen(lua_State* L)
|
||||
{
|
||||
std::println("lua_loadScreen called!");
|
||||
const char* path = luaL_checkstring(L, 1);
|
||||
std::println("Loading: {}", path);
|
||||
|
||||
std::filesystem::path full_path = g_app.assets_path / path;
|
||||
|
||||
if (!std::filesystem::exists(full_path))
|
||||
{
|
||||
std::println("Screen not found: {}", full_path.string());
|
||||
std::println("Screen not found: {}", full_path.generic_string());
|
||||
lua_pushboolean(L, false);
|
||||
return 1;
|
||||
}
|
||||
@@ -62,8 +144,10 @@ int lua_loadScreen(lua_State* L)
|
||||
g_app.document = nullptr;
|
||||
}
|
||||
|
||||
// Load new document
|
||||
g_app.document = g_app.context->LoadDocument(full_path.string());
|
||||
// Load new document with absolute path
|
||||
std::string full_path_str = full_path.generic_string();
|
||||
std::println("Full path: {}", full_path_str);
|
||||
g_app.document = g_app.context->LoadDocument(full_path_str);
|
||||
if (g_app.document)
|
||||
{
|
||||
g_app.document->Show();
|
||||
@@ -233,10 +317,15 @@ int main(const int argc, const char* argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Rml::SetSystemInterface(Backend::GetSystemInterface());
|
||||
// Use custom interfaces
|
||||
g_logging_interface = new LoggingSystemInterface(Backend::GetSystemInterface());
|
||||
g_file_interface = new WindowsFileInterface();
|
||||
Rml::SetSystemInterface(g_logging_interface);
|
||||
Rml::SetFileInterface(g_file_interface);
|
||||
Rml::SetRenderInterface(Backend::GetRenderInterface());
|
||||
Rml::Initialise();
|
||||
Rml::Lua::Initialise();
|
||||
std::println("RmlUi and Lua initialized");
|
||||
|
||||
g_app.context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
|
||||
if (!g_app.context)
|
||||
@@ -249,31 +338,46 @@ int main(const int argc, const char* argv[])
|
||||
// Register custom Lua functions
|
||||
registerLuaFunctions();
|
||||
|
||||
// Find the assets folder by checking for fonts
|
||||
// Find the assets folder by checking for fonts/ subdirectory
|
||||
g_app.assets_path = std::filesystem::absolute(file.parent_path());
|
||||
|
||||
// Walk up the directory tree to find a folder containing .ttf fonts
|
||||
// Walk up the directory tree to find a folder containing a fonts/ subdirectory with .ttf files
|
||||
std::filesystem::path fonts_path;
|
||||
while (!g_app.assets_path.empty() && g_app.assets_path.has_parent_path())
|
||||
{
|
||||
bool has_fonts = false;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(g_app.assets_path))
|
||||
fonts_path = g_app.assets_path / "fonts";
|
||||
if (std::filesystem::exists(fonts_path) && std::filesystem::is_directory(fonts_path))
|
||||
{
|
||||
if (entry.path().extension() == ".ttf")
|
||||
bool has_fonts = false;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(fonts_path))
|
||||
{
|
||||
has_fonts = true;
|
||||
break;
|
||||
if (entry.path().extension() == ".ttf")
|
||||
{
|
||||
has_fonts = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (has_fonts) break;
|
||||
}
|
||||
if (has_fonts) break;
|
||||
g_app.assets_path = g_app.assets_path.parent_path();
|
||||
}
|
||||
|
||||
load_fonts(g_app.assets_path);
|
||||
load_fonts(fonts_path);
|
||||
|
||||
g_app.document = g_app.context->LoadDocument(file.string());
|
||||
// Load document with absolute path
|
||||
std::filesystem::path abs_file = std::filesystem::absolute(file);
|
||||
std::string abs_file_str = abs_file.generic_string();
|
||||
std::println("Loading document: {}", abs_file_str);
|
||||
std::println("Assets path: {}", g_app.assets_path.generic_string());
|
||||
g_app.document = g_app.context->LoadDocument(abs_file_str);
|
||||
if (g_app.document)
|
||||
{
|
||||
g_app.document->Show();
|
||||
std::println("Document loaded successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
std::println("Failed to load document!");
|
||||
}
|
||||
|
||||
// Dump mode: render and capture screenshot
|
||||
@@ -350,7 +454,7 @@ int main(const int argc, const char* argv[])
|
||||
g_app.context->UnloadDocument(g_app.document);
|
||||
g_app.context->Update();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
g_app.document = g_app.context->LoadDocument(file.string());
|
||||
g_app.document = g_app.context->LoadDocument(abs_file_str);
|
||||
if (g_app.document)
|
||||
{
|
||||
g_app.document->ReloadStyleSheet();
|
||||
|
||||
Reference in New Issue
Block a user