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