游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4698|回复: 0

UI设计概要4:UI基件

[复制链接]

56

主题

94

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2010-10-20 18:22:00 | 显示全部楼层 |阅读模式
接上篇本文主要讨论盖莫游戏引擎中UI部分的控件基类

UI控件应该包括以下几个功能
1.按名生成
2.控件尺寸
3.控件大小
4.控件状态
5.对消息事件的处理
6.控件渲染
7.控件检测
8.控件文本
然后具体代码如下:
///////////////////////////////////////////////////////////
/// 定义UI控件基类
///////////////////////////////////////////////////////////
class GAPI UI_Widget : public SlotHolder,public UI_EventHandler,public Object
{
public:
    typedef std::list<UI_EventListener*> UIEventListener;
    typedef std::list<UI_EventListener*>::iterator UIEventListenerItr;
public:
    ////////////////////////////////////////////////////////
    /// 窗体构造和析构
    ////////////////////////////////////////////////////////
    explicit UI_Widget(UI_Widget* parent = NULL,const engine_wstring& text = L"widget");
    virtual ~UI_Widget();
public:
    ////////////////////////////////////////////////////////
    /// 获取,设置设置窗体文本
    ////////////////////////////////////////////////////////
    engine_wstring GetText()const{return text_;}
    void SetText(const engine_wstring& text);
public:
    ////////////////////////////////////////////////////////
    /// 窗体大小和尺寸
    ////////////////////////////////////////////////////////
    Size  GetSize()const;
    void  SetSize(const Size& size);
    Point GetPosition()const;
    void  SetPosition(const Point& point);

    ////////////////////////////////////////////////////////
    /// 获取窗体推荐大小
    ////////////////////////////////////////////////////////
    virtual Size GetRecommendedSize()const;
public:
    ////////////////////////////////////////////////////////
    /// 检测给定点下控件
    ////////////////////////////////////////////////////////
    UI_Widget* GetWidgetBelow(int x,int y);

    ////////////////////////////////////////////////////////
    /// 获取本控件的顶层控件
    ////////////////////////////////////////////////////////
    UI_Widget* GetTopWidget()const;

    ////////////////////////////////////////////////////////
    /// 获取,设置父窗体
    ////////////////////////////////////////////////////////
    UI_Widget* GetParentWidget()const;
    void  SetParentWidget(UI_Widget* parent);
public:
    ////////////////////////////////////////////////////////
    /// 设置,获取控件边框
    ////////////////////////////////////////////////////////
    void SetBorder(const RefPtr<UI_AbstractBorder>& border);
    RefPtr<UI_AbstractBorder> GetBorder()const;
    ////////////////////////////////////////////////////////
    /// 设置,获取是否渲染边框
    ////////////////////////////////////////////////////////
    void SetBorderVisible(bool visible);
    bool IsBorderVisible();
public:
    ////////////////////////////////////////////////////////
    /// 增加,移除事件消息
    ////////////////////////////////////////////////////////
    void AddEventListener(UI_EventListener* listener);
    void RemoveEventListener(UI_EventListener* listener);
    ////////////////////////////////////////////////////////
    /// 消息处理
    ////////////////////////////////////////////////////////
    virtual bool Process(const UI_Event& event);
public:
    ////////////////////////////////////////////////////////
    /// 焦点函数
    ////////////////////////////////////////////////////////
    bool IsFocusOn()const;
    void SetFocusOn(bool focus);
    void ChangedFocusOn();

    ////////////////////////////////////////////////////////
    /// 设置,检测是否为活动控件
    ////////////////////////////////////////////////////////
    void SetAsActiveWidget();
    bool IsActiveWidget()const;

    ////////////////////////////////////////////////////////
    /// 检测控件层次关系
    ////////////////////////////////////////////////////////
    virtual bool DoesHierarchyContain(UI_Widget* widget)const;
public:
    ////////////////////////////////////////////////////////
    /// 显示,隐藏窗体
    ////////////////////////////////////////////////////////
    void SetVisible(bool visible);
    void Show();
    void Hide();
    bool IsVisible()const;

    ////////////////////////////////////////////////////////
    /// 窗体状态函数
    ////////////////////////////////////////////////////////
    void IsEnable(){enabled_ = true;}
    void Disable(){enabled_ = false;}
    bool IsEnabled()const{return enabled_;}
public:
    ////////////////////////////////////////////////////////
    /// 窗体渲染
    ////////////////////////////////////////////////////////
    void Render();

    ////////////////////////////////////////////////////////
    /// 当窗体关闭的时候发射信号
    ////////////////////////////////////////////////////////
    Signal0 closed_;

     ////////////////////////////////////////////////////////
    /// 窗体数据载入
    ////////////////////////////////////////////////////////
    virtual bool Load(const engine_string& file,const engine_string& widget);
protected:
    ////////////////////////////////////////////////////////
    /// 执行函数
    ////////////////////////////////////////////////////////
    virtual void OnResize(){}
    virtual void OnMove(){}
    virtual void OnTextChanged(){}
    virtual void OnShow(){}
    virtual void OnHide(){}
    virtual void OnGainedFocus(){}
    virtual void OnLostFocus(){}
    virtual void OnDraw(){}
private:
    UI_Widget*         parent_;
    UIEventListener    message_listeners_;
    engine_wstring     text_;
    Size               size_;
    Point              position_;
    bool               visible_;
    bool               enabled_;
    RefPtr<UI_AbstractBorder> border_;
    bool               border_visible_;
private:
    DECLARE_OBJECT(UI_Widget)
};

}

#ifdef G_COMPILER_MSVC
#pragma warning(pop)
#endif

需要说明的就是这里有3个父类
一个是Object
一个是SlotHolder这是消息桩
另外一个是UI_EventHandler负责对事件的处理

需要说明的是UI_Widget总是和UI_WidgetManager配合使用的
毕竟UI上下文最大只能有一个活动控件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-4-28 21:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表