Skip to content

Commit

Permalink
add code for locking
Browse files Browse the repository at this point in the history
  • Loading branch information
GameParrot committed Dec 25, 2024
1 parent 9670c8d commit 0237757
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ bool Conf::logClicks = false;
bool Conf::showLogWindow = false;
int Conf::threshold = 50;

bool Conf::locked = false;

static properties::property_list conf('=');
static properties::property<bool> enabled(conf, "Enabled", true);
static properties::property<bool> blockRightDc(conf, "BlockRightDc", true);
Expand Down
2 changes: 2 additions & 0 deletions src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct Conf {
static bool useLegacyMousefeed;
static int threshold;

static bool locked;

static void load(ImGUIOptions opts);
static void save();
};
75 changes: 71 additions & 4 deletions src/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ void ImGUIOptions::initImgui() {
struct MenuEntryABI enabled;
enabled.name = "Enabled";
enabled.click = [](void* user) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
if(Conf::locked) {
inst->showLockedAlert();
return;
}
Conf::enabled = !Conf::enabled;
Conf::save();
if(Conf::showLogWindow) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
inst->updateLogWindow();
}
};
Expand All @@ -32,10 +36,14 @@ void ImGUIOptions::initImgui() {
struct MenuEntryABI blockRightDc;
blockRightDc.name = "Block right DC";
blockRightDc.click = [](void* user) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
if(Conf::locked) {
inst->showLockedAlert();
return;
}
Conf::blockRightDc = !Conf::blockRightDc;
Conf::save();
if(Conf::showLogWindow) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
inst->updateLogWindow();
}
};
Expand Down Expand Up @@ -68,6 +76,11 @@ void ImGUIOptions::initImgui() {
struct MenuEntryABI changeThreshold;
changeThreshold.name = "Change threshold";
changeThreshold.click = [](void* user) {
if(Conf::locked) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
inst->showLockedAlert();
return;
}
struct control thresholdWindow;
thresholdWindow.data.sliderint.min = 1;
thresholdWindow.data.sliderint.max = 150;
Expand All @@ -82,17 +95,42 @@ void ImGUIOptions::initImgui() {
};
changeThreshold.selected = [](void* user) -> bool { return false; };
changeThreshold.length = 0;
changeThreshold.user = (void*)this;

struct MenuEntryABI reloadConf;
reloadConf.name = "Reload config";
reloadConf.click = [](void* user) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
if(Conf::locked) {
inst->showLockedAlert();
return;
}
Conf::load(*inst);
};
reloadConf.user = (void*)this;
reloadConf.selected = [](void* user) -> bool { return false; };
reloadConf.length = 0;

struct MenuEntryABI lock;
lock.name = "Lock";
lock.click = [](void* user) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
if(Conf::locked) {
inst->showLockedAlert();
return;
}
inst->showConfirmPrompt((char*)"Are you sure you want to lock?\nYou will not be able to change settings until restart.", (char*)"Lock", user, [](void* user) { mcpelauncher_close_window("Lock"); }, [](void* user) {
mcpelauncher_close_window("Lock");
Conf::locked = true;
if(Conf::showLogWindow) {
ImGUIOptions* inst = static_cast<ImGUIOptions*>(user);
inst->updateLogWindow();
} });
};
lock.user = (void*)this;
lock.selected = [](void* user) -> bool { return Conf::locked; };
lock.length = 0;

struct MenuEntryABI entry;
struct MenuEntryABI entries[] = {enabled, blockRightDc, logClicks, showLogWindow, changeThreshold, reloadConf};
entry.subentries = entries;
Expand Down Expand Up @@ -122,7 +160,7 @@ void ImGUIOptions::updateLogWindow() {
logWindow.type = 3;
logWindow.data.text.label = (char*)clickLog.c_str();

std::string infoText = "Enabled: " + formatBool(Conf::enabled) + " | Block right DC: " + formatBool(Conf::blockRightDc) + " | Threshold: " + std::to_string(Conf::threshold) + "\n";
std::string infoText = "Enabled: " + formatBool(Conf::enabled) + " | Block right DC: " + formatBool(Conf::blockRightDc) + " | Threshold: " + std::to_string(Conf::threshold) + (Conf::locked ? " | Locked" : "") + "\n";
struct control infoBox;
infoBox.type = 3;
infoBox.data.text.label = (char*)infoText.c_str();
Expand All @@ -134,4 +172,33 @@ void ImGUIOptions::updateLogWindow() {

void ImGUIOptions::closeLogWindow() {
mcpelauncher_close_window("DCBlock");
}
}

void ImGUIOptions::showLockedAlert() {
struct control alertWindow;
alertWindow.type = 3;
alertWindow.data.text.label = (char*)"DCBlock is locked! Restart your game to change settings.";
mcpelauncher_show_window("Locked", 1, NULL, [](void* user) {}, 1, &alertWindow);
}

void ImGUIOptions::showConfirmPrompt(char* label, char* title, void* user, void (*onCancel)(void* user), void (*onConfirm)(void* user)) {
struct control alertWindow;
alertWindow.type = 3;
alertWindow.data.text.label = label;

struct control cancelButton;
cancelButton.data.button.label = "Cancel";
cancelButton.data.button.user = user;
cancelButton.data.button.onClick = onCancel;
cancelButton.type = 0;

struct control okButton;
okButton.data.button.label = "OK";
okButton.data.button.user = user;
okButton.data.button.onClick = onConfirm;
okButton.type = 0;

struct control entries[] = {alertWindow, cancelButton, okButton};

mcpelauncher_show_window(title, 1, NULL, [](void* user) {}, 3, entries);
}
5 changes: 4 additions & 1 deletion src/imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ class ImGUIOptions {
static showwindow mcpelauncher_show_window;
static closewindow mcpelauncher_close_window;

void showLockedAlert();

public:
void initImgui();
void addClick(int button, bool supressed);
void updateLogWindow();
void closeLogWindow();
static void closeLogWindow();
static void showConfirmPrompt(char* label, char* title, void* user, void (*onCancel)(void* user), void (*onConfirm)(void* user));
};

struct MenuEntryABI {
Expand Down

0 comments on commit 0237757

Please sign in to comment.