FURWARE text/Snippets

From Second Life Wiki
Jump to navigation Jump to search


This page contains several useful code snippets (functions, constants) that can make the usage of FURWARE_text a lot more convenient.

Basic convenience functions

The functions in this section are wrappers around each of the FURWARE text commands. They were designed to follow these rules:

  • The functions shall be self-contained. That is, you may copy any subset of the functions that you need and they will work.
  • The functions (and their parameters) shall be self-explanatory. That is, they also serve as a quick reference guide to the parameters of the commands they execute.

The fact that the functions are self-contained also means that each of them contains a llMessageLinked(...)-call. The target of this call is set to LINK_SET (the whole linkset). Please change this target accordingly in your scripts, if needed.

fwSetText(string text) {
    llMessageLinked(LINK_SET, 0, text, "fw_data");
}

fwSetBoxText(string box, string text) {
    llMessageLinked(LINK_SET, 0, text, "fw_data:" + box);
}

fwSetStyle(string style) {
    llMessageLinked(LINK_SET, 0, style, "fw_conf");
}

fwSetBoxStyle(string box, string style) {
    llMessageLinked(LINK_SET, 0, style, "fw_conf:" + box);
}

fwSetDefaultStyle(string target, string style) {
    // "target" may be empty, "root" or "nonroot".
    llMessageLinked(LINK_SET, 0, style, "fw_defaultconf:" + target);
}

fwSetVariable(string name, string value) {
    llMessageLinked(LINK_SET, 0, value, "fw_var:" + name);
}

fwAddBox(string name, string parent, integer x, integer y, integer w, integer h, string text, string style) {
    llMessageLinked(LINK_SET, 0, text, "fw_addbox: " +name + ":" + parent + ":"+
                    (string)x + "," + (string)y + "," + (string)w + "," + (string)h + ":" + style);
}

fwDelBox(string name) {
    llMessageLinked(LINK_SET, 0, "", "fw_delbox:" + name);
}

fwTouchQuery(integer linkNumber, integer faceNumber, string userData) {
    llMessageLinked(LINK_SET, 0, userData, "fw_touchquery:" + (string)linkNumber + ":" + (string)faceNumber);
}

list fwTouchReply(string replyString) {
    list tokens = llParseStringKeepNulls(replyString, [":"], []);
    return [
        llList2String(tokens, 0), llList2Integer(tokens, 1), llList2Integer(tokens, 2),
        llList2String(tokens, 3), llList2Integer(tokens, 4), llList2Integer(tokens, 5),
        llDumpList2String(llDeleteSubList(tokens, 0, 5), ":")
    ];
}

fwSetNotify(integer enabled) {
    string s = "off"; if (enabled) s = "on";
    llMessageLinked(LINK_SET, 0, s, "fw_notify");
}

fwMemory() {
    llMessageLinked(LINK_SET, 0, "", "fw_memory");
}

fwReset() {
    llMessageLinked(LINK_SET, 0, "", "fw_reset");
}

Examples

The above snippets allow you to use the basic commands more easily, but you may still need to do some manual work, for example when specifying style configuration strings. The following lines give a few assorted examples of calls using the functions.

// Set the text of all boxes in all sets.
fwSetText("Hello world!");

// Set the text of the box "SomeBox".
fwSetBoxText("SomeBox", "This is some box!");

// Set the default style of all boxes to color=red, align=center (see the next section how to make this much nicer).
fwSetDefaultStyle("", "c=red;a=center");

// Add a box "NewBox" using "ExistingBox" as its parent.
fwAddBox("NewBox", "ExistingBox", 2, 2, 10, 5, "Some initial text", "");

// Delete a box.
fwDelBox("NewBox");

// Send a touch query from the touch_start(...)-event.
touch_start(integer num) {
    fwTouchQuery(llDetectedLinkNumber(0), llDetectedTouchFace(0), "Arbitrary user data goes here");
}

// Parse a touch reply in the link_message(...)-event.
link_message(integer sender, integer num, string str, key id) {
    if (id == "fw_touchreply") {
        list result = fwTouchReply(str);
        // The list "result" now contains the reply as [boxName, boxRelativeX, boxRelativeY, rootName, absoluteX, absoluteY, userData].
        // "boxName" is empty if the touch query was invalid.
    }
}

Additional awesomeness

