Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
gui_label.cpp
Go to the documentation of this file.
1 #include "gui_label.h"
2 
3 #include "imgui.h"
4 
5 /*
6  The GUI library allows for formatted texts to be used. For example: ImGui::Text("Int value: %d", int_variable);
7  %d will always get filled with current int_variable values. However this is impossible to replicate with this class
8  due to inability to save va_list (argumet list for formatted text), also the values would not get updated.
9  It could be theorethically possible to use this class as observer for single value (through pointer), but
10  that would complicate this class for no reason.
11 
12  For formatted text in GUI use gui_direct (calls user defined function, that calls GUI directly)
13 */
14 
15 gui_label::gui_label(const char *label, int x, int y, int width, app_workspace_ns::font_size font_size) {
16  this->label = label;
17  this->x = x;
18  this->y = y;
19  this->width = width;
20  this->font_size = font_size;
21 }
22 
24 }
25 
28 
29  app_workspace_ns::font_size current_fs = app_workspace::get_instance()->get_font_size();
30 
31  // if element is requested with different font size
32  if (current_fs != font_size)
33  app_workspace::get_instance()->use_font_size(font_size);
34 
35  if (width > -1) {
36  // 480 = window width, 10 = padding
37  float wrap_width = (width + (x > -1 ? x : 0)) > 480 ? (480 - x - 10) : width;
38  ImVec2 cpos = ImGui::GetCursorPos();
39  ImGui::PushTextWrapPos(cpos.x + wrap_width);
40  }
41 
42  ImGui::TextWrapped(this->label);
43  //ImGui::Text(this->label);
44 
45  if (width > -1) {
46  ImGui::PopTextWrapPos();
47  }
48 
49  // return back original font size (otherwise all subsequent elements would use font_size, unless specified)
50  if (current_fs != font_size)
51  app_workspace::get_instance()->use_font_size(current_fs);
52 }
53 
54 void gui_label::set_label(const char* label) {
55  this->label = label;
56 }
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()
Definition: gui_label.cpp:26
gui_label(const char *label, 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 label object.
Definition: gui_label.cpp:15
void set_label(const char *label)
Definition: gui_label.cpp:54
font_size
This enum defines sizes of corresponding fonts. E.g.: SMALL_FONT is 12px.