annotate UI.lua @ 66:516ceb31703d

New profile system that stores channel and tag settings for each saved AddOn list. A boatload of structural revisions, making better use of the built-in table, and hopefully making console command issues easier to pick up.
author Nenue
date Sat, 05 Mar 2016 13:35:51 -0500
parents 8729d39f6a3d
children 1687ae1c6162
rev   line source
Nenue@50 1 --- Devian - UI.lua
Nenue@35 2 -- @file-author@
Nenue@35 3 -- @project-revision@ @project-hash@
Nenue@35 4 -- @file-revision@ @file-hash@
Nenue@35 5 -- Created: 12/27/2015 3:01 AM
Nenue@35 6 if not LibStub then
Nenue@35 7 print('Something has happened...')
Nenue@35 8 end
Nenue@66 9 local _, D = ...
Nenue@35 10 local DEVIAN_FRAME = 'DevianConsole'
Nenue@66 11 local insert, tonumber, pairs, concat = tinsert, tonumber, pairs, table.concat
Nenue@66 12 local L = D.L
Nenue@66 13 local print = D.print
Nenue@66 14
Nenue@66 15
Nenue@66 16
Nenue@66 17
Nenue@66 18 local Console_OnMovementChanged = function(self, event)
Nenue@66 19 local db = D.db
Nenue@66 20 if self.enabled then
Nenue@66 21 if event == 'PLAYER_STARTED_MOVING' then
Nenue@66 22 self.moveFade:Stop()
Nenue@66 23 local F1 = self.moveFade.alphaOut
Nenue@66 24 F1:SetFromAlpha(db.movement_fade_from)
Nenue@66 25 F1:SetToAlpha(db.movement_fade_to)
Nenue@66 26 F1:SetDuration(db.movement_fade_time)
Nenue@66 27 self.moveFade:Play()
Nenue@66 28 self:EnableMouse(false)
Nenue@66 29 else
Nenue@66 30 self.moveFade:Stop()
Nenue@66 31 local F1 = self.moveFade.alphaOut
Nenue@66 32 F1:SetToAlpha(db.movement_fade_from)
Nenue@66 33 F1:SetFromAlpha(db.movement_fade_to)
Nenue@66 34 F1:SetDuration(db.movement_fade_time)
Nenue@66 35 self.moveFade:Play()
Nenue@66 36 self:EnableMouse(true)
Nenue@66 37 end
Nenue@66 38 end
Nenue@66 39 end
Nenue@66 40
Nenue@35 41
Nenue@35 42
Nenue@35 43 local function Console_MinMax(self)
Nenue@35 44 if self.minimized then
Nenue@35 45 self:Maximize()
Nenue@35 46 else
Nenue@35 47 self:Minimize()
Nenue@35 48 end
Nenue@35 49 end
Nenue@35 50
Nenue@35 51 local function Console_Minimize(self)
Nenue@35 52 self:SetHeight(20)
Nenue@35 53 self:SetMaxResize(GetScreenWidth(),20)
Nenue@35 54 self.minimized = true
Nenue@35 55 self.out:Hide()
Nenue@35 56 self:Save()
Nenue@35 57 end
Nenue@35 58
Nenue@35 59 local function Console_Maximize(self)
Nenue@35 60 local db = D.channels[self.index]
Nenue@35 61 self:SetHeight(db.height)
Nenue@35 62 self:SetMaxResize(GetScreenWidth(),GetScreenHeight())
Nenue@35 63 self.minimized = nil
Nenue@35 64 self.out:Show()
Nenue@35 65 self:Save()
Nenue@35 66 end
Nenue@35 67
Nenue@35 68
Nenue@35 69 local function Console_Save(self)
Nenue@35 70 local db = D.channels[self.index]
Nenue@35 71 db.x = self:GetLeft()
Nenue@35 72 db.y = (self:GetTop() - GetScreenHeight())
Nenue@35 73 db.width = self:GetWidth()
Nenue@35 74
Nenue@35 75 if not self.minimized then
Nenue@55 76 db.height = self:GetHeight()
Nenue@35 77 self:SetHeight(db.height)
Nenue@35 78 end
Nenue@35 79
Nenue@35 80 db.dockedTo = self.dockedTo
Nenue@35 81 db.docked = self.docked
Nenue@35 82
Nenue@35 83 db.minimized = self.minimized and true or nil
Nenue@35 84 db.enabled = self:IsVisible() and true or nil
Nenue@35 85 db.active = self.active and true or nil
Nenue@35 86 self:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', db.x, db.y)
Nenue@35 87 self:SetWidth(db.width)
Nenue@35 88 end
Nenue@35 89
Nenue@35 90 --- Brings the console to the front.
Nenue@35 91 -- Frame method used to bring a console frame to the front of the display stack.
Nenue@35 92 local function Console_ToFront(c)
Nenue@35 93 local db = D.db
Nenue@35 94 c:Raise()
Nenue@35 95 c:SetAlpha(db.frontalpha)
Nenue@35 96 c.out.backdrop:SetTexture(unpack(db.frontdrop))
Nenue@35 97 c.out.backdrop:SetGradientAlpha(unpack(db.frontgrad))
Nenue@35 98 c.out.backdrop:SetBlendMode(db.frontblend)
Nenue@45 99 c.dropmenu.icon:SetVertexColor(unpack(db.frontheader))
Nenue@45 100 c.title:SetTextColor(unpack(db.frontborder))
Nenue@60 101
Nenue@62 102 --oldprint('changing current toplevel from', db.current_channel, 'to', c.index)
Nenue@35 103 db.current_channel = c.index
Nenue@35 104
Nenue@62 105 --oldprint('toplevel is now', db.current_channel)
Nenue@60 106 c:Save()
Nenue@60 107
Nenue@60 108
Nenue@50 109 -- update dock buttons
Nenue@50 110 local beacon = D.dock.buttons[c.index]
Nenue@50 111 beacon.selected = true
Nenue@58 112 beacon.newMessage = nil
Nenue@50 113 D.UpdateBeacon(beacon)
Nenue@50 114
Nenue@35 115 for _, part in pairs(c.border) do
Nenue@35 116 part:SetTexture(unpack(db.frontborder))
Nenue@35 117 end
Nenue@35 118
Nenue@35 119 for id, bc in pairs(D.console) do
Nenue@35 120 if id ~= c.index then
Nenue@35 121 bc:SetAlpha(db.backalpha)
Nenue@35 122 bc.out.backdrop:SetTexture(unpack(db.backdrop))
Nenue@35 123 bc.out.backdrop:SetGradientAlpha(unpack(db.backgrad))
Nenue@35 124 bc.out.backdrop:SetBlendMode(db.backblend)
Nenue@45 125 bc.dropmenu.icon:SetVertexColor(unpack(db.backheader))
Nenue@36 126 bc.title:SetTextColor(unpack(db.backborder))
Nenue@35 127
Nenue@50 128 local beacon = D.dock.buttons[bc.index]
Nenue@58 129 beacon.raised = nil
Nenue@58 130 beacon.showName = nil
Nenue@50 131 beacon.selected = nil
Nenue@50 132 D.UpdateBeacon(beacon)
Nenue@50 133
Nenue@35 134 for _, part in pairs(bc.border) do
Nenue@35 135 part:SetTexture(unpack(db.backborder))
Nenue@35 136 end
Nenue@35 137 end
Nenue@35 138 end
Nenue@35 139 end
Nenue@35 140
Nenue@35 141 local function Console_MouseDown(self, button, up)
Nenue@35 142 if button == 'LeftButton' then
Nenue@35 143 if up then
Nenue@35 144 self:StopMovingOrSizing()
Nenue@35 145 self:ToFront()
Nenue@35 146 self:Save()
Nenue@35 147 elseif self.out.grip:IsMouseOver() then
Nenue@35 148 self:StartSizing()
Nenue@35 149 else
Nenue@35 150 self:StartMoving()
Nenue@35 151 end
Nenue@35 152 else
Nenue@35 153 if up then
Nenue@35 154 self:MinMax()
Nenue@35 155 end
Nenue@35 156 end
Nenue@35 157 end
Nenue@35 158 local function Console_MouseUp(self, button)
Nenue@35 159 return Console_MouseDown(self, button, true)
Nenue@35 160 end
Nenue@35 161
Nenue@35 162
Nenue@35 163 --- Constructs the frame object for a console channel
Nenue@35 164 -- Initializes the console channel at a specified index.
Nenue@35 165 -- Configuration data can be overridden by passing a desired settings table.
Nenue@35 166 -- @param i Numeric index of the channel as it manifests in db.channels
Nenue@35 167 -- @param vars Optional settings table to be used.
Nenue@35 168 local function CreateConsole(i, vars)
Nenue@35 169 local db = D.db
Nenue@35 170 if tonumber(i) == nil or math.floor(i) ~= i then
Nenue@35 171 error('Non-integer index value.')
Nenue@35 172 end
Nenue@35 173 if not vars then
Nenue@35 174 vars = D.channels[i] and D.channels[i] or D.channels[db.primary_channel]
Nenue@35 175 end
Nenue@35 176 local f
Nenue@47 177 f= CreateFrame('Frame', 'DevianChannelFrame' .. i, UIParent, DEVIAN_FRAME)
Nenue@47 178
Nenue@35 179 --@debug@
Nenue@35 180 --print(f:GetName())
Nenue@35 181
Nenue@35 182 --print('create(2)')
Nenue@35 183 for k,v in pairs(vars) do
Nenue@35 184 f[k] = v
Nenue@35 185 --@debug@
Nenue@36 186 --print(' f['..type(k)..' '..tostring(k)..'] = '..type(v)..' '..tostring(v))
Nenue@35 187 end
Nenue@35 188
Nenue@35 189 f:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', vars.x, vars.y)
Nenue@35 190 f:SetSize(vars.width, vars.height)
Nenue@35 191 f:Lower()
Nenue@35 192 f.out:SetFont(db.font, db.fontsize, db.fontoutline)
Nenue@35 193 if (db.current_channel == i) then
Nenue@35 194 f.out.backdrop:SetTexture(unpack(db.frontdrop))
Nenue@36 195 f.dropmenu.icon:SetVertexColor(unpack(db.headerfontcolor))
Nenue@36 196 f.title:SetTextColor(unpack(db.headerfontcolor))
Nenue@45 197 f.header:SetAlpha(db.headeralpha)
Nenue@35 198 else
Nenue@35 199 f.out.backdrop:SetTexture(unpack(db.backdrop))
Nenue@35 200 end
Nenue@35 201
Nenue@35 202 f.Save = Console_Save
Nenue@35 203 f.Minimize = Console_Minimize
Nenue@35 204 f.Maximize = Console_Maximize
Nenue@35 205 f.MinMax = Console_MinMax
Nenue@35 206 f.ToFront = Console_ToFront
Nenue@35 207 f.Toggle = D.Console_Toggle
Nenue@35 208 f:SetScript('OnMouseDown', Console_MouseDown)
Nenue@35 209 f:SetScript('OnMouseUp', Console_MouseUp)
Nenue@66 210 f.profileID = db.current_profile
Nenue@35 211
Nenue@36 212
Nenue@36 213 UIDropDownMenu_Initialize(f.menuFrame, function()
Nenue@36 214 local info = { {
Nenue@36 215 text= "Close",
Nenue@36 216 value = "OptClose",
Nenue@36 217 func = function ()
Nenue@36 218 f.enabled = nil
Nenue@36 219 f:Hide()
Nenue@36 220 f:Save()
Nenue@38 221 end },--[[
Nenue@36 222 {
Nenue@36 223 text = "Dock",
Nenue@36 224 value = "OptDock",
Nenue@38 225 func = function() print('Dvn', 'docking shenanary') end }]]
Nenue@36 226 }
Nenue@36 227 for _, v in ipairs(info) do
Nenue@36 228 UIDropDownMenu_AddButton(v)
Nenue@36 229 end
Nenue@36 230 end, 'MENU')
Nenue@36 231
Nenue@66 232 if db.movement_fade then
Nenue@66 233 f:RegisterEvent('PLAYER_STARTED_MOVING')
Nenue@66 234 f:RegisterEvent('PLAYER_STOPPED_MOVING')
Nenue@66 235 f:SetScript('OnEvent', Console_OnMovementChanged)
Nenue@66 236 end
Nenue@66 237
Nenue@47 238 D.dock.buttons[i] = CreateFrame('Button', 'Channel'..i..'Beacon', UIParent, 'DevianBeacon')
Nenue@47 239 D.dock.buttons[i].icon:SetVertexColor(math.random(),math.random(),math.random())
Nenue@47 240 D.dock.buttons[i].console = f
Nenue@47 241 D.dock.buttons[i].index = i
Nenue@47 242 D.dock.buttons[i].caption.name:SetText(vars.signature)
Nenue@47 243 D.dock.buttons[i]:Show()
Nenue@47 244
Nenue@35 245 if vars.minimized then
Nenue@35 246 f:Minimize()
Nenue@35 247 else
Nenue@35 248 f:Maximize()
Nenue@35 249 end
Nenue@35 250 if db.enabled and f.enabled then
Nenue@35 251 f:Show()
Nenue@35 252 end
Nenue@35 253
Nenue@35 254 return f
Nenue@35 255 end
Nenue@35 256
Nenue@35 257
Nenue@35 258 --- Updates console information and returns the handle of the channel object that was worked on.
Nenue@35 259 -- When key is nil or not a valid handle, a new channel is created using whatever signature can be found in cinfo.
Nenue@35 260 -- The signature can be passed as a string, or as a table entry under the key 'signature'
Nenue@35 261 -- If the signature of a new channel is also a tag, the channel will be added to that tag
Nenue@35 262 -- @param cinfo string signature of a new channel, or a table of config variables to be imposed on the channel
Nenue@35 263 -- @param key string signature or index number of channel to operate on
Nenue@35 264 -- @usage channel = D:SetChannel('new', nil) -- creates a new channel
Nenue@35 265 -- @usage channel = D:SetChannel({x = 200, y = 100}, 4) -- updates channel #4
Nenue@35 266 function D:SetChannel(cinfo, key)
Nenue@66 267 local profile = D.currentProfile
Nenue@35 268 local t_info = {}
Nenue@35 269 local channel, isNew, id, sig, t_id
Nenue@36 270 --@debug@
Nenue@66 271 print('setchan(0) cinfo, key', cinfo, key)--@end-debug@
Nenue@35 272 -- obtain source data
Nenue@66 273 if tonumber(key) ~= nil and D.channels[key] then
Nenue@35 274 id = tonumber(key)
Nenue@35 275 elseif D.sigID[tostring(key)] then
Nenue@35 276 id = D.sigID[tostring(key)]
Nenue@35 277 else
Nenue@66 278 id = profile.default_channel
Nenue@35 279 isNew = true
Nenue@35 280 end
Nenue@66 281 local dbvars = D.channels[id]
Nenue@35 282 t_id = id -- overridden later if new
Nenue@35 283 t_info.index = t_id --
Nenue@35 284 --@debug@
Nenue@66 285 print('setchan(1) cinfo, key, id=', cinfo, key, id)--@end-debug@
Nenue@35 286
Nenue@35 287
Nenue@35 288 -- obtain config info
Nenue@35 289 if type(cinfo) == 'string' then
Nenue@35 290 sig = cinfo
Nenue@35 291 cinfo = {signature = sig}
Nenue@35 292 elseif type(cinfo) ~= 'table' then -- stop here if a table wans't passed
Nenue@66 293 error('Expecting table of string as arg1')
Nenue@35 294 elseif cinfo.signature then -- new sig
Nenue@66 295 sig = cinfo.signature
Nenue@35 296 elseif isNew then -- new channel sig
Nenue@66 297 sig = 'Ch'
Nenue@35 298 else -- old sig
Nenue@66 299 sig = D.channels[id].signature
Nenue@35 300 end
Nenue@35 301 t_info.signature = sig
Nenue@35 302 --@debug@
Nenue@66 303 print('setchan(2) sig,id,isNew=', sig, id, isNew)--@end-debug@
Nenue@35 304
Nenue@35 305 for k,v in pairs(cinfo) do -- allow all cinfo to pass
Nenue@35 306 t_info[k] = v
Nenue@35 307 end
Nenue@35 308
Nenue@35 309 local blocked = { -- ignore these vars:
Nenue@35 310 ['docked'] = true, -- table
Nenue@35 311 ['dockedTo'] = true, -- table-related
Nenue@35 312 ['signature'] = true} -- already determined
Nenue@35 313 for k,v in pairs(dbvars) do
Nenue@35 314 if not t_info[k] and not blocked[k] then -- already set or blocked?
Nenue@35 315 t_info[k] = v
Nenue@35 316 end
Nenue@35 317 end
Nenue@35 318 -- new channel overrides
Nenue@35 319 if isNew then
Nenue@35 320 if D.sigID[sig]then -- find a non-clashing signature
Nenue@35 321 local result, i = sig, 1
Nenue@35 322 while D.sigID[result] do
Nenue@35 323 result = sig .. i
Nenue@35 324 i = i + 1
Nenue@35 325 end
Nenue@35 326 t_info.signature = result
Nenue@35 327 end
Nenue@36 328 t_id = self.max_channel + 1
Nenue@35 329 t_info.index = t_id
Nenue@35 330 --@debug@
Nenue@66 331 print('setchan(3a) t_id, isNew, sig, t_info.signature=', t_id, isNew, sig, t_info.signature)--@end-debug@
Nenue@35 332 else
Nenue@35 333 --@debug@
Nenue@66 334 print('setchan(3b) t_id, isNew, sig, t_info.signature=', t_id, isNew, sig, t_info.signature)--@end-debug@
Nenue@35 335 end
Nenue@35 336
Nenue@35 337 local channel
Nenue@35 338 if not self.console[t_id] then -- create a frame
Nenue@66 339 if isNew then -- position the channel frame
Nenue@66 340 profile.max_channel = t_id
Nenue@66 341 t_info.x = t_info.x + 20
Nenue@66 342 t_info.y = t_info.y - 20
Nenue@66 343 profile.channels[t_id] = t_info
Nenue@66 344 --@debug@
Nenue@66 345 print('setchan(4a)', 't_id, x, y=', t_id, t_info.x, t_info.y)--@end-debug@
Nenue@66 346 end
Nenue@66 347 channel = CreateConsole(t_id, t_info)
Nenue@66 348 self.console[t_id] = channel
Nenue@66 349 self.sig[t_info.signature] = channel
Nenue@66 350 self.sigID[t_info.signature] = t_id
Nenue@66 351 self.IDsig[t_id] = t_info.signature
Nenue@35 352 end
Nenue@35 353 channel = self.console[t_id]
Nenue@35 354 if channel.minimized then
Nenue@35 355 channel:Minimize()
Nenue@35 356 else
Nenue@35 357 channel:Maximize()
Nenue@35 358 end
Nenue@35 359
Nenue@55 360 if channel.enabled then -- hide or show last since Min/Max mess with visibility
Nenue@66 361 print('setchan(5a) enable')
Nenue@55 362 channel:Show()
Nenue@60 363 --channel:ToFront()
Nenue@35 364 else
Nenue@66 365 print('setchan(5a) disable')
Nenue@35 366 channel:Hide()
Nenue@35 367 end
Nenue@35 368 --@debug@
Nenue@66 369 print('setchan(end); c:IsVisible(), c.enabled, db.enabled=', channel:IsVisible(), channel.enabled, profile.enabled)--@end-debug@
Nenue@35 370 return channel
Nenue@66 371 end
Nenue@66 372
Nenue@66 373 --- Console frame toggler
Nenue@66 374 -- @paramsig [...]
Nenue@66 375 -- @param ... one or more space-seperated channel keys
Nenue@66 376 function D:Console_Toggle(input, force)
Nenue@66 377 local profile = D.currentProfile
Nenue@66 378 --oldprint(input)
Nenue@66 379 local setAll
Nenue@66 380 local search = {}
Nenue@66 381 local key
Nenue@66 382 local n = 0
Nenue@66 383 while self:GetArgs(input,1,n) and n < 255 do --should end itself when it gets nil, but
Nenue@66 384 key, n = self:GetArgs(input,1,n)
Nenue@66 385
Nenue@66 386 if self.sig[key] then
Nenue@66 387 --print(key, self.sigID[key])
Nenue@66 388 insert(search, self.sigID[key])
Nenue@66 389 elseif self.console[tonumber(key)] then
Nenue@66 390 --print(key, tonumber(key))
Nenue@66 391 insert(search, tonumber(key))
Nenue@66 392 end
Nenue@66 393
Nenue@66 394 --oldprint(#search, key, n)
Nenue@66 395 end
Nenue@66 396 if #search < 1 then
Nenue@66 397 search = self.sigID
Nenue@66 398 setAll = true
Nenue@66 399 end
Nenue@66 400 if setAll then
Nenue@66 401 --oldprint('setall', setAll)
Nenue@66 402 profile.enabled = (not profile.enabled) and true or nil
Nenue@66 403 if force == 0 then
Nenue@66 404 profile.enabled = nil
Nenue@66 405 end
Nenue@66 406 end
Nenue@66 407
Nenue@66 408 for i, id in pairs(search) do
Nenue@66 409 --oldprint(i, id)
Nenue@66 410 local c = self.console[id]
Nenue@66 411 if setAll then
Nenue@66 412 c.enabled = profile.enabled and profile.enabled or nil
Nenue@66 413 else
Nenue@66 414
Nenue@66 415 profile.enabled = true
Nenue@66 416 c.enabled = (not c.enabled) and true or nil
Nenue@66 417 if force == 0 then
Nenue@66 418 c.enabled = nil
Nenue@66 419 end
Nenue@66 420 --oldprint(id, ' ', force, c.enabled, db.enabled)
Nenue@66 421 end
Nenue@66 422
Nenue@66 423 if c.enabled or (setAll and profile.enabled) then
Nenue@66 424 c:Show()
Nenue@66 425 elseif not (c.enabled and profile.enabled) then
Nenue@66 426 c:Hide()
Nenue@66 427 end
Nenue@66 428 c:Save()
Nenue@66 429 end
Nenue@66 430
Nenue@66 431 if setAll then
Nenue@66 432 if profile.enabled then
Nenue@66 433 self:Print('toggled all consoles ON')
Nenue@66 434 if D.console[profile.current_channel] then
Nenue@66 435 D.console[profile.current_channel]:ToFront()
Nenue@66 436 end
Nenue@66 437 else
Nenue@66 438 self:Print('toggled all consoles OFF')
Nenue@66 439 end
Nenue@66 440 else
Nenue@66 441 local result = {}
Nenue@66 442 for i, id in pairs(search) do
Nenue@66 443 result[i] = tostring(id) .. ' = ' .. (self.console[id].enabled and 'ON' or 'OFF')
Nenue@66 444 end
Nenue@66 445 self:Print('toggled: '..concat(result, ', '))
Nenue@66 446 end
Nenue@66 447 end