mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-16 20:15:55 +08:00
Move functionality from emboss gizmo into icon manager
This commit is contained in:
parent
970cbbd132
commit
4d0b8679eb
@ -2798,7 +2798,7 @@ bool GLGizmoEmboss::draw_italic_button()
|
|||||||
const std::optional<wxFont> &wx_font_opt = m_style_manager.get_wx_font();
|
const std::optional<wxFont> &wx_font_opt = m_style_manager.get_wx_font();
|
||||||
const auto& ff = m_style_manager.get_font_file_with_cache();
|
const auto& ff = m_style_manager.get_font_file_with_cache();
|
||||||
if (!wx_font_opt.has_value() || !ff.has_value()) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
const wxFont& wx_font = *wx_font_opt;
|
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);
|
bool is_font_italic = skew.has_value() || WxFontUtils::is_italic(wx_font);
|
||||||
if (is_font_italic) {
|
if (is_font_italic) {
|
||||||
// unset italic
|
// unset italic
|
||||||
if (draw_clickable(IconType::italic, IconState::hovered,
|
if (clickable(get_icon(IconType::italic, IconState::hovered),
|
||||||
IconType::unitalic, IconState::hovered)) {
|
get_icon(IconType::unitalic, IconState::hovered))) {
|
||||||
skew.reset();
|
skew.reset();
|
||||||
if (wx_font.GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) {
|
if (wx_font.GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) {
|
||||||
wxFont new_wx_font = wx_font; // copy
|
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 std::optional<wxFont> &wx_font_opt = m_style_manager.get_wx_font();
|
||||||
const auto& ff = m_style_manager.get_font_file_with_cache();
|
const auto& ff = m_style_manager.get_font_file_with_cache();
|
||||||
if (!wx_font_opt.has_value() || !ff.has_value()) {
|
if (!wx_font_opt.has_value() || !ff.has_value()) {
|
||||||
draw_icon(IconType::bold, IconState::disabled);
|
draw(get_icon(IconType::bold, IconState::disabled));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const wxFont &wx_font = *wx_font_opt;
|
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);
|
bool is_font_bold = boldness.has_value() || WxFontUtils::is_bold(wx_font);
|
||||||
if (is_font_bold) {
|
if (is_font_bold) {
|
||||||
// unset bold
|
// unset bold
|
||||||
if (draw_clickable(IconType::bold, IconState::hovered,
|
if (clickable(get_icon(IconType::bold, IconState::hovered),
|
||||||
IconType::unbold, IconState::hovered)) {
|
get_icon(IconType::unbold, IconState::hovered))) {
|
||||||
boldness.reset();
|
boldness.reset();
|
||||||
if (wx_font.GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) {
|
if (wx_font.GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) {
|
||||||
wxFont new_wx_font = wx_font; // copy
|
wxFont new_wx_font = wx_font; // copy
|
||||||
@ -3800,67 +3800,14 @@ void GLGizmoEmboss::init_icons()
|
|||||||
m_icons = m_icon_manager.init(filenames, size, type);
|
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
|
return Slic3r::GUI::button(
|
||||||
assert(icon != IconType::_count);
|
get_icon(type, IconState::activable),
|
||||||
if (icon == IconType::_count) return;
|
get_icon(type, IconState::hovered),
|
||||||
|
get_icon(type, IconState::disabled),
|
||||||
const auto &i = *m_icons[static_cast<int>(icon)][static_cast<int>(state)];
|
disable
|
||||||
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
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,9 +362,7 @@ private:
|
|||||||
_count
|
_count
|
||||||
};
|
};
|
||||||
enum class IconState : unsigned { activable = 0, hovered /*1*/, disabled /*2*/ };
|
enum class IconState : unsigned { activable = 0, hovered /*1*/, disabled /*2*/ };
|
||||||
void draw_icon(IconType icon, IconState state, ImVec2 size = ImVec2(0,0));
|
const IconManager::Icon& get_icon(IconType type, IconState state);
|
||||||
void draw_transparent_icon();
|
|
||||||
bool draw_clickable(IconType icon, IconState state, IconType hover_icon, IconState hover_state);
|
|
||||||
bool draw_button(IconType icon, bool disable = false);
|
bool draw_button(IconType icon, bool disable = false);
|
||||||
|
|
||||||
// only temporary solution
|
// only temporary solution
|
||||||
|
@ -8,6 +8,7 @@ namespace priv {
|
|||||||
// set shared pointer to point on bad texture
|
// set shared pointer to point on bad texture
|
||||||
static void clear(IconManager::Icons &icons);
|
static void clear(IconManager::Icons &icons);
|
||||||
static const std::vector<std::pair<int, bool>>& get_states(IconManager::RasterType type);
|
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() {
|
IconManager::~IconManager() {
|
||||||
@ -94,19 +95,6 @@ void IconManager::release() {
|
|||||||
BOOST_LOG_TRIVIAL(error) << "Not implemented yet";
|
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) {
|
void priv::clear(IconManager::Icons &icons) {
|
||||||
std::string message;
|
std::string message;
|
||||||
for (auto &icon : icons) {
|
for (auto &icon : icons) {
|
||||||
@ -148,3 +136,69 @@ const std::vector<std::pair<int, bool>> &priv::get_states(IconManager::RasterTyp
|
|||||||
default: return color;
|
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
|
||||||
|
@ -91,6 +91,12 @@ public:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void release();
|
void release();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// keep data stored on GPU
|
||||||
|
GLTexture m_icons_texture;
|
||||||
|
Icons m_icons;
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draw imgui image with icon
|
/// Draw imgui image with icon
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -98,13 +104,27 @@ public:
|
|||||||
/// <param name="size">[optional]Size of image, wen zero than use same size as stored 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="tint_col">viz ImGui::Image </param>
|
||||||
/// <param name="border_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));
|
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));
|
||||||
|
|
||||||
private:
|
/// <summary>
|
||||||
// keep data stored on GPU
|
/// Draw icon which change on hover
|
||||||
GLTexture m_icons_texture;
|
/// </summary>
|
||||||
Icons m_icons;
|
/// <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
|
} // namespace Slic3r::GUI
|
||||||
#endif // slic3r_IconManager_hpp_
|
#endif // slic3r_IconManager_hpp_
|
Loading…
x
Reference in New Issue
Block a user