local format, math, pi, halfpi = format, math, math.pi, math.pi / 2
local frame = CreateFrame("Frame", nil, UIParent)
frame:SetPoint("Center")
frame:SetSize(512, 512)
frame:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", insets = {left = 0, top = 0, right = 0, bottom = 0}, tile = true,})
frame:SetBackdropColor(0, 0.4, 0, 1)
frame:SetMovable(false)
local model = CreateFrame("PlayerModel", nil, frame)
model:SetModel("Creature\\Alexstrasza\\LadyAlexstrasa.m2")
model:SetAllPoints(frame)
model:SetSize(512, 512)
model:SetMovable(false)
model:EnableMouse(true)
model:EnableMouseWheel(true)
local function OnDragStart(self)
self:SetMovable(true)
self:StartMoving()
end
local function OnDragStop(self)
self:StopMovingOrSizing()
self:SetMovable(false)
local x = math.floor(self:GetLeft() + (self:GetWidth() - UIParent:GetWidth()) / 2 + 0.5)
local y = math.floor(self:GetTop() - (self:GetHeight() + UIParent:GetHeight()) / 2 + 0.5)
self:ClearAllPoints()
self:SetPoint("Center", x, y)
end
local function OnUpdate(self, elapsed)
local x, y = GetCursorPosition()
local pitch = model.pitch + (y - self.y) * pi / 256
local limit = false
if pitch > halfpi - 0.05 or pitch < - halfpi + 0.05 then
limit = true
end
if limit then
local rotation = format("%.0f", math.abs(math.deg(((x - self.x) / 64 + self:GetFacing())) % 360))
if rotation ~= format("%.0f", math.abs(math.deg(self:GetFacing()) % 360)) then
self:SetRotation(math.rad(rotation))
end
else
local yaw = self.yaw + (x - self.x) * pi / 256
self:SetOrientation(self.distance, yaw, pitch)
end
self.x, self.y = x, y
end
local function RightButtonOnUpdate(self, elapsed)
local x, y = GetCursorPosition()
local px, py, pz = self:GetPosition()
if IsAltKeyDown() then
local mx = format("%.2f", (px + (y - self.y) / 64))
if format("%.2f", px) ~= mx then
self:SetPosition(mx, py, pz)
end
else
local my = format("%.2f", (py + (x - self.x) / 64))
local mz = format("%.2f", (pz + (y - self.y) / 64))
if format("%.2f", py) ~= my or format("%.2f", pz) ~= mz then
self:SetPosition(px, my, mz)
end
end
self.x, self.y = x, y
end
local function MiddleButtonOnUpdate(self, elapsed)
local x, y = GetCursorPosition()
local rotation = format("%.0f", math.abs(math.deg(((x - self.x) / 84 + self:GetFacing())) % 360))
if rotation ~= format("%.0f", math.abs(math.deg(self:GetFacing()) % 360)) then
self:SetRotation(math.rad(rotation))
end
self.x, self.y = x, y
end
local function OnMouseDown(self, button)
if button == "LeftButton" then
if IsAltKeyDown() then
OnDragStart(frame)
else
self.x, self.y = GetCursorPosition()
self:SetScript("OnUpdate", OnUpdate)
end
elseif button == "RightButton" then
self.x, self.y = GetCursorPosition()
self:SetScript("OnUpdate", RightButtonOnUpdate)
elseif button == "MiddleButton" then
if IsAltKeyDown() then
self:Reset()
else
self.x, self.x = GetCursorPosition()
self:SetScript("OnUpdate", MiddleButtonOnUpdate)
end
end
end
local function OnMouseUp(self, button)
OnDragStop(frame)
if button == "LeftButton" then
self:SetScript("OnUpdate", nil)
elseif button == "RightButton" then
self:SetScript("OnUpdate", nil)
elseif button == "MiddleButton" then
self:SetScript("OnUpdate", nil)
end
end
local function OnMouseWheel(self, delta)
local zoom = 0.1
if IsControlKeyDown() then
zoom = 0.5
elseif IsAltKeyDown() then
zoom = 1
end
local distance = model.distance - delta * zoom
if distance > 40 then
distance = 40
elseif distance < zoom then
distance = zoom
end
self:SetOrientation(distance, model.yaw, model.pitch)
end
function model:SetOrientation(distance, yaw, pitch)
if self:HasCustomCamera() then
self.distance, self.yaw, self.pitch = distance, yaw, pitch
local x = distance * math.cos(yaw) * math.cos(pitch)
local y = distance * math.sin(- yaw) * math.cos(pitch)
local z = (distance * math.sin(- pitch))
self:SetCameraPosition(x, y, z)
end
end
local x, y, z, px, py, pz, rot
function model:Reset()
self:SetCustomCamera(1)
self:SetCameraPosition(x, y, z)
self:SetPosition(px, py, pz)
self:SetRotation(rot)
self:SetCameraTarget(0, 0, pi / 5)
self:SetOrientation(math.sqrt(x * x + y * y + z * z), - math.atan(y / x), - math.atan(z / x))
end
function model:Initialize()
self:SetCustomCamera(1)
x, y, z = self:GetCameraPosition()
px, py, pz = self:GetPosition()
rot = self:GetFacing()
self:SetCameraTarget(0, 0, pi / 5)
self:SetOrientation(math.sqrt(x * x + y * y + z * z), - math.atan(y / x), - math.atan(z / x))
end
model:SetScript("OnKeyUp", OnKeyUp)
model:SetScript("OnDragStart", OnDragStart)
model:SetScript("OnDragStop", OnDragStop)
model:SetScript("OnMouseDown", OnMouseDown)
model:SetScript("OnMouseUp", OnMouseUp)
model:SetScript("OnMouseWheel", OnMouseWheel)
model:Initialize()