Empirical
emfunctions.h
Go to the documentation of this file.
1 
10 #ifndef EMP_EM_FUNCTIONS_H
11 #define EMP_EM_FUNCTIONS_H
12 
13 #include <functional>
14 
15 #include "../tools/alert.h"
16 #include "../tools/string_utils.h"
17 #include "JSWrap.h"
18 
19 namespace emp {
20 
21 #define AlertVar(VAR) emp::Alert(std::string(#VAR) + std::string("=") + std::to_string(VAR))
22 
24  static void DelayCall(const std::function<void()> & in_fun, int delay) {
25  uint32_t callback_id = JSWrapOnce(in_fun); // Wrap and dispose when called.
26  (void)callback_id;
27  EM_ASM_ARGS({
28  window.setTimeout(function() { emp.Callback($0); }, $1);
29  }, callback_id, delay);
30  }
31 
33  static void OnResize(const std::function<void()> & in_fun) {
34  uint32_t callback_id = JSWrap(in_fun);
35  (void)callback_id;
36  EM_ASM_ARGS({
37  window.addEventListener("resize", function() { emp.Callback($0); });
38  }, callback_id);
39  }
40 
42  static void OnResize(const std::function<void(int,int)> & in_fun) {
43  uint32_t callback_id = JSWrap(in_fun);
44  (void)callback_id;
45  EM_ASM_ARGS({
46  window.addEventListener("resize", function() {
47  emp.Callback($0, window.innerWidth, window.innerHeight);
48  });
49  }, callback_id);
50  }
51 
53  inline double GetTime() { return EM_ASM_DOUBLE_V({ return (new Date()).getTime(); }); }
54 
56  inline int GetWindowInnerWidth() { return EM_ASM_INT_V({ return window.innerWidth; }); }
57 
59  inline int GetWindowInnerHeight() { return EM_ASM_INT_V({ return window.innerHeight; }); }
60 
62  static void SetBackgroundColor(const std::string color) {
63  EM_ASM_ARGS({
64  var color = Pointer_stringify($0);
65  $("body").first().css("background-color", color);
66  }, color.c_str());
67  }
68 
69  static void SetColor(const std::string color) {
70  EM_ASM_ARGS({
71  var color = Pointer_stringify($0);
72  $("body").first().css("color", color);
73  }, color.c_str());
74  }
75 
76  // These may already be in HTML5 for Emscripten
77  static void SetCursor(const char * type) {
78  EM_ASM_ARGS({
79  var type = Pointer_stringify($0);
80  document.body.style.cursor = type;
81  }, type);
82  }
83 
84  static void OpenWindow(const std::string & url) {
85  EM_ASM_ARGS({
86  var url = Pointer_stringify($0);
87  window.open = url;
88  }, url.c_str());
89  }
90 
91  // Convert a sequence with possible html codes to appear identically in html.
92  static std::string text2html(const std::string & text) {
93  std::stringstream html;
94  for (char x : text) {
95  switch (x) {
96  case '<': html << "&lt;"; break;
97  case '>': html << "&gt;"; break;
98  case '&': html << "&amp;"; break;
99  case ' ': html << "&nbsp;"; break;
100  case '\n': html << "<br>"; break;
101  default: html << x;
102  };
103  }
104  return html.str();
105  }
106 
107 }
108 
109 #endif
double GetTime()
Get the current time, as provided by the web browser.
Definition: emfunctions.h:53
static void DelayCall(const std::function< void()> &in_fun, int delay)
Call a function after a specified amount of time.
Definition: emfunctions.h:24
static std::string text2html(const std::string &text)
Definition: emfunctions.h:92
int GetWindowInnerHeight()
Determine with height of the current window.
Definition: emfunctions.h:59
static void OnResize(const std::function< void()> &in_fun)
Provide a function to call whenever a window&#39;s size changes (no arguments).
Definition: emfunctions.h:33
static void SetBackgroundColor(const std::string color)
Set the background color of this web page.
Definition: emfunctions.h:62
Wrap a C++ function and convert it to an integer that can be called from Javascript.
If we are in emscripten, make sure to include the header.
Definition: array.h:37
static void SetColor(const std::string color)
Definition: emfunctions.h:69
static void OpenWindow(const std::string &url)
Definition: emfunctions.h:84
static void SetCursor(const char *type)
Definition: emfunctions.h:77
int GetWindowInnerWidth()
Determine with width of the current window.
Definition: emfunctions.h:56