Skip to content

Commit

Permalink
Text Window helper functions (otland#4668)
Browse files Browse the repository at this point in the history
Helper functions in lua for Text Windows
  • Loading branch information
MillhioreBT authored May 14, 2024
1 parent aefd870 commit 1387a7d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
19 changes: 19 additions & 0 deletions data/scripts/creaturescripts/player/#text_window_example.lua
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 data/scripts/creaturescripts/player/text_window_helper.lua
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()

0 comments on commit 1387a7d

Please sign in to comment.