2021-06-18 11:44:54 +02:00
{.section-title.accent.text-primary}
2021-06-22 15:18:41 +02:00
# API UI functions Reference
2021-06-18 11:44:54 +02:00
### Opening a popup
In order to open a popup window, you must first define the position of the popup on your map.
You can position this popup by using a "rectangle" object in Tiled that you will place on an "object" layer.
< div class = "row" >
< div class = "col" >
2021-08-20 16:56:03 +02:00
< img src = "images/screen_popup_tiled.png" class = "figure-img img-fluid rounded" alt = "" / >
2021-06-18 11:44:54 +02:00
< / div >
< div class = "col" >
2021-08-20 16:56:03 +02:00
< img src = "images/screen_popup_in_game.png" class = "figure-img img-fluid rounded" alt = "" / >
2021-06-18 11:44:54 +02:00
< / div >
< / div >
```
WA.ui.openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup
```
* **targetObject** : the name of the rectangle object defined in Tiled.
* **message** : the message to display in the popup.
* **buttons** : an array of action buttons defined underneath the popup.
Action buttons are `ButtonDescriptor` objects containing these properties.
* **label (_string_)** : The label of the button.
* **className (_string_)** : The visual type of the button. Can be one of "normal", "primary", "success", "warning", "error", "disabled".
* **callback (_(popup: Popup)=>void_)** : Callback called when the button is pressed.
Please note that `openPopup` returns an object of the `Popup` class. Also, the callback called when a button is clicked is passed a `Popup` object.
The `Popup` class that represents an open popup contains a single method: `close()` . This will obviously close the popup when called.
```javascript
class Popup {
/**
* Closes the popup
*/
close() {};
}
```
Example:
```javascript
let helloWorldPopup;
// Open the popup when we enter a given zone
2021-11-03 19:24:24 +01:00
helloWorldPopup = WA.room.onEnterLayer("myZone").subscribe(() => {
2021-06-18 11:44:54 +02:00
WA.ui.openPopup("popupRectangle", 'Hello world!', [{
label: "Close",
className: "primary",
callback: (popup) => {
// Close the popup when the "Close" button is pressed.
popup.close();
}
2021-11-03 19:24:24 +01:00
}]);
});
2021-06-18 11:44:54 +02:00
// Close the popup when we leave the zone.
2021-11-03 19:24:24 +01:00
WA.room.onLeaveLayer("myZone").subscribe(() => {
2021-06-18 11:44:54 +02:00
helloWorldPopup.close();
2021-11-03 19:24:24 +01:00
})
2021-06-18 11:44:54 +02:00
```
2021-06-21 18:22:31 +02:00
2021-06-23 11:32:11 +02:00
### Add custom menu
2021-06-21 18:22:31 +02:00
```
2021-08-27 16:28:59 +02:00
WA.ui.registerMenuCommand(commandDescriptor: string, options: MenuOptions): Menu
2021-08-27 14:49:57 +02:00
```
Add a custom menu item containing the text `commandDescriptor` in the navbar of the menu.
2021-08-27 16:28:59 +02:00
`options` attribute accepts an object with three properties :
- `callback : (commandDescriptor: string) => void` : A click on the custom menu will trigger the `callback` .
- `iframe: string` : A click on the custom menu will open the `iframe` inside the menu.
- `allowApi?: boolean` : Allow the iframe of the custom menu to use the Scripting API.
Important : `options` accepts only `callback` or `iframe` not both.
2021-08-27 14:49:57 +02:00
2021-06-23 11:32:11 +02:00
Custom menu exist only until the map is unloaded, or you leave the iframe zone of the script.
2021-06-21 18:22:31 +02:00
2021-08-27 14:49:57 +02:00
< div class = "row" >
< div class = "col" >
< img src = "images/custom-menu-navbar.png" class = "figure-img img-fluid rounded" alt = "" / >
< / div >
< div class = "col" >
< img src = "images/custom-menu-iframe.png" class = "figure-img img-fluid rounded" alt = "" / >
< / div >
< / div >
2021-06-21 18:22:31 +02:00
2021-08-27 14:49:57 +02:00
Example:
2021-06-21 18:22:31 +02:00
```javascript
2021-08-27 16:28:59 +02:00
const menu = WA.ui.registerMenuCommand('menu test',
{
callback: () => {
WA.chat.sendChatMessage('test');
}
})
2021-06-21 18:22:31 +02:00
2021-08-27 16:28:59 +02:00
// Some time later, if you want to remove the menu:
menu.remove();
2021-06-21 18:22:31 +02:00
```
2021-08-27 14:49:57 +02:00
Please note that `registerMenuCommand` returns an object of the `Menu` class.
The `Menu` class contains a single method: `remove(): void` . This will obviously remove the menu when called.
```javascript
class Menu {
/**
* Remove the menu
*/
remove() {};
}
```
2021-06-23 17:32:32 +02:00
### Awaiting User Confirmation (with space bar)
2021-08-04 19:31:17 +02:00
```
2021-08-05 12:02:00 +02:00
WA.ui.displayActionMessage({
message: string,
callback: () => void,
type?: "message"|"warning",
}): ActionMessage
2021-06-23 17:32:32 +02:00
```
Displays a message at the bottom of the screen (that will disappear when space bar is pressed).
2021-08-04 19:31:17 +02:00
< div class = "col" >
2021-08-20 16:56:03 +02:00
< img src = "images/trigger_message.png" class = "figure-img img-fluid rounded" alt = "" / >
2021-08-04 19:31:17 +02:00
< / div >
2021-06-23 17:32:32 +02:00
Example:
```javascript
2021-08-05 12:02:00 +02:00
const triggerMessage = WA.ui.displayActionMessage({
message: "press 'space' to confirm",
callback: () => {
WA.chat.sendChatMessage("confirmed", "trigger message logic")
}
2021-06-28 11:17:22 +02:00
});
2021-08-04 19:31:17 +02:00
setTimeout(() => {
// later
triggerMessage.remove();
}, 1000)
```
Please note that `displayActionMessage` returns an object of the `ActionMessage` class.
The `ActionMessage` class contains a single method: `remove(): Promise<void>` . This will obviously remove the message when called.
```javascript
class ActionMessage {
/**
* Hides the message
*/
remove() {};
}
```
2022-03-14 10:15:10 +01:00
### Adding custom ActionsMenu Action
When clicking on other player's WOKA, the contextual menu (we call it ActionsMenu) is displayed with some default Actions. It is possible to add custom actions right when player is clicked:
< div class = "col" >
< img src = "images/actions-menu-1.png" class = "figure-img img-fluid rounded" alt = "" / >
< / div >
To do that, we need to listen for the `onRemotePlayerClicked` stream and make use of the `remotePlayer` object that is passed by as a payload.
```javascript
WA.ui.onRemotePlayerClicked.subscribe((remotePlayer) => {
remotePlayer.addAction('Ask to tell a joke', () => {
console.log('I am NOT telling you a joke!');
});
}
```
`remotePlayer.addAction(actionName, callback)` returns an Action object, which can remove itself from ActionsMenu:
```javascript
const action = remotePlayer.addAction('This will disappear!', () => {
console.log('You managed to click me!');
});
setTimeout(
() => {
action.remove();
},
1000,
);
```