Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
gui_combobox.cpp
Go to the documentation of this file.
1 #include "gui_combobox.h"
2 
3 
4 gui_combobox::gui_combobox(const char** list, int list_size, int *selected_index, const char *label,
5  int x, int y, int width, app_workspace_ns::font_size font_size) {
6  this->list = list;
7  this->list_size = list_size;
8  this->selected_index = selected_index;
9  si = *selected_index; // if the screen is refreshed (elements are reinitialized), this should select corrent opt
10  this->label = label;
11  this->x = x;
12  this->y = y;
13  this->width = width;
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  char lab[100] = {0};
22  const char* preview_value = list[si];
23 
24  // if element is requested with different font size
25  if (current_fs != font_size)
26  app_workspace::get_instance()->use_font_size(font_size);
27 
28  if (width > -1)
29  ImGui::SetNextItemWidth((float) width);
30  //ImGui::PushItemWidth((float) width);
31 
32  sprintf(lab, "%s##%d", label ? label : "", index);
33 
34  // slightly changed version from ImGui Demo
35  if (ImGui::BeginCombo(lab, preview_value)) {
36  for (int n = 0; n < list_size; n++) {
37  const bool is_selected = (si == n);
38  if (ImGui::Selectable(list[n], is_selected)) {
39  si = n;
40  *(this->selected_index) = n;
41  selected_item = list[n];
42 
43  if (this->refresh_screen != nullptr)
44  *(this->refresh_screen) = true;
45 
46  if (this->callback != nullptr)
47  this->callback();
48  }
49 
50  // Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
51  if (is_selected)
52  ImGui::SetItemDefaultFocus();
53  }
54  ImGui::EndCombo();
55  }
56 
57  // return back original font size (otherwise all subsequent elements would use font_size, unless specified)
58  if (current_fs != font_size)
59  app_workspace::get_instance()->use_font_size(current_fs);
60 }
61 
63  return si;
64 }
65 
67  return selected_item;
68 }
69 
70 void gui_combobox::set_value_change_action(void (*callback)()) {
71  this->callback = callback;
72 }
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
const char * get_selected_item()
void render_element()
void set_value_change_action(void(*callback)())
gui_combobox(const char **list, int list_size, int *selected_index, const char *label=nullptr, 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 combobox object.
Definition: gui_combobox.cpp:4
int get_selected_index()
bool * refresh_screen
Definition: gui_element.h:14
virtual void render_element()
Definition: gui_element.cpp:9
font_size
This enum defines sizes of corresponding fonts. E.g.: SMALL_FONT is 12px.