CCScriptSupport.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __SCRIPT_SUPPORT_H__
  23. #define __SCRIPT_SUPPORT_H__
  24. #include "base/ccConfig.h"
  25. #include "platform/CCCommon.h"
  26. #include "base/CCTouch.h"
  27. #include "base/CCEventTouch.h"
  28. #include "base/CCEventKeyboard.h"
  29. #include <map>
  30. #include <string>
  31. #include <list>
  32. /**
  33. * @addtogroup base
  34. * @{
  35. */
  36. #if CC_ENABLE_SCRIPT_BINDING
  37. typedef struct lua_State lua_State;
  38. NS_CC_BEGIN
  39. class TimerScriptHandler;
  40. class Layer;
  41. class MenuItem;
  42. class CallFunc;
  43. class Acceleration;
  44. class Action;
  45. enum ccScriptType {
  46. kScriptTypeNone = 0,
  47. kScriptTypeLua,
  48. kScriptTypeJavascript
  49. };
  50. /**
  51. * This classes is wrapped to store the handler corresponding to the Lua function pointer and assign the handler a unique id
  52. * @js NA
  53. */
  54. class ScriptHandlerEntry : public Ref
  55. {
  56. public:
  57. /**
  58. * create a ScriptHandlerEntry instance by the handler.
  59. *
  60. * @param handler corresponding to the Lua function pointer.
  61. * @return ScriptHandlerEntry instance.
  62. * @lua NA
  63. * @js NA
  64. */
  65. static ScriptHandlerEntry* create(int handler);
  66. /**
  67. * Destructor of ScriptHandlerEntry.
  68. * @lua NA
  69. * @js NA
  70. */
  71. virtual ~ScriptHandlerEntry();
  72. /**
  73. * Get the handler corresponding to the Lua function pointer.
  74. *
  75. * @return the handler corresponding to the Lua function pointer.
  76. * @lua NA
  77. * @js NA
  78. */
  79. int getHandler(void) {
  80. return _handler;
  81. }
  82. /**
  83. * Get the unique id corresponding to the handler.
  84. *
  85. * @return the unique id corresponding to the handler.
  86. * @lua NA
  87. * @js NA
  88. */
  89. int getEntryId(void) {
  90. return _entryId;
  91. }
  92. protected:
  93. ScriptHandlerEntry(int handler)
  94. : _handler(handler)
  95. {
  96. static int newEntryId = 0;
  97. newEntryId++;
  98. _entryId = newEntryId;
  99. }
  100. int _handler;
  101. int _entryId;
  102. };
  103. /**
  104. * The SchedulerScriptHandlerEntry is used to store the handler corresponding to the Lua function pointer and assign the handler a unique id like ScriptHandlerEntry.
  105. * Meanwhile,create a timer that named TimerScriptHandler to execute the Lua function corresponding to the handler in the interval time if the SchedulerScriptHandlerEntry object isn't paused.
  106. * @js NA
  107. */
  108. class SchedulerScriptHandlerEntry : public ScriptHandlerEntry
  109. {
  110. public:
  111. /**
  112. * create a SchedulerScriptHandlerEntry object.
  113. *
  114. * @param handler the index corresponding to the Lua function pointer.
  115. * @param interval the interval to execute the Lua function. If the value is 0, then the lua function will be scheduled every frame.
  116. * @param paused if paused is true, then the timer won't be started until it is resumed.
  117. * @return a SchedulerScriptHandlerEntry object.
  118. * @js NA
  119. * @lua NA
  120. */
  121. static SchedulerScriptHandlerEntry* create(int handler, float interval, bool paused);
  122. /**
  123. * Destructor of SchedulerScriptHandlerEntry.
  124. * @js NA
  125. * @lua NA
  126. */
  127. virtual ~SchedulerScriptHandlerEntry();
  128. /**
  129. * Get the pointer of TimerScriptHandler object.
  130. *
  131. * @return the pointer of TimerScriptHandler object.
  132. * @js NA
  133. * @lua NA
  134. */
  135. TimerScriptHandler* getTimer(void) {
  136. return _timer;
  137. }
  138. /**
  139. * Get the flag whether paused or not.
  140. *
  141. * @return the flag whether paused or not.
  142. * @js NA
  143. * @lua NA
  144. */
  145. bool isPaused(void) {
  146. return _paused;
  147. }
  148. /**
  149. * Set the markedForDeletion flag true.
  150. * @js NA
  151. * @lua NA
  152. */
  153. void markedForDeletion(void) {
  154. _markedForDeletion = true;
  155. }
  156. /**
  157. * Get the flag whether markedForDeletion or not.
  158. *
  159. * @return the flag whether markedForDeletion or not.
  160. * @js NA
  161. * @lua NA
  162. */
  163. bool isMarkedForDeletion(void) {
  164. return _markedForDeletion;
  165. }
  166. private:
  167. SchedulerScriptHandlerEntry(int handler)
  168. : ScriptHandlerEntry(handler)
  169. , _timer(nullptr)
  170. , _paused(false)
  171. , _markedForDeletion(false)
  172. {
  173. }
  174. bool init(float interval, bool paused);
  175. TimerScriptHandler* _timer;
  176. bool _paused;
  177. bool _markedForDeletion;
  178. };
  179. /**
  180. * @cond
  181. * @js NA
  182. */
  183. class TouchScriptHandlerEntry : public ScriptHandlerEntry
  184. {
  185. public:
  186. static TouchScriptHandlerEntry* create(int handler, bool isMultiTouches, int priority, bool swallowsTouches);
  187. virtual ~TouchScriptHandlerEntry();
  188. bool isMultiTouches(void) {
  189. return _isMultiTouches;
  190. }
  191. int getPriority(void) {
  192. return _priority;
  193. }
  194. bool getSwallowsTouches(void) {
  195. return _swallowsTouches;
  196. }
  197. private:
  198. TouchScriptHandlerEntry(int handler)
  199. : ScriptHandlerEntry(handler)
  200. , _isMultiTouches(false)
  201. , _priority(0)
  202. , _swallowsTouches(false)
  203. {
  204. }
  205. bool init(bool isMultiTouches, int priority, bool swallowsTouches);
  206. bool _isMultiTouches;
  207. int _priority;
  208. bool _swallowsTouches;
  209. };
  210. /**
  211. * @endcond
  212. * @js NA
  213. */
  214. /** ScriptEventType enum*/
  215. enum ScriptEventType
  216. {
  217. kNodeEvent = 0,
  218. kMenuClickedEvent,
  219. kCallFuncEvent,
  220. kScheduleEvent,
  221. kTouchEvent,
  222. kTouchesEvent,
  223. kKeypadEvent,
  224. kAccelerometerEvent,
  225. kControlEvent,
  226. kCommonEvent,
  227. kComponentEvent,
  228. kRestartGame,
  229. kScriptActionEvent
  230. };
  231. /**
  232. * For Lua, Wrapper the script data that should be used to find the handler corresponding to the Lua function by the nativeobject pointer and store the value pointer which would be converted concretely by the different events,then the converted data would be passed into the Lua stack.
  233. * @js NA
  234. */
  235. struct BasicScriptData
  236. {
  237. /**
  238. * For Lua, nativeobject is used to get handler corresponding to the Lua function.
  239. *
  240. * @js NA
  241. * @lua NA
  242. */
  243. void* nativeObject;
  244. /**
  245. * A pointer point to the value data which would be converted by different events.
  246. *
  247. * @js NA
  248. * @lua NA
  249. */
  250. void* value;
  251. /**
  252. * Constructor of BasicScriptData.
  253. *
  254. * @js NA
  255. * @lua NA
  256. */
  257. BasicScriptData(void* inObject,void* inValue = nullptr)
  258. : nativeObject(inObject),value(inValue)
  259. {
  260. }
  261. };
  262. /**
  263. * For Lua, Wrapper the script data that should be used to find the handler corresponding to the Lua function by the nativeobject pointer and store the value pointer which would be converted concretely by the different events,then the converted data would be passed into the Lua stack.
  264. * @js NA
  265. */
  266. struct ActionObjectScriptData
  267. {
  268. /**
  269. * For Lua, nativeobject is used to get handler corresponding to the Lua function.
  270. *
  271. * @js NA
  272. * @lua NA
  273. */
  274. void* nativeObject;
  275. /**
  276. * A pointer point to the value data which event action
  277. *
  278. * @js NA
  279. * @lua NA
  280. */
  281. int* eventType;
  282. /**
  283. * A pointer point to the value data which would be converted by different events.
  284. *
  285. * @js NA
  286. * @lua NA
  287. */
  288. void* param;
  289. /**
  290. * Constructor of BasicScriptData.
  291. *
  292. * @js NA
  293. * @lua NA
  294. */
  295. ActionObjectScriptData(void* inObject,int* inValue = nullptr, void* inParam = nullptr)
  296. : nativeObject(inObject),eventType(inValue), param(inParam)
  297. {
  298. }
  299. };
  300. /**
  301. * For Lua, the SchedulerScriptData is used to find the Lua function pointer by the handler, then call the Lua function by push the elapse into the Lua stack as a parameter when scheduler update event is triggered.
  302. * @js NA
  303. */
  304. struct SchedulerScriptData
  305. {
  306. /**
  307. * the handler corresponding to the Lua function pointer, only use in the Lua.
  308. *
  309. * @js NA
  310. * @lua NA
  311. */
  312. int handler;
  313. /**
  314. * the parameter would be passed in to the Lua function, only use in the Lua.
  315. *
  316. * @js NA
  317. * @lua NA
  318. */
  319. float elapse;
  320. // js use
  321. void* node;
  322. /**
  323. * Constructor of SchedulerScriptData.
  324. *
  325. * @js NA
  326. * @lua NA
  327. */
  328. SchedulerScriptData(int inHandler,float inElapse,void* inNode = nullptr)
  329. : handler(inHandler),
  330. elapse(inElapse),
  331. node(inNode)
  332. {
  333. }
  334. };
  335. /**
  336. * For Lua, the TouchesScriptData is used to find the Lua function pointer by the nativeObject, then call the Lua function by push touches data and actionType into the Lua stack as the parameters when the touches event is triggered.
  337. * @js NA
  338. */
  339. struct TouchesScriptData
  340. {
  341. /**
  342. * The EventTouch::EventCode type.
  343. *
  344. * @lua NA
  345. * @js NA
  346. */
  347. EventTouch::EventCode actionType;
  348. /**
  349. * For Lua, it Used to find the Lua function pointer by the ScriptHandlerMgr.
  350. *
  351. * @lua NA
  352. * @js NA
  353. */
  354. void* nativeObject;
  355. /**
  356. * The vector of Touch.For Lua, it would be convert to the Lua table form to be pushed into the Lua stack.
  357. *
  358. * @lua NA
  359. * @js NA
  360. */
  361. const std::vector<Touch*>& touches;
  362. /**
  363. * event information, it is useless for Lua.
  364. *
  365. * @lua NA
  366. * @js NA
  367. */
  368. Event* event;
  369. /**
  370. * Constructor of TouchesScriptData.
  371. *
  372. * @lua NA
  373. * @js NA
  374. */
  375. TouchesScriptData(EventTouch::EventCode inActionType, void* inNativeObject, const std::vector<Touch*>& inTouches, Event* evt)
  376. : actionType(inActionType),
  377. nativeObject(inNativeObject),
  378. touches(inTouches),
  379. event(evt)
  380. {
  381. }
  382. };
  383. /**
  384. * For Lua, the TouchScriptData is used to find the Lua function pointer by the nativeObject, then call the Lua function by push touch data and actionType converted to string type into the Lua stack as the parameters when the touch event is triggered.
  385. * @js NA
  386. */
  387. struct TouchScriptData
  388. {
  389. /**
  390. * The EventTouch::EventCode type.
  391. *
  392. * @lua NA
  393. * @js NA
  394. */
  395. EventTouch::EventCode actionType;
  396. /**
  397. * For Lua, it Used to find the Lua function pointer by the ScriptHandlerMgr.
  398. *
  399. * @lua NA
  400. * @js NA
  401. */
  402. void* nativeObject;
  403. /**
  404. * touch information. it would be in x,y form to push into the Lua stack.
  405. *
  406. * @lua NA
  407. * @js NA
  408. */
  409. Touch* touch;
  410. /**
  411. * event information,it is useless for Lua.
  412. *
  413. * @lua NA
  414. * @js NA
  415. */
  416. Event* event;
  417. /**
  418. * Constructor of TouchScriptData.
  419. *
  420. * @lua NA
  421. * @js NA
  422. */
  423. TouchScriptData(EventTouch::EventCode inActionType, void* inNativeObject, Touch* inTouch, Event* evt)
  424. : actionType(inActionType),
  425. nativeObject(inNativeObject),
  426. touch(inTouch),
  427. event(evt)
  428. {
  429. }
  430. };
  431. /**
  432. * For Lua, the KeypadScriptData is used to find the Lua function pointer by the nativeObject, then call the Lua function by push the actionType converted to string type into the Lua stack as the parameters when the Keypad event is triggered.
  433. * @js NA
  434. */
  435. struct KeypadScriptData
  436. {
  437. /**
  438. * The specific type of EventKeyboard::KeyCode
  439. *
  440. * @lua NA
  441. * @js NA
  442. */
  443. EventKeyboard::KeyCode actionType;
  444. /**
  445. * For Lua, it Used to find the Lua function pointer by the ScriptHandlerMgr.
  446. *
  447. * @lua NA
  448. * @js NA
  449. */
  450. void* nativeObject;
  451. /**
  452. * Constructor of KeypadScriptData.
  453. *
  454. * @lua NA
  455. * @js NA
  456. */
  457. KeypadScriptData(EventKeyboard::KeyCode inActionType,void* inNativeObject)
  458. : actionType(inActionType),nativeObject(inNativeObject)
  459. {
  460. }
  461. };
  462. /**
  463. * For Lua, the CommonScriptData is used to find the Lua function pointer by the handler, then call the Lua function by push the eventName, eventSource(if it not nullptr), eventSourceClassName(if it is nullptr or "", and the eventSource is not nullptr,would give the default string "cc.Ref") into the Lua stack as the parameter when the common event such as is triggered.
  464. * @js NA
  465. */
  466. struct CommonScriptData
  467. {
  468. /**
  469. * The index to find the corresponding to the Lua function pointer.
  470. *
  471. * @lua NA
  472. * @js NA
  473. */
  474. int handler;
  475. /**
  476. * The string value to be pushed into the Lua stack.
  477. *
  478. * @lua NA
  479. * @js NA
  480. */
  481. char eventName[64];
  482. /**
  483. * The source object trigger the event,could be nullptr.
  484. *
  485. * @lua NA
  486. * @js NA
  487. */
  488. Ref* eventSource;
  489. /**
  490. * The class name of source object trigger the event, could be nullptr.
  491. *
  492. * @lua NA
  493. * @js NA
  494. */
  495. char eventSourceClassName[64];
  496. /**
  497. * Constructor of CommonScriptData.
  498. *
  499. * @lua NA
  500. * @js NA
  501. */
  502. CommonScriptData(int inHandler,const char* inName, Ref* inSource = nullptr,const char* inClassName = nullptr)
  503. : handler(inHandler),
  504. eventSource(inSource)
  505. {
  506. if (nullptr == inName)
  507. {
  508. memset(eventName, 0, sizeof(eventName));
  509. }
  510. else
  511. {
  512. strncpy(eventName, inName, sizeof(eventName));
  513. }
  514. if (nullptr == inClassName)
  515. {
  516. memset(eventSourceClassName, 0, sizeof(eventSourceClassName));
  517. }
  518. else
  519. {
  520. strncpy(eventSourceClassName, inClassName, 64);
  521. }
  522. }
  523. };
  524. /**
  525. * The ScriptEvent wrapper the different script data corresponding to the ScriptEventType in to the unified struct.
  526. * when the corresponding event is triggered, we could call the `sendEvent` of ScriptEngineProtocol to handle the event.
  527. * @js NA
  528. */
  529. struct ScriptEvent
  530. {
  531. /**
  532. * The specific type of ScriptEventType.
  533. *
  534. * @lua NA
  535. * @js NA
  536. */
  537. ScriptEventType type;
  538. /**
  539. * Pointer point to the different data.
  540. *
  541. * @lua NA
  542. * @js NA
  543. */
  544. void* data;
  545. /**
  546. * Constructor of ScriptEvent.
  547. *
  548. * @lua NA
  549. * @js NA
  550. */
  551. ScriptEvent(ScriptEventType inType,void* inData)
  552. : type(inType),
  553. data(inData)
  554. {
  555. }
  556. };
  557. /**
  558. * Don't make ScriptEngineProtocol inherits from Object since setScriptEngine is invoked only once in AppDelegate.cpp,
  559. * It will affect the lifecycle of ScriptEngine instance, the autorelease pool will be destroyed before destructing ScriptEngine.
  560. * So a crash will appear on Win32 if you click the close button.
  561. * @js NA
  562. */
  563. class CC_DLL ScriptEngineProtocol
  564. {
  565. public:
  566. /**
  567. * Constructor of ScriptEngineProtocol.
  568. *
  569. * @lua NA
  570. * @js NA
  571. */
  572. ScriptEngineProtocol()
  573. {}
  574. /**
  575. * Destructor of ScriptEngineProtocol.
  576. *
  577. * @lua NA
  578. * @js NA
  579. */
  580. virtual ~ScriptEngineProtocol() {}
  581. /**
  582. * Get the specific script type.
  583. *
  584. * @return the specific script type.
  585. *
  586. * @lua NA
  587. * @js NA
  588. */
  589. virtual ccScriptType getScriptType() { return kScriptTypeNone; }
  590. /**
  591. * Reflect the retain relationship to script scope
  592. */
  593. virtual void retainScriptObject(Ref* /*owner*/, Ref* /*target*/) {}
  594. /**
  595. * Add the script object to root object
  596. */
  597. virtual void rootScriptObject(Ref* /*target*/) {}
  598. /**
  599. * Reflect the release relationship to script scope
  600. */
  601. virtual void releaseScriptObject(Ref* /*owner*/, Ref* /*target*/) {}
  602. /**
  603. * Remove the script object from root object
  604. */
  605. virtual void unrootScriptObject(Ref* /*target*/) {}
  606. /**
  607. * Release all children native refs for the given node in script scope
  608. */
  609. virtual void releaseAllChildrenRecursive(Node* /*node*/) {}
  610. /**
  611. * Release all native refs for the given owner in script scope
  612. */
  613. virtual void releaseAllNativeRefs(cocos2d::Ref* /*owner*/) {}
  614. /**
  615. * Remove script object,The specific meaning should refer to the ScriptType.
  616. * For Lua, @see removeScriptObjectByObject of LuaEngine.
  617. *
  618. * @lua NA
  619. * @js NA
  620. */
  621. virtual void removeScriptObjectByObject(Ref* /*obj*/) {}
  622. /**
  623. * Remove script function handler, only LuaEngine class need to implement this function.
  624. * @see removeScriptHandler of LuaEngine.
  625. * @lua NA
  626. * @js NA
  627. */
  628. virtual void removeScriptHandler(int /*handler*/) {}
  629. /**
  630. * Reallocate script function handler, only LuaEngine class need to implement this function.
  631. * @see reallocateScriptHandler of LuaEngine.
  632. * @lua NA
  633. * @js NA
  634. */
  635. virtual int reallocateScriptHandler(int /*handler*/) { return 0; }
  636. /**
  637. * Execute script code contained in the given string.
  638. *
  639. * @param codes holding the valid script code that should be executed.
  640. * @return 0 if the string is executed correctly.
  641. * @return other if the string is executed wrongly.
  642. * @lua NA
  643. * @js NA
  644. */
  645. virtual int executeString(const char* codes) = 0;
  646. /**
  647. * Execute a script file.
  648. *
  649. * @param filename String object holding the filename of the script file that is to be executed.
  650. * @return 0 if it happen the error or it hasn't return value, otherwise it return the value by calling the lua function.
  651. * @lua NA
  652. * @js NA
  653. */
  654. virtual int executeScriptFile(const char* filename) = 0;
  655. /**
  656. * Execute a scripted global function.
  657. * The function should not take any parameters and should return an integer.
  658. *
  659. * @param functionName String object holding the name of the function, in the global script environment, that is to be executed.
  660. * @return The integer value returned from the script function.
  661. * @lua NA
  662. * @js NA
  663. */
  664. virtual int executeGlobalFunction(const char* functionName) = 0;
  665. /**
  666. * When trigger a script event ,call this func,add params needed into ScriptEvent object.nativeObject is object triggering the event, can be nullptr in Lua.
  667. *
  668. *
  669. * @lua NA
  670. * @js NA
  671. */
  672. virtual int sendEvent(ScriptEvent* evt) = 0;
  673. /**
  674. * Handle the assert message.
  675. *
  676. * @return true if the assert was handled by the script engine, false otherwise.
  677. *
  678. * @lua NA
  679. * @js NA
  680. */
  681. virtual bool handleAssert(const char *msg) = 0;
  682. /**
  683. * Useless for Lua.
  684. *
  685. * @lua NA
  686. * @js NA
  687. */
  688. virtual void setCalledFromScript(bool /*callFromScript*/) {}
  689. /**
  690. * Useless for Lua.
  691. *
  692. * @lua NA
  693. * @js NA
  694. */
  695. virtual bool isCalledFromScript() { return false; };
  696. /** ConfigType enum. */
  697. enum class ConfigType
  698. {
  699. NONE,
  700. COCOSTUDIO
  701. };
  702. /**
  703. * Parse configuration file.
  704. *
  705. * @param type the specific type value.
  706. * @param str the information data.
  707. *
  708. * @lua NA
  709. * @js NA
  710. */
  711. virtual bool parseConfig(ConfigType type, const std::string& str) = 0;
  712. /** Root a Reference.
  713. It tells the Garbage Collector that the associated Scripting object should not be collected
  714. */
  715. virtual void rootObject(Ref* /*obj*/) {}
  716. /** Unroot a Reference.
  717. It tells the Garbage Collector that the associated Scripting object can be collected
  718. */
  719. virtual void unrootObject(Ref* /*obj*/) {}
  720. /** Remove proxy for a native object
  721. */
  722. virtual void removeObjectProxy(Ref* obj) {}
  723. /** Triggers the garbage collector */
  724. virtual void garbageCollect() {}
  725. };
  726. class Node;
  727. /**
  728. * ScriptEngineManager is a singleton which manager an object instance of ScriptEngineProtocol, such as LuaEngine.
  729. *
  730. * @since v0.99.5-x-0.8.5
  731. * @js NA
  732. */
  733. class CC_DLL ScriptEngineManager
  734. {
  735. public:
  736. /**
  737. * Constructor of ScriptEngineManager.
  738. *
  739. * @lua NA
  740. * @js NA
  741. */
  742. ~ScriptEngineManager(void);
  743. /**
  744. * Get the ScriptEngineProtocol object.
  745. *
  746. * @return the ScriptEngineProtocol object.
  747. *
  748. * @lua NA
  749. * @js NA
  750. */
  751. ScriptEngineProtocol* getScriptEngine(void) {
  752. return _scriptEngine;
  753. }
  754. /**
  755. * Set the ScriptEngineProtocol object should be managed.
  756. *
  757. * @param scriptEngine should be managed.
  758. *
  759. * @lua NA
  760. * @js NA
  761. */
  762. void setScriptEngine(ScriptEngineProtocol *scriptEngine);
  763. /**
  764. * Remove the ScriptEngineProtocol object managed.
  765. *
  766. *
  767. * @lua NA
  768. * @js NA
  769. */
  770. void removeScriptEngine(void);
  771. /**
  772. * Get the instance of ScriptEngineManager object.
  773. *
  774. * @return the instance of ScriptEngineManager object.
  775. *
  776. * @lua NA
  777. * @js NA
  778. */
  779. static ScriptEngineManager* getInstance();
  780. /**
  781. * Destroy the singleton about ScriptEngineManager.
  782. *
  783. * @lua NA
  784. * @js NA
  785. */
  786. static void destroyInstance();
  787. /**
  788. *
  789. *
  790. * @lua NA
  791. * @js NA
  792. */
  793. static bool sendActionEventToJS(Action* actionObject, int eventType, void* param);
  794. /**
  795. *
  796. *
  797. * @lua NA
  798. * @js NA
  799. */
  800. static bool sendNodeEventToJS(Node* node, int action);
  801. /**
  802. *
  803. *
  804. * @lua NA
  805. * @js NA
  806. */
  807. static bool sendNodeEventToJSExtended(Node* node, int action);
  808. /**
  809. * Call the Lua function when the event of node is triggered.
  810. *
  811. * @param node the nativeobject triggers the event.
  812. * @param action the specific type.
  813. *
  814. * @lua NA
  815. * @js NA
  816. */
  817. static void sendNodeEventToLua(Node* node, int action);
  818. /**
  819. * @deprecated Use getInstance() instead.
  820. *
  821. * @lua NA
  822. * @js NA
  823. */
  824. CC_DEPRECATED_ATTRIBUTE static ScriptEngineManager* sharedManager() { return ScriptEngineManager::getInstance(); };
  825. /**
  826. * @deprecated Use destroyInstance() instead.
  827. *
  828. * @lua NA
  829. * @js NA
  830. */
  831. CC_DEPRECATED_ATTRIBUTE static void purgeSharedManager() { ScriptEngineManager::destroyInstance(); };
  832. private:
  833. ScriptEngineManager(void)
  834. : _scriptEngine(nullptr)
  835. {
  836. }
  837. ScriptEngineProtocol *_scriptEngine;
  838. };
  839. NS_CC_END
  840. #endif // #if CC_ENABLE_SCRIPT_BINDING
  841. // end group
  842. /// @}
  843. #endif // __SCRIPT_SUPPORT_H__