Skip to content

Commit

Permalink
limitNodeWidth, limitNodeHeight, and width/height properties in Markd…
Browse files Browse the repository at this point in the history
…own images (#1043)

* New image arguments format

* add ampersand support whoops

* first moves

* i think that's it for now
  • Loading branch information
hiimjustin000 authored Sep 10, 2024
1 parent 2d9ce8f commit e8751bf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions loader/include/Geode/utils/cocos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions loader/src/ui/nodes/MDTextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -342,6 +344,18 @@ struct MDParser {
spriteScale = *scaleRes;
}
}
else if (key == "width") {
auto widthRes = utils::numFromString<float>(value);
if (widthRes) {
spriteWidth = *widthRes;
}
}
else if (key == "height") {
auto heightRes = utils::numFromString<float>(value);
if (heightRes) {
spriteHeight = *heightRes;
}
}
}

if (utils::string::startsWith(s_lastImage, "frame:")) {
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions loader/src/utils/cocos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e8751bf

Please sign in to comment.