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

Reasonably functional now. Cleaning up some stuff which might have to be reverted.
author Zerotorescue
date Thu, 07 Oct 2010 17:17:43 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c6ff7ba0e8f6
1 --[[-----------------------------------------------------------------------------
2 ScrollFrame Container
3 Plain container that scrolls its content and doesn't grow in height.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "ScrollFrame", 20
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 = math.min, math.max, math.floor
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 height > viewheight then
81 self.scrollbar:Hide()
82 else
83 self.scrollbar:Show()
84 local diff = height - viewheight
85 local delta = 1
86 if value < 0 then
87 delta = -1
88 end
89 self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000))
90 end
91 end,
92
93 ["FixScroll"] = function(self)
94 if self.updateLock then return end
95 self.updateLock = true
96 local status = self.status or self.localstatus
97 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
98 local offset = status.offset or 0
99 local curvalue = self.scrollbar:GetValue()
100 if viewheight < height then
101 if self.scrollBarShown then
102 self.scrollBarShown = nil
103 self.scrollbar:Hide()
104 self.scrollbar:SetValue(0)
105 self.scrollframe:SetPoint("BOTTOMRIGHT")
106 self:DoLayout()
107 end
108 else
109 if not self.scrollBarShown then
110 self.scrollBarShown = true
111 self.scrollbar:Show()
112 self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0)
113 self:DoLayout()
114 end
115 local value = (offset / (viewheight - height) * 1000)
116 if value > 1000 then value = 1000 end
117 self.scrollbar:SetValue(value)
118 self:SetScroll(value)
119 if value < 1000 then
120 self.content:ClearAllPoints()
121 self.content:SetPoint("TOPLEFT", 0, offset)
122 self.content:SetPoint("TOPRIGHT", 0, offset)
123 status.offset = offset
124 end
125 end
126 self.updateLock = nil
127 end,
128
129 ["LayoutFinished"] = function(self, width, height)
130 self.content:SetHeight(height or 0 + 20)
131 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
132 end,
133
134 ["SetStatusTable"] = function(self, status)
135 assert(type(status) == "table")
136 self.status = status
137 if not status.scrollvalue then
138 status.scrollvalue = 0
139 end
140 end,
141
142 ["OnWidthSet"] = function(self, width)
143 local content = self.content
144 content.width = width
145 end,
146
147 ["OnHeightSet"] = function(self, height)
148 local content = self.content
149 content.height = height
150 end
151 }
152 --[[-----------------------------------------------------------------------------
153 Constructor
154 -------------------------------------------------------------------------------]]
155 local function Constructor()
156 local frame = CreateFrame("Frame", nil, UIParent)
157 local num = AceGUI:GetNextWidgetNum(Type)
158
159 local scrollframe = CreateFrame("ScrollFrame", nil, frame)
160 scrollframe:SetPoint("TOPLEFT")
161 scrollframe:SetPoint("BOTTOMRIGHT")
162 scrollframe:EnableMouseWheel(true)
163 scrollframe:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
164 scrollframe:SetScript("OnSizeChanged", ScrollFrame_OnSizeChanged)
165
166 local scrollbar = CreateFrame("Slider", ("AceConfigDialogScrollFrame%dScrollBar"):format(num), scrollframe, "UIPanelScrollBarTemplate")
167 scrollbar:SetPoint("TOPLEFT", scrollframe, "TOPRIGHT", 4, -16)
168 scrollbar:SetPoint("BOTTOMLEFT", scrollframe, "BOTTOMRIGHT", 4, 16)
169 scrollbar:SetMinMaxValues(0, 1000)
170 scrollbar:SetValueStep(1)
171 scrollbar:SetValue(0)
172 scrollbar:SetWidth(16)
173 scrollbar:Hide()
174 -- set the script as the last step, so it doesn't fire yet
175 scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged)
176
177 local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
178 scrollbg:SetAllPoints(scrollbar)
179 scrollbg:SetTexture(0, 0, 0, 0.4)
180
181 --Container Support
182 local content = CreateFrame("Frame", nil, scrollframe)
183 content:SetPoint("TOPLEFT")
184 content:SetPoint("TOPRIGHT")
185 content:SetHeight(400)
186 scrollframe:SetScrollChild(content)
187
188 local widget = {
189 localstatus = { scrollvalue = 0 },
190 scrollframe = scrollframe,
191 scrollbar = scrollbar,
192 content = content,
193 frame = frame,
194 type = Type
195 }
196 for method, func in pairs(methods) do
197 widget[method] = func
198 end
199 scrollframe.obj, scrollbar.obj = widget, widget
200
201 return AceGUI:RegisterAsContainer(widget)
202 end
203
204 AceGUI:RegisterWidgetType(Type, Constructor, Version)