From 2d9ce8f3e374401645f621436b3502924ee21431 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Tue, 10 Sep 2024 21:09:09 +0300 Subject: [PATCH 1/2] weakref hash from sleepyut --- loader/include/Geode/utils/cocos.hpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index 02eba1a0..900fe51c 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -401,6 +401,9 @@ namespace geode { WeakRef(std::shared_ptr obj) : m_controller(obj) {} + friend class std::hash>; + + public: /** * Construct a WeakRef of an object. A weak reference is one that will @@ -972,6 +975,13 @@ namespace std { return std::hash()(ref.data()); } }; + + template + struct std::hash> { + size_t operator()(geode::WeakRef const& ref) const { + return hash{}(ref.m_controller); + } + }; } // more utils From e8751bf899c216b84a7af5c2445d9d5a0af74bfe Mon Sep 17 00:00:00 2001 From: Justin <52604018+hiimjustin000@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:13:42 -0400 Subject: [PATCH 2/2] limitNodeWidth, limitNodeHeight, and width/height properties in Markdown images (#1043) * New image arguments format * add ampersand support whoops * first moves * i think that's it for now --- loader/include/Geode/utils/cocos.hpp | 18 ++++++++++++++++++ loader/src/ui/nodes/MDTextArea.cpp | 23 +++++++++++++++++++++++ loader/src/utils/cocos.cpp | 8 ++++++++ 3 files changed, 49 insertions(+) diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index 900fe51c..90af7e60 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -716,6 +716,24 @@ namespace geode::cocos { */ GEODE_DLL void limitNodeSize(cocos2d::CCNode* node, cocos2d::CCSize const& size, float def, float min); + /** + * Rescale node to fit inside given width + * @param node Node to rescale + * @param width Width to fit inside + * @param def Default scale + * @param min Minimum scale + */ + GEODE_DLL void limitNodeWidth(cocos2d::CCNode* node, float width, float def, float min); + + /** + * Rescale node to fit inside given height + * @param node Node to rescale + * @param height Height to fit inside + * @param def Default scale + * @param min Minimum scale + */ + GEODE_DLL void limitNodeHeight(cocos2d::CCNode* node, float height, float def, float min); + /** * Checks if a node is visible (recursively * checks parent visibility) diff --git a/loader/src/ui/nodes/MDTextArea.cpp b/loader/src/ui/nodes/MDTextArea.cpp index da766a4f..20a3b97f 100644 --- a/loader/src/ui/nodes/MDTextArea.cpp +++ b/loader/src/ui/nodes/MDTextArea.cpp @@ -334,6 +334,8 @@ struct MDParser { } float spriteScale = 1.0f; + float spriteWidth = 0.0f; + float spriteHeight = 0.0f; for (auto [key, value] : imgArguments) { if (key == "scale") { @@ -342,6 +344,18 @@ struct MDParser { spriteScale = *scaleRes; } } + else if (key == "width") { + auto widthRes = utils::numFromString(value); + if (widthRes) { + spriteWidth = *widthRes; + } + } + else if (key == "height") { + auto heightRes = utils::numFromString(value); + if (heightRes) { + spriteHeight = *heightRes; + } + } } if (utils::string::startsWith(s_lastImage, "frame:")) { @@ -357,6 +371,15 @@ struct MDParser { } if (spr && spr->getUserObject("geode.texture-loader/fallback") == nullptr) { spr->setScale(spriteScale); + if (spriteWidth > 0.0f && spriteHeight <= 0.0f) { + limitNodeWidth(spr, spriteWidth, 999.f, .1f); + } + else if (spriteHeight > 0.0f && spriteWidth <= 0.0f) { + limitNodeHeight(spr, spriteHeight, 999.f, .1f); + } + else if (spriteWidth > 0.0f && spriteHeight > 0.0f) { + limitNodeSize(spr, { spriteWidth, spriteHeight }, 999.f, .1f); + } renderer->renderNode(spr); } else { diff --git a/loader/src/utils/cocos.cpp b/loader/src/utils/cocos.cpp index f1400e7a..0ae4486b 100644 --- a/loader/src/utils/cocos.cpp +++ b/loader/src/utils/cocos.cpp @@ -455,6 +455,14 @@ void geode::cocos::limitNodeSize(CCNode* spr, CCSize const& size, float def, flo spr->setScale(clamp(std::min(size.height / spr->getContentHeight(), size.width / spr->getContentWidth()), min, def)); } +void geode::cocos::limitNodeWidth(CCNode* spr, float width, float def, float min) { + spr->setScale(clamp(width / spr->getContentSize().width, min, def)); +} + +void geode::cocos::limitNodeHeight(CCNode* spr, float height, float def, float min) { + spr->setScale(clamp(height / spr->getContentSize().height, min, def)); +} + bool geode::cocos::nodeIsVisible(CCNode* node) { if (!node->isVisible()) { return false;