Guest User

Untitled

a guest
Dec 21st, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. dxText = {}
  2. dxText_mt = { __index = dxText }
  3. local idAssign,idPrefix = 0,"c"
  4. local g_screenX,g_screenY = guiGetScreenSize()
  5. local visibleText = {}
  6. ------
  7. defaults = {
  8. fX = 0.5,
  9. fY = 0.5,
  10. bRelativePosition = true,
  11. strText = "",
  12. bVerticalAlign = "center",
  13. bHorizontalAlign = "center",
  14. tColor = {255,255,255,255},
  15. fScale = 1,
  16. strFont = "default",
  17. strType = "normal",
  18. tAttributes = {},
  19. bPostGUI = false,
  20. bClip = false,
  21. bWordWrap = true,
  22. bVisible = true,
  23. tBoundingBox = false, --If a bounding box is not set, it will not be used.
  24. bRelativeBoundingBox = true,
  25. }
  26.  
  27. local validFonts = {
  28. default = true,
  29. ["default-bold"] = true,
  30. clear = true,
  31. arial = true,
  32. pricedown = true,
  33. bankgothic = true,
  34. diploma = true,
  35. beckett = true,
  36. }
  37.  
  38. local validTypes = {
  39. normal = true,
  40. shadow = true,
  41. border = true,
  42. stroke = true, --Clone of border
  43. }
  44.  
  45. local validAlignTypes = {
  46. center = true,
  47. left = true,
  48. right = true,
  49. }
  50.  
  51. function dxText:create( text, x, y, relative, strFont, fScale, horzA )
  52. assert(not self.fX, "attempt to call method 'create' (a nil value)")
  53. if ( type(text) ~= "string" ) or ( not tonumber(x) ) or ( not tonumber(y) ) then
  54. outputDebugString ( "dxText:create - Bad argument", 0, 112, 112, 112 )
  55. return false
  56. end
  57. local new = {}
  58. setmetatable( new, dxText_mt )
  59. --Add default settings
  60. for i,v in pairs(defaults) do
  61. new[i] = v
  62. end
  63. idAssign = idAssign + 1
  64. new.id = idPrefix..idAssign
  65. new.strText = text or new.strText
  66. new.fX = x or new.fX
  67. new.fY = y or new.fY
  68. if type(relative) == "boolean" then
  69. new.bRelativePosition = relative
  70. end
  71. new:scale( fScale or new.fScale )
  72. new:font( strFont or new.strFont )
  73. new:align( horzA or new.bHorizontalAlign )
  74. visibleText[new] = true
  75. return new
  76. end
  77.  
  78. function dxText:text(text)
  79. if type(text) ~= "string" then return self.strText end
  80. self.strText = text
  81. return true
  82. end
  83.  
  84. function dxText:position(x,y,relative)
  85. if not tonumber(x) then return self.fX, self.fY end
  86. self.fX = x
  87. self.fY = y
  88. if type(relative) == "boolean" then
  89. self.bRelativePosition = relative
  90. else
  91. self.bRelativePosition = true
  92. end
  93. return true
  94. end
  95.  
  96. function dxText:color(r,g,b,a)
  97. if not tonumber(r) then return unpack(self.tColor) end
  98. g = g or self.tColor[2]
  99. b = b or self.tColor[3]
  100. a = a or self.tColor[4]
  101. self.tColor = { r,g,b,a }
  102. return true
  103. end
  104.  
  105. function dxText:scale(scale)
  106. if not tonumber(scale) then return self.fScale end
  107. self.fScale = scale
  108. return true
  109. end
  110.  
  111. function dxText:visible(bool)
  112. if type(bool) ~= "boolean" then return self.bVisible end
  113. if self.bVisible == bool then return end
  114. self.bVisible = bool
  115. if bool then
  116. visibleText[self] = true
  117. else
  118. visibleText[self] = nil
  119. end
  120. return true
  121. end
  122.  
  123. function dxText:destroy()
  124. self.bDestroyed = true
  125. setmetatable( self, self )
  126. return true
  127. end
  128.  
  129. function dxText:font(font)
  130. if not validFonts[font] then return self.strFont end
  131. self.strFont = font
  132. return true
  133. end
  134.  
  135. function dxText:postGUI(bool)
  136. if type(bool) ~= "boolean" then return self.bPostGUI end
  137. self.bPostGUI = bool
  138. return true
  139. end
  140.  
  141. function dxText:clip(bool)
  142. if type(bool) ~= "boolean" then return self.bClip end
  143. self.bClip = bool
  144. return true
  145. end
  146.  
  147. function dxText:wordWrap(bool)
  148. if type(bool) ~= "boolean" then return self.bWordWrap end
  149. self.bWordWrap = bool
  150. return true
  151. end
  152.  
  153. function dxText:type(type,...)
  154. if not validTypes[type] then return self.strType, unpack(self.tAttributes) end
  155. self.strType = type
  156. self.tAttributes = {...}
  157. return true
  158. end
  159.  
  160. function dxText:align(horzA, vertA)
  161. if not validAlignTypes[horzA] then return self.bHorizontalAlign, self.bVerticalAlign end
  162. vertA = vertA or self.bVerticalAlign
  163. self.bHorizontalAlign, self.bVerticalAlign = horzA, vertA
  164. return true
  165. end
  166.  
  167. function dxText:boundingBox(left,top,right,bottom,relative)
  168. if left == nil then
  169. if self.tBoundingBox then
  170. return unpack(boundingBox)
  171. else
  172. return false
  173. end
  174. elseif tonumber(left) and tonumber(right) and tonumber(top) and tonumber(bottom) then
  175. self.tBoundingBox = {left,top,right,bottom}
  176. if type(relative) == "boolean" then
  177. self.bRelativeBoundingBox = relative
  178. else
  179. self.bRelativeBoundingBox = true
  180. end
  181. else
  182. self.tBoundingBox = false
  183. end
  184. return true
  185. end
  186.  
  187. addEventHandler ( "onClientRender", getRootElement(),
  188. function()
  189. if(isMainMenuActive()) then return end
  190. for self,_ in pairs(visibleText) do
  191. while true do
  192. if self.bDestroyed then
  193. visibleText[self] = nil
  194. break
  195. end
  196. if self.tColor[4] < 1 then
  197. break
  198. end
  199. local l,t,r,b
  200. --If we arent using a bounding box
  201. if not self.tBoundingBox then
  202. --Decide if we use relative or absolute
  203. local p_screenX,p_screenY = 1,1
  204. if self.bRelativePosition then
  205. p_screenX,p_screenY = g_screenX,g_screenY
  206. end
  207. local fX,fY = (self.fX)*p_screenX,(self.fY)*p_screenY
  208. if self.bHorizontalAlign == "left" then
  209. l = fX
  210. r = fX + g_screenX
  211. elseif self.bHorizontalAlign == "right" then
  212. l = fX - g_screenX
  213. r = fX
  214. else
  215. l = fX - g_screenX
  216. r = fX + g_screenX
  217. end
  218. if self.bVerticalAlign == "top" then
  219. t = fY
  220. b = fY + g_screenY
  221. elseif self.bVerticalAlign == "bottom" then
  222. t = fY - g_screenY
  223. b = fY
  224. else
  225. t = fY - g_screenY
  226. b = fY + g_screenY
  227. end
  228. elseif type(self.tBoundingBox) == "table" then
  229. local b_screenX,b_screenY = 1,1
  230. if self.bRelativeBoundingBox then
  231. b_screenX,b_screenY = g_screenX,g_screenY
  232. end
  233. l,t,r,b = self.tBoundingBox[1],self.tBoundingBox[2],self.tBoundingBox[3],self.tBoundingBox[4]
  234. l = l*b_screenX
  235. t = t*b_screenY
  236. r = r*b_screenX
  237. b = b*b_screenY
  238. end
  239. local type,att1,att2,att3,att4,att5 = self:type()
  240. if type == "border" or type == "stroke" then
  241. att2 = att2 or 0
  242. att3 = att3 or 0
  243. att4 = att4 or 0
  244. att5 = att5 or self.tColor[4]
  245. outlinesize = att1 or 2
  246. if outlinesize > 0 then
  247. for offsetX=-outlinesize,outlinesize,outlinesize do
  248. for offsetY=-outlinesize,outlinesize,outlinesize do
  249. if not (offsetX == 0 and offsetY == 0) then
  250. dxDrawText(self.strText, l + offsetX, t + offsetY, r + offsetX, b + offsetY, tocolor(att2, att3, att4, att5), self.fScale, self.strFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI )
  251. end
  252. end
  253. end
  254. end
  255. elseif type == "shadow" then
  256. local shadowDist = att1
  257. att2 = att2 or 0
  258. att3 = att3 or 0
  259. att4 = att4 or 0
  260. att5 = att5 or self.tColor[4]
  261. dxDrawText(self.strText, l + shadowDist, t + shadowDist, r + shadowDist, b + shadowDist, tocolor(att2, att3, att4, att5), self.fScale, self.strFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI )
  262. end
  263. dxDrawText ( self.strText, l, t, r, b, tocolor(unpack(self.tColor)), self.fScale, self.strFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI )
  264. break
  265. end
  266. end
  267. end
  268. )
  269.  
  270. if addEvent ( "updateDisplays", true ) then
  271. addEventHandler ( "updateDisplays", getRootElement(),
  272. function(self)
  273. setmetatable( self, dxText_mt )
  274. --Remove any old ones with the same id
  275. for text,_ in pairs(visibleText) do
  276. if text.id == self.id then
  277. visibleText[text] = nil
  278. end
  279. end
  280. if self.bVisible and not self.bDestroyed then
  281. visibleText[self] = true
  282. end
  283. end
  284. )
  285. end
Advertisement
Add Comment
Please, Sign In to add comment