CEGUI右击快捷菜单

我使用的是CEGUI 0.7.1、VS2008、lua5.1
下面是基于CEGUISample下面的Sample_Demo8改编的,使用的是自己写的脚本图层和脚本,以及在TaharezLook.scheme文件中加了一个自己的imageset元素(自己定义了一个imageset的窗口背景图像集),下面说一下详细步骤,以及将我遇到的问题总结一下

1.使用CEGUI的Layout编辑器编辑出一个右击菜单(也可以自己一个字母一个字母的敲出来,毕竟是xml文件)下面是自定义窗口的layout xml代码,右击菜单窗口在也其中

View Code
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <GUILayout >
4 <Window Type="DefaultWindow" Name="root" >
5 <!--------------背景窗口-------------->
6 <Window Type="TaharezLook/StaticImage" Name="root/background" >
7 <Property Name="UnifiedPosition" Value="{{0,0},{0,0}}" />
8 <Property Name="UnifiedSize" Value="{{1,0},{1,0}}" />
9 <Property Name="FrameEnabled" Value="false" />
10 <Property Name="Image" Value="set:Background1 image:bg" />
11 <Event Name="MouseClick" Function="lua_onBackgroundClick" />
12 </Window>
13
14 <!--------------右击菜单窗口-------------->
15 <Window Type="TaharezLook/StaticImage" Name="root/shortcut" >
16 <Property Name="Alpha" Value="0" />
17 <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{0,160},{0,100}}" />
18 <Window Type="TaharezLook/Button" Name="root/shortcut/but1" >
19 <Property Name="InheritsAlpha" Value="False" />
20 <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{1,0},{0.2,0}}" />
21 <Property Name="Text" Value="button1" />
22 <Event Name="Clicked" Function="lua_onBut1Click" />
23 </Window>
24 <Window Type="TaharezLook/Button" Name="root/shortcut/but2" >
25 <Property Name="InheritsAlpha" Value="False" />
26 <Property Name="UnifiedAreaRect" Value="{{0,0},{0.2,0},{1,0},{0.4,0}}" />
27 <Property Name="Text" Value="button2" />
28 </Window>
29 <Window Type="TaharezLook/Button" Name="root/shortcut/but3" >
30 <Property Name="Alpha" Value="0.5" />
31 <Property Name="InheritsAlpha" Value="False" />
32 <Property Name="UnifiedAreaRect" Value="{{0,0},{0.4,0},{1,0},{0.6,0}}" />
33 <Property Name="Text" Value="button3" />
34 </Window>
35 <Window Type="TaharezLook/Button" Name="root/shortcut/but4" >
36 <Property Name="Alpha" Value="0.5" />
37 <Property Name="InheritsAlpha" Value="False" />
38 <Property Name="UnifiedAreaRect" Value="{{0,0},{0.6,0},{1,0},{0.8,0}}" />
39 <Property Name="Text" Value="button4" />
40 </Window>
41 <Window Type="TaharezLook/Button" Name="root/shortcut/but5" >
42 <Property Name="Alpha" Value="0.5" />
43 <Property Name="InheritsAlpha" Value="False" />
44 <Property Name="UnifiedAreaRect" Value="{{0,0},{0.8,0},{1,0},{1,0}}" />
45 <Property Name="Text" Value="button5" />
46 </Window>
47 </Window>
48 </Window>
49 </GUILayout>


2.(这一步实际是在上一步之前做的)写一个窗口背景需要的imageset下面,在CEGUI的\datafiles\imagesets添加一个Background1.imageset文件,再在这个目录下面添加一个名为background1.jpg的图片文件,Background1.imageset的内容:

View Code
1 <?xml version="1.0" ?>
2 <Imageset Name="Background1" Imagefile="background1.jpg" NativeHorzRes="1000" NativeVertRes="651" AutoScaled="true">
3 <Image Name="bg" XPos="0" YPos="0" Width="1000" Height="651" />
4 </Imageset>

3.添加一个自己的scheme文件,将自定义的Background1.imageset元素包含进去(我就将\datafiles\schemes下的TaharezLook.scheme复制一份,然后改名为myWindow.scheme.xml,并添加Background1.imageset)下面是部分代码(其它部分都和TaharezLook.scheme内容一样)
<?xml version="1.0" ?>
<GUIScheme Name="TaharezLook">
<Imageset Filename="TaharezLook.imageset" />
<Imageset Filename="Background1.imageset" /> <!--这个地方添加自己的窗口背景imageset-->
<Font Filename="DejaVuSans-10.font" />
<LookNFeel Filename="TaharezLook.looknfeel" />
<WindowRendererSet Filename="CEGUIFalagardWRBase" />

4.下面编写自己的lua脚本,参照demo8的脚本,放在\datafiles\lua_scripts目录下面,命名为:myWindow.lua,下面贴上脚本内容
View Code
-- but1按钮事件
--
function lua_onBut1Click(args)
print("but1被按")
end

-- 窗口背景右击事件
--
function lua_onBackgroundClick(args)
local event = CEGUI.toMouseEventArgs(args)
if (event.button == CEGUI.RightButton ) then
local pos = CEGUI.UVector2(CEGUI.UDim(0, event.position.x), CEGUI.UDim(0, event.position.y))
local shortcut = CEGUI.WindowManager:getSingleton():getWindow("root/shortcut")
shortcut:setPosition(pos)
shortcut:show()
shortcut:activate()
end

end

local guiSys = CEGUI.System:getSingleton()
local schemeMgr = CEGUI.SchemeManager:getSingleton()
local winMgr = CEGUI.WindowManager:getSingleton()

schemeMgr:create(
"myWindow.scheme.xml")
local root = winMgr:loadWindowLayout("myWindow2.layout.xml")
winMgr:getWindow(
"root/shortcut"):hide()
guiSys:setGUISheet(root)
guiSys:setDefaultMouseCursor(
"TaharezLook", "MouseArrow")


小结一下:使用CEGUI写一个窗口,就是在xml文件里面写一下窗口的布局,并将每个窗口及子窗口的属性和相关事件定义一下,事件的响应函数对应为lua脚本的处理函数,然后在lua脚本中处理窗口来的消息,这个地方,我处理了鼠标右击的消息,要将窗口消息转换成"鼠标消息",然后在鼠标点击的地方将事先制作好的"右击窗口菜单显示出来",在这个地方注意一下,鼠标点击事件有一个属性是position,它的类型是Vector2(又叫Point),而设置窗口显示的位置setPosition(UVector2)所要求的参数是UVector2类型,Vector2和UVector2类型不可以直接转换,参看我上面的代码转换方式;再者就是imageset,可以使用多个imageset,字体也是一样,这个地方我就是自己定义了一个imageset用来做窗口的背景,imageset里面指定的图片不一定要使用CEGUI demo里面的tga文件,也可以使用像jpg、bmp之类的文件;
使用CEGUI做界面方便的地方就是,像Demo8那样,就指定了一个lua脚本的入口,生成项目的可执行文件:Demo8.exe,后面想改变窗口及窗口的事件,就可以在lua脚本中完成,只要lua脚本的名称不变,照样使用Demo8.exe这个可执行文件来启动新的窗体

posted @ 2011-05-15 10:11  莫压枯枯地  阅读(899)  评论(0编辑  收藏  举报