Mercurial > wow > askmrrobot
comparison ui/AmrUiScrollFrame.lua @ 57:01b63b8ed811 v21
total rewrite to version 21
author | yellowfive |
---|---|
date | Fri, 05 Jun 2015 11:05:15 -0700 |
parents | |
children | 0515882856f1 |
comparison
equal
deleted
inserted
replaced
56:75431c084aa0 | 57:01b63b8ed811 |
---|---|
1 --[[----------------------------------------------------------------------------- | |
2 ScrollFrame Container | |
3 Plain container that scrolls its content and doesn't grow in height. | |
4 Based on AceGUI ScrollFrame, but with a custom scrollbar look. | |
5 -------------------------------------------------------------------------------]] | |
6 local Type, Version = "AmrUiScrollFrame", 1 | |
7 local AceGUI = LibStub and LibStub("AceGUI-3.0", true) | |
8 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end | |
9 | |
10 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot") | |
11 | |
12 -- Lua APIs | |
13 local pairs, assert, type = pairs, assert, type | |
14 local min, max, floor, abs = math.min, math.max, math.floor, math.abs | |
15 | |
16 -- WoW APIs | |
17 local CreateFrame, UIParent = CreateFrame, UIParent | |
18 | |
19 --[[----------------------------------------------------------------------------- | |
20 Support functions | |
21 -------------------------------------------------------------------------------]] | |
22 local function FixScrollOnUpdate(frame) | |
23 frame:SetScript("OnUpdate", nil) | |
24 frame.obj:FixScroll() | |
25 end | |
26 | |
27 --[[----------------------------------------------------------------------------- | |
28 Scripts | |
29 -------------------------------------------------------------------------------]] | |
30 local function ScrollFrame_OnMouseWheel(frame, value) | |
31 frame.obj:MoveScroll(value) | |
32 end | |
33 | |
34 local function ScrollFrame_OnSizeChanged(frame) | |
35 frame:SetScript("OnUpdate", FixScrollOnUpdate) | |
36 end | |
37 | |
38 local function ScrollBar_OnScrollValueChanged(frame, value) | |
39 frame.obj:SetScroll(value) | |
40 end | |
41 | |
42 local function ScrollBar_UpArrowClick(frame) | |
43 frame.obj:MoveScroll(50) | |
44 end | |
45 | |
46 local function ScrollBar_DownArrowClick(frame) | |
47 frame.obj:MoveScroll(-50) | |
48 end | |
49 | |
50 --[[----------------------------------------------------------------------------- | |
51 Methods | |
52 -------------------------------------------------------------------------------]] | |
53 local methods = { | |
54 ["OnAcquire"] = function(self) | |
55 self:SetScroll(0) | |
56 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate) | |
57 end, | |
58 | |
59 ["OnRelease"] = function(self) | |
60 self.status = nil | |
61 for k in pairs(self.localstatus) do | |
62 self.localstatus[k] = nil | |
63 end | |
64 self.scrollframe:SetPoint("BOTTOMRIGHT") | |
65 self.scrollbar:Hide() | |
66 self.uparrow:Hide() | |
67 self.downarrow:Hide() | |
68 self.scrollBarShown = nil | |
69 self.content.height, self.content.width = nil, nil | |
70 end, | |
71 | |
72 ["SetScroll"] = function(self, value) | |
73 local status = self.status or self.localstatus | |
74 local viewheight = self.scrollframe:GetHeight() | |
75 local height = self.content:GetHeight() | |
76 local offset | |
77 | |
78 if viewheight > height then | |
79 offset = 0 | |
80 else | |
81 offset = floor((height - viewheight) / 1000.0 * value) | |
82 end | |
83 self.content:ClearAllPoints() | |
84 self.content:SetPoint("TOPLEFT", 0, offset) | |
85 self.content:SetPoint("TOPRIGHT", 0, offset) | |
86 status.offset = offset | |
87 status.scrollvalue = value | |
88 end, | |
89 | |
90 ["MoveScroll"] = function(self, value) | |
91 local status = self.status or self.localstatus | |
92 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight() | |
93 | |
94 if self.scrollBarShown then | |
95 local diff = height - viewheight | |
96 local delta = 1 | |
97 if value < 0 then | |
98 delta = -1 | |
99 end | |
100 self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000)) | |
101 end | |
102 end, | |
103 | |
104 ["FixScroll"] = function(self) | |
105 if self.updateLock then return end | |
106 self.updateLock = true | |
107 local status = self.status or self.localstatus | |
108 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight() | |
109 local offset = status.offset or 0 | |
110 local curvalue = self.scrollbar:GetValue() | |
111 -- Give us a margin of error of 2 pixels to stop some conditions that i would blame on floating point inaccuracys | |
112 -- No-one is going to miss 2 pixels at the bottom of the frame, anyhow! | |
113 if viewheight < height + 2 then | |
114 if self.scrollBarShown then | |
115 self.scrollBarShown = nil | |
116 self.scrollbar:Hide() | |
117 self.uparrow:Hide() | |
118 self.downarrow:Hide() | |
119 self.scrollbar:SetValue(0) | |
120 self.scrollframe:SetPoint("BOTTOMRIGHT") | |
121 self:DoLayout() | |
122 end | |
123 else | |
124 if not self.scrollBarShown then | |
125 self.scrollBarShown = true | |
126 self.scrollbar:Show() | |
127 self.uparrow:Show() | |
128 self.downarrow:Show() | |
129 self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0) | |
130 self:DoLayout() | |
131 end | |
132 local value = (offset / (viewheight - height) * 1000) | |
133 if value > 1000 then value = 1000 end | |
134 self.scrollbar:SetValue(value) | |
135 self:SetScroll(value) | |
136 if value < 1000 then | |
137 self.content:ClearAllPoints() | |
138 self.content:SetPoint("TOPLEFT", 0, offset) | |
139 self.content:SetPoint("TOPRIGHT", 0, offset) | |
140 status.offset = offset | |
141 end | |
142 end | |
143 self.updateLock = nil | |
144 end, | |
145 | |
146 ["LayoutFinished"] = function(self, width, height) | |
147 self.content:SetHeight(height or 0 + 20) | |
148 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate) | |
149 end, | |
150 | |
151 ["SetStatusTable"] = function(self, status) | |
152 assert(type(status) == "table") | |
153 self.status = status | |
154 if not status.scrollvalue then | |
155 status.scrollvalue = 0 | |
156 end | |
157 end, | |
158 | |
159 ["OnWidthSet"] = function(self, width) | |
160 local content = self.content | |
161 content.width = width | |
162 end, | |
163 | |
164 ["OnHeightSet"] = function(self, height) | |
165 local content = self.content | |
166 content.height = height | |
167 end | |
168 } | |
169 --[[----------------------------------------------------------------------------- | |
170 Constructor | |
171 -------------------------------------------------------------------------------]] | |
172 local function Constructor() | |
173 local frame = CreateFrame("Frame", nil, UIParent) | |
174 local num = AceGUI:GetNextWidgetNum(Type) | |
175 | |
176 local scrollframe = CreateFrame("ScrollFrame", nil, frame) | |
177 scrollframe:SetPoint("TOPLEFT") | |
178 scrollframe:SetPoint("BOTTOMRIGHT") | |
179 scrollframe:EnableMouseWheel(true) | |
180 scrollframe:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel) | |
181 scrollframe:SetScript("OnSizeChanged", ScrollFrame_OnSizeChanged) | |
182 | |
183 local scrollbar = CreateFrame("Slider", ("AmrUiScrollFrame%dScrollBar"):format(num), scrollframe) | |
184 scrollbar:SetPoint("TOPLEFT", scrollframe, "TOPRIGHT", 4, -16) | |
185 scrollbar:SetPoint("BOTTOMLEFT", scrollframe, "BOTTOMRIGHT", 4, 16) | |
186 scrollbar:SetMinMaxValues(0, 1000) | |
187 scrollbar:SetValueStep(1) | |
188 scrollbar:SetValue(0) | |
189 scrollbar:SetWidth(16) | |
190 scrollbar:Hide() | |
191 | |
192 local thumb = Amr.CreateTexture(scrollbar, Amr.Colors.Gray) | |
193 thumb:SetWidth(16) | |
194 thumb:SetHeight(32) | |
195 scrollbar:SetThumbTexture(thumb) | |
196 | |
197 local uparrow = CreateFrame("Button", nil, frame) | |
198 uparrow:SetWidth(16) | |
199 uparrow:SetHeight(16) | |
200 | |
201 local tex = uparrow:CreateTexture() | |
202 tex:SetWidth(16) | |
203 tex:SetHeight(16) | |
204 tex:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\IconScrollUp") | |
205 tex:SetPoint("LEFT", uparrow, "LEFT") | |
206 uparrow:SetNormalTexture(tex) | |
207 | |
208 uparrow:SetPoint("BOTTOMRIGHT", scrollbar, "TOPRIGHT") | |
209 uparrow:SetScript("OnClick", ScrollBar_UpArrowClick) | |
210 uparrow:Hide() | |
211 | |
212 local downarrow = CreateFrame("Button", nil, frame) | |
213 downarrow:SetWidth(16) | |
214 downarrow:SetHeight(16) | |
215 | |
216 tex = downarrow:CreateTexture() | |
217 tex:SetWidth(16) | |
218 tex:SetHeight(16) | |
219 tex:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\IconScrollDown") | |
220 tex:SetPoint("LEFT", downarrow, "LEFT") | |
221 downarrow:SetNormalTexture(tex) | |
222 | |
223 downarrow:SetPoint("TOPRIGHT", scrollbar, "BOTTOMRIGHT") | |
224 downarrow:SetScript("OnClick", ScrollBar_DownArrowClick) | |
225 downarrow:Hide() | |
226 | |
227 -- set the script as the last step, so it doesn't fire yet | |
228 scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged) | |
229 | |
230 local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND") | |
231 scrollbg:SetPoint("TOPLEFT", scrollbar, "TOPLEFT", 0, 16) | |
232 scrollbg:SetPoint("BOTTOMRIGHT", scrollbar, "BOTTOMRIGHT", 0, -16) | |
233 scrollbg:SetTexture(0, 0, 0, 0.3) | |
234 | |
235 --Container Support | |
236 local content = CreateFrame("Frame", nil, scrollframe) | |
237 content:SetPoint("TOPLEFT") | |
238 content:SetPoint("TOPRIGHT") | |
239 content:SetHeight(400) | |
240 scrollframe:SetScrollChild(content) | |
241 | |
242 local widget = { | |
243 localstatus = { scrollvalue = 0 }, | |
244 scrollframe = scrollframe, | |
245 uparrow = uparrow, | |
246 downarrow = downarrow, | |
247 scrollbar = scrollbar, | |
248 content = content, | |
249 frame = frame, | |
250 type = Type | |
251 } | |
252 for method, func in pairs(methods) do | |
253 widget[method] = func | |
254 end | |
255 scrollframe.obj, scrollbar.obj, uparrow.obj, downarrow.obj = widget, widget, widget, widget | |
256 | |
257 return AceGUI:RegisterAsContainer(widget) | |
258 end | |
259 | |
260 AceGUI:RegisterWidgetType(Type, Constructor, Version) |