<采用了单继承的类的导出> 这个……,tolua++支持采用了单继承的类的直接导出,在lua中可以像在C++中那样访问基类的方法。和其它简单类的导出没什么区别。 只是个简单的示例,我们定义一个控件基类,从它派生一个按钮类。然后在lua中分别访问基类和按钮类的方法。我们导出一个全局变量lbutton,同时也在lua中生成一个新button。
先看实际的头文件inheritance.h,我把实现也写在了头文件里。
g_button的实例定义在main函数所在的文件中。
下面是inheritance.pkg文件:
对于基类CAUIControl,只导出部分方法,不导出构造函数,不允许在Lua中直接生成其实例。派生类CAUIButton可以在lua中生成实 例。CAUIButton重写了基类的SetAlpha函数也增加了一些新的函数,如设置纹理函数SetTexture。 全局变量的导出很简单,extern CAUIButton g_button@lbutton;一个语句就可以了。我们还可以为其加上tolua_readonly修饰符。我把它重名为lbutton。
好了,下面用tolua++.exe生成inherit.cpp文件:
tolua++.exe
-n inherit
-o inherit.cpp inheritance.pkg
接下来是驱动文件inheritance.cpp:
相当简单,和前面几个几乎一样,唯一变化的是多了个全局变量。
先看实际的头文件inheritance.h,我把实现也写在了头文件里。
#ifndef _CLASS_INHERITANCE_H 
#define _CLASS_INHERITANCE_H
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>
typedef enum
.{
AUICSNormal = 0,
AUICSHover = 1,
AUICSPushed = 2,
AUICSDisabled = 3,
AUICSHide = 4,
AUICSFORCEDOWRD = 0xFFFFFFFF
} AUIControlState;

class CAUIControl
...
{
public:
//should not be called from lua scripts
CAUIControl()
:m_nID(-1), m_state(AUICSNormal), m_bVisible(true),
m_bEnable(true), m_fAlpha(0.0f),m_strText("") {}
virtual ~CAUIControl()...{}
public:
//tolua
void SetID(int nID)...{ m_nID = nID; }
int GetID()...{ return m_nID; }
void SetText(char * szText)...{ m_strText = szText; }
const char * GetText()...{ return m_strText.c_str(); }
void SetPosition(POINT pt)...{ m_position = pt; }
POINT GetPosition()...{ return m_position; }
void SetSize(SIZE sz)...{ m_size = sz; }
SIZE GetSize()...{ return m_size; }
void SetVisible(bool bVisible)...{ m_bVisible = bVisible; }
bool IsVisible()...{ return m_bVisible; }
void SetEnabled(bool bEnable)...{ m_bEnable=bEnable; }
bool IsEnabled()...{ return m_bEnable; }
void SetAlpha(float fAlpha)...{ m_fAlpha=fAlpha; }
float GetAlpha()...{ return m_fAlpha; }
public:
//should not be called from lua scripts
virtual void Render() = 0;
virtual
bool MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)...{return
false;}
protected:
int m_nID;
AUIControlState m_state;
bool m_bVisible;
bool m_bEnable;
POINT m_position;
SIZE m_size;
float m_fAlpha;
std::string m_strText;
};
class CAUIButton : public CAUIControl
...{
public:
CAUIButton():m_pTexture(NULL) ...{}
virtual ~CAUIButton()...{}
public:
void SetTexture(char * szFile)...{}
void SetTextureRects(const RECT & rcNormal, const RECT &rcHover, const RECT &rcPushed, const RECT& rcDisabled)...{}
void SetAlpha(float
fAlpha)...
{ m_fAlpha = fAlpha; printf("CAUIButton::SetAlpha, extra process here!"); }
public:
void Render()...{printf("CAUIButton::Render ");}
bool MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
...{ printf("CAUIButton::MsgProc "); return false;}
protected:
void * LoadTexture(char * szTextureFile)...{return NULL;}
void * m_pTexture;
RECT m_rects[4];
};

extern CAUIButton g_button;

#endif

#define _CLASS_INHERITANCE_H

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>

typedef enum
.{
AUICSNormal = 0,
AUICSHover = 1,
AUICSPushed = 2,
AUICSDisabled = 3,
AUICSHide = 4,
AUICSFORCEDOWRD = 0xFFFFFFFF
} AUIControlState;


class CAUIControl

