riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
ImGuiINI.hpp
Go to the documentation of this file.
1 /* Copyright (C) 2021 Thomas Friedrich, Chu-Ping Yu,
2  * University of Antwerp - All Rights Reserved.
3  * You may use, distribute and modify
4  * this code under the terms of the GPL3 license.
5  * You should have received a copy of the GPL3 license with
6  * this file. If not, please visit:
7  * https://www.gnu.org/licenses/gpl-3.0.en.html
8  *
9  * Authors:
10  * Thomas Friedrich <thomas.friedrich@uantwerpen.be>
11  * Chu-Ping Yu <chu-ping.yu@uantwerpen.be>
12  */
13 
14 #include "imgui.h"
15 #include "ini.h"
16 #include <string>
17 
18 namespace ImGuiINI
19 {
20  // Styles
21  void set_style(int style_idx)
22  {
23  switch (style_idx)
24  {
25  case 0:
26  ImGui::StyleColorsDark();
27  break;
28  case 1:
29  ImGui::StyleColorsLight();
30  break;
31  case 2:
32  ImGui::StyleColorsClassic();
33  break;
34  }
35  }
36 
37  bool ShowStyleSelector(const char *label, int &style_idx, mINI::INIStructure &ini_cfg)
38  {
39  if (ImGui::Combo(label, &style_idx, "Dark\0Light\0Classic\0"))
40  {
41  set_style(style_idx);
42  ini_cfg["Appearance"]["Style"] = std::to_string(style_idx);
43  return true;
44  }
45  return false;
46  }
47 
48  // Fonts
49  const char *font_names[6] = {
50  "Karla-Regular",
51  "Roboto-Medium",
52  "Cousine-Regular",
53  "DroidSans",
54  "ProggyClean",
55  "ProggyTiny"};
56 
57  bool ShowFontSelector(const char *label, int &selectedFont, mINI::INIStructure &ini_cfg)
58  {
59  ImGuiIO &io = ImGui::GetIO();
60  ImFont *font_current = ImGui::GetFont();
61  if (ImGui::BeginCombo(label, font_names[selectedFont]))
62  {
63  for (int n = 0; n < io.Fonts->Fonts.Size; n++)
64  {
65  ImFont *font = io.Fonts->Fonts[n];
66  ImGui::PushID((void *)font);
67  if (ImGui::Selectable(font_names[n], font == font_current))
68  {
69  io.FontDefault = font;
70  selectedFont = n;
71  }
72  ImGui::PopID();
73  }
74  ImGui::EndCombo();
75  ini_cfg["Appearance"]["Font"] = std::to_string(selectedFont);
76  return true;
77  }
78  return false;
79  }
80 
81  void set_font(int font_idx)
82  {
83  ImGuiIO &io = ImGui::GetIO();
84  ImFont *font = io.Fonts->Fonts[font_idx];
85  io.FontDefault = font;
86  }
87 
88  // Helper functions for conversions
89  template <typename T>
90  std::vector<T> split2T(std::string &s, char delimiter)
91  {
92  std::vector<T> values;
93  std::string token;
94  std::istringstream tokenStream(s);
95  while (std::getline(tokenStream, token, delimiter))
96  {
97  values.push_back((T)std::stod(token));
98  }
99  return values;
100  }
101 
102  std::string ImVec2string(const ImVec4 &v)
103  {
104  std::string s = std::to_string(v.x) + "," + std::to_string(v.y) + "," + std::to_string(v.z) + "," + std::to_string(v.w);
105  return s;
106  }
107 
108  std::string ImVec2string(const ImVec2 &v)
109  {
110  std::string s = std::to_string(v.x) + "," + std::to_string(v.y);
111  return s;
112  }
113 
114  // Check if INI has a stored value for a given key and update the value if it does
115  // Strings
116  void check_ini_setting(mINI::INIStructure &ini_cfg, std::string section, std::string key, char *value)
117  {
118  if (ini_cfg.has(section))
119  {
120  if (ini_cfg[section].has(key))
121  {
122  value = &ini_cfg[section][key][0];
123  }
124  ini_cfg[section][key] = std::string(value);
125  }
126  }
127 
128  void check_ini_setting(mINI::INIStructure &ini_cfg, std::string section, std::string key, std::string value)
129  {
130  if (ini_cfg.has(section))
131  {
132  if (ini_cfg[section].has(key))
133  {
134  value = ini_cfg[section][key];
135  }
136  ini_cfg[section][key] = value;
137  }
138  }
139 
140  // General Numeric types
141  template <typename T>
142  void check_ini_setting(mINI::INIStructure &ini_cfg, std::string section, std::string key, T &value)
143  {
144  if (ini_cfg.has(section))
145  {
146  if (ini_cfg[section].has(key))
147  {
148  value = (T)stod(ini_cfg[section][key]);
149  }
150  ini_cfg[section][key] = std::to_string(value);
151  }
152  }
153 
154  // ImVec4
155  void check_ini_setting(mINI::INIStructure &ini_cfg, std::string section, std::string key, ImVec4 &value)
156  {
157  if (ini_cfg.has(section))
158  {
159  if (ini_cfg[section].has(key))
160  {
161  std::vector<float> val_vec = split2T<float>(ini_cfg[section][key], ',');
162  value = ImVec4(val_vec[0], val_vec[1], val_vec[2], val_vec[3]);
163  }
164  ini_cfg[section][key] = ImVec2string(value);
165  }
166  }
167 
168  // ImVec2
169  void check_ini_setting(mINI::INIStructure &ini_cfg, std::string section, std::string key, ImVec2 &value)
170  {
171  if (ini_cfg.has(section))
172  {
173  if (ini_cfg[section].has(key))
174  {
175  std::vector<float> val_vec = split2T<float>(ini_cfg[section][key], ',');
176  value = ImVec2(val_vec[0], val_vec[1]);
177  }
178  ini_cfg[section][key] = ImVec2string(value);
179  }
180  }
181 }
std::string ImVec2string(const ImVec4 &v)
Definition: ImGuiINI.hpp:102
void check_ini_setting(mINI::INIStructure &ini_cfg, std::string section, std::string key, char *value)
Definition: ImGuiINI.hpp:116
bool ShowFontSelector(const char *label, int &selectedFont, mINI::INIStructure &ini_cfg)
Definition: ImGuiINI.hpp:57
bool ShowStyleSelector(const char *label, int &style_idx, mINI::INIStructure &ini_cfg)
Definition: ImGuiINI.hpp:37
void set_font(int font_idx)
Definition: ImGuiINI.hpp:81
std::vector< T > split2T(std::string &s, char delimiter)
Definition: ImGuiINI.hpp:90
void set_style(int style_idx)
Definition: ImGuiINI.hpp:21
const char * font_names[6]
Definition: ImGuiINI.hpp:49