/****************************************************************************/
/* Nebula2 - Tutorial 14 */
/* Using nGui Server */
/* author: happykevins */
/****************************************************************************/
///----------------------------------------------------------------------------
/// +必要头文件
// nebula2 includes
#include "kernel/nkernelserver.h"
#include "kernel/nfileserver2.h"
#include "script/ntclserver.h"
#include "gfx2/nd3d9server.h"
#include "gfx2/ncamera2.h"
#include "gui/nguiserver.h"
#include "tools/nmayacamcontrol.h"
#include "scene/nsceneserver.h"
#include "gui/nguiwindow.h"
#include "gui/nguilabel.h"
#include "gui/nguitextlabel.h"
#include "gui/nguitextbutton.h"
#include "../NebulaUtils/nutildefs.h"
#include "../NebulaUtils/nkernelinfo.h"
/// -必要头文件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_microtcl.lib")
#pragma comment(lib, "d_nkernel.lib")
#pragma comment(lib, "d_nnebula.lib")
#pragma comment(lib, "d_ntoollib.lib")
#pragma comment(lib, "d_ngui.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dxerr9.lib")
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9d.lib")
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "d_ndinput8.lib")
#pragma comment(lib, "d_ndirect3d9.lib")
/// -链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +声明使用的Nebula2 Package&Module
nNebulaUseModule(ntclserver);
nNebulaUseModule(nresource);
nNebulaUseModule(nresourceserver);
nNebulaUseModule(ninputserver);
nNebulaUseModule(nfont2);
nNebulaUseModule(nmesh2);
nNebulaUseModule(nmesharray);
nNebulaUseModule(nshader2);
nNebulaUseModule(ntexture2);
nNebulaUseModule(ngfxserver2);
nNebulaUseModule(nsceneserver);
nNebulaUseModule(nshadowserver2);
nNebulaUseModule(nscenenode);
nNebulaUseModule(ntransformnode);
nNebulaUseModule(nabstractcameranode);
nNebulaUseModule(nclippingcameranode);
nNebulaUseModule(nreflectioncameranode);
nNebulaUseModule(nvariableserver);
nNebulaUseModule(nconserver);
nNebulaUsePackage(ngui);
nNebulaUsePackage(ndirect3d9);
nNebulaUsePackage(ndinput8);
/// -声明使用的Nebula2 Package&Module
///----------------------------------------------------------------------------
nCamera2 cam;
nMayaCamControl ccam;
nKernelInfoHelper ks_info(NULL);
nTclServer* tcl = NULL;
nKernelServer* ks = NULL;
nD3D9Server* gfx2 = NULL;
nInputServer* input = NULL;
nSceneServer* scene = NULL;
nGuiServer* gui = NULL;
///----------------------------------------------------------------------------
/// +添加GUI控件
///
void SetupGUI()
{
const float borderSize = 0.02f;
// create a dummy root window
//gui->SetRootWindowPointer(0);
nGuiWindow* userRootWindow = gui->NewWindow("nguiwindow", true);
n_assert(userRootWindow);
rectangle nullRect(vector2(0.0f, 0.0f), vector2(1.0f, 1.0f));
userRootWindow->SetRect(nullRect);
//nGuiWindow* userRootWindow = gui->GetRootWindowPointer();
ks->PushCwd(userRootWindow);
// create logo label
nGuiLabel* rightLabel = (nGuiLabel*) ks->New("nguilabel", "RightLogo");
n_assert(rightLabel);
vector2 rightLabelSize = gui->ComputeScreenSpaceBrushSize("n2logo");
rectangle rightRect;
rightRect.v0.set(1.0f - rightLabelSize.x - borderSize, 1.0f - rightLabelSize.y - borderSize);
rightRect.v1.set(1.0f - borderSize, 1.0f - borderSize);
rightLabel->SetRect(rightRect);
rightLabel->SetDefaultBrush("n2logo");
rightLabel->SetPressedBrush("n2logo");
rightLabel->SetHighlightBrush("n2logo");
rightLabel->OnShow();
// create a help text label
nGuiTextLabel* textLabel = (nGuiTextLabel*) ks->New("nguitextlabel", "HelpLabel");
n_assert(textLabel);
textLabel->SetText("Esc: 系统菜单请不要点击"Har/Scn/Dis/Cha" 由于没有连接以上服务,会导致程序崩溃!");
textLabel->SetFont("GuiSmall");
textLabel->SetAlignment(nGuiTextLabel::Left);
textLabel->SetColor(vector4(1.0f, 1.0f, 1.0f, 1.0f));
textLabel->SetClipping(false);
vector2 textExtent = textLabel->GetTextExtent();
rectangle textRect(vector2(0.0f, 0.0f), textExtent);
textLabel->SetRect(textRect);
textLabel->OnShow();
// create a custom button
nGuiTextButton* toggleBtn = (nGuiTextButton*) ks->New("nguitextbutton", "ToggleBtn");
n_assert(toggleBtn);
toggleBtn->SetText("系统菜单");
toggleBtn->SetCommand("UIEvent_ToggleBtn"); // 脚本中的函数名
toggleBtn->SetFont("GuiSmall");
toggleBtn->SetAlignment(nGuiTextButton::Center);
toggleBtn->SetRect(rectangle(vector2(0.0f + borderSize, 1 - 0.1f + borderSize), vector2(0.15f - borderSize, 1.0f - borderSize)));
toggleBtn->SetDefaultBrush("button_n");
toggleBtn->SetPressedBrush("button_p");
toggleBtn->SetHighlightBrush("button_h");
toggleBtn->OnShow();
ks->PopCwd();
// set the new user root window
gui->SetRootWindowPointer(userRootWindow);
}
///
/// +添加GUI控件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +初始化环境,创建需要的Server
///
bool InitApp()
{
/// 创建KernelServer
ks = n_new(nKernelServer);
///----------------------------------------------------------------------------
/// +向KernelServer中添加Package&Module
nNebulaAddModule(ntclserver);
nNebulaAddModule(nresource);
nNebulaAddModule(nresourceserver);
nNebulaAddModule(ninputserver);
nNebulaAddModule(nfont2);
nNebulaAddModule(nmesh2);
nNebulaAddModule(nmesharray);
nNebulaAddModule(nshader2);
nNebulaAddModule(ntexture2);
nNebulaAddModule(ngfxserver2);
nNebulaAddModule(nsceneserver);
nNebulaAddModule(nshadowserver2);
nNebulaAddModule(nscenenode);
nNebulaAddModule(ntransformnode);
nNebulaAddModule(nabstractcameranode);
nNebulaAddModule(nclippingcameranode);
nNebulaAddModule(nreflectioncameranode);
nNebulaAddModule(nvariableserver);
nNebulaAddModule(nconserver);
ks->AddPackage(ndinput8);
ks->AddPackage(ngui);
ks->AddPackage(ndirect3d9);
/// +向KernelServer中添加Package&Module
///----------------------------------------------------------------------------
/// 获得FileServer设置shaders的路径
nFileServer2* file = (nFileServer2*)ks->Lookup("sys/servers/file2");
/// 设置系统路径
file->SetAssign("home", "bin:../../");
file->SetAssign("renderpath", "home:datafiles/shaders");
file->SetAssign("shaders", "renderpath:fixed");
file->SetAssign("scripts", "home:datafiles/scripts");
/// 创建tclserver
tcl = (nTclServer*)ks->New("ntclserver", "/sys/servers/script");
/// 创建ResourceServer
ks->New("nresourceserver", "/sys/servers/resource");
/// 创建variableserver
ks->New("nvariableserver", "/sys/servers/variable");
/// 创建guiserver(RenderPath依赖)
gui = (nGuiServer*)ks->New("nguiserver", "/sys/servers/gui");
/// 创建ConsoleServer(RenderPath依赖)
ks->New("nconserver", "/sys/servers/console");
/// 创建InputServer(GuiServer依赖)
input = (nInputServer*)ks->New("ndi8server", "/sys/servers/input");
/// 创建D3D9Server
gfx2 = (nD3D9Server*)ks->New("nd3d9server", "/sys/servers/gfx");
/// 创建sceneserver
scene = (nSceneServer*)ks->New("nsceneserver", "/sys/servers/scene");
/// 创建shadowserver
ks->New("nshadowserver2", "/sys/servers/shadow");
/// 初始化显示模式
nDisplayMode2 mode;
mode.SetXPos(150);
mode.SetYPos(100);
mode.SetWidth(640);
mode.SetHeight(480);
/// 将显示模式应用到d3d9server
gfx2->SetDisplayMode(mode);
gfx2->SetCamera(cam);
/// 启动sceneserver(启动d3d9server)
scene->SetRenderPathFilename("renderpath:dx7_renderpath.xml");
if ( !scene->Open() )
{
return false;
}
/// 启动GUIServer
nString ret;
tcl->RunScript("scripts:T14_GuiServer.tcl", ret);
gui->Open();
/// 启动InputServer
input->Open();
ret.Clear();
tcl->RunFunction("OnT14MapInput", ret);
/// 设置GUI环境
SetupGUI();
return true;
}
///
/// +初始化环境,创建需要的Server
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +初始化环境,创建需要的Server
///
bool CloseApp()
{
/// 关闭GUIServer
gui->Close();
/// 关闭sceneserver(关闭d3d9server)
scene->Close();
/// 销毁KernelServer
n_delete(ks);
return true;
}
///
/// +初始化环境,创建需要的Server
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +Application
int main(int argc, const char** argv)
{
/// 初始化Application
if ( !InitApp() )
{
n_error("程序初始化失败! ");
return 0;
}
ks_info.SetKernelServer(ks);
ks_info.LogNOH("/");
/// 这里相当于游戏循环,gfx2->Trigger()将触发win32的消息泵
while ( gfx2->Trigger() && tcl->Trigger() )
{
/// Trigger TimeServer
nTimeServer::Instance()->Trigger();
double time = nTimeServer::Instance()->GetTime();
/// Trigger InputServer
input->Trigger(time);
/// 处理GUI事件,需要inputserver的支持
gui->Trigger();
/// Begin Scene (Begin Frame)
if ( scene->BeginScene(ccam.GetViewMatrix()) )
{
// Render Scene
scene->RenderScene();
// End Scene
scene->EndScene();
// Present Scene (End Frame)
scene->PresentScene();
}
/// Flush Events
input->FlushEvents();
n_sleep(0.00f);
}
/// 释放资源
if ( !CloseApp() )
{
n_error("释放资源失败! ");
return 0;
}
return 0;
}
/// -Application
///----------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# T14_GuiServer.tcl
#
# This is extract from the the central Nebula runtime startup script
# which is used for Nebula2 Tutorial GuiServer
#
# The script mainly sets up the nGui Variables and Map Inputs
#
# (C) 2003 RadonLabs GmbH
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# OnGuiServerOpen
#
# This function is called when the Nebula2 GUI server is opened.
#-------------------------------------------------------------------------------
proc OnGuiServerOpen {} {
set cwd [psel]
# 工具提示
# initialize the default tooltip
set guiRoot [/sys/servers/gui.getrootpath]
sel $guiRoot
new nguitooltip Tooltip
sel Tooltip
.setdefaultbrush "tooltip"
.setfont "GuiSmall"
.setcolor 0 0 0 1
.setalignment "left"
.setborder 0.005 0.005
sel ..
# 设置Gui外观相关信息
# define the system skin
set skin [/sys/servers/gui.newskin system]
sel $skin
# set texture path pre- and postfix (NOTE: don't change path to textures:system!!!)
.settextureprefix "home:datafiles/textures/system/"
.settexturepostfix ".dds"
# active and inactive window modulation color
.setactivewindowcolor 1.0 1.0 1.0 1.0
.setinactivewindowcolor 0.6 0.6 0.6 0.6
.setbuttontextcolor 0.0 0.0 0.0 1.0
.settitletextcolor 0.0 0.0 0.0 1.0
.setlabeltextcolor 0.0 0.0 0.0 1.0
.setentrytextcolor 0.0 0.0 0.0 1.0
.settextcolor 0.0 0.0 0.0 1.0
.setmenutextcolor 0.0 0.0 0.0 1.0
#####################################################################
# +添加画刷
#
# define brushes
.beginbrushes
# window title bar, window background, tooltip background
.addbrush titlebar skin 66 152 10 20 1.0 1.0 1.0 1.0
.addbrush window skin 8 154 4 4 1.0 1.0 1.0 1.0
.addbrush tooltip skin 8 154 4 4 1.0 1.0 0.878 0.8
.addbrush pink skin 8 154 4 4 1.0 0.0 1.0 1.0
.addbrush dragbox skin 8 154 4 4 1.0 0.8 0.8 0.5
# text entry field
.addbrush textentry_n skin 446 124 8 8 0.7 0.7 0.7 1.0
.addbrush textentry_p skin 446 124 8 8 0.8 0.8 0.8 1.0
.addbrush textentry_h skin 446 124 8 8 0.9 0.9 0.9 1.0
.addbrush textcursor skin 446 124 8 8 0.4 0.4 0.4 1.0
# the window close button
.addbrush close_n skin 388 40 16 16 1.0 1.0 1.0 1.0
.addbrush close_h skin 404 40 16 16 1.0 1.0 1.0 1.0
.addbrush close_p skin 420 40 16 16 1.0 1.0 1.0 1.0
# the window size button
.addbrush size_n skin 372 40 16 16 1.0 1.0 1.0 1.0
.addbrush size_h skin 372 40 16 16 1.0 1.0 1.0 1.0
.addbrush size_p skin 372 40 16 16 1.0 1.0 1.0 1.0
# arrows
.addbrush arrowleft_n skin 68 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowleft_h skin 84 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowleft_p skin 100 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowright_n skin 116 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowright_h skin 132 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowright_p skin 148 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowup_n skin 164 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowup_h skin 180 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowup_p skin 196 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowdown_n skin 20 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowdown_h skin 36 40 16 16 1.0 1.0 1.0 1.0
.addbrush arrowdown_p skin 52 40 16 16 1.0 1.0 1.0 1.0
# sliders
.addbrush sliderhori_bg skin 228 40 16 16 0.5 0.5 0.5 1.0
.addbrush slidervert_bg skin 228 40 16 16 0.5 0.5 0.5 1.0
.addbrush sliderknobhori_n skin 276 40 16 16 1.0 1.0 1.0 1.0
.addbrush sliderknobhori_h skin 292 40 16 16 1.0 1.0 1.0 1.0
.addbrush sliderknobhori_p skin 308 40 16 16 1.0 1.0 1.0 1.0
.addbrush sliderknobvert_n skin 324 40 16 16 1.0 1.0 1.0 1.0
.addbrush sliderknobvert_h skin 340 40 16 16 1.0 1.0 1.0 1.0
.addbrush sliderknobvert_p skin 356 40 16 16 1.0 1.0 1.0 1.0
# standard buttons
.addbrush button_n skin 192 152 96 20 1.0 1.0 1.0 1.0
.addbrush button_h skin 288 152 96 20 1.0 1.0 1.0 1.0
.addbrush button_p skin 384 152 96 20 1.0 1.0 1.0 1.0
.addbrush menu_n skin 192 172 96 16 1.0 1.0 1.0 1.0
.addbrush menu_h skin 288 172 96 16 1.0 1.0 1.0 1.0
.addbrush menu_p skin 384 172 96 16 1.0 1.0 1.0 1.0
.addbrush button_64x16_n skin 0 0 64 16 1.0 1.0 1.0 1.0
.addbrush button_64x16_h skin 64 0 64 16 1.0 1.0 1.0 1.0
.addbrush button_64x16_p skin 128 0 64 16 1.0 1.0 1.0 1.0
.addbrush button_96x16_n skin 192 0 96 16 1.0 1.0 1.0 1.0
.addbrush button_96x16_h skin 288 0 96 16 1.0 1.0 1.0 1.0
.addbrush button_96x16_p skin 384 0 96 16 1.0 1.0 1.0 1.0
.addbrush button_64x24_n skin 0 16 64 24 1.0 1.0 1.0 1.0
.addbrush button_64x24_h skin 64 16 64 24 1.0 1.0 1.0 1.0
.addbrush button_64x24_p skin 128 16 64 24 1.0 1.0 1.0 1.0
.addbrush button_96x24_n skin 192 16 96 24 1.0 1.0 1.0 1.0
.addbrush button_96x24_h skin 288 16 96 24 1.0 1.0 1.0 1.0
.addbrush button_96x24_p skin 384 16 96 24 1.0 1.0 1.0 1.0
.addbrush button_64x20_n skin 192 152 96 20 1.0 1.0 1.0 1.0
.addbrush button_64x20_h skin 288 152 96 20 1.0 1.0 1.0 1.0
.addbrush button_64x20_p skin 384 152 96 20 1.0 1.0 1.0 1.0
.addbrush menu_64x16_n skin 0 172 64 16 1.0 1.0 1.0 1.0
.addbrush menu_64x16_h skin 64 172 64 16 1.0 1.0 1.0 1.0
.addbrush menu_64x16_p skin 128 172 64 16 1.0 1.0 1.0 1.0
.addbrush menu_128x16_n skin 192 172 96 16 1.0 1.0 1.0 1.0
.addbrush menu_128x16_h skin 288 172 96 16 1.0 1.0 1.0 1.0
.addbrush menu_128x16_p skin 384 172 96 16 1.0 1.0 1.0 1.0
# list views
.addbrush list_background skin 446 72 8 8 1.0 1.0 1.0 1.0
.addbrush list_selection skin 64 172 64 16 1.0 1.0 1.0 1.0
# icons
.addbrush console_n skin 0 56 48 48 1.0 1.0 1.0 1.0
.addbrush console_p skin 0 56 48 48 0.5 0.5 0.5 1.0
.addbrush console_h skin 0 104 48 48 1.0 1.0 1.0 1.0
.addbrush texbrowser_n skin 48 56 48 48 1.0 1.0 1.0 1.0
.addbrush texbrowser_p skin 48 56 48 48 0.5 0.5 0.5 1.0
.addbrush texbrowser_h skin 48 104 48 48 1.0 1.0 1.0 1.0
.addbrush gfxbrowser_n skin 96 56 48 48 1.0 1.0 1.0 1.0
.addbrush gfxbrowser_p skin 96 56 48 48 0.5 0.5 0.5 1.0
.addbrush gfxbrowser_h skin 96 104 48 48 1.0 1.0 1.0 1.0
.addbrush dbgwindow_n skin 144 56 48 48 1.0 1.0 1.0 1.0
.addbrush dbgwindow_p skin 144 56 48 48 0.5 0.5 0.5 1.0
.addbrush dbgwindow_h skin 144 104 48 48 1.0 1.0 1.0 1.0
.addbrush syswindow_n skin 192 56 48 48 1.0 1.0 1.0 1.0
.addbrush syswindow_p skin 192 56 48 48 0.5 0.5 0.5 1.0
.addbrush syswindow_h skin 192 104 48 48 1.0 1.0 1.0 1.0
.addbrush contrwindow_n skin 336 56 48 48 1.0 1.0 1.0 1.0
.addbrush contrwindow_p skin 336 56 48 48 0.5 0.5 0.5 1.0
.addbrush contrwindow_h skin 336 104 48 48 1.0 1.0 1.0 1.0
.addbrush hidegui_n skin 240 56 48 48 1.0 1.0 1.0 1.0
.addbrush hidegui_p skin 240 56 48 48 0.5 0.5 0.5 1.0
.addbrush hidegui_h skin 240 104 48 48 1.0 1.0 1.0 1.0
.addbrush quit_n skin 288 56 48 48 1.0 1.0 1.0 1.0
.addbrush quit_p skin 288 56 48 48 0.5 0.5 0.5 1.0
.addbrush quit_h skin 288 104 48 48 1.0 1.0 1.0 1.0
.addbrush hardpoint_n skin 0 188 48 48 1.0 1.0 1.0 1.0
.addbrush hardpoint_p skin 0 188 48 48 0.5 0.5 0.5 1.0
.addbrush hardpoint_h skin 48 188 48 48 1.0 1.0 1.0 1.0
.addbrush savewindow_n skin 96 188 48 48 1.0 1.0 1.0 1.0
.addbrush savewindow_p skin 96 188 48 48 0.5 0.5 0.5 1.0
.addbrush savewindow_h skin 144 188 48 48 1.0 1.0 1.0 1.0
.addbrush charwindow_n skin 192 188 48 48 1.0 1.0 1.0 1.0
.addbrush charwindow_p skin 192 188 48 48 0.5 0.5 0.5 1.0
.addbrush charwindow_h skin 240 188 48 48 1.0 1.0 1.0 1.0
.addbrush disp_n skin 384 56 48 48 1.0 1.0 1.0 1.0
.addbrush disp_p skin 384 56 48 48 0.5 0.5 0.5 1.0
.addbrush disp_h skin 384 104 48 48 1.0 1.0 1.0 1.0
# the left and right logos
.addbrush n2logo n2logo 0 0 64 64 1.0 1.0 1.0 0.5
.addbrush leftlogo radonlabs 0 0 128 64 1.0 1.0 1.0 1.0
.addbrush rightlogo n2toolkit 0 0 64 64 1.0 1.0 1.0 1.0
# the color hexagon for the colorpicker
.addbrush colorhex colorhexagon 0 0 170 141 1.0 1.0 1.0 1.0
.endbrushes
#
# -添加画刷
#####################################################################
/sys/servers/gui.setsystemskin $skin
/sys/servers/gui.setskin $skin
# create the Nebula dock window
# 这个是Nebula2的系统工具栏
/sys/servers/gui.newwindow nguidockwindow true
sel $cwd
}
#-------------------------------------------------------------------------------
# OnGuiServerClose
#
# This procedure is called when the gui server is closed.
#-------------------------------------------------------------------------------
proc OnGuiServerClose {} {
#在这里添加关闭GuiServer,释放资源的代码
}
#-------------------------------------------------------------------------------
# OnT14MapInput
#
# This procedure is called by nviewer when input should be mapped.
#-------------------------------------------------------------------------------
proc OnT14MapInput {} {
set cwd [psel]
sel /sys/servers/input
.beginmap
.map "keyb0:esc.down" "script:/sys/servers/gui.togglesystemgui"
.endmap
sel $cwd
}
#-------------------------------------------------------------------------------
# UIEvent_ToggleBtn
#
# 在程序中向ToggleBtn控件注册的事件处理函数
#-------------------------------------------------------------------------------
proc UIEvent_ToggleBtn {} {
set cwd [psel]
sel /sys/servers/gui
.togglesystemgui
sel $cwd
}