Code: Select all
[LeftText]
Meter=String
StringAlign=Left
X=0
[CenteredText]
Meter=String
StringAlign=Center
X=100
[RightText]
Meter=String
StringAlign=Right
X=200
1) One Meter
Let's say you want your skin's width to be adaptive - to get wider when the text is longer, and thinner when the text is shorter. This makes it hard to align the text center or right, because the width is unknown, and therefore so is the X.
The solution? Use Lua to get the width of the text meter, then just move the meter by the same amount:
Code: Select all
function RightStringMeter()
local StringMeter = SKIN:GetMeter('RightText')
local W = StringMeter:GetW()
local Y = StringMeter:GetY()
local X = W
SKIN:Bang('!MoveMeter', X, Y, 'CenterText')
end
X = W/2
You may also not want the text area to be pressed right up against the left side of the skin.
If you want to add a little margin to the skin - say, 10 pixels - just add it to the new X.
X = W + 10
Finally, in the interest of performance, you may want to save the meter's width as a variable, and only update the X when the width has changed:
Code: Select all
function Initialize()
SavedW = nil
end
function RightStringMeter()
local StringMeter = SKIN:GetMeter('RightText')
local W = StringMeter:GetW()
local Y = StringMeter:GetY()
if W ~= SavedW then
local X = W
SKIN:Bang('!MoveMeter', X, Y, 'CenterText')
SavedW = W
end
end
2) Multiple Meters
Let's say you have a whole column of string meters, all supposed to be aligned to the right:
Code: Select all
[RightText1]
Meter=String
StringAlign=Right
Text=This string is pretty long!
[RightText2]
Meter=String
StringAlign=Right
Text=This one is shorter!
[RightText3]
Meter=String
StringAlign=Right
Text=But this one is even longer than the other two!
Code: Select all
function Initialize()
SavedMaxW = nil
end
function RightStringMeters()
StringMeters = {
SKIN:GetMeter ('RightText1'),
SKIN:GetMeter ('RightText2'),
SKIN:GetMeter ('RightText3')
}
local W = {}
local Y = {}
for i,v in ipairs(StringMeters) do
W[i] = v:GetW()
Y[i] = v:GetY()
end
local MaxW = 0
for i,v in ipairs(W) do
if v > MaxWidth then MaxWidth = v end
end
if MaxW ~= SavedMaxW then
SKIN:Bang('!MoveMeter', MaxW, Y[1], 'RightText1')
SKIN:Bang('!MoveMeter', MaxW, Y[2], 'RightText2')
SKIN:Bang('!MoveMeter', MaxW, Y[3], 'RightText3')
SavedMaxW = MaxW
end
end
Code: Select all
for i,v in ipairs(W) do
if v > MaxWidth then MaxWidth = v end
end
3) Dynamic Variable
If you have other meters or measures that depend on knowing the width of the text meters, you may find it easier to just set the number as a variable in the skin. The only downside is that it will require dynamic variables to use, which may slow things down if you have a very large skin.
Code: Select all
function Initialize()
SavedMaxW = nil
end
function RightStringMeters()
local StringMeters = {
SKIN:GetMeter ('RightText1'),
SKIN:GetMeter ('RightText2'),
SKIN:GetMeter ('RightText3')
}
local W = {}
local Y = {}
for i,v in ipairs(StringMeters) do
W[i] = v:GetW()
Y[i] = v:GetY()
end
local MaxW = 0
for i,v in ipairs(W) do
if v > MaxWidth then MaxWidth = v end
end
if MaxW ~= SavedMaxW then
SKIN:Bang('!SetVariable', 'MaxW', MaxW)
SavedMaxW = MaxW
end
end
Code: Select all
[Rainmeter]
DynamicWindowSize=1