annotate Libs/AceGUI-3.0/widgets/AceGUIContainer-ScrollFrame.lua @ 5:c31ee4251181

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