Mercurial > wow > askmrrobot
comparison ui/AmrUiLabel.lua @ 57:01b63b8ed811 v21
total rewrite to version 21
author | yellowfive |
---|---|
date | Fri, 05 Jun 2015 11:05:15 -0700 |
parents | |
children | e31b02b24488 |
comparison
equal
deleted
inserted
replaced
56:75431c084aa0 | 57:01b63b8ed811 |
---|---|
1 --[[----------------------------------------------------------------------------- | |
2 Label Widget | |
3 Displays text. | |
4 -------------------------------------------------------------------------------]] | |
5 local Type, Version = "AmrUiLabel", 1 | |
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true) | |
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end | |
8 | |
9 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot") | |
10 | |
11 -- Lua APIs | |
12 local max, select, pairs = math.max, select, pairs | |
13 | |
14 -- WoW APIs | |
15 local CreateFrame, UIParent = CreateFrame, UIParent | |
16 | |
17 | |
18 --[[----------------------------------------------------------------------------- | |
19 Support functions | |
20 -------------------------------------------------------------------------------]] | |
21 | |
22 local function updateSize(self) | |
23 if self.resizing then return end | |
24 local frame = self.frame | |
25 local width = frame.width or frame:GetWidth() or 0 | |
26 local label = self.label | |
27 local height | |
28 | |
29 label:ClearAllPoints() | |
30 label:SetPoint("TOPLEFT") | |
31 label:SetWidth(width) | |
32 height = label:GetHeight() | |
33 | |
34 self.resizing = true | |
35 frame:SetHeight(height) | |
36 frame.height = height | |
37 self.resizing = nil | |
38 end | |
39 | |
40 --[[----------------------------------------------------------------------------- | |
41 Scripts | |
42 -------------------------------------------------------------------------------]] | |
43 | |
44 local function frameOnEnter(frame) | |
45 frame.obj:Fire("OnEnter") | |
46 end | |
47 | |
48 local function frameOnLeave(frame) | |
49 frame.obj:Fire("OnLeave") | |
50 end | |
51 | |
52 local function frameOnMouseDown(frame, ...) | |
53 frame.obj:Fire("OnMouseDown", ...) | |
54 end | |
55 | |
56 local function frameOnMouseUp(frame, ...) | |
57 frame.obj:Fire("OnMouseUp", ...) | |
58 end | |
59 | |
60 --[[----------------------------------------------------------------------------- | |
61 Methods | |
62 -------------------------------------------------------------------------------]] | |
63 local methods = { | |
64 ["OnAcquire"] = function(self) | |
65 -- set the flag to stop constant size updates | |
66 self.resizing = true | |
67 -- height is set dynamically by the text size | |
68 self:SetWidth(200) | |
69 self:SetText() | |
70 self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text)) | |
71 self:SetJustifyH("LEFT") | |
72 self:SetJustifyV("MIDDLE") | |
73 self:SetWordWrap(true) | |
74 self:SetVisible(true) | |
75 | |
76 -- reset the flag | |
77 self.resizing = nil | |
78 -- run the update explicitly | |
79 updateSize(self) | |
80 end, | |
81 | |
82 -- ["OnRelease"] = nil, | |
83 | |
84 ["OnWidthSet"] = function(self, width) | |
85 updateSize(self) | |
86 end, | |
87 | |
88 ["GetHeight"] = function(self) | |
89 return self.frame:GetHeight() | |
90 end, | |
91 | |
92 ["SetText"] = function(self, text) | |
93 self.label:SetText(text) | |
94 updateSize(self) | |
95 end, | |
96 | |
97 ["SetFont"] = function(self, font) | |
98 self.label:SetFontObject(font) | |
99 updateSize(self) | |
100 end, | |
101 | |
102 ["SetJustifyV"] = function(self, val) | |
103 self.label:SetJustifyV(val) | |
104 end, | |
105 | |
106 ["SetJustifyH"] = function(self, val) | |
107 self.label:SetJustifyH(val) | |
108 end, | |
109 | |
110 ["SetWordWrap"] = function(self, enable) | |
111 self.label:SetWordWrap(enable) | |
112 updateSize(self) | |
113 end, | |
114 | |
115 ["SetVisible"] = function(self, visible) | |
116 if visible then | |
117 self.frame:Show() | |
118 else | |
119 self.frame:Hide() | |
120 end | |
121 end | |
122 } | |
123 | |
124 --[[----------------------------------------------------------------------------- | |
125 Constructor | |
126 -------------------------------------------------------------------------------]] | |
127 local function Constructor() | |
128 local frame = CreateFrame("Frame", nil, UIParent) | |
129 frame:Hide() | |
130 | |
131 frame:SetScript("OnEnter", frameOnEnter) | |
132 frame:SetScript("OnLeave", frameOnLeave) | |
133 frame:SetScript("OnMouseDown", frameOnMouseDown) | |
134 frame:SetScript("OnMouseUp", frameOnMouseUp) | |
135 | |
136 local label = frame:CreateFontString(nil, "ARTWORK") | |
137 label:SetFontObject(Amr.CreateFont("Regular", 16, Amr.Colors.Text)) | |
138 | |
139 -- create widget | |
140 local widget = { | |
141 label = label, | |
142 frame = frame, | |
143 type = Type | |
144 } | |
145 for method, func in pairs(methods) do | |
146 widget[method] = func | |
147 end | |
148 | |
149 return AceGUI:RegisterAsWidget(widget) | |
150 end | |
151 | |
152 AceGUI:RegisterWidgetType(Type, Constructor, Version) |