yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: ScrollFrame Container yellowfive@57: Plain container that scrolls its content and doesn't grow in height. yellowfive@57: Based on AceGUI ScrollFrame, but with a custom scrollbar look. yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local Type, Version = "AmrUiScrollFrame", 1 yellowfive@57: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) yellowfive@57: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end yellowfive@57: yellowfive@57: local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot") yellowfive@57: yellowfive@57: -- Lua APIs yellowfive@57: local pairs, assert, type = pairs, assert, type yellowfive@133: local min, max, floor = math.min, math.max, math.floor yellowfive@57: yellowfive@57: -- WoW APIs yellowfive@57: local CreateFrame, UIParent = CreateFrame, UIParent yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Support functions yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local function FixScrollOnUpdate(frame) yellowfive@57: frame:SetScript("OnUpdate", nil) yellowfive@57: frame.obj:FixScroll() yellowfive@57: end yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Scripts yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local function ScrollFrame_OnMouseWheel(frame, value) yellowfive@57: frame.obj:MoveScroll(value) yellowfive@57: end yellowfive@57: yellowfive@57: local function ScrollFrame_OnSizeChanged(frame) yellowfive@57: frame:SetScript("OnUpdate", FixScrollOnUpdate) yellowfive@57: end yellowfive@57: yellowfive@57: local function ScrollBar_OnScrollValueChanged(frame, value) yellowfive@57: frame.obj:SetScroll(value) yellowfive@57: end yellowfive@57: yellowfive@57: local function ScrollBar_UpArrowClick(frame) yellowfive@57: frame.obj:MoveScroll(50) yellowfive@57: end yellowfive@57: yellowfive@57: local function ScrollBar_DownArrowClick(frame) yellowfive@57: frame.obj:MoveScroll(-50) yellowfive@57: end yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Methods yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local methods = { yellowfive@57: ["OnAcquire"] = function(self) yellowfive@57: self:SetScroll(0) yellowfive@57: self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate) yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnRelease"] = function(self) yellowfive@57: self.status = nil yellowfive@57: for k in pairs(self.localstatus) do yellowfive@57: self.localstatus[k] = nil yellowfive@57: end yellowfive@57: self.scrollframe:SetPoint("BOTTOMRIGHT") yellowfive@57: self.scrollbar:Hide() yellowfive@57: self.uparrow:Hide() yellowfive@57: self.downarrow:Hide() yellowfive@57: self.scrollBarShown = nil yellowfive@57: self.content.height, self.content.width = nil, nil yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetScroll"] = function(self, value) yellowfive@57: local status = self.status or self.localstatus yellowfive@57: local viewheight = self.scrollframe:GetHeight() yellowfive@57: local height = self.content:GetHeight() yellowfive@57: local offset yellowfive@57: yellowfive@57: if viewheight > height then yellowfive@57: offset = 0 yellowfive@57: else yellowfive@57: offset = floor((height - viewheight) / 1000.0 * value) yellowfive@57: end yellowfive@57: self.content:ClearAllPoints() yellowfive@57: self.content:SetPoint("TOPLEFT", 0, offset) yellowfive@57: self.content:SetPoint("TOPRIGHT", 0, offset) yellowfive@57: status.offset = offset yellowfive@57: status.scrollvalue = value yellowfive@57: end, yellowfive@57: yellowfive@57: ["MoveScroll"] = function(self, value) yellowfive@57: local status = self.status or self.localstatus yellowfive@57: local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight() yellowfive@57: yellowfive@57: if self.scrollBarShown then yellowfive@57: local diff = height - viewheight yellowfive@57: local delta = 1 yellowfive@57: if value < 0 then yellowfive@57: delta = -1 yellowfive@57: end yellowfive@57: self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000)) yellowfive@57: end yellowfive@57: end, yellowfive@57: yellowfive@57: ["FixScroll"] = function(self) yellowfive@57: if self.updateLock then return end yellowfive@57: self.updateLock = true yellowfive@57: local status = self.status or self.localstatus yellowfive@57: local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight() yellowfive@57: local offset = status.offset or 0 yellowfive@133: --local curvalue = self.scrollbar:GetValue() yellowfive@57: -- Give us a margin of error of 2 pixels to stop some conditions that i would blame on floating point inaccuracys yellowfive@57: -- No-one is going to miss 2 pixels at the bottom of the frame, anyhow! yellowfive@57: if viewheight < height + 2 then yellowfive@57: if self.scrollBarShown then yellowfive@57: self.scrollBarShown = nil yellowfive@57: self.scrollbar:Hide() yellowfive@57: self.uparrow:Hide() yellowfive@57: self.downarrow:Hide() yellowfive@57: self.scrollbar:SetValue(0) yellowfive@57: self.scrollframe:SetPoint("BOTTOMRIGHT") yellowfive@57: self:DoLayout() yellowfive@57: end yellowfive@57: else yellowfive@57: if not self.scrollBarShown then yellowfive@57: self.scrollBarShown = true yellowfive@57: self.scrollbar:Show() yellowfive@57: self.uparrow:Show() yellowfive@57: self.downarrow:Show() yellowfive@57: self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0) yellowfive@57: self:DoLayout() yellowfive@57: end yellowfive@57: local value = (offset / (viewheight - height) * 1000) yellowfive@57: if value > 1000 then value = 1000 end yellowfive@57: self.scrollbar:SetValue(value) yellowfive@57: self:SetScroll(value) yellowfive@57: if value < 1000 then yellowfive@57: self.content:ClearAllPoints() yellowfive@57: self.content:SetPoint("TOPLEFT", 0, offset) yellowfive@57: self.content:SetPoint("TOPRIGHT", 0, offset) yellowfive@57: status.offset = offset yellowfive@57: end yellowfive@57: end yellowfive@57: self.updateLock = nil yellowfive@57: end, yellowfive@57: yellowfive@57: ["LayoutFinished"] = function(self, width, height) yellowfive@57: self.content:SetHeight(height or 0 + 20) yellowfive@57: self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetStatusTable"] = function(self, status) yellowfive@57: assert(type(status) == "table") yellowfive@57: self.status = status yellowfive@57: if not status.scrollvalue then yellowfive@57: status.scrollvalue = 0 yellowfive@57: end yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnWidthSet"] = function(self, width) yellowfive@57: local content = self.content yellowfive@57: content.width = width yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnHeightSet"] = function(self, height) yellowfive@57: local content = self.content yellowfive@57: content.height = height yellowfive@57: end yellowfive@57: } yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Constructor yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local function Constructor() yellowfive@57: local frame = CreateFrame("Frame", nil, UIParent) yellowfive@57: local num = AceGUI:GetNextWidgetNum(Type) yellowfive@57: yellowfive@57: local scrollframe = CreateFrame("ScrollFrame", nil, frame) yellowfive@57: scrollframe:SetPoint("TOPLEFT") yellowfive@57: scrollframe:SetPoint("BOTTOMRIGHT") yellowfive@57: scrollframe:EnableMouseWheel(true) yellowfive@57: scrollframe:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel) yellowfive@57: scrollframe:SetScript("OnSizeChanged", ScrollFrame_OnSizeChanged) yellowfive@57: yellowfive@57: local scrollbar = CreateFrame("Slider", ("AmrUiScrollFrame%dScrollBar"):format(num), scrollframe) yellowfive@57: scrollbar:SetPoint("TOPLEFT", scrollframe, "TOPRIGHT", 4, -16) yellowfive@57: scrollbar:SetPoint("BOTTOMLEFT", scrollframe, "BOTTOMRIGHT", 4, 16) yellowfive@57: scrollbar:SetMinMaxValues(0, 1000) yellowfive@57: scrollbar:SetValueStep(1) yellowfive@57: scrollbar:SetValue(0) yellowfive@57: scrollbar:SetWidth(16) yellowfive@57: scrollbar:Hide() yellowfive@57: yellowfive@57: local thumb = Amr.CreateTexture(scrollbar, Amr.Colors.Gray) yellowfive@57: thumb:SetWidth(16) yellowfive@57: thumb:SetHeight(32) yellowfive@57: scrollbar:SetThumbTexture(thumb) yellowfive@57: yellowfive@57: local uparrow = CreateFrame("Button", nil, frame) yellowfive@57: uparrow:SetWidth(16) yellowfive@57: uparrow:SetHeight(16) yellowfive@57: yellowfive@57: local tex = uparrow:CreateTexture() yellowfive@57: tex:SetWidth(16) yellowfive@57: tex:SetHeight(16) yellowfive@57: tex:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\IconScrollUp") yellowfive@57: tex:SetPoint("LEFT", uparrow, "LEFT") yellowfive@57: uparrow:SetNormalTexture(tex) yellowfive@57: yellowfive@57: uparrow:SetPoint("BOTTOMRIGHT", scrollbar, "TOPRIGHT") yellowfive@57: uparrow:SetScript("OnClick", ScrollBar_UpArrowClick) yellowfive@57: uparrow:Hide() yellowfive@57: yellowfive@57: local downarrow = CreateFrame("Button", nil, frame) yellowfive@57: downarrow:SetWidth(16) yellowfive@57: downarrow:SetHeight(16) yellowfive@57: yellowfive@57: tex = downarrow:CreateTexture() yellowfive@57: tex:SetWidth(16) yellowfive@57: tex:SetHeight(16) yellowfive@57: tex:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\IconScrollDown") yellowfive@57: tex:SetPoint("LEFT", downarrow, "LEFT") yellowfive@57: downarrow:SetNormalTexture(tex) yellowfive@57: yellowfive@57: downarrow:SetPoint("TOPRIGHT", scrollbar, "BOTTOMRIGHT") yellowfive@57: downarrow:SetScript("OnClick", ScrollBar_DownArrowClick) yellowfive@57: downarrow:Hide() yellowfive@57: yellowfive@57: -- set the script as the last step, so it doesn't fire yet yellowfive@57: scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged) yellowfive@57: yellowfive@57: local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND") yellowfive@57: scrollbg:SetPoint("TOPLEFT", scrollbar, "TOPLEFT", 0, 16) yellowfive@57: scrollbg:SetPoint("BOTTOMRIGHT", scrollbar, "BOTTOMRIGHT", 0, -16) yellowfive@81: scrollbg:SetColorTexture(0, 0, 0, 0.3) yellowfive@57: yellowfive@57: --Container Support yellowfive@57: local content = CreateFrame("Frame", nil, scrollframe) yellowfive@57: content:SetPoint("TOPLEFT") yellowfive@57: content:SetPoint("TOPRIGHT") yellowfive@57: content:SetHeight(400) yellowfive@57: scrollframe:SetScrollChild(content) yellowfive@57: yellowfive@57: local widget = { yellowfive@57: localstatus = { scrollvalue = 0 }, yellowfive@57: scrollframe = scrollframe, yellowfive@57: uparrow = uparrow, yellowfive@57: downarrow = downarrow, yellowfive@57: scrollbar = scrollbar, yellowfive@57: content = content, yellowfive@57: frame = frame, yellowfive@57: type = Type yellowfive@57: } yellowfive@57: for method, func in pairs(methods) do yellowfive@57: widget[method] = func yellowfive@57: end yellowfive@57: scrollframe.obj, scrollbar.obj, uparrow.obj, downarrow.obj = widget, widget, widget, widget yellowfive@57: yellowfive@57: return AceGUI:RegisterAsContainer(widget) yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(Type, Constructor, Version)