Add lobby refresh button

This commit is contained in:
MysterD 2023-04-13 18:27:18 -07:00
parent fe2bb479ca
commit 3f38c740d8
8 changed files with 59 additions and 10 deletions

View file

@ -331,5 +331,6 @@ LANGUAGE = "LANGUAGE"
PUBLIC_LOBBIES = "PUBLIC LOBBIES"
PRIVATE_LOBBIES = "PRIVATE LOBBIES"
REFRESH = "Refresh"
REFRESHING = "Refreshing..."
ENTER_PASSWORD = "Enter the private lobby's password:"
SEARCH = "Search"

View file

@ -31,6 +31,7 @@ typedef struct {
void (*OnLobbyJoined)(uint64_t aLobbyId, uint64_t aUserId, uint64_t aOwnerId);
void (*OnLobbyLeft)(uint64_t aLobbyId, uint64_t aUserId);
void (*OnLobbyListGot)(uint64_t aLobbyId, uint64_t aOwnerId, uint16_t aConnections, uint16_t aMaxConnections, const char* aGame, const char* aVersion, const char* aHostName, const char* aMode);
void (*OnLobbyListFinish)(void);
void (*OnReceive)(uint64_t aFromUserId, const uint8_t* aData, uint64_t aSize);
void (*OnError)(enum MPacketErrorNumber aErrorNumber);
void (*OnPeerConnected)(uint64_t aPeerId);

Binary file not shown.

View file

@ -393,6 +393,19 @@ void djui_base_destroy(struct DjuiBase* base) {
base->destroy(base);
}
void djui_base_destroy_children(struct DjuiBase* base) {
// destroy all children and our linked list
struct DjuiBaseChild* child = base->child;
while (child != NULL) {
struct DjuiBaseChild* nextChild = child->next;
child->base->parent = NULL;
djui_base_destroy(child->base);
free(child);
child = nextChild;
}
base->child = NULL;
}
void djui_base_init(struct DjuiBase* parent, struct DjuiBase* base, bool (*render)(struct DjuiBase*), void (*destroy)(struct DjuiBase*)) {
memset(base, 0, sizeof(struct DjuiBase));
base->parent = parent;

View file

@ -69,4 +69,5 @@ void djui_base_compute_tree(struct DjuiBase* base);
bool djui_base_render(struct DjuiBase* base);
void djui_base_destroy(struct DjuiBase* base);
void djui_base_destroy_children(struct DjuiBase* base);
void djui_base_init(struct DjuiBase* parent, struct DjuiBase* base, bool (*render)(struct DjuiBase*), void (*destroy)(struct DjuiBase*));

View file

@ -15,6 +15,8 @@
#ifdef COOPNET
static struct DjuiFlowLayout* sLobbyLayout = NULL;
static struct DjuiButton* sRefreshButton = NULL;
static char* sPassword = NULL;
void djui_panel_join_lobby(struct DjuiBase* caller) {
gCoopNetDesiredLobby = (uint64_t)caller->tag;
@ -25,6 +27,8 @@ void djui_panel_join_lobby(struct DjuiBase* caller) {
}
void djui_panel_join_query(uint64_t aLobbyId, UNUSED uint64_t aOwnerId, uint16_t aConnections, uint16_t aMaxConnections, UNUSED const char* aGame, UNUSED const char* aVersion, const char* aHostName, const char* aMode) {
if (!sLobbyLayout) { return; }
char playerText[64];
snprintf(playerText, 63, "%u/%u", aConnections, aMaxConnections);
@ -33,16 +37,36 @@ void djui_panel_join_query(uint64_t aLobbyId, UNUSED uint64_t aOwnerId, uint16_t
entry->base.tag = (s64)aLobbyId;
}
void djui_panel_join_lobbies_create(struct DjuiBase* caller, const char* password) {
void djui_panel_join_query_finish(void) {
if (!sRefreshButton) { return; }
djui_text_set_text(sRefreshButton->text, DLANG(LOBBIES, REFRESH));
djui_base_set_enabled(&sRefreshButton->base, true);
}
void djui_panel_join_lobbies_on_destroy(UNUSED struct DjuiBase* caller) {
if (sPassword) { free(sPassword); }
sPassword = NULL;
sRefreshButton = NULL;
sLobbyLayout = NULL;
}
void djui_panel_join_lobbies_refresh(UNUSED struct DjuiBase* caller) {
djui_base_destroy_children(&sLobbyLayout->base);
djui_text_set_text(sRefreshButton->text, DLANG(LOBBIES, REFRESHING));
djui_base_set_enabled(&sRefreshButton->base, false);
ns_coopnet_query(djui_panel_join_query, djui_panel_join_query_finish, sPassword);
}
void djui_panel_join_lobbies_create(struct DjuiBase* caller, const char* password) {
if (sPassword) { free(sPassword); sPassword = NULL; }
sPassword = strdup(password);
bool private = (strlen(password) > 0);
ns_coopnet_query(djui_panel_join_query, password);
ns_coopnet_query(djui_panel_join_query, djui_panel_join_query_finish, password);
struct DjuiBase* defaultBase = NULL;
struct DjuiThreePanel* panel = djui_panel_menu_create(private ? DLANG(LOBBIES, PRIVATE_LOBBIES) : DLANG(LOBBIES, PUBLIC_LOBBIES));
struct DjuiBase* body = djui_three_panel_get_body(panel);
{
struct DjuiPaginated* paginated = djui_paginated_create(body, 8);
sLobbyLayout = paginated->layout;
djui_flow_layout_set_margin(sLobbyLayout, 4);
@ -65,14 +89,16 @@ void djui_panel_join_lobbies_create(struct DjuiBase* caller, const char* passwor
djui_base_set_size(&button1->base, 0.485f, 64);
djui_base_set_alignment(&button1->base, DJUI_HALIGN_LEFT, DJUI_VALIGN_TOP);
struct DjuiButton* button2 = djui_button_create(&rect2->base, DLANG(LOBBIES, REFRESH), DJUI_BUTTON_STYLE_NORMAL, NULL);
djui_base_set_size(&button2->base, 0.485f, 64);
djui_base_set_alignment(&button2->base, DJUI_HALIGN_RIGHT, DJUI_VALIGN_TOP);
defaultBase = &button2->base;
sRefreshButton = djui_button_create(&rect2->base, DLANG(LOBBIES, REFRESHING), DJUI_BUTTON_STYLE_NORMAL, djui_panel_join_lobbies_refresh);
djui_base_set_size(&sRefreshButton->base, 0.485f, 64);
djui_base_set_alignment(&sRefreshButton->base, DJUI_HALIGN_RIGHT, DJUI_VALIGN_TOP);
djui_base_set_enabled(&sRefreshButton->base, false);
defaultBase = &sRefreshButton->base;
}
}
djui_panel_add(caller, panel, defaultBase);
struct DjuiPanel* p = djui_panel_add(caller, panel, defaultBase);
p->on_panel_destroy = djui_panel_join_lobbies_on_destroy;
}
#endif

View file

@ -21,8 +21,9 @@ static enum NetworkType sNetworkType;
static CoopNetRc coopnet_initialize(void);
void ns_coopnet_query(QueryCallbackPtr callback, const char* password) {
void ns_coopnet_query(QueryCallbackPtr callback, QueryFinishCallbackPtr finishCallback, const char* password) {
gCoopNetCallbacks.OnLobbyListGot = callback;
gCoopNetCallbacks.OnLobbyListFinish = finishCallback;
if (coopnet_initialize() != COOPNET_OK) { return; }
coopnet_lobby_list_get(CN_GAME_STR, password);
}
@ -35,6 +36,8 @@ static void coopnet_on_disconnected(void) {
LOG_INFO("Coopnet shutdown!");
djui_popup_create(DLANG(NOTIF, COOPNET_DISCONNECTED), 2);
coopnet_shutdown();
gCoopNetCallbacks.OnLobbyListGot = NULL;
gCoopNetCallbacks.OnLobbyListFinish = NULL;
}
static void coopnet_on_peer_disconnected(uint64_t peerId) {
@ -121,6 +124,8 @@ static int ns_coopnet_network_send(u8 localIndex, void* address, u8* data, u16 d
static void ns_coopnet_shutdown(void) {
LOG_INFO("Coopnet shutdown!");
coopnet_shutdown();
gCoopNetCallbacks.OnLobbyListGot = NULL;
gCoopNetCallbacks.OnLobbyListFinish = NULL;
}
static CoopNetRc coopnet_initialize(void) {

View file

@ -3,10 +3,12 @@
#ifdef COOPNET
typedef void (*QueryCallbackPtr)(uint64_t aLobbyId, uint64_t aOwnerId, uint16_t aConnections, uint16_t aMaxConnections, const char* aGame, const char* aVersion, const char* aHostName, const char* aMode);
typedef void (*QueryFinishCallbackPtr)(void);
extern struct NetworkSystem gNetworkSystemCoopNet;
extern uint64_t gCoopNetDesiredLobby;
void ns_coopnet_query(QueryCallbackPtr callback, const char* password);
void ns_coopnet_query(QueryCallbackPtr callback, QueryFinishCallbackPtr finishCallback, const char* password);
bool ns_coopnet_is_connected(void);
void ns_coopnet_update(void);