Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
Public Member Functions | List of all members
gui_observer Class Reference

Allows to create a label, that automatically updates to observed variable value. This class doesn't work well with strings. More...

#include <gui_observer.h>

Inheritance diagram for gui_observer:
gui_element

Public Member Functions

 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. More...
 
void render_element ()
 
- Public Member Functions inherited from gui_element
void set_refresh_screen (bool *refresh_flag)
 

Additional Inherited Members

- Public Attributes inherited from gui_element
int index = -1
 
- Protected Attributes inherited from gui_element
int width = -1
 
int x = -1
 
int y = -1
 
bool * refresh_screen = nullptr
 

Detailed Description

Allows to create a label, that automatically updates to observed variable value. This class doesn't work well with strings.

Definition at line 23 of file gui_observer.h.

Constructor & Destructor Documentation

◆ gui_observer()

gui_observer::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.

Parameters
typeObserved type
targetObserved data
xif set to -1, isn't used
yif set to -1, isn't used
widthif set to -1, isn't used
font_sizeDefault font size is NORMAL

Definition at line 6 of file gui_observer.cpp.

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 }

Member Function Documentation

◆ render_element()

void gui_observer::render_element ( )
virtual

Override of parent function, that is required, because it does the actual rendering of the element

Reimplemented from gui_element.

Definition at line 17 of file gui_observer.cpp.

17  {
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
@ 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.

The documentation for this class was generated from the following files: