comparison Libs/AceGUI-3.0/widgets/AceGUIContainer-ScrollFrame.lua @ 0:98c6f55e6619

First commit
author Xiiph
date Sat, 05 Feb 2011 16:45:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:98c6f55e6619
1 --[[-----------------------------------------------------------------------------
2 ScrollFrame Container
3 Plain container that scrolls its content and doesn't grow in height.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "ScrollFrame", 21
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 -- Lua APIs
10 local pairs, assert, type = pairs, assert, type
11 local min, max, floor, abs = math.min, math.max, math.floor, math.abs
12
13 -- WoW APIs
14 local CreateFrame, UIParent = CreateFrame, UIParent
15
16 --[[-----------------------------------------------------------------------------
17 Support functions
18 -------------------------------------------------------------------------------]]
19 local function FixScrollOnUpdate(frame)
20 frame:SetScript("OnUpdate", nil)
21 frame.obj:FixScroll()
22 end
23
24 --[[-----------------------------------------------------------------------------
25 Scripts
26 -------------------------------------------------------------------------------]]
27 local function ScrollFrame_OnMouseWheel(frame, value)
28 frame.obj:MoveScroll(value)
29 end
30
31 local function ScrollFrame_OnSizeChanged(frame)
32 frame:SetScript("OnUpdate", FixScrollOnUpdate)
33 end
34
35 local function ScrollBar_OnScrollValueChanged(frame, value)
36 frame.obj:SetScroll(value)
37 end
38
39 --[[-----------------------------------------------------------------------------
40 Methods
41 -------------------------------------------------------------------------------]]
42 local methods = {
43 ["OnAcquire"] = function(self)
44 self:SetScroll(0)
45 end,
46
47 ["OnRelease"] = function(self)
48 self.status = nil
49 for k in pairs(self.localstatus) do
50 self.localstatus[k] = nil
51 end
52 self.scrollframe:SetPoint("BOTTOMRIGHT")
53 self.scrollbar:Hide()
54 self.scrollBarShown = nil
55 self.content.height, self.content.width = nil, nil
56 end,
57
58 ["SetScroll"] = function(self, value)
59 local status = self.status or self.localstatus
60 local viewheight = self.scrollframe:GetHeight()
61 local height = self.content:GetHeight()
62 local offset
63
64 if viewheight > height then
65 offset = 0
66 else
67 offset = floor((height - viewheight) / 1000.0 * value)
68 end
69 self.content:ClearAllPoints()
70 self.content:SetPoint("TOPLEFT", 0, offset)
71 self.content:SetPoint("TOPRIGHT", 0, offset)
72 status.offset = offset
73 status.scrollvalue = value
74 end,
75
76 ["MoveScroll"] = function(self, value)
77 local status = self.status or self.localstatus
78 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
79
80 if self.scrollBarShown then
81 local diff = height - viewheight
82 local delta = 1
83 if value < 0 then
84 delta = -1
85 end
86 self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000))
87 end
88 end,
89
90 ["FixScroll"] = function(self)
91 if self.updateLock then return end
92 self.updateLock = true
93 local status = self.status or self.localstatus
94 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
95 local offset = status.offset or 0
96 local curvalue = self.scrollbar:GetValue()
97 -- Give us a margin of error of 2 pixels to stop some conditions that i would blame on floating point inaccuracys
98 -- No-one is going to miss 2 pixels at the bottom of the frame, anyhow!
99 if viewheight < height + 2 then
100 if self.scrollBarShown then
101 self.scrollBarShown = nil
102 self.scrollbar:Hide()
103 self.scrollbar:SetValue(0)
104 self.scrollframe:SetPoint("BOTTOMRIGHT")
105 self:DoLayout()
106 end
107 else
108 if not self.scrollBarShown then
109 self.scrollBarShown = true
110 self.scrollbar:Show()
111 self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0)
112 self:DoLayout()
113 end
114 local value = (offset / (viewheight - height) * 1000)
115 if value > 1000 then value = 1000 end
116 self.scrollbar:SetValue(value)
117 self:SetScroll(value)
118 if value < 1000 then
119 self.content:ClearAllPoints()
120 self.content:SetPoint("TOPLEFT", 0, offset)
121 self.content:SetPoint("TOPRIGHT", 0, offset)
122 status.offset = offset
123 end
124 end
125 self.updateLock = nil
126 end,
127
128 ["LayoutFinished"] = function(self, width, height)
129 self.content:SetHeight(height or 0 + 20)
130 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
131 end,
132
133 ["SetStatusTable"] = function(self, status)
134 assert(type(status) == "table")
135 self.status = status
136 if not status.scrollvalue then
137 status.scrollvalue = 0
138 end
139 end,
140
141 ["OnWidthSet"] = function(self, width)
142 local content = self.content
143 content.width = width
144 end,
145
146 ["OnHeightSet"] = function(self, height)
147 local content = self.content
148 content.height = height
149 end
150 }
151 --[[-----------------------------------------------------------------------------
152 Constructor
153 -------------------------------------------------------------------------------]]
154 local function Constructor()
155 local frame = CreateFrame("Frame", nil, UIParent)
156 local num = AceGUI:GetNextWidgetNum(Type)
157
158 local scrollframe = CreateFrame("ScrollFrame", nil, frame)
159 scrollframe:SetPoint("TOPLEFT")
160 scrollframe:SetPoint("BOTTOMRIGHT")
161 scrollframe:EnableMouseWheel(true)
162 scrollframe:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
163 scrollframe:SetScript("OnSizeChanged", ScrollFrame_OnSizeChanged)
164
165 local scrollbar = CreateFrame("Slider", ("AceConfigDialogScrollFrame%dScrollBar"):format(num), scrollframe, "UIPanelScrollBarTemplate")
166 scrollbar:SetPoint("TOPLEFT", scrollframe, "TOPRIGHT", 4, -16)
167 scrollbar:SetPoint("BOTTOMLEFT", scrollframe, "BOTTOMRIGHT", 4, 16)
168 scrollbar:SetMinMaxValues(0, 1000)
169 scrollbar:SetValueStep(1)
170 scrollbar:SetValue(0)
171 scrollbar:SetWidth(16)
172 scrollbar:Hide()
173 -- set the script as the last step, so it doesn't fire yet
174 scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged)
175
176 local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
177 scrollbg:SetAllPoints(scrollbar)
178 scrollbg:SetTexture(0, 0, 0, 0.4)
179
180 --Container Support
181 local content = CreateFrame("Frame", nil, scrollframe)
182 content:SetPoint("TOPLEFT")
183 content:SetPoint("TOPRIGHT")
184 content:SetHeight(400)
185 scrollframe:SetScrollChild(content)
186
187 local widget = {
188 localstatus = { scrollvalue = 0 },
189 scrollframe = scrollframe,
190 scrollbar = scrollbar,
191 content = content,
192 frame = frame,
193 type = Type
194 }
195 for method, func in pairs(methods) do
196 widget[method] = func
197 end
198 scrollframe.obj, scrollbar.obj = widget, widget
199
200 return AceGUI:RegisterAsContainer(widget)
201 end
202
203 AceGUI:RegisterWidgetType(Type, Constructor, Version)