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