Details | Compare with Previous | View Log
Line No. | Rev | Author | Line |
---|---|---|---|
1 | 10 | Seerah-7 | --[[editboxData = { |
2 | Seerah-7 | type = "editbox", |
|
3 | Seerah-7 | name = "My Editbox", |
|
4 | Seerah-7 | tooltip = "Editbox's tooltip text.", |
|
5 | Seerah-7 | getFunc = function() return db.text end, |
|
6 | Seerah-7 | setFunc = function(text) db.text = text doStuff() end, |
|
7 | Seerah-7 | isMultiline = true, --boolean |
|
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.text, --(optional) |
|
12 | 11 | Seerah-7 | reference = "MyAddonEditbox" --(optional) unique global reference to control |
13 | 10 | Seerah-7 | } ]] |
14 | Seerah-7 | ||
15 | Seerah-7 | ||
16 | 16 | Seerah-7 | local widgetVersion = 6 |
17 | 10 | Seerah-7 | local LAM = LibStub("LibAddonMenu-2.0") |
18 | Seerah-7 | if not LAM:RegisterWidget("editbox", widgetVersion) then return end |
|
19 | Seerah-7 | ||
20 | Seerah-7 | local wm = WINDOW_MANAGER |
|
21 | Seerah-7 | local cm = CALLBACK_MANAGER |
|
22 | Seerah-7 | local tinsert = table.insert |
|
23 | 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 | Seerah-7 | if disable then |
|
34 | Seerah-7 | control.label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA()) |
|
35 | Seerah-7 | control.editbox:SetColor(ZO_DEFAULT_DISABLED_MOUSEOVER_COLOR:UnpackRGBA()) |
|
36 | Seerah-7 | else |
|
37 | Seerah-7 | control.label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA()) |
|
38 | Seerah-7 | control.editbox:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA()) |
|
39 | Seerah-7 | end |
|
40 | Seerah-7 | --control.editbox:SetEditEnabled(not disable) |
|
41 | Seerah-7 | control.editbox:SetMouseEnabled(not disable) |
|
42 | Seerah-7 | end |
|
43 | Seerah-7 | ||
44 | Seerah-7 | local function UpdateValue(control, forceDefault, value) |
|
45 | Seerah-7 | if forceDefault then --if we are forcing defaults |
|
46 | Seerah-7 | value = control.data.default |
|
47 | Seerah-7 | control.data.setFunc(value) |
|
48 | Seerah-7 | control.editbox:SetText(value) |
|
49 | Seerah-7 | elseif value then |
|
50 | Seerah-7 | control.data.setFunc(value) |
|
51 | Seerah-7 | --after setting this value, let's refresh the others to see if any should be disabled or have their settings changed |
|
52 | Seerah-7 | if control.panel.data.registerForRefresh then |
|
53 | Seerah-7 | cm:FireCallbacks("LAM-RefreshPanel", control) |
|
54 | Seerah-7 | end |
|
55 | Seerah-7 | else |
|
56 | Seerah-7 | value = control.data.getFunc() |
|
57 | Seerah-7 | control.editbox:SetText(value) |
|
58 | Seerah-7 | end |
|
59 | Seerah-7 | end |
|
60 | Seerah-7 | ||
61 | Seerah-7 | ||
62 | Seerah-7 | function LAMCreateControl.editbox(parent, editboxData, controlName) |
|
63 | 11 | Seerah-7 | local control = wm:CreateTopLevelWindow(controlName or editboxData.reference) |
64 | 13 | Seerah-7 | control:SetParent(parent.scroll or parent) |
65 | 10 | Seerah-7 | control:SetMouseEnabled(true) |
66 | Seerah-7 | control:SetResizeToFitDescendents(true) |
|
67 | 16 | Seerah-7 | --control.tooltipText = editboxData.tooltip |
68 | 10 | Seerah-7 | control:SetHandler("OnMouseEnter", ZO_Options_OnMouseEnter) |
69 | Seerah-7 | control:SetHandler("OnMouseExit", ZO_Options_OnMouseExit) |
|
70 | Seerah-7 | ||
71 | Seerah-7 | control.label = wm:CreateControl(nil, control, CT_LABEL) |
|
72 | Seerah-7 | local label = control.label |
|
73 | Seerah-7 | label:SetAnchor(TOPLEFT) |
|
74 | Seerah-7 | label:SetFont("ZoFontWinH4") |
|
75 | Seerah-7 | label:SetWrapMode(TEXT_WRAP_MODE_ELLIPSIS) |
|
76 | Seerah-7 | label:SetText(editboxData.name) |
|
77 | Seerah-7 | ||
78 | Seerah-7 | control.bg = wm:CreateControlFromVirtual(nil, control, "ZO_EditBackdrop") |
|
79 | Seerah-7 | local bg = control.bg |
|
80 | Seerah-7 | ||
81 | Seerah-7 | if editboxData.isMultiline then |
|
82 | 13 | Seerah-7 | control.editbox = wm:CreateControlFromVirtual(nil, bg, "ZO_DefaultEditMultiLineForBackdrop") |
83 | Seerah-7 | control.editbox:SetHandler("OnMouseWheel", function(self, delta) |
|
84 | Seerah-7 | if self:HasFocus() then --only set focus to new spots if the editbox is currently in use |
|
85 | Seerah-7 | local cursorPos = self:GetCursorPosition() |
|
86 | Seerah-7 | local text = self:GetText() |
|
87 | Seerah-7 | local textLen = text:len() |
|
88 | Seerah-7 | local newPos |
|
89 | Seerah-7 | if delta > 0 then --scrolling up |
|
90 | Seerah-7 | local reverseText = text:reverse() |
|
91 | Seerah-7 | local revCursorPos = textLen - cursorPos |
|
92 | Seerah-7 | local revPos = reverseText:find("\n", revCursorPos+1) |
|
93 | Seerah-7 | newPos = revPos and textLen - revPos |
|
94 | Seerah-7 | else --scrolling down |
|
95 | Seerah-7 | newPos = text:find("\n", cursorPos+1) |
|
96 | Seerah-7 | end |
|
97 | Seerah-7 | if newPos then --if we found a new line, then scroll, otherwise don't |
|
98 | Seerah-7 | self:SetCursorPosition(newPos) |
|
99 | Seerah-7 | end |
|
100 | Seerah-7 | end |
|
101 | Seerah-7 | end) |
|
102 | 10 | Seerah-7 | else |
103 | Seerah-7 | control.editbox = wm:CreateControlFromVirtual(nil, bg, "ZO_DefaultEditForBackdrop") |
|
104 | Seerah-7 | end |
|
105 | Seerah-7 | local editbox = control.editbox |
|
106 | Seerah-7 | editbox:SetText(editboxData.getFunc()) |
|
107 | Seerah-7 | editbox:SetMaxInputChars(3000) |
|
108 | Seerah-7 | editbox:SetHandler("OnFocusLost", function(self) control:UpdateValue(false, self:GetText()) end) |
|
109 | 13 | Seerah-7 | editbox:SetHandler("OnEscape", function(self) self:LoseFocus() control:UpdateValue(false, self:GetText()) end) |
110 | 10 | Seerah-7 | editbox:SetHandler("OnMouseEnter", function() ZO_Options_OnMouseEnter(control) end) |
111 | Seerah-7 | editbox:SetHandler("OnMouseExit", function() ZO_Options_OnMouseExit(control) end) |
|
112 | Seerah-7 | ||
113 | Seerah-7 | local isHalfWidth = editboxData.width == "half" |
|
114 | Seerah-7 | if isHalfWidth then |
|
115 | Seerah-7 | control:SetDimensions(250, 55) |
|
116 | Seerah-7 | label:SetDimensions(250, 26) |
|
117 | 13 | Seerah-7 | bg:SetDimensions(225, editboxData.isMultiline and 74 or 24) |
118 | 10 | Seerah-7 | bg:SetAnchor(TOPRIGHT, label, BOTTOMRIGHT) |
119 | 13 | Seerah-7 | if editboxData.isMultiline then |
120 | Seerah-7 | editbox:SetDimensionConstraints(210, 74, 210, 500) |
|
121 | Seerah-7 | end |
|
122 | 10 | Seerah-7 | else |
123 | Seerah-7 | control:SetDimensions(510, 30) |
|
124 | Seerah-7 | label:SetDimensions(300, 26) |
|
125 | Seerah-7 | bg:SetDimensions(200, editboxData.isMultiline and 100 or 24) |
|
126 | Seerah-7 | bg:SetAnchor(TOPRIGHT) |
|
127 | 13 | Seerah-7 | if editboxData.isMultiline then |
128 | Seerah-7 | editbox:SetDimensionConstraints(185, 100, 185, 500) |
|
129 | Seerah-7 | end |
|
130 | 10 | Seerah-7 | end |
131 | Seerah-7 | ||
132 | Seerah-7 | if editboxData.warning then |
|
133 | Seerah-7 | control.warning = wm:CreateControlFromVirtual(nil, control, "ZO_Options_WarningIcon") |
|
134 | Seerah-7 | control.warning:SetAnchor(TOPRIGHT, control.bg, TOPLEFT, -5, 0) |
|
135 | 16 | Seerah-7 | --control.warning.tooltipText = editboxData.warning |
136 | Seerah-7 | control.warning.data = {tooltipText = editboxData.warning} |
|
137 | 10 | Seerah-7 | end |
138 | Seerah-7 | ||
139 | Seerah-7 | control.panel = parent.panel or parent --if this is in a submenu, panel is its parent |
|
140 | Seerah-7 | control.data = editboxData |
|
141 | 16 | Seerah-7 | control.data.tooltipText = editboxData.tooltip |
142 | 10 | Seerah-7 | |
143 | Seerah-7 | if editboxData.disabled then |
|
144 | Seerah-7 | control.UpdateDisabled = UpdateDisabled |
|
145 | Seerah-7 | control:UpdateDisabled() |
|
146 | Seerah-7 | end |
|
147 | Seerah-7 | control.UpdateValue = UpdateValue |
|
148 | Seerah-7 | control:UpdateValue() |
|
149 | Seerah-7 | ||
150 | 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 |
|
151 | Seerah-7 | tinsert(control.panel.controlsToRefresh, control) |
|
152 | Seerah-7 | end |
|
153 | Seerah-7 | ||
154 | Seerah-7 | return control |
|
155 | Seerah-7 | end |