cleanup: fetch checkboxes rather than pass in

This commit is contained in:
Eric House 2021-02-16 06:14:39 -08:00
parent 88e44d45ff
commit 540cf73496
3 changed files with 20 additions and 30 deletions

View file

@ -103,6 +103,12 @@ EM_JS(void, setButtonText, (const char* id, const char* text), {
document.getElementById(jsid).textContent = jstext; document.getElementById(jsid).textContent = jstext;
}); });
EM_JS(bool, getChecked, (const char* id), {
let jsid = UTF8ToString(id);
let box = document.getElementById(jsid);
return box.checked;
});
static void updateScreen( Globals* globals, bool doSave ); static void updateScreen( Globals* globals, bool doSave );
typedef void (*ConfirmProc)( void* closure, bool confirmed ); typedef void (*ConfirmProc)( void* closure, bool confirmed );
@ -338,7 +344,7 @@ loadSavedGame( Globals* globals )
static void static void
loadAndDraw( Globals* globals, const NetLaunchInfo* invite, loadAndDraw( Globals* globals, const NetLaunchInfo* invite,
bool forceNew, bool p0robot, bool p1robot ) bool forceNew )
{ {
if ( !!globals->util ) { if ( !!globals->util ) {
game_dispose( &globals->game, NULL ); game_dispose( &globals->game, NULL );
@ -360,6 +366,8 @@ loadAndDraw( Globals* globals, const NetLaunchInfo* invite,
} }
if ( !haveGame ) { if ( !haveGame ) {
bool p0robot = getChecked("robot0");
bool p1robot = getChecked("robot1");
globals->gi.serverRole = SERVER_STANDALONE; globals->gi.serverRole = SERVER_STANDALONE;
globals->gi.phoniesAction = PHONIES_WARN; globals->gi.phoniesAction = PHONIES_WARN;
globals->gi.gameID = 0; globals->gi.gameID = 0;
@ -791,40 +799,26 @@ initNoReturn( int argc, const char** argv )
initDeviceGlobals( globals ); initDeviceGlobals( globals );
loadAndDraw( globals, nlip, false, false, true ); loadAndDraw( globals, nlip, false );
emscripten_set_main_loop_arg( looper, globals, -1, 1 ); emscripten_set_main_loop_arg( looper, globals, -1, 1 );
} }
typedef struct _NewgameState {
Globals* globals;
bool p0;
bool p1;
} NewgameState;
static void static void
onNewgameResponse( void* closure, const char* button ) onNewgameResponse( void* closure, bool confirmed )
{ {
NewgameState* ngs = (NewgameState*)closure; Globals* globals = (Globals*)closure;
Globals* globals = ngs->globals; if ( confirmed ) {
if ( 0 == strcmp( button, BUTTON_OK ) ) { loadAndDraw( globals, NULL, true );
loadAndDraw( ngs->globals, NULL, true, ngs->p0, ngs->p1 );
} }
XP_FREE( globals->mpool, ngs );
} }
void void
newgame( void* closure, bool p0, bool p1 ) newgame( void* closure )
{ {
Globals* globals = (Globals*)closure; Globals* globals = (Globals*)closure;
NewgameState* ngs = XP_MALLOC( globals->mpool, sizeof(*ngs) ); const char* query = "Replace the current game?";
ngs->globals = globals; call_confirm( globals, query, onNewgameResponse, globals );
ngs->p0 = p0;
ngs->p1 = p1;
XP_LOGFF( "(args: %d,%d)", p0, p1 );
const char* query = "Are you sure you want to replace the current game?";
const char* buttons[] = { BUTTON_CANCEL, BUTTON_OK, NULL };
call_dialog( query, buttons, onNewgameResponse, ngs );
} }
void void

View file

@ -93,11 +93,11 @@
</div> </div>
<div> <div>
<span>Player 1</span> <span>Player 1</span>
<input type="checkbox" id="player0Checked">Is Robot</input> <input type="checkbox" id="robot0">Is Robot</input>
</div> </div>
<div> <div>
<span>Player 2</span> <span>Player 2</span>
<input type="checkbox" id="player1Checked">Is Robot</input> <input type="checkbox" id="robot1">Is Robot</input>
</div> </div>
<div> <div>
<button type="button" onclick="callNewGame();">New Local Game</button> <button type="button" onclick="callNewGame();">New Local Game</button>

View file

@ -4,11 +4,7 @@ var state = {client: null,
}; };
function callNewGame() { function callNewGame() {
var args = [ state.closure, Module.ccall('newgame', null, ['number'], [state.closure]);
document.getElementById("player0Checked").checked,
document.getElementById("player1Checked").checked,
];
Module.ccall('newgame', null, ['number', 'boolean', 'boolean'], args);
} }
function callButton(obj) { function callButton(obj) {