分析CEGUI的最小资源定制

分析CEGUI的最小资源定制

本文针对只创建一个按钮(PushButton)的情况,分析CEGUI资源目录datafiles下最少可以放些什 么,并加以解释。
[sihan@localhost datafiles]$ tree
.
|-- fonts
|   |-- Commonv2c.ttf
|   |-- Commonwealth-10.font
|   `-- Font.xsd
|-- imagesets
|   |-- Imageset.xsd
|   |-- TaharezLook.imageset
|   `-- TaharezLook.tga
|-- looknfeel
|   |-- Falagard.xsd
|   `-- TaharezLook.looknfeel
`-- schemes
    |-- GUIScheme.xsd
    `-- TaharezLook.scheme
CEGUI资源文件
如下是资源类的一个列表,这四个是必须用到的:
CEGUI::Imageset                 - Imageset xml and texture image files.
CEGUI::Font                     - Font xml and ttf font files.
CEGUI::Scheme                   - Scheme xml files.
CEGUI::WidgetLookManager        - LookNFeel xml files

这两个实在这次没有用到的:
CEGUI::WindowManager            - Window layout xml files.
CEGUI::ScriptModule             - Script files in whichever scripted langauge.
  • 图片集Imageset
    Imageset只是原图形/纹理文件某个区域的定义(Imageset定义时将会指定区域)。每个定义的区域都有一个唯一的名字,并且在系统内被 认为是一个Image。Image是Imageset中一个基本的元素。如果想要更改CEGUI画出的图形,可以更改原图形/纹理文件,或者更改 image的定义区。
  • 字体 Font
    可以定义两种类型的字体供CEGUI使用:
                动态字体    基于true-type(.ttf)的字体文件
                静态字体    一个位图字体,这种字体基于一个定义了Images的Imageset。
  • Scheme
    Schema是把零碎的数据文件组合起来的一种方法。 它也是装载和注册widget类型的最简便的方法。一个Schema可以包含以下一个或者多个类型的定义(这些文件描述的资源可以在schema被装载时 装载)。
                Imageset
                Font
                Window Set
                Window Alias

    Imageset和font在前面已经提到过。一个Window Set指定了可装载模块(.dll/.so, 等等)的名字。
    Widgets集包含了你希望注册的模块集。(注,根据网上的资料,widgets类似于windows的themes)
    Window或者widget类型可以有多个名字,Windows Alias提供了这一机制。我们也可以通过这种机制使一个widget"隐藏"另一个已经注册了的widget。
  • 布局layout
    布局文件包含了一个窗口布局的信息,它也是xml格式的。其中每个'window'元素定义了被创建的window或者widet,'Property' 元素定义每个window的属性值。
下面看看这些文件的内容:
TaharezLook.scheme:
<?xml version="1.0" ?>
<GUIScheme Name="TaharezLook">
        <Imageset Name="TaharezLook" Filename="TaharezLook.imageset" />
        <Font Name="Commonwealth-10" Filename="Commonwealth-10.font" />
        <LookNFeel Filename="TaharezLook.looknfeel" />
        <WindowRendererSet Filename="CEGUIFalagardWRBase" />
        <FalagardMapping WindowType="TaharezLook/Button"      TargetType="CEGUI/PushButton"  Renderer="Falagard/Button"       LookNFeel="TaharezLook/Button" />
</GUIScheme>

TaharezLook.looknfeel:
<?xml version="1.0" ?>
<Falagard>
    <!--
    ***************************************************
        TaharezLook/Button
    ***************************************************
    -->
    <WidgetLook name="TaharezLook/Button">
        <PropertyDefinition name="NormalTextColour" initialValue="FFFFFFFF" redrawOnWrite="true" />
        <PropertyDefinition name="HoverTextColour" initialValue="FFFFFFFF" redrawOnWrite="true" />
        <PropertyDefinition name="PushedTextColour" initialValue="FFFFFFFF" redrawOnWrite="true" />
        <PropertyDefinition name="DisabledTextColour" initialValue="FF7F7F7F" redrawOnWrite="true" />
        <PropertyDefinition name="VertLabelFormatting" initialValue="CentreAligned" />
        <PropertyDefinition name="HorzLabelFormatting" initialValue="CentreAligned" />
        <PropertyDefinition name="NormalImage" initialValue="" redrawOnWrite="true" />
        <PropertyDefinition name="HoverImage" initialValue="" redrawOnWrite="true" />
        <PropertyDefinition name="PushedImage" initialValue="" redrawOnWrite="true" />
        <PropertyDefinition name="DisabledImage" initialValue="" redrawOnWrite="true" />
        <PropertyDefinition name="VertImageFormatting" initialValue="Stretched" redrawOnWrite="true" />
        <PropertyDefinition name="HorzImageFormatting" initialValue="Stretched" redrawOnWrite="true" />
        <ImagerySection name="normal">
            <FrameComponent>
                <Area>
                    <Dim type="LeftEdge"><AbsoluteDim value="0" /></Dim>
                    <Dim type="TopEdge"><AbsoluteDim value="0" /></Dim>
                    <Dim type="Width"><UnifiedDim scale="1" type="Width" /></Dim>
                    <Dim type="Height"><UnifiedDim scale="1" type="Height" /></Dim>
                </Area>
                <Image type="LeftEdge" imageset="TaharezLook" image="ButtonLeftNormal" />
                <Image type="RightEdge" imageset="TaharezLook" image="ButtonRightNormal" />
                <Image type="Background" imageset="TaharezLook" image="ButtonMiddleNormal" />
            </FrameComponent>
            <ImageryComponent>
                <Area>
                    <Dim type="LeftEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="TopEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="Width" ><UnifiedDim scale="1" type="Width" /></Dim>
                    <Dim type="Height" ><UnifiedDim scale="1" type="Height" /></Dim>
                </Area>
                <ImageProperty name="NormalImage" />
                <VertFormatProperty name="VertImageFormatting" />
                <HorzFormatProperty name="HorzImageFormatting" />
            </ImageryComponent>
        </ImagerySection>
        <ImagerySection name="hover">
            <FrameComponent>
                <Area>
                    <Dim type="LeftEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="TopEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="Width" ><UnifiedDim scale="1" type="Width" /></Dim>
                    <Dim type="Height" ><UnifiedDim scale="1" type="Height" /></Dim>
                </Area>
                <Image type="LeftEdge" imageset="TaharezLook" image="ButtonLeftHighlight" />
                <Image type="RightEdge" imageset="TaharezLook" image="ButtonRightHighlight" />
                <Image type="Background" imageset="TaharezLook" image="ButtonMiddleHighlight" />
            </FrameComponent>
            <ImageryComponent>
                <Area>
                    <Dim type="LeftEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="TopEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="Width" ><UnifiedDim scale="1" type="Width" /></Dim>
                    <Dim type="Height" ><UnifiedDim scale="1" type="Height" /></Dim>
                </Area>
                <ImageProperty name="HoverImage" />
                <VertFormatProperty name="VertImageFormatting" />
                <HorzFormatProperty name="HorzImageFormatting" />
            </ImageryComponent>
        </ImagerySection>
        <ImagerySection name="pushed">
            <FrameComponent>
                <Area>
                    <Dim type="LeftEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="TopEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="Width" ><UnifiedDim scale="1" type="Width" /></Dim>
                    <Dim type="Height" ><UnifiedDim scale="1" type="Height" /></Dim>
                </Area>
                <Image type="LeftEdge" imageset="TaharezLook" image="ButtonLeftPushed" />
                <Image type="RightEdge" imageset="TaharezLook" image="ButtonRightPushed" />
                <Image type="Background" imageset="TaharezLook" image="ButtonMiddlePushed" />
            </FrameComponent>
            <ImageryComponent>
                <Area>
                    <Dim type="LeftEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="TopEdge" ><AbsoluteDim value="0" /></Dim>
                    <Dim type="Width" ><UnifiedDim scale="1" type="Width" /></Dim>
                    <Dim type="Height" ><UnifiedDim scale="1" type="Height" /></Dim>
                </Area>
                <ImageProperty name="PushedImage" />
                <VertFormatProperty name="VertImageFormatting" />
                <HorzFormatProperty name="HorzImageFormatting" />
            </ImageryComponent>
        </ImagerySection>
        <StateImagery name="Normal">
            <Layer>
                <Section section="normal" />
                <Section section="label">
                    <ColourProperty name="NormalTextColour" />
                </Section>
            </Layer>
        </StateImagery>
        <StateImagery name="Hover">
            <Layer>
                <Section section="hover" />
                <Section section="label">
        <ColourProperty name="HoverTextColour" />
        </Section>
            </Layer>
        </StateImagery>
        <StateImagery name="Pushed">
            <Layer>
                <Section section="pushed" />
                <Section section="label">
                    <ColourProperty name="PushedTextColour" />
                </Section>
            </Layer>
        </StateImagery>
        <StateImagery name="PushedOff">
            <Layer>
                <Section section="hover" />
                <Section section="label">
                    <ColourProperty name="HoverTextColour" />
                </Section>
            </Layer>
        </StateImagery>
        <StateImagery name="Disabled">
            <Layer>
                <Section section="normal">
                    <Colours topLeft="FF7F7F7F" topRight="FF7F7F7F" bottomLeft="FF7F7F7F" bottomRight="FF7F7F7F" />
                </Section>
                <Section section="label">
                    <ColourProperty name="DisabledTextColour" />
                </Section>
            </Layer>
        </StateImagery>
    </WidgetLook>
</Falagard>

Commonwealth-10.font:
<?xml version="1.0" ?>
<Font Name="Commonwealth-10" Filename="Commonv2c.ttf" Type="FreeType" Size="10" NativeHorzRes="800" NativeVertRes="600" AutoScaled="true"/>

TaharezLook.imageset:
<?xml version="1.0" ?>
<Imageset Name="TaharezLook" Imagefile="TaharezLook.tga" NativeHorzRes="800" NativeVertRes="600" AutoScaled="true">
    <Image Name="ClientBrush" XPos="2" YPos="2" Width="64" Height="64" />
    <Image Name="GenericBrush" XPos="9" YPos="71" Width="1" Height="1" />
        <Image Name="ButtonLeftNormal" XPos="68" YPos="20" Width="12" Height="16" />
        <Image Name="ButtonMiddleNormal" XPos="82" YPos="20" Width="12" Height="16" />
        <Image Name="ButtonRightNormal" XPos="96" YPos="20" Width="12" Height="16" />
        <Image Name="ButtonLeftPushed" XPos="68" YPos="38" Width="12" Height="16" />
        <Image Name="ButtonMiddlePushed" XPos="82" YPos="38" Width="12" Height="16" />
        <Image Name="ButtonRightPushed" XPos="96" YPos="38" Width="12" Height="16" />
        <Image Name="ButtonLeftHighlight" XPos="68" YPos="56" Width="12" Height="16" />
        <Image Name="ButtonMiddleHighlight" XPos="82" YPos="56" Width="12" Height="16" />
        <Image Name="ButtonRightHighlight" XPos="96" YPos="56" Width="12" Height="16" />
        <Image Name="MouseArrow" XPos="138" YPos="127" Width="31" Height="25" XOffset="0" YOffset="0" />
</Imageset>
代码
glut_loop.cpp:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <CEGUI.h>
#include <CEGUIDefaultResourceProvider.h>
#include <RendererModules/OpenGLGUIRenderer/openglrenderer.h>

#define DATA_FILES "./datafiles/"

void render(void);
void resource_group(void);
void load_data_files(void);
void create_window(void);

int window_id;
bool keep_running = true;

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
   
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(640, 480);
   
    window_id = glutCreateWindow("GLUT Loop");
    glutSetCursor(GLUT_CURSOR_NONE);

    GLenum err = glewInit();
    CEGUI::OpenGLRenderer* myRenderer = new CEGUI::OpenGLRenderer(0);
    new CEGUI::System(myRenderer);
   
    resource_group();
    load_data_files();
    create_window();

    while (keep_running)
    {
        glutMainLoopEvent();
        render();
        CEGUI::System::getSingleton().renderGUI();
        glutSwapBuffers();
    }

    glutDestroyWindow(window_id);
    return 0;
}

void render(void)
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void resource_group(void)
{
    CEGUI::DefaultResourceProvider *rp = static_cast<CEGUI::DefaultResourceProvider*>
        (CEGUI::System::getSingleton().getResourceProvider());

    rp->setResourceGroupDirectory("schemes", DATA_FILES "schemes/");
    rp->setResourceGroupDirectory("imagesets", DATA_FILES "imagesets/");
    rp->setResourceGroupDirectory("fonts", DATA_FILES "fonts/");
    rp->setResourceGroupDirectory("looknfeels", DATA_FILES "looknfeel/");

    CEGUI::Imageset::setDefaultResourceGroup("imagesets");
    CEGUI::Font::setDefaultResourceGroup("fonts");
    CEGUI::Scheme::setDefaultResourceGroup("schemes");
    CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");
}

void load_data_files(void)
{
    CEGUI::SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
    if (!CEGUI::FontManager::getSingleton().isFontPresent("Commonwealth-10"))
        CEGUI::FontManager::getSingleton().createFont("Commonwealth-10");

    CEGUI::System::getSingleton().setDefaultFont("Commonwealth-10");
    CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
}

void create_window(void)
{
    using namespace CEGUI;

    WindowManager &wmgr = WindowManager::getSingleton();

    Window* myRoot = wmgr.createWindow("DefaultWindow", "root");
    System::getSingleton().setGUISheet(myRoot);

    // 创建PushButton
    PushButton *btn = (PushButton*)wmgr.createWindow("TaharezLook/Button", "testButton");
    myRoot->addChildWindow(btn);
   
    // 设置位置,大小   
    btn->setPosition( UVector2( UDim(0.25f, 0), UDim(0.25f, 0) ) );
    btn->setSize( UVector2( UDim(0, 60), UDim(0, 24) ) );
    btn->setText("OK");
}

Makefile:
glut_loop: glut_loop.o
    g++ glut_loop.o -lglut -L/usr/local/lib/ -lCEGUIBase -lCEGUIOpenGLRenderer -o glut_loop

glut_loop.o: glut_loop.cpp
    g++ -g -c glut_loop.cpp -I/usr/local/include/CEGUI

.PHONY: clean
clean:
    rm -rf *.o glut_loop

 

你可能感兴趣的:(xml,image,Scheme,System,layer,fonts)