From 2f0850372ed814edabb6b3251212a12eacc777e1 Mon Sep 17 00:00:00 2001 From: Zane Zook Date: Thu, 9 Jun 2022 11:49:40 -0500 Subject: [PATCH 1/5] initial simple window build --- examples/ex_fonts.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 examples/ex_fonts.cpp diff --git a/examples/ex_fonts.cpp b/examples/ex_fonts.cpp new file mode 100644 index 0000000..ecc46a7 --- /dev/null +++ b/examples/ex_fonts.cpp @@ -0,0 +1,109 @@ +// MIT License +// +// Copyright (c) 2022 Mechatronics and Haptic Interfaces Lab - Rice University +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// Author(s): Zane Zook (gadzooks@rice.edu) + +//----------------------------------------------------------------------------- +// preprocessor directives +//----------------------------------------------------------------------------- +#define MAHI_GUI_NO_CONSOLE + +/// mahi includes +#include // mahi-gui include +#include // mahi-utility include + +// definition of filesystem import based on os +#if defined(__linux__) + #include + namespace fs = std::experimental::filesystem; +#else + #include + namespace fs = std::filesystem; +#endif + +// relevant MAHI namespaces to use +using namespace mahi::gui; +using namespace mahi::util; + +//----------------------------------------------------------------------------- +// text window class declaration +//----------------------------------------------------------------------------- +class TextWindow : public Application { +public: + // public variables + //----------------------------------- + std::string dummyText = "The quick brown fox jumps over the lazy dog"; + + // base methods + //----------------------------------- + /// constructor + TextWindow() : Application(500,500,"",false) { + ImGui::DisableViewports(); + loaded = load(); + } + + /// main update code + void update() override { + ImGui::BeginFixed("##TextWindow", ImVec2{0,0}, ImVec2{1000, 800}, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); + if (loaded) + { + // Display dummy text + ImGui::TextWrapped(dummyText.c_str()); + } + else { + ImGui::Text("Text window failed to load! :("); + } + ImGui::End(); + } + + /// Load in Likert config file + bool load() { + if (fs::exists("likert.json")) { + try { + std::ifstream file("likert.json"); + json j; + file >> j; + title = j["title"].get(); + // set_window_title(title); + // set_window_size((int)width, (int)height); + center_window(); + } + catch(...) { + return false; + } + return true; + } + return false; + } + + bool loaded = false; ///< was the text window config loaded? + std::string title; ///< survey title + float width, height; ///< window width/height +}; + +int main(int argc, char const *argv[]) +{ + // if there doesn't exist a "tw_config.json" file, make a default one + if (!fs::exists("tw_config.json")) { + json j; + j["title"] = "My Likert Survey"; + std::ofstream file("likert.json"); + if (file.is_open()) + file << std::setw(4) << j; + } + // run the GUI + TextWindow textWindow; + textWindow.run(); + return 0; +} From 2346b02e31dd0dee0aca6b2d99534e51295be4c1 Mon Sep 17 00:00:00 2001 From: Zane Zook Date: Thu, 9 Jun 2022 11:50:05 -0500 Subject: [PATCH 2/5] addition of example to cmake lists --- examples/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 73c6144..9296282 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -16,6 +16,7 @@ mahi_gui_example(implot_demo) mahi_gui_example(plots_benchmark) mahi_gui_example(dpi) mahi_gui_example(likert) +mahi_gui_example(fonts) mahi_gui_example(events) mahi_gui_example(hidden) mahi_gui_example(log_window) From f9825dbc0927801b623e90688426415da8bfb7ba Mon Sep 17 00:00:00 2001 From: Zane Zook Date: Thu, 9 Jun 2022 14:52:16 -0500 Subject: [PATCH 3/5] added font switch capability --- examples/ex_fonts.cpp | 170 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 151 insertions(+), 19 deletions(-) diff --git a/examples/ex_fonts.cpp b/examples/ex_fonts.cpp index ecc46a7..d783699 100644 --- a/examples/ex_fonts.cpp +++ b/examples/ex_fonts.cpp @@ -43,23 +43,95 @@ class TextWindow : public Application { public: // public variables //----------------------------------- - std::string dummyText = "The quick brown fox jumps over the lazy dog"; + + /// general GUI variables + std::string title_; // window title + float width_{1920}; // window width + float height_{1080}; // window height + bool loaded_{false}; // was the text window config loaded? + + /// font options from MAHI GUI + enum FontOptions { + Roboto_Regular = 0, + Roboto_Bold = 1, + Roboto_Italic = 2, + RobotoMono_Regular = 3, + RobotoMono_Bold = 4, // Note that RobotMono_Bold is imported by mahi-gui by default and is only included here for clarity + RobotoMono_Italic = 5 + }; + + /// text variables + std::vector> fonts_; // vector of font pointers + std::vector fontsize_; // vector holding fontsize options + int currentSizeIndex_{0}; // default fontsize index + FontOptions currentFont_{RobotoMono_Bold}; // default font + std::string dummyText_ = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "In ut malesuada magna. Proin et iaculis felis, nec faucibus nisi. " + "Sed gravida tempor risus ac accumsan. Pellentesque placerat faucibus eros sed fringilla. " + "Nam eu euismod orci, at vestibulum risus. Pellentesque ut aliquam diam, eget maximus urna. " + "Suspendisse potenti. Curabitur tempus porttitor neque non sodales. Praesent ac rutrum nisl. " + "Nam eleifend nisi et ligula fringilla iaculis id ac arcu. In ante justo, suscipit ac dui vel, feugiat viverra elit. " + "Fusce auctor magna vitae justo scelerisque rhoncus. Fusce vel feugiat odio."; + // base methods //----------------------------------- /// constructor - TextWindow() : Application(500,500,"",false) { + TextWindow() : Application(500,500,"",false) + { + // load config + loaded_ = load(); + + // ImGui ImGui::DisableViewports(); - loaded = load(); + ImGui::DisableDocking(); + ImPlot::StyleColorsDark(); } /// main update code void update() override { ImGui::BeginFixed("##TextWindow", ImVec2{0,0}, ImVec2{1000, 800}, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); - if (loaded) + if (loaded_) { + // show text size options + for (unsigned int i = 0; i < fontsize_.size(); ++i) + { + if (ImGui::RadioButton(std::to_string(fontsize_[i]).c_str(), currentSizeIndex_ == i) && currentSizeIndex_ != i) + currentSizeIndex_ = i; + if(i+1 != fontsize_.size()) ImGui::SameLine(); + } + // show font type options + { + if (ImGui::RadioButton("Roboto Regular", currentFont_ == Roboto_Regular) && currentFont_ != Roboto_Regular) + currentFont_ = Roboto_Regular; + ImGui::SameLine(); + if (ImGui::RadioButton("Roboto Bold", currentFont_ == Roboto_Bold) && currentFont_ != Roboto_Bold) + currentFont_ = Roboto_Bold; + ImGui::SameLine(); + if (ImGui::RadioButton("Roboto Italic", currentFont_ == Roboto_Italic) && currentFont_ != Roboto_Italic) + currentFont_ = Roboto_Italic; + ImGui::SameLine(); + if (ImGui::RadioButton("RobotoMono Regular", currentFont_ == RobotoMono_Regular) && currentFont_ != RobotoMono_Regular) + currentFont_ = RobotoMono_Regular; + ImGui::SameLine(); + if (ImGui::RadioButton("RobotoMono Bold", currentFont_ == RobotoMono_Bold) && currentFont_ != RobotoMono_Bold) + currentFont_ = RobotoMono_Bold; + ImGui::SameLine(); + if (ImGui::RadioButton("RobotoMono Italic", currentFont_ == RobotoMono_Italic) && currentFont_ != RobotoMono_Italic) + currentFont_ = RobotoMono_Italic; + } + ImGui::Separator(); + ImGui::Separator(); + + // change font used + ImGui::PushFont(fonts_[(int)currentFont_][currentSizeIndex_]); + // Display dummy text - ImGui::TextWrapped(dummyText.c_str()); + ImGui::TextWrapped(dummyText_.c_str()); + + // stop use of font + ImGui::PopFont(); } else { ImGui::Text("Text window failed to load! :("); @@ -67,38 +139,98 @@ class TextWindow : public Application { ImGui::End(); } - /// Load in Likert config file + /// Load in text window config file bool load() { - if (fs::exists("likert.json")) { + // checks for config file + if (fs::exists("font_config.json")) { + // load in config settings try { - std::ifstream file("likert.json"); + // open and load in config file + std::ifstream file("font_config.json"); json j; file >> j; - title = j["title"].get(); - // set_window_title(title); - // set_window_size((int)width, (int)height); + + // convert relevant variables for use in GUI + title_ = j["title"].get(); + width_ = j["width"].get(); + height_ = j["height"].get(); + fontsize_ = j["fontsize"].get>(); + currentSizeIndex_ = std::floor(fontsize_.size() / 2); + + // set window title and size + set_window_title(title_); + set_window_size((int)width_, (int)height_); center_window(); + + // load in all font options + { + ImGuiIO& io = ImGui::GetIO(); + std::vector fontset; + + // Roboto Regular Set + for(unsigned int i = 0; i < fontsize_.size(); i++) + fontset.push_back(io.Fonts->AddFontFromMemoryTTF(Roboto_Regular_ttf, Roboto_Regular_ttf_len, fontsize_[i])); + fonts_.push_back(fontset); + fontset.clear(); + + // Roboto Bold Set + for(unsigned int i = 0; i < fontsize_.size(); i++) + fontset.push_back(io.Fonts->AddFontFromMemoryTTF(Roboto_Bold_ttf, Roboto_Bold_ttf_len, fontsize_[i])); + fonts_.push_back(fontset); + fontset.clear(); + + // Roboto Italic Set + for(unsigned int i = 0; i < fontsize_.size(); i++) + fontset.push_back(io.Fonts->AddFontFromMemoryTTF(Roboto_Italic_ttf, Roboto_Italic_ttf_len, fontsize_[i])); + fonts_.push_back(fontset); + fontset.clear(); + + // RobotoMono Regular Set + for(unsigned int i = 0; i < fontsize_.size(); i++) + fontset.push_back(io.Fonts->AddFontFromMemoryTTF(RobotoMono_Regular_ttf, RobotoMono_Regular_ttf_len, fontsize_[i])); + fonts_.push_back(fontset); + fontset.clear(); + + // RobotoMono Bold Set + for(unsigned int i = 0; i < fontsize_.size(); i++) + fontset.push_back(io.Fonts->AddFontFromMemoryTTF(RobotoMono_Bold_ttf, RobotoMono_Bold_ttf_len, fontsize_[i])); + fonts_.push_back(fontset); + fontset.clear(); + + // RobotoMono Italic Set + for(unsigned int i = 0; i < fontsize_.size(); i++) + fontset.push_back(io.Fonts->AddFontFromMemoryTTF(RobotoMono_Italic_ttf, RobotoMono_Italic_ttf_len, fontsize_[i])); + fonts_.push_back(fontset); + fontset.clear(); + } + + // add information to mahi log + LOG(Info) << "Test Config file loaded"; } + // note if loading fails catch(...) { + // add information to mahi log + LOG(Fatal) << "Test Config file could not be loaded"; + + // return error return false; } return true; } return false; } - - bool loaded = false; ///< was the text window config loaded? - std::string title; ///< survey title - float width, height; ///< window width/height }; int main(int argc, char const *argv[]) { - // if there doesn't exist a "tw_config.json" file, make a default one - if (!fs::exists("tw_config.json")) { + // if there doesn't exist a "font_config.json" file, make a default one + if (!fs::exists("font_config.json")) { json j; - j["title"] = "My Likert Survey"; - std::ofstream file("likert.json"); + j["title"] = "Font Example"; + j["width"] = 1000; + j["height"] = 800; + j["fontsize"] = {8,10,12,14,16,20,24,28,36,48,72,100}; + std::ofstream file("font_config.json"); if (file.is_open()) file << std::setw(4) << j; } From 9faac46ea7d8c590095f30ac65e75f71a0ced2aa Mon Sep 17 00:00:00 2001 From: Zane Zook Date: Thu, 9 Jun 2022 14:54:29 -0500 Subject: [PATCH 4/5] comment adjustment --- examples/ex_fonts.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/ex_fonts.cpp b/examples/ex_fonts.cpp index d783699..c1444d6 100644 --- a/examples/ex_fonts.cpp +++ b/examples/ex_fonts.cpp @@ -90,7 +90,8 @@ class TextWindow : public Application { } /// main update code - void update() override { + void update() override + { ImGui::BeginFixed("##TextWindow", ImVec2{0,0}, ImVec2{1000, 800}, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); if (loaded_) { @@ -133,18 +134,22 @@ class TextWindow : public Application { // stop use of font ImGui::PopFont(); } - else { + else + { ImGui::Text("Text window failed to load! :("); } ImGui::End(); } - /// Load in text window config file - bool load() { + /// load in text window config file + bool load() + { // checks for config file - if (fs::exists("font_config.json")) { + if (fs::exists("font_config.json")) + { // load in config settings - try { + try + { // open and load in config file std::ifstream file("font_config.json"); json j; @@ -224,7 +229,8 @@ class TextWindow : public Application { int main(int argc, char const *argv[]) { // if there doesn't exist a "font_config.json" file, make a default one - if (!fs::exists("font_config.json")) { + if (!fs::exists("font_config.json")) + { json j; j["title"] = "Font Example"; j["width"] = 1000; From 84a4e62a941a07ffa6e97b8a2654bb31ddd6ea4b Mon Sep 17 00:00:00 2001 From: Zane Zook Date: Thu, 9 Jun 2022 14:56:59 -0500 Subject: [PATCH 5/5] more comments --- examples/ex_fonts.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/ex_fonts.cpp b/examples/ex_fonts.cpp index c1444d6..66bc211 100644 --- a/examples/ex_fonts.cpp +++ b/examples/ex_fonts.cpp @@ -226,6 +226,10 @@ class TextWindow : public Application { } }; + +//----------------------------------------------------------------------------- +// main method +//----------------------------------------------------------------------------- int main(int argc, char const *argv[]) { // if there doesn't exist a "font_config.json" file, make a default one @@ -240,6 +244,7 @@ int main(int argc, char const *argv[]) if (file.is_open()) file << std::setw(4) << j; } + // run the GUI TextWindow textWindow; textWindow.run();