INSTINCT Code Coverage Report


Directory: src/
File: internal/gui/widgets/EnumCombo.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 0 29 0.0%
Functions: 0 16 0.0%
Branches: 0 31 0.0%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 /// @file EnumCombo.hpp
10 /// @brief Combo representing an enumeration
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2023-01-31
13
14 #pragma once
15
16 #include <imgui.h>
17 #include <fmt/core.h>
18
19 namespace NAV::gui::widgets
20 {
21
22 /// @brief Combo representing an enumeration
23 /// @tparam T Enumeration Type
24 /// @param[in] label Label to show beside the combo box. This has to be a unique id for ImGui.
25 /// @param[in] enumeration Reference to the enumeration variable to select
26 /// @param[in] startIdx Start Index in the enum (so skip first items)
27 /// @return True if the value changed
28 /// @attention The Enum type needs a last element called 'COUNT'
29 template<typename T>
30 bool EnumCombo(const char* label, T& enumeration, size_t startIdx = 0)
31 {
32 bool clicked = false;
33 if (ImGui::BeginCombo(label, NAV::to_string(enumeration)))
34 {
35 for (size_t i = startIdx; i < static_cast<size_t>(T::COUNT); i++)
36 {
37 const bool is_selected = (static_cast<size_t>(enumeration) == i);
38 if (ImGui::Selectable(NAV::to_string(static_cast<T>(i)), is_selected))
39 {
40 enumeration = static_cast<T>(i);
41 clicked = true;
42 }
43
44 // Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
45 if (is_selected)
46 {
47 ImGui::SetItemDefaultFocus();
48 }
49 }
50
51 ImGui::EndCombo();
52 }
53 return clicked;
54 }
55
56 /// @brief Combo representing two enumerations. Values will be displayed appended and set to the same value.
57 /// @tparam T Enumeration Type
58 /// @param[in] label Label to show beside the combo box. This has to be a unique id for ImGui.
59 /// @param[in] enumeration1 Reference to the first enumeration variable to select
60 /// @param[in] enumeration2 Reference to the second enumeration variable to set to the same value as the first one
61 /// @param[in] previewAppendix Additional text to show in the combo preview
62 /// @return True if the value changed
63 /// @attention The Enum type needs a last element called 'COUNT'
64 template<typename T>
65 bool EnumCombo(const char* label, T& enumeration1, T& enumeration2, const char* previewAppendix = "")
66 {
67 bool clicked = false;
68 std::string previewText = enumeration1 == enumeration2
69 ? fmt::format("{}{}", NAV::to_string(enumeration1), previewAppendix)
70 : fmt::format("{} | {}{}", NAV::to_string(enumeration1), NAV::to_string(enumeration2), previewAppendix);
71 if (ImGui::BeginCombo(label, previewText.c_str()))
72 {
73 for (size_t i = 0; i < static_cast<size_t>(T::COUNT); i++)
74 {
75 const bool is_selected = (static_cast<size_t>(enumeration1) == i);
76 if (ImGui::Selectable(NAV::to_string(static_cast<T>(i)), is_selected))
77 {
78 enumeration1 = static_cast<T>(i);
79 enumeration2 = static_cast<T>(i);
80 clicked = true;
81 }
82
83 // Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
84 if (is_selected)
85 {
86 ImGui::SetItemDefaultFocus();
87 }
88 }
89
90 ImGui::EndCombo();
91 }
92 return clicked;
93 }
94
95 } // namespace NAV::gui::widgets
96