Thread: Model tilting
View Single Post
10-28-13, 10:54 AM   #37
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
I put every fuction together now.

Alt + LeftClick - Move frame
LeftClick + horizontal/vertical mouse movement - Tilt X, Tilt Z
RightClick + horizontal/vertical mouse movement - Move Y, Move Z
Alt + RightClick + vertical mouse movement - Move X
MiddleClick + horizontal mouse movement - Rotate
MouseWheel - Zoom camera (0.1)
Ctrl + MouseWheel - Zoom camera (0.5)
Alt + MouseWheel - Zoom camera (1)

Alt + MiddleMouse - Reset to default

Only Y Tilting is missing atm.

Lua Code:
  1. local format, math, pi, halfpi = format, math, math.pi, math.pi / 2
  2.  
  3. local frame = CreateFrame("Frame", nil, UIParent)
  4. frame:SetPoint("Center")
  5. frame:SetSize(512, 512)
  6. frame:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", insets = {left = 0, top = 0, right = 0, bottom = 0}, tile = true,})
  7. frame:SetBackdropColor(0, 0.4, 0, 1)
  8. frame:SetMovable(false)
  9.  
  10. local model = CreateFrame("PlayerModel", nil, frame)
  11. model:SetModel("Creature\\Alexstrasza\\LadyAlexstrasa.m2")
  12. model:SetAllPoints(frame)
  13. model:SetSize(512, 512)
  14. model:SetMovable(false)
  15. model:EnableMouse(true)
  16. model:EnableMouseWheel(true)
  17.  
  18. local function OnDragStart(self)
  19.     self:SetMovable(true)
  20.     self:StartMoving()
  21. end
  22.  
  23. local function OnDragStop(self)
  24.     self:StopMovingOrSizing()
  25.     self:SetMovable(false)
  26.     local x = math.floor(self:GetLeft() + (self:GetWidth() - UIParent:GetWidth()) / 2 + 0.5)
  27.     local y = math.floor(self:GetTop() - (self:GetHeight() + UIParent:GetHeight()) / 2 + 0.5)
  28.     self:ClearAllPoints()
  29.     self:SetPoint("Center", x, y)
  30. end
  31.  
  32. local function OnUpdate(self, elapsed)
  33.     local x, y = GetCursorPosition()
  34.     local pitch = model.pitch + (y - self.y) * pi / 256
  35.     local limit = false
  36.     if pitch > halfpi - 0.05 or pitch < - halfpi + 0.05 then
  37.         limit = true
  38.     end
  39.     if limit then
  40.         local rotation = format("%.0f", math.abs(math.deg(((x - self.x) / 64 + self:GetFacing())) % 360))
  41.         if rotation ~= format("%.0f", math.abs(math.deg(self:GetFacing()) % 360)) then
  42.             self:SetRotation(math.rad(rotation))
  43.         end
  44.     else
  45.         local yaw = self.yaw + (x - self.x) * pi / 256
  46.         self:SetOrientation(self.distance, yaw, pitch)
  47.     end
  48.     self.x, self.y = x, y
  49. end
  50.  
  51. local function RightButtonOnUpdate(self, elapsed)
  52.     local x, y = GetCursorPosition()
  53.     local px, py, pz = self:GetPosition()
  54.     if IsAltKeyDown() then
  55.         local mx = format("%.2f", (px + (y - self.y) / 64))
  56.         if format("%.2f", px) ~= mx then
  57.             self:SetPosition(mx, py, pz)
  58.         end
  59.     else
  60.         local my = format("%.2f", (py + (x - self.x) / 64))
  61.         local mz = format("%.2f", (pz + (y - self.y) / 64))
  62.         if format("%.2f", py) ~= my or format("%.2f", pz) ~= mz then
  63.             self:SetPosition(px, my, mz)
  64.         end
  65.     end
  66.     self.x, self.y = x, y
  67. end
  68.  
  69. local function MiddleButtonOnUpdate(self, elapsed)
  70.     local x, y = GetCursorPosition()
  71.     local rotation = format("%.0f", math.abs(math.deg(((x - self.x) / 84 + self:GetFacing())) % 360))
  72.     if rotation ~= format("%.0f", math.abs(math.deg(self:GetFacing()) % 360)) then
  73.         self:SetRotation(math.rad(rotation))
  74.     end
  75.     self.x, self.y = x, y
  76. end
  77.  
  78. local function OnMouseDown(self, button)
  79.     if button == "LeftButton" then
  80.         if IsAltKeyDown() then
  81.             OnDragStart(frame)
  82.         else
  83.             self.x, self.y = GetCursorPosition()
  84.             self:SetScript("OnUpdate", OnUpdate)
  85.         end
  86.     elseif button == "RightButton" then
  87.         self.x, self.y = GetCursorPosition()
  88.         self:SetScript("OnUpdate", RightButtonOnUpdate)
  89.     elseif button == "MiddleButton" then
  90.         if IsAltKeyDown() then
  91.             self:Reset()
  92.         else
  93.             self.x, self.x = GetCursorPosition()
  94.             self:SetScript("OnUpdate", MiddleButtonOnUpdate)
  95.         end
  96.     end
  97. end
  98.  
  99. local function OnMouseUp(self, button)
  100.     OnDragStop(frame)
  101.     if button == "LeftButton" then
  102.         self:SetScript("OnUpdate", nil)
  103.     elseif button == "RightButton" then
  104.         self:SetScript("OnUpdate", nil)
  105.     elseif button == "MiddleButton" then
  106.         self:SetScript("OnUpdate", nil)
  107.     end
  108. end
  109.  
  110. local function OnMouseWheel(self, delta)
  111.     local zoom = 0.1
  112.     if IsControlKeyDown() then
  113.         zoom = 0.5
  114.     elseif IsAltKeyDown() then
  115.         zoom = 1
  116.     end
  117.     local distance = model.distance - delta * zoom
  118.     if distance > 40 then
  119.         distance = 40
  120.     elseif distance < zoom then
  121.         distance = zoom
  122.     end
  123.     self:SetOrientation(distance, model.yaw, model.pitch)
  124. end
  125.  
  126. function model:SetOrientation(distance, yaw, pitch)
  127.     if self:HasCustomCamera() then
  128.         self.distance, self.yaw, self.pitch = distance, yaw, pitch
  129.         local x = distance * math.cos(yaw) * math.cos(pitch)
  130.         local y = distance * math.sin(- yaw) * math.cos(pitch)
  131.         local z = (distance * math.sin(- pitch))
  132.         self:SetCameraPosition(x, y, z)
  133.     end
  134. end
  135.  
  136. local x, y, z, px, py, pz, rot
  137.  
  138. function model:Reset()
  139.     self:SetCustomCamera(1)
  140.     self:SetCameraPosition(x, y, z)
  141.     self:SetPosition(px, py, pz)
  142.     self:SetRotation(rot)
  143.     self:SetCameraTarget(0, 0, pi / 5)
  144.     self:SetOrientation(math.sqrt(x * x + y * y + z * z), - math.atan(y / x), - math.atan(z / x))
  145. end
  146.  
  147. function model:Initialize()
  148.     self:SetCustomCamera(1)
  149.     x, y, z = self:GetCameraPosition()
  150.     px, py, pz = self:GetPosition()
  151.     rot = self:GetFacing()
  152.     self:SetCameraTarget(0, 0, pi / 5)
  153.     self:SetOrientation(math.sqrt(x * x + y * y + z * z), - math.atan(y / x), - math.atan(z / x))
  154. end
  155.  
  156. model:SetScript("OnKeyUp", OnKeyUp)
  157. model:SetScript("OnDragStart", OnDragStart)
  158. model:SetScript("OnDragStop", OnDragStop)
  159. model:SetScript("OnMouseDown", OnMouseDown)
  160. model:SetScript("OnMouseUp", OnMouseUp)
  161. model:SetScript("OnMouseWheel", OnMouseWheel)
  162.  
  163. model:Initialize()

I think it's quite lightweigth but optimizations can always be done.

Last edited by Resike : 10-28-13 at 12:49 PM.
  Reply With Quote