/Tukui/core/api.lua

http://github.com/Asphyxia/Tukui · Lua · 483 lines · 407 code · 64 blank · 12 comment · 99 complexity · 886ae5e2f215dd48af4fbd47c8b3995e MD5 · raw file

  1. -- Tukui API, see DOCS/API.txt for more informations
  2. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  3. local noop = T.dummy
  4. local floor = math.floor
  5. local class = T.myclass
  6. local texture = C.media.blank
  7. local backdropr, backdropg, backdropb, backdropa, borderr, borderg, borderb = 0, 0, 0, 1, 0, 0, 0
  8. -- pixel perfect script of custom ui Scale.
  9. local mult = 768/string.match(GetCVar("gxResolution"), "%d+x(%d+)")/C["general"].uiscale
  10. local Scale = function(x)
  11. return mult*math.floor(x/mult+.5)
  12. end
  13. T.Scale = function(x) return Scale(x) end
  14. T.mult = mult
  15. ---------------------------------------------------
  16. -- TEMPLATES
  17. ---------------------------------------------------
  18. local function GetTemplate(t)
  19. if t == "Tukui" then
  20. borderr, borderg, borderb = .6, .6, .6
  21. backdropr, backdropg, backdropb = .1, .1, .1
  22. elseif t == "ClassColor" then
  23. local c = T.oUF_colors.class[class]
  24. borderr, borderg, borderb = c[1], c[2], c[3]
  25. backdropr, backdropg, backdropb = unpack(C["media"].backdropcolor)
  26. elseif t == "Elv" then
  27. borderr, borderg, borderb = .3, .3, .3
  28. backdropr, backdropg, backdropb = .1, .1, .1
  29. elseif t == "Duffed" then
  30. borderr, borderg, borderb = .2, .2, .2
  31. backdropr, backdropg, backdropb = .02, .02, .02
  32. elseif t == "Dajova" then
  33. borderr, borderg, borderb = .05, .05, .05
  34. backdropr, backdropg, backdropb = .1, .1, .1
  35. elseif t == "Eclipse" then
  36. borderr, borderg, borderb = .1, .1, .1
  37. backdropr, backdropg, backdropb = 0, 0, 0
  38. elseif t == "Hydra" then
  39. borderr, borderg, borderb = .2, .2, .2
  40. backdropr, backdropg, backdropb = .075, .075, .075
  41. elseif t == "ThickTransparent" then
  42. borderr, borderg, borderb = unpack(C["media"].bordercolor)
  43. backdropr, backdropg, backdropb = 0,0,0
  44. elseif t == "Thin" then
  45. borderr, borderg, borderb = 0,0,0
  46. backdropr, backdropg, backdropb = unpack(C["media"].backdropcolor)
  47. else
  48. borderr, borderg, borderb = unpack(C["media"].bordercolor)
  49. backdropr, backdropg, backdropb = unpack(C["media"].backdropcolor)
  50. end
  51. end
  52. ---------------------------------------------------
  53. -- END OF TEMPLATES
  54. ---------------------------------------------------
  55. local function Size(frame, width, height)
  56. frame:SetSize(Scale(width), Scale(height or width))
  57. end
  58. local function Width(frame, width)
  59. frame:SetWidth(Scale(width))
  60. end
  61. local function Height(frame, height)
  62. frame:SetHeight(Scale(height))
  63. end
  64. local function Point(obj, arg1, arg2, arg3, arg4, arg5)
  65. -- anyone has a more elegant way for this?
  66. if type(arg1)=="number" then arg1 = Scale(arg1) end
  67. if type(arg2)=="number" then arg2 = Scale(arg2) end
  68. if type(arg3)=="number" then arg3 = Scale(arg3) end
  69. if type(arg4)=="number" then arg4 = Scale(arg4) end
  70. if type(arg5)=="number" then arg5 = Scale(arg5) end
  71. obj:SetPoint(arg1, arg2, arg3, arg4, arg5)
  72. end
  73. local function CreateBackdrop(f, t, tex)
  74. if not t then t = "Default" end
  75. local b = CreateFrame("Frame", nil, f)
  76. b:Point("TOPLEFT", -2, 2)
  77. b:Point("BOTTOMRIGHT", 2, -2)
  78. b:SetTemplate(t, tex)
  79. if f:GetFrameLevel() - 1 >= 0 then
  80. b:SetFrameLevel(f:GetFrameLevel() - 1)
  81. else
  82. b:SetFrameLevel(0)
  83. end
  84. f.backdrop = b
  85. end
  86. local function SetTemplate(f, t, tex)
  87. if tex then texture = C.media.normTex else texture = C.media.blank end
  88. GetTemplate(t)
  89. f:SetBackdrop({
  90. bgFile = texture,
  91. edgeFile = C.media.blank,
  92. tile = false, tileSize = 0, edgeSize = mult,
  93. insets = { left = -mult, right = -mult, top = -mult, bottom = -mult}
  94. })
  95. if t == "Thin" then
  96. f:SetBackdrop({
  97. bgFile = texture,
  98. edgeFile = C.media.blank,
  99. tile = false, tileSize = 0, edgeSize = mult,
  100. insets = { left = 0, right = 0, top = 0, bottom = 0 }
  101. })
  102. else
  103. f:SetBackdrop({
  104. bgFile = texture,
  105. edgeFile = C.media.blank,
  106. tile = false, tileSize = 0, edgeSize = mult,
  107. insets = { left = -mult, right = -mult, top = -mult, bottom = -mult }
  108. })
  109. end
  110. if t == "ThickTransparent" then
  111. outerBorder(f)
  112. innerBorder(f)
  113. backdropa = 0.8
  114. elseif t == "Transparent" then
  115. backdropa = 0.7
  116. elseif t == "Invisible" then
  117. backdropa = 0
  118. bordera = 0
  119. elseif t == "ThickBorder" then
  120. outerBorder(f)
  121. innerBorder(f)
  122. bordera = 1
  123. backdropa = 1
  124. else
  125. bordera = 1
  126. backdropa = 1
  127. end
  128. f:SetBackdropColor(backdropr, backdropg, backdropb, backdropa)
  129. f:SetBackdropBorderColor(borderr, borderg, borderb, bordera)
  130. f.bg = f
  131. end
  132. local function CreatePanel(f, t, w, h, a1, p, a2, x, y, text)
  133. GetTemplate(t)
  134. if t == "Transparent" then
  135. f:CreateBorder(true, true)
  136. backdropa = .7
  137. else
  138. backdropa = 1
  139. end
  140. local sh = Scale(h)
  141. local sw = Scale(w)
  142. f:SetFrameLevel(1)
  143. f:SetHeight(sh)
  144. f:SetWidth(sw)
  145. f:SetFrameStrata("BACKGROUND")
  146. f:SetPoint(a1, p, a2, Scale(x), Scale(y))
  147. f:SetBackdrop({
  148. bgFile = C["media"].normTex,
  149. edgeFile = C["media"].blank,
  150. tile = false, tileSize = 0, edgeSize = mult,
  151. insets = { left = -mult, right = -mult, top = -mult, bottom = -mult}
  152. })
  153. f:SetBackdropColor(backdropr, backdropg, backdropb, backdropa)
  154. f:SetBackdropBorderColor(borderr, borderg, borderb)
  155. if t == "Transparent" then
  156. f:CreateShadow()
  157. end
  158. if text then
  159. if f.text then return end
  160. local text = T.SetFontString(f, C.media.pixelfont, 12, "MONOCHROMEOUTLINE")
  161. text:Point("CENTER", f, 1, 1)
  162. text:SetJustifyH("CENTER")
  163. f.text = text
  164. end
  165. end
  166. local function CreateShadow(f, t)
  167. if f.shadow then return end -- we seriously don't want to create shadow 2 times in a row on the same frame.
  168. borderr, borderg, borderb = 0, 0, 0
  169. backdropr, backdropg, backdropb = 0, 0, 0
  170. if t == "ClassColor" then
  171. local c = T.oUF_colors.class[class]
  172. borderr, borderg, borderb = c[1], c[2], c[3]
  173. backdropr, backdropg, backdropb = unpack(C["media"].backdropcolor)
  174. end
  175. local shadow = CreateFrame("Frame", nil, f)
  176. shadow:SetFrameLevel(1)
  177. shadow:SetFrameStrata(f:GetFrameStrata())
  178. shadow:Point("TOPLEFT", -3, 3)
  179. shadow:Point("BOTTOMLEFT", -3, -3)
  180. shadow:Point("TOPRIGHT", 3, 3)
  181. shadow:Point("BOTTOMRIGHT", 3, -3)
  182. shadow:SetBackdrop( {
  183. edgeFile = C["media"].glowTex, edgeSize = T.Scale(3),
  184. insets = {left = T.Scale(5), right = T.Scale(5), top = T.Scale(5), bottom = T.Scale(5)},
  185. })
  186. shadow:SetBackdropColor(backdropr, backdropg, backdropb, 0)
  187. shadow:SetBackdropBorderColor(borderr, borderg, borderb, 0.8)
  188. f.shadow = shadow
  189. end
  190. -- Credits to Eclipse
  191. local function CreateOverlay(f)
  192. if f.overlay then return end
  193. local overlay = f:CreateTexture(f:GetName() and f:GetName().."Overlay" or nil, "BORDER", f)
  194. overlay:ClearAllPoints()
  195. overlay:Point("TOPLEFT", 2, -2)
  196. overlay:Point("BOTTOMRIGHT", -2, 2)
  197. overlay:SetTexture(C.media.normTex)
  198. overlay:SetVertexColor(.05, .05, .05)
  199. f.overlay = overlay
  200. end
  201. local function Kill(object)
  202. if object.UnregisterAllEvents then
  203. object:UnregisterAllEvents()
  204. end
  205. object.Show = noop
  206. object:Hide()
  207. end
  208. local function StripTextures(object, kill)
  209. for i=1, object:GetNumRegions() do
  210. local region = select(i, object:GetRegions())
  211. if region:GetObjectType() == "Texture" then
  212. if kill then
  213. region:Kill()
  214. else
  215. region:SetTexture(nil)
  216. end
  217. end
  218. end
  219. end
  220. -- styleButton function authors are Chiril & Karudon.
  221. local function StyleButton(b, c)
  222. local name = b:GetName()
  223. local button = _G[name]
  224. local icon = _G[name.."Icon"]
  225. local count = _G[name.."Count"]
  226. local border = _G[name.."Border"]
  227. local hotkey = _G[name.."HotKey"]
  228. local cooldown = _G[name.."Cooldown"]
  229. local nametext = _G[name.."Name"]
  230. local flash = _G[name.."Flash"]
  231. local normaltexture = _G[name.."NormalTexture"]
  232. local icontexture = _G[name.."IconTexture"]
  233. local hover = b:CreateTexture("frame", nil, self) -- hover
  234. hover:SetTexture(1,1,1, .03)
  235. hover:SetHeight(button:GetHeight())
  236. hover:SetWidth(button:GetWidth())
  237. hover:Point("TOPLEFT",button,2,-2)
  238. hover:Point("BOTTOMRIGHT",button,-2,2)
  239. button:SetHighlightTexture(hover)
  240. local pushed = b:CreateTexture("frame", nil, self) -- pushed
  241. pushed:SetTexture(1,1,1,0.3)
  242. pushed:SetHeight(button:GetHeight())
  243. pushed:SetWidth(button:GetWidth())
  244. pushed:Point("TOPLEFT",button,2,-2)
  245. pushed:Point("BOTTOMRIGHT",button,-2,2)
  246. button:SetPushedTexture(pushed)
  247. if c then
  248. local checked = b:CreateTexture("frame", nil, self) -- checked
  249. checked:SetTexture(1,1,1,0.4)
  250. checked:SetHeight(button:GetHeight())
  251. checked:SetWidth(button:GetWidth())
  252. checked:Point("TOPLEFT",button,2,-2)
  253. checked:Point("BOTTOMRIGHT",button,-2,2)
  254. button:SetCheckedTexture(checked)
  255. end
  256. end
  257. local function FontString(parent, name, fontName, fontHeight, fontStyle)
  258. local fs = parent:CreateFontString(nil, "OVERLAY")
  259. fs:SetJustifyH("LEFT")
  260. fs:SetShadowOffset(0,0)
  261. fs:SetFont(fontName, fontHeight, fontStyle)
  262. if not name then
  263. parent.text = fs
  264. else
  265. parent[name] = fs
  266. end
  267. return fs
  268. end
  269. local function HighlightTarget(self, event, unit)
  270. if self.unit == "target" then return end
  271. if UnitIsUnit('target', self.unit) then
  272. self.HighlightTarget:Show()
  273. else
  274. self.HighlightTarget:Hide()
  275. end
  276. end
  277. local function HighlightUnit(f, r, g, b)
  278. if f.HighlightTarget then return end
  279. local glowBorder = {edgeFile = C["media"].blank, edgeSize = 1}
  280. f.HighlightTarget = CreateFrame("Frame", nil, f)
  281. f.HighlightTarget:Point("TOPLEFT", f, "TOPLEFT", -2, 2)
  282. f.HighlightTarget:Point("BOTTOMRIGHT", f, "BOTTOMRIGHT", 2, -2)
  283. f.HighlightTarget:SetBackdrop(glowBorder)
  284. f.HighlightTarget:SetFrameLevel(f:GetFrameLevel() + 1)
  285. f.HighlightTarget:SetBackdropBorderColor(r,g,b,1)
  286. f.HighlightTarget:Hide()
  287. f:RegisterEvent("PLAYER_TARGET_CHANGED", HighlightTarget)
  288. end
  289. local function CreateBorder(f, i, o)
  290. if i then
  291. if f.iborder then return end
  292. local border = CreateFrame("Frame", f:GetName() and f:GetName() .. "InnerBorder" or nil, f)
  293. border:Point("TOPLEFT", mult, -mult)
  294. border:Point("BOTTOMRIGHT", -mult, mult)
  295. border:SetBackdrop({
  296. edgeFile = C["media"].blank,
  297. edgeSize = mult,
  298. insets = { left = mult, right = mult, top = mult, bottom = mult }
  299. })
  300. border:SetBackdropBorderColor(0, 0, 0)
  301. f.iborder = border
  302. end
  303. if o then
  304. if f.oborder then return end
  305. local border = CreateFrame("Frame", f:GetName() and f:GetName() .. "OuterBorder" or nil, f)
  306. border:Point("TOPLEFT", -mult, mult)
  307. border:Point("BOTTOMRIGHT", mult, -mult)
  308. border:SetFrameLevel(f:GetFrameLevel() + 1)
  309. border:SetBackdrop({
  310. edgeFile = C["media"].blank,
  311. edgeSize = mult,
  312. insets = { left = mult, right = mult, top = mult, bottom = mult }
  313. })
  314. border:SetBackdropBorderColor(0,0,0)
  315. f.oborder = border
  316. end
  317. end
  318. -- Animation functions
  319. local function Animate(self, x, y, duration)
  320. self.anim = self:CreateAnimationGroup("Move_In")
  321. self.anim.in1 = self.anim:CreateAnimation("Translation")
  322. self.anim.in1:SetDuration(0)
  323. self.anim.in1:SetOrder(1)
  324. self.anim.in2 = self.anim:CreateAnimation("Translation")
  325. self.anim.in2:SetDuration(duration)
  326. self.anim.in2:SetOrder(2)
  327. self.anim.in2:SetSmoothing("OUT")
  328. self.anim.out1 = self:CreateAnimationGroup("Move_Out")
  329. self.anim.out2 = self.anim.out1:CreateAnimation("Translation")
  330. self.anim.out2:SetDuration(duration)
  331. self.anim.out2:SetOrder(1)
  332. self.anim.out2:SetSmoothing("IN")
  333. self.anim.in1:SetOffset(Scale(x), Scale(y))
  334. self.anim.in2:SetOffset(Scale(-x), Scale(-y))
  335. self.anim.out2:SetOffset(Scale(x), Scale(y))
  336. self.anim.out1:SetScript("OnFinished", function() self:Hide() end)
  337. end
  338. local function SlideIn(self)
  339. if not self.anim then
  340. Animate(self)
  341. end
  342. self.anim.out1:Stop()
  343. self:Show()
  344. self.anim:Play()
  345. end
  346. local function SlideOut(self)
  347. if self.anim then
  348. self.anim:Finish()
  349. end
  350. self.anim:Stop()
  351. self.anim.out1:Play()
  352. end
  353. local function FadeIn(f)
  354. UIFrameFadeIn(f, .4, f:GetAlpha(), 1)
  355. end
  356. local function FadeOut(f)
  357. UIFrameFadeOut(f, .8, f:GetAlpha(), 0)
  358. end
  359. T.SetUpAnimGroup = function(self)
  360. self.anim = self:CreateAnimationGroup("Flash")
  361. self.anim.fadein = self.anim:CreateAnimation("ALPHA", "FadeIn")
  362. self.anim.fadein:SetChange(1)
  363. self.anim.fadein:SetOrder(2)
  364. self.anim.fadeout = self.anim:CreateAnimation("ALPHA", "FadeOut")
  365. self.anim.fadeout:SetChange(-1)
  366. self.anim.fadeout:SetOrder(1)
  367. end
  368. T.Flash = function(self, duration)
  369. if not self.anim then
  370. T.SetUpAnimGroup(self)
  371. end
  372. self.anim.fadein:SetDuration(duration)
  373. self.anim.fadeout:SetDuration(duration)
  374. self.anim:Play()
  375. end
  376. T.StopFlash = function(self)
  377. if self.anim then
  378. self.anim:Finish()
  379. end
  380. end
  381. local function addapi(object)
  382. local mt = getmetatable(object).__index
  383. if not object.Size then mt.Size = Size end
  384. if not object.Point then mt.Point = Point end
  385. if not object.ApplyBorder then mt.ApplyBorder = ApplyBorder end
  386. if not object.SetTemplate then mt.SetTemplate = SetTemplate end
  387. if not object.CreateBackdrop then mt.CreateBackdrop = CreateBackdrop end
  388. if not object.StripTextures then mt.StripTextures = StripTextures end
  389. if not object.CreatePanel then mt.CreatePanel = CreatePanel end
  390. if not object.CreateShadow then mt.CreateShadow = CreateShadow end
  391. if not object.Kill then mt.Kill = Kill end
  392. if not object.StyleButton then mt.StyleButton = StyleButton end
  393. if not object.Width then mt.Width = Width end
  394. if not object.Height then mt.Height = Height end
  395. if not object.FontString then mt.FontString = FontString end
  396. if not object.CreateOverlay then mt.CreateOverlay = CreateOverlay end
  397. if not object.HighlightUnit then mt.HighlightUnit = HighlightUnit end
  398. if not object.CreateBorder then mt.CreateBorder = CreateBorder end
  399. if not object.Animate then mt.Animate = Animate end
  400. if not object.SlideIn then mt.SlideIn = SlideIn end
  401. if not object.SlideOut then mt.SlideOut = SlideOut end
  402. if not object.FadeIn then mt.FadeIn = FadeIn end
  403. if not object.FadeOut then mt.FadeOut = FadeOut end
  404. if not object.Flash then mt.Flash = Flash end
  405. if not object.StopFlash then mt.StopFlash = StopFlash end
  406. end
  407. local handled = {["Frame"] = true}
  408. local object = CreateFrame("Frame")
  409. addapi(object)
  410. addapi(object:CreateTexture())
  411. addapi(object:CreateFontString())
  412. object = EnumerateFrames()
  413. while object do
  414. if not handled[object:GetObjectType()] then
  415. addapi(object)
  416. handled[object:GetObjectType()] = true
  417. end
  418. object = EnumerateFrames(object)
  419. end