- Button
namespace OvUI::Widgets::Buttons
{
class AButton : public AWidget
{
protected:
void _Draw_Impl() override = 0;
public:
OvTools::Eventing::Event<> ClickedEvent;
};
}
定义按钮小部件的基类
OvUI::Widgets::Buttons::Button::Button(const std::string& p_label, const OvMaths::FVector2& p_size, bool p_disabled) :
label(p_label), size(p_size), disabled(p_disabled)
{
auto& style = ImGui::GetStyle();
idleBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_Button]);
hoveredBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_ButtonHovered]);
clickedBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_ButtonActive]);
textColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_Text]);
}
void OvUI::Widgets::Buttons::Button::_Draw_Impl()
{
auto& style = ImGui::GetStyle();
auto defaultIdleColor = style.Colors[ImGuiCol_Button];
auto defaultHoveredColor = style.Colors[ImGuiCol_ButtonHovered];
auto defaultClickedColor = style.Colors[ImGuiCol_ButtonActive];
auto defaultTextColor = style.Colors[ImGuiCol_Text];
style.Colors[ImGuiCol_Button] = OvUI::Internal::Converter::ToImVec4(idleBackgroundColor);
style.Colors[ImGuiCol_ButtonHovered] = OvUI::Internal::Converter::ToImVec4(hoveredBackgroundColor);
style.Colors[ImGuiCol_ButtonActive] = OvUI::Internal::Converter::ToImVec4(clickedBackgroundColor);
style.Colors[ImGuiCol_Text] = OvUI::Internal::Converter::ToImVec4(textColor);
if (ImGui::ButtonEx((label + m_widgetID).c_str(), Internal::Converter::ToImVec2(size), disabled ? ImGuiButtonFlags_Disabled : 0))
ClickedEvent.Invoke();
style.Colors[ImGuiCol_Button] = defaultIdleColor;
style.Colors[ImGuiCol_ButtonHovered] = defaultHoveredColor;
style.Colors[ImGuiCol_ButtonActive] = defaultClickedColor;
style.Colors[ImGuiCol_Text] = defaultTextColor;
}
这是一个简单按钮小部件的实现。
OvUI::Widgets::Buttons::ButtonArrow::ButtonArrow(ImGuiDir p_direction) :
direction(p_direction)
{
}
void OvUI::Widgets::Buttons::ButtonArrow::_Draw_Impl()
{
if (ImGui::ArrowButton(m_widgetID.c_str(), direction))
ClickedEvent.Invoke();
}
带有箭头的按钮小部件的实现,需要使用ImGui库中的箭头按钮。
OvUI::Widgets::Buttons::ButtonColored::ButtonColored(const std::string & p_label, const Types::Color& p_color, const OvMaths::FVector2& p_size, bool p_enableAlpha) :
label(p_label), color(p_color), size(p_size), enableAlpha(p_enableAlpha)
{
}
void OvUI::Widgets::Buttons::ButtonColored::_Draw_Impl()
{
ImVec4 imColor = Internal::Converter::ToImVec4(color);
if (ImGui::ColorButton((label + m_widgetID).c_str(), imColor, !enableAlpha ? ImGuiColorEditFlags_NoAlpha : 0, Internal::Converter::ToImVec2(size)))
ClickedEvent.Invoke();
color = Internal::Converter::ToColor(imColor);
}
这是一个单一颜色的按钮小部件的实现,需要使用调色板元素。
OvUI::Widgets::Buttons::ButtonImage::ButtonImage(uint32_t p_textureID, const OvMaths::FVector2 & p_size) :
textureID{ p_textureID }, size(p_size)
{
}
void OvUI::Widgets::Buttons::ButtonImage::_Draw_Impl()
{
ImVec4 bg = Internal::Converter::ToImVec4(background);
ImVec4 tn = Internal::Converter::ToImVec4(tint);
if (ImGui::ImageButton(textureID.raw, Internal::Converter::ToImVec2(size), ImVec2(0.f, 1.f), ImVec2(1.f, 0.f), -1, bg, tn, disabled ? ImGuiButtonFlags_Disabled : 0))
ClickedEvent.Invoke();
}
带有图像的按钮小部件
OvUI::Widgets::Buttons::ButtonSmall::ButtonSmall(const std::string& p_label) :
label(p_label)
{
auto& style = ImGui::GetStyle();
idleBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_Button]);
hoveredBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_ButtonHovered]);
clickedBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_ButtonActive]);
textColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_Text]);
}
void OvUI::Widgets::Buttons::ButtonSmall::_Draw_Impl()
{
auto& style = ImGui::GetStyle();
auto defaultIdleColor = style.Colors[ImGuiCol_Button];
auto defaultHoveredColor = style.Colors[ImGuiCol_ButtonHovered];
auto defaultClickedColor = style.Colors[ImGuiCol_ButtonActive];
auto defaultTextColor = style.Colors[ImGuiCol_Text];
style.Colors[ImGuiCol_Button] = OvUI::Internal::Converter::ToImVec4(idleBackgroundColor);
style.Colors[ImGuiCol_ButtonHovered] = OvUI::Internal::Converter::ToImVec4(hoveredBackgroundColor);
style.Colors[ImGuiCol_ButtonActive] = OvUI::Internal::Converter::ToImVec4(clickedBackgroundColor);
style.Colors[ImGuiCol_Text] = OvUI::Internal::Converter::ToImVec4(textColor);
if (ImGui::SmallButton((label + m_widgetID).c_str()))
ClickedEvent.Invoke();
style.Colors[ImGuiCol_Button] = defaultIdleColor;
style.Colors[ImGuiCol_ButtonHovered] = defaultHoveredColor;
style.Colors[ImGuiCol_ButtonActive] = defaultClickedColor;
style.Colors[ImGuiCol_Text] = defaultTextColor;
}
小按钮小部件



