Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 14th, 2012  |  syntax: Lua  |  size: 9.14 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. local sx,sy = guiGetScreenSize()
  4.  
  5. RankingBoard = {}
  6. RankingBoard.__index = RankingBoard
  7.  
  8. RankingBoard.instances = {}
  9.  
  10. local screenWidth, screenHeight = guiGetScreenSize()
  11. local topDistance = 250
  12. local bottomDistance = 0.26*screenHeight
  13. local posLeftDistance = 30
  14. local nameLeftDistance = 60
  15. local labelHeight = 16
  16. local maxPositions = math.floor((screenHeight - topDistance - bottomDistance)/labelHeight)
  17.  
  18. posLabel = {}
  19. playerLabel = {}
  20.  
  21. function RankingBoard.create(id)
  22.         RankingBoard.instances[id] = setmetatable({ id = id, direction = 'down', labels = {}, position = 0 }, RankingBoard)
  23.         posLabel = {}
  24.         playerLabel = {}
  25. end
  26.  
  27. function RankingBoard.call(id, fn, ...)
  28.         RankingBoard[fn](RankingBoard.instances[id], ...)
  29. end
  30.  
  31. function RankingBoard:setDirection(direction, plrs)
  32.         self.direction = direction
  33.         if direction == 'up' then
  34.                 self.highestPos = plrs--#g_Players
  35.                 self.position = self.highestPos + 1
  36.         end
  37. end
  38.  
  39. function RankingBoard:add(name, time)
  40.         local position
  41.         local y
  42.         local doBoardScroll = false
  43.         if self.direction == 'down' then
  44.                 self.position = self.position + 1
  45.                 if self.position > maxPositions then
  46.                         return
  47.                 end
  48.                 y = topDistance + (self.position-1)*labelHeight
  49.         elseif self.direction == 'up' then
  50.                 self.position = self.position - 1
  51.                 local labelPosition = self.position
  52.                 if self.highestPos > maxPositions then
  53.                         labelPosition = labelPosition - (self.highestPos - maxPositions)
  54.                         if labelPosition < 1 then
  55.                                 labelPosition = 0
  56.                                 doBoardScroll = true
  57.                         end
  58.                 elseif labelPosition < 1 then
  59.                         return
  60.                 end
  61.                 y = topDistance + (labelPosition-1)*labelHeight
  62.         end
  63.         posLabel[name], posLabelShadow = createShadowedLabelFromSpare(posLeftDistance, y, 20, labelHeight, tostring(self.position) .. ')', 'right')
  64.         if time then
  65.                 if not self.firsttime then
  66.                         self.firsttime = time
  67.                         time = '' .. msToTimeStr(time)
  68.                 else
  69.                         time = '' .. msToTimeStr(time)
  70.                 end
  71.         else
  72.                 time = ''
  73.         end
  74.         playerLabel[name], playerLabelShadow = createShadowedLabelFromSpare(nameLeftDistance, y, 250, labelHeight, name .. ' #abcdef[#ffffff'..time..'#abcdef]')
  75.         table.insert(self.labels, posLabel[name])
  76.         table.insert(self.labels, posLabelShadow)
  77.         table.insert(self.labels, playerLabel[name])
  78.         table.insert(self.labels, playerLabelShadow)
  79.     playSoundFrontEnd(7)
  80.         if doBoardScroll then
  81.                 guiSetAlpha(posLabel[name], 0)
  82.                 guiSetAlpha(posLabelShadow, 0)
  83.                 guiSetAlpha(playerLabel[name], 0)
  84.                 guiSetAlpha(playerLabelShadow, 0)
  85.                 local anim = Animation.createNamed('race.boardscroll', self)
  86.                 anim:addPhase({ from = 0, to = 1, time = 700, fn = RankingBoard.scroll, firstLabel = posLabel[name] })
  87.                 anim:addPhase({ fn = RankingBoard.destroyLastLabel, firstLabel = posLabel[name] })
  88.                 anim:play()
  89.         end
  90. end
  91.  
  92. function test()
  93.         RankingBoard:add(getPlayerName(getLocalPlayer()), math.random(5000))
  94. end
  95.  
  96. addCommandHandler("test", test)
  97.  
  98. function RankingBoard:scroll(param, phase)
  99.         local firstLabelIndex = table.find(self.labels, phase.firstLabel)
  100.         for i=firstLabelIndex,firstLabelIndex+3 do
  101.                 guiSetAlpha(self.labels[i], param)
  102.         end
  103.         local x, y
  104.         for i=0,#self.labels/4-1 do
  105.                 for j=1,4 do
  106.                         x = (j <= 2 and posLeftDistance or nameLeftDistance)
  107.                         y = topDistance + ((maxPositions - i - 1) + param)*labelHeight
  108.                         if j % 2 == 0 then
  109.                                 x = x + 1
  110.                                 y = y + 1
  111.                         end
  112.                         guiSetPosition(self.labels[i*4+j], sx + x, y, false)
  113.                 end
  114.         end
  115.         for i=1,4 do
  116.                 guiSetAlpha(self.labels[i], 1 - param)
  117.         end
  118. end
  119.  
  120. function RankingBoard:destroyLastLabel(phase)
  121.         for i=1,4 do
  122.                 destroyElementToSpare(self.labels[1])
  123.                 guiSetVisible(self.labels[1],false)
  124.                 table.remove(self.labels, 1)
  125.         end
  126.         local firstLabelIndex = table.find(self.labels, phase.firstLabel)
  127.         for i=firstLabelIndex,firstLabelIndex+3 do
  128.                 guiSetAlpha(self.labels[i], 1)
  129.         end
  130. end
  131.  
  132. function RankingBoard:addMultiple(items)
  133.         for i,item in ipairs(items) do
  134.                 self:add(item.name, item.time)
  135.         end
  136. end
  137.  
  138. function RankingBoard:clear()
  139.         table.each(self.labels, destroyElementToSpare)
  140.         self.labels = {}
  141. end
  142.  
  143. function RankingBoard:destroy()
  144.         self:clear()
  145.         RankingBoard.instances[self.id] = nil
  146. end
  147.  
  148.  
  149.  
  150. --
  151. -- Label cache
  152. --
  153.  
  154.  
  155. local spareElems = {}
  156. local donePrecreate = false
  157.  
  158.  
  159. function RankingBoard.precreateLabels(count)
  160.     donePrecreate = false
  161.     while #spareElems/4 < count do
  162.         local label, shadow = createShadowedLabel(10, 1, 20, 10, 'a' )
  163.                 --guiSetAlpha(label,0)
  164.                 guiSetAlpha(shadow,0)
  165.                
  166.  
  167.                
  168.                 guiSetVisible(label, false)
  169.                 guiSetVisible(shadow, false)
  170.         destroyElementToSpare(label)
  171.         destroyElementToSpare(shadow)
  172.         end
  173.     donePrecreate = true
  174. end
  175.  
  176. function destroyElementToSpare(elem)
  177.     table.insertUnique( spareElems, elem )
  178.     guiSetVisible(elem, false)
  179. end
  180.  
  181. dxTextCache = {}
  182. dxTextShadowCache = {}
  183.  
  184. function dxDrawColoredLabel(str, ax, ay, bx, by, color,tcolor,scale, font)
  185.         local rax = ax
  186.         if not dxTextShadowCache[str] then
  187.                 dxTextShadowCache[str] = string.gsub( str, '#%x%x%x%x%x%x', '' )
  188.         end
  189.         dxDrawText(dxTextShadowCache[str], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "center", false,false,false)
  190.         if dxTextCache[str] then
  191.                 for id, text in ipairs(dxTextCache[str]) do
  192.                         local w = text[2] * ( scale / text[4]  )
  193.                         dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "center", false,false,false)
  194.                 end
  195.         else
  196.                 dxTextCache[str] = {}
  197.                 local pat = "(.-)#(%x%x%x%x%x%x)"
  198.                 local s, e, cap, col = str:find(pat, 1)
  199.                 local last = 1
  200.                 local r = tcolor[1]
  201.                 local g = tcolor[2]
  202.                 local b = tcolor[3]
  203.                 local textalpha = tcolor[4]
  204.                 while s do
  205.                         if cap == "" and col then
  206.                                 r = tonumber("0x"..col:sub(1, 2))
  207.                                 g = tonumber("0x"..col:sub(3, 4))
  208.                                 b = tonumber("0x"..col:sub(5, 6))
  209.                                 color = tocolor(r, g, b, textalpha)
  210.                         end
  211.                         if s ~= 1 or cap ~= "" then
  212.                                 local w = dxGetTextWidth(cap, scale, font)
  213.                                 dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "center")
  214.                                 table.insert(dxTextCache[str], { cap, ax-rax, {r,g,b}, scale } )
  215.                                 ax = ax + w
  216.                                 r = tonumber("0x"..col:sub(1, 2))
  217.                                 g = tonumber("0x"..col:sub(3, 4))
  218.                                 b = tonumber("0x"..col:sub(5, 6))
  219.                                 color = tocolor( r, g, b, textalpha)
  220.                         end
  221.                         last = e + 1
  222.                         s, e, cap, col = str:find(pat, last)
  223.                 end
  224.                 if last <= #str then
  225.                         cap = str:sub(last)
  226.                         local w = dxGetTextWidth(cap, scale, font)
  227.                         dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "center")
  228.                         table.insert(dxTextCache[str], { cap, ax-rax, {r,g,b}, scale } )
  229.                 end
  230.         end
  231. end
  232.  
  233. local font = "default-bold"
  234.  
  235. addEventHandler("onClientRender", getRootElement(),
  236. function()
  237.         for id, elem in pairs(playerLabel) do
  238.                 if guiGetVisible(elem) and string.len(guiGetText(elem)) > 4 then
  239.                         local x,y = guiGetPosition(elem, false)
  240.                         local a = guiGetAlpha(elem) * 255
  241.                                 if not getKeyState("tab") then
  242.                                 dxDrawColoredLabel(string.gsub(guiGetText(elem)," ", " #ffffff",1), 50,y,200,y+20, tocolor(255,255,255,a*0.85),{255,255,255,a*0.85}, 1, font, "left", "center", false,false,false)                             
  243.                                 --if getElementData(elem, "spectated") then
  244.                                         --dxDrawImage(2,y,16,16,"img/hurry.png", 0,0,0,tocolor(255,255,255,255), false)
  245.                                 --end
  246.                         end
  247.                         if x < 100 then guiSetPosition(elem, sx+100,y,false) end
  248.                 end
  249.         end
  250.         for id, elem in pairs(posLabel) do
  251.                 if guiGetVisible(elem) and string.len(guiGetText(elem)) <= 4 then
  252.                         local x,y = guiGetPosition(elem, false )
  253.                         local a = guiGetAlpha(elem) * 255
  254.                         if not getKeyState("tab") then
  255.                                 if getElementData(elem,"spectated") then
  256.                                         dxDrawText(guiGetText(elem), 1,y+1,41,y+21, tocolor(0,0,0,math.floor(a*0.85)), 1, font, "right", "center", false,false,false)
  257.                                         dxDrawText(guiGetText(elem), 0,y,40,y+20, tocolor(170,70,0,a), 1, font, "right", "center", false,false,false)
  258.                                 else
  259.                                         dxDrawText(guiGetText(elem), 1,y+1,41,y+21, tocolor(0,0,0,math.floor(a*0.85)), 1, font, "right", "center", false,false,false)
  260.                                         dxDrawText(guiGetText(elem), 0,y,40,y+20, tocolor(255,255,255,a*0.85), 1, font, "right", "center", false,false,false)
  261.                                 end
  262.                         end
  263.                         if x < 100 then guiSetPosition(elem, sx+100,y,false) end
  264.                 end
  265.         end
  266. end)
  267.  
  268.  
  269.  
  270. function createShadowedLabelFromSpare(x, y, width, height, text, align)
  271.  
  272.     if #spareElems < 2 then
  273.         if not donePrecreate then
  274.             outputDebug( 'OPTIMIZATION', 'createShadowedLabel' )
  275.         end
  276.             return createShadowedLabel(x, y, width, height, text, align)
  277.     else
  278.         local shadow = table.popLast( spareElems )
  279.             guiSetSize(shadow, width, height, false)
  280.             guiSetText(shadow, text)
  281.             --guiLabelSetColor(shadow, 0, 0, 0)
  282.             guiSetPosition(shadow, sx + x + 1, y + 1, false)
  283.         guiSetVisible(shadow, false)
  284.  
  285.         local label = table.popLast( spareElems )
  286.             guiSetSize(label, width, height, false)
  287.             guiSetText(label, text)
  288.             --guiLabelSetColor(label, 255, 255, 255)
  289.             guiSetPosition(label, sx + x, y, false)
  290.         guiSetVisible(label, true)
  291.             if align then
  292.                     guiLabelSetHorizontalAlign(shadow, align)
  293.                     guiLabelSetHorizontalAlign(label, align)
  294.         else
  295.                     guiLabelSetHorizontalAlign(shadow, 'left')
  296.                     guiLabelSetHorizontalAlign(label, 'left')
  297.             end
  298.         return label, shadow
  299.     end
  300. end