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_combobox Class Reference

Wrapper for ImGui combo box structure. More...

#include <gui_combobox.h>

Inheritance diagram for gui_combobox:
gui_element

Public Member Functions

 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. More...
 
void render_element ()
 
int get_selected_index ()
 
const char * get_selected_item ()
 
void set_value_change_action (void(*callback)())
 
- 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

Wrapper for ImGui combo box structure.

Definition at line 11 of file gui_combobox.h.

Constructor & Destructor Documentation

◆ gui_combobox()

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

Parameters
listis an array of labels, that the combobox selection consists of
list_sizelabel array size
selected_indexthis variable contains selected index
labelcombox can have a label (name), nullptr when not used
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 4 of file gui_combobox.cpp.

5  {
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 }

Member Function Documentation

◆ get_selected_index()

int gui_combobox::get_selected_index ( )

Returns selected index, that is used internally. This is for legacy reasons, only selected_index could be used

Definition at line 62 of file gui_combobox.cpp.

62  {
63  return si;
64 }

◆ get_selected_item()

const char * gui_combobox::get_selected_item ( )

Returns selected label

Definition at line 66 of file gui_combobox.cpp.

66  {
67  return selected_item;
68 }

◆ render_element()

void gui_combobox::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_combobox.cpp.

17  {
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 }
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
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.

◆ set_value_change_action()

void gui_combobox::set_value_change_action ( void(*)()  callback)

Sets value change action

Definition at line 70 of file gui_combobox.cpp.

70  {
71  this->callback = callback;
72 }

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