Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
gui_input.cpp
Go to the documentation of this file.
1 #include "gui_input.h"
2 
3 #include <spdlog/spdlog.h>
4 
5 
6 gui_input::gui_input(input_type type, void *data, int data_size, int x, int y, int width,
7  const char *label, app_workspace_ns::font_size font_size)
8 {
9  this->type = type;
10  this->data = data;
11  this->data_size = data_size;
12  this->x = x;
13  this->y = y;
14  this->width = width;
15  this->label = label;
16  this->font_size = font_size;
17 }
18 
21 
22  app_workspace_ns::font_size current_fs = app_workspace::get_instance()->get_font_size();
23  char lab[100] = {0};
24  bool modified = false;
25 
26  // if element is requested with different font size
27  if (current_fs != font_size)
28  app_workspace::get_instance()->use_font_size(font_size);
29 
30  if (width > -1)
31  ImGui::SetNextItemWidth((float) width);
32  //ImGui::PushItemWidth((float) width);
33 
34  sprintf(lab, "%s##%d", label ? label : "", index);
35  /*
36  if (this->label != nullptr)
37  sprintf(lab, "%s##%d", this->label, this->index);
38  else
39  sprintf(lab, "##%d", this->index);
40  */
41 
42  switch (this->type) {
43  case IN_INT:
44  modified = ImGui::InputInt(lab, (int*) data, 0, 0);
45  break;
46  case IN_FLOAT:
47  modified = ImGui::InputFloat(lab, (float*) data);
48  break;
49  case IN_PASSWORD_PLAIN:
50  case IN_TEXT:
51  //modified = ImGui::InputText(lab, (char*) data, data_size);
52  ImGui::InputText(lab, (char*) data, data_size);
53  break;
54  case IN_PASSWORD:
55  //modified = ImGui::InputText(lab, (char*) data, data_size, ImGuiInputTextFlags_Password);
56  ImGui::InputText(lab, (char*) data, data_size, ImGuiInputTextFlags_Password);
57  break;
58  case IN_DOUBLE:
59  modified = ImGui::InputDouble(lab, (double*) data);
60  break;
61 
62  default:
63  spdlog::error("gui_input.cpp - Unrecognized input type");
64  break;
65  }
66 
67  // return back original font size (otherwise all subsequent elements would use font_size, unless specified)
68  if (current_fs != font_size)
69  app_workspace::get_instance()->use_font_size(current_fs);
70 
71  if (modified) {
72  check_input_limit();
73 
74  if (this->callback != nullptr)
75  this->callback();
76  }
77 }
78 
79 
80 void gui_input::check_input_limit() {
81  // TODO/ FIXME - return to last valid value instead of limit?
82  switch (this->type) {
83  case IN_INT:
84  if (*((int*) data) < i_min) *((int*) data) = i_min;
85  if (*((int*) data) > i_max) *((int*) data) = i_max;
86  break;
87  case IN_FLOAT:
88  if (*((float*) data) < f_min) *((float*) data) = f_min;
89  if (*((float*) data) > f_max) *((float*) data) = f_max;
90  break;
91  case IN_DOUBLE:
92  if (*((double*) data) < d_min) *((double*) data) = d_min;
93  if (*((double*) data) > d_max) *((double*) data) = d_max;
94  break;
95 
96  default:
97  spdlog::error("gui_input.cpp - Unrecognized input type of modified value");
98  break;
99  }
100 }
101 
103  return type;
104 }
105 
106 
108  // change: password moved to be IN_INT instead of IN_TEXT (due to difficulty inputting with key cycling)
109  switch (type) {
117  }
118 }
119 
120 void gui_input::set_min_max(int min, int max) {
121  this->has_min_max = true;
122  this->i_min = min;
123  this->i_max = max;
124 }
125 
126 void gui_input::set_min_max(float min, float max) {
127  this->has_min_max = true;
128  this->f_min = min;
129  this->f_max = max;
130 }
131 
132 void gui_input::set_min_max(double min, double max) {
133  this->has_min_max = true;
134  this->d_min = min;
135  this->d_max = max;
136 }
137 
138 void gui_input::set_value_change_action(void (*callback)()) {
139  this->callback = callback;
140 }
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
app_workspace_ns::kb_input_type get_in_type()
Definition: gui_input.cpp:107
input_type get_type()
Definition: gui_input.cpp:102
void set_value_change_action(void(*callback)())
Definition: gui_input.cpp:138
void render_element()
Definition: gui_input.cpp:19
gui_input(input_type type, void *data, int data_size, int x=-1, int y=-1, int width=-1, const char *label=nullptr, app_workspace_ns::font_size font_size=app_workspace_ns::font_size::NORMAL_FONT)
Construct a new gui input object.
Definition: gui_input.cpp:6
void set_min_max(int min=INT_MIN, int max=INT_MAX)
Definition: gui_input.cpp:120
input_type
Allowed input types.
Definition: gui_input.h:11
@ IN_DOUBLE
Definition: gui_input.h:17
@ IN_INT
Definition: gui_input.h:12
@ IN_PASSWORD_PLAIN
Definition: gui_input.h:16
@ IN_TEXT
Definition: gui_input.h:14
@ IN_PASSWORD
Definition: gui_input.h:15
@ IN_FLOAT
Definition: gui_input.h:13
font_size
This enum defines sizes of corresponding fonts. E.g.: SMALL_FONT is 12px.
kb_input_type
Enum holding options of keyboard INPUT mode types.
Definition: app_workspace.h:85