📄 asl_guiobj.h
字号:
//-----------------------------------------------------------------------------
//
// ____ Azure Star Game Engine 蓝星游戏引擎 ____
//
// Copyright (c) 2006, 蓝星工作室
// All rights reserved.
//
// 文件名称: asl_guiobj.h
// 摘 要: 各种GUI控件类定义
//
// 当前版本: 1.0
// 作 者: 汤 祺
// 创建日期: 2006-8-12
//
//-----------------------------------------------------------------------------
#ifndef ASL_GUIOBJ_INCLUDE
#define ASL_GUIOBJ_INCLUDE
#pragma once
#include "asl_utils.h"
#include "asl_font.h"
#include "asl_input.h"
#include <vector>
#include <list>
#include <string>
//-----------------------------------------------------------------------------
namespace ASL
{
//-----------------------------------------------------------------------------
// 类的前向声明
//-----------------------------------------------------------------------------
class ASLControl;
//-----------------------------------------------------------------------------
// 事件处理函数及包裹结构
//-----------------------------------------------------------------------------
typedef void (ASLControl::*NotifyFun)(void);
struct NotifyEvent
{
NotifyEvent() : function(NULL), invoker(NULL) {}
NotifyFun function;
ASLControl *invoker;
};
typedef void (ASLControl::*MouseMoveFun)(POINT pt);
struct MouseMoveEvent
{
MouseMoveEvent() : function(NULL), invoker(NULL) {}
MouseMoveFun function;
ASLControl *invoker;
};
typedef void (ASLControl::*MouseFun)(POINT pt, MouseButton mb);
struct MouseEvent
{
MouseEvent() : function(NULL), invoker(NULL) {}
MouseFun function;
ASLControl *invoker;
};
typedef void (ASLControl::*KeyFun)(DWORD dwKey, ShiftState ss);
struct KeyEvent
{
KeyEvent() : function(NULL), invoker(NULL) {}
KeyFun function;
ASLControl *invoker;
};
typedef void (ASLControl::*KeyPressFun)(char cKey);
struct KeyPressEvent
{
KeyPressEvent() : function(NULL), invoker(NULL) {}
KeyPressFun function;
ASLControl *invoker;
};
//-----------------------------------------------------------------------------
// 用户通过使用这组宏将自定义事件处理函数与控件捆绑起来.
//-----------------------------------------------------------------------------
#define ON_MOUSEENTER(obj, fun) \
{ \
(obj)->OnMouseEnter.function = NotifyFun(fun); \
(obj)->OnMouseEnter.invoker = this; \
}
#define ON_MOUSELEAVE(obj, fun) \
{ \
(obj)->OnMouseLeave.function = NotifyFun(fun); \
(obj)->OnMouseLeave.invoker = this; \
}
#define ON_MOUSEMOVE(obj, fun) \
{ \
(obj)->OnMouseMove.function = MouseMoveFun(fun); \
(obj)->OnMouseMove.invoker = this; \
}
#define ON_MOUSEDOWN(obj, fun) \
{ \
(obj)->OnMouseDown.function = MouseFun(fun); \
(obj)->OnMouseDown.invoker = this; \
}
#define ON_MOUSEUP(obj, fun) \
{ \
(obj)->OnMouseUp.function = MouseFun(fun); \
(obj)->OnMouseUp.invoker = this; \
}
#define ON_KEYDOWN(obj, fun) \
{ \
(obj)->OnKeyDown.function = KeyFun(fun); \
(obj)->OnKeyDown.invoker = this; \
}
#define ON_KEYUP(obj, fun) \
{ \
(obj)->OnKeyUp.function = KeyFun(fun); \
(obj)->OnKeyUp.invoker = this; \
}
#define ON_KEYPRESS(obj, fun) \
{ \
(obj)->OnKeyPress.function = KeyPressFun(fun); \
(obj)->OnKeyPress.invoker = this; \
}
#define ON_ENTER(obj, fun) \
{ \
(obj)->OnEnter.function = NotifyFun(fun); \
(obj)->OnEnter.invoker = this; \
}
#define ON_LEAVE(obj, fun) \
{ \
(obj)->OnLeave.function = NotifyFun(fun); \
(obj)->OnLeave.invoker = this; \
}
#define ON_CLICK(obj, fun) \
{ \
(obj)->OnClick.function = NotifyFun(fun); \
(obj)->OnClick.invoker = this; \
}
#define ON_CHANGE(obj, fun) \
{ \
(obj)->OnChange.function = NotifyFun(fun); \
(obj)->OnChange.invoker = this; \
}
#define ON_SCROLL(obj, fun) \
{ \
(obj)->OnScroll.function = NotifyFun(fun); \
(obj)->OnScroll.invoker = this; \
}
//-----------------------------------------------------------------------------
// 类名: ASLControl
// 功能: 控件类定义
// 本类是所有控件的基类, 提供的抽象接口被ASLGui类调用, 通过与该类的合作
// 实现整个GUI系统.
// 本类的核心是提供了两个级别的事件处理能力. 第一级是控件级, 本类提供了
// 一组事件处理虚函数, 当事件发生时会被ASLGui类调用. 各子类可以根据需要
// 改写这些函数, 以实现各自的功能; 第二级是用户级, 本类提供用户处理事件
// 的函数指针. 用户可以自己撰写事件处理函数, 同时把函数地址赋给控件的函
// 指针. 各控件在处理完实现自身功能的事件后, 将会调用用户的事件处理函数.
//-----------------------------------------------------------------------------
class ASLControl
{
// 构造, 析构函数
public:
ASLControl(void);
virtual ~ASLControl(void);
// 禁用拷贝构造函数和赋值函数, 无实现
private:
ASLControl(const ASLControl&);
ASLControl& operator=(const ASLControl&);
// 公有方法
public:
// 绘图函数, 子类必须改写
virtual void Draw(void) const = 0;
// 更新函数
virtual void Update(float fDelta) {}
// 是否可以获得焦点. 获得焦点的控件可以处理键盘事件
virtual bool CanHaveFocus() const { return true; }
// 事件处理函数, 子类可根据需要改写, 但必须在改写函数中调用本类的同名函数
// 所传入的鼠标指针位置是相对于程序窗口的.
public:
// 鼠标进入
virtual void MouseEnter(void);
// 鼠标离开
virtual void MouseLeave(void);
// 鼠标移动
virtual void MouseMove(POINT pt);
// 鼠标按下
virtual void MouseDown(POINT pt, MouseButton mb);
// 鼠标弹起
virtual void MouseUp(POINT pt, MouseButton mb);
// 键盘按下
virtual void KeyDown(DWORD dwKey, ShiftState ss);
// 键盘弹起
virtual void KeyUp(DWORD dwKey, ShiftState ss);
// 字符按下
virtual void KeyPress(char cKey);
// 获得焦点
virtual void Enter(void);
// 失去焦点
virtual void Leave(void);
// 非虚函数, 提供公共的操作
public:
// 创建控件, 在ASLGui类的唯一实例中注册本控件
void Create(void);
// 设置父控件
void SetParent(ASLControl *pParent);
// 添加子控件
void AddChild(ASLControl* pChild);
// 删除子控件
void DelChild(ASLControl* pChild);
// 使控件有效
void Enable(void);
// 使控件无效
void Disable(void);
// 显示控件
void Show(void);
// 隐藏控件
void Hide(void);
// 控件坐标转屏幕(程序窗口)坐标
void ClientToScreen(POINT &pt) const;
// 屏幕(程序窗口)坐标转控件坐标
void ScreenToClient(POINT &pt) const;
// 一点是否在控件内
bool IsPointIn(POINT pt) const;
// 使控件获得焦点
void SetFocus(void);
// 控件是否有效
bool IsEnabled(void) const { return m_bEnabled; }
// 控件是否可见
bool IsVisible(void) const { return m_bVisible; }
// 控件是否获得焦点
bool IsFocused(void) const { return m_bFocused; }
// 设置控件距父控件左侧距离
void SetLeft(int nLeft) { m_nLeft = nLeft; }
// 设置控件距父控件顶端距离
void SetTop(int nTop) { m_nTop = nTop; }
// 取控件距父控件左侧距离
int GetLeft(void) const { return m_nLeft; }
// 取控件距父控件顶端距离
int GetTop(void) const { return m_nTop; }
// 设置控件宽度
void SetWidth(int nWidth) { m_nWidth = nWidth; }
// 设置控件高度
void SetHeight(int nHeight) { m_nHeight = nHeight; }
// 取控件宽度
int GetWidth(void) const { return m_nWidth; }
// 取控件高度
int GetHeight(void) const { return m_nHeight; }
// 设置控件字体
void SetFont(ASLFont &fnt) { m_pFont = &fnt; }
// 设置控件字体颜色
void SetFontColor(COLOR cl) { m_clFontColor = cl; }
// 取控件字体
ASLFont* GetFont(void) const { return m_pFont; }
// 取控件字体颜色
COLOR GetFontColor(void) const { return m_clFontColor; }
// 设置控件标题
void SetCaption(LPCSTR szCaption) { m_strCaption = szCaption; }
// 取控件标题
LPCSTR GetCaption(void) const { return m_strCaption.c_str(); }
// 用户自定义的事件处理函数指针
// 所传入的鼠标指针位置是相对于父控件的.
public:
NotifyEvent OnMouseEnter;
NotifyEvent OnMouseLeave;
MouseMoveEvent OnMouseMove;
MouseEvent OnMouseDown;
MouseEvent OnMouseUp;
KeyEvent OnKeyDown;
KeyEvent OnKeyUp;
KeyPressEvent OnKeyPress;
NotifyEvent OnEnter;
NotifyEvent OnLeave;
// 控件属性
protected:
ASLControl* m_pParent; // 父控件指针
int m_nLeft; // 距父控件左侧距离
int m_nTop; // 距父控件顶端距离
int m_nWidth; // 宽度
int m_nHeight; // 高度
bool m_bEnabled; // 是否有效
bool m_bVisible; // 是否可见
bool m_bFocused; // 是否获得焦点
ASLFont* m_pFont; // 字体
COLOR m_clFontColor; // 字体颜色
std::string m_strCaption; // 标题
std::list<ASLControl*> m_lChildren; // 子控件链表
public:
int Tag; // 用于用户自定义数据
}; // ASLControl 类定义结束
//-----------------------------------------------------------------------------
// 类名: ASLWinControl
// 功能: 窗体控件类
// 本类是面板类(ASLPanel)和窗体类(ASLForm)的纯虚基类, 提供了设置和使用
// 背景图片的能力.
// 本类主要作用是作为一个控件容器. 对象的所有子控件将共享对象的有效性和
// 可见性. 由于子控件使用相对于父类的坐标系, 所有子控件将随其父控件一起
// 移动而保持相对位置不变.
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -