yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Dropdown picker widget. yellowfive@57: Simple version that handles our limited needs. yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local Type, Version = "AmrUiDropDown", 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 = pairs yellowfive@57: yellowfive@57: -- WoW APIs yellowfive@57: local _G = _G yellowfive@57: local PlaySound, CreateFrame, UIParent = PlaySound, CreateFrame, UIParent yellowfive@57: yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Scripts yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local function buttonOnClick(frame, ...) yellowfive@57: if frame.obj.list:IsVisible() then yellowfive@57: frame.obj.list:Hide() yellowfive@57: else yellowfive@57: frame.obj.list:Show() yellowfive@57: frame.obj:RenderItems() yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function buttonOnEnter(frame) yellowfive@57: frame.obj:Fire("OnEnter") yellowfive@57: end yellowfive@57: yellowfive@57: local function buttonOnLeave(frame) yellowfive@57: frame.obj:Fire("OnLeave") yellowfive@57: end yellowfive@57: yellowfive@57: local function itemOnClick(frame, ...) yellowfive@57: frame.obj:SelectItem(frame.value) yellowfive@57: frame.obj.list:Hide() yellowfive@57: end yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Methods yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local methods = { yellowfive@57: ["OnAcquire"] = function(self) yellowfive@57: -- restore default values yellowfive@57: self:SetHeight(24) yellowfive@57: self:SetWidth(200) yellowfive@57: self:SetDisabled(false) yellowfive@57: self:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text)) yellowfive@57: self:SelectItem() yellowfive@57: self.frame:ClearAllPoints() yellowfive@57: self.list:Hide() yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnRelease"] = function(self) yellowfive@57: self.itemlist = nil yellowfive@57: for _, item in pairs(self.items) do yellowfive@57: item:Hide() yellowfive@57: end yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnWidthSet"] = function(self, width) yellowfive@57: self.frame:GetFontString():SetWidth(width) yellowfive@57: self.list:SetWidth(width) yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnHeightSet"] = function(self, height) yellowfive@57: self.frame:GetFontString():SetHeight(height) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetFont"] = function(self, font) yellowfive@57: self.frame:SetNormalFontObject(font) yellowfive@57: yellowfive@57: local _, h = font:GetFont() yellowfive@57: self.fontHeight = h yellowfive@57: end, yellowfive@57: yellowfive@57: ["SelectItem"] = function(self, value) yellowfive@57: -- clear any current selection yellowfive@57: self.frame:SetText() yellowfive@57: yellowfive@57: if not self.itemlist or not value then return end yellowfive@57: yellowfive@57: local found = nil yellowfive@57: for i, obj in ipairs(self.itemlist) do yellowfive@57: local wasSelected = obj.selected yellowfive@57: obj.selected = obj.value == value yellowfive@57: yellowfive@57: if obj.selected then yellowfive@57: self.frame:SetText(obj.text) yellowfive@57: if not wasSelected then yellowfive@57: found = obj.value yellowfive@57: end yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: -- redraw the list if it is open yellowfive@57: if self.list:IsVisible() then yellowfive@57: self:RenderItems() yellowfive@57: end yellowfive@57: yellowfive@57: -- only fires if selection actually changed yellowfive@57: if found then yellowfive@57: self:Fire("OnChange", found) yellowfive@57: end yellowfive@57: end, yellowfive@57: yellowfive@57: -- the list of items to display, ordered list of objects with value, text, color, and selected properties, only supports single selection yellowfive@57: ["SetItems"] = function(self, items) yellowfive@57: self.itemlist = items yellowfive@57: self:RenderItems() yellowfive@57: end, yellowfive@57: yellowfive@57: ["CreateItem"] = function(self, index) yellowfive@57: local itemname = ("AmrUiDropDown%dItem%d"):format(self.num, index) yellowfive@57: yellowfive@57: local item = CreateFrame("Button", itemname, self.list) yellowfive@57: item.obj = self yellowfive@57: yellowfive@57: item:SetHeight(24) yellowfive@57: item:SetPoint("LEFT", self.list, "LEFT", 1, 0) yellowfive@57: item:SetPoint("RIGHT", self.list, "RIGHT", -1, 0) yellowfive@57: yellowfive@57: local txt = item:CreateFontString() yellowfive@57: item:SetFontString(txt) yellowfive@57: txt:SetPoint("LEFT", item, "LEFT", 4, 0) yellowfive@57: txt:SetPoint("RIGHT", item, "RIGHT", -4, 0) yellowfive@57: txt:SetJustifyH("LEFT") yellowfive@57: yellowfive@57: item:SetPushedTextOffset(0, 0) yellowfive@57: yellowfive@57: -- not perfect, but more or less achieves the effect of lightening the bg color slightly on hover yellowfive@57: local texHigh = item:CreateTexture(nil, "BORDER") yellowfive@57: texHigh:SetTexture(1, 1, 1, 0.1) yellowfive@57: texHigh:SetAllPoints(true) yellowfive@57: item:SetHighlightTexture(texHigh) yellowfive@57: yellowfive@57: item:SetScript("OnClick", itemOnClick) yellowfive@57: yellowfive@57: return item yellowfive@57: end, yellowfive@57: yellowfive@57: ["RenderItems"] = function(self) yellowfive@57: if not self.itemlist then return end yellowfive@57: if not self.list:IsVisible() then return end yellowfive@57: yellowfive@57: local prev = nil yellowfive@57: local h = 0 yellowfive@57: yellowfive@57: for i, obj in ipairs(self.itemlist) do yellowfive@57: local item = self.items[i] yellowfive@57: if not item then yellowfive@57: item = self:CreateItem(i) yellowfive@57: self.items[i] = item yellowfive@57: end yellowfive@57: yellowfive@57: item:SetNormalFontObject(Amr.CreateFont(obj.selected and "Bold" or "Regular", obj.selected and self.fontHeight + 2 or self.fontHeight, obj.color or Amr.Colors.White, 1)) yellowfive@57: item:SetText(obj.text) yellowfive@57: item.value = obj.value yellowfive@57: yellowfive@57: if prev then yellowfive@57: item:SetPoint("TOP", prev, "BOTTOM") yellowfive@57: else yellowfive@57: item:SetPoint("TOP", self.list, "TOP", 0, -1) yellowfive@57: end yellowfive@57: yellowfive@57: h = h + item:GetHeight() yellowfive@57: prev = item yellowfive@57: end yellowfive@57: yellowfive@57: self.list:SetHeight(h + 2) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetDisabled"] = function(self, disabled) yellowfive@57: self.disabled = disabled yellowfive@57: if disabled then yellowfive@57: self.frame:Disable() yellowfive@57: else yellowfive@57: self.frame:Enable() yellowfive@57: end yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetVisible"] = function(self, visible) yellowfive@57: if visible then yellowfive@57: self.frame:Show() yellowfive@57: else yellowfive@57: self.frame:Hide() yellowfive@57: self.list:Hide() yellowfive@57: end yellowfive@57: end yellowfive@57: } yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Constructor yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local function Constructor() yellowfive@57: local num = AceGUI:GetNextWidgetNum(Type) yellowfive@57: local name = "AmrUiDropDown" .. num yellowfive@57: local frame = CreateFrame("Button", name, UIParent) yellowfive@57: frame:Hide() yellowfive@57: yellowfive@57: local txt = frame:CreateFontString() yellowfive@57: frame:SetFontString(txt) yellowfive@57: txt:SetPoint("LEFT", frame, "LEFT", 4, 0) yellowfive@57: txt:SetPoint("RIGHT", frame, "RIGHT", -24, 0) yellowfive@57: txt:SetJustifyH("LEFT") yellowfive@57: yellowfive@57: frame:SetPushedTextOffset(0, 0) yellowfive@57: yellowfive@57: frame:EnableMouse(true) yellowfive@57: frame:SetScript("OnEnter", buttonOnEnter) yellowfive@57: frame:SetScript("OnLeave", buttonOnLeave) yellowfive@57: frame:SetScript("OnClick", buttonOnClick) yellowfive@57: yellowfive@57: local border = Amr.CreateTexture(frame, Amr.Colors.BorderGray, 1, "BACKGROUND") yellowfive@57: border:SetAllPoints() yellowfive@57: yellowfive@57: local bg = Amr.CreateTexture(frame, Amr.Colors.BgInput, 1, "BORDER") yellowfive@57: bg:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1) yellowfive@57: bg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1) yellowfive@57: yellowfive@57: local arrow = frame:CreateTexture(nil, "ARTWORK") yellowfive@57: arrow:SetWidth(16) yellowfive@57: arrow:SetHeight(16) yellowfive@57: arrow:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\IconScrollDown") yellowfive@57: arrow:SetPoint("RIGHT", frame, "RIGHT", -4, 0) yellowfive@57: yellowfive@57: local list = CreateFrame("Frame", nil, frame) yellowfive@57: list:SetPoint("TOPLEFT", frame, "BOTTOMLEFT", 0, 1) yellowfive@57: list:Hide() yellowfive@57: yellowfive@57: local listBorder = Amr.CreateTexture(list, Amr.Colors.BorderGray, 1, "BACKGROUND") yellowfive@57: listBorder:SetAllPoints() yellowfive@57: yellowfive@57: local listBg = Amr.CreateTexture(list, Amr.Colors.BgInput, 1, "BORDER") yellowfive@57: listBg:SetPoint("TOPLEFT", list, "TOPLEFT", 1, -1) yellowfive@57: listBg:SetPoint("BOTTOMRIGHT", list, "BOTTOMRIGHT", -1, 1) yellowfive@57: yellowfive@57: local widget = { yellowfive@57: num = num, yellowfive@57: list = list, yellowfive@57: items = {}, 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: frame.obj = widget yellowfive@57: yellowfive@57: return AceGUI:RegisterAsWidget(widget) yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(Type, Constructor, Version)