Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
gui_observer.cpp
Go to the documentation of this file.
1 #include "gui_observer.h"
2 
3 #include <spdlog/spdlog.h>
4 
5 // IMPORTANT - for string only char* is supplied into target, not char**
6 gui_observer::gui_observer(observed_type type, void* target, int x, int y, int width,
8 {
9  this->type = type;
10  this->target = target;
11  this->x = x;
12  this->y = y;
13  this->width = width; // width might not make sense with this
14  this->font_size = font_size;
15 }
16 
19 
20  app_workspace_ns::font_size current_fs = app_workspace::get_instance()->get_font_size();
21 
22  // if element is requested with different font size
23  if (current_fs != font_size)
24  app_workspace::get_instance()->use_font_size(font_size);
25 
26  if (width > -1) {
27  // 480 = window width, 10 = padding - TODO constants for those values
28  float wrap_width = (width + (x > -1 ? x : 0)) > 480 ? (480 - x - 10) : width;
29  ImVec2 cpos = ImGui::GetCursorPos();
30  ImGui::PushTextWrapPos(cpos.x + wrap_width);
31  }
32 
33  switch (type) {
34  case OBS_INT:
35  ImGui::TextWrapped("%d", *((int*) target));
36  break;
37  case OBS_LONG:
38  ImGui::TextWrapped("%ld", *((long*) target));
39  break;
40  case OBS_FLOAT:
41  ImGui::TextWrapped("%.3f", *((float*) target));
42  break;
43  case OBS_STRING:
44 
45  ImGui::TextWrapped("%s", (const char*) target);
46  /*
47  char* tmp = *((char**) target);
48  // true if const char* is supplied, false if char[] is supplied
49  if (tmp)
50  ImGui::TextWrapped("%s", tmp);
51  else
52  ImGui::TextWrapped("%s", (char*) target);
53  */
54  break;
55  default:
56  spdlog::error("gui_observer.cpp - Unknown observed type. This can only occur on memory corruption (overflow)");
57  }
58 
59  if (width > -1)
60  ImGui::PopTextWrapPos();
61 
62  // return back original font size (otherwise all subsequent elements would use font_size, unless specified)
63  if (current_fs != font_size)
64  app_workspace::get_instance()->use_font_size(current_fs);
65 }
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
virtual void render_element()
Definition: gui_element.cpp:9
void render_element()
gui_observer(observed_type type, void *target, int x=-1, int y=-1, int width=-1, app_workspace_ns::font_size font_size=app_workspace_ns::font_size::NORMAL_FONT)
Construct a new gui observer object.
Definition: gui_observer.cpp:6
observed_type
Types, that can be observed.
Definition: gui_observer.h:11
@ OBS_INT
Definition: gui_observer.h:12
@ OBS_FLOAT
Definition: gui_observer.h:14
@ OBS_LONG
Definition: gui_observer.h:13
@ OBS_STRING
Definition: gui_observer.h:15
font_size
This enum defines sizes of corresponding fonts. E.g.: SMALL_FONT is 12px.