From d9c2053c2650aaa40d76d233d9a2f4f487966bc6 Mon Sep 17 00:00:00 2001 From: Albin Johansson Date: Mon, 6 May 2024 20:12:21 +0200 Subject: [PATCH] Add button utilities --- .../inc/tactile/core/ui/common/buttons.hpp | 49 +++++++++++++++++++ .../src/tactile/core/ui/common/buttons.cpp | 44 +++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 source/core/inc/tactile/core/ui/common/buttons.hpp create mode 100644 source/core/src/tactile/core/ui/common/buttons.cpp diff --git a/source/core/inc/tactile/core/ui/common/buttons.hpp b/source/core/inc/tactile/core/ui/common/buttons.hpp new file mode 100644 index 0000000000..c67a6da84f --- /dev/null +++ b/source/core/inc/tactile/core/ui/common/buttons.hpp @@ -0,0 +1,49 @@ +// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0) + +#pragma once + +#include "tactile/base/prelude.hpp" +#include "tactile/core/ui/common/icons.hpp" + +namespace tactile::ui { + +/// \addtogroup UI +/// \{ + +/** + * Adds a button to the widget stack. + * + * \param label The text label to use. + * \param tooltip An optional tooltip. + * \param enabled Controls whether the button is pressable. + * \param width The width of the button. Pass \c 0 to use automatic width. + * \param height The height of the button. Pass \c 0 to use automatic height. + * + * \return + * True if the button was pressed; false otherwise. + */ +[[nodiscard]] +auto push_button(const char* label, + const char* tooltip = nullptr, + bool enabled = true, + float width = 0.0f, + float height = 0.0f) -> bool; + +/** + * Adds an icon button to the widget stack. + * + * \param icon The icon to use. + * \param tooltip An optional tooltip. + * \param enabled Controls whether the button is pressable. + * + * \return + * True if the button was pressed; false otherwise. + */ +[[nodiscard]] +auto push_icon_button(Icon icon, + const char* tooltip = nullptr, + bool enabled = true) -> bool; + +/// \} + +} // namespace tactile::ui diff --git a/source/core/src/tactile/core/ui/common/buttons.cpp b/source/core/src/tactile/core/ui/common/buttons.cpp new file mode 100644 index 0000000000..eda12f5611 --- /dev/null +++ b/source/core/src/tactile/core/ui/common/buttons.cpp @@ -0,0 +1,44 @@ +// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0) + +#include "tactile/core/ui/common/buttons.hpp" + +#include + +#include "tactile/core/ui/common/widgets.hpp" + +namespace tactile::ui { + +auto push_button(const char* label, + const char* tooltip, + const bool enabled, + const float width, + const float height) -> bool +{ + const DisabledScope disable {!enabled}; + + const auto was_pressed = ImGui::Button(label, ImVec2 {width, height}); + + if (ImGui::IsItemHovered()) { + ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); + + if (tooltip != nullptr) { + ImGui::SetTooltip("%s", tooltip); + } + } + + return was_pressed; +} + +auto push_icon_button(const Icon icon, + const char* tooltip, + const bool enabled) -> bool +{ + const auto* icon_string = to_string(icon); + + const auto& style = ImGui::GetStyle(); + const auto side_length = ImGui::GetFontSize() + style.ItemSpacing.y * 2.0f; + + return push_button(icon_string, tooltip, enabled, side_length, side_length); +} + +} // namespace tactile::ui