-- Client Side --
-- news feed
function createNewsFeed()
-- get the screen width and height
local sWidth, sHeight = guiGetScreenSize()
-- create the gridlist, using some maths to find the bottom-centre of the screen
local Width,Height = 800,45
local X = (sWidth/2) - (Width/2)
local Y = sHeight - (Height/2)
-- the gridlist will act as the background to our news reel
newsGridlist = guiCreateGridList(X,Y,Width,Height,false)
-- create the label in the same place, but as a child of the gridlist
-- also make it half the height of the gridlist, as half of the gridlist is off the bottom of the screen
newsLabel = guiCreateLabel(X,Y,Width,Height/2,"Test text",false,newsGridlist)
-- call our function to read the news out of the xml file
loadNews()
-- load the first news item in the table
updateNewsItem(1)
-- define a global variable called 'currentItem' with the value 1
currentItem = 1
end
addEventHandler("onClientResourceStart",resourceRoot,createNewsFeed)
function loadNews()
local newsfile = xmlLoadFile("newsfeed.xml")
if newsfile then
-- loop through all the children of the root node (the "news" nodes)
for index,itemNode in ipairs(xmlNodeGetChildren(newsfile)) do
-- get the text (the news item) from the node
local item = xmlNodeGetValue(itemNode)
-- insert it into our newsItems table
table.insert(newsItems,item)
end
xmlUnloadFile(newsfile)
end
end
-- define our function with a newIndex parameter, so that we can pass which news item we want to show
function updateNewsItem(newIndex)
-- get the new news item from the table
local item = newsItems[newIndex]
-- update the label text
guiSetText(newsLabel,item)
-- update the 'currentItem' global variable
currentItem = newIndex
-- get the current dimensions of the gui label
local width,height = guiGetSize(newsLabel,false)
-- get the text width of the label
local extent = guiLabelGetTextExtent(newsLabel)
-- update the size of the label with the new width (we do not change the height)
guiSetSize(newsLabel,extent,height,false)
-- set the positon to the far right side of the gridlist, ready to scroll on to the left
guiSetPosition(newsLabel,1,0,true)
end
addEventHandler("onClientRender",root,scrollNews)
function scrollNews()
local x,y = guiGetPosition(newsLabel,false)
local labelWidth, labelHeight = guiGetSize(newsLabel,false)
-- if the far right position of the label (x + labelWidth) is greater than or equal to 0 (ie: still showing on the gridlist)
if ((x-1) + labelWidth) >= 0 then
-- compensate for a small off-by-one bug in MTA
if x <= 0 then x = x - 1 end
-- update the position as normal
guiSetPosition(newsLabel,x-1,y,false)
-- otherwise, we move on to the next item and reset the position
else
-- get the total number of items in the 'newsItems' table
local totalItems = #newsItems
-- if the next item on our list does not exist in our table
if (currentItem + 1) > totalItems then
-- loop back to the first item in the list
updateNewsItem(1)
else
-- otherwise move onto the next item in the list
updateNewsItem(currentItem + 1)
end
end
end
--newsfeed.xml--
<root>
<news>Visit [url=http://www.JWORLDSA.tk]http://www.JWORLDSA.tk[/url] for latest news.</news>
<news>Still Updating</news>
</root>