---++!! !commonlib %TOC{title="Contents:"}% %STARTINCLUDE% ---++ common lib funcions | *Title* | common lib funcions | | *Author(s)* | LiXizhi | | *Date* | 2006/11/25 | | *File* | script/ide/commonlib.lua | ---+++ Description basic debugging,serialization functions, etc. %T% __Sample Code__ <verbatim> NPL.load("(gl)script/ide/commonlib.lua"); </verbatim> ---+++ Member Functions ---++++ !commonlib.GetUIObject get an UI object by specifying a serie of names separated by #, such as childname#childname#childname, e.g. commonlib.GetUIObject("wndParent#button1"); * _param_ __name__ : e.g. "wndParent#button1" * _param_ __parent__ : nil or a parent UI object inside which the name is searched. If nil, first childname is searched globally. * _return_ ____ : return the ParaUIObject found. if not found, the returned object IsValid() returns false. __syntax__ <verbatim>function commonlib.GetUIObject(name, parent)</verbatim> __parameters__ | *name* | e.g. "wndParent#button1" | | *parent* | | | *return* | return the ParaUIObject found. if not found, the returned object IsValid() returns false. | ---++++ !commonlib.getfield We rely on gfind, from the string library, to iterate over all words in f (where "word" is a sequence of one or more alphanumeric characters and underscores). * _param_ __f__ : f is a string like "a.b.c.d" * _param_ __rootEnv__ : it can be a table from which to search for f, if nil, the global table _G is used. * _return_ ____ : return the field in LUA, it may be nil, a value, or a table, etc. __syntax__ <verbatim>function commonlib.getfield (f, rootEnv)</verbatim> __parameters__ | *f* | f is a string like "a.b.c.d" | | *rootEnv* | | | *return* | return the field in LUA, it may be nil, a value, or a table, etc. | ---++++ !commonlib.setfield set a variable v to f, where f is a string the call setfield("t.x.y", 10) creates a global table t, another table t.x, and assigns 10 to t.x.y * _param_ __f__ : f is a string like "a.b.c.d" * _param_ __rootEnv__ : it can be a table from which to search for f, if nil, the global table _G is used. __syntax__ <verbatim>function commonlib.setfield (f, v, rootEnv)</verbatim> __parameters__ | *f* | f is a string like "a.b.c.d" | | *v* | | | *rootEnv* | | ---++++ !commonlib.MetaClone clone a obj using just meta table. It does not actually copy parameters. Note: one can not serialize a meta cloned object, because it is an empty table with the same meta table as input obj. __syntax__ <verbatim>function commonlib.MetaClone(obj)</verbatim> __parameters__ | *obj* | | ---++++ !commonlib.deepcopy [[ code from: http://lua-users.org/wiki/CopyTable This function returns a deep copy of a given table. The function below also copies the metatable to the new table if there is one, so the behaviour of the copied table is the same as the original. But the 2 tables share the same metatable, you can avoid this by changing this 'getmetatable(object)' to '_copy( getmetatable(object) )'. ]] __syntax__ <verbatim>function commonlib.deepcopy(object)</verbatim> __parameters__ | *object* | | ---++++ !commonlib.partialcopy alias for deepcopy commonlib.clone = commonlib.deepcopy; this function simply does, dest = src, but it copies value from src to dest. It is mostly used with tables.It just copies what is in src to dest, but dest retains its other fields that does not exist in src. __syntax__ <verbatim>function commonlib.partialcopy(dest, src)</verbatim> __parameters__ | *dest* | | | *src* | | ---++++ !commonlib.mincopy this function simply does, dest = src, but it copies value from src to dest. It is mostly used with tables.It only copies src field to dest field, if src field name does not exist in dest. In other words, dest will preserve all its named fields plus additional fields in dest __syntax__ <verbatim>function commonlib.mincopy(dest, src)</verbatim> __parameters__ | *dest* | | | *src* | | ---++++ !commonlib.partialfields compare all fields in dest with src and return true if equal. __syntax__ <verbatim>function commonlib.partialfields(dest, src, fields)</verbatim> __parameters__ | *dest* | | | *src* | | | *fields* | | ---++++ !commonlib.compare strict compare all fields reccursively in dest and src, value by value Note: it also compare indexed array items * _param_ __tolerance__ : if not nil, it will be used to compare number type values. smaller than this will be regarded as equal. * _return_ __true__ : if equal. __syntax__ <verbatim>function commonlib.compare(dest, src, tolerance)</verbatim> __parameters__ | *dest* | | | *src* | | | *tolerance* | if not nil, it will be used to compare number type values. smaller than this will be regarded as equal. | ---++++ !commonlib.resize resize a table to a new size. It ensures that all elements are nil. this function uses table.getn() instead of #t, use table.resize for #t __syntax__ <verbatim>function commonlib.resize(t, size, v)</verbatim> __parameters__ | *t* | | | *size* | | | *v* | | ---++++ !table.resize resize a table to a new size. It ensures that all elements are nil. * _param_ __t__ : table to resize * _param_ __size__ : the new size * _param_ __v__ : the item value; __syntax__ <verbatim>function table.resize(t, size, v)</verbatim> __parameters__ | *t* | table to resize | | *size* | | | *v* | the item value; | ---++++ !commonlib.removeArrayItem remove an item from a table. The table size will be minored by 1. * _param_ __t__ : table array. * _param_ __nIndex__ : 1 based index, at which to remove the item __syntax__ <verbatim>function commonlib.removeArrayItem(t, nIndex)</verbatim> __parameters__ | *t* | table array. | | *nIndex* | | ---++++ !commonlib.insertArrayItem insert an array item to a table array. The table size will be increased by 1. * _param_ __t__ : table array. * _param_ __nIndex__ : 1 based index, at which to insert the item. if nil, it will inserted to the end * _return_ __return__ : 1 based index at which the item is inserted __syntax__ <verbatim>function commonlib.insertArrayItem(t, nIndex, item)</verbatim> __parameters__ | *t* | table array. | | *nIndex* | | | *item* | | | *return* | 1 based index at which the item is inserted | ---++++ !commonlib.swapArrayItem swap two items in a table. __syntax__ <verbatim>function commonlib.swapArrayItem(t, nIndex1, nIndex2)</verbatim> __parameters__ | *t* | | | *nIndex1* | | | *nIndex2* | | ---++++ !utf8.len ------------------------- a simple UTF8 lib use: commonlib.utf8.len("abc"); commonlib.utf8.sub("abc", 1, nil) ------------------------- local utf8 = {}; commonlib.utf8 = utf8; return the number of characters in UTF8 encoding. more info at: http://lua-users.org/wiki/LuaUnicode __syntax__ <verbatim>function utf8.len(unicode_string)</verbatim> __parameters__ | *unicode* | | | *string* | | ---++++ !utf8.sub similar to string.sub(), except that nFrom, nTo refers to characters, instead of byte __syntax__ <verbatim>function utf8.sub(ustring, nFrom, nTo)</verbatim> __parameters__ | *ustring* | | | *nFrom* | | | *nTo* | | ---++++ !commonlib.SearchFiles [[ search files and directories in a given path. Results are returned in a table array. e.g. commonlib.SearchFiles(o, "temp/", "*.txt", 0, 150, true) Users can override the default behaviors of the UI controls. the Default behavior is this: listbox_dir shows directories, and is initialized to display sub directories of sInitDir. single click an item will display files in that directory in listbox_file. double click an item will display sub directories in listbox_dir. * _param_ __output__ : values are stored in the out arrays. it must be a table. * _param_ __sInitDir__ : the initial directory. it must ends with slash / * _param_ __sFilePattern__ : e.g."*.", "*.x" or it could be table like {"*.lua", "*.raw"} * _param_ __nMaxFileLevels__ : max file levels. 0 shows files in the current directory. * _param_ __nMaxNumFiles__ : max number of files in file listbox. e.g. 150 * _param_ __listFile__ : True to include file. This can be nil. * _param_ __listDir__ : True to include directory. This can be nil. * _param_ __btn__ :_dir_uplevel: a valid ParaUIObject of type button for displaying the last directory. ]] __syntax__ <verbatim>function commonlib.SearchFiles(output, sInitDir, sFilePattern, nMaxFileLevels, nMaxNumFiles, listFile, listDir)</verbatim> __parameters__ | *output* | values are stored in the out arrays. it must be a table. | | *sInitDir* | | | *sFilePattern* | e.g."*.", "*.x" or it could be table like {"*.lua", "*.raw"} | | *nMaxFileLevels* | | | *nMaxNumFiles* | max number of files in file listbox. e.g. 150 | | *listFile* | | | *listDir* | True to include directory. This can be nil. | ---++++ !commonlib.Absolute get the absolute of the given number, if param not number type return nil __syntax__ <verbatim>function commonlib.Absolute(num)</verbatim> __parameters__ | *num* | | ---++++ !commonlib.shellsort array = shellsort(array, before, n) * _param_ __array__ : is an array of comparable elements to be sorted in place * _param_ __before__ : is a function of two arguments which returns true if its first argument should be before the second argument in the second result. It must define a total order on the elements of array. * _param_ __n__ : is the number of elements in the array. If it is omitted, #array will be used. * _return_ __returns__ : its first argument. __syntax__ <verbatim>function commonlib.shellsort(t, n, before)</verbatim> __parameters__ | *t* | | | *n* | is the number of elements in the array. If it is omitted, #array will be used. | | *before* | | ---++++ !commonlib.multi_inherit look up for `k' in list of tables `plist' local function search (k, plist) for i=1, table.getn(plist) do local v = plist[i][k] -- try `i'-th superclass if v then return v end end end multiple inheritance see. http://www.lua.org/pil/16.3.html __syntax__ <verbatim>function commonlib.multi_inherit(...)</verbatim> ---++++ !commonlib.inherit create a new class inheriting from a baseClass. the new class has new(), _super, isa() function. * _param_ __baseClass__ : the base class from which to inherite the new one. it can be nil if there is no base class. * _param_ __new__ :_class: nil or a raw table. * _return_ __the__ : new class is created. One can later create and instance of the new class by calling its new function(). __syntax__ <verbatim>function commonlib.inherit(baseClass, new_class)</verbatim> __parameters__ | *baseClass* | the base class from which to inherite the new one. it can be nil if there is no base class. | | *new* | | | *class* | | | *return* | new class is created. One can later create and instance of the new class by calling its new function(). | ---++++ !commonlib.tonumber convert the string to number this function will handle the string will additional "0" digits in the front of the string e.x. input: "010" return: 10 __syntax__ <verbatim>function commonlib.tonumber(s)</verbatim> __parameters__ | *s* | | ---++++ !timer.GetNewTimerID [[commonlib.timer is a simpler timer ID generator. Use GetNewTimerID() to create new timer id to avoid timer id collide. ]] local timer = {}; timer.id = 0; timer.count = 0; commonlib.timer = timer; __syntax__ <verbatim>function timer.GetNewTimerID()</verbatim> %STOPINCLUDE%
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r1 - 2008-02-29
-
LiXizhi
Home
Site map
CCWeb web
HaqiTeen web
Main web
ParaEngine web
TWiki web
Main Web
Users
Groups
Index
Search
Changes
Notifications
RSS Feed
Statistics
导航页WebTopMenu
Preferences
开发指南
Getting Started
ParacraftSDK
NPL
MCML
NPL Reference Manual
美术Mod
Account
Log In
English
简体中文
簡體中文
E
dit
A
ttach
Copyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback