annotate Libs/AceGUI-3.0/widgets/AceGUIContainer-ScrollFrame.lua @ 11:371e14cd2feb

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