CCScriptSupport.h 23 KB

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