- 전체
- C/C++ 일반
- C/C++ 수학
- C/C++ 그래픽
- C/C++ 자료구조
- C/C++ 인공지능
- C/C++ 인터넷
- wxWidget
- GTK+
- UNIX or LINUX programming
- 리눅스 마스터 - 국가공인자격
- VC++/ MFC
- C#/CLI/.NET
- QT/기타UI
- Boost lib
- 오픈소스 C 분석자료
- MSA (마이크로서비스), Docker, kubernetes
- WSL(windows subsystem linux)
C/C++ 일반 Kigs Framework Introduction (7/8) - Lua Binding
2020.03.23 21:47
Kigs Framework Introduction (7/8) - Lua Binding
Table of Contents
- Introduction
- Bound Framework's Classes and Methods
- LuaBehaviour Class
- LuaImporter Class
- Already Published in this Series
- History
Introduction
In this article, we will see how to enhance the Kigs framework, and particularly CoreModifiable classes with Lua.
Lua (5.3.5) programming language is embedded in the framework here:
With minor changes for Universal Windows Platform build.
The same directory also contains LuaIntf: A binding between C++11 and Lua language:
To learn more about Lua, look here: https://www.lua.org.
Bound Framework Classes and Methods
Here is the list of bound classes and their accessible methods from Lua:
-
CoreModifiable:name(): Return instance nameaddItem(CMSP item): Add aCoreModifiableinstance smart pointer (CMSP) to another (seeCoreModifiablearticle)removeItem(CMSP item): Remove an instance smart pointer (CMSP) from another (seeCoreModifiablearticle)aggregateWith(CMSP item): Aggregate aCoreModifiableinstance smart pointer (CMSP) to another (seeCoreModifiablearticle)removeAggregateWith(CMSP item): Remove an aggregate (seeCoreModifiablearticle)parents(): Get parents listchilds()oritems(): Get children listgetSonsByType(type): Get list of children of given typegetSonsByName(name): Get list of children with given nameinit(): CallCoreModifiableInitdestroy(): Call CoreModifiableDestroyuid(): Get instance unique idtype(): Get instance exact typeisSubType(type): Check if instance is of the given typeaddAttribute(val): Add a dynamic attribute of the type of the given val (bool,float,stringorvector)onEvent(method_name or Lua function,notification_name): Add an observer onnotification_nameto callmethod_nameor directly Lua function.getByPath(path): Search an instance by pathimportAsSon(filename): Import the given file and addCoreModifiabletree to callingCoreModifiableinstanceemit(signal_name): Emit a signal with given name
-
CMSP:get(): getCoreModifiableinstance pointed by the smart pointer
-
v2f, v3f and v4f: 2D, 3D or 4D vectorsDot(v1,v2): static method, return dot product of v1 and v2Cross(v1,v2): static method, return cross product of v1 and v2 (not available on v4f)Norm(v1): static method, return norm of v1NormSquare(v1): static method, return squared norm of v1normalize(): normalize vectornormalized(): return a copy of the normalized vectorcopy(): return a copy of the vector- members can be accessed with
.x,.y,.z(forv3fandv4f),.w(forv4f) - vector standard operation :
* + - /are also available
-
mat3x4: 3 x 4 matrix (3D transform matrix)setRotationX(angle): Set current matrix to be a X axis rotation matrix of given anglesetRotationY(angle): Set current matrix to be a Y axis rotation matrix of given anglesetRotationZ(angle): Set current matrix to be a Z axis rotation matrix of given anglepreRotateX(angle): Pre rotate current matrix around X axis with given anglepreRotateY(angle): Pre rotate current matrix around Y axis with given anglepreRotateZ(angle): Pre rotate current matrix around Z axis with given anglepostRotateX(angle): Post rotate current matrix around X axis with given anglepostRotateY(angle): Post rotate current matrix around Y axis with given anglepostRotateZ(angle): Post rotate current matrix around Z axis with given anglesetScale(sx,sy,sz): Set current matrix to be a scale matrix with given scale on each axispreScale(sx,sy,sz): Pre scale current matrix with given scale on each axispostScale(sx,sy,sz): Post scale current matrix with given scale on each axissetTranslation(v): Set current matrix to be a translation matrix of givenv3fvectorpreTranslate(v): Pre translate current matrix with givenv3fvectorpostTranslate(v): Post translate current matrix with givenv3fvectorsetRotationXYZ(aX,aY,aZ): Set current matrix to be a rotation matrix with angles aX, aY, aZ. Rotations are applied in order X, then Y, then ZsetRotationZYX(aX,aY,aZ): Set current matrix to be a rotation matrix with angles aX, aY, aZ. Rotations are applied in order Z, then Y, then XpreRotateXYZ(aX,aY,aZ): Pre rotate current matrix (Rotations are applied in order X, then Y, then Z)preRotateZYX(aX,aY,aZ): Pre rotate current matrix (Rotations are applied in order Z, then Y, then X)postRotateXYZ(aX,aY,aZ): Post rotate current matrix (Rotations are applied in order X, then Y, then Z)postRotateZYX(aX,aY,aZ): Post rotate current matrix (Rotations are applied in order Z, then Y, then X)setIdentity(): Set matrix to identitysetNull(): Set each matrix member to 0isIdentity(): Returntrueif the matrix is identitytransformPoint(v): Transform givenv3fvector as a point with the current matrix (apply translation)transformPoints(list_of_v): Transform eachv3fvector in the given list as a point with current matrix (apply translation)transformVector(v): Transform givenv3fvector with current matrix (translation is not applied)transformVectors(list_of_v): Transform eachv3fvector in the given list with current matrix (translation is not applied)copy(): Return a copy of the current matrix
-
core:ByName(name): Returns the list ofCoreModifiablewith the given nameByType(type): Returns the list ofCoreModifiablewith the given typeGetFirstInstanceByName(name): Returns the firstCoreModifiablefound with the given nameGetModule(name): Returns the module (CoreModifiable) with the given namePostNotification(name,sender,data): Post notification with the given name. sender and data are optionalCoreModifiableparametersAddAutoUpdate(instance): Add givenCoreModifiableinstance to auto-update mechanismRemoveAutoUpdate(instance): Remove givenCoreModifiableinstance from auto-update mechanismSID(string): ReturnKigsID(unsigned int) for the given stringConnect(sender,signal,receiver,slot): ConnectCoreModifiablesender's signal toCoreModifiablereceiver's slotDisconnect(sender,signal,receiver,slot): DisconnectCoreModifiablesender's signal toCoreModifiablereceiver's slotImport(filename): Import filename asCoreModifiabletree and returnsCMSProot
Accessing Kigs Framework from Lua
On Lua side, generic functionalities of the framework are available:
-- create an UIImage instance named logo (get a smart pointer)
local logoSP = CoreModifiable("logo","UIImage")
local logo=logoSP:get()
-- set CoreModifiable attributes directly
logo.Texture = "KigsHD.png"
logo.Dock = {0.5,0.5}
logo.Anchor = {0.5,0.5}
logo.Priority = 10
-- and init logo
logo:init()
CoreModifiable attributes are accessed directly by their name preceded by a dot '.'.
local rotate = logo.RotationAngle
rotate = rotate + 0.1
logo.RotationAngle = rotate
Bound methods or CoreModifiable methods are accessed the classic Lua way with a ':' ( except for static methods, were '.' should be used ) :
-- call CoreModifiable method "HelloFromLua" defined on target object
target:HelloFromLua()
Accessing Lua from Kigs Framework
Executing Lua Code
It's possible to just execute some Lua code from a file in C++ :
auto lua = KigsCore::GetModule<LuaKigsBindModule>();
bool result=lua->ExecuteLuaFile("myCode.lua");
Or to execute some code when importing an XML file by adding a "Lua" item to an instance:
<Lua N="someCode">
-- code is executed when XML file is imported
local panel=core.GetFirstInstanceByName("panel")
panel.Color={1.0,0.0,0.0}
</Lua>
or:
<Lua N="someCode" V="#someCode.lua"/>
Add Lua Method to an Instance
In XML, it's also possible to add a Lua function (as a CoreModifiable method) to a CoreModifiable instance:
<Lua N="Click">
-- here self is the current CoreModifiable
return function(self)
local currentColor=self.Color
self.Color={1.0-currentColor.x,0.0,0.0}
end
</Lua>
LuaBehaviour Class
The LuaBehaviour class is used to add functionalities to an instance using the Lua language.
Serialization
The easy way to do it is to add this kind of item directly in XML to the targeted instance:
<Inst N="testLua" T="LuaBehaviour" Aggregate="true">
<Attr N="Script" V="#test.lua"/>
</Inst>
The LuaBehaviour must be added as an aggregate to its parent instance.
Here, the "Script" attribute value is "#test.lua". The starting '#' indicates a filename to load. So here, the "test.lua" file is loaded (if found) and its content is interpreted as Lua.
The code can also be embedded in the XML file as CDATA:
<Inst N="testLua" T="LuaBehaviour" Aggregate="true">
<Attr N="Script">
<![CDATA[
local Sample7Script = {}
Sample7Script.logo=0
function Sample7Script:init()
local logoSP = CoreModifiable("logo","UIImage")
self.logo=logoSP:get()
self.logo.Texture = "KigsHD.png"
self.logo.Dock = {0.5,0.5}
self.logo.Anchor = {0.5,0.5}
self.logo.Priority = 10
self.target:addItem(logoSP)
self.logo:init()
end
return Sample7Script
]]>
</Attr>
</Inst>
In C++ Code
Of course, LuaBehaviour instance can be created using framework code, the "classic" way :
CMSP behaviour = KigsCore::GetInstanceOf("behaviour", "LuaBehaviour");
behaviour->setValue("Script", "#ScriptOnApp.lua");
// aggregate LuaBehaviour with this
aggregateWith(behaviour);
behaviour->Init();
Lua Side Syntax
Behaviour Code
Lua code must have the following form:
-- you can call it whatever you want
local behaviourObjectName = { var1 = 0; var2 = "two" }
-- Insert methods here
function behaviourObjectName:doSomething(param1)
self.var1 = param1
end
return behaviourObjectName
The methods "init()", "update(current_time)", "destroy()", "addItem(other)", "removeItem(other)" are called automatically when aggregate is initialized, update...
Adding a CoreModifiable Lua method directly in Lua is possible using WRAP_METHODS table in the object declaration:
-- reset is wrapped so C++ can call directly the reset method on target object
local Sample7Script = { logo=0; startingTime=0; WRAP_METHODS = {"reset"}; }
-- the reset method
function Sample7Script:reset(current_time)
self.startingTime = current_time
end
Or directly by adding a Lua function to the target object in init function:
function Sample7Script:init()
-- add reset function to target
self.target.reset = function (current_time)
self.startingTime = current_time
end
end
So on the C++ side, the Lua reset method can be called like another CoreModifiable method:
// instance is the object aggregated with LuaBehaviour
instance->SimpleCall("reset",GetApplicationTimer()->GetTime());
Accessing CoreModifiable Instance from LuaBehaviour
LuaBehaviour functions can use self.target to access the CoreModifiable instance aggregated to LuaBehaviour:
-- create an UIImage instance named logo (get a smart pointer)
local logoSP = CoreModifiable("logo","UIImage")
-- then add logo (smart pointer) to target
self.target:addItem(logoSP)
LuaImporter Class
It's also possible to import CoreModifiable trees created in Lua using LuaImporter class.
In XML, add the following item to import "scene.lua" file and add it in the current XML hierarchy:
<Inst N="lua" T="LuaImporter">
<Attr N="Script" V="scene.lua"/>
</Inst>
The syntax of a Lua file aimed at import looks like that:
local result =
{
item("itemName1", "itemType1", {attributeName1=0,... }).items(
item("itemName2", "itemType2", { attributeName1=true, ... } ),
item("itemName3", "itemType3", { attributeName1=32, ... } ),
...
)
}
return result
"item" keyword creates a CoreModifiable instance. The first two parameters are instance name and instance type, then the list of attributes to initialize with given values.
To add sons to an item, use the ".items" keyword.
In the previous example, itemName2 and itemName3 are sons of itemName1.
As it's Lua code, it's possible to define functions and call them as helpers to create objects with the same type or initialize attributes:
local specialNode3D = function(name)
return item(name, "Node3D", {Show=false, IgnoreBBox=true, CollideMask=0}).items(
item("nodepth", "RenderingCustomizer", {OverrideDepthTest=0, RenderPassMask=16})
)
end
local result =
{
specialNode3D("node1").items(
specialNode3D("node2"),
specialNode3D("node3")
)
}
return result
It's also possible to import an item from another XML or Lua file:
local result =
{
item("itemName1", "itemType1", {Param1=-1, Param2=-1}).items(
xml("item2FromXML", "item2.xml",{Param1 = 5}),
lua("item3FromExternLua", "item3.lua"),
)
}
return result
Or to reference an already loaded item with the "ref" keyword:
item("itemName1", "itemType1", {Param1=-1, Param2=-1}).items(
ref("refName", "refType")
)
And finally, it's also possible to connect a signal and slot:
item("button", "UIButtonImage", {Priority=10, UpTexture="Up.png",
DownTexture="Down.png",Dock={0.9,0.1}}).items(
connect("this","ClickUp" ,"../UIItem:panel","Click")
)
And to add an instance to auto-update mechanism:
item("itemName1", "itemType1", {attributeName1=0,... }).items(core.AddAutoUpdate)
Find all the sample code from this wiki section in Sample7 project (browse the code).
Already Published in this Series
- Kigs Framework Introduction (1/8) - Overview
- Kigs Framework Introduction (2/8) - CoreModifiable
- Kigs Framework Introduction (3/8) - Attributes
- Kigs Framework Introduction (4/8) - Methods
- Kigs Framework Introduction (5/8) - CoreItem
- Kigs Framework Introduction (6/8) - Signal, Slot, Notification
- Kigs Framework Introduction (7/8) - Lua Binding
[출처] https://www.codeproject.com/Articles/5260595/Kigs-Framework-Introduction-7-8-Lua-Binding
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 16 |
[C/C++ 자료구조] 아스키 코드표(ASCII Table)
| 졸리운_곰 | 2023.08.16 | 361 |
| 15 | [C 프로그래밍] 파일 출력 함수_3.연결 리스트 저장, 불러오기 | 졸리운_곰 | 2022.06.17 | 327 |
| 14 |
[C/C++ 자료구조] 5일만에 뚝딱 스크립트 언어 만들기 PGLight (1/5)
| 졸리운_곰 | 2021.04.12 | 301 |
| 13 |
[C/C++] 미니멀 가상화폐 코드 , 미니멀 블룩체인, noincoin (Block Chain in C Major)
| 졸리운_곰 | 2021.01.30 | 279 |
| 12 |
Visual Studio Community로 V8 엔진 다운로드 및 빌드하고 간단히 살펴보기
| 졸리운_곰 | 2020.11.30 | 272 |
| 11 | zlib msvc로 포팅하기 2 | 졸리운_곰 | 2018.02.23 | 357 |
| 10 | zlib msvc로 포팅하기 1 | 졸리운_곰 | 2018.02.23 | 288 |
| 9 | zlib 개요 | 졸리운_곰 | 2018.02.23 | 573 |
| 8 | [zlib] API 사용법 | 졸리운_곰 | 2018.02.23 | 502 |
| 7 | [zlib] DEFLATE algorithm (2) - Huffman coding | 졸리운_곰 | 2018.02.23 | 352 |
| 6 | [zlib] DEFLATE algorithm (1) - LZ77 | 졸리운_곰 | 2018.02.23 | 504 |
| 5 |
zlib 입문
| 졸리운_곰 | 2018.02.23 | 630 |
| 4 | [펌] zlib를 사용법 | 졸리운_곰 | 2018.02.23 | 784 |
| 3 |
Create Custom Binary File Formats for Your Game's Data
| 졸리운_곰 | 2018.02.23 | 373 |
| 2 |
GitHub - codelibs/libdxfrw: C++ library to read and write DXF/DWG files
| 졸리운_곰 | 2017.04.27 | 655 |
| 1 | C++ Json Parser 조사 | 졸리운_곰 | 2015.11.10 | 1005 |

