Get dialogs working.

This commit is contained in:
zontreck 2024-06-03 02:31:23 -07:00
parent 7b0ad9f60b
commit 6e28878b6a
2 changed files with 88 additions and 29 deletions

View file

@ -54,7 +54,7 @@ saveMenu(string sName, list lButtons, list lUtilityButtons, integer iMenuVersion
llLinksetDataWrite("menus." + sName, llList2Json(JSON_OBJECT, [
"buttons", llList2Json(JSON_ARRAY, lButtons),
"utility", llList2Json(JSON_ARRAY, lUtilityButtons),
"version", iMenuVersion // This flag is used to know if on startup a script needs to overwrite this menu definition,
"version", iMenuVersion, // This flag is used to know if on startup a script needs to overwrite this menu definition,
"prompt", sMenuText
]));
}
@ -64,23 +64,10 @@ updateMenu(string sName, string sJson) {
}
// This function deserializes a menu from the buffer
list readMenu(string sName) {
list lMenu = [];
string readMenu(string sName) {
string sMenu = llLinksetDataRead("menus." + sName);
list lButtons = llJson2List(llJsonGetValue(sMenu, ["buttons"]));
list lUtility = llJson2List(llJsonGetValue(sMenu, ["utility"]));
integer iVer = (integer)llJsonGetValue(sMenu, ["version"]);
string sPrompt = llJsonGetValue(sMenu, ["prompt"]);
lMenu += [llGetListLength(lButtons)] + lButtons;
lMenu += [llGetListLength(lUtility)] + lUtility;
lMenu += [iVer];
lMenu += [sPrompt];
return lMenu;
return sMenu;
}
list g_lListeners = [];
@ -89,18 +76,18 @@ startListen(key kAv, integer iChannel, string sMenuName, string sMenuText) {
integer iIndex=llListFindList(g_lListeners, [kAv]);
if(iIndex == -1) {
integer iListener = llListen(iChannel, "", kAv, "");
g_lListeners += [kAv, sMenuName, iChannel, sMenuText, 0];
g_lListeners += [kAv, sMenuName, iChannel, sMenuText, 0, iListener];
}else {
stopListen(kAv);
startListen(kAv, iChannel, sMenuName);
startListen(kAv, iChannel, sMenuName, sMenuText);
}
}
stopListen(key kAv) {
integer iIndex = llListFindList(g_lListeners, [kAv]);
if(iIndex != -1) {
g_lListeners = llDeleteSubList(g_lListeners, iIndex, iIndex+4); // Stride is 5 - ID, path, channel, text, page
g_lListeners = llDeleteSubList(g_lListeners, iIndex, iIndex+5); // Stride is 6 - ID, path, channel, text, page, listen handle
}
}
@ -115,7 +102,7 @@ integer getChannel(key kID) {
integer iIndex = llListFindList(g_lListeners, [kID]);
if(iIndex!=-1) {
return llList2Integer(g_lListeners, iIndex+2);
}
} else return -1;
}
string CreateBlankMenu() {
@ -226,11 +213,8 @@ list getPage(string sMenu, integer iPage) {
lPageButtons += llList2String(lButtons, i);
}
// Add navigation buttons if necessary using the existing function
if (totalPages > 1) {
list navigatorButtons = getNavigatorButtons(iPage, totalPages);
lPageButtons += navigatorButtons;
}
// Add navigation buttons
lPageButtons = getNavigatorButtons(iPage, totalPages) + lPageButtons;
return lPageButtons;
}
@ -238,7 +222,7 @@ list getPage(string sMenu, integer iPage) {
default
{
state_entry() {
saveMenu("menus.root", [], [], 1, "Basic and initialized root menu");
llLinksetDataDeleteFound("menus", "");
llMessageLinked(LINK_SET, LINK_SIGNAL_QUERY_MENU, "root", "");
}
@ -251,7 +235,18 @@ default
}
}
changed(integer t) {
if(t & CHANGED_INVENTORY) {
llSleep(2);
llResetScript();
}
}
listen(integer c,string n,key i,string m) {
integer iIndex = llListFindList(g_lListeners, [i]);
// Get the listener handle
integer iListener = llList2Integer(g_lListeners, iIndex+5);
llListenRemove(iListener);
}
@ -262,6 +257,8 @@ default
llMessageLinked(LINK_SET, LINK_SIGNAL_CHANNEL_BACK, (string)iChan, "");
} else if(n == LINK_SIGNAL_SHOW_MENU) {
string sMenu = readMenu(llJsonGetValue(m,["menu"]));
integer iChan = generateChannel(FALSE);
startListen(i, iChan, m, llJsonGetValue(sMenu, ["prompt"]));
// Show the dialog window now after constructing pages if needed.
@ -274,7 +271,7 @@ default
updatePage(i, iPage);
string sAppend = "";
integer maxPages = calcMaxPages(sMenu);
if(maxPages>1) sAppend = "\n\nPage " + (string)iPage+"/" + (string)maxPages;
llDialog(i, llJsonGetValue(sMenu, ["prompt"])+sAppend, lPageButtons, getChannel(i));
} else if(n == LINK_SIGNAL_REGISTER_MENU) {
@ -288,7 +285,7 @@ default
llLinksetDataWrite("menus."+(string)i, sMenuJson);
llSleep(0.1);
llMessageLinked(LINK_SET, LINK_SIGNAL_QUERY_MENU, i + "/" + m, "")
llMessageLinked(LINK_SET, LINK_SIGNAL_QUERY_MENU, (string)i + "/" + m, "");
} else if(n == LINK_SIGNAL_RESET) {
llResetScript();
} else if(n == LINK_SIGNAL_MENU_DATA) {
@ -303,7 +300,7 @@ default
sJson = llJsonSetValue(sJson, ["prompt"], llJsonGetValue(m, ["prompt"]));
}
llLinksetDataWrite("menus." + i, sJson);
llLinksetDataWrite("menus." + (string)i, sJson);
}
}

