View Single Post
10-10-11, 02:21 AM   #4
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Originally Posted by zork View Post
lua Code:
  1. function RotateTexture(self, degrees)
  2.     local angle = math.rad(degrees)
  3.     local cos, sin = math.cos(angle), math.sin(angle)
  4.     self:SetTexCoord((sin - cos), -(cos + sin), -cos, -sin, sin, -cos, 0, 0)
  5. end

Maybe some of the math functions are bugged.
This function worked fine for me. It rotates the image around the bottom-right corner of the texture region instead of its center though. Here's a version that rotates properly around the center of the texture:
lua Code:
  1. --- Applies an affine transformation to this texture.
  2. local function ApplyTransform( self, A, B, C, D, E, F )
  3.   local Det = A * E - B * D;
  4.   if ( Det == 0 ) then
  5.     return self:Hide(); -- Scaled infinitely small
  6.   end
  7.   local AF, BF, CD, CE = A * F, B * F, C * D, C * E;
  8.  
  9.   local ULx, ULy = ( BF - CE ) / Det, ( CD - AF ) / Det;
  10.   local LLx, LLy = ( BF - CE - B ) / Det, ( CD - AF + A ) / Det;
  11.   local URx, URy = ( BF - CE + E ) / Det, ( CD - AF - D ) / Det;
  12.   local LRx, LRy = ( BF - CE + E - B ) / Det, ( CD - AF - D + A ) / Det;
  13.  
  14.   -- Bounds to prevent "TexCoord out of range" errors
  15.   if ( ULx < -1e4 ) then ULx = -1e4; elseif ( ULx > 1e4 ) then ULx = 1e4; end
  16.   if ( ULy < -1e4 ) then ULy = -1e4; elseif ( ULy > 1e4 ) then ULy = 1e4; end
  17.   if ( LLx < -1e4 ) then LLx = -1e4; elseif ( LLx > 1e4 ) then LLx = 1e4; end
  18.   if ( LLy < -1e4 ) then LLy = -1e4; elseif ( LLy > 1e4 ) then LLy = 1e4; end
  19.   if ( URx < -1e4 ) then URx = -1e4; elseif ( URx > 1e4 ) then URx = 1e4; end
  20.   if ( URy < -1e4 ) then URy = -1e4; elseif ( URy > 1e4 ) then URy = 1e4; end
  21.   if ( LRx < -1e4 ) then LRx = -1e4; elseif ( LRx > 1e4 ) then LRx = 1e4; end
  22.   if ( LRy < -1e4 ) then LRy = -1e4; elseif ( LRy > 1e4 ) then LRy = 1e4; end
  23.  
  24.   self:Show();
  25.   return self:SetTexCoord( ULx, ULy, LLx, LLy, URx, URy, LRx, LRy );
  26. end
  27. --- Rotates this texture around its center by Angle.
  28. local function RotateTexture( self, Angle )
  29.   local Cos, Sin = math.cos( Angle ), math.sin( Angle );
  30.   ApplyTransform( self,
  31.     Cos, Sin, ( Cos + Sin ) * -0.5 + 0.5,
  32.     -Sin, Cos, ( Cos - Sin ) * -0.5 + 0.5
  33.   );
  34. end
  35. --- Slowly rotates this frame's texture.
  36. local function OnUpdate ( self, Elapsed )
  37.   self.Elapsed = self.Elapsed + Elapsed;
  38.   return RotateTexture( self.Texture, math.pi * self.Elapsed );
  39. end
  40.  
  41.  
  42. local Frame = CreateFrame( "Frame" );
  43. Frame.Texture = Frame:CreateTexture();
  44. Frame.Elapsed = 0;
  45.  
  46. Frame:SetSize( 128, 128 );
  47. Frame:SetPoint( "CENTER" );
  48. Frame.Texture:SetAllPoints();
  49. Frame.Texture:SetTexture( [[Interface\MacroFrame\MacroFrame-Icon]] );
  50. Frame:SetScript( "OnUpdate", OnUpdate );

Only the animation system's rotations are bugged; Lua's math library and SetTexCoord seem fine.
  Reply With Quote