Add yes-no dialog and general dialog code to present choices to players. Can be used by triggers.

This was SVN commit r15480.
This commit is contained in:
sanderd17 2014-07-02 14:48:22 +00:00
parent c844589f48
commit 404e808ed5
5 changed files with 134 additions and 2 deletions

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<object name="yes-no-dialog"
size="50%-300 50%-200 50%+300 50%+150"
type="image"
hidden="true"
sprite="ModernDialog"
>
<object type="text" style="TitleText" size="50%-96 -16 50%+96 16" name="yes-no-dialog-title">
<translatableAttribute id="caption">Question</translatableAttribute>
</object>
<object name="yes-no-dialog-text" size="20 32 100%-20 100%-70" type="text" style="ModernLabelText" ghost="true">
<translatableAttribute id="caption">Yes or no?</translatableAttribute>
</object>
<object size="10 100%-50 50%-10 100%-22" type="button" style="StoneButton" name="yes-no-dialog-button1">
<translatableAttribute id="caption">Yes</translatableAttribute>
<action on="Press">sendDialogAnswer(this, "yes-no");</action>
</object>
<object size="50%+10 100%-50 100%-10 100%-22" type="button" style="StoneButton" name="yes-no-dialog-button2">
<translatableAttribute id="caption">No</translatableAttribute>
<action on="Press">sendDialogAnswer(this, "yes-no");</action>
</object>
</object>

View File

@ -112,6 +112,11 @@ var g_NotificationsTypes =
"attacker": notification.attacker
});
},
"dialog": function(notification, player)
{
if (player == Engine.GetPlayerID())
openDialog(notification.dialogName, notification.data, player);
},
};
// Notifications
@ -648,3 +653,55 @@ function parseChatCommands(msg, playerAssignments)
if (recurse)
parseChatCommands(msg, playerAssignments);
}
function sendDialogAnswer(guiObject, dialogName)
{
Engine.GetGUIObjectByName(dialogName+"-dialog").hidden = true;
Engine.PostNetworkCommand({
"type": "dialog-answer",
"dialog": dialogName,
"answer": guiObject.name.split("-").pop(),
});
resumeGame();
}
function openDialog(dialogName, data, player)
{
var dialog = Engine.GetGUIObjectByName(dialogName+"-dialog");
if (!dialog)
{
warn("messages.js: Unknow dialog with name "+dialogName);
return;
}
dialog.hidden = false;
for (var objName in data)
{
var obj = Engine.GetGUIObjectByName(dialogName + "-dialog-" + objName);
if (!obj)
{
warn("messages.js: Key '" + objName + "' not found in '" + dialogName + "' dialog.");
continue;
}
for (var key in data[objName])
{
var n = data[objName][key];
if (typeof n == "object" && n.message)
{
var message = n.message;
if (n.translateMessage)
message = translate(message);
var parameters = n.parameters || {};
if (n.translateParameters)
translateObjectKeys(parameters, n.translateParameters);
obj[key] = sprintf(message, parameters);
}
else
obj[key] = n;
}
}
pauseGame();
}

View File

@ -213,6 +213,11 @@
</object>
</object>
<!-- ================================ ================================ -->
<!-- Custom dialogs -->
<!-- ================================ ================================ -->
<include directory="gui/session/dialogs/"/>
<!-- ================================ ================================ -->
<!-- Diplomacy Window -->
<!-- ================================ ================================ -->

View File

@ -60,6 +60,43 @@ Trigger.prototype.IntervalAction = function(data)
this.numberOfTimerTrigger++;
if (this.numberOfTimerTrigger >= this.maxNumberOfTimerTrigger)
this.DisableTrigger("OnInterval", "IntervalAction");
// try out the dialog
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
cmpGUIInterface.PushNotification({
"type": "dialog",
"players": [1,2,3,4,5,6,7,8],
"dialogName": "yes-no",
"data": {
"text": {
"caption": {
"message": markForTranslation("Testing the yes-no dialog. Do you want to say sure or rather not?"),
"translateMessage": true,
},
},
"button1": {
"caption": {
"message": markForTranslation("Sure"),
"translateMessage": true,
},
"tooltip": {
"message": markForTranslation("Say sure"),
"translateMessage": true,
},
},
"button2": {
"caption": {
"message": markForTranslation("Rather not"),
"translateMessage": true,
},
"tooltip": {
"message": markForTranslation("Say rather not"),
"translateMessage": true,
},
},
},
});
};
Trigger.prototype.RangeAction = function(data)
@ -82,9 +119,9 @@ cmpTrigger.RegisterTrigger("OnOwnershipChanged", "OwnershipChangedAction", data)
cmpTrigger.RegisterTrigger("OnPlayerCommand", "PlayerCommandAction", data);
data.delay = 10000; // after 10 seconds
data.interval = 1000; // every second
data.interval = 5000; // every 5 seconds
cmpTrigger.numberOfTimerTrigger = 0;
cmpTrigger.maxNumberOfTimerTrigger = 10; // execute it 10 times maximum
cmpTrigger.maxNumberOfTimerTrigger = 3; // execute it 3 times maximum
cmpTrigger.RegisterTrigger("OnInterval", "IntervalAction", data);
var entities = cmpTrigger.GetTriggerPoints("A");
data = {

View File

@ -648,6 +648,11 @@ var commands = {
}
}
},
"dialog-answer": function(player, cmd, data)
{
// Currently nothing. Triggers can read it anyway, and send this
// message to any component you like.
},
};
/**