forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Text Window helper functions (otland#4668)
Helper functions in lua for Text Windows
- Loading branch information
1 parent
aefd870
commit 1387a7d
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
data/scripts/creaturescripts/player/#text_window_example.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
local reactWindow = TalkAction("!textWindow") | ||
|
||
function reactWindow.onSay(player, words, param, type) | ||
local txtWindow = TextWindow({ | ||
itemId = 1234, | ||
text = "Write something here:", | ||
canWrite = true, | ||
length = 100, | ||
callback = function(player, item, text) | ||
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You wrote: " .. text) | ||
return true | ||
end | ||
}) | ||
|
||
txtWindow:sendToPlayer(player) | ||
return false | ||
end | ||
|
||
reactWindow:register() |
83 changes: 83 additions & 0 deletions
83
data/scripts/creaturescripts/player/text_window_helper.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
if not TextWindows then TextWindows = {} end | ||
|
||
local textWindow = {} | ||
textWindow.__index = textWindow | ||
|
||
function TextWindow(window) return setmetatable(window, textWindow) end | ||
|
||
function textWindow.setCallback(window, callback) | ||
window.callback = callback | ||
return true | ||
end | ||
|
||
function textWindow.setItemId(window, itemId) | ||
window.itemId = itemId | ||
return true | ||
end | ||
|
||
function textWindow.setText(window, text) | ||
window.text = text | ||
return true | ||
end | ||
|
||
function textWindow.setCanWrite(window, canWrite) | ||
window.canWrite = canWrite | ||
return true | ||
end | ||
|
||
function textWindow.setLength(window, length) | ||
window.length = length | ||
return true | ||
end | ||
|
||
function textWindow.sendToPlayer(window, player) | ||
player:registerEvent("TextWindowHelperClearLogout") | ||
player:registerEvent("TextWindowHelperClearDeath") | ||
player:registerEvent("TextWindowHelper") | ||
window.id = player:showTextDialog(window.itemId, window.text, window.canWrite, window.length) | ||
local playerGuid = player:getGuid() | ||
local windows = TextWindows[playerGuid] | ||
if not windows then | ||
windows = {} | ||
TextWindows[playerGuid] = windows | ||
end | ||
|
||
windows[window.id] = window | ||
return window.id | ||
end | ||
|
||
local textWindowHelper = CreatureEvent("TextWindowHelper") | ||
|
||
function textWindowHelper.onTextEdit(player, item, text, windowTextId) | ||
player:unregisterEvent("TextWindowHelper") | ||
local playerGuid = player:getGuid() | ||
local windows = TextWindows[playerGuid] | ||
if not windows then return true end | ||
local window = windows[windowTextId] | ||
if not window then return true end | ||
windows[windowTextId] = nil | ||
if window.id == windowTextId and window.callback then | ||
return window.callback(player, item, text) | ||
end | ||
return true | ||
end | ||
|
||
textWindowHelper:register() | ||
|
||
local clearWindows = CreatureEvent("TextWindowHelperClearLogout") | ||
|
||
function clearWindows.onLogout(player) | ||
TextWindows[player:getGuid()] = nil | ||
return true | ||
end | ||
|
||
clearWindows:register() | ||
|
||
local clearWindows = CreatureEvent("TextWindowHelperClearDeath") | ||
|
||
function clearWindows.onDeath(player) | ||
TextWindows[player:getGuid()] = nil | ||
return true | ||
end | ||
|
||
clearWindows:register() |