ofxOui - UI Components for OpenFrameworks

ofxOuiCommon.hpp
Go to the documentation of this file.
1 /*
2  * ofxOuiCommon.hpp
3  *
4  * Jui's Common definition: Component types, anchors and shapes
5  *
6  * This code has been authored by Jules Francoise <jfrancoi@sfu.ca>
7  * during his postdoc contract at Simon Fraser University (MovingStories
8  * project).
9  * See:
10  * http://julesfrancoise.com/
11  * http://movingstories.ca/
12  *
13  * Copyright (C) 2016 Jules Francoise.
14  *
15  * This Source Code Form is subject to the terms of the Mozilla Public
16  * License, v. 2.0. If a copy of the MPL was not distributed with this
17  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
18  */
19 
20 #ifndef ofxOuiCommon_h
21 #define ofxOuiCommon_h
22 
23 #include "ofMain.h"
24 
25 namespace ofxOui {
26 
32 extern bool RetinaEnabled;
33 
38 enum class Type {
39  Label,
40  Button,
41  CheckBox,
43  Matrix,
44  DropDown,
45  Numbox,
46  Slider,
48  Bpf,
49  TextInput,
50  Waveform
51 };
52 
57 enum class Anchor {
58  TopLeft,
59  TopCenter,
60  TopRight,
61  MiddleLeft,
64  BottomLeft,
67 };
68 
73 enum class Shape { Circle, Rectangle, RectRounded };
74 
79 class Globals {
80  public:
85  static void setRetina(bool enabled) {
86  getInstance().retina_enabled_ = enabled;
87  getInstance().reloadFonts();
88  }
89 
94  static bool getRetina() { return getInstance().retina_enabled_; }
95 
100  static void setDefaultFontPath(string path) {
101  getInstance().font_path_ = path;
102  }
103 
108  static string getDefaultFontPath() { return getInstance().font_path_; }
109 
114  static void setDefaultFontSize(float font_size) {
115  getInstance().font_size_ = font_size;
116  getInstance().reloadFonts();
117  }
118 
123  static float getDefaultFontSize() { return getInstance().font_size_; }
124 
129  static void setDefaultLineHeight(float line_height) {
130  getInstance().line_height_ = line_height;
131  getInstance().reloadFonts();
132  }
133 
138  static float getDefaultLineHeight() { return getInstance().line_height_; }
139 
149  static ofTrueTypeFont* requestFont(void* client, string font_path,
150  float font_size, float line_height,
151  float letter_spacing) {
152  pair<string, vector<float>> fontSettings(
153  font_path, {font_size, line_height, letter_spacing});
154  for (auto& f : getInstance().catalog_) {
155  if (f.second.second.count(client) > 0) {
156  if (f.first == fontSettings) return f.second.first;
157  f.second.second.erase(client);
158  if (f.second.second.empty()) {
159  delete f.second.first;
160  getInstance().catalog_.erase(f.first);
161  }
162  break;
163  }
164  }
165  auto it = getInstance().catalog_.find(fontSettings);
166  if (it == getInstance().catalog_.end()) {
167  ofTrueTypeFont* font = new ofTrueTypeFont();
168  font->load(font_path, getRetina() ? font_size * 2. : font_size);
169  font->setLineHeight(getRetina() ? line_height * 2. : line_height);
170  font->setLetterSpacing(letter_spacing);
171  getInstance().catalog_[fontSettings].first = font;
172  getInstance().catalog_.at(fontSettings).second.insert(client);
173  return font;
174  } else {
175  getInstance().catalog_.at(fontSettings).second.insert(client);
176  return it->second.first;
177  }
178  }
179 
184  static void releaseFont(void* client) {
185  for (auto& f : getInstance().catalog_) {
186  if (f.second.second.count(client) > 0) {
187  f.second.second.erase(client);
188  if (f.second.second.empty()) {
189  delete f.second.first;
190  getInstance().catalog_.erase(f.first);
191  }
192  break;
193  }
194  }
195  }
196 
197  private:
198  Globals(){};
199  Globals(const Globals&) = delete;
200  Globals(Globals&&) = delete;
201  Globals& operator=(const Globals&) = delete;
202  Globals& operator=(Globals&&) = delete;
203 
204  static Globals& getInstance() {
205  static Globals settings;
206  return settings;
207  }
208 
209  void reloadFonts() {
210  for (auto& f : catalog_) {
211  f.second.first->load(f.first.first, getRetina()
212  ? f.first.second[0] * 2.
213  : f.first.second[0]);
214  f.second.first->setLineHeight(ofxOui::Globals::getRetina()
215  ? f.first.second[1] * 2.
216  : f.first.second[1]);
217  f.second.first->setLetterSpacing(f.first.second[2]);
218  }
219  }
220 
221  bool retina_enabled_ = false;
222  string font_path_ = "Lato-Regular.ttf";
223  float font_size_ = 10.;
224  float line_height_ = 12.;
225 
226  map<pair<string, vector<float>>, pair<ofTrueTypeFont*, set<void*>>>
228 };
229 }
230 
231 #endif
static void setDefaultLineHeight(float line_height)
Set the default line height.
Definition: ofxOuiCommon.hpp:129
static ofTrueTypeFont * requestFont(void *client, string font_path, float font_size, float line_height, float letter_spacing)
Request a font with a particular setting.
Definition: ofxOuiCommon.hpp:149
void reloadFonts()
Definition: ofxOuiCommon.hpp:209
Shape
Base shape of the UI components.
Definition: ofxOuiCommon.hpp:73
static void setRetina(bool enabled)
Set retina display on/off.
Definition: ofxOuiCommon.hpp:85
Global configuration.
Definition: ofxOuiCommon.hpp:79
static void setDefaultFontPath(string path)
Set the default font path.
Definition: ofxOuiCommon.hpp:100
bool RetinaEnabled
Breakpoint function component.
Definition: ofxOuiWaveform.hpp:35
Type
Type of UI Component.
Definition: ofxOuiCommon.hpp:38
Globals()
Definition: ofxOuiCommon.hpp:198
static Globals & getInstance()
Definition: ofxOuiCommon.hpp:204
static void releaseFont(void *client)
Release a font from a particular client.
Definition: ofxOuiCommon.hpp:184
map< pair< string, vector< float > >, pair< ofTrueTypeFont *, set< void * > > > catalog_
Definition: ofxOuiCommon.hpp:227
static string getDefaultFontPath()
Get the default font path.
Definition: ofxOuiCommon.hpp:108
Definition: ofxOuiColormap.hpp:25
static bool getRetina()
Get the retina setting.
Definition: ofxOuiCommon.hpp:94
static void setDefaultFontSize(float font_size)
Set the default font size.
Definition: ofxOuiCommon.hpp:114
static float getDefaultLineHeight()
Get the default line height.
Definition: ofxOuiCommon.hpp:138
Anchor
Generic Anchor specifier for Text/Image alignment.
Definition: ofxOuiCommon.hpp:57
static float getDefaultFontSize()
Get the default font size.
Definition: ofxOuiCommon.hpp:123