Blame view

ios/cocos2d/cocos/base/CCScriptSupport.cpp 6.31 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  /****************************************************************************
   Copyright (c) 2010-2012 cocos2d-x.org
   Copyright (c) 2013-2016 Chukong Technologies Inc.
   Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
   
   http://www.cocos2d-x.org
   
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
   ****************************************************************************/
  
  #include "base/CCScriptSupport.h"
  
  #if CC_ENABLE_SCRIPT_BINDING
  
  #include "base/CCScheduler.h"
  #include "2d/CCNode.h"
  
  bool CC_DLL cc_assert_script_compatible(const char *msg)
  {
      cocos2d::ScriptEngineProtocol* engine = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine();
      if (engine && engine->handleAssert(msg))
      {
          return true;
      }
      return false;
  }
  
  NS_CC_BEGIN
  
  // 
  // // ScriptHandlerEntry
  
  ScriptHandlerEntry* ScriptHandlerEntry::create(int handler)
  {
      ScriptHandlerEntry* entry = new (std::nothrow) ScriptHandlerEntry(handler);
      entry->autorelease();
      return entry;
  }
  
  ScriptHandlerEntry::~ScriptHandlerEntry()
  {
      if (_handler != 0 )
      {
          ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_handler);
          LUALOG("[LUA] Remove event handler: %d", _handler);
          _handler = 0;
      }
  }
  
  // 
  // // SchedulerScriptHandlerEntry
  
  SchedulerScriptHandlerEntry* SchedulerScriptHandlerEntry::create(int handler, float interval, bool paused)
  {
      SchedulerScriptHandlerEntry* entry = new (std::nothrow) SchedulerScriptHandlerEntry(handler);
      entry->init(interval, paused);
      entry->autorelease();
      return entry;
  }
  
  bool SchedulerScriptHandlerEntry::init(float interval, bool paused)
  {
      _timer = new (std::nothrow) TimerScriptHandler();
      _timer->initWithScriptHandler(_handler, interval);
      _paused = paused;
      LUALOG("[LUA] ADD script schedule: %d, entryID: %d", _handler, _entryId);
      return true;
  }
  
  SchedulerScriptHandlerEntry::~SchedulerScriptHandlerEntry()
  {
      _timer->release();
      LUALOG("[LUA] DEL script schedule %d, entryID: %d", _handler, _entryId);
  }
  
  
  // 
  // // TouchScriptHandlerEntry
  
  TouchScriptHandlerEntry* TouchScriptHandlerEntry::create(int handler,
                                                               bool isMultiTouches,
                                                               int priority,
                                                               bool swallowsTouches)
  {
      TouchScriptHandlerEntry* entry = new (std::nothrow) TouchScriptHandlerEntry(handler);
      entry->init(isMultiTouches, priority, swallowsTouches);
      entry->autorelease();
      return entry;
  }
  
  TouchScriptHandlerEntry::~TouchScriptHandlerEntry()
  {
  }
  
  bool TouchScriptHandlerEntry::init(bool isMultiTouches, int priority, bool swallowsTouches)
  {
      _isMultiTouches = isMultiTouches;
      _priority = priority;
      _swallowsTouches = swallowsTouches;
      
      return true;
  }
  
  // 
  // // ScriptEngineManager
  
  static ScriptEngineManager* s_pSharedScriptEngineManager = nullptr;
  
  
  ScriptEngineManager::~ScriptEngineManager()
  {
      removeScriptEngine();
  }
  
  void ScriptEngineManager::setScriptEngine(ScriptEngineProtocol *scriptEngine)
  {
  	if (_scriptEngine != scriptEngine)
  	{
  		removeScriptEngine();
  		_scriptEngine = scriptEngine;
  	}
  }
  
  void ScriptEngineManager::removeScriptEngine()
  {
      if (_scriptEngine)
      {
          delete _scriptEngine;
          _scriptEngine = nullptr;
      }
  }
  
  ScriptEngineManager* ScriptEngineManager::getInstance()
  {
      if (!s_pSharedScriptEngineManager)
      {
          s_pSharedScriptEngineManager = new (std::nothrow) ScriptEngineManager();
      }
      return s_pSharedScriptEngineManager;
  }
  
  void ScriptEngineManager::destroyInstance()
  {
      if (s_pSharedScriptEngineManager)
      {
          delete s_pSharedScriptEngineManager;
          s_pSharedScriptEngineManager = nullptr;
      }
  }
  
  bool ScriptEngineManager::sendActionEventToJS(Action* actionObject, int eventType, void* param)
  {
      auto scriptEngine = getInstance()->getScriptEngine();
      
      ActionObjectScriptData data(actionObject,(int*)&eventType, param);
      ScriptEvent scriptEvent(kScriptActionEvent,(void*)&data);
      if (scriptEngine->sendEvent(&scriptEvent))
          return true;
      
      return false;
  }
  
  bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action)
  {
      auto scriptEngine = getInstance()->getScriptEngine();
      
      if (scriptEngine->isCalledFromScript())
      {
          // Should only be invoked at root class Node
          scriptEngine->setCalledFromScript(false);
      }
      else
      {
          BasicScriptData data(node,(void*)&action);
          ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
          if (scriptEngine->sendEvent(&scriptEvent))
              return true;
      }
      
      return false;
  }
  
  bool ScriptEngineManager::sendNodeEventToJSExtended(Node* node, int action)
  {
      auto scriptEngine = getInstance()->getScriptEngine();
      
      if (!scriptEngine->isCalledFromScript())
      {
          BasicScriptData data(node,(void*)&action);
          ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
          if (scriptEngine->sendEvent(&scriptEvent))
              return true;
      }
      
      return false;
  }
  
  void ScriptEngineManager::sendNodeEventToLua(Node* node, int action)
  {
      auto scriptEngine = getInstance()->getScriptEngine();
      
      BasicScriptData data(node,(void*)&action);
      ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
      
      scriptEngine->sendEvent(&scriptEvent);
  }
  
  NS_CC_END
  
  #endif // #if CC_ENABLE_SCRIPT_BINDING