Details | Compare with Previous | View Log
Line No. | Rev | Author | Line |
---|---|---|---|
1 | 36 | Seerah-7 | --[[buttonData = { |
2 | Seerah-7 | type = "button", |
|
3 | Seerah-7 | name = "My Button", |
|
4 | Seerah-7 | tooltip = "Button's tooltip text.", |
|
5 | Seerah-7 | func = function() end, |
|
6 | Seerah-7 | width = "full", --or "half" (optional) |
|
7 | Seerah-7 | disabled = function() return db.someBooleanSetting end, --or boolean (optional) |
|
8 | Seerah-7 | icon = "icon\\path.dds", --(optional) |
|
9 | Seerah-7 | warning = "Will need to reload the UI.", --(optional) |
|
10 | Seerah-7 | reference = "MyAddonButton" --(optional) unique global reference to control |
|
11 | Seerah-7 | } ]] |
|
12 | Seerah-7 | ||
13 | Seerah-7 | ||
14 | 53 | Seerah-7 | local widgetVersion = 5 |
15 | 36 | Seerah-7 | local LAM = LibStub("LibAddonMenu-2.0") |
16 | Seerah-7 | if not LAM:RegisterWidget("button", widgetVersion) then return end |
|
17 | Seerah-7 | ||
18 | Seerah-7 | local wm = WINDOW_MANAGER |
|
19 | Seerah-7 | local cm = CALLBACK_MANAGER |
|
20 | Seerah-7 | local tinsert = table.insert |
|
21 | Seerah-7 | ||
22 | Seerah-7 | local function UpdateDisabled(control) |
|
23 | Seerah-7 | local disable |
|
24 | Seerah-7 | if type(control.data.disabled) == "function" then |
|
25 | Seerah-7 | disable = control.data.disabled() |
|
26 | Seerah-7 | else |
|
27 | Seerah-7 | disable = control.data.disabled |
|
28 | Seerah-7 | end |
|
29 | Seerah-7 | ||
30 | Seerah-7 | control.button:SetEnabled(not disable) |
|
31 | Seerah-7 | end |
|
32 | Seerah-7 | ||
33 | Seerah-7 | ||
34 | Seerah-7 | --controlName is optional |
|
35 | Seerah-7 | function LAMCreateControl.button(parent, buttonData, controlName) |
|
36 | Seerah-7 | local control = wm:CreateTopLevelWindow(controlName or buttonData.reference) |
|
37 | 45 | Seerah-7 | control:SetParent(parent.scroll or parent) |
38 | 36 | Seerah-7 | |
39 | Seerah-7 | local isHalfWidth = buttonData.width == "half" |
|
40 | Seerah-7 | control:SetDimensions(isHalfWidth and 250 or 510, isHalfWidth and 55 or 28) |
|
41 | Seerah-7 | control:SetMouseEnabled(true) |
|
42 | Seerah-7 | ||
43 | Seerah-7 | if buttonData.icon then |
|
44 | Seerah-7 | control.button = wm:CreateControl(nil, control, CT_BUTTON) |
|
45 | Seerah-7 | control.button:SetDimensions(26, 26) |
|
46 | Seerah-7 | control.button:SetNormalTexture(buttonData.icon) |
|
47 | Seerah-7 | control.button:SetPressedOffset(2, 2) |
|
48 | Seerah-7 | else |
|
49 | Seerah-7 | --control.button = wm:CreateControlFromVirtual(controlName.."Button", control, "ZO_DefaultButton") |
|
50 | Seerah-7 | control.button = wm:CreateControlFromVirtual(nil, control, "ZO_DefaultButton") |
|
51 | Seerah-7 | control.button:SetWidth(isHalfWidth and 180 or 200) |
|
52 | Seerah-7 | control.button:SetText(buttonData.name) |
|
53 | Seerah-7 | end |
|
54 | Seerah-7 | local button = control.button |
|
55 | Seerah-7 | button:SetAnchor(isHalfWidth and CENTER or RIGHT) |
|
56 | Seerah-7 | button:SetClickSound("Click") |
|
57 | 53 | Seerah-7 | --button.tooltipText = buttonData.tooltip |
58 | Seerah-7 | button.data = {tooltipText = buttonData.tooltip} |
|
59 | 36 | Seerah-7 | button:SetHandler("OnMouseEnter", ZO_Options_OnMouseEnter) |
60 | Seerah-7 | button:SetHandler("OnMouseExit", ZO_Options_OnMouseExit) |
|
61 | Seerah-7 | button:SetHandler("OnClicked", function(self, ...) |
|
62 | Seerah-7 | buttonData.func(self, ...) |
|
63 | Seerah-7 | if control.panel.data.registerForRefresh then |
|
64 | Seerah-7 | cm:FireCallbacks("LAM-RefreshPanel", control) |
|
65 | Seerah-7 | end |
|
66 | Seerah-7 | end) |
|
67 | Seerah-7 | ||
68 | Seerah-7 | if buttonData.warning then |
|
69 | Seerah-7 | control.warning = wm:CreateControlFromVirtual(nil, control, "ZO_Options_WarningIcon") |
|
70 | Seerah-7 | control.warning:SetAnchor(RIGHT, button, LEFT, -5, 0) |
|
71 | 53 | Seerah-7 | --control.warning.tooltipText = buttonData.warning |
72 | Seerah-7 | control.warning.data = {tooltipText = buttonData.warning} |
|
73 | 36 | Seerah-7 | end |
74 | Seerah-7 | ||
75 | Seerah-7 | control.panel = parent.panel or parent --if this is in a submenu, panel is its parent |
|
76 | Seerah-7 | control.data = buttonData |
|
77 | Seerah-7 | ||
78 | Seerah-7 | if buttonData.disabled then |
|
79 | Seerah-7 | control.UpdateDisabled = UpdateDisabled |
|
80 | Seerah-7 | control:UpdateDisabled() |
|
81 | Seerah-7 | ||
82 | Seerah-7 | --this is here because buttons don't have an UpdateValue method |
|
83 | Seerah-7 | if control.panel.data.registerForRefresh then --if our parent window wants to refresh controls, then add this to the list |
|
84 | Seerah-7 | tinsert(control.panel.controlsToRefresh, control) |
|
85 | Seerah-7 | end |
|
86 | Seerah-7 | end |
|
87 | Seerah-7 | ||
88 | Seerah-7 | return control |
|
89 | Seerah-7 | end |