annotate Libs/AceGUI-3.0/widgets/AceGUIContainer-ScrollFrame.lua @ 63:3552946c0b9a tip

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