{
public:
//should not be called from lua scripts
CAUIControl()
:m_nID(-1), m_state(AUICSNormal), m_bVisible(true),
m_bEnable(true), m_fAlpha(0.0f),m_strText("") {}
virtual ~CAUIControl()...{}
public:
//tolua
void SetID(int nID)...{ m_nID = nID; }
int GetID()...{ return m_nID; }
void SetText(char * szText)...{ m_strText = szText; }

const char * GetText()...{ return m_strText.c_str(); }
void SetPosition(POINT pt)...{ m_position = pt; }
POINT GetPosition()...{ return m_position; }
void SetSize(SIZE sz)...{ m_size = sz; }
SIZE GetSize()...{ return m_size; }
void SetVisible(bool bVisible)...{ m_bVisible = bVisible; }
bool IsVisible()...{ return m_bVisible; }
void SetEnabled(bool bEnable)...{ m_bEnable=bEnable; }
bool IsEnabled()...{ return m_bEnable; }
void SetAlpha(float fAlpha)...{ m_fAlpha=fAlpha; }
float GetAlpha()...{ return m_fAlpha; }
public:
//should not be called from lua scripts
virtual void Render() = 0;

protected:
int m_nID;
AUIControlState m_state;
bool m_bVisible;
bool m_bEnable;
POINT m_position;
SIZE m_size;
float m_fAlpha;
std::string m_strText;
};
class CAUIButton : public CAUIControl

public:
CAUIButton():m_pTexture(NULL) ...{}
virtual ~CAUIButton()...{}
public:
void SetTexture(char * szFile)...{}
void SetTextureRects(const RECT & rcNormal, const RECT &rcHover, const RECT &rcPushed, const RECT& rcDisabled)...{}

{ m_fAlpha = fAlpha; printf("CAUIButton::SetAlpha, extra process here!"); }
public:
void Render()...{printf("CAUIButton::Render ");}
bool MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
...{ printf("CAUIButton::MsgProc "); return false;}
protected:
void * LoadTexture(char * szTextureFile)...{return NULL;}
void * m_pTexture;
RECT m_rects[4];
};


extern CAUIButton g_button;


#endif
下面是inheritance.pkg文件:
$#include "inheritance.h"
class CAUIControl
...
{
public: //tolua
void SetID(int nID);
int GetID();
void SetText(char * szText);
const char * GetText();
void SetPosition(POINT pt);
POINT GetPosition();
void SetSize(SIZE sz);
SIZE GetSize();
void SetVisible(bool bVisible);
bool IsVisible();
void SetEnabled(bool bEnable);
bool IsEnabled();
void SetAlpha(float fAlpha);
float GetAlpha();
};

class CAUIButton : public CAUIControl
...{
public:
CAUIButton();
virtual ~CAUIButton();
public:
void SetTexture(char * szFile);
void SetTextureRects(const RECT & rcNormal, const RECT &rcHover, const RECT &rcPushed, const RECT& rcDisabled);
void SetAlpha(float fAlpha);
};

extern CAUIButton g_button@lbutton;


class CAUIControl

{
public: //tolua
void SetID(int nID);
int GetID();
void SetText(char * szText);
const char * GetText();
void SetPosition(POINT pt);
POINT GetPosition();
void SetSize(SIZE sz);
SIZE GetSize();
void SetVisible(bool bVisible);
bool IsVisible();
void SetEnabled(bool bEnable);
bool IsEnabled();
void SetAlpha(float fAlpha);
float GetAlpha();
};


class CAUIButton : public CAUIControl

public:
CAUIButton();
virtual ~CAUIButton();
public:
void SetTexture(char * szFile);
void SetTextureRects(const RECT & rcNormal, const RECT &rcHover, const RECT &rcPushed, const RECT& rcDisabled);
void SetAlpha(float fAlpha);
};


extern CAUIButton g_button@lbutton;
好了,下面用tolua++.exe生成inherit.cpp文件:

#include "lua.hpp"
#include "inheritance.h"
int tolua_inherit_open(lua_State *);
CAUIButton g_button;
int _tmain(int argc, _TCHAR* argv[])
...{
lua_State * L = luaL_newstate();
luaopen_base(L);
tolua_inherit_open(L);
luaL_dofile(L, "..\scripts\inheritance.lua");
lua_close(L);
return 0;
}
#include "inheritance.h"

int tolua_inherit_open(lua_State *);

CAUIButton g_button;

int _tmain(int argc, _TCHAR* argv[])

lua_State * L = luaL_newstate();
luaopen_base(L);
tolua_inherit_open(L);
luaL_dofile(L, "..\scripts\inheritance.lua");
lua_close(L);
return 0;
}
