Nui template
From NWN Lexicon
Jump to navigationJump to search
NUI Template
This is a basic template for creating new NUI windows. Some boilerplate code to help you boot strap a new NUI project.
Setup: Create a new file called 'nui_template.nss' and fill in contents with #template below.
In OnNuiEvent call something like this :
#include "nui_template"
void main()
{
object oPlayer = NuiGetEventPlayer();
int nToken = NuiGetEventWindow();
string sEvent = NuiGetEventType();
string sElem = NuiGetEventElement();
int nIdx = NuiGetEventArrayIndex();
string sWndId = NuiGetWindowId(oPlayer, nToken);
if (sWndId == NUI_TEMPLATE_WINDOW_ID)
{
NUI_Template_Events();
return;
}
}
Then to open the window call this from anywhere you like
#include "nui_template"
void main()
{
// ...
NUI_Template_NewWindow(oPlayer);
// ...
}
Optionally, if you need to update the NUI on external events (like if a player equips or unequips an item), just call the update function:
#include "nui_template"
void main()
{
// ...
NUI_Template_Update(oPlayer);
// ...
}
Or the Update function can be called within the NUI code directly (see the "mouseup" event for element: "buttonUpdate" in #template below).
Template
#include "nw_inc_nui"
const string NUI_TEMPLATE_WINDOW_ID = "nui_template_full";
void NUI_Template_NewWindow(object oPlayer);
void NUI_Template_Update(object oPlayer, int nToken = -1);
void NUI_Template_Events();
void NUI_Template_Events()
{
object oPlayer = NuiGetEventPlayer();
int nToken = NuiGetEventWindow();
string sEvent = NuiGetEventType();
string sElement = NuiGetEventElement();
int nIndex = NuiGetEventArrayIndex();
string sWindowId = NuiGetWindowId(oPlayer, nToken);
if (sWindowId != NUI_TEMPLATE_WINDOW_ID)
{
return;
}
// If events aren't working, log what is going on.
// LogMessage("NUIExample: sElement: " + sElement + " sEvent: " + sEvent);
switch (HashString(sEvent))
{
case ("blur"):
{
}
break;
case ("click"):
{
}
break;
case ("close"):
{
}
break;
case ("focus"):
{
}
break;
case ("mousedown"):
{
}
break;
case ("mousescroll"):
{
}
break;
case ("mouseup"):
{
switch (HashString(sElement))
{
case ("buttonUpdate"):
{
NUI_Template_Update(oPlayer, nToken);
return;
}
break;
case ("buttonCancel"):
{
NuiDestroy(oPlayer, nToken);
return;
}
break;
}
}
break;
case ("open"):
{
}
break;
case ("range"):
{
}
break;
case ("watch"):
{
}
break;
default:
{
// LogMessage("NUIExample: Unknown event occured: " + sEvent + " Element: " + sElement);
}
break;
}
}
void NUI_Template_Update(object oPlayer, int nToken = -1)
{
if (nToken == -1)
{
nToken = NuiFindWindow(oPlayer, NUI_TEMPLATE_WINDOW_ID);
}
if (nToken == 0)
{
return;
}
NuiSetBind(oPlayer, nToken, "labelMain", JsonString(IntToString(Random(100))));
}
void NUI_Template_NewWindow(object oPlayer)
{
int nToken = NuiFindWindow(oPlayer, NUI_TEMPLATE_WINDOW_ID);
if (nToken != 0)
{
NuiDestroy(oPlayer, nToken);
}
/////////////////////////////////////////
// Build the main root pane here
/////////////////////////////////////////
json jRoot = JsonArray();
json jRow;
// Build the first row
{
jRow = JsonArray();
json jLabelMain = NuiLabel(NuiBind("labelMain"), JsonInt(NUI_HALIGN_CENTER), JsonInt(NUI_VALIGN_MIDDLE));
jRow = JsonArrayInsert(jRow, jLabelMain);
jRow = NuiRow(jRow);
jRoot = JsonArrayInsert(jRoot, jRow);
}
// Build the second row
{
jRow = JsonArray();
json jButtonUpdate = NuiId(NuiButton(JsonString("Update")), "buttonUpdate");
json jButtonCancel = NuiId(NuiButton(JsonString("cancel")), "buttonCancel");
jRow = JsonArrayInsert(jRow, NuiSpacer());
jRow = JsonArrayInsert(jRow, jButtonUpdate);
jRow = JsonArrayInsert(jRow, jButtonCancel);
jRow = NuiRow(jRow);
jRoot = JsonArrayInsert(jRoot, jRow);
}
///////////////////////////////////////////
// Build final group
///////////////////////////////////////////
jRoot = NuiCol(jRoot);
/////////////////////////////////////////
// Create new window here
/////////////////////////////////////////
json jNuiWindow = NuiWindow(jRoot, NuiBind("windowtitle"), NuiBind("geometry"), NuiBind("resizable"), NuiBind("collapsed"), NuiBind("closable"), NuiBind("transparent"), NuiBind("border"), NuiBind("acceptinput"));
nToken = NuiCreate(oPlayer, jNuiWindow, NUI_TEMPLATE_WINDOW_ID);
NuiSetBind(oPlayer, nToken, "windowtitle", JsonString("NUI Template"));
float fX = -1.0f;
float fY = -1.0f;
float fWidth = 512.0f;
float fHeight = 512.0f;
NuiSetBind(oPlayer, nToken, "geometry", NuiRect(fX, fY, fWidth, fHeight));
NuiSetBind(oPlayer, nToken, "collapsed", JsonBool(FALSE));
NuiSetBind(oPlayer, nToken, "resizable", JsonBool(TRUE));
NuiSetBind(oPlayer, nToken, "closable", JsonBool(TRUE));
NuiSetBind(oPlayer, nToken, "transparent", JsonBool(FALSE));
NuiSetBind(oPlayer, nToken, "border", JsonBool(TRUE));
NuiSetBind(oPlayer, nToken, "acceptinput", JsonBool(TRUE));
// Update values after window is up
NUI_Template_Update(oPlayer);
}