The functions in the previous section are wrappers around the FURWARE_text commands, but you still need to write, for instance, style configuration strings yourself and remember the style options and values. The following set of constants and functions help you with that (again, you don't have to copy all of them but just the ones you need to save script memory!).

KBtip2.png Tip: Note that these constants complement the functions defined above, so make sure to copy the functions you need as well.
// === Colors ===

string COLOR_DEFAULT            = "c=def;";

// Grayscales.
string COLOR_WHITE              = "c=white;";
string COLOR_SILVER             = "c=silver;";
string COLOR_GRAY               = "c=gray;";
string COLOR_BLACK              = "c=black;";

// Standard colors.
string COLOR_RED                = "c=red;";
string COLOR_GREEN              = "c=green;";
string COLOR_BLUE               = "c=blue;";
string COLOR_CYAN               = "c=cyan;";
string COLOR_MAGENTA            = "c=magenta;";
string COLOR_YELLOW             = "c=yellow;";

// Dark colors.
string COLOR_DARKRED            = "c=darkred;";
string COLOR_DARKGREEN          = "c=darkgreen;";
string COLOR_DARKBLUE           = "c=darkblue;";
string COLOR_DARKCYAN           = "c=darkcyan;";
string COLOR_DARKMAGENTA        = "c=darkmagenta;";
string COLOR_DARKYELLOW         = "c=darkyellow;";

// Arbitrary RGB (red, green, blue) color.
string fwColorRGB(float red, float green, float blue) {
    return "c=" + (string)red + "," + (string)green + "," + (string)blue + ",1" + ";";
}

// Arbitrary RGBA (red, green, blue, alpha/opacity) color.
string fwColorRGBA(float red, float green, float blue, float alpha) {
    return "c=" + (string)red + "," + (string)green + "," + (string)blue + "," + (string)alpha + ";";
}

// === Alignment ===

string ALIGN_DEFAULT            = "a=def;";
string ALIGN_LEFT               = "a=left;";
string ALIGN_CENTER             = "a=center;";
string ALIGN_RIGHT              = "a=right;";

// === Wrapping ===

string WRAP_DEFAULT             = "w=def;";
string WRAP_WORD                = "w=word;";
string WRAP_CHAR                = "w=char;";
string WRAP_NONE                = "w=none;";

// === Trimming ===

string TRIM_DEFAULT             = "t=def;";
string TRIM_ON                  = "t=on;";
string TRIM_OFF                 = "t=off;";

// === Fonts ===

string FONT_DEFAULT             = "f=def;";
string FONT_ANDALE              = "f=e31ce6e8-4117-7073-6aac-e6503034b4c5;";
string FONT_ANONYMOUS_PRO       = "f=24c4ead5-04cd-1831-5fd9-ec48882e00b1;";
string FONT_ANONYMOUS_PRO_BOLD  = "f=82c4fcac-3990-223c-286d-5bc92a258fbc;";
string FONT_DEJAVU              = "f=f974fdfc-8fbd-2f29-2b38-3c34411c1fcc;";
string FONT_DEJAVU_BOLD         = "f=5054fec3-1465-af8f-2fe5-e9507795c82a;";
string FONT_ENVYCODER           = "f=a2ec2cf0-b207-db51-d4a1-f3f8d6684c07;";
string FONT_FREEFONT            = "f=05f6f69e-8bdf-a824-a2cf-d91c9e371c23;";
string FONT_INCONSOLATA         = "f=867288f9-940e-a7cf-ac7d-5596ea7fb2c5;";
string FONT_LIBERATION          = "f=c992dfc4-0aca-02c8-bc01-8330e6db6f87;";
string FONT_LIBERATION_BOLD     = "f=1494337d-5296-3c43-8f7c-45617448ec9a;";
string FONT_LUXI                = "f=2d8f52d7-2220-2d7a-15b5-e685a9449e5c;";
string FONT_LUXI_BOLD           = "f=03812794-535b-f546-6171-6bec5bf399be;";
string FONT_MONOFUR             = "f=8fc63e35-a18f-e335-3936-e2ca196a944d;";
string FONT_NOVA                = "f=2df8e09d-e1bd-d17f-6bec-b8d713790380;";
string FONT_PROFONT             = "f=45a8e6f6-90e4-299d-d3bc-0bf4dbf170db;";

// === Borders ===

string BORDER_DEFAULT           = "border=def;";

// These are just some examples for common border styles.
string BORDER_ALL_SIDES         = "border=tblr;";
string BORDER_ALL_SIDES_THICK   = "border=tblr1;";
string BORDER_ALL_SIDES_DOUBLE  = "border=tblr2;";
string BORDER_BOTTOM            = "border=b;";
string BORDER_LEFT_RIGHT        = "border=lr;";

// A more general helper function for borders. The side parameters are ordered clockwise, starting on top.
string fwBorder(integer top, integer right, integer bottom, integer left, integer style) {
    string result = "border=";
    if (top)    result += "t"; if (right)  result += "r";
    if (bottom) result += "b"; if (left)   result += "l";
    if (style)  result += (string)style;
    return result + ";";
}

// === Style variables ===

// Loads a style stored in a fw_var(iable).
string fwStyle(string styleVariableName) {
    return "style=" + styleVariableName + ";";
}

Examples

Note that the value of each constant ends with ";". This allows you to simply "+" them together and you still get valid style strings which you may then use wherever you need a style config string. This allows you to write things like this:

// Set the color, wrapping and font of all boxes.
fwSetStyle(COLOR_YELLOW + WRAP_NONE + FONT_MONOFUR);

// Set the default style for all boxes.
fwSetDefaultStyle("", COLOR_RED + ALIGN_CENTER);

// Add a box with some style config. We will use a custom RGB color here.
fwAddBox("SomeBox", "ParentBox", 2, 2, 10, 5, "Some initial text", FONT_LUXI + fwColorRGB(0.5, 0.75, 0.0) + BORDER_ALL_SIDES);

// Store a style string in a variable and use it.
fwSetVariable("MyStyle", FONT_MONOFUR + ALIGN_CENTER + BORDER_ALL_SIDES);
fwSetBoxStyle("BoxA", fwStyle("MyStyle"));
fwSetBoxStyle("BoxB", fwStyle("MyStyle"));