Permalink
Browse files

Improve reticule control from scripts.

setReticuleButton() now takes an optional
extra parameter to set a callback function
that is called when the button is pressed.
  • Loading branch information...
perim committed Nov 20, 2017
1 parent 22a6919 commit 2ce3d680b4914b2990567970a4292d4e527f199b
Showing with 73 additions and 15 deletions.
  1. +12 −7 data/mp/multiplay/skirmish/rules.js
  2. +41 −6 src/hci.cpp
  3. +2 −1 src/hci.h
  4. +9 −0 src/qtscript.cpp
  5. +3 −0 src/qtscript.h
  6. +6 −1 src/qtscriptfuncs.cpp
@@ -12,6 +12,17 @@ var lastHitTime = 0;
var cheatmode = false;
var maxOilDrums = 0;
function setMainReticule()
{
setReticuleButton(0, _("Close"), "image_cancel_up.png", "image_cancel_down.png");
setReticuleButton(1, _("Manufacture (F1)"), "image_manufacture_up.png", "image_manufacture_down.png");
setReticuleButton(2, _("Research (F2)"), "image_research_up.png", "image_research_down.png");
setReticuleButton(3, _("Build (F3)"), "image_build_up.png", "image_build_down.png");
setReticuleButton(4, _("Design (F4)"), "image_design_up.png", "image_design_down.png");
setReticuleButton(5, _("Intelligence Display (F5)"), "image_intelmap_up.png", "image_intelmap_down.png");
setReticuleButton(6, _("Commanders (F6)"), "image_commanddroid_up.png", "image_commanddroid_down.png");
}
function setupGame()
{
if (tilesetType == "URBAN")
@@ -30,13 +41,7 @@ function setupGame()
{
setSky("texpages/page-25-sky-urban.png", 0.5, 10000.0);
}
setReticuleButton(0, _("Close"), "image_cancel_up.png", "image_cancel_down.png");
setReticuleButton(1, _("Manufacture (F1)"), "image_manufacture_up.png", "image_manufacture_down.png");
setReticuleButton(2, _("Research (F2)"), "image_research_up.png", "image_research_down.png");
setReticuleButton(3, _("Build (F3)"), "image_build_up.png", "image_build_down.png");
setReticuleButton(4, _("Design (F4)"), "image_design_up.png", "image_design_down.png");
setReticuleButton(5, _("Intelligence Display (F5)"), "image_intelmap_up.png", "image_intelmap_down.png");
setReticuleButton(6, _("Commanders (F6)"), "image_commanddroid_up.png", "image_commanddroid_down.png");
setMainReticule();
showInterface();
}
View
@@ -376,23 +376,42 @@ static SDWORD intNumSelectedDroids(UDWORD droidType);
struct RETBUTSTATS
{
int downTime;
int downTime = 0;
QString filename;
QString filenameDown;
QString tip;
int flashing;
int flashTime;
QString func;
int flashing = 0;
int flashTime = 0;
W_BUTTON *button = nullptr;
QScriptEngine *engine = nullptr;
};
static RETBUTSTATS retbutstats[NUMRETBUTS];
void setReticuleStats(int ButId, QString tip, QString filename, QString filenameDown)
void setReticuleStats(int ButId, QString tip, QString filename, QString filenameDown, QString func, QScriptEngine *engine)
{
retbutstats[ButId].tip = std::move(tip);
retbutstats[ButId].filename = std::move(filename);
retbutstats[ButId].filenameDown = std::move(filenameDown);
retbutstats[ButId].downTime = 0;
retbutstats[ButId].flashing = 0;
retbutstats[ButId].flashTime = 0;
retbutstats[ButId].func = std::move(func);
retbutstats[ButId].engine = engine;
if (!retbutstats[ButId].button) // not quite set up yet
{
return;
}
retbutstats[ButId].button->setTip(retbutstats[ButId].tip);
if (retbutstats[ButId].filename.isEmpty())
{
retbutstats[ButId].button->setState(WBUT_DISABLE);
}
else
{
retbutstats[ButId].button->setState(0);
}
}
static void intDisplayReticuleButton(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset)
@@ -407,7 +426,7 @@ static void intDisplayReticuleButton(WIDGET *psWidget, UDWORD xOffset, UDWORD yO
ASSERT_OR_RETURN(, psWidget->type == WIDG_BUTTON, "Not a button");
W_BUTTON *psButton = (W_BUTTON *)psWidget;
if (psButton->state & WBUT_DISABLE)
if ((psButton->state & WBUT_DISABLE) || retbutstats[psWidget->UserData].filename.isEmpty())
{
iV_DrawImage(IntImages, IMAGE_RETICULE_GREY, x, y);
return;
@@ -485,7 +504,8 @@ static void setReticuleBut(int ButId)
retbutstats[ButId].downTime = 0;
retbutstats[ButId].flashing = 0;
retbutstats[ButId].flashTime = 0;
if (!widgAddButton(psWScreen, &sButInit))
retbutstats[ButId].button = widgAddButton(psWScreen, &sButInit);
if (!retbutstats[ButId].button)
{
debug(LOG_ERROR, "Failed to add reticule button");
}
@@ -1042,6 +1062,14 @@ void hciUpdate()
}
}
static void reticuleCallback(int retbut)
{
if (!retbutstats[retbut].func.isEmpty())
{
namedScriptCallback(retbutstats[retbut].engine, retbutstats[retbut].func, selectedPlayer);
}
}
/* Run the widgets for the in game interface */
INT_RETVAL intRunWidgets()
{
@@ -1199,24 +1227,28 @@ INT_RETVAL intRunWidgets()
intResetScreen(false);
widgSetButtonState(psWScreen, IDRET_COMMAND, WBUT_CLICKLOCK);
intAddCommand(nullptr);
reticuleCallback(RETBUT_COMMAND);
break;
case IDRET_BUILD:
intResetScreen(true);
widgSetButtonState(psWScreen, IDRET_BUILD, WBUT_CLICKLOCK);
intAddBuild(nullptr);
reticuleCallback(RETBUT_BUILD);
break;
case IDRET_MANUFACTURE:
intResetScreen(true);
widgSetButtonState(psWScreen, IDRET_MANUFACTURE, WBUT_CLICKLOCK);
intAddManufacture(nullptr);
reticuleCallback(RETBUT_FACTORY);
break;
case IDRET_RESEARCH:
intResetScreen(true);
widgSetButtonState(psWScreen, IDRET_RESEARCH, WBUT_CLICKLOCK);
(void)intAddResearch(nullptr);
reticuleCallback(RETBUT_RESEARCH);
break;
case IDRET_INTEL_MAP:
@@ -1232,6 +1264,7 @@ INT_RETVAL intRunWidgets()
psCurrentMsg = nullptr;
}
addIntelScreen();
reticuleCallback(RETBUT_INTELMAP);
break;
case IDRET_DESIGN:
@@ -1241,11 +1274,13 @@ INT_RETVAL intRunWidgets()
intShowPowerBar();
intAddDesign(false);
intMode = INT_DESIGN;
reticuleCallback(RETBUT_DESIGN);
break;
case IDRET_CANCEL:
intResetScreen(false);
psCurrentMsg = nullptr;
reticuleCallback(RETBUT_CANCEL);
break;
/*Transporter button pressed - OFFWORLD Mission Maps ONLY *********/
View
@@ -38,6 +38,7 @@ struct PROXIMITY_DISPLAY;
struct STRUCTURE;
struct W_SCREEN;
struct iIMDShape;
struct QScriptEngine;
#define BASE_COORDS_X (640)
#define BASE_COORDS_Y (460)
@@ -284,7 +285,7 @@ void intDisplayWidgets();
bool intAddReticule();
bool intAddPower();
void intRemoveReticule();
void setReticuleStats(int ButId, QString tip, QString filename, QString filenameDown);
void setReticuleStats(int ButId, QString tip, QString filename, QString filenameDown, QString func, QScriptEngine *engine);
/* Set the map view point to the world coordinates x,y */
void intSetMapPos(UDWORD x, UDWORD y);
View
@@ -1515,3 +1515,12 @@ bool triggerEventKeyPressed(int meta, int key)
}
return true;
}
bool namedScriptCallback(QScriptEngine *engine, const QString& func, int player)
{
ASSERT(scriptsReady, "Scripts not initialized yet");
QScriptValueList args;
args += QScriptValue(player);
callFunction(engine, func, args);
return true;
}
View
@@ -92,6 +92,9 @@ void jsAutogameSpecific(const QString &name, int player);
/// Run-time code from user
bool jsEvaluate(QScriptEngine *engine, const QString &text);
/// Run a named script callback
bool namedScriptCallback(QScriptEngine *engine, const QString& func, int player);
// ----------------------------------------------
// Event functions
View
@@ -3124,7 +3124,12 @@ static QScriptValue js_setReticuleButton(QScriptContext *context, QScriptEngine
QString tip = context->argument(1).toString();
QString file = context->argument(2).toString();
QString fileDown = context->argument(3).toString();
setReticuleStats(button, tip, file, fileDown);
QString func;
if (context->argumentCount() > 4)
{
func = context->argument(4).toString();
}
setReticuleStats(button, tip, file, fileDown, func, engine);
return QScriptValue();
}

0 comments on commit 2ce3d68

Please sign in to comment.