tolua++初探(五)
系統(tǒng)
1971 0
<采用了單繼承的類的導(dǎo)出> 這個(gè)……,tolua++支持采用了單繼承的類的直接導(dǎo)出,在lua中可以像在C++中那樣訪問基類的方法。和其它簡(jiǎn)單類的導(dǎo)出沒什么區(qū)別。 只是個(gè)簡(jiǎn)單的示例,我們定義一個(gè)控件基類,從它派生一個(gè)按鈕類。然后在lua中分別訪問基類和按鈕類的方法。我們導(dǎo)出一個(gè)全局變量lbutton,同時(shí)也在lua中生成一個(gè)新button。 先看實(shí)際的頭文件inheritance.h,我把實(shí)現(xiàn)也寫在了頭文件里。
#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
:
//
shouldnotbecalledfromluascripts
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(POINTpt)
...
{m_position
=
pt;}
POINTGetPosition()
...
{
return
m_position;}
void
SetSize(SIZEsz)
...
{m_size
=
sz;}
SIZEGetSize()
...
{
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
:
//
shouldnotbecalledfromluascripts
virtual
void
Render()
=
0
;
virtual
bool
MsgProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
...
{
return
false
;}
protected
:
int
m_nID;
AUIControlStatem_state;
bool
m_bVisible;
bool
m_bEnable;
POINTm_position;
SIZEm_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,extraprocesshere!
"
);}
public
:
void
Render()
...
{printf(
"
CAUIButton::Render
"
);}
bool
MsgProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
...
{printf(
"
CAUIButton::MsgProc
"
);
return
false
;}
protected
:
void
*
LoadTexture(
char
*
szTextureFile)
...
{
return
NULL;}
void
*
m_pTexture;
RECTm_rects[
4
];
}
;
extern
CAUIButtong_button;
#endif
g_button的實(shí)例定義在main函數(shù)所在的文件中。 下面是inheritance.pkg文件:
$#include
"
inheritance.h
"
class
CAUIControl
...
{
public
:
//
tolua
void
SetID(
int
nID);
int
GetID();
void
SetText(
char
*
szText);
const
char
*
GetText();
void
SetPosition(POINTpt);
POINTGetPosition();
void
SetSize(SIZEsz);
SIZEGetSize();
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
CAUIButtong_button@lbutton;
對(duì)于基類CAUIControl,只導(dǎo)出部分方法,不導(dǎo)出構(gòu)造函數(shù),不允許在Lua中直接生成其實(shí)例。派生類CAUIButton可以在lua中生成實(shí)例。CAUIButton重寫了基類的SetAlpha函數(shù)也增加了一些新的函數(shù),如設(shè)置紋理函數(shù)SetTexture。 全局變量的導(dǎo)出很簡(jiǎn)單,
extern
CAUIButtong_button@lbutton;一個(gè)語句就可以了。我們還可以為其加上tolua_readonly修飾符。我把它重名為lbutton。 好了,下面用tolua++.exe生成inherit.cpp文件:
tolua
++
.exe
-
ninherit
-
oinherit.cppinheritance.pkg
接下來是驅(qū)動(dòng)文件inheritance.cpp:
#include
"
lua.hpp
"
#include
"
inheritance.h
"
int
tolua_inherit_open(lua_State
*
);
CAUIButtong_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
;
}
相當(dāng)簡(jiǎn)單,和前面幾個(gè)幾乎一樣,唯一變化的是多了個(gè)全局變量。 最后是inheritance.lua文件:
print(
"
nowininheritance.lua!
"
)
--
access
global
button
print(
"
globalbuttontest
"
)
lbutton:SetAlpha(
0.5
)
print(lbutton:GetAlpha())
lbutton:SetID(
100
)
lbutton:SetText(
"
globalbutton
"
)
print(lbutton:GetText())
--
alloc
new
button
newbutton
=
CAUIButton:
new
()
--
CAUIControl
'
smethods
newbutton:SetID(
101
)
print(newbutton:GetID())
newbutton:SetText(
"
newbutton
"
)
print(newbutton:GetText())
--
CAUIButton
'
sSetAlpha
newbutton:SetAlpha(
0.7
)
print(newbutton:GetAlpha())
大功告成了,幾乎沒有任何新意。不過還是驗(yàn)證了一點(diǎn)東西,僅此而已。 接下來要演示如何呼叫l(wèi)ua腳本中的函數(shù),并向其傳遞參數(shù),在該lua函數(shù)中對(duì)參數(shù)進(jìn)行類型轉(zhuǎn)換,然后呼叫其特定方法。
tolua++初探(五)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元