Skip to content

Commit

Permalink
Cleanup, more features, fix UI folder crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphalaneous committed May 16, 2024
1 parent b1cc722 commit f685edd
Show file tree
Hide file tree
Showing 8 changed files with 1,256 additions and 979 deletions.
22 changes: 21 additions & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,25 @@
"resources/bigFont-hd.fnt",
"resources/bigFont-uhd.fnt"
]
}
},
"settings": {
"ccscale9sprite-fix": {
"name": "CCScale9Sprite Fix",
"description": "Fix sprite overlapping when you have transparent CCScale9Sprites (Such as on the level search page).",
"type": "bool",
"default": true
},
"pusab-fix": {
"name": "Pusab Fix",
"description": "Makes changing pusab in texture packs not affect the font within levels.",
"type": "bool",
"default": true
},
"ui-modifications": {
"name": "Allow UI Modifications",
"description": "Allow Tetxure Packs to edit the game's interface.",
"type": "bool",
"default": true
}
}
}
25 changes: 25 additions & 0 deletions src/CCLabelBMFont.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <Geode/Geode.hpp>
#include <Geode/modify/CCLabelBMFont.hpp>
#include "Utils.h"

using namespace geode::prelude;

class $modify(MyCCLabelBMFont, CCLabelBMFont){

struct Fields {
float m_limitWidth = 1;
float m_limitDefaultScale = 1;
float m_limitMinScale = 1;
bool m_isLimited = false;
};

void limitLabelWidth(float width, float defaultScale, float minScale){

m_fields->m_limitWidth = width;
m_fields->m_limitDefaultScale = defaultScale;
m_fields->m_limitMinScale = minScale;
m_fields->m_isLimited = true;

CCLabelBMFont::limitLabelWidth(width, defaultScale, minScale);
}
};
199 changes: 163 additions & 36 deletions src/CCScale9SpriteFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,182 @@

using namespace geode::prelude;

