tibia-client/modules/game_console/console.lua

201 lines
5.6 KiB
Lua
Raw Normal View History

Console = {}
-- private variables
local SpeakTypes = {
say = { color = '#FFFF00' },
whisper = { color = '#FFFF00' },
yell = { color = '#FFFF00' },
2012-01-09 06:23:39 +01:00
monsterSay = { color = '#FE6500', hideInConsole = true},
monsterYell = { color = '#FE6500', hideInConsole = true},
2012-01-14 02:37:15 +01:00
npcToPlayer = { color = '#5FF7F7', private = true, npcChat = true },
channelYellow = { color = '#FFFF00' },
channelWhite = { color = '#FFFFFF' },
channelRed = { color = '#F55E5E' },
channelOrange = { color = '#FE6500' },
2012-01-14 02:37:15 +01:00
private = { color = '#5FF7F7', private = true },
playerToNpc = { color = '#9F9DFD', private = true, npcChat = true },
2012-01-14 06:54:20 +01:00
broadcast = { color = '#F55E5E' },
2012-01-14 02:37:15 +01:00
privateRed = { color = '#F55E5E', private = true }
}
local consolePanel
local consoleBuffer
local consoleTabBar
2012-01-14 02:37:15 +01:00
local consoleLineEdit
local channels
-- private functions
function applyMessagePrefixies(name, level, message)
if name then
if Options.showLevelsInConsole and level > 0 then
message = name .. ' [' .. level .. ']: ' .. message
else
message = name .. ': ' .. message
end
end
return message
end
-- public functions
function Console.create()
consolePanel = displayUI('console.otui', { parent = Game.gameBottomPanel } )
2012-01-14 02:37:15 +01:00
consoleLineEdit = consolePanel:getChildById('consoleLineEdit')
consoleBuffer = consolePanel:getChildById('consoleBuffer')
consoleTabBar = consolePanel:getChildById('consoleTabBar')
consoleTabBar:setContentWidget(consoleBuffer)
2012-01-14 02:37:15 +01:00
channels = {}
Console.addChannel('Default', 0)
Console.addTab('Server Log')
Hotkeys.bind('Tab', function() consoleTabBar:selectNextTab() end, consolePanel)
Hotkeys.bind('Shift+Tab', function() consoleTabBar:selectPrevTab() end, consolePanel)
2012-01-14 02:37:15 +01:00
Hotkeys.bind('Enter', Console.sendCurrentMessage, consolePanel)
Hotkeys.bind('Return', Console.sendCurrentMessage, consolePanel)
end
function Console.destroy()
consolePanel:destroy()
consolePanel = nil
end
2012-01-14 02:37:15 +01:00
function Console.addTab(name)
local tab = consoleTabBar:addTab(name)
consoleTabBar:selectTab(tab)
return tab
end
2012-01-14 02:37:15 +01:00
function Console.getTab(name)
return consoleTabBar:getTab(name)
end
function Console.getCurrentTab()
return consoleTabBar:getCurrentTab()
end
function Console.addChannel(name, id)
channels[id] = name
local tab = Console.addTab(name)
tab.channelId = id
return tab
end
function Console.addPrivateText(text, speaktype, name)
local privateTab = Console.getTab(name)
if privateTab == nil then
if Options.showPrivateMessagesInConsole then
privateTab = Console.getTab('Default')
else
privateTab = Console.addTab(name)
end
privateTab.npcChat = speaktype.npcChat
end
Console.addTabText(text, speaktype, privateTab)
end
function Console.addText(text, speaktype, tabName)
local tab = Console.getTab(tabName)
Console.addTabText(text, speaktype, tab)
end
function Console.addTabText(text, speaktype, tab)
2012-01-09 00:28:49 +01:00
if Options.showTimestampsInConsole then
text = os.date('%H:%M') .. ' ' .. text
end
2012-01-14 21:14:40 +01:00
local panel = consoleTabBar:getTabPanel(tab)
local label = createWidget('ConsoleLabel', panel)
label:setText(text)
2012-01-14 02:37:15 +01:00
label:setColor(speaktype.color)
consoleTabBar:blinkTab(tab)
2012-01-14 21:14:40 +01:00
if panel:getChildCount() > 10 then
panel:removeChild(panel:getFirstChild())
end
end
2012-01-14 02:37:15 +01:00
function Console.sendCurrentMessage()
local message = consoleLineEdit:getText()
if #message == 0 then return end
consoleLineEdit:clearText()
2012-01-14 06:54:20 +01:00
-- get current channel
2012-01-14 02:37:15 +01:00
local tab = Console.getCurrentTab()
2012-01-14 06:54:20 +01:00
-- when talking on server log, the message goes to default channel
local name = tab:getText()
if name == 'Server Log' then
tab = Console.getTab('Default')
name = 'Default'
2012-01-14 02:37:15 +01:00
end
2012-01-14 06:54:20 +01:00
local speaktypedesc
2012-01-14 02:37:15 +01:00
if tab.channelId then
2012-01-14 06:54:20 +01:00
if tab.channelId == 0 then
speaktypedesc = 'say'
else
2012-01-14 02:37:15 +01:00
speaktypedesc = 'channelYellow'
end
Game.talkChannel(speaktypedesc, tab.channelId, message)
return
else
2012-01-14 06:54:20 +01:00
if tab.npcChat then
speaktypedesc = 'playerToNpc'
else
speaktypedesc = 'private'
end
2012-01-14 02:37:15 +01:00
local speaktype = SpeakTypes[speaktypedesc]
local player = Game.getLocalPlayer()
2012-01-14 06:54:20 +01:00
Game.talkPrivate(speaktypedesc, name, message)
2012-01-14 02:37:15 +01:00
message = applyMessagePrefixies(player:getName(), player:getLevel(), message)
Console.addPrivateText(message, speaktype, name)
end
end
-- hooked events
2012-01-09 00:28:49 +01:00
local function onCreatureSpeak(name, level, speaktypedesc, message, channelId, creaturePos)
speaktype = SpeakTypes[speaktypedesc]
2012-01-09 06:23:39 +01:00
if speaktype.hideInConsole then return end
2012-01-14 02:37:15 +01:00
message = applyMessagePrefixies(name, level, message)
if speaktype.private then
Console.addPrivateText(message, speaktype, name)
else
Console.addText(message, speaktype, channels[channelId])
end
2012-01-14 02:37:15 +01:00
end
2012-01-14 02:37:15 +01:00
local function onOpenChannel(channelId, channelName)
Console.addChannel(channelName, channelId)
end
local function onChannelList(channelList)
local channelsWindow = displayUI('channelswindow.otui')
local channelListPanel = channelsWindow:getChildById('channelList')
channelsWindow.onEnter = function(self)
local selectedChannelLabel = channelListPanel:getFocusedChild()
if not selectedChannelLabel then return end
2012-01-14 06:54:20 +01:00
Game.joinChannel(selectedChannelLabel.channelId)
2012-01-14 02:37:15 +01:00
channelsWindow:destroy()
end
for k,v in pairs(channelList) do
local channelId = v[1]
local channelName = v[2]
if channelId ~= 0 and #channelName > 0 then
local label = createWidget('ChannelListLabel', channelListPanel)
label.channelId = channelId
label:setText(channelName)
end
end
end
connect(Game, { onLogin = Console.create,
onLogout = Console.destroy,
2012-01-14 02:37:15 +01:00
onCreatureSpeak = onCreatureSpeak,
onChannelList = onChannelList,
onOpenChannel = onOpenChannel})