Files
OrcaSlicer/src/slic3r/GUI/GLToolbar.cpp
T
HanifKoh eb790b0a09 Let Users Hide, Reorder and Group the Prepare Toolbar and Gizmo Bar Icons (#17632)
* Add a Toolbar Layout Model With Merge and Round-Trip Tests

* Order and Hide Gizmo Bar Icons From the Saved Toolbar Layout

* Order and Hide Main Toolbar Items From the Saved Toolbar Layout

* Add a Customize Toolbar Dialog to the View Menu and Preferences

* Include What the Toolbar Layout Files Use

* Use the Gizmos' Contexts for Their Names and Never Press a Hidden Layer Editing Button

* Skip the Hidden Layer Editing Path When the Toolbar Has No Such Item

* Highlight the Selected Row and Move Rows in Place in the Customize Toolbar Dialog

A click anywhere on a full-width row selects it, and the selected row gets the list-selection colour. Selecting, moving and Reset now restyle or reorder the existing rows instead of rebuilding both lists, which flickered and, on GTK, made the rows 1px taller after each Reset. The up and down arrows sit to the left of the lists, and Reset to default joins OK and Cancel at the bottom.

* Show When a Row Can Move and Select a Row When Its Box Is Ticked

The up and down arrows are enabled, and drawn brighter, only when the selected row can move that way. Ticking a row's box also selects it, so it can be moved straight away, and an icon the current printer or selection leaves off the toolbar now says so.

* Show the Move Arrows on the Selected Row

The arrows sat in a column of their own to the left of both lists, which made them look as if they moved everything selected. Each row now has its own pair, shown only while the row is selected, so they plainly move that row. Their space is kept while hidden, so rows do not shift when the selection changes, and the pointer follows the arrow after a move so repeated clicks keep moving the same row. The arrows never take keyboard focus, so a disabled arrow cannot hand focus to another control; Alt+Up and Alt+Down move the selected row instead, and Tab selects a row by focusing its box.

* Leave the Pointer Where It Is After a Row Moves

Moving the pointer onto the arrow after each move kept repeated clicks on the same row, but apps moving the pointer is unexpected, macOS guidelines discourage it, and GNOME on Wayland ignores it, so the dialog behaved differently by platform. Without it the row slides out from under the pointer after a move, so a click where another row's hidden arrows sit now does nothing; before, a second click there selected the neighbour and a third moved it back. Alt+Up and Alt+Down remain the quick way to move a row several places.

* Move the Dialog's Row Actions to a Side Column and Add a Toolbar Right-Click Menu

The Customize Toolbar dialog gets a left column with Move up and Move
down, rows sit without gaps, the hovered row gets a border and the
selected row is highlighted without bold. Right-clicking the toolbar or
the gizmo bar on the Prepare canvas opens a menu with Customize Toolbar.

* Leave No Gap for Hidden Icons in the Toolbar Width

* Size the Customize Toolbar Dialog's Buttons and Lists Like the Mockup

* Save Named Gizmo Groups in the Toolbar Layout

The toolbar layout gains a list of named groups of gizmo bar icons. Loading drops members that are not gizmo bar icons or that an earlier group already holds, drops groups left with fewer than two members, and keeps each group's members together in the bar order. Entries this build does not recognise as groups are kept on save.

* Add Grouping, Ungrouping and Moving Rules for Gizmo Groups

The toolbar layout model gains the edits the Customize Toolbar dialog needs: the gizmo rows with a header before each group, making a group, adding rows to one, ungrouping, renaming, the default group name, and moving a row one step. A header moves its whole group, a plain row jumps a whole group, and a member moves within its group and leaves it at the group's edge; a group left with one member is removed.

* Compute a Gizmo Bar Slot for Each Group

Each group with at least two icons on the bar becomes one slot at its first icon's place. The slot shows the open tool, else the group's most recently used tool that can be opened, else the first that can, so it greys only when none of its tools can be opened.

* Collapse Each Gizmo Group Into One Bar Button With a List

Each group with two or more icons on the bar becomes one button with a corner triangle and the group's name as its tooltip. Clicking it opens a list under it, headed by the name, with each tool's icon, name and shortcut; a click on another group's button switches lists. A button does nothing while none of its own tools can be opened, a list closes when its group loses its slot, and the button shows the open or most recently used tool that can be opened.

* Let Users Group, Rename and Ungroup Gizmos in the Customize Toolbar Dialog

Ctrl+click or Shift+click selects several gizmo rows, and Group selected makes them a named group, asking for a name pre-filled with Group N; with a group and other rows selected it adds them to that group. A group shows as a bold header row with its members indented under it; its box is half-ticked when only some members show, and clicking it shows or hides them all. Rename (or a double-click on the header) and Ungroup act on the selected group, and the row arrows and Alt+Up/Down move a whole group, move a member within its group, or take it out at the group's edge. Rows rebuilt after the dialog is shown keep the row height measured when it opened.

* Let Users Group Object Tools Icons and Show Each Group as an Icon Grid

Groups now work in the Object tools column as well as the gizmo bar; a
group holds icons of one list, and the separator cannot be grouped. The
dialog's left column holds Rename, Move up, Move down, Group selected and
Ungroup. A group's list is a grid of toolbar-size icons, three per row,
named in their tooltips, and opens even when every member is disabled.
Three dots under a group's button mark it, in place of the corner
triangle.

* Match the Group Lists and the Dialog's Group Selection to the Mockup

The side buttons are full size with the short labels Up, Down and Group,
and each list is only as wide as its rows. Selecting a group highlights
its members with it. A group's list gets an accent border, and its title
is drawn in the normal text colour.

* Refresh Both Lists' Rows When Groups Change in the Customize Toolbar Dialog

Groups of both lists share one vector, so making or removing a group in
one list renumbers the other list's groups. Syncing only the changed
list left the other list's rows with stale group numbers, which picked
the wrong rows to highlight and could read past the end of the groups
on Ungroup or Reset.

* Ignore Focus Moves While the Customize Toolbar Dialog Syncs Its Rows

* Share One Group List Between the Two Bars and Open the Toolbar Menu From the Whole Strip

The main toolbar and the gizmo bar draw their group lists, dots and close
test with shared code, so the main toolbar's list now also stays on the
canvas. The lists' tooltips keep the usual border, open lists close when
the Customize Toolbar dialog opens, and a right click on the divider, the
gaps between gizmo icons or the assembly view button opens the menu too.

Smaller trims: one list of the gizmos shown on the bar, one gizmo id
lookup, a shorter choice of the tool a group shows, boost's trim, and
fewer one-line wrappers.

* Let a Quick Second Click Reopen a Group List

A press within the double-click time arrives as a double click, which group buttons ignored, so reopening a list right after closing it took another click. Group buttons now toggle on a double click like the bars' other icons, and a double click elsewhere closes an open list like a plain press.

* Keep the Upstream Main Toolbar Code and Reorder It Afterwards

Apply the saved layout to the stock toolbar with GLToolbar::apply_layout instead of rebuilding it from a builder map, open the toolbar menu from one right-click hook in GLCanvas3D, let the existing input-window path draw a gizmo opened while the gizmo bar is empty, read the layout through load_toolbar_layout, compute the gizmo bar's group slots once per pass, and skip folding when there are no groups and no group slots are left.

* Hide Empty Toolbars Fully and Keep the Group Dots Under Windows

- An empty main toolbar or gizmo bar takes no width and draws nothing, and a
  divider only follows a bar that shows icons.
- The group dots are drawn on ImGui's background list, so windows and
  tooltips cover them.
- The main toolbar is placed before its dots and group list are drawn, so
  they follow it right after a resize.
- A test ties every gizmo type to its id in the layout list.

* Keep the MCP Server's Gizmo List and Toolbar Clicks Right With Grouped and Hidden Icons

* Share Plain 16 px Arrow Icons for the Toolbar Dialog's Move Buttons

* Keep the Toolbar Dialog's Width and Keyboard Focus Steady While Rows Move

Measure a group header from its text, and only when its name changes:
the best size never drops below the min size, so every move widened
the header by 6 px and refitted the dialog.

Treat a double click on the Move buttons and row arrows as a press, so
a quick second click on Windows is not lost. Focus the clicked row's
box, so Space toggles that row, and let Up and Down select the row
they focus, as Tab does.

The MCP refusal for a hidden toolbar item now says when the printer,
not the layout, hides it.

* Ungroup Only the Selected Icons and Widen the List of a Large Toolbar Group

- Ungroup with only members selected takes those icons out of the group
  and puts them right below it; with the header selected it removes the
  group, as before. Either way the rows stay selected.
- A member moved up or down stops at its group's first and last place
  instead of leaving the group there.
- Group skips a separator in the selection instead of being disabled.
- A group's list is four icons wide from ten icons and five wide from
  seventeen.
- The main toolbar's group list takes the canvas's display scale, as the
  gizmo bar's does, so both have the same padding on a 2x display.
- Focus moves are ignored while the buttons take their new state: on
  Windows a focused button that gets disabled hands its focus to a row.

* Let a Click Decide the Selection After the Keyboard Was Used in the Toolbar Dialog

Once Tab, Up or Down had been pressed, a row's box taking the focus
selected its row from then on, also when a mouse click moved the focus
there. A click where a row's hidden arrows sit then selected that row,
and a Ctrl+click that deselected a row on its own area, not its name,
made it the only selection. A click on a row now ends that state until
the next Tab or arrow key. Left and Right count as Up and Down do, so
the row they focus is selected too.
2026-12-01 13:15:39 +08:00

1982 lines
70 KiB
C++

#include "libslic3r/Point.hpp"
#include "GLToolbar.hpp"
#include "slic3r/GUI/GLCanvas3D.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/Camera.hpp"
#include "slic3r/GUI/Plater.hpp"
#include "slic3r/GUI/ImGuiWrapper.hpp"
#include "slic3r/GUI/ToolbarLayout.hpp"
#include <boost/container_hash/hash.hpp>
#include <boost/functional/hash.hpp>
#include "libslic3r/Utils.hpp"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <set>
#include <string>
#include <cstddef>
#include <functional>
#include <vector>
#include <utility>
#include <imgui/imgui.h>
#include <wx/event.h>
#include <wx/bitmap.h>
#include <wx/dcmemory.h>
#include <wx/settings.h>
#include <wx/glcanvas.h>
#include "slic3r/GUI/GLTexture.hpp"
class wxFont;
namespace Slic3r::GUI { struct IntEvent; }
namespace Slic3r::GUI { struct SimpleEvent; }
namespace Slic3r {
namespace GUI {
//BBS: GUI refactor: GLToolbar
wxDEFINE_EVENT(EVT_GLTOOLBAR_OPEN_PROJECT, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SLICE_ALL, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SLICE_PLATE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_PRINT_ALL, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_PRINT_PLATE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_EXPORT_GCODE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SEND_GCODE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_UPLOAD_GCODE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_EXPORT_SLICED_FILE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_EXPORT_ALL_SLICED_FILE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_PRINT_SELECT, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SEND_TO_PRINTER, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SEND_TO_PRINTER_ALL, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_PRINT_MULTI_MACHINE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_ADD, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_DELETE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_DELETE_ALL, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_ADD_PLATE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_DEL_PLATE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_ORIENT, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_ARRANGE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_CUT, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_COPY, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_PASTE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_LAYERSEDITING, SimpleEvent);
//BBS: add clone event
wxDEFINE_EVENT(EVT_GLTOOLBAR_CLONE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_MORE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_FEWER, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SPLIT_OBJECTS, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SPLIT_VOLUMES, SimpleEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_FILLCOLOR, IntEvent);
wxDEFINE_EVENT(EVT_GLTOOLBAR_SELECT_SLICED_PLATE, wxCommandEvent);
wxDEFINE_EVENT(EVT_GLVIEWTOOLBAR_3D, SimpleEvent);
wxDEFINE_EVENT(EVT_GLVIEWTOOLBAR_PREVIEW, SimpleEvent);
wxDEFINE_EVENT(EVT_GLVIEWTOOLBAR_ASSEMBLE, SimpleEvent);
const GLToolbarItem::ActionCallback GLToolbarItem::Default_Action_Callback = [](){};
const GLToolbarItem::VisibilityCallback GLToolbarItem::Default_Visibility_Callback = []()->bool { return true; };
const GLToolbarItem::EnablingCallback GLToolbarItem::Default_Enabling_Callback = []()->bool { return true; };
const GLToolbarItem::RenderCallback GLToolbarItem::Default_Render_Callback = [](float, float, float, float){};
GLToolbarItem::Data::Option::Option()
: toggable(false)
, action_callback(Default_Action_Callback)
, render_callback(nullptr)
{
}
GLToolbarItem::Data::Data()
: name("")
, icon_filename("")
, tooltip("")
, additional_tooltip("")
, sprite_id(-1)
, visible(true)
, visibility_callback(Default_Visibility_Callback)
, enabling_callback(Default_Enabling_Callback)
//BBS: GUI refactor
, extra_size_ratio(0.0)
, button_text("")
//BBS gei a default value
, image_width(40)
, image_height(40)
{
}
GLToolbarItem::GLToolbarItem(GLToolbarItem::EType type, const GLToolbarItem::Data& data)
: m_type(type)
, m_state(Normal)
, m_data(data)
, m_last_action_type(Undefined)
, m_highlight_state(NotHighlighted)
{
render_left_pos = 0.0f;
}
bool GLToolbarItem::update_visibility()
{
bool visible = m_data.visibility_callback();
bool ret = (m_data.visible != visible);
if (ret)
m_data.visible = visible;
// Return false for separator as it would always return true.
return is_separator() ? false : ret;
}
bool GLToolbarItem::update_enabled_state()
{
bool enabled = m_data.enabling_callback();
bool ret = (is_enabled() != enabled);
if (ret)
m_state = enabled ? GLToolbarItem::Normal : GLToolbarItem::Disabled;
return ret;
}
//BBS: GUI refactor: GLToolbar
void GLToolbarItem::render_text(float left, float right, float bottom, float top) const
{
float tex_width = (float)m_data.text_texture.get_width();
float tex_height = (float)m_data.text_texture.get_height();
//float inv_tex_width = (tex_width != 0.0f) ? 1.0f / tex_width : 0.0f;
//float inv_tex_height = (tex_height != 0.0f) ? 1.0f / tex_height : 0.0f;
float internal_left_uv = 0.0f;
float internal_right_uv = (float)m_data.text_texture.m_original_width / tex_width;
float internal_top_uv = 0.0f;
float internal_bottom_uv = (float)m_data.text_texture.m_original_height / tex_height;
GLTexture::render_sub_texture(m_data.text_texture.get_id(), left, right, bottom, top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
}
//BBS: GUI refactor: GLToolbar
int GLToolbarItem::generate_texture(wxFont& font)
{
int ret = 0;
bool result;
if (m_type != ActionWithText && m_type != ActionWithTextImage)
return -1;
result = m_data.text_texture.generate_from_text_string(m_data.button_text, font);
if (!result)
ret = -1;
return ret;
}
int GLToolbarItem::generate_image_texture()
{
int ret = 0;
bool result = false;
if (m_type != ActionWithTextImage)
return -1;
/* load default texture when image is empty */
if (m_data.image_data.empty()) {
std::string default_image = resources_dir() + "/images/default_thumbnail.svg";
result = m_data.image_texture.load_from_svg_file(default_image, true, false, false, m_data.image_width);
} else {
result = m_data.image_texture.load_from_raw_data(m_data.image_data, m_data.image_width, m_data.image_height);
}
if (!result)
ret = -1;
return ret;
}
void GLToolbarItem::render(unsigned int tex_id, float left, float right, float bottom, float top, unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) const
{
auto uvs = [this](unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) -> GLTexture::Quad_UVs
{
assert((tex_width != 0) && (tex_height != 0));
GLTexture::Quad_UVs ret;
// tiles in the texture are spaced by 1 pixel
float icon_size_px = (float)(tex_width - 1) / ((float)Num_States + (float)Num_Rendered_Highlight_States);
char render_state = (m_highlight_state == NotHighlighted ? m_state : Num_States + m_highlight_state);
float inv_tex_width = 1.0f / (float)tex_width;
float inv_tex_height = 1.0f / (float)tex_height;
// tiles in the texture are spaced by 1 pixel
float u_offset = 1.0f * inv_tex_width;
float v_offset = 1.0f * inv_tex_height;
float du = icon_size_px * inv_tex_width;
float dv = icon_size_px * inv_tex_height;
float left = u_offset + (float)render_state * du;
float right = left + du - u_offset;
float top = v_offset + (float)m_data.sprite_id * dv;
float bottom = top + dv - v_offset;
ret.left_top = { left, top };
ret.left_bottom = { left, bottom };
ret.right_bottom = { right, bottom };
ret.right_top = { right, top };
return ret;
};
GLTexture::render_sub_texture(tex_id, left, right, bottom, top, uvs(tex_width, tex_height, icon_size));
}
void GLToolbarItem::render_window(float left, float right, float bottom, float top) const
{
if (is_pressed())
{
if ((m_last_action_type == Left) && m_data.left.can_render())
m_data.left.render_callback(left, right, bottom, top);
else if ((m_last_action_type == Right) && m_data.right.can_render())
m_data.right.render_callback(left, right, bottom, top);
}
}
void GLToolbarItem::render_image(unsigned int tex_id, float left, float right, float bottom, float top, unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) const
{
GLTexture::Quad_UVs image_uvs = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f } };
//GLTexture::Quad_UVs image_uvs = { { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f }, { 0.0f, 0.0f } };
GLTexture::render_sub_texture(tex_id, left, right, bottom, top, image_uvs);
}
BackgroundTexture::Metadata::Metadata()
: filename("")
, left(0)
, right(0)
, top(0)
, bottom(0)
{
}
const float GLToolbar::Default_Icons_Size = 40.0f;
GLToolbar::Layout::Layout()
: type(Horizontal)
, horizontal_orientation(HO_Center)
, vertical_orientation(VO_Center)
, top(0.0f)
, left(0.0f)
, border(0.0f)
, corner_radius(0.0f)
, separator_size(0.0f)
, gap_size(0.0f)
, icons_size(Default_Icons_Size)
, scale(1.0f)
, width(0.0f)
, height(0.0f)
, dirty(true)
{
}
GLToolbar::GLToolbar(GLToolbar::EType type, const std::string& name)
: m_type(type)
, m_name(name)
, m_enabled(false)
, m_icons_texture_dirty(true)
, m_pressed_toggable_id(-1)
{
}
GLToolbar::~GLToolbar()
{
for (GLToolbarItem* item : m_items)
{
delete item;
}
}
bool GLToolbar::init(const BackgroundTexture::Metadata& background_texture)
{
std::string path = resources_dir() + "/images/";
bool res = false;
if (!background_texture.filename.empty())
res = m_background_texture.texture.load_from_file(path + background_texture.filename, false, GLTexture::SingleThreaded, false);
if (res)
m_background_texture.metadata = background_texture;
return res;
}
bool GLToolbar::init_arrow(const std::string& filename)
{
if (m_arrow_texture.get_id() != 0)
return true;
const std::string path = resources_dir() + "/images/";
return (!filename.empty()) ? m_arrow_texture.load_from_svg_file(path + filename, false, false, false, 1000) : false;
}
GLToolbar::Layout::EType GLToolbar::get_layout_type() const
{
return m_layout.type;
}
void GLToolbar::set_layout_type(GLToolbar::Layout::EType type)
{
m_layout.type = type;
m_layout.dirty = true;
}
void GLToolbar::set_position(float top, float left)
{
m_layout.top = top;
m_layout.left = left;
}
void GLToolbar::set_border(float border)
{
m_layout.border = border;
m_layout.dirty = true;
}
void GLToolbar::set_separator_size(float size)
{
m_layout.separator_size = size;
m_layout.dirty = true;
}
void GLToolbar::set_gap_size(float size)
{
m_layout.gap_size = size;
m_layout.dirty = true;
}
void GLToolbar::set_icons_size(float size)
{
if (m_layout.icons_size != size)
{
m_layout.icons_size = size;
m_layout.dirty = true;
m_icons_texture_dirty = true;
}
}
void GLToolbar::set_text_size(float size)
{
if (m_layout.text_size != size)
{
m_layout.text_size = size;
m_layout.dirty = true;
}
}
void GLToolbar::set_scale(float scale)
{
if (m_layout.scale != scale) {
m_layout.scale = scale;
m_layout.dirty = true;
m_icons_texture_dirty = true;
}
}
//BBS: GUI refactor: GLToolbar
bool GLToolbar::add_item(const GLToolbarItem::Data& data, GLToolbarItem::EType type)
{
GLToolbarItem* item = new GLToolbarItem(type, data);
if (item == nullptr)
return false;
m_items.push_back(item);
m_layout.dirty = true;
return true;
}
bool GLToolbar::del_all_item()
{
for (int i = 0; i < m_items.size(); i++) {
delete m_items[i];
m_items[i] = nullptr;
}
m_items.clear();
m_layout.dirty = true;
return true;
}
bool GLToolbar::add_separator()
{
GLToolbarItem::Data data;
GLToolbarItem* item = new GLToolbarItem(GLToolbarItem::Separator, data);
if (item == nullptr)
return false;
m_items.push_back(item);
m_layout.dirty = true;
return true;
}
float GLToolbar::get_width()
{
if (m_layout.dirty)
calc_layout();
return m_layout.width;
}
float GLToolbar::get_height()
{
if (m_layout.dirty)
calc_layout();
return m_layout.height;
}
void GLToolbar::select_item(const std::string& name)
{
if (is_item_disabled(name))
return;
for (GLToolbarItem* item : m_items)
{
if (!item->is_disabled())
{
bool hover = item->is_hovered();
item->set_state((item->get_name() == name) ? (hover ? GLToolbarItem::HoverPressed : GLToolbarItem::Pressed) : (hover ? GLToolbarItem::Hover : GLToolbarItem::Normal));
}
}
}
bool GLToolbar::is_item_pressed(const std::string& name) const
{
for (const GLToolbarItem* item : m_items)
{
if (item->get_name() == name)
return item->is_pressed();
}
return false;
}
bool GLToolbar::is_item_disabled(const std::string& name) const
{
for (const GLToolbarItem* item : m_items)
{
if (item->get_name() == name)
return item->is_disabled();
}
return false;
}
bool GLToolbar::is_item_visible(const std::string& name) const
{
for (const GLToolbarItem* item : m_items)
{
// On the toolbar, though maybe folded into its group's button.
if (item->get_name() == name)
return item->m_data.visible;
}
return false;
}
bool GLToolbar::is_any_item_pressed() const
{
for (const GLToolbarItem* item : m_items)
{
if (item->is_pressed())
return true;
}
return false;
}
int GLToolbar::get_item_id(const std::string& name) const
{
for (int i = 0; i < (int)m_items.size(); ++i)
{
if (m_items[i]->get_name() == name)
return i;
}
return -1;
}
std::string GLToolbar::get_tooltip() const
{
std::string tooltip;
for (size_t id = 0; id < m_items.size(); ++id)
{
const GLToolbarItem* item = m_items[id];
if (item->is_hovered())
{
// A group's button is named for the group, and says nothing while its list is open below it.
if (const int group = group_of_slot(static_cast<int>(id)); group >= 0)
return group == m_group_popup.open ? std::string() : m_groups[static_cast<size_t>(group)].name;
tooltip = item->get_tooltip();
if (!item->is_pressed())
{
const std::string& additional_tooltip = item->get_additional_tooltip();
if (!additional_tooltip.empty())
tooltip += "\n" + additional_tooltip;
break;
}
}
}
return tooltip;
}
void GLToolbar::get_additional_tooltip(int item_id, std::string& text)
{
if (0 <= item_id && item_id < (int)m_items.size())
{
text = m_items[item_id]->get_additional_tooltip();
return;
}
text.clear();
}
void GLToolbar::set_additional_tooltip(int item_id, const std::string& text)
{
if (0 <= item_id && item_id < (int)m_items.size())
m_items[item_id]->set_additional_tooltip(text);
}
void GLToolbar::set_tooltip(int item_id, const std::string& text)
{
if (0 <= item_id && item_id < (int)m_items.size())
m_items[item_id]->set_tooltip(text);
}
bool GLToolbar::update_items_state()
{
bool ret = false;
ret |= update_items_visibility();
ret |= update_items_enabled_state();
if (!is_any_item_pressed())
m_pressed_toggable_id = -1;
return ret;
}
void GLToolbar::render(const GLCanvas3D& parent,GLToolbarItem::EType type)
{
if (!m_enabled || m_items.empty())
return;
if (m_icons_texture_dirty)
generate_icons_texture();
switch (m_layout.type)
{
default:
case Layout::Horizontal: { render_horizontal(parent, type, true); break; }
case Layout::Vertical: { render_vertical(parent, true); break; }
}
}
void GLToolbar::render_item_windows(const GLCanvas3D& parent)
{
if (!m_enabled || m_items.empty())
return;
switch (m_layout.type)
{
default:
case Layout::Horizontal: { render_horizontal(parent, GLToolbarItem::Action, false); render_group_marks(parent); break; }
case Layout::Vertical: { render_vertical(parent, false); break; }
}
}
size_t GLToolbar::get_state_hash() const
{
size_t hash = 0;
boost::hash_combine(hash, m_enabled);
boost::hash_combine(hash, m_group_popup.open);
for (const GLToolbarItem* item : m_items) {
boost::hash_combine(hash, (int)item->get_state());
boost::hash_combine(hash, item->is_visible());
}
return hash;
}
bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent)
{
if (!m_enabled)
return false;
const Vec2d mouse_pos((double)evt.GetX(), (double)evt.GetY());
bool processed = false;
// mouse anywhere
if (!evt.Dragging() && !evt.Leaving() && !evt.Entering() && m_mouse_capture.parent != nullptr) {
if (m_mouse_capture.any() && (evt.LeftUp() || evt.MiddleUp() || evt.RightUp())) {
// prevents loosing selection into the scene if mouse down was done inside the toolbar and mouse up was down outside it,
// as when switching between views
m_mouse_capture.reset();
return true;
}
m_mouse_capture.reset();
}
if (evt.Moving())
update_hover_state(mouse_pos, parent);
else if (evt.LeftUp()) {
if (m_mouse_capture.left) {
processed = true;
m_mouse_capture.left = false;
}
else
return false;
}
else if (evt.MiddleUp()) {
if (m_mouse_capture.middle) {
processed = true;
m_mouse_capture.middle = false;
}
else
return false;
}
else if (evt.RightUp()) {
if (m_mouse_capture.right) {
processed = true;
m_mouse_capture.right = false;
}
else
return false;
}
else if (evt.Dragging()) {
if (m_mouse_capture.any())
// if the button down was done on this toolbar, prevent from dragging into the scene
processed = true;
else
return false;
}
const int item_id = contains_mouse(mouse_pos, parent);
if (item_id != -1) {
// mouse inside toolbar
if (evt.LeftDown() || evt.LeftDClick()) {
m_mouse_capture.left = true;
m_mouse_capture.parent = &parent;
processed = true;
if (const int group = group_of_slot(item_id); group >= 0) {
// A double click toggles too: a quick reopen after closing arrives as one.
if (group == m_group_popup.open) {
close_group_list();
parent.set_as_dirty();
} else
open_group_list(group, parent);
}
else if (item_id != -2 && !m_items[item_id]->is_separator() && !m_items[item_id]->is_disabled() &&
(m_pressed_toggable_id == -1 || m_items[item_id]->get_last_action_type() == GLToolbarItem::Left)) {
// mouse is inside an icon
do_action(GLToolbarItem::Left, item_id, parent, true);
parent.set_as_dirty();
}
}
else if (evt.MiddleDown()) {
m_mouse_capture.middle = true;
m_mouse_capture.parent = &parent;
}
else if (evt.RightDown()) {
m_mouse_capture.right = true;
m_mouse_capture.parent = &parent;
processed = true;
if (item_id != -2 && !m_items[item_id]->is_separator() && !m_items[item_id]->is_disabled() &&
(m_pressed_toggable_id == -1 || m_items[item_id]->get_last_action_type() == GLToolbarItem::Right)) {
// mouse is inside an icon
do_action(GLToolbarItem::Right, item_id, parent, true);
parent.set_as_dirty();
}
}
}
return processed;
}
void GLToolbar::calc_layout()
{
switch (m_layout.type)
{
default:
case Layout::Horizontal:
{
m_layout.width = get_width_horizontal();
m_layout.height = get_height_horizontal();
break;
}
case Layout::Vertical:
{
m_layout.width = get_width_vertical();
m_layout.height = get_height_vertical();
break;
}
}
m_layout.dirty = false;
}
//BBS: GUI refactor: GLToolbar
float GLToolbar::get_width_horizontal() const
{
float size = 2.0f * m_layout.border;
int shown = 0;
for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i)
{
if (!m_items[i]->is_visible())
continue;
++shown;
if (m_items[i]->is_separator())
size += m_layout.separator_size;
else
{
size += (float)m_layout.icons_size;
if (m_items[i]->is_action_with_text())
size += m_items[i]->get_extra_size_ratio() * m_layout.icons_size;
if (m_items[i]->is_action_with_text_image())
size += m_layout.text_size;
}
}
// A toolbar with every item hidden takes no space.
if (shown == 0)
return 0.0f;
if (shown > 1)
size += static_cast<float>(shown - 1) * m_layout.gap_size;
return size * m_layout.scale;
}
//BBS: GUI refactor: GLToolbar
float GLToolbar::get_width_vertical() const
{
float max_extra_text_size = 0.0;
for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i)
{
if (m_items[i]->is_action_with_text())
{
float temp_size = m_items[i]->get_extra_size_ratio() * m_layout.icons_size;
max_extra_text_size = (temp_size > max_extra_text_size) ? temp_size : max_extra_text_size;
}
if (m_items[i]->is_action_with_text_image())
{
max_extra_text_size = m_layout.text_size;
}
}
return (2.0f * m_layout.border + m_layout.icons_size + max_extra_text_size) * m_layout.scale;
}
float GLToolbar::get_height_horizontal() const
{
return (2.0f * m_layout.border + m_layout.icons_size) * m_layout.scale;
}
float GLToolbar::get_height_vertical() const
{
return get_main_size();
}
float GLToolbar::get_main_size() const
{
float size = 2.0f * m_layout.border;
int shown = 0;
for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i)
{
if (!m_items[i]->is_visible())
continue;
++shown;
if (m_items[i]->is_separator())
size += m_layout.separator_size;
else
size += (float)m_layout.icons_size;
}
if (shown > 1)
size += static_cast<float>(shown - 1) * m_layout.gap_size;
return size * m_layout.scale;
}
int GLToolbar::get_visible_items_cnt() const
{
int cnt = 0;
for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i)
if (m_items[i]->is_visible() && !m_items[i]->is_separator())
cnt++;
return cnt;
}
void GLToolbar::do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas3D& parent, bool check_hover)
{
if (m_pressed_toggable_id == -1 || m_pressed_toggable_id == item_id) {
if (0 <= item_id && item_id < (int)m_items.size()) {
GLToolbarItem* item = m_items[item_id];
if (item != nullptr && !item->is_separator() && !item->is_disabled() && (!check_hover || item->is_hovered())) {
// A grouped item used, from its button, its group's list or a shortcut, becomes what its button shows.
m_recent_items.erase(std::remove(m_recent_items.begin(), m_recent_items.end(), item->get_name()), m_recent_items.end());
m_recent_items.insert(m_recent_items.begin(), item->get_name());
if (fold_groups())
m_layout.dirty = true;
if ((type == GLToolbarItem::Right && item->is_right_toggable()) ||
(type == GLToolbarItem::Left && item->is_left_toggable())) {
GLToolbarItem::EState state = item->get_state();
if (state == GLToolbarItem::Hover)
item->set_state(GLToolbarItem::HoverPressed);
else if (state == GLToolbarItem::HoverPressed)
item->set_state(GLToolbarItem::Hover);
else if (state == GLToolbarItem::Pressed)
item->set_state(GLToolbarItem::Normal);
else if (state == GLToolbarItem::Normal)
item->set_state(GLToolbarItem::Pressed);
m_pressed_toggable_id = item->is_pressed() ? item_id : -1;
item->reset_last_action_type();
parent.render();
switch (type)
{
default:
case GLToolbarItem::Left: { item->do_left_action(); break; }
case GLToolbarItem::Right: { item->do_right_action(); break; }
}
}
else {
if (m_type == Radio)
select_item(item->get_name());
else
item->set_state(item->is_hovered() ? GLToolbarItem::HoverPressed : GLToolbarItem::Pressed);
item->reset_last_action_type();
parent.render();
switch (type)
{
default:
case GLToolbarItem::Left: { item->do_left_action(); break; }
case GLToolbarItem::Right: { item->do_right_action(); break; }
}
if (m_type == Normal && item->get_state() != GLToolbarItem::Disabled) {
// the item may get disabled during the action, if not, set it back to hover state
item->set_state(GLToolbarItem::Hover);
parent.render();
}
}
}
}
}
}
void GLToolbar::update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent)
{
if (!m_enabled)
return;
switch (m_layout.type)
{
default:
case Layout::Horizontal: { update_hover_state_horizontal(mouse_pos, parent); break; }
case Layout::Vertical: { update_hover_state_vertical(mouse_pos, parent); break; }
}
}
void GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent)
{
const Size cnv_size = parent.get_canvas_size();
const Vec2d scaled_mouse_pos((mouse_pos.x() - 0.5 * (double)cnv_size.get_width()), (0.5 * (double)cnv_size.get_height() - mouse_pos.y()));
const float icons_size = m_layout.icons_size * m_layout.scale;
const float separator_size = m_layout.separator_size * m_layout.scale;
const float gap_size = m_layout.gap_size * m_layout.scale;
const float border = m_layout.border * m_layout.scale;
const float separator_stride = separator_size + gap_size;
const float icon_stride = icons_size + gap_size;
float left = m_layout.left + border;
float top = m_layout.top - border;
for (GLToolbarItem* item : m_items) {
if (!item->is_visible())
continue;
if (item->is_separator())
left += separator_stride;
else {
float right = left + icons_size;
const float bottom = top - icons_size;
//BBS: GUI refactor: GLToolbar
if (item->is_action_with_text())
right += icons_size * item->get_extra_size_ratio();
const GLToolbarItem::EState state = item->get_state();
bool inside = (left <= (float)scaled_mouse_pos.x()) &&
((float)scaled_mouse_pos.x() <= right) &&
(bottom <= (float)scaled_mouse_pos.y()) &&
((float)scaled_mouse_pos.y() <= top);
switch (state)
{
case GLToolbarItem::Normal:
{
if (inside) {
item->set_state(GLToolbarItem::Hover);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::Hover:
{
if (!inside) {
item->set_state(GLToolbarItem::Normal);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::Pressed:
{
if (inside) {
item->set_state(GLToolbarItem::HoverPressed);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::HoverPressed:
{
if (!inside) {
item->set_state(GLToolbarItem::Pressed);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::Disabled:
{
if (inside) {
item->set_state(GLToolbarItem::HoverDisabled);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::HoverDisabled:
{
if (!inside) {
item->set_state(GLToolbarItem::Disabled);
parent.set_as_dirty();
}
break;
}
default:
{
break;
}
}
left += icon_stride;
//BBS: GUI refactor: GLToolbar
if (item->is_action_with_text())
left += icons_size * item->get_extra_size_ratio();
}
}
}
void GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent)
{
const Size cnv_size = parent.get_canvas_size();
const Vec2d scaled_mouse_pos((mouse_pos.x() - 0.5 * (double)cnv_size.get_width()), (0.5 * (double)cnv_size.get_height() - mouse_pos.y()));
const float icons_size = m_layout.icons_size * m_layout.scale;
const float separator_size = m_layout.separator_size * m_layout.scale;
const float gap_size = m_layout.gap_size * m_layout.scale;
const float border = m_layout.border * m_layout.scale;
const float separator_stride = separator_size + gap_size;
const float icon_stride = icons_size + gap_size;
float left = m_layout.left + border;
float top = m_layout.top - border;
for (GLToolbarItem* item : m_items) {
if (!item->is_visible())
continue;
if (item->is_separator())
top -= separator_stride;
else {
float right = left + icons_size;
const float bottom = top - icons_size;
if (item->is_action_with_text_image())
right += m_layout.text_size * m_layout.scale;
//BBS: GUI refactor: GLToolbar
if (item->is_action_with_text())
right += icons_size * item->get_extra_size_ratio();
GLToolbarItem::EState state = item->get_state();
const bool inside = (left <= (float)scaled_mouse_pos.x()) &&
((float)scaled_mouse_pos.x() <= right) &&
(bottom <= (float)scaled_mouse_pos.y()) &&
((float)scaled_mouse_pos.y() <= top);
switch (state)
{
case GLToolbarItem::Normal:
{
if (inside) {
item->set_state(GLToolbarItem::Hover);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::Hover:
{
if (!inside) {
item->set_state(GLToolbarItem::Normal);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::Pressed:
{
if (inside) {
item->set_state(GLToolbarItem::HoverPressed);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::HoverPressed:
{
if (!inside) {
item->set_state(GLToolbarItem::Pressed);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::Disabled:
{
if (inside) {
item->set_state(GLToolbarItem::HoverDisabled);
parent.set_as_dirty();
}
break;
}
case GLToolbarItem::HoverDisabled:
{
if (!inside) {
item->set_state(GLToolbarItem::Disabled);
parent.set_as_dirty();
}
break;
}
default:
{
break;
}
}
top -= icon_stride;
}
}
}
GLToolbarItem* GLToolbar::get_item(const std::string& item_name)
{
if (!m_enabled)
return nullptr;
for (GLToolbarItem* item : m_items)
{
if (item->get_name() == item_name)
{
return item;
}
}
return nullptr;
}
int GLToolbar::contains_mouse(const Vec2d& mouse_pos, const GLCanvas3D& parent) const
{
if (!m_enabled)
return -1;
switch (m_layout.type)
{
default:
case Layout::Horizontal: { return contains_mouse_horizontal(mouse_pos, parent); }
case Layout::Vertical: { return contains_mouse_vertical(mouse_pos, parent); }
}
}
int GLToolbar::contains_mouse_horizontal(const Vec2d& mouse_pos, const GLCanvas3D& parent) const
{
const Size cnv_size = parent.get_canvas_size();
const Vec2d scaled_mouse_pos((mouse_pos.x() - 0.5 * (double)cnv_size.get_width()), (0.5 * (double)cnv_size.get_height() - mouse_pos.y()));
const float icons_size = m_layout.icons_size * m_layout.scale;
const float separator_size = m_layout.separator_size * m_layout.scale;
const float gap_size = m_layout.gap_size * m_layout.scale;
const float border = m_layout.border * m_layout.scale;
float left = m_layout.left + border;
const float top = m_layout.top - border;
for (size_t id = 0; id < m_items.size(); ++id) {
GLToolbarItem* item = m_items[id];
if (!item->is_visible())
continue;
if (item->is_separator()) {
float right = left + separator_size;
const float bottom = top - icons_size;
// mouse inside the separator
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return id;
left = right;
right += gap_size;
if (id < m_items.size() - 1) {
// mouse inside the gap
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return -2;
}
left = right;
}
else {
float right = left + icons_size;
const float bottom = top - icons_size;
//BBS: GUI refactor: GLToolbar
if (item->is_action_with_text())
right += icons_size * item->get_extra_size_ratio();
// mouse inside the icon
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return id;
left = right;
right += gap_size;
if (id < m_items.size() - 1) {
// mouse inside the gap
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return -2;
}
left = right;
}
}
return -1;
}
int GLToolbar::contains_mouse_vertical(const Vec2d& mouse_pos, const GLCanvas3D& parent) const
{
const Size cnv_size = parent.get_canvas_size();
const Vec2d scaled_mouse_pos((mouse_pos.x() - 0.5 * (double)cnv_size.get_width()), (0.5 * (double)cnv_size.get_height() - mouse_pos.y()));
const float icons_size = m_layout.icons_size * m_layout.scale;
const float separator_size = m_layout.separator_size * m_layout.scale;
const float gap_size = m_layout.gap_size * m_layout.scale;
const float border = m_layout.border * m_layout.scale;
const float left = m_layout.left + border;
float top = m_layout.top - border;
for (size_t id = 0; id < m_items.size(); ++id) {
GLToolbarItem* item = m_items[id];
if (!item->is_visible())
continue;
if (item->is_separator()) {
const float right = left + icons_size;
float bottom = top - separator_size;
// mouse inside the separator
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return id;
top = bottom;
bottom -= gap_size;
if (id < m_items.size() - 1) {
// mouse inside the gap
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return -2;
}
top = bottom;
}
else {
float right = left + icons_size;
float bottom = top - icons_size;
if (item->is_action_with_text_image())
right += m_layout.text_size * m_layout.scale;
//BBS: GUI refactor: GLToolbar
if (item->is_action_with_text())
right += icons_size * item->get_extra_size_ratio();
// mouse inside the icon
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return id;
top = bottom;
bottom -= gap_size;
if (id < m_items.size() - 1) {
// mouse inside the gap
if (left <= (float)scaled_mouse_pos.x() &&
(float)scaled_mouse_pos.x() <= right &&
bottom <= (float)scaled_mouse_pos.y() &&
(float)scaled_mouse_pos.y() <= top)
return -2;
}
top = bottom;
}
}
return -1;
}
void GLToolbar::render_background(float left, float top, float right, float bottom, float corner_w, float corner_h) const
{
const unsigned int tex_id = m_background_texture.texture.get_id();
const float tex_width = (float)m_background_texture.texture.get_width();
const float tex_height = (float)m_background_texture.texture.get_height();
if (tex_id != 0 && tex_width > 0.0f && tex_height > 0.0f) {
const float inv_tex_width = 1.0f / tex_width;
const float inv_tex_height = 1.0f / tex_height;
const float internal_left = left + corner_w;
const float internal_right = right - corner_w;
const float internal_top = top - corner_h;
const float internal_bottom = bottom + corner_h;
const float left_uv = 0.0f;
const float right_uv = 1.0f;
const float top_uv = 1.0f;
const float bottom_uv = 0.0f;
const float internal_left_uv = (float)m_background_texture.metadata.left * inv_tex_width;
const float internal_right_uv = 1.0f - (float)m_background_texture.metadata.right * inv_tex_width;
const float internal_top_uv = 1.0f - (float)m_background_texture.metadata.top * inv_tex_height;
const float internal_bottom_uv = (float)m_background_texture.metadata.bottom * inv_tex_height;
// top-left corner
if (m_layout.horizontal_orientation == Layout::HO_Left || m_layout.vertical_orientation == Layout::VO_Top)
GLTexture::render_sub_texture(tex_id, left, internal_left, internal_top, top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, left, internal_left, internal_top, top, { { left_uv, internal_top_uv }, { internal_left_uv, internal_top_uv }, { internal_left_uv, top_uv }, { left_uv, top_uv } });
// top edge
if (m_layout.vertical_orientation == Layout::VO_Top)
GLTexture::render_sub_texture(tex_id, internal_left, internal_right, internal_top, top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, internal_left, internal_right, internal_top, top, { { internal_left_uv, internal_top_uv }, { internal_right_uv, internal_top_uv }, { internal_right_uv, top_uv }, { internal_left_uv, top_uv } });
// top-right corner
if (m_layout.horizontal_orientation == Layout::HO_Right || m_layout.vertical_orientation == Layout::VO_Top)
GLTexture::render_sub_texture(tex_id, internal_right, right, internal_top, top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, internal_right, right, internal_top, top, { { internal_right_uv, internal_top_uv }, { right_uv, internal_top_uv }, { right_uv, top_uv }, { internal_right_uv, top_uv } });
// center-left edge
if (m_layout.horizontal_orientation == Layout::HO_Left)
GLTexture::render_sub_texture(tex_id, left, internal_left, internal_bottom, internal_top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, left, internal_left, internal_bottom, internal_top, { { left_uv, internal_bottom_uv }, { internal_left_uv, internal_bottom_uv }, { internal_left_uv, internal_top_uv }, { left_uv, internal_top_uv } });
// center
GLTexture::render_sub_texture(tex_id, internal_left, internal_right, internal_bottom, internal_top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
// center-right edge
if (m_layout.horizontal_orientation == Layout::HO_Right)
GLTexture::render_sub_texture(tex_id, internal_right, right, internal_bottom, internal_top, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, internal_right, right, internal_bottom, internal_top, { { internal_right_uv, internal_bottom_uv }, { right_uv, internal_bottom_uv }, { right_uv, internal_top_uv }, { internal_right_uv, internal_top_uv } });
// bottom-left corner
if (m_layout.horizontal_orientation == Layout::HO_Left || m_layout.vertical_orientation == Layout::VO_Bottom)
GLTexture::render_sub_texture(tex_id, left, internal_left, bottom, internal_bottom, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, left, internal_left, bottom, internal_bottom, { { left_uv, bottom_uv }, { internal_left_uv, bottom_uv }, { internal_left_uv, internal_bottom_uv }, { left_uv, internal_bottom_uv } });
// bottom edge
if (m_layout.vertical_orientation == Layout::VO_Bottom)
GLTexture::render_sub_texture(tex_id, internal_left, internal_right, bottom, internal_bottom, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, internal_left, internal_right, bottom, internal_bottom, { { internal_left_uv, bottom_uv }, { internal_right_uv, bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_left_uv, internal_bottom_uv } });
// bottom-right corner
if (m_layout.horizontal_orientation == Layout::HO_Right || m_layout.vertical_orientation == Layout::VO_Bottom)
GLTexture::render_sub_texture(tex_id, internal_right, right, bottom, internal_bottom, { { internal_left_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv }, { internal_right_uv, internal_top_uv }, { internal_left_uv, internal_top_uv } });
else
GLTexture::render_sub_texture(tex_id, internal_right, right, bottom, internal_bottom, { { internal_right_uv, bottom_uv }, { right_uv, bottom_uv }, { right_uv, internal_bottom_uv }, { internal_right_uv, internal_bottom_uv } });
}
}
void GLToolbar::render_arrow(const GLCanvas3D& parent, GLToolbarItem* highlighted_item)
{
// arrow texture not initialized
if (m_arrow_texture.get_id() == 0)
return;
float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom();
float factor = inv_zoom * m_layout.scale;
float scaled_icons_size = m_layout.icons_size * factor;
float scaled_separator_size = m_layout.separator_size * factor;
float scaled_gap_size = m_layout.gap_size * factor;
float border = m_layout.border * factor;
float separator_stride = scaled_separator_size + scaled_gap_size;
float icon_stride = scaled_icons_size + scaled_gap_size;
float left = m_layout.left;
float top = m_layout.top - icon_stride;
bool found = false;
for (const GLToolbarItem* item : m_items) {
if (!item->is_visible())
continue;
if (item->is_separator())
left += separator_stride;
else {
if (item->get_name() == highlighted_item->get_name()) {
found = true;
break;
}
left += icon_stride;
}
}
if (!found)
return;
left += border;
top -= separator_stride;
float right = left + scaled_icons_size;
const unsigned int tex_id = m_arrow_texture.get_id();
// arrow width and height
const float arr_tex_width = (float)m_arrow_texture.get_width();
const float arr_tex_height = (float)m_arrow_texture.get_height();
if (tex_id != 0 && arr_tex_width > 0.0f && arr_tex_height > 0.0f) {
float internal_left = left + border - scaled_icons_size * 1.5f; // add scaled_icons_size for huge arrow
float internal_right = right - border + scaled_icons_size * 1.5f;
float internal_top = top - border;
// bottom is not moving and should be calculated from arrow texture sides ratio
float arrow_sides_ratio = (float)m_arrow_texture.get_height() / (float)m_arrow_texture.get_width();
float internal_bottom = internal_top - (internal_right - internal_left) * arrow_sides_ratio ;
const float left_uv = 0.0f;
const float right_uv = 1.0f;
const float top_uv = 1.0f;
const float bottom_uv = 0.0f;
GLTexture::render_sub_texture(tex_id, internal_left, internal_right, internal_bottom, internal_top, { { left_uv, top_uv }, { right_uv, top_uv }, { right_uv, bottom_uv }, { left_uv, bottom_uv } });
}
}
void GLToolbar::render_horizontal(const GLCanvas3D& parent, GLToolbarItem::EType type, bool draw_icons)
{
const Size cnv_size = parent.get_canvas_size();
const float cnv_w = (float)cnv_size.get_width();
const float cnv_h = (float)cnv_size.get_height();
if (cnv_w == 0 || cnv_h == 0)
return;
const float inv_cnv_w = 1.0f / cnv_w;
const float inv_cnv_h = 1.0f / cnv_h;
const float icons_size_x = 2.0f * m_layout.icons_size * m_layout.scale * inv_cnv_w;
const float icons_size_y = 2.0f * m_layout.icons_size * m_layout.scale * inv_cnv_h;
const float separator_size = 2.0f * m_layout.separator_size * m_layout.scale * inv_cnv_w;
const float gap_size = 2.0f * m_layout.gap_size * m_layout.scale * inv_cnv_w;
const float border_w = 2.0f * m_layout.border * m_layout.scale * inv_cnv_w;
const float border_h = 2.0f * m_layout.border * m_layout.scale * inv_cnv_h;
const float corner_w = 2.0f * m_layout.corner_radius * m_layout.scale * inv_cnv_w;
const float corner_h = 2.0f * m_layout.corner_radius * m_layout.scale * inv_cnv_h;
const float width = 2.0f * get_width() * inv_cnv_w;
const float height = 2.0f * get_height() * inv_cnv_h;
const float separator_stride = separator_size + gap_size;
const float icon_stride = icons_size_x + gap_size;
float left = 2.0f * m_layout.left * inv_cnv_w;
float top = 2.0f * m_layout.top * inv_cnv_h;
float right = left + width;
if (type == GLToolbarItem::SeparatorLine)
right = left + width * 0.5;
const float bottom = top - height;
if (draw_icons)
render_background(left, top, right, bottom, corner_w, corner_h);
left += border_w;
top -= border_h;
// renders icons
for (const GLToolbarItem* item : m_items) {
if (!item->is_visible())
continue;
if (item->is_separator())
left += separator_stride;
else {
//BBS GUI refactor
item->render_left_pos = left;
if (!draw_icons)
item->render_window(left, left + icons_size_x, top - icons_size_y, top);
else if (!item->is_action_with_text_image()) {
unsigned int tex_id = m_icons_texture.get_id();
int tex_width = m_icons_texture.get_width();
int tex_height = m_icons_texture.get_height();
if ((tex_id == 0) || (tex_width <= 0) || (tex_height <= 0))
return;
item->render(tex_id, left, left + icons_size_x, top - icons_size_y, top, (unsigned int)tex_width, (unsigned int)tex_height, (unsigned int)(m_layout.icons_size * m_layout.scale));
}
//BBS: GUI refactor: GLToolbar
if (item->is_action_with_text())
{
float scaled_text_size = item->get_extra_size_ratio() * icons_size_x;
if (draw_icons)
item->render_text(left + icons_size_x, left + icons_size_x + scaled_text_size, top - icons_size_y, top);
left += scaled_text_size;
}
left += icon_stride;
}
}
}
void GLToolbar::render_vertical(const GLCanvas3D& parent, bool draw_icons)
{
const Size cnv_size = parent.get_canvas_size();
const float cnv_w = (float)cnv_size.get_width();
const float cnv_h = (float)cnv_size.get_height();
if (cnv_w == 0 || cnv_h == 0)
return;
const float inv_cnv_w = 1.0f / cnv_w;
const float inv_cnv_h = 1.0f / cnv_h;
const float icons_size_x = 2.0f * m_layout.icons_size * m_layout.scale * inv_cnv_w;
const float icons_size_y = 2.0f * m_layout.icons_size * m_layout.scale * inv_cnv_h;
const float separator_size = 2.0f * m_layout.separator_size * m_layout.scale * inv_cnv_h;
const float gap_size = 2.0f * m_layout.gap_size * m_layout.scale * inv_cnv_h;
const float border_w = 2.0f * m_layout.border * m_layout.scale * inv_cnv_w;
const float border_h = 2.0f * m_layout.border * m_layout.scale * inv_cnv_h;
const float corner_w = 2.0f * m_layout.corner_radius * m_layout.scale * inv_cnv_w;
const float corner_h = 2.0f * m_layout.corner_radius * m_layout.scale * inv_cnv_h;
const float width = 2.0f * get_width() * inv_cnv_w;
const float height = 2.0f * get_height() * inv_cnv_h;
const float separator_stride = separator_size + gap_size;
const float icon_stride = icons_size_y + gap_size;
float left = 2.0f * m_layout.left * inv_cnv_w;
float top = 2.0f * m_layout.top * inv_cnv_h;
const float right = left + width;
const float bottom = top - height;
if (draw_icons)
render_background(left, top, right, bottom, corner_w, corner_h);
left += border_w;
top -= border_h;
// renders icons
for (const GLToolbarItem* item : m_items) {
if (!item->is_visible())
continue;
if (item->is_separator())
top -= separator_stride;
else {
if (!draw_icons) {
item->render_window(left, left + icons_size_x, top - icons_size_y, top);
top -= icon_stride;
continue;
}
unsigned int tex_id;
int tex_width, tex_height;
if (item->is_action_with_text_image()) {
float scaled_text_size = m_layout.text_size * m_layout.scale * inv_cnv_w;
float scaled_text_width = item->get_extra_size_ratio() * icons_size_x;
float scaled_text_border = 2.5 * m_layout.scale * inv_cnv_h;
float scaled_text_height = icons_size_y / 2.0f;
item->render_text(left, left + scaled_text_size, top - scaled_text_border - scaled_text_height, top - scaled_text_border);
float image_left = left + scaled_text_size;
tex_id = item->m_data.image_texture.get_id();
tex_width = item->m_data.image_texture.get_width();
tex_height = item->m_data.image_texture.get_height();
if ((tex_id == 0) || (tex_width <= 0) || (tex_height <= 0))
return;
item->render_image(tex_id, image_left, image_left + icons_size_x, top - icons_size_y, top, (unsigned int)tex_width, (unsigned int)tex_height, (unsigned int)(m_layout.icons_size * m_layout.scale));
}
else {
tex_id = m_icons_texture.get_id();
tex_width = m_icons_texture.get_width();
tex_height = m_icons_texture.get_height();
if ((tex_id == 0) || (tex_width <= 0) || (tex_height <= 0))
return;
item->render(tex_id, left, left + icons_size_x, top - icons_size_y, top, (unsigned int)tex_width, (unsigned int)tex_height, (unsigned int)(m_layout.icons_size * m_layout.scale));
//BBS: GUI refactor: GLToolbar
}
if (item->is_action_with_text())
{
float scaled_text_width = item->get_extra_size_ratio() * icons_size_x;
float scaled_text_height = icons_size_y;
item->render_text(left + icons_size_x, left + icons_size_x + scaled_text_width, top - scaled_text_height, top);
}
top -= icon_stride;
}
}
}
bool GLToolbar::generate_icons_texture()
{
std::string path = resources_dir() + "/images/";
std::vector<std::string> filenames;
for (GLToolbarItem* item : m_items) {
const std::string& icon_filename = item->get_icon_filename();
if (!icon_filename.empty())
filenames.push_back(path + icon_filename);
}
std::vector<std::pair<int, bool>> states;
//1: white only, 2: gray only, 0 : normal
//true/false: apply background or not
if (m_type == Normal) {
states.push_back({ 1, false }); // Normal
states.push_back({ 0, false }); // Pressed
states.push_back({ 2, false }); // Disabled
states.push_back({ 0, false }); // Hover
states.push_back({ 0, false }); // HoverPressed
states.push_back({ 2, false }); // HoverDisabled
states.push_back({ 0, false }); // HighlightedShown
states.push_back({ 2, false }); // HighlightedHidden
}
else {
states.push_back({ 1, false }); // Normal
states.push_back({ 0, true }); // Pressed
states.push_back({ 2, false }); // Disabled
states.push_back({ 0, false }); // Hover
states.push_back({ 1, true }); // HoverPressed
states.push_back({ 1, false }); // HoverDisabled
states.push_back({ 0, false }); // HighlightedShown
states.push_back({ 1, false }); // HighlightedHidden
}
unsigned int sprite_size_px = (unsigned int)(m_layout.icons_size * m_layout.scale);
// // force even size
// if (sprite_size_px % 2 != 0)
// sprite_size_px += 1;
bool res = m_icons_texture.load_from_svg_files_as_sprites_array(filenames, states, sprite_size_px, false);
if (res)
m_icons_texture_dirty = false;
return res;
}
bool GLToolbar::update_items_visibility()
{
bool ret = false;
for (GLToolbarItem* item : m_items) {
ret |= item->update_visibility();
}
ret |= fold_groups();
if (ret)
m_layout.dirty = true;
// updates separators visibility to avoid having two of them consecutive
bool any_item_visible = false;
for (GLToolbarItem* item : m_items) {
if (!item->is_separator())
any_item_visible |= item->is_visible();
else {
item->set_visible(any_item_visible);
any_item_visible = false;
}
}
return ret;
}
bool GLToolbar::update_items_enabled_state()
{
bool ret = false;
for (int i = 0; i < (int)m_items.size(); ++i)
{
GLToolbarItem* item = m_items[i];
ret |= item->update_enabled_state();
if (item->is_enabled() && (m_pressed_toggable_id != -1) && (m_pressed_toggable_id != i))
{
ret = true;
item->set_state(GLToolbarItem::Disabled);
}
}
if (ret)
m_layout.dirty = true;
return ret;
}
//BBS: GUI refactor: GLToolbar
int GLToolbar::generate_button_text_textures(wxFont& font)
{
int ret = 0;
for (int i = 0; i < (int)m_items.size(); ++i)
{
GLToolbarItem* item = m_items[i];
if (item->is_action_with_text())
{
ret |= item->generate_texture(font);
}
if (item->is_action_with_text_image())
{
ret |= item->generate_texture(font);
}
}
return ret;
}
int GLToolbar::generate_image_textures()
{
int ret = 0;
for (int i = 0; i < (int)m_items.size(); ++i)
{
GLToolbarItem* item = m_items[i];
if (item->is_action_with_text_image()) {
ret |= item->generate_image_texture();
}
}
return ret;
}
//BBS: GUI refactor: GLToolbar
float GLToolbar::get_scaled_icon_size()
{
return m_layout.icons_size * m_layout.scale;
}
bool setup_collapse_toolbar(GLToolbar& toolbar, std::function<void()> toggle)
{
toolbar.set_layout_type(GLToolbar::Layout::Vertical);
toolbar.set_horizontal_orientation(GLToolbar::Layout::HO_Right);
toolbar.set_vertical_orientation(GLToolbar::Layout::VO_Top);
toolbar.set_border(4.0f);
toolbar.set_corner_radius(GLToolbar::Top_Corner_Radius);
toolbar.set_separator_size(4);
toolbar.set_gap_size(2);
toolbar.del_all_item();
GLToolbarItem::Data item;
item.name = "collapse_sidebar";
// set collapse svg name
item.icon_filename = "collapse.svg";
item.sprite_id = 0;
item.left.action_callback = std::move(toggle);
return toolbar.add_item(item);
}
void GLToolbar::apply_layout(const ToolbarGroupLayout& layout)
{
const auto id_of = [](const GLToolbarItem* item) { return item->is_separator() ? std::string(TOOLBAR_SEPARATOR_ID) : item->get_name(); };
const auto rank = [&](const GLToolbarItem* item) { return std::find(layout.order.begin(), layout.order.end(), id_of(item)) - layout.order.begin(); };
std::stable_sort(m_items.begin(), m_items.end(), [&](const GLToolbarItem* a, const GLToolbarItem* b) { return rank(a) < rank(b); });
// The icon texture has one row per item in m_items order.
unsigned int sprite_id = 0;
for (auto it = m_items.begin(); it != m_items.end();) {
GLToolbarItem* item = *it;
const bool hidden = layout.hidden.count(id_of(item)) != 0;
if (item->is_separator()) {
// The toolbar rewrites a separator's visibility every frame, so a hidden one is removed instead.
if (hidden) {
delete item;
it = m_items.erase(it);
continue;
}
} else {
item->m_data.sprite_id = sprite_id++;
// A hidden item stays, only invisible, so lookups by name keep finding it.
if (hidden)
item->m_data.visibility_callback = [] { return false; };
}
++it;
}
m_layout.dirty = true;
}
void GLToolbar::set_groups(const std::vector<ToolbarIconGroup>& groups)
{
close_group_list();
m_groups = groups;
if (fold_groups())
m_layout.dirty = true;
}
int GLToolbar::group_of_slot(int item_id) const
{
if (item_id < 0 || static_cast<size_t>(item_id) >= m_items.size())
return -1;
const std::string& name = m_items[static_cast<size_t>(item_id)]->get_name();
for (const ToolbarBarSlot& slot : m_group_slots)
if (slot.shown == name)
return slot.group;
return -1;
}
bool GLToolbar::fold_groups()
{
// Runs every idle. Without groups there is nothing to fold once the last slots are cleared.
if (m_groups.empty() && m_group_slots.empty())
return false;
std::vector<std::string> bar_ids;
std::set<std::string> cannot_open;
std::string pressed;
for (const GLToolbarItem* item : m_items) {
if (item->is_separator() || !item->m_data.visible)
continue;
bar_ids.push_back(item->get_name());
if (item->is_disabled())
cannot_open.insert(item->get_name());
if (item->is_pressed())
pressed = item->get_name();
}
m_group_slots.clear();
std::set<std::string> folded;
for (ToolbarBarSlot& slot : compute_bar_slots(bar_ids, m_groups, pressed, m_recent_items, cannot_open))
if (slot.group >= 0) {
for (const std::string& member : slot.members)
if (member != slot.shown)
folded.insert(member);
m_group_slots.push_back(std::move(slot));
}
// A list whose group no longer has a slot (a printer switch left it one item on the toolbar) closes.
if (std::none_of(m_group_slots.begin(), m_group_slots.end(), [this](const ToolbarBarSlot& slot) { return slot.group == m_group_popup.open; }))
close_group_list();
bool changed = false;
for (GLToolbarItem* item : m_items) {
const bool fold = folded.count(item->get_name()) != 0;
changed |= item->m_folded != fold;
item->m_folded = fold;
}
return changed;
}
void GLToolbar::open_group_list(int group, GLCanvas3D& parent)
{
close_group_list();
// A pressed item's window sits where the list goes, so close it first.
if (m_pressed_toggable_id >= 0)
do_action(GLToolbarItem::Left, m_pressed_toggable_id, parent, false);
if (m_pressed_toggable_id >= 0)
return;
m_group_popup.open = group;
parent.set_as_dirty();
parent.request_extra_frame();
}
bool GLToolbar::close_group_list()
{
m_group_picked.clear();
return m_group_popup.close();
}
void GLToolbar::close_group_list_unless_at(const Vec2d& mouse_pos, GLCanvas3D& parent)
{
// A press on its own button toggles the list in on_mouse().
if (m_group_popup.closes_at(mouse_pos, group_of_slot(contains_mouse(mouse_pos, parent)))) {
close_group_list();
parent.set_as_dirty();
parent.request_extra_frame();
}
}
void GLToolbar::render_group_marks(const GLCanvas3D& parent)
{
if (m_group_slots.empty())
return;
const float cnv_w = static_cast<float>(parent.get_canvas_size().get_width());
const float cnv_h = static_cast<float>(parent.get_canvas_size().get_height());
const float icon = m_layout.icons_size * m_layout.scale;
const float border = m_layout.border * m_layout.scale;
const float top = 0.5f * cnv_h - m_layout.top;
for (const ToolbarBarSlot& slot : m_group_slots) {
const GLToolbarItem* item = get_item(slot.shown);
if (item == nullptr || !item->is_visible())
continue;
const float left = 0.5f * (1.0f + item->render_left_pos) * cnv_w;
render_group_dots(left, top + border + icon + 0.5f * border, icon, m_group_popup.open == slot.group, wxGetApp().dark_mode());
if (m_group_popup.open == slot.group)
render_group_list(slot.group, left, top + get_height_horizontal(), parent);
}
}
void GLToolbar::render_group_list(int group, float left, float top, const GLCanvas3D& parent)
{
std::vector<const GLToolbarItem*> members;
std::vector<GroupPopupIcon> icons;
for (const ToolbarBarSlot& slot : m_group_slots)
if (slot.group == group)
for (const std::string& name : slot.members)
if (const GLToolbarItem* item = get_item(name); item != nullptr) {
members.push_back(item);
icons.push_back({item->get_sprite_id(), !item->is_disabled(), item->get_tooltip()});
}
// The sprite layout GLToolbarItem::render() reads: one row per item, one column per state, tiles 1 px apart.
GroupPopupSprites sprites{m_icons_texture.get_id(), m_icons_texture.get_width(), m_icons_texture.get_height()};
if (sprites.tex_width > 0 && sprites.tex_height > 0) {
const float icon_px = static_cast<float>(sprites.tex_width - 1) /
static_cast<float>(GLToolbarItem::Num_States + GLToolbarItem::Num_Rendered_Highlight_States);
sprites.du = icon_px / static_cast<float>(sprites.tex_width);
sprites.dv = icon_px / static_cast<float>(sprites.tex_height);
}
sprites.normal_column = GLToolbarItem::Normal;
sprites.hover_column = GLToolbarItem::Hover;
sprites.disabled_column = GLToolbarItem::Disabled;
// The canvas's scale, as the gizmo bar's list takes: m_layout.scale is not the display's.
const int picked = render_group_popup("##toolbar_group_list", m_groups[static_cast<size_t>(group)].name, icons, sprites,
m_layout.icons_size * m_layout.scale, parent.get_scale(), 6.0f * m_layout.scale, left, top,
static_cast<float>(parent.get_canvas_size().get_width()), m_group_popup);
// A pick closes the list; the caller runs the item's action after the frame.
if (picked >= 0) {
m_group_picked = members[static_cast<size_t>(picked)]->get_name();
m_group_popup.close();
}
}
void render_group_dots(float icon_left, float y, float icon_size, bool open, bool dark)
{
const float r = std::max(1.5f, 0.04f * icon_size);
const ImU32 colour = open ? IM_COL32(0, 150, 136, 255) : dark ? IM_COL32(220, 220, 220, 255) : IM_COL32(80, 80, 80, 255);
for (int dot = -1; dot <= 1; ++dot)
ImGui::GetBackgroundDrawList()->AddCircleFilled(ImVec2(icon_left + 0.5f * icon_size + static_cast<float>(dot) * 3.0f * r, y), r,
colour);
}
int render_group_popup(const char* window_id, const std::string& name, const std::vector<GroupPopupIcon>& icons,
const GroupPopupSprites& sprites, float icon_size, float style_scale, float rounding, float left, float top,
float canvas_width, GroupPopupState& state)
{
if (icons.empty() || sprites.tex_id == 0 || sprites.tex_width <= 1 || sprites.tex_height <= 1)
return -1;
const float u_offset = 1.0f / static_cast<float>(sprites.tex_width);
const float v_offset = 1.0f / static_cast<float>(sprites.tex_height);
const float last_width = state.rect(2) - state.rect(0);
if (last_width > 0.0f && left + last_width > canvas_width)
left = std::max(0.0f, canvas_width - last_width);
ImGuiWrapper& imgui = *wxGetApp().imgui();
ImGuiWrapper::push_toolbar_style(style_scale);
imgui.set_next_window_pos(left, top, ImGuiCond_Always, 0.0f, 0.0f);
// An accent border, as on the open group's dots.
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, rounding);
ImGui::PushStyleColor(ImGuiCol_Border, ImGuiWrapper::COL_ORCA);
// A fixed id: the group's name is user text and may repeat or contain "##".
imgui.begin(std::string(window_id), ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);
// The window took its border when it began; the icons' tooltips keep the usual one.
ImGui::PopStyleColor();
ImGui::PopStyleVar(2);
ImDrawList* draw = ImGui::GetWindowDrawList();
const auto tex = reinterpret_cast<ImTextureID>(static_cast<intptr_t>(sprites.tex_id));
ImGui::TextUnformatted(name.c_str());
// Bar-sized icons, so they stay pixel-exact, in a grid that widens with the group: 3x3, then 4x4, then five to a row.
const size_t columns = icons.size() > 16 ? 5 : icons.size() > 9 ? 4 : 3;
int clicked = -1;
for (size_t i = 0; i < icons.size(); ++i) {
const GroupPopupIcon& icon = icons[i];
if (i % columns != 0)
ImGui::SameLine();
ImGui::PushID(static_cast<int>(i));
const ImVec2 at = ImGui::GetCursorScreenPos();
if (ImGui::InvisibleButton("##icon", ImVec2(icon_size, icon_size)) && icon.enabled)
clicked = static_cast<int>(i);
const bool hovered = ImGui::IsItemHovered();
ImGui::PopID();
const int column = icon.enabled ? (hovered ? sprites.hover_column : sprites.normal_column) : sprites.disabled_column;
const float u_left = u_offset + static_cast<float>(column) * sprites.du;
const float v_top = v_offset + static_cast<float>(icon.sprite_row) * sprites.dv;
draw->AddImage(tex, at, ImVec2(at.x + icon_size, at.y + icon_size), ImVec2(u_left, v_top),
ImVec2(u_left + sprites.du - u_offset, v_top + sprites.dv - v_offset));
if (hovered && !icon.tooltip.empty())
imgui.tooltip(icon.tooltip, ImGui::GetFontSize() * 20.0f);
}
const ImVec2 pos = ImGui::GetWindowPos();
const ImVec2 sz = ImGui::GetWindowSize();
state.rect = Vec4f(pos.x, pos.y, pos.x + sz.x, pos.y + sz.y);
imgui.end();
ImGuiWrapper::pop_toolbar_style();
return clicked;
}
} // namespace GUI
} // namespace Slic3r