|
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +-- This file is part of the JX3 Plugin Project. |
| 3 | +-- @desc : UserInput |
| 4 | +-- @copyright: Emil Zhai <root@zhaiyiming.com> |
| 5 | +-------------------------------------------------------------------------------- |
| 6 | +---@class (partial) Boilerplate |
| 7 | +local X = Boilerplate |
| 8 | +-------------------------------------------------------------------------------- |
| 9 | +local MODULE_PATH = X.NSFormatString('{$NS}_!Base/lib/UI.UserInput') |
| 10 | +-------------------------------------------------------------------------------- |
| 11 | +--[[#DEBUG BEGIN]]X.ReportModuleLoading(MODULE_PATH, 'START')--[[#DEBUG END]] |
| 12 | +-------------------------------------------------------------------------------- |
| 13 | +local _L = X.LoadLangPack(X.PACKET_INFO.FRAMEWORK_ROOT .. 'lang/lib/') |
| 14 | +-------------------------------------------------------------------------------- |
| 15 | + |
| 16 | +local DEFAULT_W, DEFAULT_H = 420, 140 |
| 17 | +local DEFAULT_MULTILINE_W, DEFAULT_MULTILINE_H = 520, 320 |
| 18 | +local BTN_H, BTN_W = 30, 100 |
| 19 | +local PADDING = 10 |
| 20 | + |
| 21 | +local function OpenSingleLineInput(opt) |
| 22 | + local szFrameName = opt.name or X.NSFormatString('{$NS}_DefaultUserInput') |
| 23 | + X.UI.CloseFrame(szFrameName) |
| 24 | + |
| 25 | + local ui |
| 26 | + local function OnResize() |
| 27 | + local nW, nH = ui:ContainerSize() |
| 28 | + ui:Fetch('WndEditBox') |
| 29 | + :Pos(PADDING, PADDING) |
| 30 | + :Size(nW - PADDING * 2, nH - PADDING * 3 - BTN_H) |
| 31 | + ui:Fetch('Btn_Confirm') |
| 32 | + :Pos((nW - BTN_W) / 2, nH - PADDING - BTN_H) |
| 33 | + :Size(BTN_W, BTN_H) |
| 34 | + end |
| 35 | + |
| 36 | + ui = X.UI.CreateFrame(szFrameName, { |
| 37 | + w = opt.w or DEFAULT_W, |
| 38 | + h = opt.h or DEFAULT_H, |
| 39 | + alpha = opt.alpha or 180, |
| 40 | + text = opt.title or _L['User Input'], |
| 41 | + anchor = opt.anchor or { s = 'CENTER', r = 'CENTER', x = 0, y = 0 }, |
| 42 | + theme = X.UI.FRAME_THEME.SIMPLE, |
| 43 | + close = true, |
| 44 | + esc = true, |
| 45 | + resize = true, |
| 46 | + minimize = false, |
| 47 | + onSizeChange = OnResize, |
| 48 | + }) |
| 49 | + |
| 50 | + ui:Append('WndEditBox', { |
| 51 | + name = 'WndEditBox', |
| 52 | + x = PADDING, |
| 53 | + y = PADDING, |
| 54 | + w = (opt.w or DEFAULT_W) - PADDING * 2, |
| 55 | + h = (opt.h or DEFAULT_H) - PADDING * 3 - BTN_H, |
| 56 | + multiline = false, |
| 57 | + text = opt.initialValue and tostring(opt.initialValue) or '', |
| 58 | + placeholder = opt.placeholder, |
| 59 | + alignHorizontal = 0, |
| 60 | + alignVertical = 0, |
| 61 | + maxLength = opt.maxLength, |
| 62 | + onSpecialKeyDown = function(_, szKey) |
| 63 | + if szKey == 'Enter' then |
| 64 | + ui:Fetch('Btn_Confirm'):Click() |
| 65 | + return 1 |
| 66 | + end |
| 67 | + end, |
| 68 | + }) |
| 69 | + |
| 70 | + ui:Append('WndButton', { |
| 71 | + name = 'Btn_Confirm', |
| 72 | + x = 0, |
| 73 | + y = 0, |
| 74 | + w = BTN_W, |
| 75 | + h = BTN_H, |
| 76 | + text = opt.confirmText or (g_tStrings and g_tStrings.STR_HOTKEY_SURE) or 'OK', |
| 77 | + onClick = function() |
| 78 | + local szValue = ui:Fetch('WndEditBox'):Text() or '' |
| 79 | + opt.fnAction(szValue) |
| 80 | + ui:Remove() |
| 81 | + end, |
| 82 | + }) |
| 83 | + |
| 84 | + ui:Focus() |
| 85 | + OnResize() |
| 86 | + return ui |
| 87 | +end |
| 88 | + |
| 89 | +local function OpenMultiLineInput(opt) |
| 90 | + local szFrameName = opt.name or X.NSFormatString('{$NS}_DefaultUserInput_Multiline') |
| 91 | + X.UI.CloseFrame(szFrameName) |
| 92 | + |
| 93 | + local ui |
| 94 | + local function OnResize() |
| 95 | + local nW, nH = ui:ContainerSize() |
| 96 | + ui:Fetch('WndEditBox') |
| 97 | + :Pos(PADDING, PADDING) |
| 98 | + :Size(nW - PADDING * 2, nH - PADDING * 3 - BTN_H) |
| 99 | + ui:Fetch('Btn_Confirm') |
| 100 | + :Pos((nW - BTN_W) / 2, nH - PADDING - BTN_H) |
| 101 | + :Size(BTN_W, BTN_H) |
| 102 | + end |
| 103 | + |
| 104 | + ui = X.UI.CreateFrame(szFrameName, { |
| 105 | + w = opt.w or DEFAULT_MULTILINE_W, |
| 106 | + h = opt.h or DEFAULT_MULTILINE_H, |
| 107 | + alpha = opt.alpha or 180, |
| 108 | + text = opt.title or _L['User Input'], |
| 109 | + anchor = opt.anchor or { s = 'CENTER', r = 'CENTER', x = 0, y = 0 }, |
| 110 | + theme = X.UI.FRAME_THEME.SIMPLE, |
| 111 | + close = true, |
| 112 | + esc = true, |
| 113 | + resize = true, |
| 114 | + minimize = false, |
| 115 | + onSizeChange = OnResize, |
| 116 | + }) |
| 117 | + |
| 118 | + ui:Append('WndEditBox', { |
| 119 | + name = 'WndEditBox', |
| 120 | + x = PADDING, |
| 121 | + y = PADDING, |
| 122 | + w = (opt.w or DEFAULT_MULTILINE_W) - PADDING * 2, |
| 123 | + h = (opt.h or DEFAULT_MULTILINE_H) - PADDING * 3 - BTN_H, |
| 124 | + multiline = true, |
| 125 | + text = opt.initialValue and tostring(opt.initialValue) or '', |
| 126 | + placeholder = opt.placeholder, |
| 127 | + alignHorizontal = 0, |
| 128 | + alignVertical = 0, |
| 129 | + maxLength = opt.maxLength, |
| 130 | + }) |
| 131 | + |
| 132 | + ui:Append('WndButton', { |
| 133 | + name = 'Btn_Confirm', |
| 134 | + x = 0, |
| 135 | + y = 0, |
| 136 | + w = BTN_W, |
| 137 | + h = BTN_H, |
| 138 | + text = opt.confirmText or (g_tStrings and g_tStrings.STR_HOTKEY_SURE) or 'OK', |
| 139 | + onClick = function() |
| 140 | + local szValue = ui:Fetch('WndEditBox'):Text() or '' |
| 141 | + opt.fnAction(szValue) |
| 142 | + ui:Remove() |
| 143 | + end, |
| 144 | + }) |
| 145 | + |
| 146 | + ui:Focus() |
| 147 | + OnResize() |
| 148 | + return ui |
| 149 | +end |
| 150 | + |
| 151 | +-- Global API |
| 152 | +---@class UI_GetUserInput_Options @用户输入参数 |
| 153 | +---@field placeholder string? @占位提示 |
| 154 | +---@field initialValue string|number? @初始值(内部会 tostring) |
| 155 | +---@field multiline boolean? @是否多行 |
| 156 | +---@field maxLength number? @最大长度 |
| 157 | +---@field name string? @窗体名(用于复用/关闭) |
| 158 | +---@field title string? @标题 |
| 159 | +---@field w number? @宽度 |
| 160 | +---@field h number? @高度 |
| 161 | +---@field alpha number? @透明度 |
| 162 | +---@field anchor FrameAnchor? @位置 |
| 163 | +---@field confirmText string? @确认按钮文本 |
| 164 | +---@field fnAction fun(value: string) @确认回调 |
| 165 | + |
| 166 | +---@param opt UI_GetUserInput_Options @参数 |
| 167 | +---@return any ui @窗体对象(由 UI.CreateFrame 返回) |
| 168 | +function X.UI.GetUserInput(opt) |
| 169 | + if not X.IsTable(opt) or type(opt.fnAction) ~= 'function' then |
| 170 | + return |
| 171 | + end |
| 172 | + if opt.multiline then |
| 173 | + return OpenMultiLineInput(opt) |
| 174 | + end |
| 175 | + return OpenSingleLineInput(opt) |
| 176 | +end |
| 177 | + |
| 178 | +---@class UI_GetUserInputNumber_Options @数字输入参数 |
| 179 | +---@field placeholder string? @占位提示 |
| 180 | +---@field initialValue number? @初始值 |
| 181 | +---@field min number? @最小值 |
| 182 | +---@field max number? @最大值 |
| 183 | +---@field maxLength number? @最大长度 |
| 184 | +---@field w number? @宽度 |
| 185 | +---@field h number? @高度 |
| 186 | +---@field alpha number? @透明度 |
| 187 | +---@field anchor FrameAnchor? @位置 |
| 188 | +---@field confirmText string? @确认按钮文本 |
| 189 | +---@field fnAction fun(value: number) @确认回调 |
| 190 | + |
| 191 | +---@param opt UI_GetUserInputNumber_Options @参数 |
| 192 | +---@return any ui @窗体对象(由 UI.CreateFrame 返回) |
| 193 | +function X.UI.GetUserInputNumber(opt) |
| 194 | + if not X.IsTable(opt) or type(opt.fnAction) ~= 'function' then |
| 195 | + return |
| 196 | + end |
| 197 | + return X.UI.GetUserInput({ |
| 198 | + placeholder = opt.placeholder, |
| 199 | + initialValue = opt.initialValue, |
| 200 | + multiline = false, |
| 201 | + maxLength = opt.maxLength, |
| 202 | + w = opt.w, |
| 203 | + h = opt.h, |
| 204 | + alpha = opt.alpha, |
| 205 | + anchor = opt.anchor, |
| 206 | + confirmText = opt.confirmText, |
| 207 | + fnAction = function(szText) |
| 208 | + local n = tonumber(szText) |
| 209 | + if not n then |
| 210 | + return |
| 211 | + end |
| 212 | + if not X.IsNil(opt.min) and n < opt.min then |
| 213 | + return |
| 214 | + end |
| 215 | + if not X.IsNil(opt.max) and n > opt.max then |
| 216 | + return |
| 217 | + end |
| 218 | + opt.fnAction(n) |
| 219 | + end, |
| 220 | + }) |
| 221 | +end |
| 222 | + |
| 223 | +--[[#DEBUG BEGIN]]X.ReportModuleLoading(MODULE_PATH, 'FINISH')--[[#DEBUG END]] |
0 commit comments