62
includes/header.lsl Normal file
View file

@ -0,0 +1,62 @@
integer LINK_SIGNAL_GEN_CHANNEL = 0601241;
integer LINK_SIGNAL_CHANNEL_BACK = 0601242;
integer LINK_SIGNAL_SHOW_MENU = 0601243;
integer LINK_SIGNAL_MENU_TIMEOUT = 0601244;
integer LINK_SIGNAL_REREGISTER_MENUS= 0601245;
integer LINK_SIGNAL_QUERY_MENU = 0602241;
integer LINK_SIGNAL_REGISTER_MENU = 0602242;
integer LINK_SIGNAL_RESET = 0602243;
integer LINK_SIGNAL_MENU_DATA = 0602244;
string PREVIOUS_MENU = "<--";
string EXIT_MENU = "-exit-";
string NEXT_MENU = "-->";
string decodeLM(integer iSignal) {
switch(iSignal) {
case LINK_SIGNAL_GEN_CHANNEL: {
return "LINK_SIGNAL_GEN_CHANNEL";
}
case LINK_SIGNAL_CHANNEL_BACK: {
return "LINK_SIGNAL_CHANNEL_BACK";
}
case LINK_SIGNAL_SHOW_MENU: {
return "LINK_SIGNAL_SHOW_MENU";
}
case LINK_SIGNAL_MENU_TIMEOUT: {
return "LINK_SIGNAL_MENU_TIMEOUT";
}
case LINK_SIGNAL_REREGISTER_MENUS: {
return "LINK_SIGNAL_REREGISTER_MENUS";
}
case LINK_SIGNAL_QUERY_MENU: {
return "LINK_SIGNAL_QUERY_MENU";
}
case LINK_SIGNAL_REGISTER_MENU: {
return "LINK_SIGNAL_REGISTER_MENU";
}
case LINK_SIGNAL_RESET: {
return "LINK_SIGNAL_RESET";
}
case LINK_SIGNAL_MENU_DATA: {
return "LINK_SIGNAL_MENU_DATA";
}
default: {
return (string)iSignal;
}
}
}
RegisterMenuButton(string sMenu, string sButton) {
llMessageLinked(LINK_SET, LINK_SIGNAL_REGISTER_MENU, sButton, sMenu);
}
SetMenuPrompt(string sMenu, string sPromptText) {
llMessageLinked(LINK_SET, LINK_SIGNAL_MENU_DATA, llList2Json(JSON_OBJECT, ["prompt", sPromptText]), sMenu);
}
showMenu(key kID, string sMenu, integer iPage) {
llMessageLinked(LINK_SET, LINK_SIGNAL_SHOW_MENU, llList2Json(JSON_OBJECT, ["menu", sMenu, "page", iPage]), kID);
}