class $modify(CCScale9Sprite) {
class $modify(MyCCScale9Sprite, CCScale9Sprite){

struct Fields {
bool m_hasFixed = false;
};
void visit(){
bool doFix = Mod::get()->getSettingValue<bool>("ccscale9sprite-fix");

void visit() {
CCScale9Sprite::visit();
if (!m_fields->m_hasFixed) {
fixSprites();
m_fields->m_hasFixed = true;
if(doFix){
if(this->*(&MyCCScale9Sprite::m_positionsAreDirty)){
updateSprites();
this->*(&MyCCScale9Sprite::m_positionsAreDirty) = false;
}
CCNode::visit();
}
else{
CCScale9Sprite::visit();
}
}

void transformSprite(CCSprite* sprite, CCSize transform, int idx) {
auto tRect = sprite->getTextureRect();
void updateSprites(){

tRect.size -= transform;
CCSprite* topLeft = public_cast(this, _topLeft);
topLeft->setID("top-left");
CCSprite* top = public_cast(this, _top);
top->setID("top");
CCSprite* topRight = public_cast(this, _topRight);
topRight->setID("top-right");
CCSprite* left = public_cast(this, _left);
left->setID("left");
CCSprite* center = public_cast(this, _centre);
center->setID("center");
CCSprite* right = public_cast(this, _right);
right->setID("right");
CCSprite* bottomLeft = public_cast(this, _bottomLeft);
bottomLeft->setID("bottom-left");
CCSprite* bottom = public_cast(this, _bottom);
bottom->setID("bottom");
CCSprite* bottomRight = public_cast(this, _bottomRight);
bottomRight->setID("bottom-right");

if (idx == 0)
sprite->setPosition(sprite->getPosition() + transform);
else if (idx == 1 && (transform.width > 0 || transform.height > 0))
sprite->setVisible(false);
else if (idx == 2)
tRect.origin += transform;
if(!(topLeft && topRight && bottomRight && bottomLeft && center)) {
return;
}

sprite->setTextureRect(tRect);
}
CCSize size = this->m_obContentSize;

void fixSprites() {
CCSprite* sprites[3][3] = {
{_topLeft, _top, _topRight},
{_left, _centre, _right},
{_bottomLeft, _bottom, _bottomRight}
};
float edgeHeight = top->getContentSize().height + bottom->getContentSize().height;

auto halfSize = getContentSize() / 2;
float edgeWidth = left->getContentSize().width + right->getContentSize().width;

for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
auto sprite = sprites[i][j];
bool isSquishedVertically = false;
bool isSquishedHorizontally = false;

auto tRect = sprite->getTextureRect();
auto diff = tRect.size - halfSize;
diff = CCSize(fmax(0, diff.width), fmax(0, diff.height));
if(edgeHeight >= size.height){
isSquishedVertically = true;
}

transformSprite(sprite, CCSize(diff.width, 0), j);
transformSprite(sprite, CCSize(0, diff.height), i);
}
if(edgeWidth >= size.width){
isSquishedHorizontally = true;
}

float sizableWidth = size.width - topLeft->getContentSize().width - topRight->getContentSize().width;
float sizableHeight = size.height - topLeft->getContentSize().height - bottomRight->getContentSize().height;

float horizontalScale = sizableWidth / center->getContentSize().width;
float verticalScale = sizableHeight / center->getContentSize().height;

center->setScaleX(horizontalScale);
center->setScaleY(verticalScale);

float rescaledWidth = center->getContentSize().width * horizontalScale;
float rescaledHeight = center->getContentSize().height * verticalScale;

float leftWidth = bottomLeft->getContentSize().width;
float bottomHeight = bottomLeft->getContentSize().height;

bottomLeft->setAnchorPoint({0, 0});
bottomRight->setAnchorPoint({0, 0});
topLeft->setAnchorPoint({0, 0});
topRight->setAnchorPoint({0, 0});
left->setAnchorPoint({0, 0});
right->setAnchorPoint({0, 0});
top->setAnchorPoint({0, 0});
bottom->setAnchorPoint({0, 0});
center->setAnchorPoint({0, 0});

bottomLeft->setPosition({0, 0});
bottomRight->setPosition({leftWidth+rescaledWidth, 0});

topLeft->setPosition({0, bottomHeight+rescaledHeight});
topRight->setPosition({leftWidth+rescaledWidth, bottomHeight + rescaledHeight});

left->setPosition({0, bottomHeight});
left->setScaleY(verticalScale);

right->setPosition({leftWidth+rescaledWidth, bottomHeight});
right->setScaleY(verticalScale);

bottom->setPosition({leftWidth, 0});
bottom->setScaleX(horizontalScale);

top->setPosition({leftWidth, bottomHeight + rescaledHeight});
top->setScaleX(horizontalScale);

center->setPosition({leftWidth, bottomHeight});

left->setVisible(true);
right->setVisible(true);
top->setVisible(true);
bottom->setVisible(true);
center->setVisible(true);

if(isSquishedVertically || isSquishedHorizontally){
center->setVisible(false);
}

if(isSquishedVertically){
left->setVisible(false);
right->setVisible(false);

fixTopSprite(top);
fixTopSprite(topRight);
fixTopSprite(topLeft);

fixBottomSprite(bottom);
fixBottomSprite(bottomRight);
fixBottomSprite(bottomLeft);
}

if(isSquishedHorizontally){
top->setVisible(false);
bottom->setVisible(false);

fixLeftSprite(left);
fixLeftSprite(topLeft);
fixLeftSprite(bottomLeft);

fixRightSprite(right);
fixRightSprite(topRight);
fixRightSprite(bottomRight);
}
}

void fixTopSprite(CCSprite* spr){
float overlappingHeight = spr->getContentSize().height*2 - this->getContentSize().height;

auto rect = spr->getTextureRect();
rect.size = CCSize{rect.size.width, rect.size.height - overlappingHeight/2};
spr->setPositionY(spr->getPositionY() + overlappingHeight/2);
spr->setTextureRect(rect);
}

void fixBottomSprite(CCSprite* spr){
float overlappingHeight = spr->getContentSize().height*2 - this->getContentSize().height;

auto rect = spr->getTextureRect();
rect.size = CCSize{rect.size.width, rect.size.height - overlappingHeight/2};
rect.origin = CCPoint{rect.origin.x, rect.origin.y + overlappingHeight/2};

spr->setTextureRect(rect);
}

void fixRightSprite(CCSprite* spr){
float overlappingWidth = spr->getContentSize().width*2 - this->getContentSize().width;

auto rect = spr->getTextureRect();
rect.size = CCSize{rect.size.width - overlappingWidth/2, rect.size.height};
spr->setPositionX(spr->getPositionX() + overlappingWidth/2);
spr->setTextureRect(rect);
}

void fixLeftSprite(CCSprite* spr){
float overlappingWidth = spr->getContentSize().width*2 - this->getContentSize().width;

auto rect = spr->getTextureRect();
rect.size = CCSize{rect.size.width - overlappingWidth/2, rect.size.height};
rect.origin = CCPoint{rect.origin.x + overlappingWidth/2, rect.origin.y};

spr->setTextureRect(rect);
}
};
52 changes: 28 additions & 24 deletions src/FileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,45 @@ class FileWatcher {

// Keep a record of files from the base directory and their last modification time
FileWatcher(std::string path_to_watch, std::chrono::duration<int, std::milli> delay) : path_to_watch{path_to_watch}, delay{delay} {
for(auto &file : std::filesystem::recursive_directory_iterator(path_to_watch)) {
paths_[file.path().string()] = std::filesystem::last_write_time(file);
if(std::filesystem::is_directory(path_to_watch)){
for(auto &file : std::filesystem::recursive_directory_iterator(path_to_watch)) {
paths_[file.path().string()] = std::filesystem::last_write_time(file);
}
}
}

// Monitor "path_to_watch" for changes and in case of a change execute the user supplied "action" function
void start(const std::function<void (std::string, FileStatus)> &action) {
while(running_) {
// Wait for "delay" milliseconds
std::this_thread::sleep_for(delay);
if(std::filesystem::is_directory(path_to_watch)){
// Wait for "delay" milliseconds
std::this_thread::sleep_for(delay);

auto it = paths_.begin();
while (it != paths_.end()) {
if (!std::filesystem::exists(it->first)) {
action(it->first, FileStatus::erased);
it = paths_.erase(it);
auto it = paths_.begin();
while (it != paths_.end()) {
if (!std::filesystem::exists(it->first)) {
action(it->first, FileStatus::erased);
it = paths_.erase(it);
}
else {
it++;
}
}
else {
it++;
}
}

// Check if a file was created or modified
for(auto &file : std::filesystem::recursive_directory_iterator(path_to_watch)) {
auto current_file_last_write_time = std::filesystem::last_write_time(file);
// Check if a file was created or modified
for(auto &file : std::filesystem::recursive_directory_iterator(path_to_watch)) {
auto current_file_last_write_time = std::filesystem::last_write_time(file);

// File creation
if(!contains(file.path().string())) {
paths_[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::created);
// File modification
} else {
if(paths_[file.path().string()] != current_file_last_write_time) {
// File creation
if(!contains(file.path().string())) {
paths_[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::modified);
action(file.path().string(), FileStatus::created);
// File modification
} else {
if(paths_[file.path().string()] != current_file_last_write_time) {
paths_[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::modified);
}
}
}
}
Expand Down
Loading

0 comments on commit f685edd

Please sign in to comment.