Skip to content

Commit

Permalink
Merge pull request #327 from thisisignitedoreo/main
Browse files Browse the repository at this point in the history
add more label types
  • Loading branch information
TobyAdd authored Nov 29, 2024
2 parents d39d3f4 + d8e57ec commit 9789784
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,13 @@ void Gui::Render() {
ImGui::Combo("##labelspos", &selected_label_corner, labels_positions, 6, 6);

const char *label_types[] = {
"Hui",
"Time (24H)",
"Time (12H)",
"Session Time",
"Level Progress",
"Attempt",
"CPS Counter",
"Level Info",
"Custom Text",
};
int label_types_count = sizeof(label_types)/sizeof(label_types[0]);
Expand All @@ -229,7 +235,13 @@ void Gui::Render() {
if (ImGui::Button("+")) {
std::string text;
if (selected_label_type == label_types_count - 1) text = selected_label_text;
else if (selected_label_type == 0) text = "eto {hui}";
else if (selected_label_type == 0) text = "{time:24}";
else if (selected_label_type == 1) text = "{time:12}";
else if (selected_label_type == 2) text = "Session Time: {sessionTime}";
else if (selected_label_type == 3) text = "{progress}";
else if (selected_label_type == 4) text = "Attempt {attempt}";
else if (selected_label_type == 5) text = "{cps}/{cpsHigh}/{clicks}";
else if (selected_label_type == 6) text = "{levelName}{byLevelCreator} ({levelId})";

Label l((LabelCorner) (selected_label_corner+1), text);
labels.add(l);
Expand Down
20 changes: 20 additions & 0 deletions src/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class $modify(PlayLayer) {
addChild(m_fields->labels_bottom);
addChild(m_fields->labels_top);

Labels::get().attempts = 1;
Labels::get().session_time = 0.f;

if (config.get<bool>("startpos_switcher", false) && !startPositions.empty()) {
auto win_size = cocos2d::CCDirector::sharedDirector()->getWinSize();

Expand Down Expand Up @@ -361,6 +364,8 @@ class $modify(PlayLayer) {

PlayLayer::playEndAnimationToPos({0, 0});
}

CpsCounter::get().reset();
}

void levelComplete() {
Expand All @@ -379,6 +384,16 @@ class $modify(PlayLayer) {
if (Config::get().get<bool>("no_new_best_popup", false)) return;
PlayLayer::showNewBest(p0, p1, p2, p3, p4, p5);
}

void updateAttempts() {
PlayLayer::updateAttempts();
Labels::get().attempts++;
}

void updateAttemptTime(float time) {
PlayLayer::updateAttemptTime(time);
Labels::get().session_time += time;
}

void updateVisibility(float dt) {
auto& config = Config::get();
Expand Down Expand Up @@ -456,6 +471,11 @@ class $modify(GJBaseGameLayer) {
GJBaseGameLayer::lightningFlash(from, to, color, lineWidth, duration, displacement, flash, opacity);
gm->m_performanceMode = performanceMode;
}

void handleButton(bool down, int button, bool isPlayer1) {
GJBaseGameLayer::handleButton(down, button, isPlayer1);
if (down) CpsCounter::get().click();
}
};

class $modify(CameraTriggerGameObject) {
Expand Down
51 changes: 49 additions & 2 deletions src/labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,36 @@ Label::Label(LabelCorner _corner, std::string _format_text) {
}

std::string Label::get_text() {
SYSTEMTIME localTime;
GetLocalTime(&localTime);

int hour12 = localTime.wHour;
std::string period12 = (hour12 < 12) ? "AM" : "PM";
if (hour12 == 0) hour12 = 12;
if (hour12 > 12) hour12 -= 12;

std::string result = format_text;
result = replace_all(result, "{hui}", "popa jopa hui");

auto pl = PlayLayer::get();
bool platformer = pl->m_level->isPlatformer();
float percentage = platformer ? static_cast<float>(pl->m_gameState.m_levelTime) : pl->getCurrentPercent();

CpsCounter::get().update();

result = replace_all(result, "{time:12}", fmt::format("{} {}", time_to_fmt_time(hour12, localTime.wMinute, localTime.wSecond), period12));
result = replace_all(result, "{time:24}", time_to_fmt_time(localTime.wHour, localTime.wMinute, localTime.wSecond));
result = replace_all(result, "{attempt}", std::to_string(Labels::get().attempts));
result = replace_all(result, "{sessionTime}", seconds_to_fmt_time(Labels::get().session_time));
result = replace_all(result, "{progress}", platformer ? seconds_to_fmt_time(percentage) : fmt::format("{}%", round_float(percentage, 2)));
result = replace_all(result, "{clicks}", std::to_string(CpsCounter::get().overall));
result = replace_all(result, "{cps}", std::to_string(CpsCounter::get().cps));
result = replace_all(result, "{cpsHigh}", std::to_string(CpsCounter::get().highscore));
result = replace_all(result, "{levelName}", pl->m_level->m_levelName);
result = replace_all(result, "{levelCreator}", pl->m_level->m_creatorName);
result = replace_all(result, "{byLevelCreator}", pl->m_level->m_creatorName.empty() ? "" : fmt::format(" by {}", pl->m_level->m_creatorName));
result = replace_all(result, "{levelId}", std::to_string(pl->m_level->m_levelID));
result = replace_all(result, "{\\n}", "\n");

return result;
}

Expand All @@ -22,6 +50,25 @@ std::string Label::replace_all(std::string src, std::string replace, std::string
return src;
}

std::string Label::time_to_fmt_time(int hours, int minutes, int seconds) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":" << std::setw(2) << seconds;
return ss.str();
}

std::string Label::seconds_to_fmt_time(float seconds) {
int time = (int) seconds;
int minutes = seconds / 60;
int hour = minutes / 60;
return time_to_fmt_time(hour % 60, minutes % 60, time % 60);
}

std::string Label::round_float(float value, int decimal_places) {
std::stringstream fmtFloat;
fmtFloat << std::fixed << std::setprecision(decimal_places) << value;
return fmtFloat.str();
}

std::string Labels::get_label_string(LabelCorner corner) {
std::string result = "";

Expand Down Expand Up @@ -99,4 +146,4 @@ void Labels::load() {
}

file.close();
}
}
46 changes: 46 additions & 0 deletions src/labels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Label {

private:
std::string replace_all(std::string src, std::string replace, std::string replacement);

std::string time_to_fmt_time(int hours, int minutes, int seconds);
std::string seconds_to_fmt_time(float seconds);
std::string round_float(float value, int decimal_places);
};

class Labels {
Expand All @@ -48,9 +52,51 @@ class Labels {
void move_down(int index);
void swap(int index_0, int index_1);

int attempts = 1;
float session_time = 0.f;
float progress = 0.f;
bool platformer = false;

void save();
void load();

private:
Labels() = default;
};

class CpsCounter {
public:
static CpsCounter& get() {
static CpsCounter instance;
return instance;
}

CpsCounter& operator=(const CpsCounter&) = delete;
CpsCounter(const CpsCounter&) = delete;

std::vector<DWORD> clicks = {};
int cps, highscore, overall;

void reset() { cps = 0; highscore = 0; overall = 0; }
void click() {
DWORD millis = GetTickCount();
overall++;
clicks.push_back(millis);
}
void update() {
time_t currentTick = GetTickCount();

clicks.erase(std::remove_if(clicks.begin(), clicks.end(), [currentTick](DWORD tick) {
return currentTick - tick > 1000;
}), clicks.end());

cps = clicks.size();

if (highscore < cps) {
highscore = cps;
}
}

private:
CpsCounter() = default;
};

0 comments on commit 9789784

Please sign in to comment.