Quantcast

Use a custom dropdown to avoid any possible taints, and gain more control

Adrian L Lange [08-10-14 - 20:07]
Use a custom dropdown to avoid any possible taints, and gain more control
Filename
Config.lua
Localization.lua
diff --git a/Config.lua b/Config.lua
index a2b93ba..e7d84e2 100644
--- a/Config.lua
+++ b/Config.lua
@@ -1,6 +1,6 @@
 local addonName, L = ...

-local buttons = {}
+local objects = {}
 local temporary = {}

 local defaults = {
@@ -83,14 +83,11 @@ function Panel:default()
 end

 function Panel:refresh()
-	for key, button in next, buttons do
-		if(button:IsObjectType('CheckButton')) then
-			button:SetChecked(QuickQuestDB[key])
-		elseif(button:IsObjectType('Button')) then
-			UIDropDownMenu_SetSelectedValue(button, QuickQuestDB[key])
-
-			-- This is for some reason needed, gotta take a look into it later
-			UIDropDownMenu_SetText(button, _G[QuickQuestDB[key] .. '_KEY'])
+	for key, object in next, objects do
+		if(object:IsObjectType('CheckButton')) then
+			object:SetChecked(QuickQuestDB[key])
+		elseif(object:IsObjectType('Frame')) then
+			object.Label:SetText(object.keys[QuickQuestDB[key]])
 		end
 	end
 end
@@ -98,28 +95,30 @@ end
 local function ToggleAll(self)
 	local enabled = self:GetChecked()

-	for _, button in next, buttons do
-		if(button:IsObjectType('CheckButton')) then
+	for _, object in next, objects do
+		if(object:IsObjectType('CheckButton')) then
 			if(enabled) then
-				local parent = button.realParent
+				local parent = object.realParent
 				if(not parent or parent:GetChecked()) then
-					button:Enable()
-					button.Text:SetTextColor(1, 1, 1)
+					object:Enable()
+					object.Text:SetTextColor(1, 1, 1)
 				end
 			else
-				if(button ~= self) then
-					button:Disable()
+				if(object ~= self) then
+					object:Disable()
 				end

-				button.Text:SetTextColor(1/3, 1/3, 1/3)
+				object.Text:SetTextColor(1/3, 1/3, 1/3)
 			end
-		elseif(button:IsObjectType('Button')) then
+		elseif(object:IsObjectType('Frame')) then
 			if(enabled) then
-				UIDropDownMenu_EnableDropDown(button)
-				button.Text:SetTextColor(1, 1, 1)
+				object.Button:Enable()
+				object.Label:SetTextColor(1, 1, 1)
+				object.Text:SetTextColor(1, 1, 1)
 			else
-				UIDropDownMenu_DisableDropDown(button)
-				button.Text:SetTextColor(1/3, 1/3, 1/3)
+				object.Button:Disable()
+				object.Label:SetTextColor(1/3, 1/3, 1/3)
+				object.Text:SetTextColor(1/3, 1/3, 1/3)
 			end
 		end
 	end
@@ -142,7 +141,7 @@ do
 		CheckButton.realParent = realParent
 		CheckButton.key = key

-		buttons[key] = CheckButton
+		objects[key] = CheckButton

 		return CheckButton
 	end
@@ -150,25 +149,131 @@ end

 local CreateDropdown
 do
-	local function OnClick(self)
-		UIDropDownMenu_SetSelectedValue(self:GetParent().dropdown, self.value)
-		temporary[self:GetParent().dropdown.key] = self.value
+	local BACKDROP = {
+		bgFile = [[Interface\DialogFrame\UI-DialogBox-Background-Dark]],
+		edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]], edgeSize = 32,
+		insets = {top = 12, bottom = 9, left = 11, right = 12}
+	}
+
+	local function OnHide(self)
+		self.Menu:Hide()
+	end
+
+	local function MenuClick(self)
+		local Menu = self:GetParent().Menu
+		if(Menu:IsShown()) then
+			Menu:Hide()
+		else
+			for key, Item in next, Menu.items do
+				Item.Button:SetChecked(key == (temporary[Menu.key] or QuickQuestDB[Menu.key]))
+			end
+
+			Menu:Show()
+		end
+
+		PlaySound('igMainMenuOptionCheckBoxOn')
 	end

