diff --git a/src/Bookmarks/BookmarkManager.cpp b/src/Bookmarks/BookmarkManager.cpp index 6432774..2549676 100644 --- a/src/Bookmarks/BookmarkManager.cpp +++ b/src/Bookmarks/BookmarkManager.cpp @@ -21,7 +21,7 @@ void BookmarkManager::refresh_gui_data() { nameForNew.clear(); } -void BookmarkManager::setup_list_gui(const std::string& id) { +void BookmarkManager::setup_list_gui(const char* id) { using namespace NetworkingObjects; if(bookmarkListRoot) { auto& gui = world.main.toolbar.gui; diff --git a/src/Bookmarks/BookmarkManager.hpp b/src/Bookmarks/BookmarkManager.hpp index 15f1481..f41b2ec 100644 --- a/src/Bookmarks/BookmarkManager.hpp +++ b/src/Bookmarks/BookmarkManager.hpp @@ -13,7 +13,7 @@ class BookmarkManager { BookmarkManager(World& w); void server_init_no_file(); void scale_up(const WorldScalar& scaleUpAmount); - void setup_list_gui(const std::string& id); + void setup_list_gui(const char* id); void refresh_gui_data(); void save_file(cereal::PortableBinaryOutputArchive& a) const; void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version); diff --git a/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.cpp b/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.cpp index af6b74a..868aa8b 100644 --- a/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.cpp +++ b/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.cpp @@ -22,7 +22,7 @@ void DrawingProgramLayerManagerGUI::refresh_gui_data() { blendModeValToEdit = 0; } -void DrawingProgramLayerManagerGUI::setup_list_gui(const std::string& id, bool& hoveringOverDropdown) { +void DrawingProgramLayerManagerGUI::setup_list_gui(const char* id, bool& hoveringOverDropdown) { using namespace NetworkingObjects; auto& world = layerMan.drawP.world; if(layerMan.layerTreeRoot) { diff --git a/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.hpp b/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.hpp index 2512558..c3ae558 100644 --- a/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.hpp +++ b/src/DrawingProgram/Layers/DrawingProgramLayerManagerGUI.hpp @@ -12,7 +12,7 @@ class DrawingProgramLayerManagerGUI { public: DrawingProgramLayerManagerGUI(DrawingProgramLayerManager& layerManager); void refresh_gui_data(); - void setup_list_gui(const std::string& id, bool& hoveringOverDropdown); + void setup_list_gui(const char* id, bool& hoveringOverDropdown); private: std::optional>> try_to_create_in_proper_position(DrawingProgramLayerListItem* newItem); std::pair> create_in_proper_position(DrawingProgramLayerListItem* newItem); diff --git a/src/GUIStuff/Elements/Element.hpp b/src/GUIStuff/Elements/Element.hpp index 5e3c222..f30fcfb 100644 --- a/src/GUIStuff/Elements/Element.hpp +++ b/src/GUIStuff/Elements/Element.hpp @@ -21,27 +21,23 @@ namespace GUIStuff { // Taken from https://github.com/TimothyHoytBSME/ClayMan (rewritten to be a separate struct, and use templates to change size) template class StringArena { public: - StringArena() { - stringArena = std::vector(S); - } void reset() { stringArenaIndex = 0; } - const char* insert_string_into_arena(const std::string& str) { + const char* insert_string_into_arena(std::string_view str) { size_t strSize = str.size(); - if(stringArenaIndex + strSize + 1 > S) + if(stringArenaIndex + strSize > S) throw std::overflow_error("[StringArena::insert_string_into_arena] Not enough space to insert string"); char* startPtr = &stringArena[stringArenaIndex]; - for(size_t i = 0; i < strSize; i++) - stringArena[stringArenaIndex++] = str[i]; - stringArena[stringArenaIndex++] = ' '; + std::copy(str.begin(), str.end(), startPtr); + stringArenaIndex += strSize; return startPtr; } - Clay_String std_str_to_clay_str(const std::string& str) { + Clay_String std_str_to_clay_str(std::string_view str) { return Clay_String{.length = static_cast(str.size()), .chars = insert_string_into_arena(str)}; } private: - std::vector stringArena; + std::array stringArena; size_t stringArenaIndex; }; diff --git a/src/GUIStuff/GUIManager.cpp b/src/GUIStuff/GUIManager.cpp index 54c875b..ca2b200 100644 --- a/src/GUIStuff/GUIManager.cpp +++ b/src/GUIStuff/GUIManager.cpp @@ -291,23 +291,23 @@ void GUIManager::left_to_right_line_centered_layout(const std::function& } } -void GUIManager::text_label_size(const std::string& val, float modifier) { +void GUIManager::text_label_size(std::string_view val, float modifier) { CLAY_TEXT(strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4(io->theme->frontColor1), .fontSize = static_cast(io->fontSize * modifier)})); } -void GUIManager::text_label_color(const std::string& val, const SkColor4f& color) { +void GUIManager::text_label_color(std::string_view val, const SkColor4f& color) { CLAY_TEXT(strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4(color), .fontSize = io->fontSize })); } -void GUIManager::text_label_light(const std::string& val) { +void GUIManager::text_label_light(std::string_view val) { CLAY_TEXT(strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4(io->theme->frontColor2), .fontSize = io->fontSize })); } -void GUIManager::text_label(const std::string& val) { +void GUIManager::text_label(std::string_view val) { CLAY_TEXT(strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4(io->theme->frontColor1), .fontSize = io->fontSize })); } -void GUIManager::text_label_centered(const std::string& val) { +void GUIManager::text_label_centered(std::string_view val) { CLAY_AUTO_ID({ .layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) }, @@ -318,7 +318,7 @@ void GUIManager::text_label_centered(const std::string& val) { } } -void GUIManager::text_label_light_centered(const std::string& val) { +void GUIManager::text_label_light_centered(std::string_view val) { CLAY_AUTO_ID({ .layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) }, @@ -329,7 +329,7 @@ void GUIManager::text_label_light_centered(const std::string& val) { } } -bool GUIManager::radio_button(const std::string& id, bool val, const std::function& elemUpdate) { +bool GUIManager::radio_button(const char* id, bool val, const std::function& elemUpdate) { push_id(id); RadioButton* e = insert_element(); e->update(*io, val, elemUpdate); @@ -337,7 +337,7 @@ bool GUIManager::radio_button(const std::string& id, bool val, const std::functi return e->selection.clicked; } -void GUIManager::checkbox(const std::string& id, bool* val, const std::function& elemUpdate) { +void GUIManager::checkbox(const char* id, bool* val, const std::function& elemUpdate) { push_id(id); CheckBox* e = insert_element(); e->update(*io, *val, elemUpdate); @@ -346,7 +346,7 @@ void GUIManager::checkbox(const std::string& id, bool* val, const std::function< pop_id(); } -bool GUIManager::input_text_field(const std::string& id, const std::string& name, std::string* val, bool updateEveryEdit, const std::function& elemUpdate) { +bool GUIManager::input_text_field(const char* id, std::string_view name, std::string* val, bool updateEveryEdit, const std::function& elemUpdate) { bool toRet = false; left_to_right_line_layout([&]() { text_label(name); @@ -355,21 +355,21 @@ bool GUIManager::input_text_field(const std::string& id, const std::string& name return toRet; } -void GUIManager::input_path_field(const std::string& id, const std::string& name, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate) { +void GUIManager::input_path_field(const char* id, std::string_view name, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate) { left_to_right_line_layout([&]() { text_label(name); input_path(id, val, fileTypeRestriction, elemUpdate); }); } -void GUIManager::checkbox_field(const std::string& id, const std::string& name, bool* val, const std::function& elemUpdate) { +void GUIManager::checkbox_field(const char* id, std::string_view name, bool* val, const std::function& elemUpdate) { left_to_right_line_layout([&]() { checkbox(id, val, elemUpdate); text_label(name); }); } -bool GUIManager::radio_button_field(const std::string& id, const std::string& name, bool val, const std::function& elemUpdate) { +bool GUIManager::radio_button_field(const char* id, std::string_view name, bool val, const std::function& elemUpdate) { bool toRet; left_to_right_line_layout([&]() { toRet = radio_button(id, val, elemUpdate); @@ -378,10 +378,10 @@ bool GUIManager::radio_button_field(const std::string& id, const std::string& na return toRet; } -bool GUIManager::input_color_component_255(const std::string& id, float* val, const std::function& elemUpdate) { +bool GUIManager::input_color_component_255(const char* id, float* val, const std::function& elemUpdate) { push_id(id); bool isUpdating = insert_element>()->update(*io, val, - [&](const std::string& str) { + [&](std::string_view str) { int roundTo255; std::stringstream ss; ss << str; @@ -398,7 +398,7 @@ bool GUIManager::input_color_component_255(const std::string& id, float* val, co return isUpdating; } -bool GUIManager::input_text(const std::string& id, std::string* val, bool updateEveryEdit, const std::function& elemUpdate) { +bool GUIManager::input_text(const char* id, std::string* val, bool updateEveryEdit, const std::function& elemUpdate) { push_id(id); bool toRet = insert_element>()->update(*io, val, [&](const std::string& str) { @@ -411,7 +411,7 @@ bool GUIManager::input_text(const std::string& id, std::string* val, bool update return toRet; } -bool GUIManager::selectable_button(const std::string& id, const std::function& elemUpdate, GUIStuff::SelectableButton::DrawType drawType, bool isSelected) { +bool GUIManager::selectable_button(const char* id, const std::function& elemUpdate, GUIStuff::SelectableButton::DrawType drawType, bool isSelected) { push_id(id); SelectableButton* e = insert_element(); e->update(*io, drawType, elemUpdate, isSelected); @@ -420,13 +420,13 @@ bool GUIManager::selectable_button(const std::string& id, const std::function& elemUpdate) { +void GUIManager::svg_icon(const char* id, const std::string& svgPath, bool isHighlighted, const std::function& elemUpdate) { push_id(id); insert_element()->update(*io, svgPath, isHighlighted, elemUpdate); pop_id(); } -bool GUIManager::svg_icon_button(const std::string& id, const std::string& svgPath, bool isSelected, float size, bool hasBorder, const std::function& elemUpdate) { +bool GUIManager::svg_icon_button(const char* id, const std::string& svgPath, bool isSelected, float size, bool hasBorder, const std::function& elemUpdate) { bool toRet = false; push_id(id); CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(size), .height = CLAY_SIZING_FIXED(size) } } }) { @@ -440,7 +440,7 @@ bool GUIManager::svg_icon_button(const std::string& id, const std::string& svgPa return toRet; } -bool GUIManager::svg_icon_button_transparent(const std::string& id, const std::string& svgPath, bool isSelected, float size, bool hasBorder, const std::function& elemUpdate) { +bool GUIManager::svg_icon_button_transparent(const char* id, const std::string& svgPath, bool isSelected, float size, bool hasBorder, const std::function& elemUpdate) { bool toRet = false; push_id(id); CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(size), .height = CLAY_SIZING_FIXED(size) } } }) { @@ -454,7 +454,7 @@ bool GUIManager::svg_icon_button_transparent(const std::string& id, const std::s return toRet; } -bool GUIManager::text_button_sized(const std::string& id, const std::string& text, Clay_SizingAxis x, Clay_SizingAxis y, bool isSelected, const std::function& elemUpdate) { +bool GUIManager::text_button_sized(const char* id, std::string_view text, Clay_SizingAxis x, Clay_SizingAxis y, bool isSelected, const std::function& elemUpdate) { bool toRet = false; CLAY_AUTO_ID({.layout = {.sizing = {.width = x, .height = y } } }) { toRet = selectable_button(id, [&](SelectionHelper& s, bool iS) { @@ -466,7 +466,7 @@ bool GUIManager::text_button_sized(const std::string& id, const std::string& tex return toRet; } -bool GUIManager::text_button(const std::string& id, const std::string& text, bool isSelected, const std::function& elemUpdate) { +bool GUIManager::text_button(const char* id, std::string_view text, bool isSelected, const std::function& elemUpdate) { bool toRet = false; CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIT(0), .height = CLAY_SIZING_FIT(0) } } }) { toRet = selectable_button(id, [&](SelectionHelper& s, bool iS) { @@ -478,7 +478,7 @@ bool GUIManager::text_button(const std::string& id, const std::string& text, boo return toRet; } -bool GUIManager::text_button_wide(const std::string& id, const std::string& text, bool isSelected, const std::function& elemUpdate) { +bool GUIManager::text_button_wide(const char* id, std::string_view text, bool isSelected, const std::function& elemUpdate) { bool toRet = false; CLAY_AUTO_ID({ .layout = { @@ -494,7 +494,7 @@ bool GUIManager::text_button_wide(const std::string& id, const std::string& text return toRet; } -bool GUIManager::text_button_left_transparent(const std::string& id, const std::string& text, bool isSelected, const std::function& elemUpdate) { +bool GUIManager::text_button_left_transparent(const char* id, std::string_view text, bool isSelected, const std::function& elemUpdate) { bool toRet = false; CLAY_AUTO_ID({ .layout = { @@ -516,11 +516,11 @@ bool GUIManager::text_button_left_transparent(const std::string& id, const std:: return toRet; } -bool GUIManager::input_scalar(const std::string& id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function& elemUpdate) { +bool GUIManager::input_scalar(const char* id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function& elemUpdate) { bool isUpdating = false; push_id(id); isUpdating = insert_element>()->update(*io, val, - [&](const std::string& a) { + [&](std::string_view a) { if(a.empty()) return min; uint32_t toRet; @@ -538,7 +538,7 @@ bool GUIManager::input_scalar(const std::string& id, uint8_t* val, uint8_t min, return isUpdating; } -void GUIManager::input_path(const std::string& id, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate) { +void GUIManager::input_path(const char* id, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate) { push_id(id); insert_element>()->update(*io, val, [&](const std::string& a) { @@ -553,20 +553,20 @@ void GUIManager::input_path(const std::string& id, std::filesystem::path* val, s pop_id(); } -void GUIManager::tab_list(const std::string& id, const std::vector>& tabNames, size_t& selectedTab, std::optional& closedTab, const std::function& elemUpdate) { +void GUIManager::tab_list(const char* id, const std::vector>& tabNames, size_t& selectedTab, std::optional& closedTab, const std::function& elemUpdate) { push_id(id); insert_element()->update(*io, tabNames, selectedTab, closedTab, elemUpdate); pop_id(); } -bool GUIManager::font_picker(const std::string& id, std::string* fontName) { +bool GUIManager::font_picker(const char* id, std::string* fontName) { push_id(id); bool toRet = insert_element()->update(*io, fontName, this); pop_id(); return toRet; } -void GUIManager::dropdown_select(const std::string& id, size_t* val, const std::vector& selections, float width, const std::function& hoverboxElemUpdate) { +void GUIManager::dropdown_select(const char* id, size_t* val, const std::vector& selections, float width, const std::function& hoverboxElemUpdate) { push_id(id); struct DropdownData { bool isOpen = false; @@ -669,7 +669,7 @@ void GUIManager::dropdown_select(const std::string& id, size_t* val, const std:: pop_id(); } -void GUIManager::scroll_bar_area(const std::string& id, bool clipHorizontal, const std::function& elemUpdate, bool maxScrollBugWorkaround) { +void GUIManager::scroll_bar_area(const char* id, bool clipHorizontal, const std::function& elemUpdate, bool maxScrollBugWorkaround) { push_id(id); CLAY_AUTO_ID({ .layout = { @@ -721,8 +721,7 @@ void GUIManager::scroll_bar_area(const std::string& id, bool clipHorizontal, con float scrollerPos = std::fabs(scrollData.scrollPosition->y / scrollPosMax); float areaAboveScrollerSize = scrollerPos * (sAreaDim - scrollerSize); - if(Clay_Hovered()) - sD.currentScrollPos += io->mouse.scroll.y() * io->deltaTime * 3000.0f; + sD.currentScrollPos = scrollData.scrollPosition->y; CLAY_AUTO_ID({ .layout = { @@ -781,7 +780,7 @@ void GUIManager::scroll_bar_area(const std::string& id, bool clipHorizontal, con } -void GUIManager::scroll_bar_many_entries_area(const std::string& id, float entryHeight, size_t entryCount, bool clipHorizontal, const std::function& entryUpdate, const std::function& elemUpdate) { +void GUIManager::scroll_bar_many_entries_area(const char* id, float entryHeight, size_t entryCount, bool clipHorizontal, const std::function& entryUpdate, const std::function& elemUpdate) { push_id(id); scroll_bar_area(id, clipHorizontal, [&](float scrollContentHeight, float containerHeight, float& scrollAmount) { if(elemUpdate) @@ -819,7 +818,7 @@ void GUIManager::scroll_bar_many_entries_area(const std::string& id, float entry pop_id(); } -bool GUIManager::rotate_wheel(const std::string& id, double* angle, float size, const std::function& elemUpdate) { +bool GUIManager::rotate_wheel(const char* id, double* angle, float size, const std::function& elemUpdate) { bool toRet = false; push_id(id); CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(size), .height = CLAY_SIZING_FIXED(size) } } }) { @@ -831,14 +830,14 @@ bool GUIManager::rotate_wheel(const std::string& id, double* angle, float size, return toRet; } -void GUIManager::text_paragraph(const std::string& id, std::unique_ptr paragraph, float width, const std::function& elemUpdate) { +void GUIManager::text_paragraph(const char* id, std::unique_ptr paragraph, float width, const std::function& elemUpdate) { push_id(id); TextParagraph* e = insert_element(); e->update(*io, std::move(paragraph), width, elemUpdate); pop_id(); } -void GUIManager::paint_circle_popup_menu(const std::string& id, const Vector2f& centerPos, const PaintCircleMenu::Data& val, const std::function& elemUpdate) { +void GUIManager::paint_circle_popup_menu(const char* id, const Vector2f& centerPos, const PaintCircleMenu::Data& val, const std::function& elemUpdate) { float size = 300.0f; push_id(id); CLAY_AUTO_ID({.layout = { @@ -856,7 +855,7 @@ void GUIManager::paint_circle_popup_menu(const std::string& id, const Vector2f& pop_id(); } -void GUIManager::list_popup_menu(const std::string& id, Vector2f popupPos, const std::function& elemUpdate) { +void GUIManager::list_popup_menu(const char* id, Vector2f popupPos, const std::function& elemUpdate) { CLAY_AUTO_ID({ .layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)} @@ -899,7 +898,7 @@ void GUIManager::list_popup_menu(const std::string& id, Vector2f popupPos, const } } -void GUIManager::tree_listing(const std::string& id, NetworkingObjects::NetObjID rootObjID, const TreeListing::DisplayData& displayData, TreeListing::SelectionData& selectionData) { +void GUIManager::tree_listing(const char* id, NetworkingObjects::NetObjID rootObjID, const TreeListing::DisplayData& displayData, TreeListing::SelectionData& selectionData) { push_id(id); insert_element()->update(*io, *this, rootObjID, displayData, selectionData); pop_id(); @@ -918,7 +917,7 @@ void GUIManager::push_id(int64_t id) { idStack.emplace_back(id); } -void GUIManager::push_id(const std::string& id) { +void GUIManager::push_id(const char* id) { idStack.emplace_back(id); } diff --git a/src/GUIStuff/GUIManager.hpp b/src/GUIStuff/GUIManager.hpp index 3eb4bb8..6b22929 100644 --- a/src/GUIStuff/GUIManager.hpp +++ b/src/GUIStuff/GUIManager.hpp @@ -48,61 +48,61 @@ class GUIManager { Vector2f screen_pos_to_window_pos(const Vector2f& screenPos); Vector2f get_mouse_pos(); - bool input_text_field(const std::string& id, const std::string& name, std::string* val, bool updateEveryEdit = true, const std::function& elemUpdate = nullptr); + bool input_text_field(const char* id, std::string_view name, std::string* val, bool updateEveryEdit = true, const std::function& elemUpdate = nullptr); - void text_paragraph(const std::string& id, std::unique_ptr paragraph, float width, const std::function& elemUpdate = nullptr); - void text_label_color(const std::string& val, const SkColor4f& color); - void text_label_size(const std::string& val, float modifier); - void text_label_light(const std::string& val); - void text_label(const std::string& val); - void text_label_light_centered(const std::string& val); - void text_label_centered(const std::string& val); - bool text_button_left_transparent(const std::string& id, const std::string& text, bool isSelected = false, const std::function& elemUpdate = nullptr); - bool text_button(const std::string& id, const std::string& text, bool isSelected = false, const std::function& elemUpdate = nullptr); - bool text_button_wide(const std::string& id, const std::string& text, bool isSelected = false, const std::function& elemUpdate = nullptr); - bool text_button_sized(const std::string& id, const std::string& text, Clay_SizingAxis x, Clay_SizingAxis y, bool isSelected = false, const std::function& elemUpdate = nullptr); + void text_paragraph(const char* id, std::unique_ptr paragraph, float width, const std::function& elemUpdate = nullptr); + void text_label_color(std::string_view val, const SkColor4f& color); + void text_label_size(std::string_view val, float modifier); + void text_label_light(std::string_view val); + void text_label(std::string_view val); + void text_label_light_centered(std::string_view val); + void text_label_centered(std::string_view val); + bool text_button_left_transparent(const char* id, std::string_view text, bool isSelected = false, const std::function& elemUpdate = nullptr); + bool text_button(const char* id, std::string_view text, bool isSelected = false, const std::function& elemUpdate = nullptr); + bool text_button_wide(const char* id, std::string_view text, bool isSelected = false, const std::function& elemUpdate = nullptr); + bool text_button_sized(const char* id, std::string_view text, Clay_SizingAxis x, Clay_SizingAxis y, bool isSelected = false, const std::function& elemUpdate = nullptr); - void svg_icon(const std::string& id, const std::string& svgPath, bool isHighlighted = false, const std::function& elemUpdate = nullptr); - bool svg_icon_button(const std::string& id, const std::string& svgPath, bool isSelected = false, float size = BIG_BUTTON_SIZE, bool hasBorder = true, const std::function& elemUpdate = nullptr); + void svg_icon(const char* id, const std::string& svgPath, bool isHighlighted = false, const std::function& elemUpdate = nullptr); + bool svg_icon_button(const char* id, const std::string& svgPath, bool isSelected = false, float size = BIG_BUTTON_SIZE, bool hasBorder = true, const std::function& elemUpdate = nullptr); - bool svg_icon_button_transparent(const std::string& id, const std::string& svgPath, bool isSelected = false, float size = BIG_BUTTON_SIZE, bool hasBorder = true, const std::function& elemUpdate = nullptr); + bool svg_icon_button_transparent(const char* id, const std::string& svgPath, bool isSelected = false, float size = BIG_BUTTON_SIZE, bool hasBorder = true, const std::function& elemUpdate = nullptr); - bool rotate_wheel(const std::string& id, double* angle, float size = BIG_BUTTON_SIZE, const std::function& elemUpdate = nullptr); + bool rotate_wheel(const char* id, double* angle, float size = BIG_BUTTON_SIZE, const std::function& elemUpdate = nullptr); void top_to_bottom_window_popup_layout(Clay_SizingAxis x, Clay_SizingAxis y, const std::function& elemUpdate); void left_to_right_layout(Clay_SizingAxis x, Clay_SizingAxis y, const std::function& elemUpdate); void left_to_right_line_layout(const std::function& elemUpdate); void left_to_right_line_centered_layout(const std::function& elemUpdate); - bool selectable_button(const std::string& id, const std::function& elemUpdate, GUIStuff::SelectableButton::DrawType drawType, bool isSelected); + bool selectable_button(const char* id, const std::function& elemUpdate, GUIStuff::SelectableButton::DrawType drawType, bool isSelected); void push_id(int64_t id); - void push_id(const std::string& id); + void push_id(const char* id); void pop_id(); - void scroll_bar_area(const std::string& id, bool clipHorizontal, const std::function& elemUpdate, bool maxScrollBugWorkaround = true); - void scroll_bar_many_entries_area(const std::string& id, float entryHeight, size_t entryCount, bool clipHorizontal, const std::function& entryUpdate, const std::function& elemUpdate = nullptr); + void scroll_bar_area(const char* id, bool clipHorizontal, const std::function& elemUpdate, bool maxScrollBugWorkaround = true); + void scroll_bar_many_entries_area(const char* id, float entryHeight, size_t entryCount, bool clipHorizontal, const std::function& entryUpdate, const std::function& elemUpdate = nullptr); - bool input_color_component_255(const std::string& id, float* val, const std::function& elemUpdate = nullptr); - bool input_text(const std::string& id, std::string* val, bool updateEveryEdit = true, const std::function& elemUpdate = nullptr); + bool input_color_component_255(const char* id, float* val, const std::function& elemUpdate = nullptr); + bool input_text(const char* id, std::string* val, bool updateEveryEdit = true, const std::function& elemUpdate = nullptr); - bool radio_button(const std::string& id, bool val, const std::function& elemUpdate = nullptr); - bool radio_button_field(const std::string& id, const std::string& name, bool val, const std::function& elemUpdate = nullptr); + bool radio_button(const char* id, bool val, const std::function& elemUpdate = nullptr); + bool radio_button_field(const char* id, std::string_view name, bool val, const std::function& elemUpdate = nullptr); - void checkbox_field(const std::string& id, const std::string& name, bool* val, const std::function& elemUpdate = nullptr); - void checkbox(const std::string& id, bool* val, const std::function& elemUpdate = nullptr); + void checkbox_field(const char* id, std::string_view name, bool* val, const std::function& elemUpdate = nullptr); + void checkbox(const char* id, bool* val, const std::function& elemUpdate = nullptr); - void tab_list(const std::string& id, const std::vector>& tabNames, size_t& selectedTab, std::optional& closedTab, const std::function& elemUpdate = nullptr); + void tab_list(const char* id, const std::vector>& tabNames, size_t& selectedTab, std::optional& closedTab, const std::function& elemUpdate = nullptr); - void input_path(const std::string& id, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate = nullptr); - void input_path_field(const std::string& id, const std::string& name, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate = nullptr); + void input_path(const char* id, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate = nullptr); + void input_path_field(const char* id, std::string_view name, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function& elemUpdate = nullptr); - void dropdown_select(const std::string& id, size_t* val, const std::vector& selections, float width = 200.0f, const std::function& hoverboxElemUpdate = nullptr); + void dropdown_select(const char* id, size_t* val, const std::vector& selections, float width = 200.0f, const std::function& hoverboxElemUpdate = nullptr); - void paint_circle_popup_menu(const std::string& id, const Vector2f& centerPos, const PaintCircleMenu::Data& val, const std::function& elemUpdate = nullptr); - void list_popup_menu(const std::string& id, Vector2f popupPos, const std::function& elemUpdate); + void paint_circle_popup_menu(const char* id, const Vector2f& centerPos, const PaintCircleMenu::Data& val, const std::function& elemUpdate = nullptr); + void list_popup_menu(const char* id, Vector2f popupPos, const std::function& elemUpdate); - void tree_listing(const std::string& id, NetworkingObjects::NetObjID rootObjID, const TreeListing::DisplayData& displayData, TreeListing::SelectionData& selectionData); + void tree_listing(const char* id, NetworkingObjects::NetObjID rootObjID, const TreeListing::DisplayData& displayData, TreeListing::SelectionData& selectionData); void obstructing_window(); @@ -147,7 +147,7 @@ class GUIManager { return toRet; } - template bool input_generic(const std::string& id, T* val, const std::function(const std::string&)>& fromStr, const std::function& toStr, bool singleLine, bool updateEveryEdit, const std::function& elemUpdate = nullptr) { + template bool input_generic(const char* id, T* val, const std::function(std::string_view)>& fromStr, const std::function& toStr, bool singleLine, bool updateEveryEdit, const std::function& elemUpdate = nullptr) { bool isUpdating = false; push_id(id); isUpdating = insert_element>()->update(*io, val, fromStr, toStr, singleLine, updateEveryEdit, elemUpdate); @@ -155,9 +155,9 @@ class GUIManager { return isUpdating; } - template bool input_scalar(const std::string& id, T* val, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { + template bool input_scalar(const char* id, T* val, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { return input_generic(id, val, - [&](const std::string& a) { + [&](std::string_view a) { if(a.empty()) return min; T toRet; @@ -180,23 +180,26 @@ class GUIManager { }, true, false, elemUpdate); } - bool input_scalar(const std::string& id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function& elemUpdate); + bool input_scalar(const char* id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function& elemUpdate); - template bool input_scalar_fields(const std::string& id, const std::string& name, TContainer* val, size_t elemCount, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { + template bool input_scalar_fields(const char* id, std::string_view name, TContainer* val, size_t elemCount, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { bool isUpdating = false; push_id(id); left_to_right_line_layout([&]() { text_label(name); - for(size_t i = 0; i < elemCount; i++) - isUpdating |= input_scalar(std::to_string(i), &(*val)[i], min, max, decimalPrecision, elemUpdate); + for(size_t i = 0; i < elemCount; i++) { + push_id(i); + isUpdating |= input_scalar("field", &(*val)[i], min, max, decimalPrecision, elemUpdate); + pop_id(); + } }); pop_id(); return isUpdating; } - template bool input_color_hex(const std::string& id, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { + template bool input_color_hex(const char* id, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { return input_generic(id, val, - [&](const std::string& str) { + [&](std::string_view str) { T def = {0.0f, 0.0f, 0.0f, 1.0f}; unsigned startIndex = 0; if(str.empty()) @@ -231,21 +234,21 @@ class GUIManager { true, false, elemUpdate); } - template bool color_picker(const std::string& id, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { + template bool color_picker(const char* id, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { push_id(id); bool isUpdating = insert_element>()->update(*io, val, selectAlpha, elemUpdate); pop_id(); return isUpdating; } - template bool slider_scalar(const std::string& id, T* val, T min, T max, const std::function& elemUpdate = nullptr) { + template bool slider_scalar(const char* id, T* val, T min, T max, const std::function& elemUpdate = nullptr) { push_id(id); bool isUpdating = insert_element>()->update(*io, val, min, max, elemUpdate); pop_id(); return isUpdating; } - template bool input_scalar_field(const std::string& id, const std::string& name, T* val, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { + template bool input_scalar_field(const char* id, std::string_view name, T* val, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { bool isUpdating = false; left_to_right_line_layout([&]() { text_label(name); @@ -254,7 +257,7 @@ class GUIManager { return isUpdating; } - template bool slider_scalar_field(const std::string& id, const std::string& name, T* val, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { + template bool slider_scalar_field(const char* id, std::string_view name, T* val, T min, T max, int decimalPrecision = 0, const std::function& elemUpdate = nullptr) { bool isUpdating = false; push_id(id); left_to_right_line_layout([&]() { @@ -266,7 +269,7 @@ class GUIManager { return isUpdating; } - template bool big_color_button(const std::string& id, T* val, bool isSelected = false, const std::function& elemUpdate = nullptr) { + template bool big_color_button(const char* id, T* val, bool isSelected = false, const std::function& elemUpdate = nullptr) { bool toRet = false; CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(GUIStuff::GUIManager::BIG_BUTTON_SIZE), .height = CLAY_SIZING_FIXED(GUIStuff::GUIManager::BIG_BUTTON_SIZE)}}}) { toRet = color_button(id, val, isSelected, elemUpdate); @@ -274,7 +277,7 @@ class GUIManager { return toRet; } - template bool color_button(const std::string& id, T* val, bool isSelected = false, const std::function& elemUpdate = nullptr) { + template bool color_button(const char* id, T* val, bool isSelected = false, const std::function& elemUpdate = nullptr) { return selectable_button(id, [&](SelectionHelper& s, bool iS) { CLAY_AUTO_ID({.layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}, @@ -287,7 +290,7 @@ class GUIManager { }, GUIStuff::SelectableButton::DrawType::TRANSPARENT_BORDER, isSelected); } - template bool color_button_big(const std::string& id, T* val, bool isSelected = false, const std::function& elemUpdate = nullptr) { + template bool color_button_big(const char* id, T* val, bool isSelected = false, const std::function& elemUpdate = nullptr) { bool toRet = false; CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(BIG_BUTTON_SIZE), .height = CLAY_SIZING_FIXED(BIG_BUTTON_SIZE)}}}) { toRet = selectable_button(id, [&](SelectionHelper& s, bool iS) { @@ -304,7 +307,7 @@ class GUIManager { return toRet; } - template bool color_picker_button(const std::string& id, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { + template bool color_picker_button(const char* id, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { bool isUpdating = false; push_id(id); bool& isOpen = insert_any_with_id(0, false); @@ -325,9 +328,9 @@ class GUIManager { return isUpdating; } - bool font_picker(const std::string& id, std::string* fontName); + bool font_picker(const char* id, std::string* fontName); - template bool color_picker_items(const std::string& id, T* val, bool selectAlpha, float fixedPickerWidth = 0.0f) { + template bool color_picker_items(const char* id, T* val, bool selectAlpha, float fixedPickerWidth = 0.0f) { bool isUpdating = false; push_id(id); if(fixedPickerWidth == 0.0f) @@ -361,7 +364,7 @@ class GUIManager { return isUpdating; } - template bool color_picker_button_field(const std::string& id, const std::string& name, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { + template bool color_picker_button_field(const char* id, std::string_view name, T* val, bool selectAlpha, const std::function& elemUpdate = nullptr) { bool isUpdating = false; left_to_right_line_layout([&]() { isUpdating = color_picker_button(id, val, selectAlpha, elemUpdate); diff --git a/src/GUIStuff/GUIManagerID.cpp b/src/GUIStuff/GUIManagerID.cpp index 36394c0..9227019 100644 --- a/src/GUIStuff/GUIManagerID.cpp +++ b/src/GUIStuff/GUIManagerID.cpp @@ -2,18 +2,18 @@ namespace GUIStuff { -GUIManagerID::GUIManagerID(int64_t numID): +GUIManagerID::GUIManagerID(intptr_t numID): idIsStr(false), idNum(numID) {} -GUIManagerID::GUIManagerID(const std::string& strID): +GUIManagerID::GUIManagerID(const char* strID): idIsStr(true), - idStr(strID) + idNum(reinterpret_cast(strID)) {} bool GUIManagerID::operator==(const GUIManagerID& other) const { - return idIsStr == other.idIsStr && idStr == other.idStr && idNum == other.idNum; + return idIsStr == other.idIsStr && idNum == other.idNum; } } diff --git a/src/GUIStuff/GUIManagerID.hpp b/src/GUIStuff/GUIManagerID.hpp index cc30549..d4c12e5 100644 --- a/src/GUIStuff/GUIManagerID.hpp +++ b/src/GUIStuff/GUIManagerID.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -7,12 +8,11 @@ namespace GUIStuff { struct GUIManagerID { GUIManagerID() = default; GUIManagerID(int64_t numID); - GUIManagerID(const std::string& strID); + GUIManagerID(const char* strID); bool operator==(const GUIManagerID& other) const; bool idIsStr; - std::string idStr; - int64_t idNum = 0; + intptr_t idNum; }; typedef std::vector GUIManagerIDStack; @@ -22,10 +22,7 @@ typedef std::vector GUIManagerIDStack; template <> struct std::hash { size_t operator()(const GUIStuff::GUIManagerID& v) const { - if(v.idIsStr) - return std::hash{}(v.idStr); - else - return std::hash{}(v.idNum); + return std::hash{}(v.idNum); } }; diff --git a/src/Toolbar.cpp b/src/Toolbar.cpp index bb57409..6875393 100644 --- a/src/Toolbar.cpp +++ b/src/Toolbar.cpp @@ -1112,12 +1112,14 @@ void Toolbar::drawing_program_gui() { } } -bool Toolbar::color_palette(const std::string& id, Vector4f* color, bool& hoveringOnDropdown) { +bool Toolbar::color_palette(const char* id, Vector4f* color, bool& hoveringOnDropdown) { bool isUpdating = false; gui.push_id(id); auto& palette = paletteData.palettes[paletteData.selectedPalette].colors; constexpr float COLOR_BUTTON_SIZE = GUIStuff::GUIManager::BIG_BUTTON_SIZE; + size_t nextID = 0; + gui.scroll_bar_area("color palette scroll area", false, [&](float, float, float &) { CLAY_AUTO_ID({ .layout = { @@ -1143,7 +1145,8 @@ bool Toolbar::color_palette(const std::string& id, Vector4f* color, bool& hoveri } }) { Vector4f newC = {palette[i].x(), palette[i].y(), palette[i].z(), 1.0f}; - if(gui.color_button("c" + std::to_string(i), &newC, (paletteData.selectedColor == (int)i))) { + gui.push_id(nextID++); + if(gui.color_button("c", &newC, (paletteData.selectedColor == (int)i))) { paletteData.selectedColor = (int)i; // We want to keep the old color's alpha color->x() = newC.x(); @@ -1151,6 +1154,7 @@ bool Toolbar::color_palette(const std::string& id, Vector4f* color, bool& hoveri color->z() = newC.z(); isUpdating = true; } + gui.pop_id(); if(paletteData.selectedColor == (int)i && (newC.x() != color->x() || newC.y() != color->y() || newC.z() != color->z())) paletteData.selectedColor = -1; } @@ -1626,7 +1630,7 @@ void Toolbar::options_menu() { .layoutDirection = CLAY_LEFT_TO_RIGHT } }) { - gui.text_label(nlohmann::json(static_cast(i))); + gui.text_label(std::string(nlohmann::json(static_cast(i)))); auto f = std::find_if(main.input.keyAssignments.begin(), main.input.keyAssignments.end(), [&](auto& p) { return p.second == i; }); diff --git a/src/Toolbar.hpp b/src/Toolbar.hpp index 41a0190..a2703c4 100644 --- a/src/Toolbar.hpp +++ b/src/Toolbar.hpp @@ -108,7 +108,7 @@ class Toolbar { void options_menu(); void file_picker_gui(); void performance_metrics(); - bool color_palette(const std::string& id, Vector4f* color, bool& hoveringOnDropdown); + bool color_palette(const char* id, Vector4f* color, bool& hoveringOnDropdown); void open_world_file(bool isClient, const std::string& netSource, const std::string& serverLocalID); void load_default_palette(); void load_default_theme();