Skip to content

Commit

Permalink
Replace callback method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico8340 committed Feb 4, 2024
1 parent f0183ce commit 00a47dc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 48 deletions.
84 changes: 46 additions & 38 deletions [gameplay]/dialogs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,72 +47,80 @@ This function creates a dialog box with a callback function.

**Syntax**
```lua
messageBox( string messageTitle, string messageContent, function messageCallback, [ string messageIcon, string messageButton, int messageButtonDefault, string messageSound, int messageSoundVolume] )
messageBox( string messageTitle, string messageContent, string messageCallback, [ string messageIcon, string messageButton, int messageButtonDefault, string messageSound, int messageSoundVolume] )
```

**Example**
> This example creates a dialog containing an information.
```lua
messageBox("Welcome", "Multi Theft Auto provides the best online Grand Theft Auto experience there is.",
function(callbackResult)
if callbackResult == "OK" then
print("Read")
end
end,
"INFO", "OK")
messageBox("Welcome", "Multi Theft Auto provides the best online Grand Theft Auto experience there is.", "callbackFunction", "INFO", "OK")
```

```lua
function callbackFunction(callbackResult)
if callbackResult == "OK" then
print("Read")
end
end
```

<br>

> This example creates a dialog containing a question.
```lua
messageBox("Save", "Are you sure you want to save your changes? This action will overwrite your previous saves.",
function(callbackResult)
if callbackResult == "YES" then
print("Save")
elseif callbackResult == "NO" then
print("Undo")
else
print("Cancel")
end
end,
"QUESTION", "YESNOCANCEL")
messageBox("Save", "Are you sure you want to save your changes? This action will overwrite your previous saves.", "callbackFunction", "QUESTION", "YESNOCANCEL")
```

```lua
function callbackFunction(callbackResult)
if callbackResult == "YES" then
print("Save")
elseif callbackResult == "NO" then
print("Undo")
else
print("Cancel")
end
end
```

<br>

> This example creates a dialog containing a warning.
```lua
messageBox("Mismatch", "The selected file does not exist. Would you like to try the download again?",
function(callbackResult)
if callbackResult == "ABORT" then
print("Abort")
elseif callbackResult == "RETRY" then
print("Retry")
else
print("Ignore")
end
end,
"WARNING", "ABORTRETRYIGNORE")
messageBox("Mismatch", "The selected file does not exist. Would you like to try the download again?", "callbackFunction", "WARNING", "ABORTRETRYIGNORE")
```

```lua
function callbackFunction(callbackResult)
if callbackResult == "ABORT" then
print("Abort")
elseif callbackResult == "RETRY" then
print("Retry")
else
print("Ignore")
end
end
```

<br>

> This example creates a dialog containing an error.
```lua
messageBox("Your wallet is empty", "You do not have enough money to purchase the selected vehicle.",
function(callbackResult)
if callbackResult == "Ok" then
print("Read")
end
end,
"ERROR", "OK")
messageBox("Your wallet is empty", "You do not have enough money to purchase the selected vehicle.", "callbackFunction", "ERROR", "OK")
```

```lua
function callbackFunction(callbackResult)
if callbackResult == "OK" then
print("Read")
end
end
```

**Arguments**
> - messageTitle: A string specifing the title of the message (note that it will be always uppercase)
> - messageContent: A string specifing the content of the message
> - messageCallback: A function that is called when the user presses a button. Use the callbackResult parameter to register the result.
> - messageCallback: A string specifying the name of the function that will be exported when a button is pressed. (Note that you must specify the function name as a string and specify the function as exportable in the meta.xml of the source.)
> - messageIcon: A string specifing the icon of the message, which is in the **messageIcons** table.
> - messageButton: A string specifing the button(s) of the message, which is in the **messageButtons** table.
> - messageButtonDefault: An integer specifing the default button (note that it will receive a bolder font)
Expand Down
17 changes: 7 additions & 10 deletions [gameplay]/dialogs/sourceC.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ function messageClick()
local elementBox = messageBoxWindows[elementParent]

if elementBox then
local elementCallback = messageBoxCallbacks[elementParent]
local elementCallback = messageBoxCallbacks[elementParent][1]
local elementCallbackSource = messageBoxCallbacks[elementParent][2]
local elementCallbackResult = guiGetText(source)

elementCallbackResult = utf8.upper(elementCallbackResult)

if elementCallback and type(elementCallback) == "function" then
elementCallback(elementCallbackResult)
if elementCallback and type(elementCallback) == "string" then
if elementCallbackSource and getResourceState(elementCallbackSource) == "running" then
call(elementCallbackSource, elementCallback, elementCallbackResult)
end
end

destroyElement(elementParent)
Expand Down Expand Up @@ -72,12 +75,6 @@ function messageBox(messageTitle, messageContent, messageCallback, messageIcon,
error("Bad argument @ 'messageBox' [Expected string at argument 2, got " .. type(messageContent) .. "]")
end

if not messageCallback or type(messageCallback) ~= "function" then
messageCallback = function()
outputDebugString("Since you did not assign a callback function to this message, we replaced it with an empty function.", 2)
end
end

if not messageIcon or type(messageIcon) ~= "string" then
messageIcon = "INFO"
end
Expand Down Expand Up @@ -183,7 +180,7 @@ function messageBox(messageTitle, messageContent, messageCallback, messageIcon,
end

messageBoxWindows[messageWindowElement] = {messageTitle, messageContent, messageIcon, messageButton}
messageBoxCallbacks[messageWindowElement] = messageCallback
messageBoxCallbacks[messageWindowElement] = {messageCallback, sourceResource}
end

function messageBoxEx(messageTitle, messageContent, messageIcon, messageButton, messageButtonDefault, messageSound, messageSoundVolume)
Expand Down

0 comments on commit 00a47dc

Please sign in to comment.