下一页(2)

:: [原创][翻译] 头像插件PitBull4之 使用LuaTexts实现自定义文本[全文完] ::

AUTHOR CONTENT

» [url=http://bbs.ngacn.cc/read.php?tid=3589395][原创][翻译] 头像插件PitBull4之 使用LuaTexts实现自定义文本[全文完][/url]


[h]引言[/h]
作为一名WoW玩家,插件和脚本是除了RAID外,WoW能吸引我的一个重要原因。在曾经使用过的Perl、XPerl、oUF、ag_UnitFrame等头像插件里,只有oUF和PitBull给我留下了深刻印象。oUF体积小、可定制程度高、样式多样,实在是非常优秀的一款头像插件,但是要灵活地定制oUF,需要你具备一定程度的lua语言基础。相比而言,PitBull也是一款强大的头像插件,可定制程度也不差。特别是在PitBull 4推出后,继DogTagTexts后又为玩家提供了直接的lua支持。当然,PitBull占用内存也较大。

我曾经是一名C/C++程序员,lua对我也是陌生的事物。但编程的语言总是触类旁通的,所以最近我正通过Pitbull4在熟悉lua。在此,我利用午间的时间,为大家献上一篇关于PitBull4中LuaTexts的译文。希望能对大家使用PitBull、接触lua提供一点帮助。词汇翻译偏误之处,还请批评斧正。

需要说明的是,没有称手的工具Copy原文,所以原文的格式未能保留,我尽量把自己翻译的内容版式弄清楚一点吧。

[b](需要转载者,请注明原作者、译者及出处,非常感谢!)[/b]
[h][/h]


改动



[h] [align=center][b][size=120%]PitBull Unit Frame 4.0之LuaTexts[/size][/b][/align]
[/h]

原文链接:[url]http://www.wowace.com/addons/pitbull4/pages/lua-texts/[/url]

[b]以下是正文:[/b]
[collapse]LuaTexts is a text provider module for PitBull4. It's a more efficient alternative to DogTagTexts. Texts are entered using Lua rather that DogTags. You can select which events cause the text to update. Custom texts are harder to write than DogTags as it requires some knowledge of Lua.[/collapse]
LuaTexts是PitBull4内置的一个文本提供模块,较之DogTagTexts具有更高的效率。你可以直接输入lua语句而不再是简单的DogTags标签,还可以选择不同的事件来触发文本的更新。相反的,要用LuaTexts实现自定义的文本将会比使用DogTags更困难,因为前者需要你具备一定的lua语言基础。

[collapse=The Basics]

When configuring a LuaText you have the normal basics of font, size, and positioning just like any other text provider. Additionally you can configure some code (in this case a Lua script) and select some events.

The script is wrapped into a function by LuaTexts for you. So you can consider the text to be the body of a function. Your function will have the parameter unit passed to it which is the unit id for the frames unit.

Let's start with a basic text that shows the current and max HP for the frames unit.

return "%s / %s",HP(unit),MaxHP(unit)

You'll see we're returning three values. A static string and the return values of the HP and MaxHP functions.

All of these return values are passed along to the SetFormattedText() function of the FontString that the text will be displayed on. SetFormattedText does two things it takes your parameters and puts them together the same way as the Lua string.format function (or similar format functions in pretty much every language).

This means that the first parameter is a format string. You can think of this string as a template for building your other text. The two %s placeholders are replaced with the current and max HP values.

If you do not wish to display anything do not return any values and LuaTexts will set the FontString to an empty string for you.[/collapse]

[h][size=130%]基础篇[/size] [/h]
和其他的文本提供模块一样,LuaTexts也支持字体、文本大小、位置等基本元素的配置。而较之更灵活的是,LuaTexts可以让你通过输入一些代码(暂称为lua script,lua脚本)、选择不同的事件来提供更为丰富的表现。

LuaTexts会将你输入的脚本封装成一个函数。所以在某种意义上,你可以将自己输入的文本视作一个lua函数的函数体,并且这个函数只有一个传入参数—当前单位框体的id。

让我们先来看一个示例:以“当前血量/总血量”方式显示单位血量
[code=lua]
return "%s / %s",HP(unit),MaxHP(unit)[/code]
上述语句将会返回3个值:一个静态的字符串,函数HP()与MaxHP()的返回值。这3个值随后将会被传递给FontString对象的SetFormattedText()方法,以形成最终被显示在框体上的文本。SetFormattedText()方法主要完成两件事:先把你给的参数组合成一个lua字符串,然后就象其他编程语言format()函数那样,输出一个格式化后的字符串。

即,SetFormattedText()方法的第一个参数是一个相当于文本模板(泽注:暂且称为Style)的字符串,其后是若干个替代占位符的值(泽注:参见C/C++等语言中的可变参数函数)。在上面的例子里,两个“%s”占位符将先后被HP()与MaxHP()函数的返回值替代。

如果你不希望显示任何文本,那么只要在脚本里不返回任何的值,LuaTexts就会自动地返回一个空字符串作为回应。

[collapse=The Format String]

You can find more information on the possible values for the placeholders on the Wowiki entry for the format API: [url]http://www.wowwiki.com/API_format[/url]

If you want to insert the actual % character just put two of them in. For instance: "25%%" would actually display "25%".

WoW uses the pipe character "|" as an escape for special sequences that they use for formatting characters. So if you want to insert a pipe character, you'll want to insert two of them as well. For instance "||" would actually output "|".

If you want to display a single string and don't need to do any formatting. You can simply return that single string. For instance:

return Name(unit)[/collapse]

[h][size=130%]Format()及文本模板[/size] [/h]
在WoW Wiki([url]http://www.wowwiki.com/API_format[/url])上,你可以找到更多关于format()这个API可以使用的占位符的信息。

要在文本中输出百分号“%”,需要你使用“%%”这样的转义符。比如要显示“25%”,就需要你在return的串里写为“25%%”。

WoW使用了管道符号“|”作为格式化文本使用的逃逸符,以支持更丰富的文本格式。同上面的“%”一样,要在文本中输出“|”时,也需要写作“||”(译注:管道操作,在Unix和DOS里很常见,也很强大。escape symbol:逃逸符,类似上面的占位符,也是引导特定的格式串。)

如果你只需要简单地输出一个串,而无需进行任何的修饰,那直接返回这个串即可。就象下面这样:

[code=lua]return Name(unit)[/code]

[collapse=Formatting your text]

[b]Colors[/b]

You can set the color by wrapping the text with the proper escape codes. For colors you start with |cff followed by the hex RGB 2 digit color codes. You return the color back to normal by using |r. For example let's say we want our example current and max HP to show the current HP in red we would do:

return "|cffff0000%s|r / %s",HP(unit),MaxHP(unit)

A lot of Blizzard functions can provide color values to you. Note that these color functions usually provide the color in the range of 0 to 1.0 as a decimal value. The escape code however expects the color codes to be in the range of 0 to 255. You can convert those by multiplying the 0 to 1.0 values by 255. For example:

local _,status,percent = UnitDetailedThreatSituation("player",unit)

local r,g,b = GetThreatStatusColor(status)

return "|cff%02x%02x%02x%s%%",r*255,g*255,b*255,percent

The format placeholder %02x asks for the color values to be shown as 2 digit, zero padded hex values.

There are several functions provided by LuaTexts to help you get colors. These functions will always return the color codes as 0 to 255, but you will still need to format that into hex values as shown above.

[b]Icons[/b]

You can insert icons into the text using the proper escape codes: |TPATH_TO_ICON:SIZE|t

For example:

return "|TInterface/Icons/ability_warrior_decisivestrike:0|t"

Which would display the Icon for Decisive Strike. Setting the size to zero just uses the default size for the texture.

[b]Outline[/b]

You can set an outline for the entire text. Unfortunately there is no way to set this just for some text. Calling the function Outline() in your code will set the Outline().

Outline()

return Name(unit)

ThickOutline() is also available to have a thicker outline.

[b]Transparency[/b]

You can adjust the transparency of the text. Like outlines this can only be done for the entire text. The function Alpha(number) takes a number between 0 and 1.0 to specify how opaque the value should be. 0 would be completely transparent. 1.0 would be completely solid and 0.5 would be half transparent.

Alpha(0.5)

return Name(unit)[/collapse]

[h][size=130%]格式化你的文本[/size] [/h]

[b].颜色[/b]

你可以通过设置适当的逃逸符来改变文本的颜色。颜色值用RGB系统16进制的值表示(译注:同HTML中对颜色的使用),并以“|cff”这样的逃逸符作为前缀。需要恢复默认颜色时,使用“|r”这样的逃逸符。看看下面这个例子:用红色显示当前血量,用默认色显示总血量。

[code=lua] return "|cffff0000%s|r / %s",HP(unit),MaxHP(unit)[/code]

Blizzard在WoW里也提供了许多的函数,可以为你提供不同元素的颜色值。但要注意的是,这些函数返回的颜色值采取的是0到1之间的十进制小数表示的(0对应RGB 0,1对应RGB 255)。所以在LuaTexts中使用时,需要进行相应的转换。就象下面这样:

[code=lua]local _,status,percent = UnitDetailedThreatSituation("player",unit)

local r,g,b = GetThreatStatusColor(status)

return "|cff%02x%02x%02x%s%%",r*255,g*255,b*255,percent[/code]

(译注:[list]
[*] UnitDetailedThreatSituation()返回玩家对当前unit的仇恨情况,一共可返回5个值。本例中只取了前3个,并且第一个返回值用“_”抛弃了。status 表示当前的仇恨状态:3或者2都表示是Tank,1表示仇恨已经超过T但还未变成Tank,0表示仇恨未超Tank也不是Tank;percent表示当前的对目标的仇恨百分比,100表示OT了,0表示未进入目标的仇恨列表。
[*] GetThreatStatusColor()是返回status对应的系统颜色。
[*] “%02x”表示将值转换为16进制,并用0进行前填充。[/list])

除了Blizzard提供的这些函数,LuaTexts也提供有一些函数可以为你返回RGB 255分色方式的颜色值。但在输出文本时,仍需要将这RGB分色值进行上述方式的转换。

[b].图标[/b]

你可以通过这样的逃逸符在输出的文本中插入特定样式的图标:“|T图标路径:图标大小|t”。比如这样:

[code=lua]return "|TInterface/Icons/ability_warrior_decisivestrike:0|t"[/code]

其中:Interface/Icons/ability_warrior_decisivestrike是图标路径,0表示使用默认大小。

[b].轮廓线[/b]

你可以为整个文本加上轮廓线。注意,是整个文本,而不是其中的某些字符。你需要做的,就是调用一次OutLine(),就象下面这样:

[code=lua]Outline()
return Name(unit)[/code]

类似的,ThickOutLine()可以为文本添加更粗的轮廓线。

[b].透明度[/b]

和添加轮廓线一样,你可以通过调用函数Alpha()设置整个文本的透明度。Alpha()需要一个0到1之间的值作为传入参数:1是不透明,0是完全透明。如下代码将设置单元名称为半透明:

[code=lua]Alpha(0.5)
return Name(unit)[/code]

[collapse=Updating]

Most of the time you'll want your text to update when the text changes. This is done in one of 3 ways.

[b]Events[/b]

The game generates events to tell us about specific changes. These events are the best way to specify when to update your text. Below the Lua script there is an Events dropdown box which allows you to set the events to trigger an update for the text.

If an event is not available in the dropdown box already you can add it. Go under Modules, LuaTexts and add the event. The name of the event needs to be exact. There are several checkboxes to specify that only texts that point to specific units should be updated. Player and pet will only update units specifically pointing at the player or pet. All will update all texts that have requested to be updated by that event. Unit means updating texts that point to the unit passed from the event in the first argument passed by Blizzard.

[b]Timer[/b]

You can request that your text be updated at a specific time in the future. The UpdateIn(seconds) function lets you do that. The seconds can be fractional e.g. 0.5 would do the update in half a second. The text is only updated once, however as long as UpdateIn() is called in your code it will keep updating. This function is particularly useful for when you want to display a timer.

Displaying the time left before your PVP flag drops would look like this:

if IsPVPTimerRunning() then

UpdateIn(0.25)

return FormatDuration(GetPVPTimer()/1000)

end

Care should be taken not to set timers unnecessarily. You should trigger the initial display of the text with an event. Then keep it updating with UpdateIn(). Once the you no longer need the text you should stop calling UpdateIn(). The above example handles this by only calling UpdateIn() when there's something to actually display.

[b]Implicitly[/b]

Some of the functions provided by LuaTexts help you out by flagging your text to be updated based on specific changes.

Power(unit)
Will flag the text for fast updates if the unit for the text is the player or the player's pet.

AFK(unit) and AFKDuration(unit)
Flags the text for update when the unit goes afk or comes back.

DND(unit)
Flags the text for update when the unit goes dnd or comes back.

Offline(unit) and OfflineDuration(unit)
Flags the text for update when the unit goes offline or comes back.

Dead(unit) and DeadDuration(unit)
Flags the text for update when the unit dies or resurrects.

PVPDuration(unit)
Flags the text for periodic updates when the timer for the pvp flag dropping is running.

IsMouseOver()
Flags the text for update when the mouse enters or leaves he frame the text is attached to.

CastData(unit)
Flags the text for update for fast updates while the unit is casting.

These implicit update flags are not a replacement for events. They fill in holes where the game does not provide events required for properly updating the texts.[/collapse]

[h][size=130%]文本的更新[/size] [/h]

你肯定希望你设置的脚本能得到恰当的更新,这可以通过以下三种方式实现。

[b].事件 Event[/b]

WoW内置的事件驱动机制,将会通过不同的事件告诉我们发生了一些特定的事。使用事件来更新文本,是最恰当的选择。在PitBull的lua脚本输入框下方,有一个事件的下拉选框,可供你选择与这一脚本关联的特定事件。当事件发生时,会触发这个脚本的执行。(译注:类似Windows编程中的消息与消息处理函数的关系)

如果需要的事件不在该下拉列表中,你可以手动添加。打开PitBull配置窗口,选择左边目标树中的模块(Modules)—LuaTexts后,即可在右边窗口中自行添加。事件的名称一定要准确。同时要注意,还需要通过勾选复选框来设置与这一事件关联的单位。如果你勾选了玩家与宠物(Player、Pet),则事件更新的文本仅适用于玩家及其宠物。与特定事件关联的所有脚本(译注:一个事件可以触发多个脚本),都会在事件触发后被执行,以更新各自对应的文本。如果勾选了Unit,则在事件发生后,将会根据Blizzard内置事件驱动传来的第一个参数,决定要更新的单位。

[b].定时器 Timer[/b]

你可以通过给函数UpdateIn()一个以秒为单位的参数,来设置一个定时器,让你的文本在指定的时间之后得到更新。时长,可以是整数,也可以是象0.5这样的小数。一次脚本的执行,只能实现文本的一次更新。因此,定时器这种方式的适用范围很窄,通常仅用于显示倒计时。就象下面这样:显示决斗的倒计时(译注:尚未经测试)

[code=lua]if IsPVPTimerRunning() then
UpdateIn(0.25)
return FormatDuration(GetPVPTimer()/1000)
end[/code]

切记:不要设置非为必要的定时器。你应该用一个事件来初始化要显示的文本,随便用UpdateIn()实现定时更新。在你不再需要定时器时,应终止继续调用UpdateIn()。上述的例子,就使用了一个条件判断实现了正确的调用。

[b].隐式的更新[/b]

LuaTexts的部分函数可以方便你进行隐式的文本更新。它们包括:

[table]
[tr][td30]Power(unit)[/td][td]当单位是玩家或者宠物且能量、法力值改变时更新文本[/td][/tr]
[tr][td]AFK(unit) 与 AFKDuration(unit)[/td][td]单位AFK时更新文本[/td][/tr]
[tr][td]DND(unit)[/td][td]单位处于DND状态时更新文本[/td][/tr]
[tr][td]Offline(unit) 与 OfflineDuration(unit)[/td][td]单位离线时更新文本[/td][/tr]
[tr][td]Dead(unit) 与 DeadDuration(unit)[/td][td]单位死亡时更新文本[/td][/tr]
[tr][td]PVPDuration(unit)[/td][td]当PVP倒计时时更新文本[/td][/tr]
[tr][td]IsMouseOver()[/td][td]当鼠标进入或离开文本隶属的框架时更新文本[/td][/tr]
[tr][td]CastData(unit)[/td][td]当单位施法时更新文本[/td][/tr]
[/table]

这些隐式的更新并不是要取代事件,而是作为没有合适的事件选择时的一种有益补充。

[collapse=Errors]

Your Lua scripts can produce errors just like any other Lua code. The errors are trapped and should not break PitBull4. If there is an error your text will display {err} instead of the text. You can use an error addon such as BugSack or Swatter to display the actual errors.

They will be in one of two formats:

[string "PitBull4_LuaTexts:Target:Test"] line 1: attempt to call global 'retrun' (a nil value)

or

PitBull4_LuaTexts:Target:Test caused the following error: bad argument #2 to 'SetFormattedText' (string expected, got nil)

In the case of the first type this means there's some sort of syntax or run time error in your script. In the case of the second it means that argument #2 to SetFormattedText was nil when it should have been a string. Your return values from your script are the arguments to SetFormattedText. The FontString is actually argument #1 so argument #2 is the first return value and so on.

You'll note that both have this text: PitBull4_LuaTexts:Target:Test

This is there to help you identify the text causing the error. PitBull4_LuaTexts is of course the module. Target is the Layout the text is configured in. Test is the name of the text configuration.[/collapse]

[h][size=130%]错误处理[/size] [/h]

和普通的lua代码一样,你在PitBull里输入的lua脚本也会产生错误。不过这些错误并不会导致PitBull4的崩溃。一旦脚本发生错误,LuaTexts会自动用“{err}”替代输出文本。随后你可以使用BugSack、Swatter之类的插件收集错误信息,并进行调试。(译注:我更喜欢Swatter)错误信息会以如下两种格式显示:

[code=lua][string "PitBull4_LuaTexts:Target:Test"] line 1: attempt to call global 'retrun' (a nil value)[/code]
或者
[code=lua]PitBull4_LuaTexts:Target:Test caused the following error: bad argument #2 to 'SetFormattedText' (string expected, got nil)[/code]

上述第一条信息表明脚本中存在语法错误或者运行时错误。第二条信息表示传递给SetFormattedText()的第二个参数是个空值,而其本应是个字符串。而在lua代码运行内部,FontString是实质上的第一个参数,你用return返回的值是实质上的第二个参数,以此类推。

你也许会注意到上述两条信息中都有“PitBull4_LuaTexts:Target:Test”,这实际反映了发生错误的模块、框体和文本名。其中PitBull4_LuaTexts是发生错误的模块,Target是文本隶属的框体,Test是文本名。(译注:文本名,每一个文本样式都可以取个名字,在PitBull的文本里可以通过新增进行添加。)

[collapse=Environment]

The Lua code you enter and use for LuaTexts is run in a special environment. Within this environment a number of functions exist to help make it a little easier to get some commonly used text. This environment also allows you to save state between calls without cluttering the global namespace.

Variables that you use should be scoped within your script by using local in front of them e.g.

local foo
local bar = baz()

If you want to set a value within the namespace you should simply not use local. Keep in mind that any variables you set are shared with all the other scripts that run. If you really must set something within the global environment you must explicitly specify that by using _G

_G.foo = bar

You can however, access values and functions in the global environment without using _G. This allows easy access to Blizzard provided functions while still providing access to the functions in the ScriptEnv and protecting the global namespace.[/collapse]

[h][size=130%]上下文环境[/size] [/h]

你输入的脚本总是在特定的上下文环境下执行的(译注:比如namespace)。在这一语境帮助下,你能更方便地调用函数、使用变量,而不会影响全局namespace(这个的译名太多了,我一直用的命名空间)。

在变量前使用域定义符(为啥不叫关键字?)”local“可以在脚本内部声明一个局部变量,就象这样:
[code=lua]local foo
local bar = baz()[/code]

取消local限定符,你就可以访问当前namespace内的其他变量。切记:你声明的变量将在所有的脚本中共享。当你确实需要[b]设置[/b]全局环境中变量的值时,应当显式地使用”_G”限定符,就象这样:

[code=lua]_G.foo = bar[/code]

然而,”_G”限定符在[b]读取[/b]全局环境的变量、调用函数时并不是必须的。这样,你可以更方便地使用Blizzard提供的和ScriptEnv环境里的函数,并最大限度地保护全局namespace。

[h]附:译者的尝试[/h]

这是自己修改的头像:

[img]./mon_201008/20/200_4c6e4eafa5b31.jpg[/img]

然后是制作了OT时血量变红色的脚本,过程如下:

1. 新建一个文本名:Threat_HP,然后在正文输入了这样的脚本:
[code=lua]local _,_,threatpct,_,_ = UnitDetailedThreatSituation("player", "target");
if threatpct == 100 then
return "|cffe60012%s|r/%s", HP(unit),MaxHP(unit)
else
return "%s/%s", HP(unit), MaxHP(unit)
end[/code]

2. 选择了Threat Situation Updated 与 Health 事件

3. 将这个Threat_HP文本应用到血量条上,取代默认的“Lua:生命值”文本

[b]效果[/b]:OT后,当前血量会变红色。脱离战斗或者仇恨降低后会恢复成白色。
[h][/h]

附件

改动


辅助函数列表(原文有部分重复项)


[collapse=Helper Functions]

Name(unit)
Returns the name of the unit. If it is a Vehicle will display text like "Player's Vehicle"

VehicleName(unit)
Returns the name of the vehicle or the unit in the format of "Player's Vehicle" if the unit is a vehicle and the owner can be determined.

FormatDuration(seconds, format)
Converts time in seconds into a friendly format. format specifies the style of format. 'c' is the default format and stands for compressed e.g. "16:40" for 1000 seconds. 'f' for full e.g. "16m 40s" for 1000 seconds. 'e' for extended e.g. "16 Mins 40 Secs" for 1000 seconds. 's' for short e.g. "16.7 Mins" for 1000 seconds.

Angle(value)
Returns three values '<',value,'>' if value is not nil or ''. If value is '' or nil then it returns three empty strings.

Paren(value)
Returns three values '(',value,')' if value is not nil or ''. If value is '' or nil then it returns three empty strings.

UpdateIn(seconds)
Flag's the text to be updated one time in the given seconds (can be fractional)

AFKDuration(unit)
Return the seconds the unit has been AFK. Flags the text to be updated if the afk status changes and every 0.25 seconds when they are afk.

AFK(unit)
Returns "AFK (0:00)" for units that are AFK. Flags the text to be updated if the afk status changes and every 0.25 seconds when they are afk.

IsAFK(unit)
Returns true if the unit is AFK and false if not.

DND(unit)
Returns DND for units that are DND. Flags the text to be updated if the dnd status changes.

IsDND(unit)
Returns true if the unit is DND and false if not.

HostileColor(unit)
Returns r,g,b values (0-255) for the hostility of the unit.

ClassColor(unit)
Returns r,g,b values (0-255) for the class of the unit.

DifficultyColor(unit)
Returns r,g,b values (0-255) for the difficulty of the unit.

AggroColor(unit)
Returns r,g,b values(0-255) for the color provided by the WoW API UnitSelectionColor().

Classification(unit)
Returns the unit classification i.e. Rare, Rare-Elite, Elite, Boss

Class(unit)
Returns the class of the unit

Level(unit)
Returns the level of the unit or '??' if the level is too high to be known.

Creature(unit)
Returns the creature type for the unit.

SmartRace(unit)
Returns the race for units that are players and creature type for NPCs.

IsPet(unit)
Returns true if the unit is a pet.

OfflineDuration(unit)
Returns the time the unit has been offline. Flags the text for update when the offline state of the unit changes or ever 0.25 seconds.

Offline(unit)
Returns "Offline (0:00)". Flags the text for update when the offline state of the unit changes or ever 0.25 seconds.

IsOffline(unit)
Returns true if the unit is Offline and false if not.

DeadDuration(unit)
Returns the time the unit has been dead. Flags the text for update when the dead state of the unit changes or ever 0.25 seconds.

Dead(unit)
Returns "Dead (0:00)" or "Ghost (0:00)". Flags the text for update when the dead state of the unit changes or ever 0.25 seconds.

DruidForm(unit)
Returns the druid form of the unit if there is one.

Status(unit)
Returns "Offline (0:00)" if offline, "Divine Intervention" if they have the buff, "Feigned Death" if feigned or "Dead (0:00)" or "Ghost (0:00)". Flags the text for update as Offline() and Dead() does.

HP(unit)
Returns HP or 0 if the unit is dead.

MaxHP(unit)
Returns unit's max HP.

Power(unit,power_type)
Returns the power for the unit of the power_type. If power_type is not passed then returns the power for the current power_type.

MaxPower(unit,power_type)
Returns the max power for the unit of the power_type. If power_type is not passed then returns the max power for the current power_type.

Round(number,digits)
Rounds the number to the given number of digits.

Short(number,format)
Shortens the number e.g. 1234 becomes 1.23k. If format is true then calls the format function for you and returns a single pre-formatted string. Otherwise returns the format string and the number.

VeryShort(number,format)
Shortens the number e.g. 1234 becomes 1k. If format is true then calls the format function for you and returns a single pre-formatted string. Otherwise returns the format string and the number.

IsMouseOver()
Returns true if the mouse is over the frame. Flags the text to be updated whenever the mouse enters or leaves the frame.

Combos(unit,target)
Returns the combo points for the unit. If target is not passed it's presumed to be the "target". If the unit is left blank it will return the player's combo points or if they have a vehicle their vehicle's combo points.

ComboPoints(symbol,unit,target)
Returns a string with the symbol string repeated for every combo point. unit and target are handled the same as Combos(unit,target)

Percent(x,y)
Figures the percent of y that x is.

XP(unit)
Returns the XP for the unit.

MaxXP(unit)
Returns the maximum XP for the unit.

RestXP(unit)
Returns the rest XP for the unit.

ThreatPair(unit)
Figures the pair of units to show threat for considering if the unit is friendly or not.

ThreatStatusColor(status)
Returns the color for the status of threat (0-255).

CastData(unit)
Returns a table with information about what the unit is casting. The keys are as follows: spell is the spell they are casting, rank is the rank of the spell, start_time is the time the spell started being cast, end_time is when the spell will finish, delay is how long the cast has been delayed, target is the target of the spell if available, casting is true if the spell is a spell cast, channeling is the cast is channeled, fade_out is true if the spell is canceled and it's during the fade time for the display, stop_time is when the spell stopped casting, stop_message is why the spell was stopped. Flags the text to be updated quickly when the unit is casting.

Alpha(number)
Sets the transparency of the text. 0 is invisible, 1 is solid.

Outline()
Sets the text to have an outline.

ThickOutline()
Sets the text to have a thick outline.

Abbreviate(text)
Abbreviates the text if it has a space in it. E.G. Blood Elf becomes BE

PVPDuration(unit)
Returns the time for the PVP flag to go away. Flags the text for fast updates when the timer is running.

HPColor(cur, max)
Returns r,g,b for the color of the text where 100% is green and going towards red at 0%. Values are in the 0-255 range.

PowerColor(power_type)
Returns the r,g,b for the given power type. Values are in the 0-255 range.

ConfigMode()
Returns the name of the text in curly braces if in config mode. Useful for some texts texts where there might not always be a value but config mode will show the bar they are attached to. For example cast bars.

ShortClassification(unit or classification)
Returns a shortened classification given a unit or a classifcation.

ShortClass(unit or class)
Returns a shortened class given a unit or class.

ShortRace(unit or race)
Returns a shortened race given a unit or class.

AggroColor(unit)
Wrapper for UnitSelectionColor()

HPColor(cur,max)
Returns a color for the HP with green being full, yellow being half and red being empty.

PowerColor(power_type)
Returns the color for the power type.

ReputationColor(reaction)
Returns the color for the reputation level given.

SeparateDigits(number, thousands, decimal)
Returns the number formatted as a string with the string passed in thousands separating the thousand places and the string passed in decimal separating the integer from the fractional. If thousands is omitted "," is used and if decimal is omitted "." is used. [/collapse]

[b][size=130%]辅助函数[/size][/b]

以下是在LuaTexts的lua脚本中可以使用的辅助函数。[b](译注:这些函数在原文中有部分重复,且尚未经我逐一测试,因此函数作用的翻译可能会有一定偏差)[/b]
[table]
[tr][td30]Name(unit)[/td][td]返回单位的名称。如果是坐骑,则返回“某某人的坐骑”这样的名称[/td][/tr]
[tr][td]VehicleName(unit)[/td][td]如果单位是坐骑,并且其拥有者能被确定,则以“某某人的坐骑”这种形式返回坐骑名称[/td][/tr]
[tr][td]FormatDuration(seconds, format)[/td][td]将以秒为单位的时间转换为更易读的格式。其中format决定了格式,以1000秒为例,c表示标准格式—16:40,f表示完整格式—16m 40s,e表示扩展格式—16 Mins 40 Secs,s表示短格式—16.7 Mins[/td][/tr]
[tr][td]Angle(value)[/td][td]返回value用一对“<>”括上后的形式(3个值:"<",value,">")。如果value为空,则返回3个空字符串[/td][/tr]
[tr][td]Paren(value)[/td][td]类似Angle(),只是将“<>”换作“()”[/td][/tr]
[tr][td]UpdateIn(seconds)[/td][td]在给定的秒数后更新文本[/td][/tr]
[tr][td]AFKDuration(unit)[/td][td]返回单位AFK的时长。该函数会隐式地每0.25秒检测AFK状态并更新文本[/td][/tr]
[tr][td]AFK(unit)[/td][td]以“AFK(0:00)”形式返回一个字符串,同样会隐式地更新文本[/td][/tr]
[tr][td]IsAFK(unit)[/td][td]返回单位是否AFK,true或者false。[/td][/tr]
[tr][td]DND(unit)[/td][td]返回单位DND状态下设置的文本,会在DND状态改变时隐式地更新文本[/td][/tr]
[tr][td]IsDND(unit)[/td][td]返回单位是否设置为DND状态,类似IsAFK()[/td][/tr]
[tr][td]HostileColor(unit)[/td][td]以RGB 255分色值返回系统定义的敌对单位颜色[/td][/tr]
[tr][td]ClassColor(unit)[/td][td]以RGB 255分色值返回系统定义的职业颜色[/td][/tr]
[tr][td]DifficultyColor(unit)[/td][td]以RGB 255分色值返回系统定义的目标击杀难度颜色[/td][/tr]
[tr][td]AggroColor(unit)[/td][td]以RGB 255分色值返回仇恨颜色(同WoW API UnitSelectionColor()返回的值)[/td][/tr]
[tr][td]Classification(unit)[/td][td]返回单位的类别:稀有、稀有精英、精英、Boss[/td][/tr]
[tr][td]Class(unit)[/td][td]返回单位的职业[/td][/tr]
[tr][td]Level(unit)[/td][td]返回单位的等级[/td][/tr]
[tr][td]Creature(unit)[/td][td]返回单位的生物类型:元素、人型、亡灵等[/td][/tr]
[tr][td]SmartRace(unit)[/td][td]返回单位的种族,以及NPC的生物类型[/td][/tr]
[tr][td]IsPet(unit)[/td][td]判断目标是否是宠物[/td][/tr]
[tr][td]OfflineDuration(unit)[/td][td]返回单位离线的时长。该函数会隐式地每0.25秒检测离线状态并更新文本[/td][/tr]
[tr][td]Offline(unit)[/td][td]以“Offline(0:00)”形式返回一个字符串,同样会隐式地更新文本[/td][/tr]
[tr][td]IsOffline(unit)[/td][td]返回单位是否离线,true或者false。[/td][/tr]
[tr][td]DeadDuration(unit)[/td][td]返回单位死亡的时长。该函数会隐式地每0.25秒检测死亡状态并更新文本[/td][/tr]
[tr][td]Dead(unit)[/td][td]以死亡“Dead(0:00)”或灵魂“Ghost(0:00)”形式返回一个字符串,同样会隐式地更新文本[/td][/tr]
[tr][td]DruidForm(unit)[/td][td]返回单位的德鲁伊形态[/td][/tr]
[tr][td]Status(unit)[/td][td]返回单位的状态:离线(0:00)、无敌、假死、死亡(0:00)、灵魂(0:00),离线与死亡状态会隐式更新文本[/td][/tr]
[tr][td]HP(unit)[/td][td]返回单位当前血量,死亡则为0[/td][/tr]
[tr][td]MaxHP(unit)[/td][td]返回单位的最大血量[/td][/tr]
[tr][td]Power(unit,power_type)[/td][td]返回单位的法力值、能量、怒气值。power_type决定返回何种值(仅对德鲁伊有效)[/td][/tr]
[tr][td]MaxPower(unit,power_type)[/td][td]返回单位的最大法力值、能量、怒气值[/td][/tr]
[tr][td]Round(number,digits)[/td][td]按digits给定的位数,对number四舍五入[/td][/tr]
[tr][td]Short(number,format)[/td][td]返回number的缩略格式,比如将1234格式化为1.23K。fortmat为true,则返回一个经过预格式化后的串;反之返回内置的style与number本身[/td][/tr]
[tr][td]VeryShort(number,format)[/td][td]类似Short(),不过形式会更短,比如1234将被格式化为1K[/td][/tr]
[tr][td]IsMouseOver()[/td][td]判断鼠标是否滑过单位框体,会隐式更新文本[/td][/tr]
[tr][td]Combos(unit,target)[/td][td]返回连击点数。如果调用时省略target参数,则默认为单位的当前目标。如果传入的unit参数为空,则默认为玩家或者。[/td][/tr]
[tr][td]ComboPoints(symbol,unit,target)[/td][td]以内置预定的符号返回连击点文本串[/td][/tr]
[tr][td]Percent(x,y)[/td][td]返回x占到y的百分比[/td][/tr]
[tr][td]XP(unit)[/td][td]返回单位的当前经验值[/td][/tr]
[tr][td]MaxXP(unit)[/td][td]返回单位当前这级的总经验值[/td][/tr]
[tr][td]RestXP(unit)[/td][td]返回单位到下一等级剩余的经验值[/td][/tr]
[tr][td]ThreatPair(unit)[/td][td]指示单位是否为友好单位[/td][/tr]
[tr][td]ThreatStatusColor(status) [/td][td]以RGB 255分色值返回仇恨状态对应的颜色[/td][/tr]
[tr][td]CastData(unit)[/td][td]返回单位施放法术的相关信息表。包括法术名称(spell)、法术等级(rank)、开始施放时间(start_time)、施法结束时间(end_time)、延时(delay)、施法目标(target)、读条类(casting)或者通道类(channeling)、施法是否取消(fade_out)、施法停止时间(stop_time)、停止原因(stop_message)。该函数会在施法时即时更新文本[/td][/tr]
[tr][td]Alpha(number)[/td][td]设置文本透明度[/td][/tr]
[tr][td]Outline()[/td][td]为文本添加轮廓线[/td][/tr]
[tr][td]ThickOutline()[/td][td]为文本添加粗轮廓线[/td][/tr]
[tr][td]Abbreviate(text)[/td][td]返回文本的缩写格式。比如将血精灵全称“Blood Elf”缩写为“BE”[/td][/tr]
[tr][td]PVPDuration(unit)[/td][td]返回单位处于PVP状态下的时长,会隐式更新文本[/td][/tr]
[tr][td]HPColor(cur, max)[/td][td]根据cur相对max的比例返回RGB 255分色值。0是红色,100是绿色。[/td][/tr]
[tr][td]PowerColor(power_type)[/td][td]根据power_type返回对应的法力、能量或者怒气的RGB 255分色值[/td][/tr]
[tr][td]ConfigMode()[/td][td]用一对大括号“{}”返回配置模式下的文本值。这在调试施法条文本之类时会比较有用,这样可以避免仅施法期间才能看见文本。[/td][/tr]
[tr][td]ShortClassification(unit or classification)[/td][td]返回单位所属类型名的短名称[/td][/tr]
[tr][td]ShortClass(unit or class)[/td][td]返回单位所属职业的短名称[/td][/tr]
[tr][td]ShortRace(unit or race)[/td][td]返回单位所属种族的短名称[/td][/tr]
[tr][td]ReputationColor(reaction)[/td][td]返回给定的声望等级的颜色[/td][/tr]
[tr][td]SeparateDigits(number, thousands, decimal)[/td][td]返回number格式化后的文本,其中包括thousands定义的千分位样式(默认为“,”)与decimal定义的小数点样式(默认为“.“)[/td][/tr]
[/table]
[h]全文完[/h]

改动



留名 高玩啊


坐等楼主更新。
支持。


引用代码的部分建议使用这个版式

[code=lua]return "%s / %s",HP(unit),MaxHP(unit)[/code]


坐等楼主更新
辛苦了~


[quote][pid=60096308][b]Post by chinn (2010-08-20 13:30):[/b][/pid]

引用代码的部分建议使用这个版式

[code=lua]return "%s / %s",HP(unit),MaxHP(unit)[/code][/quote]

谢谢,才发现NGA也提供语法高亮了,呵呵。 不过Quote与Code不能交叉。

改动



坐等楼主更新,看过LZ一篇火毁的指导贴...


看看……精神上支持……有空就來學習……


那么,向程序员致敬了


请加油填坑。


这个好棒啊,LZ加了个油


坑终于填完了,欢迎大家批评指正。

昨天晚上我开始逐一试验上述辅助函数,还尝试着将WoW API中涉及Unit、Location、Map等方面的public类函数也在lua脚本中引入,发现还真是好玩。

有时间再把自己鼓捣的这些小脚本放上来,做个PitBull4的简明教程,呵呵。


感谢大神,,为这个纠结了很久了...


感谢LZ,有营养的好帖,学习了。


Reputation应该译做声望而不是荣誉。
[url]http://bbs.ngacn.cc/read.php?tid=2469559&_fp=1[/url]
这是一篇偏向于界面配置的Pitbull教程。期待楼主能够写出一些更深入的教学贴。
加勒个油,楼主!

评论

UID:407493
上WoWWiki去核对了这个词的意思,已更正,谢谢。其实还有些的翻译我也是不太满意的,等我慢慢熟悉吧。 [+]


[s:24] 其实PB最大的优点就是图形界面配置..代码实在头疼
习惯用代码的不都投奔OUF的阵营了么...不过PB的个性化设置不如STUF...
感谢楼主分享 ......

评论

UID:407493
Stuf我也用过,如果开了Stuf RAID Frame,内存占用和Pitbull不相上下。在GUI Config方面,两者也没太大差别。但Pitbull之前有DogTag,现在又加入了LuaTexts,所以我选择了Pitbull。呵呵 [+]


MARK一记,LZ高玩
[19 楼] 2010-08-22 18:12

对回复(60197934)发表了一条评论

下一页(2)