Empirical
Style.h
Go to the documentation of this file.
1 
11 #ifndef EMP_WEB_STYLE_H
12 #define EMP_WEB_STYLE_H
13 
14 
15 #ifdef EMSCRIPTEN
16 #include <emscripten.h>
17 #endif
18 
19 #include "../tools/string_utils.h"
20 
21 #include <map>
22 #include <string>
23 
24 namespace emp {
25 namespace web {
26 
29  class Style {
30  private:
31  std::map<std::string, std::string> settings;
32 
33  public:
34  Style() { ; }
35  Style(const Style &) = default;
36  Style(Style &&) = default;
37  Style & operator=(const Style &) = default;
38  Style & operator=(Style &&) = default;
39 
41  size_t GetSize() const { return settings.size(); }
42 
43  Style & DoSet(const std::string & in_set, const std::string & in_val) {
44  settings[in_set] = in_val;
45  return *this;
46  }
47 
49  template <typename SET_TYPE>
50  Style & Set(const std::string & s, SET_TYPE v) {
51  return DoSet(s, emp::to_string(v));
52  }
53 
55  Style & Insert(const Style & in_css) {
56  settings.insert(in_css.settings.begin(), in_css.settings.end());
57  return *this;
58  }
59 
61  bool Has(const std::string & setting) const {
62  return settings.find(setting) != settings.end();
63  }
64 
67  const std::string & Get(const std::string & setting) {
68  emp_assert(Has(setting));
69  return settings[setting];
70  }
71 
72  const std::map<std::string, std::string> & GetMap() const {
73  return settings;
74  }
75 
77  void Clear() { settings.clear(); }
78 
80  void Remove(const std::string & setting) {
81  settings.erase(setting);
82  }
83 
85  void Apply(const std::string & widget_id) {
86  // Stop immediately if nothing to set.
87  if (settings.size() == 0) return;
88 
89  // Find the current object only once.
90 #ifdef EMSCRIPTEN
91  EM_ASM_ARGS({
92  var id = Pointer_stringify($0);
93  emp_i.cur_obj = $( '#' + id );
94  }, widget_id.c_str());
95 #endif
96 
97  for (auto css_pair : settings) {
98  if (css_pair.second == "") continue; // Ignore empty entries.
99 #ifdef EMSCRIPTEN
100  EM_ASM_ARGS({
101  var name = Pointer_stringify($0);
102  var value = Pointer_stringify($1);
103  emp_i.cur_obj.css( name, value);
104  }, css_pair.first.c_str(), css_pair.second.c_str());
105 #else
106  std::cout << "Setting '" << widget_id << "' attribute '" << css_pair.first
107  << "' to '" << css_pair.second << "'.";
108 #endif
109  }
110  }
111 
113  void Apply(const std::string & widget_id, const std::string & setting) {
114  emp_assert(Has(setting));
115 
116 #ifdef EMSCRIPTEN
117  EM_ASM_ARGS({
118  var id = Pointer_stringify($0);
119  var setting = Pointer_stringify($1);
120  var value = Pointer_stringify($2);
121  $( '#' + id ).css( setting, value);
122  }, widget_id.c_str(), setting.c_str(), settings[setting].c_str());
123 #else
124  std::cout << "Setting '" << widget_id << "' attribute '" << setting
125  << "' to '" << settings[setting] << "'.";
126 #endif
127  }
128 
130  static void Apply(const std::string & widget_id, const std::string & setting,
131  const std::string & value) {
132 #ifdef EMSCRIPTEN
133  EM_ASM_ARGS({
134  var id = Pointer_stringify($0);
135  var setting = Pointer_stringify($1);
136  var value = Pointer_stringify($2);
137  $( '#' + id ).css( setting, value);
138  }, widget_id.c_str(), setting.c_str(), value.c_str());
139 #else
140  std::cout << "Setting '" << widget_id << "' attribute '" << setting
141  << "' to '" << value << "'.";
142 #endif
143  }
144 
146  operator bool() const { return (bool) settings.size(); }
147  };
148 
149 
150 }
151 }
152 
153 
154 #endif
void Clear()
Remove all setting values.
Definition: Style.h:77
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
Definition: Style.h:29
Style & Set(const std::string &s, SET_TYPE v)
Record that setting "s" is set to value "v" (converted to string) and return this object...
Definition: Style.h:50
const std::string & Get(const std::string &setting)
Definition: Style.h:67
void Apply(const std::string &widget_id)
Apply ALL of the style settings to a specified widget.
Definition: Style.h:85
const std::map< std::string, std::string > & GetMap() const
Definition: Style.h:72
Style & operator=(const Style &)=default
Style & Insert(const Style &in_css)
Set all values from in_css here as well. Return this object.
Definition: Style.h:55
size_t GetSize() const
Return a count of the number of settings that have been set.
Definition: Style.h:41
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void Apply(const std::string &widget_id, const std::string &setting)
Apply only a SPECIFIC style setting from the setting library.
Definition: Style.h:113
#define emp_assert(...)
Definition: assert.h:199
Style & DoSet(const std::string &in_set, const std::string &in_val)
Definition: Style.h:43
static void Apply(const std::string &widget_id, const std::string &setting, const std::string &value)
Apply onlay a SPECIFIC style setting with a specifid value!
Definition: Style.h:130
Style()
Definition: Style.h:34
bool Has(const std::string &setting) const
Return true/false based on whether "setting" has been given a value in this Style.
Definition: Style.h:61
void Remove(const std::string &setting)
Remove a specific setting value.
Definition: Style.h:80