Skip to content

Commit

Permalink
Add button utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed May 6, 2024
1 parent 79c8c74 commit d9c2053
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
49 changes: 49 additions & 0 deletions source/core/inc/tactile/core/ui/common/buttons.hpp
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions source/core/src/tactile/core/ui/common/buttons.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#include "tactile/core/ui/common/buttons.hpp"

#include <imgui.h>

#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

0 comments on commit d9c2053

Please sign in to comment.