Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch button #22

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions Source/Mod/RichPresence/Presence.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <Presence.h>
#include <Config.h>
#include <Menu.h>

#define MAX_RESPONSE 65535

Expand All @@ -22,6 +23,8 @@ presence_button serverlist_button = {

const char* app_id = "699358451494682714";

int first_run = 1;

int player_id = 0;
int reset_timer_on_map_change = 1;
int show_join_button = 1;
Expand Down Expand Up @@ -360,20 +363,91 @@ void update_presence() {
Discord_UpdatePresence(&presence);
}

void create_rich_presence_menu() {
struct Menu* rpc_menu = create_menu(200, 400, 0, "Rich presence");
rpc_menu->x_size = 250;
rpc_menu->y_size = 75;

struct ItemSwitchButton* rpc_switch = create_switch_button(rpc_menu,
"Enabled",
"enabled",
switch_rich_presence,
&presence_enabled);
struct ItemSwitchButton* join_btn_switch = create_switch_button(rpc_menu,
"Show join btn",
"show_join_button",
switch_btn,
&show_join_button);
struct ItemSwitchButton* reset_timer_on_new_map_switch = create_switch_button(rpc_menu,
"Reset timer on new map?",
"reset_timer_on_map_change",
switch_btn,
&reset_timer_on_map_change);

rpc_switch->x_pos = 200;
rpc_switch->y_pos = 5;
join_btn_switch->x_pos = 200;
join_btn_switch->y_pos = 15;
reset_timer_on_new_map_switch->x_pos = 200;
reset_timer_on_new_map_switch->y_pos = 25;

// TODO: hints maybe?
}

void init_rich_presence() {
presence_config = config_get_section("richpresence");
presence_enabled = config_get_bool_entry(presence_config, "enabled", 1);
show_join_button = config_get_bool_entry(presence_config, "show_join_button", 1);
reset_timer_on_map_change = config_get_bool_entry(presence_config, "reset_timer_on_map_change", 1);
if (first_run) {create_rich_presence_menu(); first_run = 0;}
if (!presence_enabled) { return; }

discord_init();
show_join_button = config_get_bool_entry(presence_config, "show_join_button", 1);

// intentional 2 calls, it works only this way on map loading stage idk why (again on WINDOWS)
get_server_info(0, -1);
get_server_info(0, -1);
update_presence();

reset_timer_on_map_change = config_get_bool_entry(presence_config, "reset_timer_on_map_change", 1);
state.playtime_start = time(0);

save_config();
}

void switch_rich_presence(struct Menu* menu, struct ItemSwitchButton* btn) {
if(*(btn->enabled)) {
*(btn->enabled) = 0;

presence_config = config_get_section("richpresence");
config_set_bool_entry(presence_config, btn->config_entry, *(btn->enabled));
save_config();

Discord_ClearPresence();
Discord_Shutdown();
}
else {
*(btn->enabled) = 1;

presence_config = config_get_section("richpresence");
config_set_bool_entry(presence_config, btn->config_entry, *(btn->enabled));
save_config();

init_rich_presence();
}


}

void switch_btn(struct Menu* menu, struct ItemSwitchButton* btn) {
if(*(btn->enabled)) {
*(btn->enabled) = 0;
}
else {
*(btn->enabled) = 1;
}

presence_config = config_get_section("richpresence");
config_set_bool_entry(presence_config, btn->config_entry, *(btn->enabled));

save_config();
}
2 changes: 2 additions & 0 deletions Source/Mod/RichPresence/Presence.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extern int presence_enabled;
extern int client_base;

void init_rich_presence();
void switch_rich_presence();
void switch_btn();
void update_presence();

void trigger_player_count_validation();
Expand Down
105 changes: 100 additions & 5 deletions Source/Mod/UI/Menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct ItemClickableButton* create_clickable_button(struct Menu* menu, char* tex
return btn;
}

struct ItemTextInput* create_text_input(struct Menu* menu, int x_size, int y_size, long background_color, char* placeholder) {
struct ItemTextInput* create_text_input(struct Menu* menu, int x_size, int y_size, int background_color, char* placeholder) {
int id = get_next_available_item_id(menu);

struct ItemTextInput* input = calloc(sizeof(struct ItemTextInput) + 256, 1);
Expand Down Expand Up @@ -217,6 +217,35 @@ struct ItemSlide* create_slide(struct Menu* menu, int min_value, int max_value,
return slide;
}

struct ItemSwitchButton* create_switch_button(struct Menu* menu, char* label, char* config_entry, void (*func)(), int* enabled) {
int id = get_next_available_item_id(menu);

struct ItemSwitchButton* btn = calloc(sizeof(struct ItemSwitchButton) + 32, 1);
btn->type = SWITCH_BUTTON_ITEM;
btn->id = id;
btn->enabled = enabled;
// colors in ARGB!!!
btn->enabled_color = 0xff00ff00;
btn->disabled_color = 0xffff0000;
btn->hold_color = 0xfff1a42e;
btn->color = 0xff757575;
btn->x_size = 40;
btn->y_size = 15;
btn->x_pos = 0;
btn->y_pos = 0;
btn->interval = 0;
btn->last_interaction = 0;
btn->switch_event = func;
strncpy(btn->label, label, 32);
btn->font_size = 14;
strncpy(btn->config_entry, config_entry, 32);

menu->items[id] = btn;
menus[menu->id] = menu;

return btn;
}

struct Menu* create_menu(int x, int y, int outline, char* title) {
int menu_id = get_next_available_menu_id();
struct Menu* menu = calloc(sizeof(struct Menu)+32, 1);
Expand Down Expand Up @@ -361,7 +390,7 @@ int check_cursor_over(int areaX1, int areaY1, int areaX2, int areaY2) {

void draw_cursor() {
// todo load mouse cursor image
long color[] = {0xffffffff};
int color[] = {0xffffffff};
drawtile((long)color, 1, 1, 1, 0x0, 0x0, mouse_x_pos, mouse_y_pos, 15, 15, -1);
}

Expand Down Expand Up @@ -602,6 +631,7 @@ void draw_menu(struct Menu* menu, int interaction) {
largestY = MAX(largestY, txtSizeY+8+ypos-menu->y_pos);*/
break;
case CLICKABLE_BUTTON_ITEM:
{
struct ItemClickableButton* click_btn = (struct ItemClickableButton*)item;

if (menu->minimized && !click_btn->is_toolbar)
Expand Down Expand Up @@ -674,6 +704,8 @@ void draw_menu(struct Menu* menu, int interaction) {
largestY = MAX(largestY, click_btn->y_size+2+clickBtnYpos-menu->y_pos);
}
break;
}

case MULTITEXT_ITEM:
struct ItemMultitext* multitext = (struct ItemMultitext*)item;
struct MultitextNode* lastNode = multitext->last_node;
Expand Down Expand Up @@ -916,9 +948,8 @@ void draw_menu(struct Menu* menu, int interaction) {

sliderPosX = slideXPos;
}

draw_rectangle(menu, slide->slider_color, sliderPosX, sliderPosY, sliderPosX+sliderSizeX, sliderPosY+sliderSizeY);

if (slide->show_status) {
char cool[10];
sprintf(cool, "%i", *slide->interact_int);
Expand All @@ -928,8 +959,72 @@ void draw_menu(struct Menu* menu, int interaction) {

menu->max_y = MAX(slideYPos+slide->y_size, menu->max_y);
break;

case SWITCH_BUTTON_ITEM:
{
struct ItemSwitchButton* switch_btn = (struct ItemSwitchButton*)item;

if (menu->minimized)
break;

int switchBtnXpos = menu->x_pos + switch_btn->x_pos;
int switchBtnYpos = menu->y_pos + switch_btn->y_pos;

if (switch_btn->x_pos < 0) {
switchBtnXpos += menu->x_size;
}

if (switch_btn->y_pos >= 0) {
switchBtnYpos += (!switch_btn->y_pos) ? largestY : switch_btn->y_pos;
} else {
switchBtnYpos += menu->y_size+switch_btn->y_pos;
}

int btn_color = switch_btn->disabled_color;
int secondary_btn_color = switch_btn->color;


if (interaction && check_cursor_over(switchBtnXpos, switchBtnYpos,
switchBtnXpos+switch_btn->x_size,
switchBtnYpos+switch_btn->y_size))
{
secondary_btn_color = switch_btn->hold_color;
if (!switch_btn->is_holding) {
switch_btn->switch_event(menu, switch_btn);
switch_btn->is_holding = 1;
}
}
else {
switch_btn->is_holding = 0;
if (*(switch_btn->enabled)) {
secondary_btn_color = switch_btn->enabled_color;
}
else {
secondary_btn_color = switch_btn->disabled_color;
}
}

drawtile((long)btn_color, 1, 1, 1, 0x0, 0x0, switchBtnXpos, switchBtnYpos, switch_btn->x_size, switch_btn->y_size, -1);
if (*(switch_btn->enabled))
drawtile((long)secondary_btn_color, 1, 1, 1, 0x0, 0x0, switchBtnXpos + (switch_btn->x_size)/2, switchBtnYpos, (switch_btn->x_size)/2, switch_btn->y_size, -1);
else
drawtile((long)secondary_btn_color, 1, 1, 1, 0x0, 0x0, switchBtnXpos, switchBtnYpos, (switch_btn->x_size)/2, switch_btn->y_size, -1);

// label
int textlength = strlen(switch_btn->label);
draw_text(menu, switchBtnXpos - textlength*7, switchBtnYpos, switch_btn->font_size, 0xFFFFFF, "Monocraft.otf", switch_btn->label);

draw_square(switchBtnXpos, switchBtnYpos, switchBtnXpos+switch_btn->x_size, switchBtnYpos+switch_btn->y_size, 0x0);

if (switch_btn->x_pos >= 0)
largestX = MAX(largestX, switch_btn->x_size+switch_btn->x_pos);

if (switch_btn->y_pos >= 0)
largestY = MAX(largestY, switch_btn->y_size+2+switchBtnYpos-menu->y_pos);
break;
}
}
}
}

largestY = MAX(menu->y_size, largestY);
if (menu->fixed_size) {
Expand Down
36 changes: 29 additions & 7 deletions Source/Mod/UI/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ enum itemTypes {
CLICKABLE_BUTTON_ITEM,
MULTITEXT_ITEM,
TEXTINPUT_ITEM,
SLIDE_ITEM
SLIDE_ITEM,
SWITCH_BUTTON_ITEM
};

struct Item {
Expand Down Expand Up @@ -51,8 +52,8 @@ struct ItemClickableButton
uint8_t type;
int id;
int is_clicking;
long color;
long hold_color;
int color;
int hold_color;
char text[32];
int font_size;
int x_size, y_size;
Expand All @@ -64,10 +65,30 @@ struct ItemClickableButton
void (*event)();
};

struct ItemSwitchButton
{
uint8_t type;
int id;
int is_holding;
int* enabled;
int enabled_color;
int hold_color;
int disabled_color;
int color;
int x_size, y_size;
int x_pos, y_pos;
int interval;
time_t last_interaction;
char label[32];
int font_size;
char config_entry[32];
void (*switch_event)();
};

struct ItemTextInput
{
uint8_t type;
long background_color;
int background_color;
int id;
char placeholder[128];
char input[128];
Expand All @@ -86,8 +107,8 @@ struct ItemSlide {
int max_value;
int min_value;
int show_status;
long slider_color;
long background_color;
int slider_color;
int background_color;
int* interact_int;
struct Menu* menu;
};
Expand Down Expand Up @@ -125,8 +146,9 @@ struct ItemText* create_text(struct Menu* menu, int font_size, int color, char*
void add_new_text(struct ItemMultitext* multitext, char* text);
struct ItemMultitext* create_multitext(struct Menu* menu, int color);
struct ItemClickableButton* create_clickable_button(struct Menu* menu, char* text, void (*func)());
struct ItemTextInput* create_text_input(struct Menu* menu, int x_size, int y_size, long backgroundColor, char* placeholder);
struct ItemTextInput* create_text_input(struct Menu* menu, int x_size, int y_size, int backgroundColor, char* placeholder);
struct ItemSlide* create_slide(struct Menu* menu, int minValue, int maxValue, int* interact);
struct ItemSwitchButton* create_switch_button(struct Menu* menu, char* label, char* config_entry, void (*switch_func)(), int* enabled);
void handle_keyboard();
struct Menu* create_menu(int x, int y, int outline, char* title);
void draw_to_buffer(struct Menu* menu, int* copy_buff, int offset_x, int offset_y, int size_x, int size_y);
Expand Down