annotate UI.lua @ 97:34131d11e61b

7.1 TOC
author Nenue
date Tue, 25 Oct 2016 12:35:12 -0400
parents f6fae1a4c66c
children 33bc8baba858
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@70 70 local db = D.currentProfile.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@70 83 db.minimized = self.minimized
Nenue@70 84 db.enabled = self.enabled
Nenue@70 85 db.active = self.active
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@68 94 local profile = D.currentProfile
Nenue@35 95 c:Raise()
Nenue@35 96 c:SetAlpha(db.frontalpha)
Nenue@83 97 c.out.backdrop:SetColorTexture(unpack(db.frontdrop))
Nenue@35 98 c.out.backdrop:SetGradientAlpha(unpack(db.frontgrad))
Nenue@35 99 c.out.backdrop:SetBlendMode(db.frontblend)
Nenue@45 100 c.dropmenu.icon:SetVertexColor(unpack(db.frontheader))
Nenue@45 101 c.title:SetTextColor(unpack(db.frontborder))
Nenue@60 102
Nenue@62 103 --oldprint('changing current toplevel from', db.current_channel, 'to', c.index)
Nenue@68 104 profile.current_channel = c.index
Nenue@35 105
Nenue@62 106 --oldprint('toplevel is now', db.current_channel)
Nenue@60 107 c:Save()
Nenue@60 108
Nenue@60 109
Nenue@50 110 -- update dock buttons
Nenue@93 111 local beacon = c.dockButton
Nenue@50 112 beacon.selected = true
Nenue@58 113 beacon.newMessage = nil
Nenue@93 114 beacon:Update()
Nenue@50 115
Nenue@35 116 for _, part in pairs(c.border) do
Nenue@83 117 part:SetColorTexture(unpack(db.frontborder))
Nenue@35 118 end
Nenue@35 119
Nenue@35 120 for id, bc in pairs(D.console) do
Nenue@35 121 if id ~= c.index then
Nenue@35 122 bc:SetAlpha(db.backalpha)
Nenue@83 123 bc.out.backdrop:SetColorTexture(unpack(db.backdrop))
Nenue@35 124 bc.out.backdrop:SetGradientAlpha(unpack(db.backgrad))
Nenue@35 125 bc.out.backdrop:SetBlendMode(db.backblend)
Nenue@45 126 bc.dropmenu.icon:SetVertexColor(unpack(db.backheader))
Nenue@36 127 bc.title:SetTextColor(unpack(db.backborder))
Nenue@35 128
Nenue@93 129 local beacon = D.dock.usedButtons[bc.index]
Nenue@58 130 beacon.raised = nil
Nenue@58 131 beacon.showName = nil
Nenue@50 132 beacon.selected = nil
Nenue@93 133 beacon:Update()
Nenue@50 134
Nenue@35 135 for _, part in pairs(bc.border) do
Nenue@83 136 part:SetColorTexture(unpack(db.backborder))
Nenue@35 137 end
Nenue@35 138 end
Nenue@35 139 end
Nenue@35 140 end
Nenue@35 141
Nenue@35 142 local function Console_MouseDown(self, button, up)
Nenue@35 143 if button == 'LeftButton' then
Nenue@35 144 if up then
Nenue@35 145 self:StopMovingOrSizing()
Nenue@35 146 self:ToFront()
Nenue@35 147 self:Save()
Nenue@35 148 elseif self.out.grip:IsMouseOver() then
Nenue@35 149 self:StartSizing()
Nenue@35 150 else
Nenue@35 151 self:StartMoving()
Nenue@35 152 end
Nenue@35 153 else
Nenue@35 154 if up then
Nenue@35 155 self:MinMax()
Nenue@35 156 end
Nenue@35 157 end
Nenue@35 158 end
Nenue@35 159 local function Console_MouseUp(self, button)
Nenue@35 160 return Console_MouseDown(self, button, true)
Nenue@35 161 end
Nenue@35 162
Nenue@35 163
Nenue@35 164 --- Constructs the frame object for a console channel
Nenue@35 165 -- Initializes the console channel at a specified index.
Nenue@35 166 -- Configuration data can be overridden by passing a desired settings table.
Nenue@35 167 -- @param i Numeric index of the channel as it manifests in db.channels
Nenue@35 168 -- @param vars Optional settings table to be used.
Nenue@35 169 local function CreateConsole(i, vars)
Nenue@35 170 local db = D.db
Nenue@35 171 if tonumber(i) == nil or math.floor(i) ~= i then
Nenue@35 172 error('Non-integer index value.')
Nenue@35 173 end
Nenue@35 174 if not vars then
Nenue@35 175 vars = D.channels[i] and D.channels[i] or D.channels[db.primary_channel]
Nenue@35 176 end
Nenue@35 177 local f
Nenue@70 178 f = CreateFrame('Frame', 'DevianChannelFrame' .. i, UIParent, DEVIAN_FRAME)
Nenue@47 179
Nenue@35 180 --@debug@
Nenue@35 181 --print(f:GetName())
Nenue@35 182
Nenue@35 183 --print('create(2)')
Nenue@35 184 for k,v in pairs(vars) do
Nenue@35 185 f[k] = v
Nenue@35 186 --@debug@
Nenue@36 187 --print(' f['..type(k)..' '..tostring(k)..'] = '..type(v)..' '..tostring(v))
Nenue@35 188 end
Nenue@35 189
Nenue@35 190 f:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', vars.x, vars.y)
Nenue@35 191 f:SetSize(vars.width, vars.height)
Nenue@35 192 f:Lower()
Nenue@35 193 f.out:SetFont(db.font, db.fontsize, db.fontoutline)
Nenue@35 194 if (db.current_channel == i) then
Nenue@83 195 f.out.backdrop:SetColorTexture(unpack(db.frontdrop))
Nenue@36 196 f.dropmenu.icon:SetVertexColor(unpack(db.headerfontcolor))
Nenue@36 197 f.title:SetTextColor(unpack(db.headerfontcolor))
Nenue@45 198 f.header:SetAlpha(db.headeralpha)
Nenue@35 199 else
Nenue@83 200 f.out.backdrop:SetColorTexture(unpack(db.backdrop))
Nenue@35 201 end
Nenue@35 202
Nenue@35 203 f.Save = Console_Save
Nenue@35 204 f.Minimize = Console_Minimize
Nenue@35 205 f.Maximize = Console_Maximize
Nenue@35 206 f.MinMax = Console_MinMax
Nenue@35 207 f.ToFront = Console_ToFront
Nenue@35 208 f.Toggle = D.Console_Toggle
Nenue@35 209 f:SetScript('OnMouseDown', Console_MouseDown)
Nenue@35 210 f:SetScript('OnMouseUp', Console_MouseUp)
Nenue@66 211 f.profileID = db.current_profile
Nenue@35 212
Nenue@36 213
Nenue@36 214 UIDropDownMenu_Initialize(f.menuFrame, function()
Nenue@36 215 local info = { {
Nenue@36 216 text= "Close",
Nenue@36 217 value = "OptClose",
Nenue@36 218 func = function ()
Nenue@36 219 f.enabled = nil
Nenue@36 220 f:Hide()
Nenue@36 221 f:Save()
Nenue@38 222 end },--[[
Nenue@36 223 {
Nenue@36 224 text = "Dock",
Nenue@36 225 value = "OptDock",
Nenue@38 226 func = function() print('Dvn', 'docking shenanary') end }]]
Nenue@36 227 }
Nenue@36 228 for _, v in ipairs(info) do
Nenue@36 229 UIDropDownMenu_AddButton(v)
Nenue@36 230 end
Nenue@36 231 end, 'MENU')
Nenue@36 232
Nenue@66 233 if db.movement_fade then
Nenue@66 234 f:RegisterEvent('PLAYER_STARTED_MOVING')
Nenue@66 235 f:RegisterEvent('PLAYER_STOPPED_MOVING')
Nenue@66 236 f:SetScript('OnEvent', Console_OnMovementChanged)
Nenue@66 237 end
Nenue@66 238
Nenue@93 239 f.dockButton = D:GetDockButton(f)
Nenue@93 240
Nenue@47 241
Nenue@35 242 if vars.minimized then
Nenue@35 243 f:Minimize()
Nenue@35 244 else
Nenue@35 245 f:Maximize()
Nenue@35 246 end
Nenue@68 247 if db.enabled then
Nenue@68 248 f.enabled = true
Nenue@35 249 f:Show()
Nenue@35 250 end
Nenue@35 251
Nenue@35 252 return f
Nenue@35 253 end
Nenue@35 254
Nenue@35 255
Nenue@35 256 --- Updates console information and returns the handle of the channel object that was worked on.
Nenue@35 257 -- When key is nil or not a valid handle, a new channel is created using whatever signature can be found in cinfo.
Nenue@35 258 -- The signature can be passed as a string, or as a table entry under the key 'signature'
Nenue@35 259 -- If the signature of a new channel is also a tag, the channel will be added to that tag
Nenue@35 260 -- @param cinfo string signature of a new channel, or a table of config variables to be imposed on the channel
Nenue@35 261 -- @param key string signature or index number of channel to operate on
Nenue@35 262 -- @usage channel = D:SetChannel('new', nil) -- creates a new channel
Nenue@35 263 -- @usage channel = D:SetChannel({x = 200, y = 100}, 4) -- updates channel #4
Nenue@35 264 function D:SetChannel(cinfo, key)
Nenue@66 265 local profile = D.currentProfile
Nenue@35 266 local t_info = {}
Nenue@35 267 local channel, isNew, id, sig, t_id
Nenue@36 268 --@debug@
Nenue@68 269 --print('setchan(0) cinfo, key', cinfo, key)--@end-debug@
Nenue@35 270 -- obtain source data
Nenue@66 271 if tonumber(key) ~= nil and D.channels[key] then
Nenue@35 272 id = tonumber(key)
Nenue@35 273 elseif D.sigID[tostring(key)] then
Nenue@35 274 id = D.sigID[tostring(key)]
Nenue@35 275 else
Nenue@66 276 id = profile.default_channel
Nenue@35 277 isNew = true
Nenue@35 278 end
Nenue@66 279 local dbvars = D.channels[id]
Nenue@35 280 t_id = id -- overridden later if new
Nenue@35 281 t_info.index = t_id --
Nenue@35 282 --@debug@
Nenue@68 283 --print('setchan(1) cinfo, key, id=', cinfo, key, id)--@end-debug@
Nenue@35 284
Nenue@35 285
Nenue@35 286 -- obtain config info
Nenue@35 287 if type(cinfo) == 'string' then
Nenue@35 288 sig = cinfo
Nenue@35 289 cinfo = {signature = sig}
Nenue@35 290 elseif type(cinfo) ~= 'table' then -- stop here if a table wans't passed
Nenue@66 291 error('Expecting table of string as arg1')
Nenue@35 292 elseif cinfo.signature then -- new sig
Nenue@66 293 sig = cinfo.signature
Nenue@35 294 elseif isNew then -- new channel sig
Nenue@66 295 sig = 'Ch'
Nenue@35 296 else -- old sig
Nenue@66 297 sig = D.channels[id].signature
Nenue@35 298 end
Nenue@35 299 t_info.signature = sig
Nenue@35 300 --@debug@
Nenue@68 301 --print('setchan(2) sig,id,isNew=', sig, id, isNew)--@end-debug@
Nenue@35 302
Nenue@35 303 for k,v in pairs(cinfo) do -- allow all cinfo to pass
Nenue@35 304 t_info[k] = v
Nenue@35 305 end
Nenue@35 306
Nenue@35 307 local blocked = { -- ignore these vars:
Nenue@35 308 ['docked'] = true, -- table
Nenue@35 309 ['dockedTo'] = true, -- table-related
Nenue@35 310 ['signature'] = true} -- already determined
Nenue@35 311 for k,v in pairs(dbvars) do
Nenue@35 312 if not t_info[k] and not blocked[k] then -- already set or blocked?
Nenue@70 313 --print('assign', k, '=', v)
Nenue@35 314 t_info[k] = v
Nenue@35 315 end
Nenue@35 316 end
Nenue@35 317 -- new channel overrides
Nenue@35 318 if isNew then
Nenue@72 319 if D.sigID[sig] then -- find a non-clashing signature
Nenue@35 320 local result, i = sig, 1
Nenue@35 321 while D.sigID[result] do
Nenue@35 322 result = sig .. i
Nenue@35 323 i = i + 1
Nenue@35 324 end
Nenue@35 325 t_info.signature = result
Nenue@35 326 end
Nenue@36 327 t_id = self.max_channel + 1
Nenue@35 328 t_info.index = t_id
Nenue@72 329
Nenue@72 330 self.max_channel = t_id
Nenue@35 331 --@debug@
Nenue@68 332 --print('setchan(3a) t_id, isNew, sig, t_info.signature=', t_id, isNew, sig, t_info.signature)--@end-debug@
Nenue@35 333 else
Nenue@35 334 --@debug@
Nenue@68 335 --print('setchan(3b) t_id, isNew, sig, t_info.signature=', t_id, isNew, sig, t_info.signature)--@end-debug@
Nenue@35 336 end
Nenue@35 337
Nenue@35 338 local channel
Nenue@35 339 if not self.console[t_id] then -- create a frame
Nenue@66 340 if isNew then -- position the channel frame
Nenue@66 341 profile.max_channel = t_id
Nenue@66 342 t_info.x = t_info.x + 20
Nenue@66 343 t_info.y = t_info.y - 20
Nenue@66 344 profile.channels[t_id] = t_info
Nenue@66 345 --@debug@
Nenue@68 346 --print('setchan(4a)', 't_id, x, y=', t_id, t_info.x, t_info.y)--@end-debug@
Nenue@66 347 end
Nenue@66 348 channel = CreateConsole(t_id, t_info)
Nenue@66 349 self.console[t_id] = channel
Nenue@66 350 self.sig[t_info.signature] = channel
Nenue@66 351 self.sigID[t_info.signature] = t_id
Nenue@66 352 self.IDsig[t_id] = t_info.signature
Nenue@35 353 end
Nenue@35 354 channel = self.console[t_id]
Nenue@35 355 if channel.minimized then
Nenue@35 356 channel:Minimize()
Nenue@35 357 else
Nenue@35 358 channel:Maximize()
Nenue@35 359 end
Nenue@35 360
Nenue@55 361 if channel.enabled then -- hide or show last since Min/Max mess with visibility
Nenue@70 362 --print('setchan(5a) enable')
Nenue@55 363 channel:Show()
Nenue@60 364 --channel:ToFront()
Nenue@35 365 else
Nenue@68 366 --print('setchan(5a) disable')
Nenue@35 367 channel:Hide()
Nenue@35 368 end
Nenue@35 369 --@debug@
Nenue@68 370 --print('setchan(end); c:IsVisible(), c.enabled, db.enabled=', channel:IsVisible(), channel.enabled, profile.enabled)--@end-debug@
Nenue@35 371 return channel
Nenue@66 372 end
Nenue@66 373
Nenue@66 374 --- Console frame toggler
Nenue@66 375 -- @paramsig [...]
Nenue@66 376 -- @param ... one or more space-seperated channel keys
Nenue@87 377 function D:Console_Toggle(cmd, force)
Nenue@87 378
Nenue@87 379
Nenue@88 380 local search = {}
Nenue@87 381 if cmd then
Nenue@87 382 cmd = tostring(cmd)
Nenue@87 383 local i, j = 0, 0
Nenue@87 384 repeat
Nenue@87 385 i, j = cmd:find("%S+", j+1)
Nenue@87 386 if i and j then
Nenue@87 387 local key = cmd:sub(i, j)
Nenue@87 388 if self.sig[key] then
Nenue@87 389 --print(key, self.sigID[key])
Nenue@87 390 insert(search, self.sigID[key])
Nenue@87 391 elseif self.console[tonumber(key)] then
Nenue@87 392 --print(key, tonumber(key))
Nenue@87 393 insert(search, tonumber(key))
Nenue@87 394 end
Nenue@87 395 end
Nenue@87 396 until not(i or j)
Nenue@87 397 end
Nenue@87 398
Nenue@66 399 local profile = D.currentProfile
Nenue@66 400 local setAll
Nenue@70 401
Nenue@70 402 -- if nothing was found, then do an all toggle
Nenue@66 403 if #search < 1 then
Nenue@70 404 search = self.sigID -- using this since it lists frame ID's
Nenue@66 405 setAll = true
Nenue@66 406 end
Nenue@70 407
Nenue@70 408 -- sort global enable state
Nenue@66 409 if setAll then
Nenue@66 410 --oldprint('setall', setAll)
Nenue@66 411 profile.enabled = (not profile.enabled) and true or nil
Nenue@66 412 if force == 0 then
Nenue@66 413 profile.enabled = nil
Nenue@66 414 end
Nenue@66 415 end
Nenue@66 416
Nenue@70 417 -- sort local enable states: if global enable, apply that change to channel
Nenue@70 418 -- else, channel enable takes precedence
Nenue@66 419 for i, id in pairs(search) do
Nenue@66 420 local c = self.console[id]
Nenue@66 421 if setAll then
Nenue@70 422 c.enabled = profile.enabled
Nenue@66 423 else
Nenue@66 424 c.enabled = (not c.enabled) and true or nil
Nenue@66 425 if force == 0 then
Nenue@66 426 c.enabled = nil
Nenue@66 427 end
Nenue@66 428 end
Nenue@66 429
Nenue@70 430 if c.enabled then
Nenue@70 431 profile.enabled = true
Nenue@66 432 c:Show()
Nenue@70 433 else
Nenue@66 434 c:Hide()
Nenue@66 435 end
Nenue@66 436 c:Save()
Nenue@66 437 end
Nenue@66 438
Nenue@66 439 if setAll then
Nenue@66 440 if profile.enabled then
Nenue@66 441 self:Print('toggled all consoles ON')
Nenue@66 442 if D.console[profile.current_channel] then
Nenue@66 443 D.console[profile.current_channel]:ToFront()
Nenue@66 444 end
Nenue@66 445 else
Nenue@66 446 self:Print('toggled all consoles OFF')
Nenue@66 447 end
Nenue@66 448 else
Nenue@66 449 local result = {}
Nenue@66 450 for i, id in pairs(search) do
Nenue@66 451 result[i] = tostring(id) .. ' = ' .. (self.console[id].enabled and 'ON' or 'OFF')
Nenue@66 452 end
Nenue@66 453 self:Print('toggled: '..concat(result, ', '))
Nenue@66 454 end
Nenue@66 455 end