get trade working

This commit is contained in:
Eric House 2021-02-10 15:46:52 -08:00
parent bf5db08357
commit 02a193902f
4 changed files with 48 additions and 21 deletions

View file

@ -79,6 +79,12 @@ EM_JS(void, jscallback_set, (JSCallback proc, void* closure, int inMS), {
setTimeout( timerproc, inMS, closure );
});
EM_JS(void, setButtonText, (const char* id, const char* text), {
let jsid = UTF8ToString(id);
let jstext = UTF8ToString(text);
document.getElementById(jsid).textContent = jstext;
});
static void updateScreen( Globals* globals, bool doSave );
static XP_S16
@ -115,6 +121,14 @@ send_msg( XWEnv xwe, const XP_U8* buf, XP_U16 len,
return nSent;
}
static void
updateTradeButton( Globals* globals )
{
XP_Bool inTrade = board_inTrade( globals->game.board, NULL );
const char* text = inTrade ? "Cancel trade" : "Trade";
setButtonText( "trade", text );
}
static void
initDeviceGlobals( Globals* globals )
{
@ -242,6 +256,7 @@ loadSavedGame( Globals* globals )
if ( loaded ) {
updateScreen( globals, false );
}
updateTradeButton( globals );
}
stream_destroy( stream, NULL );
return loaded;
@ -412,6 +427,7 @@ main_query( Globals* globals, const XP_UCHAR* query, QueryProc proc, void* closu
{
bool ok = call_confirm( query );
(*proc)( closure, ok );
updateTradeButton( globals );
}
void
@ -526,8 +542,10 @@ button( void* closure, const char* msg )
} else if ( 0 == strcmp(msg, "hintup") ) {
draw = board_requestHint( board, NULL, XP_FALSE, &redo );
} else if ( 0 == strcmp(msg, "trade") ) {
// draw = board_beginTrade( board, NULL );
call_alert("not implemented");
draw = board_inTrade( board, NULL )
? board_endTrade( board )
: board_beginTrade( board, NULL );
updateTradeButton( globals );
} else if ( 0 == strcmp(msg, "commit") ) {
draw = board_commitTurn( board, NULL, XP_FALSE, XP_FALSE, NULL );
} else if ( 0 == strcmp(msg, "flip") ) {

View file

@ -58,13 +58,13 @@
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
</div>
<div>
<button onclick="callButton('hintdown');">Prev Hint</button>
<button onclick="callButton('hintup');">Next Hint</button>
<button onclick="callButton('trade');">Trade</button>
<button onclick="callButton('commit');">Commit</button>
<button onclick="callButton('flip');">Flip</button>
<button onclick="callButton('redo');">Un/Redo</button>
<button onclick="callButton('vals');">Vals</button>
<button id="hintdown", onclick="callButton(this);">Prev Hint</button>
<button id="hintup", onclick="callButton(this);">Next Hint</button>
<button id="trade", onclick="callButton(this);">Trade</button>
<button id="commit", onclick="callButton(this);">Commit</button>
<button id="flip", onclick="callButton(this);">Flip</button>
<button id="redo", onclick="callButton(this);">Un/Redo</button>
<button id="vals", onclick="callButton(this);">Vals</button>
</div>
<div>
<span>Player 1</span>
@ -74,10 +74,6 @@
<span>Player 2</span>
<input type="checkbox" id="player1Checked">Is Robot</input>
</div>
<div>
<span>Local player name:</span>
<input type="textarea" id="playerName">Player 1</input>
</div>
<div>
<button type="button" onclick="callNewGame();">New Local Game</button>
</div>
@ -193,8 +189,8 @@
Module.ccall('newgame', null, ['number', 'boolean', 'boolean'], args);
}
function callButton(name) {
Module.ccall('button', null, ['number', 'string'], [state.closure, name]);
function callButton(obj) {
Module.ccall('button', null, ['number', 'string'], [state.closure, obj.id]);
}
function onHaveDevID(closure, devid) {

View file

@ -176,7 +176,7 @@ imgInRect( WasmDrawCtx* wdctx, SDL_Surface* img, const XP_Rect* rect )
static void
drawTile( WasmDrawCtx* wdctx, const XP_UCHAR* face, int val,
int owner, const XP_Rect* rect )
int owner, const XP_Rect* rect, CellFlags flags )
{
clearRect( wdctx, rect );
frameRect( wdctx, rect );
@ -196,6 +196,14 @@ drawTile( WasmDrawCtx* wdctx, const XP_UCHAR* face, int val,
XP_SNPRINTF( buf, VSIZE(buf), "%d", val );
textInRect( wdctx, buf, &tmp, &sPlayerColors[owner] );
}
if ( 0 != (flags & (CELL_PENDING|CELL_RECENT)) ) {
XP_Rect tmp = *rect;
for ( int ii = 0; ii < 3; ++ii ) {
insetRect( &tmp, 1, 1 );
frameRect( wdctx, &tmp );
}
}
}
static void
@ -463,7 +471,7 @@ wasm_draw_drawTile( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
XP_U16 val, CellFlags flags )
{
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
drawTile( wdctx, text, val, wdctx->trayOwner, rect );
drawTile( wdctx, text, val, wdctx->trayOwner, rect, flags );
return XP_TRUE;
}
@ -479,7 +487,7 @@ wasm_draw_drawTileMidDrag( DrawCtx* dctx, XWEnv xwe,
CellFlags flags )
{
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
drawTile( wdctx, text, val, wdctx->trayOwner, rect );
drawTile( wdctx, text, val, wdctx->trayOwner, rect, flags );
return XP_TRUE;
}
#endif
@ -489,7 +497,7 @@ wasm_draw_drawTileBack( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
CellFlags flags )
{
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
drawTile( wdctx, "?", -1, wdctx->trayOwner, rect );
drawTile( wdctx, "?", -1, wdctx->trayOwner, rect, flags );
return XP_TRUE;
}

View file

@ -235,7 +235,12 @@ static void
wasm_util_notifyTrade( XW_UtilCtxt* uc, XWEnv xwe, const XP_UCHAR** tiles,
XP_U16 nTiles )
{
LOG_FUNC();
WasmUtilCtx* wuctxt = (WasmUtilCtx*)uc;
Globals* globals = (Globals*)wuctxt->closure;
XP_UCHAR buf[128];
XP_SNPRINTF( buf, sizeof(buf),
"Are you sure you want to trade the %d selected tiles?", nTiles );
main_query( globals, buf, query_proc_notifyMove, uc );
}
static void
@ -519,7 +524,7 @@ wasm_util_make( MPFORMAL CurGameInfo* gi, XW_DUtilCtxt* dctxt, void* closure )
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_notifyMove, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_notifyTrade, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_notifyPickTileBlank, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_notifyPickTileBlank, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_informNeedPickTiles, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_informNeedPassword, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_trayHiddenChange, wasm );