-	function CreateDropdown(parent, key, func)
-		local Dropdown = CreateFrame('Button', 'QuickQuestDropDown_' .. GetTime(), parent, 'UIDropDownMenuTemplate')
-		Dropdown.OnClick = OnClick
-		Dropdown.key = key
+	local function ItemClick(self)
+		local Menu = self:GetParent()
+		temporary[Menu.key] = self.value

-		UIDropDownMenu_SetWidth(Dropdown, 90)
-		UIDropDownMenu_SetSelectedValue(Dropdown, QuickQuestDB[key])
-		UIDropDownMenu_Initialize(Dropdown, func)
+		Menu:Hide()
+		Menu:GetParent().Label:SetText(self:GetText())
+	end
+
+	function CreateDropdown(parent, key, items)
+		local Dropdown = CreateFrame('Frame', nil, parent)
+		Dropdown:SetSize(110, 32)
+		Dropdown:SetScript('OnHide', OnHide)
+		Dropdown.keys = items
+
+		local LeftTexture = Dropdown:CreateTexture(nil, 'ARTWORK')
+		LeftTexture:SetPoint('TOPLEFT', -14, 17)
+		LeftTexture:SetSize(25, 64)
+		LeftTexture:SetTexture([[Interface\Glues\CharacterCreate\CharacterCreate-LabelFrame]])
+		LeftTexture:SetTexCoord(0, 0.1953125, 0, 1)
+
+		local RightTexture = Dropdown:CreateTexture(nil, 'ARTWORK')
+		RightTexture:SetPoint('TOPRIGHT', 14, 17)
+		RightTexture:SetSize(25, 64)
+		RightTexture:SetTexture([[Interface\Glues\CharacterCreate\CharacterCreate-LabelFrame]])
+		RightTexture:SetTexCoord(0.8046875, 1, 0, 1)
+
+		local MiddleTexture = Dropdown:CreateTexture(nil, 'ARTWORK')
+		MiddleTexture:SetPoint('TOPLEFT', LeftTexture, 'TOPRIGHT')
+		MiddleTexture:SetPoint('TOPRIGHT', RightTexture, 'TOPLEFT')
+		MiddleTexture:SetTexture([[Interface\Glues\CharacterCreate\CharacterCreate-LabelFrame]])
+		MiddleTexture:SetTexCoord(0.1953125, 0.8046875, 0, 1)
+
+		local Button = CreateFrame('Button', nil, Dropdown)
+		Button:SetPoint('TOPRIGHT', RightTexture, -16, -18)
+		Button:SetSize(24, 24)
+		Button:SetNormalTexture([[Interface\ChatFrame\UI-ChatIcon-ScrollDown-Up]])
+		Button:SetPushedTexture([[Interface\ChatFrame\UI-ChatIcon-ScrollDown-Down]])
+		Button:SetDisabledTexture([[Interface\ChatFrame\UI-ChatIcon-ScrollDown-Disabled]])
+		Button:SetHighlightTexture([[Interface\Buttons\UI-Common-MouseHilight]])
+		Button:GetHighlightTexture():SetBlendMode('ADD')
+		Button:SetScript('OnClick', MenuClick)
+		Dropdown.Button = Button
+
+		local Label = Dropdown:CreateFontString(nil, nil, 'GameFontHighlightSmall')
+		Label:SetPoint('RIGHT', Button, 'LEFT')
+		Label:SetSize(0, 10)
+		Dropdown.Label = Label
+
+		local Menu = CreateFrame('Frame', nil, Dropdown)
+		Menu:SetPoint('TOPLEFT', Dropdown, 'BOTTOMLEFT', 0, 4)
+		Menu:SetBackdrop(BACKDROP)
+		Menu:Hide()
+		Menu.key = key
+		Menu.items = {}
+		Dropdown.Menu = Menu
+
+		local index, maxWidth = 0, 0
+		for value, name in next, items do
+			local Item = CreateFrame('Button', nil, Menu)
+			Item:SetPoint('TOPLEFT', 14, -(14 + (18 * index)))
+			Item:SetHighlightTexture([[Interface\QuestFrame\UI-QuestTitleHighlight]])
+			Item:GetHighlightTexture():SetBlendMode('ADD')
+			Item:SetScript('OnClick', ItemClick)
+			Item.value = value
+
+			local ItemButton = CreateFrame('CheckButton', nil, Item)
+			ItemButton:SetPoint('LEFT')
+			ItemButton:SetSize(16, 16)
+			ItemButton:SetNormalTexture([[Interface\Common\UI-DropDownRadioChecks]])
+			ItemButton:GetNormalTexture():SetTexCoord(0.5, 1, 0.5, 1)
+			ItemButton:SetCheckedTexture([[Interface\Common\UI-DropDownRadioChecks]])
+			ItemButton:GetCheckedTexture():SetTexCoord(0, 0.5, 0.5, 1)
+			ItemButton:EnableMouse(false)
+			Item.Button = ItemButton
+
+			local ItemLabel = Item:CreateFontString(nil, nil, 'GameFontHighlightSmall')
+			ItemLabel:SetPoint('LEFT', ItemButton, 'RIGHT', 4, -1)
+			ItemLabel:SetText(name)
+			Item:SetFontString(ItemLabel)
+
+			local width = ItemLabel:GetWidth()
+			if(width > maxWidth) then
+				maxWidth = width
+			end
+
+			Menu.items[value] = Item
+			index = index + 1
+		end
+
+		for _, Item in next, Menu.items do
+			Item:SetSize(32 + maxWidth, 18)
+		end
+
+		Menu:SetSize(60 + maxWidth, 28 + 18 * index)

 		local Text = Dropdown:CreateFontString(nil, nil, 'GameFontHighlight')
