kpmpgsmkii/client/src/login.c
2022-01-15 20:01:29 -06:00

167 lines
4 KiB
C

/*
* Kangaroo Punch MultiPlayer Game Server Mark II
* Copyright (C) 2020-2021 Scott Duensing
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "textbox.h"
#include "button.h"
#include "msgbox.h"
#include "runtime.h"
#include "config.h"
#include "comport.h"
#include "network.h"
#include "taglist.h"
#include "timer.h"
#include "task.h"
#include "login.h"
#include "signup.h"
#include "welcome.h"
static WindowT *_winLogin = NULL;
static TextboxT *_txtUser = NULL;
static TextboxT *_txtPass = NULL;
static ButtonT *_btnCancel = NULL;
static ButtonT *_btnSignUp = NULL;
static ButtonT *_btnLogin = NULL;
static void btnCancelClick(WidgetT *widget);
static void btnSignUpClick(WidgetT *widget);
static void btnLoginClick(WidgetT *widget);
static void btnMsgBoxCancel(MsgBoxButtonT button);
static void setButtons(uint8_t enabled);
static void taskDisconnect(void *data);
static void btnCancelClick(WidgetT *widget) {
(void)widget;
// ***TODO*** This should disable the entire dialog instead of just the buttons. Need to finish the Enable GUI code first.
setButtons(0);
msgBoxTwo("Cancel?", MSGBOX_ICON_QUESTION, "Cancel login?\n \nThis will disconnect you from the server.", "Okay", btnMsgBoxCancel, "Cancel", btnMsgBoxCancel);
}
static void btnSignUpClick(WidgetT *widget) {
(void)widget;
guiDelete(D(_winLogin));
taskCreate(taskSignUp, NULL);
}
static void btnLoginClick(WidgetT *widget) {
(void)widget;
}
static void btnMsgBoxCancel(MsgBoxButtonT button) {
if (button == MSGBOX_BUTTON_ONE) {
guiDelete(D(_winLogin));
taskCreate(taskDisconnect, taskWelcome);
} else {
setButtons(1);
}
}
static void setButtons(uint8_t enabled) {
widgetEnableSet(W(_btnCancel), enabled);
widgetEnableSet(W(_btnSignUp), enabled);
widgetEnableSet(W(_btnLogin), enabled);
}
static void taskDisconnect(void *nextTask) {
PacketEncodeDataT encoded = { 0 };
int16_t timeout = 2;
// Tell server we're disconnecting.
encoded.packetType = PACKET_TYPE_CLIENT_SHUTDOWN;
encoded.control = PACKET_CONTROL_DAT;
encoded.channel = 0;
encoded.encrypt = 0;
packetEncode(__packetThreadData, &encoded, NULL, 0);
packetSend(__packetThreadData, &encoded);
// Snooze a bit for the packet to send.
while (timeout > 0) {
taskYield();
if (__timerQuarterSecondTick) timeout--;
}
// Shut down packet processing & COM port.
netShutdown();
comClose(__configData.serialCom - 1);
if (nextTask) taskCreate(taskWelcome, nextTask);
}
void taskLogin(void *data) {
(void)data;
TagItemT uiLogin[] = {
T_START,
T_WINDOW, O(_winLogin),
T_TITLE, P("Login"),
T_WIDTH, 300, T_HEIGHT, 155,
T_TEXTBOX, O(_txtUser),
T_TITLE, P("User Name:"),
T_X, 42, T_Y, 10,
T_WIDTH, 200,
T_LENGTH, shget(__runtimeData.integers, "maxUser"),
T_TEXTBOX, T_DONE,
T_TEXTBOX, O(_txtPass),
T_TITLE, P(" Password:"),
T_X, 42, T_Y, 40,
T_WIDTH, 200,
T_LENGTH, shget(__runtimeData.integers, "maxPass"),
T_MASK, '*',
T_TEXTBOX, T_DONE,
T_BUTTON, O(_btnCancel),
T_TITLE, P("Cancel"),
T_X, 25, T_Y, 85,
T_CLICK, P(btnCancelClick),
T_BUTTON, T_DONE,
T_BUTTON, O(_btnSignUp),
T_TITLE, P("Sign Up"),
T_X, 110, T_Y, 85,
T_CLICK, P(btnSignUpClick),
T_BUTTON, T_DONE,
T_BUTTON, O(_btnLogin),
T_TITLE, P("Login"),
T_X, 199, T_Y, 85,
T_CLICK, P(btnLoginClick),
T_BUTTON, T_DONE,
T_WINDOW, T_DONE,
T_END
};
tagListRun(uiLogin);
}