From 02a193902f25f48be434db387693e80271bd6000 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 10 Feb 2021 15:46:52 -0800 Subject: [PATCH] get trade working --- xwords4/wasm/main.c | 22 ++++++++++++++++++++-- xwords4/wasm/shell_minimal.html | 22 +++++++++------------- xwords4/wasm/wasmdraw.c | 16 ++++++++++++---- xwords4/wasm/wasmutil.c | 9 +++++++-- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/xwords4/wasm/main.c b/xwords4/wasm/main.c index 2343acd04..126124145 100644 --- a/xwords4/wasm/main.c +++ b/xwords4/wasm/main.c @@ -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") ) { diff --git a/xwords4/wasm/shell_minimal.html b/xwords4/wasm/shell_minimal.html index 6d2f59905..1a7251c8a 100644 --- a/xwords4/wasm/shell_minimal.html +++ b/xwords4/wasm/shell_minimal.html @@ -58,13 +58,13 @@
- - - - - - - + + + + + + +
Player 1 @@ -74,10 +74,6 @@ Player 2 Is Robot
-
- Local player name: - Player 1 -
@@ -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) { diff --git a/xwords4/wasm/wasmdraw.c b/xwords4/wasm/wasmdraw.c index 2c90568a3..98da3a2c2 100644 --- a/xwords4/wasm/wasmdraw.c +++ b/xwords4/wasm/wasmdraw.c @@ -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; } diff --git a/xwords4/wasm/wasmutil.c b/xwords4/wasm/wasmutil.c index 2713eee6b..8c4ef74e0 100644 --- a/xwords4/wasm/wasmutil.c +++ b/xwords4/wasm/wasmutil.c @@ -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 );