Skip to content

Commit f9eddcf

Browse files
committed
Better dialogs (add support for some colored embedded messages), remove commandmapping from SendClientMessage, remove colorizeString from format
1 parent 6a3fe18 commit f9eddcf

File tree

2 files changed

+162
-50
lines changed

2 files changed

+162
-50
lines changed

amx/client/client.lua

Lines changed: 159 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ local screenWidth, screenHeight = guiGetScreenSize()
4444
addEventHandler('onClientResourceStart', resourceRoot,
4545
function()
4646
triggerServerEvent('onLoadedAtClient', resourceRoot, localPlayer)
47-
InitDialogs()
4847
setTimer(checkTextLabels, 500, 0)
4948
end,
5049
false
@@ -1623,56 +1622,110 @@ function TogglePlayerClock(toggle)
16231622
setPlayerHudComponentVisible('clock', toggle)
16241623
end
16251624

1626-
function createListDialog()
1627-
listDialog = nil
1628-
listWindow = guiCreateWindow(screenWidth/2 - 541/2,screenHeight/2 - 352/2,541,352,"",false)
1629-
guiWindowSetMovable(listWindow,false)
1630-
guiWindowSetSizable(listWindow,false)
1631-
listGrid = guiCreateGridList(0.0, 0.1, 1.0, 0.8,true,listWindow)
1632-
guiGridListSetSelectionMode(listGrid,2)
1633-
guiGridListSetScrollBars(listGrid, true, true)
1634-
guiGridListSetSortingEnabled (listGrid, false)
1635-
--listColumn = guiGridListAddColumn(listGrid, "List", 0.85)
1636-
listButton1 = guiCreateButton(10,323,256,20,"",false,listWindow)
1637-
listButton2 = guiCreateButton(281,323,256,20,"",false,listWindow)
1638-
guiSetVisible(listWindow, false)
1639-
addEventHandler("onClientGUIClick", listButton1, OnListDialogButton1Click, false)
1640-
addEventHandler("onClientGUIClick", listButton2, OnListDialogButton2Click, false)
1625+
function createListDialog(titleText, message, button1txt, button2txt)
1626+
if listWindow ~= nil then
1627+
removeEventHandler("onClientGUIClick", getRootElement(), OnListDialogButton1Click) --Remove handlers so they are not registered more than once
1628+
removeEventHandler("onClientGUIClick", getRootElement(), OnListDialogButton2Click)
1629+
destroyElement(listWindow) --Assuming inputWindow is the parent of everything, it should remove the whole hierarchy
1630+
end
1631+
1632+
listDialog = nil
1633+
listWindow = guiCreateWindow(screenWidth/2 - 541/2,screenHeight/2 - 352/2,541,352, titleText, false)
1634+
guiWindowSetMovable(listWindow,false)
1635+
guiWindowSetSizable(listWindow,false)
1636+
listGrid = guiCreateGridList(0.0, 0.1, 1.0, 0.8,true,listWindow)
1637+
guiGridListSetSelectionMode(listGrid,2)
1638+
guiGridListSetScrollBars(listGrid, true, true)
1639+
guiGridListSetSortingEnabled (listGrid, false)
1640+
--listColumn = guiGridListAddColumn(listGrid, "List", 0.85)
1641+
1642+
local xpos = 0.0
1643+
if #button1txt == 0 or #button2txt == 0 then
1644+
xpos = 0.40 --Center
1645+
end
1646+
1647+
listButton1 = guiCreateButton(xpos ~= 0.0 and xpos or 0.3, 0.9, 0.15, 0.1, button1txt,true,listWindow)
1648+
listButton2 = guiCreateButton(xpos ~= 0.0 and xpos or 0.5, 0.9, 0.15, 0.1, button2txt,true,listWindow)
1649+
1650+
if #button1txt == 0 then
1651+
guiSetVisible(listButton1, false)
1652+
end
1653+
if #button2txt == 0 then
1654+
guiSetVisible(listButton2, false)
1655+
end
1656+
1657+
guiSetVisible(listWindow, false)
1658+
addEventHandler("onClientGUIClick", listButton1, OnListDialogButton1Click, false)
1659+
addEventHandler("onClientGUIClick", listButton2, OnListDialogButton2Click, false)
1660+
return listGrid
16411661
end
16421662

1643-
function createInputDialog()
1663+
function createInputDialog(titleText, message, button1txt, button2txt)
1664+
if inputWindow ~= nil then
1665+
removeEventHandler("onClientGUIClick", getRootElement(), OnInputDialogButton1Click) --Remove handlers so they are not registered more than once
1666+
removeEventHandler("onClientGUIClick", getRootElement(), OnInputDialogButton2Click)
1667+
destroyElement(inputWindow) --Assuming inputWindow is the parent of everything, it should remove the whole hierarchy
1668+
end
16441669
inputDialog = nil
1645-
inputWindow = guiCreateWindow(screenWidth/2 - 541/2,screenHeight/2 - 352/2,541,352,"",false)
1670+
inputWindow = guiCreateWindow(screenWidth/2 - 541/2,screenHeight/2 - 352/2,541,352, titleText, false)
16461671
guiWindowSetMovable(listWindow,false)
16471672
guiWindowSetSizable(listWindow,false)
1648-
inputLabel = guiCreateLabel(0.1, 0.1, 1.0, 0.8, "", true, inputWindow)
1649-
inputEdit = guiCreateEdit(0.0, 0.7, 1.0, 0.1,"",true,inputWindow)
1650-
inputButton1 = guiCreateButton(0.3, 0.9, 0.15, 0.1,"",true,inputWindow) --x, y, width, height
1651-
inputButton2 = guiCreateButton(0.5, 0.9, 0.15, 0.1,"",true,inputWindow)
1673+
inputLabel = guiCreateColoredLabel(0.1, 0.1, 1.0, 0.8, message, inputWindow, true)
1674+
inputEdit = guiCreateEdit(0.0, 0.7, 1.0, 0.1, "", true, inputWindow)
1675+
1676+
local xpos = 0.0
1677+
if #button1txt == 0 or #button2txt == 0 then
1678+
xpos = 0.40 --Center
1679+
end
1680+
1681+
inputButton1 = guiCreateButton(xpos ~= 0.0 and xpos or 0.3, 0.9, 0.15, 0.1, button1txt, true,inputWindow) --x, y, width, height
1682+
inputButton2 = guiCreateButton(xpos ~= 0.0 and xpos or 0.5, 0.9, 0.15, 0.1, button2txt, true,inputWindow)
1683+
1684+
if #button1txt == 0 then
1685+
guiSetVisible(inputButton1, false)
1686+
end
1687+
if #button2txt == 0 then
1688+
guiSetVisible(inputButton2, false)
1689+
end
1690+
16521691
guiSetVisible(inputWindow, false)
16531692
addEventHandler("onClientGUIClick", inputButton1, OnInputDialogButton1Click, false)
16541693
addEventHandler("onClientGUIClick", inputButton2, OnInputDialogButton2Click, false)
16551694
end
16561695

1657-
function createMessageDialog()
1696+
function createMessageDialog(titleText, message, button1txt, button2txt )
1697+
if msgWindow ~= nil then
1698+
removeEventHandler("onClientGUIClick", getRootElement(), OnMessageDialogButton1Click) --Remove handlers so they are not registered more than once
1699+
removeEventHandler("onClientGUIClick", getRootElement(), OnMessageDialogButton2Click)
1700+
destroyElement(msgWindow) --Assuming msgWindow is the parent of everything, it should remove the whole hierarchy
1701+
end
1702+
16581703
msgDialog = nil
1659-
msgWindow = guiCreateWindow(screenWidth/2 - 541/2,screenHeight/2 - 352/2,541,352,"",false)
1704+
msgWindow = guiCreateWindow(screenWidth/2 - 541/2,screenHeight/2 - 352/2,541,352, titleText, false)
16601705
guiWindowSetMovable(msgWindow,false)
16611706
guiWindowSetSizable(msgWindow,false)
1662-
msgLabel = guiCreateLabel(0.0, 0.1, 1.0, 0.7, "", true, msgWindow)
1663-
msgButton1 = guiCreateButton(0.3, 0.9, 0.15, 0.1,"",true,msgWindow) --x, y, width, height
1664-
msgButton2 = guiCreateButton(0.5, 0.9, 0.15, 0.1,"",true,msgWindow)
1707+
msgLabel = guiCreateColoredLabel(0.1, 0.1, 1.0, 0.7, message, msgWindow, true)
1708+
1709+
local xpos = 0.0
1710+
if #button1txt == 0 or #button2txt == 0 then
1711+
xpos = 0.40 --Center
1712+
end
1713+
1714+
msgButton1 = guiCreateButton(xpos ~= 0.0 and xpos or 0.3, 0.9, 0.15, 0.1, button1txt,true,msgWindow) --x, y, width, height
1715+
msgButton2 = guiCreateButton(xpos ~= 0.0 and xpos or 0.5, 0.9, 0.15, 0.1, button2txt,true,msgWindow)
1716+
1717+
if #button1txt == 0 then
1718+
guiSetVisible(msgButton1, false)
1719+
end
1720+
if #button2txt == 0 then
1721+
guiSetVisible(msgButton2, false)
1722+
end
1723+
16651724
guiSetVisible(msgWindow, false)
16661725
addEventHandler("onClientGUIClick", msgButton1, OnMessageDialogButton1Click, false)
16671726
addEventHandler("onClientGUIClick", msgButton2, OnMessageDialogButton2Click, false)
16681727
end
16691728

1670-
function InitDialogs()
1671-
createListDialog()
1672-
createInputDialog()
1673-
createMessageDialog()
1674-
end
1675-
16761729
function clearListItem()
16771730
guiGridListRemoveColumn(listGrid, listColumn) --First remove the default column
16781731
local colAmount = guiGridListGetColumnCount(listGrid)
@@ -1745,34 +1798,92 @@ function OnMessageDialogButton2Click( button, state )
17451798
end
17461799
end
17471800

1801+
-- Originally by UAEpro from here: https://forum.mtasa.com/topic/33149-colorcodes-in-labels/?tab=comments#comment-335358
1802+
function guiCreateColoredLabel(ax, ay, bx, by,str, parent, relative) --x, y, width, height
1803+
if not relative then
1804+
relative = true
1805+
end
1806+
1807+
local scrollpane = guiCreateScrollPane(ax,ay,bx,by,relative,parent)
1808+
--outputConsole('main string:' .. str)
1809+
1810+
local pat = "(.-)#(%x%x%x%x%x%x)"
1811+
local s, e, cap, col = str:find(pat, 1)
1812+
local last = 1
1813+
local labels = {}
1814+
local incy = 0
1815+
local incx = 0
1816+
while s do
1817+
1818+
if cap == "" and col then r,g,b = tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(5, 6)) end
1819+
if (s ~= 1) or cap ~= "" then
1820+
--outputConsole('guiCreateColoredLabel: ' .. cap)
1821+
1822+
lbl = guiCreateLabel(ax + incx, ay + incy, 1.0, by, cap, relative, scrollpane)
1823+
guiLabelSetHorizontalAlign(lbl, "left")
1824+
table.insert(labels, lbl)
1825+
if (r == nil) then r = 255 end
1826+
if (g == nil) then g = 255 end
1827+
if (b == nil) then b = 255 end
1828+
guiLabelSetColor(lbl,r,g,b)
1829+
r,g,b = tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(5, 6))
1830+
1831+
local match = cap:find("\n")
1832+
if match ~= nil then
1833+
local xtxtsize, ytxtsize = guiGetSize(lbl, true) --not relative
1834+
incy = incy + (ytxtsize / 8) --We found a /n so send it further down on the next line
1835+
incx = 0 --Don't add spaces on new lines
1836+
--outputConsole('found a new line')
1837+
else
1838+
if r ~= 255 or g ~= 255 or b ~= 255 then --It's colored so separate it
1839+
incy = 0
1840+
local xsize, ysize = guiGetSize(scrollpane, false) --not relative
1841+
incx = incx + (guiLabelGetTextExtent ( lbl ) / xsize) --Make space for the next word, relative to the parent width
1842+
--outputConsole('Separating string')
1843+
else
1844+
incy = 0
1845+
incx = 0
1846+
end
1847+
end
1848+
end
1849+
last = e + 1
1850+
s, e, cap, col = str:find(pat, last)
1851+
end
1852+
if (last <= #str) then
1853+
cap = str:sub(last)
1854+
lbl2 = guiCreateLabel(ax + incx, ay + incy, 1.0, by, cap, relative, scrollpane)
1855+
table.insert(labels, lbl2)
1856+
guiLabelSetColor(lbl2,r,g,b)
1857+
end
1858+
return labels
1859+
end
1860+
1861+
--replace colors
1862+
function colorizeString(string)
1863+
return string:gsub("(=?{[0-9A-Fa-f]*})",
1864+
function(colorMatches)
1865+
colorMatches = colorMatches:gsub("[{}]+", "") --replace the curly brackets with nothing
1866+
colorMatches = '#' .. colorMatches --Append to the beginning
1867+
return colorMatches
1868+
end)
1869+
end
17481870

17491871
function ShowPlayerDialog(dialogid, dialogtype, caption, info, button1, button2)
1872+
showCursor(true)
17501873
if dialogtype == 0 then
1751-
guiSetText(msgButton1, button1)
1752-
guiSetText(msgButton2, button2)
1753-
guiSetText(msgWindow, caption)
1754-
guiSetText(msgLabel, info)
1874+
createMessageDialog(caption, colorizeString(info), button1, button2 )
17551875
guiSetVisible(msgWindow, true)
17561876
msgDialog = dialogid
1757-
showCursor(true)
17581877
elseif dialogtype == 1 or dialogtype == 3 then
1759-
guiSetText(inputButton1, button1)
1760-
guiSetText(inputButton2, button2)
1761-
guiSetText(inputWindow, caption)
1762-
guiSetText(inputEdit, "")
1878+
createInputDialog(caption, colorizeString(info), button1, button2)
17631879
guiEditSetMasked(inputEdit, dialogtype == 3)
1764-
guiSetText(inputLabel, info)
17651880
guiSetVisible(inputWindow, true)
17661881
inputDialog = dialogid
1767-
showCursor(true)
17681882
elseif dialogtype == 2 or dialogtype == 4 or dialogtype == 5 then --DIALOG_STYLE_LIST, DIALOG_STYLE_TABLIST, DIALOG_STYLE_TABLIST_HEADER
17691883
--Setup the UI
1770-
guiSetText(listButton1, button1)
1771-
guiSetText(listButton2, button2)
1772-
guiSetText(listWindow, caption)
1884+
createListDialog(caption, colorizeString(info), button1, button2)
17731885
guiSetVisible(listWindow, true)
17741886
listDialog = dialogid
1775-
showCursor(true)
17761887
-- Done
17771888

17781889
--Process each

amx/server/natives/a_samp.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ function SendClientMessage(amx, player, r, g, b, a, message)
3535
message = ('_'):rep(43)
3636
elseif message:len() > 44 and message:match('^%*+$') then
3737
message = ('*'):rep(44)
38+
--[[
3839
else
3940
for mta,samp in pairs(g_CommandMapping) do
4041
message = message:gsub('/' .. samp, '/' .. mta)
4142
end
43+
]] --Why is command mapping stuff here? This replaces any part of a string, causing commands such as '/quitfaction' to display as '/outfaction'
4244
end
4345

4446
--replace colors
@@ -875,9 +877,8 @@ function format(amx, outBuf, outBufSize, fmt, ...)
875877
fmt = fmt:gsub('(%%[%-%d%.]*)%*(%a)', '%1%2')
876878
local result = fmt:format(unpack(args))
877879

878-
--replace colors
879880
if #result+1 <= outBufSize then
880-
writeMemString(amx, outBuf, colorizeString(result))
881+
writeMemString(amx, outBuf, result)
881882
end
882883
end
883884

0 commit comments

Comments
 (0)