Move functionality from emboss gizmo into icon manager

This commit is contained in:
Filip Sykala - NTB T15p 2023-02-24 16:03:55 +01:00
parent 970cbbd132
commit 4d0b8679eb
4 changed files with 111 additions and 92 deletions

View File

@ -2798,7 +2798,7 @@ bool GLGizmoEmboss::draw_italic_button()
const std::optional<wxFont> &wx_font_opt = m_style_manager.get_wx_font();
const auto& ff = m_style_manager.get_font_file_with_cache();
if (!wx_font_opt.has_value() || !ff.has_value()) {
draw_icon(IconType::italic, IconState::disabled);
draw(*m_icons[(int) IconType::italic][(int)IconState::disabled]);
return false;
}
const wxFont& wx_font = *wx_font_opt;
@ -2807,8 +2807,8 @@ bool GLGizmoEmboss::draw_italic_button()
bool is_font_italic = skew.has_value() || WxFontUtils::is_italic(wx_font);
if (is_font_italic) {
// unset italic
if (draw_clickable(IconType::italic, IconState::hovered,
IconType::unitalic, IconState::hovered)) {
if (clickable(get_icon(IconType::italic, IconState::hovered),
get_icon(IconType::unitalic, IconState::hovered))) {
skew.reset();
if (wx_font.GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) {
wxFont new_wx_font = wx_font; // copy
@ -2845,7 +2845,7 @@ bool GLGizmoEmboss::draw_bold_button() {
const std::optional<wxFont> &wx_font_opt = m_style_manager.get_wx_font();
const auto& ff = m_style_manager.get_font_file_with_cache();
if (!wx_font_opt.has_value() || !ff.has_value()) {
draw_icon(IconType::bold, IconState::disabled);
draw(get_icon(IconType::bold, IconState::disabled));
return false;
}
const wxFont &wx_font = *wx_font_opt;
@ -2854,8 +2854,8 @@ bool GLGizmoEmboss::draw_bold_button() {
bool is_font_bold = boldness.has_value() || WxFontUtils::is_bold(wx_font);
if (is_font_bold) {
// unset bold
if (draw_clickable(IconType::bold, IconState::hovered,
IconType::unbold, IconState::hovered)) {
if (clickable(get_icon(IconType::bold, IconState::hovered),
get_icon(IconType::unbold, IconState::hovered))) {
boldness.reset();
if (wx_font.GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) {
wxFont new_wx_font = wx_font; // copy
@ -3800,67 +3800,14 @@ void GLGizmoEmboss::init_icons()
m_icons = m_icon_manager.init(filenames, size, type);
}
void GLGizmoEmboss::draw_icon(IconType icon, IconState state, ImVec2 size)
const IconManager::Icon &GLGizmoEmboss::get_icon(IconType type, IconState state) { return *m_icons[(unsigned) type][(unsigned) state]; }
bool GLGizmoEmboss::draw_button(IconType type, bool disable)
{
// canot draw count
assert(icon != IconType::_count);
if (icon == IconType::_count) return;
const auto &i = *m_icons[static_cast<int>(icon)][static_cast<int>(state)];
IconManager::draw(i);
}
void GLGizmoEmboss::draw_transparent_icon()
{
// use top left corner of first icon
IconManager::Icon icon = *m_icons.front().front(); // copy
if (!icon.is_valid()) {
ImGui::Text("");
return;
}
// size UV texture coors [in texture ratio]
ImVec2 size_uv(
icon.br.x-icon.tl.x,
icon.br.y-icon.tl.y);
ImVec2 one_px(
size_uv.x/icon.size.x,
size_uv.y/icon.size.y);
// reduce uv coors to one pixel
icon.tl = ImVec2(0, 0);
icon.br = one_px;
IconManager::draw(icon);
}
bool GLGizmoEmboss::draw_clickable(
IconType icon, IconState state,
IconType hover_icon, IconState hover_state)
{
// check of hover
float cursor_x = ImGui::GetCursorPosX();
draw_transparent_icon();
ImGui::SameLine(cursor_x);
if (ImGui::IsItemHovered()) {
// redraw image
draw_icon(hover_icon, hover_state);
} else {
// redraw normal image
draw_icon(icon, state);
}
return ImGui::IsItemClicked();
}
bool GLGizmoEmboss::draw_button(IconType icon, bool disable)
{
if (disable) {
draw_icon(icon, IconState::disabled);
return false;
}
return draw_clickable(
icon, IconState::activable,
icon, IconState::hovered
return Slic3r::GUI::button(
get_icon(type, IconState::activable),
get_icon(type, IconState::hovered),
get_icon(type, IconState::disabled),
disable
);
}

View File

@ -361,10 +361,8 @@ private:
// automatic calc of icon's count
_count
};
enum class IconState: unsigned { activable = 0, hovered /*1*/, disabled /*2*/};
void draw_icon(IconType icon, IconState state, ImVec2 size = ImVec2(0,0));
void draw_transparent_icon();
bool draw_clickable(IconType icon, IconState state, IconType hover_icon, IconState hover_state);
enum class IconState : unsigned { activable = 0, hovered /*1*/, disabled /*2*/ };
const IconManager::Icon& get_icon(IconType type, IconState state);
bool draw_button(IconType icon, bool disable = false);
// only temporary solution

View File

@ -8,6 +8,7 @@ namespace priv {
// set shared pointer to point on bad texture
static void clear(IconManager::Icons &icons);
static const std::vector<std::pair<int, bool>>& get_states(IconManager::RasterType type);
static void draw_transparent_icon(const IconManager::Icon &icon); // only help function
}
IconManager::~IconManager() {
@ -94,19 +95,6 @@ void IconManager::release() {
BOOST_LOG_TRIVIAL(error) << "Not implemented yet";
}
void IconManager::draw(const Icon &icon, const ImVec2 &size, const ImVec4 &tint_col, const ImVec4 &border_col)
{
// is icon loaded
if (!icon.is_valid()) {
ImGui::Text("?");
return;
}
ImTextureID id = (void *) icon.tex_id;
const ImVec2 &s = (size.x < 1 || size.y < 1) ? icon.size : size;
ImGui::Image(id, s, icon.tl, icon.br, tint_col, border_col);
}
void priv::clear(IconManager::Icons &icons) {
std::string message;
for (auto &icon : icons) {
@ -148,3 +136,69 @@ const std::vector<std::pair<int, bool>> &priv::get_states(IconManager::RasterTyp
default: return color;
}
}
void priv::draw_transparent_icon(const IconManager::Icon &icon)
{
// Check input
if (!icon.is_valid()) {
assert(false);
BOOST_LOG_TRIVIAL(warning) << "Drawing invalid Icon.";
ImGui::Text("?");
return;
}
// size UV texture coors [in texture ratio]
ImVec2 size_uv(icon.br.x - icon.tl.x, icon.br.y - icon.tl.y);
ImVec2 one_px(size_uv.x / icon.size.x, size_uv.y / icon.size.y);
// use top left corner of first icon
IconManager::Icon icon_px = icon; // copy
// reduce uv coors to one pixel
icon_px.tl = ImVec2(0, 0);
icon_px.br = one_px;
draw(icon_px);
}
namespace Slic3r::GUI {
void draw(const IconManager::Icon &icon, const ImVec2 &size, const ImVec4 &tint_col, const ImVec4 &border_col)
{
// Check input
if (!icon.is_valid()) {
assert(false);
BOOST_LOG_TRIVIAL(warning) << "Drawing invalid Icon.";
ImGui::Text("?");
return;
}
ImTextureID id = (void *) icon.tex_id;
const ImVec2 &s = (size.x < 1 || size.y < 1) ? icon.size : size;
ImGui::Image(id, s, icon.tl, icon.br, tint_col, border_col);
}
bool clickable(const IconManager::Icon &icon, const IconManager::Icon &icon_hover)
{
// check of hover
float cursor_x = ImGui::GetCursorPosX();
priv::draw_transparent_icon(icon);
ImGui::SameLine(cursor_x);
if (ImGui::IsItemHovered()) {
// redraw image
draw(icon_hover);
} else {
// redraw normal image
draw(icon);
}
return ImGui::IsItemClicked();
}
bool button(const IconManager::Icon &activ, const IconManager::Icon &hover, const IconManager::Icon &disable, bool disabled)
{
if (disabled) {
draw(disable);
return false;
}
return clickable(activ, hover);
}
} // namespace Slic3r::GUI

View File

@ -91,20 +91,40 @@ public:
/// </summary>
void release();
/// <summary>
/// Draw imgui image with icon
/// </summary>
/// <param name="icon">Place in texture</param>
/// <param name="size">[optional]Size of image, wen zero than use same size as stored texture</param>
/// <param name="tint_col">viz ImGui::Image </param>
/// <param name="border_col">viz ImGui::Image </param>
static void draw(const Icon &icon, const ImVec2 &size = ImVec2(0, 0), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0));
private:
// keep data stored on GPU
GLTexture m_icons_texture;
Icons m_icons;
};
/// <summary>
/// Draw imgui image with icon
/// </summary>
/// <param name="icon">Place in texture</param>
/// <param name="size">[optional]Size of image, wen zero than use same size as stored texture</param>
/// <param name="tint_col">viz ImGui::Image </param>
/// <param name="border_col">viz ImGui::Image </param>
void draw(const IconManager::Icon &icon,
const ImVec2 &size = ImVec2(0, 0),
const ImVec4 &tint_col = ImVec4(1, 1, 1, 1),
const ImVec4 &border_col = ImVec4(0, 0, 0, 0));
/// <summary>
/// Draw icon which change on hover
/// </summary>
/// <param name="icon">Draw when no hover</param>
/// <param name="icon_hover">Draw when hover</param>
/// <returns>True when click, otherwise False</returns>
bool clickable(const IconManager::Icon &icon, const IconManager::Icon &icon_hover);
/// <summary>
/// Use icon as button with 3 states activ hover and disabled
/// </summary>
/// <param name="activ">Not disabled not hovered image</param>
/// <param name="hover">Hovered image</param>
/// <param name="disable">Disabled image</param>
/// <returns>True when click on enabled, otherwise False</returns>
bool button(const IconManager::Icon &activ, const IconManager::Icon &hover, const IconManager::Icon &disable, bool disabled = false);
} // namespace Slic3r::GUI
#endif // slic3r_IconManager_hpp_