From be5244fd8d04f95bc54fc1c86b01904f0d909661 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sat, 15 Oct 2016 00:07:37 +0200 Subject: [PATCH] refactoring --- npcscan.lua | 397 ++++++++++++++++++++++++++-------------------------- npcscan.toc | 2 +- npcscan.xml | 36 ----- 3 files changed, 197 insertions(+), 238 deletions(-) delete mode 100644 npcscan.xml diff --git a/npcscan.lua b/npcscan.lua index d1f2e4c..9332327 100644 --- a/npcscan.lua +++ b/npcscan.lua @@ -1,15 +1,14 @@ -local version = '1.0' -local author = 'shirsig' +npcscan = CreateFrame'Frame' +npcscan:SetScript('OnUpdate', function() npcscan.UPDATE() end) +npcscan:SetScript('OnEvent', function() npcscan.LOAD() end) +npcscan:RegisterEvent'VARIABLES_LOADED' -local BROWN = { .7, .15, .05 } -local YELLOW = { 1, 1, .15 } +local BROWN = {.7, .15, .05} +local YELLOW = {1, 1, .15} local CHECK_INTERVAL = .1 npcscan_targets = {} -npcscan = {} - - function npcscan.without_errors(f) local orig = UIErrorsFrame.AddMessage UIErrorsFrame.AddMessage = function() end @@ -33,13 +32,7 @@ end function npcscan.check_for_targets() for name, _ in npcscan_targets do - -- local original_target = UnitName('target') if npcscan.target(name) then - -- if UnitPlayerControlled('target') then - -- if original_target then - -- TargetByName(original_target, true) - -- end - -- else npcscan.toggle_target(name) npcscan.play_sound() if npcscan.flash.animation:playing() then @@ -49,209 +42,211 @@ function npcscan.check_for_targets() npcscan.flash.animation:play() end npcscan.button:set_target() - -- end end end end function npcscan.target(name) TargetByName(name, true) - return UnitName('target') and strupper(UnitName('target')) == name + local target = UnitName'target' + return target and strupper(target) == name end -function npcscan.on_event() - if event == 'VARIABLES_LOADED' then +function npcscan.LOAD() + do + local flash = CreateFrame'Frame' + npcscan.flash = flash + flash:Show() + flash:SetAllPoints() + flash:SetAlpha(0) + flash:SetFrameStrata'FULLSCREEN_DIALOG' + + local texture = flash:CreateTexture() + texture:SetBlendMode'ADD' + texture:SetAllPoints() + texture:SetTexture[[Interface\FullScreenTextures\LowHealth]] + + function flash:reset() + self:SetAlpha(0) + end + + local screen_flash_in = npcscan.alpha_animation(flash, 1, .5) + local screen_flash_out = npcscan.alpha_animation(flash, -1, .5) + local screen_flash_delay = npcscan.delay(.5) + + flash.animation = (screen_flash_in..screen_flash_delay..screen_flash_out) * 3 + end + + local button = CreateFrame('Button', 'npcscan_button', UIParent) + button:Hide() + npcscan.button = button + button:SetPoint('BOTTOM', UIParent, 0, 128) + button:SetWidth(150) + button:SetHeight(42) + button:SetScale(1.25) + button:SetFrameStrata'FULLSCREEN_DIALOG' + button:SetNormalTexture[[Interface\AddOns\npcscan\UI-Achievement-Parchment-Horizontal]] + button:SetBackdrop{ + tile = true, + edgeSize = 16, + edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], + } + button:SetBackdropBorderColor(unpack(BROWN)) + button:SetScript('OnEnter', function() + this:SetBackdropBorderColor(unpack(YELLOW)) + end) + button:SetScript('OnLeave', function() + this:SetBackdropBorderColor(unpack(BROWN)) + end) + button:SetScript('OnClick', function() + TargetByName(this:GetText(), true) + end) + function button:set_target() + self:SetText(UnitName'target') + + self.model:reset() + self.model:SetUnit'target' + + self:Show() + self.glow.animation:stop() + self.shine.animation:stop() + self.glow:reset() + self.shine:reset() + self.glow.animation:play() + self.shine.animation:play() + end + + do + local background = button:GetNormalTexture() + background:SetDrawLayer'BACKGROUND' + background:ClearAllPoints() + background:SetPoint('BOTTOMLEFT', 3, 3) + background:SetPoint('TOPRIGHT', -3, -3) + background:SetTexCoord(0, 1, 0, .25) + end + + do + local title_background = button:CreateTexture(nil, 'BORDER') + title_background:SetTexture[[Interface\AddOns\npcscan\UI-Achievement-Title]] + title_background:SetPoint('TOPRIGHT', -5, -5) + title_background:SetPoint('LEFT', 5, 0) + title_background:SetHeight(18) + title_background:SetTexCoord(0, .9765625, 0, .3125) + title_background:SetAlpha(.8) + + local title = button:CreateFontString(nil, 'OVERLAY') + title:SetFont([[Fonts\FRIZQT__.TTF]], 14) + title:SetShadowOffset(1, -1) + title:SetPoint('TOPLEFT', title_background, 0, 0) + title:SetPoint('RIGHT', title_background) + button:SetFontString(title) + + local subtitle = button:CreateFontString(nil, 'OVERLAY') + subtitle:SetFont([[Fonts\FRIZQT__.TTF]], 9) + subtitle:SetTextColor(0, 0, 0) + subtitle:SetPoint('TOPLEFT', title, 'BOTTOMLEFT', 0, -4) + subtitle:SetPoint('RIGHT', title ) + subtitle:SetText'NPC Found!' + end + + do + local model = CreateFrame('PlayerModel', nil, button) + button.model = model + model:SetPoint('BOTTOMLEFT', button, 'TOPLEFT', 0, -4) + model:SetPoint('RIGHT', 0, 0) + model:SetHeight(button:GetWidth() * .6) + do - local flash = CreateFrame('Frame') - npcscan.flash = flash - flash:Show() - flash:SetAllPoints() - flash:SetAlpha(0) - flash:SetFrameStrata('FULLSCREEN_DIALOG') - - local texture = flash:CreateTexture() - texture:SetBlendMode('ADD') - texture:SetAllPoints() - texture:SetTexture([[Interface\FullScreenTextures\LowHealth]]) - - function flash:reset() - self:SetAlpha(0) + local last_update, delay + function model:on_update() + this:SetFacing(this:GetFacing() + (GetTime() - last_update) * math.pi / 4) + last_update = GetTime() end - local screen_flash_in = npcscan.alpha_animation(flash, 1, .5) - local screen_flash_out = npcscan.alpha_animation(flash, -1, .5) - local screen_flash_delay = npcscan.delay(.5) - - flash.animation = (screen_flash_in..screen_flash_delay..screen_flash_out) * 3 - end - - local button = CreateFrame('Button', 'npcscan_button', UIParent) - button:Hide() - npcscan.button = button - button:SetPoint('BOTTOM', UIParent, 0, 128) - button:SetWidth(150) - button:SetHeight(42) - button:SetScale(1.25) - button:SetFrameStrata('FULLSCREEN_DIALOG') - button:SetNormalTexture([[Interface\AddOns\npcscan\UI-Achievement-Parchment-Horizontal]]) - button:SetBackdrop{ - tile = true, - edgeSize = 16, - edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], - } - button:SetBackdropBorderColor(unpack(BROWN)) - button:SetScript('OnEnter', function() - this:SetBackdropBorderColor(unpack(YELLOW)) - end) - button:SetScript('OnLeave', function() - this:SetBackdropBorderColor(unpack(BROWN)) - end) - button:SetScript('OnClick', function() - TargetByName(this:GetText(), true) - end) - function button:set_target() - self:SetText(UnitName('target')) - - self.model:reset() - self.model:SetUnit('target') - - self:Show() - self.glow.animation:stop() - self.shine.animation:stop() - self.glow:reset() - self.shine:reset() - self.glow.animation:play() - self.shine.animation:play() - end - - do - local background = button:GetNormalTexture() - background:SetDrawLayer('BACKGROUND') - background:ClearAllPoints() - background:SetPoint('BOTTOMLEFT', 3, 3) - background:SetPoint('TOPRIGHT', -3, -3) - background:SetTexCoord(0, 1, 0, 0.25) - end - - do - local title_background = button:CreateTexture(nil, 'BORDER') - title_background:SetTexture([[Interface\AddOns\npcscan\UI-Achievement-Title]]) - title_background:SetPoint('TOPRIGHT', -5, -5) - title_background:SetPoint('LEFT', 5, 0) - title_background:SetHeight(18) - title_background:SetTexCoord(0, .9765625, 0, .3125) - title_background:SetAlpha(.8) - - local title = button:CreateFontString(nil, 'OVERLAY', 'GameFontHighlightMedium') - title:SetPoint('TOPLEFT', title_background, 0, 0) - title:SetPoint('RIGHT', title_background) - button:SetFontString(title) - - local subtitle = button:CreateFontString(nil, 'OVERLAY', 'GameFontBlackTiny') - subtitle:SetPoint('TOPLEFT', title, 'BOTTOMLEFT', 0, -4) - subtitle:SetPoint('RIGHT', title ) - subtitle:SetText('NPC Found!') - end - - do - local model = CreateFrame('PlayerModel', nil, button) - button.model = model - model:SetPoint('BOTTOMLEFT', button, 'TOPLEFT', 0, -4) - model:SetPoint('RIGHT', 0, 0) - model:SetHeight(button:GetWidth() * .6) - - do - local last_update, delay - function model:on_update() - this:SetFacing(this:GetFacing() + (GetTime() - last_update) * math.pi / 4) - last_update = GetTime() + function model:on_update_model() + if delay > 0 then + delay = delay - 1 + return end - function model:on_update_model() - if delay > 0 then - delay = delay - 1 - return - end - - this:SetScript('OnUpdateModel', nil) - this:SetScript('OnUpdate', this.on_update) - this:SetModelScale(.75) - this:SetAlpha(1) - last_update = GetTime() - end - - function model:reset() - self:SetAlpha(0) - self:SetFacing(0) - self:SetModelScale(1) - self:ClearModel() - self:SetScript('OnUpdate', nil) - self:SetScript("OnUpdateModel", self.on_update_model) - delay = 10 -- to prevent scaling bugs - end + this:SetScript('OnUpdateModel', nil) + this:SetScript('OnUpdate', this.on_update) + this:SetModelScale(.75) + this:SetAlpha(1) + last_update = GetTime() end - end - - do - local close = CreateFrame('Button', nil, button, 'UIPanelCloseButton') - close:SetPoint('TOPRIGHT', 0, 0) - close:SetWidth(32) - close:SetHeight(32) - close:SetScale(.8) - close:SetHitRectInsets(8, 8, 8, 8) - end - - do - local glow = button.model:CreateTexture(nil, 'OVERLAY') - button.glow = glow - glow:SetPoint('CENTER', button, 'CENTER') - glow:SetWidth(400 / 300 * button:GetWidth()) - glow:SetHeight(171 / 70 * button:GetHeight()) - glow:SetTexture([[Interface\AddOns\npcscan\UI-Achievement-Alert-Glow]]) - glow:SetBlendMode('ADD') - glow:SetTexCoord(0, .78125, 0, .66796875) - glow:SetAlpha(0) - function glow:reset() + function model:reset() self:SetAlpha(0) + self:SetFacing(0) + self:SetModelScale(1) + self:ClearModel() + self:SetScript('OnUpdate', nil) + self:SetScript("OnUpdateModel", self.on_update_model) + delay = 10 -- to prevent scaling bugs end - - local glow_in = npcscan.alpha_animation(glow, 1, .2) - local glow_out = npcscan.alpha_animation(glow, -1, .5) - - glow.animation = glow_in..glow_out end + end + + do + local close = CreateFrame('Button', nil, button, 'UIPanelCloseButton') + close:SetPoint('TOPRIGHT', 0, 0) + close:SetWidth(32) + close:SetHeight(32) + close:SetScale(.8) + close:SetHitRectInsets(8, 8, 8, 8) + end + + do + local glow = button.model:CreateTexture(nil, 'OVERLAY') + button.glow = glow + glow:SetPoint('CENTER', button, 'CENTER') + glow:SetWidth(400 / 300 * button:GetWidth()) + glow:SetHeight(171 / 70 * button:GetHeight()) + glow:SetTexture[[Interface\AddOns\npcscan\UI-Achievement-Alert-Glow]] + glow:SetBlendMode'ADD' + glow:SetTexCoord(0, .78125, 0, .66796875) + glow:SetAlpha(0) + + function glow:reset() + self:SetAlpha(0) + end + + local glow_in = npcscan.alpha_animation(glow, 1, .2) + local glow_out = npcscan.alpha_animation(glow, -1, .5) + + glow.animation = glow_in .. glow_out + end - do - local shine = button:CreateTexture(nil, 'ARTWORK') - button.shine = shine - shine:SetPoint('TOPLEFT', button, 0, 8) - shine:SetWidth(67 / 300 * button:GetWidth()) - shine:SetHeight(1.28 * button:GetHeight()) - shine:SetTexture([[Interface\AddOns\npcscan\UI-Achievement-Alert-Glow]]) - shine:SetBlendMode('ADD') - shine:SetTexCoord(.78125, .912109375, 0, .28125) - shine:SetAlpha(0) - - function shine:reset() - self:SetAlpha(0) - self:SetPoint('TOPLEFT', button, 0, 8) - end - - local shine_delay1 = npcscan.delay(.3) - local shine_in = npcscan.alpha_animation(shine, 1, 0) - local shine_delay2 = npcscan.delay(.2) - local shine_move = npcscan.translation_animation(shine, button:GetWidth() - shine:GetWidth() + 8, 0, .4) - local shine_out = npcscan.alpha_animation(shine, -1, .2) - - shine.animation = (shine_delay1..shine_in)..(shine_move + (shine_delay2..shine_out)) + do + local shine = button:CreateTexture(nil, 'ARTWORK') + button.shine = shine + shine:SetPoint('TOPLEFT', button, 0, 8) + shine:SetWidth(67 / 300 * button:GetWidth()) + shine:SetHeight(1.28 * button:GetHeight()) + shine:SetTexture[[Interface\AddOns\npcscan\UI-Achievement-Alert-Glow]] + shine:SetBlendMode'ADD' + shine:SetTexCoord(.78125, .912109375, 0, .28125) + shine:SetAlpha(0) + + function shine:reset() + self:SetAlpha(0) + self:SetPoint('TOPLEFT', button, 0, 8) end + + local shine_delay1 = npcscan.delay(.3) + local shine_in = npcscan.alpha_animation(shine, 1, 0) + local shine_delay2 = npcscan.delay(.2) + local shine_move = npcscan.translation_animation(shine, button:GetWidth() - shine:GetWidth() + 8, 0, .4) + local shine_out = npcscan.alpha_animation(shine, -1, .2) + + shine.animation = (shine_delay1 .. shine_in) .. (shine_move + (shine_delay2 .. shine_out)) end end do npcscan.last_check = GetTime() - function npcscan.on_update() + function npcscan.UPDATE() if GetTime() - npcscan.last_check >= CHECK_INTERVAL then npcscan.last_check = GetTime() npcscan.without_errors(npcscan.check_for_targets) @@ -261,7 +256,7 @@ end function npcscan.log(msg) if DEFAULT_CHAT_FRAME then - DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..'[npcscan] '..msg) + DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE .. '[npcscan] ' .. msg) end end @@ -343,15 +338,15 @@ end do local animation_metamethods = {} - local mul_mt = { __index = {} } - local add_mt = { __index = {} } - local concat_mt = { __index = {} } - local animation_mt = { __index = {} } + local mul_mt = { __index={} } + local add_mt = { __index={} } + local concat_mt = { __index={} } + local animation_mt = { __index={} } function animation_metamethods.__mul(animation, repetitions) local self = { - _frame = CreateFrame('Frame'), + _frame = CreateFrame'Frame', _repetitions_left = 0, _repetitions = repetitions, _animation = animation, @@ -373,7 +368,7 @@ do return self end function animation_metamethods.__concat(lhs, rhs) - local self = { _frame = CreateFrame('Frame') } + local self = { _frame=CreateFrame'Frame' } setmetatable(self, concat_mt) self._animations = {lhs, rhs} @@ -386,7 +381,7 @@ do end - for _, mt in ipairs({ mul_mt, add_mt, concat_mt, animation_mt }) do + for _, mt in ipairs({mul_mt, add_mt, concat_mt, animation_mt}) do for key, metamethod in pairs(animation_metamethods) do mt[key] = metamethod end @@ -466,7 +461,7 @@ do function animation_mt.__index:play() self._t0 = GetTime() - self._original_values = { self._getter() } + self._original_values = {self._getter()} end function animation_mt.__index:stop() self._t0 = nil @@ -497,7 +492,7 @@ do function npcscan.animation(changes, duration, getter, callback) local self = { - _frame = CreateFrame('Frame'), + _frame = CreateFrame'Frame', _changes = changes, _duration = duration, _getter = getter, diff --git a/npcscan.toc b/npcscan.toc index 6c76bdd..915c836 100644 --- a/npcscan.toc +++ b/npcscan.toc @@ -1,4 +1,4 @@ ## Interface: 11200 ## Title: npcscan ## SavedVariables: npcscan_targets -npcscan.xml \ No newline at end of file +npcscan.lua \ No newline at end of file diff --git a/npcscan.xml b/npcscan.xml deleted file mode 100644 index 7e660a6..0000000 --- a/npcscan.xml +++ /dev/null @@ -1,36 +0,0 @@ - -