-		Text:SetPoint('LEFT', Dropdown, 'RIGHT', -1, 2)
+		Text:SetPoint('LEFT', Dropdown, 'RIGHT', 3, 2)
 		Dropdown.Text = Text

-		buttons[key] = Dropdown
+		objects[key] = Dropdown

 		return Dropdown
 	end
@@ -229,28 +334,12 @@ Panel:SetScript('OnShow', function(self)
 	Darkmoon:SetPoint('TOPLEFT', GossipRaid, 'BOTTOMLEFT', -24, -8)
 	Darkmoon.Text:SetText(L['Darkmoon Faire: Automatically teleport'])

-	local Modifier = CreateDropdown(self, 'modifier', function(self)
-		local selected = UIDropDownMenu_GetSelectedValue(self)
-		local info = UIDropDownMenu_CreateInfo()
-		info.text = ALT_KEY
-		info.value = 'ALT'
-		info.func = self.OnClick
-		info.checked = selected == info.value
-		UIDropDownMenu_AddButton(info)
-
-		info.text = CTRL_KEY
-		info.value = 'CTRL'
-		info.func = self.OnClick
-		info.checked = selected == info.value
-		UIDropDownMenu_AddButton(info)
-
-		info.text = SHIFT_KEY
-		info.value = 'SHIFT'
-		info.func = self.OnClick
-		info.checked = selected == info.value
-		UIDropDownMenu_AddButton(info)
-	end)
-	Modifier:SetPoint('TOPLEFT', Darkmoon, 'BOTTOMLEFT', -13, -14)
+	local Modifier = CreateDropdown(self, 'modifier', {
+		ALT = L['ALT key'],
+		CTRL = L['CTRL key'],
+		SHIFT = L['SHIFT key']
+	})
+	Modifier:SetPoint('TOPLEFT', Darkmoon, 'BOTTOMLEFT', 0, -14)

 	if(QuickQuestDB.reverse) then
 		Modifier.Text:SetText(L['Modifier to temporarly enable automation'])
@@ -259,7 +348,7 @@ Panel:SetScript('OnShow', function(self)
 	end

 	local Reverse = CreateCheckButton(self, 'reverse')
-	Reverse:SetPoint('TOPLEFT', Modifier, 'BOTTOMLEFT', 37, -8)
+	Reverse:SetPoint('TOPLEFT', Modifier, 'BOTTOMLEFT', 24, -8)
 	Reverse.Text:SetText(L['Reverse the behaviour of the modifier key'])
 	Reverse:HookScript('OnClick', function(self)
 		if(self:GetChecked()) then
diff --git a/Localization.lua b/Localization.lua
index 7aa1a15..ca33d0b 100644
--- a/Localization.lua
+++ b/Localization.lua
@@ -6,6 +6,10 @@ setmetatable(L, {__index = function(L, key)
 	return value
 end})

+L['ALT key'] = ALT_KEY
+L['CTRL key'] = CTRL_KEY
+L['SHIFT key'] = SHIFT_KEY
+
 L.FilterDetailsTooltip = [[
 Easily add more items to filter by
 grabbing one from your inventory