Originally Posted by zork
lua Code:
function RotateTexture(self, degrees) local angle = math.rad(degrees) local cos, sin = math.cos(angle), math.sin(angle) self:SetTexCoord((sin - cos), -(cos + sin), -cos, -sin, sin, -cos, 0, 0) 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:
--- Applies an affine transformation to this texture.
local function ApplyTransform( self, A, B, C, D, E, F )
local Det = A * E - B * D;
if ( Det == 0 ) then
return self:Hide(); -- Scaled infinitely small
end
local AF, BF, CD, CE = A * F, B * F, C * D, C * E;
local ULx, ULy = ( BF - CE ) / Det, ( CD - AF ) / Det;
local LLx, LLy = ( BF - CE - B ) / Det, ( CD - AF + A ) / Det;
local URx, URy = ( BF - CE + E ) / Det, ( CD - AF - D ) / Det;
local LRx, LRy = ( BF - CE + E - B ) / Det, ( CD - AF - D + A ) / Det;
-- Bounds to prevent "TexCoord out of range" errors
if ( ULx < -1e4 ) then ULx = -1e4; elseif ( ULx > 1e4 ) then ULx = 1e4; end
if ( ULy < -1e4 ) then ULy = -1e4; elseif ( ULy > 1e4 ) then ULy = 1e4; end
if ( LLx < -1e4 ) then LLx = -1e4; elseif ( LLx > 1e4 ) then LLx = 1e4; end
if ( LLy < -1e4 ) then LLy = -1e4; elseif ( LLy > 1e4 ) then LLy = 1e4; end
if ( URx < -1e4 ) then URx = -1e4; elseif ( URx > 1e4 ) then URx = 1e4; end
if ( URy < -1e4 ) then URy = -1e4; elseif ( URy > 1e4 ) then URy = 1e4; end
if ( LRx < -1e4 ) then LRx = -1e4; elseif ( LRx > 1e4 ) then LRx = 1e4; end
if ( LRy < -1e4 ) then LRy = -1e4; elseif ( LRy > 1e4 ) then LRy = 1e4; end
self:Show();
return self:SetTexCoord( ULx, ULy, LLx, LLy, URx, URy, LRx, LRy );
end
--- Rotates this texture around its center by Angle.
local function RotateTexture( self, Angle )
local Cos, Sin = math.cos( Angle ), math.sin( Angle );
ApplyTransform( self,
Cos, Sin, ( Cos + Sin ) * -0.5 + 0.5,
-Sin, Cos, ( Cos - Sin ) * -0.5 + 0.5
);
end
--- Slowly rotates this frame's texture.
local function OnUpdate ( self, Elapsed )
self.Elapsed = self.Elapsed + Elapsed;
return RotateTexture( self.Texture, math.pi * self.Elapsed );
end
local Frame = CreateFrame( "Frame" );
Frame.Texture = Frame:CreateTexture();
Frame.Elapsed = 0;
Frame:SetSize( 128, 128 );
Frame:SetPoint( "CENTER" );
Frame.Texture:SetAllPoints();
Frame.Texture:SetTexture( [[Interface\MacroFrame\MacroFrame-Icon]] );
Frame:SetScript( "OnUpdate", OnUpdate );
Only the animation system's rotations are bugged; Lua's math library and SetTexCoord seem fine.