Details | Compare with Previous | View Log
Line No. | Rev | Author | Line |
---|---|---|---|
1 | 36 | Seerah-7 | --[[dropdownData = { |
2 | Seerah-7 | type = "dropdown", |
|
3 | Seerah-7 | name = "My Dropdown", |
|
4 | Seerah-7 | tooltip = "Dropdown's tooltip text.", |
|
5 | Seerah-7 | choices = {"table", "of", "choices"}, |
|
6 | 42 | Seerah-7 | sort = "name-up", --or "name-down", "numeric-up", "numeric-down" (optional) - if not provided, list will not be sorted |
7 | 36 | Seerah-7 | getFunc = function() return db.var end, |
8 | Seerah-7 | setFunc = function(var) db.var = var doStuff() end, |
|
9 | Seerah-7 | width = "full", --or "half" (optional) |
|
10 | Seerah-7 | disabled = function() return db.someBooleanSetting end, --or boolean (optional) |
|
11 | Seerah-7 | warning = "Will need to reload the UI.", --(optional) |
|
12 | Seerah-7 | default = defaults.var, --(optional) |
|
13 | Seerah-7 | reference = "MyAddonDropdown" --(optional) unique global reference to control |
|
14 | Seerah-7 | } ]] |
|
15 | Seerah-7 | ||
16 | Seerah-7 | ||
17 | 53 | Seerah-7 | local widgetVersion = 7 |
18 | 36 | Seerah-7 | local LAM = LibStub("LibAddonMenu-2.0") |
19 | Seerah-7 | if not LAM:RegisterWidget("dropdown", widgetVersion) then return end |
|
20 | Seerah-7 | ||
21 | Seerah-7 | local wm = WINDOW_MANAGER |
|
22 | Seerah-7 | local cm = CALLBACK_MANAGER |
|
23 | Seerah-7 | local tinsert = table.insert |
|
24 | Seerah-7 | ||
25 | Seerah-7 | ||
26 | Seerah-7 | local function UpdateDisabled(control) |
|
27 | Seerah-7 | local disable |
|
28 | Seerah-7 | if type(control.data.disabled) == "function" then |
|
29 | Seerah-7 | disable = control.data.disabled() |
|
30 | Seerah-7 | else |
|
31 | Seerah-7 | disable = control.data.disabled |
|
32 | Seerah-7 | end |
|
33 | Seerah-7 | ||
34 | Seerah-7 | control.dropdown:SetEnabled(not disable) |
|
35 | Seerah-7 | if disable then |
|
36 | Seerah-7 | control.label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA()) |
|
37 | Seerah-7 | else |
|
38 | Seerah-7 | control.label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA()) |
|
39 | Seerah-7 | end |
|
40 | Seerah-7 | end |
|
41 | Seerah-7 | ||
42 | Seerah-7 | local function UpdateValue(control, forceDefault, value) |
|
43 | Seerah-7 | if forceDefault then --if we are forcing defaults |
|
44 | Seerah-7 | value = control.data.default |
|
45 | Seerah-7 | control.data.setFunc(value) |
|
46 | Seerah-7 | control.dropdown:SetSelectedItem(value) |
|
47 | Seerah-7 | elseif value then |
|
48 | Seerah-7 | control.data.setFunc(value) |
|
49 | Seerah-7 | --after setting this value, let's refresh the others to see if any should be disabled or have their settings changed |
|
50 | Seerah-7 | if control.panel.data.registerForRefresh then |
|
51 | Seerah-7 | cm:FireCallbacks("LAM-RefreshPanel", control) |
|
52 | Seerah-7 | end |
|
53 | Seerah-7 | else |
|
54 | Seerah-7 | value = control.data.getFunc() |
|
55 | Seerah-7 | control.dropdown:SetSelectedItem(value) |
|
56 | Seerah-7 | end |
|
57 | Seerah-7 | end |
|
58 | Seerah-7 | ||
59 | Seerah-7 | local function DropdownCallback(choice, choiceText, choice) |
|
60 | Seerah-7 | choice.control:UpdateValue(false, choiceText) |
|
61 | Seerah-7 | end |
|
62 | Seerah-7 | ||
63 | Seerah-7 | local function UpdateChoices(control, choices) |
|
64 | Seerah-7 | control.dropdown:ClearItems() --remove previous choices --(need to call :SetSelectedItem()?) |
|
65 | Seerah-7 | ||
66 | Seerah-7 | --build new list of choices |
|
67 | Seerah-7 | local choices = choices or control.data.choices |
|
68 | Seerah-7 | for i = 1, #choices do |
|
69 | Seerah-7 | local entry = control.dropdown:CreateItemEntry(choices[i], DropdownCallback) |
|
70 | Seerah-7 | entry.control = control |
|
71 | 42 | Seerah-7 | control.dropdown:AddItem(entry, not control.data.sort and ZO_COMBOBOX_SUPRESS_UPDATE) --if sort type/order isn't specified, then don't sort |
72 | 36 | Seerah-7 | end |
73 | Seerah-7 | end |
|
74 | Seerah-7 | ||
75 | 42 | Seerah-7 | local function GrabSortingInfo(sortInfo) |
76 | Seerah-7 | local t, i = {}, 1 |
|
77 | Seerah-7 | for info in string.gmatch(sortInfo, "([^%-]+)") do |
|
78 | Seerah-7 | t[i] = info |
|
79 | Seerah-7 | i = i + 1 |
|
80 | Seerah-7 | end |
|
81 | 36 | Seerah-7 | |
82 | 42 | Seerah-7 | return t |
83 | Seerah-7 | end |
|
84 | Seerah-7 | ||
85 | Seerah-7 | ||
86 | 36 | Seerah-7 | local comboboxCount = 1 |
87 | Seerah-7 | function LAMCreateControl.dropdown(parent, dropdownData, controlName) |
|
88 | Seerah-7 | local control = wm:CreateTopLevelWindow(controlName or dropdownData.reference) |
|
89 | 45 | Seerah-7 | control:SetParent(parent.scroll or parent) |
90 | 36 | Seerah-7 | control:SetMouseEnabled(true) |
91 | 53 | Seerah-7 | --control.tooltipText = dropdownData.tooltip |
92 | 36 | Seerah-7 | control:SetHandler("OnMouseEnter", ZO_Options_OnMouseEnter) |
93 | Seerah-7 | control:SetHandler("OnMouseExit", ZO_Options_OnMouseExit) |
|
94 | Seerah-7 | ||
95 | Seerah-7 | control.label = wm:CreateControl(nil, control, CT_LABEL) |
|
96 | Seerah-7 | local label = control.label |
|
97 | Seerah-7 | label:SetAnchor(TOPLEFT) |
|
98 | Seerah-7 | label:SetFont("ZoFontWinH4") |
|
99 | Seerah-7 | label:SetWrapMode(TEXT_WRAP_MODE_ELLIPSIS) |
|
100 | Seerah-7 | label:SetText(dropdownData.name) |
|
101 | Seerah-7 | ||
102 | Seerah-7 | control.combobox = wm:CreateControlFromVirtual(parent:GetName().."Combobox"..comboboxCount, control, "ZO_ComboBox") |
|
103 | Seerah-7 | comboboxCount = comboboxCount + 1 |
|
104 | Seerah-7 | local combobox = control.combobox |
|
105 | Seerah-7 | combobox:SetHandler("OnMouseEnter", function() ZO_Options_OnMouseEnter(control) end) |
|
106 | Seerah-7 | combobox:SetHandler("OnMouseExit", function() ZO_Options_OnMouseExit(control) end) |
|
107 | Seerah-7 | control.dropdown = ZO_ComboBox_ObjectFromContainer(combobox) |
|
108 | Seerah-7 | local dropdown = control.dropdown |
|
109 | 42 | Seerah-7 | if dropdownData.sort then |
110 | Seerah-7 | local sortInfo = GrabSortingInfo(dropdownData.sort) |
|
111 | Seerah-7 | local sortType, sortOrder = sortInfo[1], sortInfo[2] |
|
112 | Seerah-7 | dropdown:SetSortOrder(sortOrder == "up" and ZO_SORT_ORDER_UP or ZO_SORT_ORDER_DOWN, sortType == "name" and ZO_SORT_BY_NAME or ZO_SORT_BY_NAME_NUMERIC) |
|
113 | Seerah-7 | end |
|
114 | Seerah-7 | ||
115 | 36 | Seerah-7 | local isHalfWidth = dropdownData.width == "half" |
116 | Seerah-7 | if isHalfWidth then |
|
117 | Seerah-7 | control:SetDimensions(250, 55) |
|
118 | Seerah-7 | label:SetDimensions(250, 26) |
|
119 | 46 | Seerah-7 | combobox:SetDimensions(225, 26) |
120 | 36 | Seerah-7 | combobox:SetAnchor(TOPRIGHT, label, BOTTOMRIGHT) |
121 | Seerah-7 | else |
|
122 | Seerah-7 | control:SetDimensions(510, 30) |
|
123 | Seerah-7 | label:SetDimensions(300, 26) |
|
124 | Seerah-7 | combobox:SetDimensions(200, 26) |
|
125 | Seerah-7 | combobox:SetAnchor(TOPRIGHT) |
|
126 | Seerah-7 | end |
|
127 | Seerah-7 | ||
128 | 46 | Seerah-7 | if dropdownData.warning then |
129 | 36 | Seerah-7 | control.warning = wm:CreateControlFromVirtual(nil, control, "ZO_Options_WarningIcon") |
130 | Seerah-7 | control.warning:SetAnchor(RIGHT, combobox, LEFT, -5, 0) |
|
131 | 53 | Seerah-7 | --control.warning.tooltipText = dropdownData.warning |
132 | Seerah-7 | control.warning.data = {tooltipText = dropdownData.warning} |
|
133 | 36 | Seerah-7 | end |
134 | Seerah-7 | ||
135 | Seerah-7 | control.panel = parent.panel or parent --if this is in a submenu, panel is its parent |
|
136 | Seerah-7 | control.data = dropdownData |
|
137 | 53 | Seerah-7 | control.data.tooltipText = dropdownData.tooltip |
138 | 36 | Seerah-7 | |
139 | Seerah-7 | if dropdownData.disabled then |
|
140 | Seerah-7 | control.UpdateDisabled = UpdateDisabled |
|
141 | Seerah-7 | control:UpdateDisabled() |
|
142 | Seerah-7 | end |
|
143 | Seerah-7 | control.UpdateChoices = UpdateChoices |
|
144 | Seerah-7 | control:UpdateChoices(dropdownData.choices) |
|
145 | Seerah-7 | control.UpdateValue = UpdateValue |
|
146 | Seerah-7 | control:UpdateValue() |
|
147 | Seerah-7 | ||
148 | Seerah-7 | if control.panel.data.registerForRefresh or control.panel.data.registerForDefaults then --if our parent window wants to refresh controls, then add this to the list |
|
149 | Seerah-7 | tinsert(control.panel.controlsToRefresh, control) |
|
150 | Seerah-7 | end |
|
151 | Seerah-7 | ||
152 | Seerah-7 | return control |
|
153 | Seerah-7 | end |