UIWidget.js 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 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. /**
  22. * The base class for ccui controls and layout
  23. * @sample
  24. * var uiWidget = ccui.Widget.create();
  25. * this.addChild(uiWidget);
  26. * @class
  27. * @extends ccui.ProtectedNode
  28. *
  29. * @property {Number} xPercent - Position x in percentage of width
  30. * @property {Number} yPercent - Position y in percentage of height
  31. * @property {Number} widthPercent - Width in percentage of parent width
  32. * @property {Number} heightPercent - Height in percentage of parent height
  33. * @property {ccui.Widget} widgetParent - <@readonly> The direct parent when it's a widget also, otherwise equals null
  34. * @property {Boolean} enabled - Indicate whether the widget is enabled
  35. * @property {Boolean} focused - Indicate whether the widget is focused
  36. * @property {ccui.Widget.SIZE_ABSOLUTE|ccui.Widget.SIZE_PERCENT} sizeType - The size type of the widget
  37. * @property {ccui.Widget.TYPE_WIDGET|ccui.Widget.TYPE_CONTAINER} widgetType - <@readonly> The type of the widget
  38. * @property {Boolean} touchEnabled - Indicate whether touch events are enabled
  39. * @property {Boolean} updateEnabled - Indicate whether the update function is scheduled
  40. * @property {Boolean} bright - Indicate whether the widget is bright
  41. * @property {String} name - The name of the widget
  42. * @property {Number} actionTag - The action tag of the widget
  43. */
  44. ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
  45. _enabled: true, ///< Highest control of widget
  46. _bright: true, ///< is this widget bright
  47. _touchEnabled: false, ///< is this widget touch endabled
  48. _brightStyle: null, ///< bright style
  49. _touchBeganPosition: null, ///< touch began point
  50. _touchMovePosition: null, ///< touch moved point
  51. _touchEndPosition: null, ///< touch ended point
  52. _touchEventListener: null,
  53. _touchEventSelector: null,
  54. _name: "default",
  55. _widgetType: null,
  56. _actionTag: 0,
  57. _customSize: null,
  58. _layoutParameterDictionary: null,
  59. _layoutParameterType:0,
  60. _focused: false,
  61. _focusEnabled: true,
  62. _ignoreSize: false,
  63. _affectByClipping: false,
  64. _sizeType: null,
  65. _sizePercent: null,
  66. _positionType: null,
  67. _positionPercent: null,
  68. _reorderWidgetChildDirty: false,
  69. _hit: false,
  70. _nodes: null,
  71. _touchListener: null,
  72. _className: "Widget",
  73. _flippedX: false,
  74. _flippedY: false,
  75. _opacity: 255,
  76. _highlight: false,
  77. _touchEventCallback: null,
  78. /**
  79. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  80. * @function
  81. */
  82. ctor: function () {
  83. cc.ProtectedNode.prototype.ctor.call(this);
  84. this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE;
  85. this._touchBeganPosition = cc.p(0, 0);
  86. this._touchMovePosition = cc.p(0, 0);
  87. this._touchEndPosition = cc.p(0, 0);
  88. this._widgetType = ccui.Widget.TYPE_WIDGET;
  89. this._customSize = cc.size(0, 0);
  90. this._layoutParameterDictionary = {};
  91. this._sizeType = ccui.Widget.SIZE_ABSOLUTE;
  92. this._sizePercent = cc.p(0, 0);
  93. this._positionType = ccui.Widget.POSITION_ABSOLUTE;
  94. this._positionPercent = cc.p(0, 0);
  95. this._nodes = [];
  96. this._layoutParameterType = ccui.LayoutParameter.NONE;
  97. this.init(); //TODO
  98. },
  99. /**
  100. * initializes state of widget. please do not call this function by yourself, you should pass the parameters to constructor to initialize it
.
  101. * @returns {boolean}
  102. */
  103. init: function () {
  104. if (cc.ProtectedNode.prototype.init.call(this)) {
  105. this._layoutParameterDictionary = {};
  106. this._initRenderer();
  107. this.setBright(true);
  108. this.onFocusChanged = this.onFocusChange.bind(this);
  109. this.onNextFocusedWidget = null;
  110. this.setAnchorPoint(cc.p(0.5, 0.5));
  111. this.ignoreContentAdaptWithSize(true);
  112. return true;
  113. }
  114. return false;
  115. },
  116. /**
  117. * Calls updateSizeAndPosition and its parent's onEnter
  118. * @override
  119. */
  120. onEnter: function () {
  121. this.updateSizeAndPosition();
  122. cc.ProtectedNode.prototype.onEnter.call(this);
  123. },
  124. /**
  125. * Calls unscheduleUpdate and its parent's onExit
  126. * @override
  127. */
  128. onExit: function(){
  129. this.unscheduleUpdate();
  130. cc.ProtectedNode.prototype.onExit.call(this);
  131. },
  132. /**
  133. * Calls _adaptRenderers(its subClass will override it) before calls its parent's visit.
  134. * @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx
  135. * @override
  136. */
  137. visit: function (ctx) {
  138. if (this._visible) {
  139. this._adaptRenderers();
  140. cc.ProtectedNode.prototype.visit.call(this, ctx);
  141. }
  142. },
  143. /**
  144. * The direct parent when it's a widget also, otherwise equals null
  145. * @returns {ccui.Widget|null}
  146. */
  147. getWidgetParent: function () {
  148. var widget = this.getParent();
  149. if (widget instanceof ccui.Widget)
  150. return widget;
  151. return null;
  152. },
  153. _updateContentSizeWithTextureSize: function(size){
  154. this.setContentSize(this._ignoreSize ? size : this._customSize);
  155. },
  156. _isAncestorsEnabled: function(){
  157. var parentWidget = this._getAncensterWidget(this);
  158. if (parentWidget == null)
  159. return true;
  160. if (parentWidget && !parentWidget.isEnabled())
  161. return false;
  162. return parentWidget._isAncestorsEnabled();
  163. },
  164. _getAncensterWidget: function(node){
  165. if (null == node)
  166. return null;
  167. var parent = node.getParent();
  168. if (null == parent)
  169. return null;
  170. if (parent instanceof ccui.Widget)
  171. return parent;
  172. else
  173. return this._getAncensterWidget(parent.getParent());
  174. },
  175. _isAncestorsVisible: function(node){
  176. if (null == node)
  177. return true;
  178. var parent = node.getParent();
  179. if (parent && !parent.isVisible())
  180. return false;
  181. return this._isAncestorsVisible(parent);
  182. },
  183. _cleanupWidget: function(){
  184. //clean up _touchListener
  185. this._eventDispatcher.removeEventListener(this._touchListener);
  186. //cleanup focused widget and focus navigation controller
  187. if (ccui.Widget._focusedWidget == this)
  188. ccui.Widget._focusedWidget = null;
  189. },
  190. /**
  191. * <p>
  192. * Sets whether the widget is enabled <br/>
  193. * true if the widget is enabled, widget may be touched , false if the widget is disabled, widget cannot be touched. <br/>
  194. * The default value is true, a widget is default to enabled
  195. * </p>
  196. * @param {Boolean} enabled
  197. */
  198. setEnabled: function (enabled) {
  199. this._enabled = enabled;
  200. },
  201. /**
  202. * initializes renderer of widget.
  203. */
  204. _initRenderer: function () {
  205. },
  206. /**
  207. * Sets _customSize of ccui.Widget, if ignoreSize is true, the content size is its renderer's contentSize, otherwise the content size is parameter.
  208. * and updates size percent by parent content size. At last, updates its children's size and position.
  209. * @param {cc.Size|Number} contentSize content size or width of content size
  210. * @param {Number} [height]
  211. * @override
  212. */
  213. setContentSize: function(contentSize, height){
  214. var locWidth = (height === undefined) ? contentSize.width : contentSize;
  215. var locHeight = (height === undefined) ? contentSize.height : height;
  216. cc.Node.prototype.setContentSize.call(this, locWidth, locHeight);
  217. this._customSize.width = locWidth;
  218. this._customSize.height = locHeight;
  219. if (this._ignoreSize)
  220. this._contentSize = this.getVirtualRendererSize();
  221. if (this._running) {
  222. var widgetParent = this.getWidgetParent();
  223. var pSize = widgetParent ? widgetParent.getContentSize() : this._parent.getContentSize();
  224. this._sizePercent.x = (pSize.width > 0.0) ? locWidth / pSize.width : 0.0;
  225. this._sizePercent.y = (pSize.height > 0.0) ? locHeight / pSize.height : 0.0;
  226. }
  227. this._onSizeChanged();
  228. },
  229. _setWidth: function (w) {
  230. cc.Node.prototype._setWidth.call(this, w);
  231. this._customSize.width = w;
  232. if(this._ignoreSize)
  233. this._contentSize = this.getVirtualRendererSize();
  234. if (this._running) {
  235. var widgetParent = this.getWidgetParent();
  236. var locWidth = widgetParent ? widgetParent.width : this._parent.width;
  237. this._sizePercent.x = locWidth > 0 ? this._customSize.width / locWidth : 0;
  238. }
  239. this._onSizeChanged();
  240. },
  241. _setHeight: function (h) {
  242. cc.Node.prototype._setHeight.call(this, h);
  243. this._customSize.height = h;
  244. if(this._ignoreSize)
  245. this._contentSize = this.getVirtualRendererSize();
  246. if (this._running) {
  247. var widgetParent = this.getWidgetParent();
  248. var locH = widgetParent ? widgetParent.height : this._parent.height;
  249. this._sizePercent.y = locH > 0 ? this._customSize.height / locH : 0;
  250. }
  251. this._onSizeChanged();
  252. },
  253. /**
  254. * Changes the percent that is widget's percent size
  255. * @param {cc.Point} percent that is widget's percent size, width and height value from 0 to 1.
  256. */
  257. setSizePercent: function (percent) {
  258. this._sizePercent.x = percent.x;
  259. this._sizePercent.y = percent.y;
  260. var width = this._customSize.width, height = this._customSize.height;
  261. if (this._running) {
  262. var widgetParent = this.getWidgetParent();
  263. if (widgetParent) {
  264. width = widgetParent.width * percent.x;
  265. height = widgetParent.height * percent.y;
  266. } else {
  267. width = this._parent.width * percent.x;
  268. height = this._parent.height * percent.y;
  269. }
  270. }
  271. if (this._ignoreSize)
  272. this.setContentSize(this.getVirtualRendererSize());
  273. else
  274. this.setContentSize(width, height);
  275. this._customSize.width = width;
  276. this._customSize.height = height;
  277. },
  278. _setWidthPercent: function (percent) {
  279. this._sizePercent.x = percent;
  280. var width = this._customSize.width;
  281. if (this._running) {
  282. var widgetParent = this.getWidgetParent();
  283. width = (widgetParent ? widgetParent.width : this._parent.width) * percent;
  284. }
  285. if (this._ignoreSize)
  286. this._setWidth(this.getVirtualRendererSize().width);
  287. else
  288. this._setWidth(width);
  289. this._customSize.width = width;
  290. },
  291. _setHeightPercent: function (percent) {
  292. this._sizePercent.y = percent;
  293. var height = this._customSize.height;
  294. if (this._running) {
  295. var widgetParent = this.getWidgetParent();
  296. height = (widgetParent ? widgetParent.height : this._parent.height) * percent;
  297. }
  298. if (this._ignoreSize)
  299. this._setHeight(this.getVirtualRendererSize().height);
  300. else
  301. this._setHeight(height);
  302. this._customSize.height = height;
  303. },
  304. /**
  305. * updates its size by size type and its position by position type.
  306. * @param {cc.Size} [parentSize] parent size
  307. */
  308. updateSizeAndPosition: function (parentSize) {
  309. if(!parentSize){
  310. var widgetParent = this.getWidgetParent();
  311. if(widgetParent)
  312. parentSize = widgetParent.getLayoutSize();
  313. else
  314. parentSize = this._parent.getContentSize();
  315. }
  316. switch (this._sizeType) {
  317. case ccui.Widget.SIZE_ABSOLUTE:
  318. if(this._ignoreSize)
  319. this.setContentSize(this.getVirtualRendererSize());
  320. else
  321. this.setContentSize(this._customSize);
  322. this._sizePercent.x = (parentSize.width > 0) ? this._customSize.width / parentSize.width : 0;
  323. this._sizePercent.y = (parentSize.height > 0) ? this._customSize.height / parentSize.height : 0;
  324. break;
  325. case ccui.Widget.SIZE_PERCENT:
  326. var cSize = cc.size(parentSize.width * this._sizePercent.x , parentSize.height * this._sizePercent.y);
  327. if(this._ignoreSize)
  328. this.setContentSize(this.getVirtualRendererSize());
  329. else
  330. this.setContentSize(cSize);
  331. this._customSize.width = cSize.width;
  332. this._customSize.height = cSize.height;
  333. break;
  334. default:
  335. break;
  336. }
  337. this._onSizeChanged();
  338. var absPos = this.getPosition();
  339. switch (this._positionType) {
  340. case ccui.Widget.POSITION_ABSOLUTE:
  341. if (parentSize.width <= 0 || parentSize.height <= 0) {
  342. this._positionPercent.x = this._positionPercent.y = 0;
  343. } else {
  344. this._positionPercent.x = absPos.x / parentSize.width;
  345. this._positionPercent.y = absPos.y / parentSize.height;
  346. }
  347. break;
  348. case ccui.Widget.POSITION_PERCENT:
  349. absPos = cc.p(parentSize.width * this._positionPercent.x, parentSize.height * this._positionPercent.y);
  350. break;
  351. default:
  352. break;
  353. }
  354. if(this._parent instanceof ccui.ImageView){
  355. var renderer = this._parent._imageRenderer;
  356. if(renderer && !renderer._textureLoaded)
  357. return;
  358. }
  359. this.setPosition(absPos);
  360. },
  361. /**TEXTURE_RES_TYPE
  362. * Changes the size type of widget.
  363. * @param {ccui.Widget.SIZE_ABSOLUTE|ccui.Widget.SIZE_PERCENT} type that is widget's size type
  364. */
  365. setSizeType: function (type) {
  366. this._sizeType = type;
  367. },
  368. /**
  369. * Gets the size type of widget.
  370. * @returns {ccui.Widget.SIZE_ABSOLUTE|ccui.Widget.SIZE_PERCENT} that is widget's size type
  371. */
  372. getSizeType: function () {
  373. return this._sizeType;
  374. },
  375. /**
  376. * Ignore the widget size
  377. * @param {Boolean} ignore true that widget will ignore it's size, use texture size, false otherwise. Default value is true.
  378. */
  379. ignoreContentAdaptWithSize: function (ignore) {
  380. if(this._ignoreSize == ignore)
  381. return;
  382. this._ignoreSize = ignore;
  383. this.setContentSize( ignore ? this.getVirtualRendererSize() : this._customSize );
  384. this._onSizeChanged();
  385. },
  386. /**
  387. * Gets whether ignore the content size (custom size)
  388. * @returns {boolean} true that widget will ignore it's size, use texture size, false otherwise.
  389. */
  390. isIgnoreContentAdaptWithSize: function () {
  391. return this._ignoreSize;
  392. },
  393. /**
  394. * Get custom size of ccui.Widget
  395. * @returns {cc.Size}
  396. */
  397. getCustomSize: function () {
  398. return cc.size(this._customSize);
  399. },
  400. /**
  401. * Gets layout size of ccui.Widget.
  402. * @returns {cc.Size}
  403. */
  404. getLayoutSize: function(){
  405. return cc.size(this._contentSize);
  406. },
  407. /**
  408. * Returns size percent of ccui.Widget
  409. * @returns {cc.Point}
  410. */
  411. getSizePercent: function () {
  412. return cc.p(this._sizePercent);
  413. },
  414. _getWidthPercent: function () {
  415. return this._sizePercent.x;
  416. },
  417. _getHeightPercent: function () {
  418. return this._sizePercent.y;
  419. },
  420. /**
  421. * Gets world position of ccui.Widget.
  422. * @returns {cc.Point} world position of ccui.Widget.
  423. */
  424. getWorldPosition: function () {
  425. return this.convertToWorldSpace(cc.p(this._anchorPoint.x * this._contentSize.width, this._anchorPoint.y * this._contentSize.height));
  426. },
  427. /**
  428. * Gets the Virtual Renderer of widget.
  429. * @returns {ccui.Widget}
  430. */
  431. getVirtualRenderer: function () {
  432. return this;
  433. },
  434. /**
  435. * Gets the content size of widget. Content size is widget's texture size.
  436. */
  437. getVirtualRendererSize:function(){
  438. return cc.size(this._contentSize);
  439. },
  440. /**
  441. * call back function called when size changed.
  442. */
  443. _onSizeChanged: function () {
  444. var locChildren = this.getChildren();
  445. for (var i = 0, len = locChildren.length; i < len; i++) {
  446. var child = locChildren[i];
  447. if(child instanceof ccui.Widget)
  448. child.updateSizeAndPosition();
  449. }
  450. },
  451. /**
  452. * Sets whether the widget is touch enabled. The default value is false, a widget is default to touch disabled
  453. * @param {Boolean} enable true if the widget is touch enabled, false if the widget is touch disabled.
  454. */
  455. setTouchEnabled: function (enable) {
  456. if (this._touchEnabled === enable)
  457. return;
  458. this._touchEnabled = enable; //TODO need consider remove and re-add.
  459. if (this._touchEnabled) {
  460. this._touchListener = cc.EventListener.create({
  461. event: cc.EventListener.TOUCH_ONE_BY_ONE,
  462. swallowTouches: true,
  463. onTouchBegan: this.onTouchBegan.bind(this),
  464. onTouchMoved: this.onTouchMoved.bind(this),
  465. onTouchEnded: this.onTouchEnded.bind(this)
  466. });
  467. cc.eventManager.addListener(this._touchListener, this);
  468. } else {
  469. cc.eventManager.removeListener(this._touchListener);
  470. }
  471. },
  472. /**
  473. * Returns whether or not touch is enabled.
  474. * @returns {boolean} true if the widget is touch enabled, false if the widget is touch disabled.
  475. */
  476. isTouchEnabled: function () {
  477. return this._touchEnabled;
  478. },
  479. /**
  480. * Determines if the widget is highlighted
  481. * @returns {boolean} true if the widget is highlighted, false if the widget is not highlighted .
  482. */
  483. isHighlighted: function(){
  484. return this._highlight;
  485. },
  486. /**
  487. * Sets whether the widget is highlighted. The default value is false, a widget is default to not highlighted
  488. * @param highlight true if the widget is highlighted, false if the widget is not highlighted.
  489. */
  490. setHighlighted:function(highlight){
  491. if (highlight == this._highlight)
  492. return;
  493. this._highlight = highlight;
  494. if (this._bright) {
  495. if (this._highlight)
  496. this.setBrightStyle(ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT);
  497. else
  498. this.setBrightStyle(ccui.Widget.BRIGHT_STYLE_NORMAL);
  499. } else
  500. this._onPressStateChangedToDisabled();
  501. },
  502. /**
  503. * Determines if the widget is on focused
  504. * @returns {boolean} whether the widget is focused or not
  505. */
  506. isFocused: function () {
  507. return this._focused;
  508. },
  509. /**
  510. * Sets whether the widget is on focused
  511. * The default value is false, a widget is default to not on focused
  512. * @param {boolean} focus pass true to let the widget get focus or pass false to let the widget lose focus
  513. */
  514. setFocused: function (focus) {
  515. this._focused = focus;
  516. //make sure there is only one focusedWidget
  517. if (focus)
  518. ccui.Widget._focusedWidget = this;
  519. },
  520. /**
  521. * returns whether the widget could accept focus.
  522. * @returns {boolean} true represent the widget could accept focus, false represent the widget couldn't accept focus
  523. */
  524. isFocusEnabled: function(){
  525. return this._focusEnabled;
  526. },
  527. /**
  528. * sets whether the widget could accept focus.
  529. * @param {Boolean} enable true represent the widget could accept focus, false represent the widget couldn't accept focus
  530. */
  531. setFocusEnabled: function(enable){
  532. this._focused = enable;
  533. },
  534. /**
  535. * <p>
  536. * When a widget is in a layout, you could call this method to get the next focused widget within a specified direction. <br/>
  537. * If the widget is not in a layout, it will return itself
  538. * </p>
  539. * @param direction the direction to look for the next focused widget in a layout
  540. * @param current the current focused widget
  541. * @return the next focused widget in a layout
  542. */
  543. findNextFocusedWidget: function( direction, current){
  544. if (null == this.onNextFocusedWidget || null == this.onNextFocusedWidget(direction) ) {
  545. var isLayout = current instanceof ccui.Layout;
  546. if (this.isFocused() || isLayout) {
  547. var layout = this.getParent();
  548. if (null == layout){
  549. //the outer layout's default behaviour is : loop focus
  550. if (isLayout)
  551. return current.findNextFocusedWidget(direction, current);
  552. return current;
  553. } else
  554. return layout.findNextFocusedWidget(direction, current);
  555. } else
  556. return current;
  557. } else {
  558. var getFocusWidget = this.onNextFocusedWidget(direction);
  559. this.dispatchFocusEvent(this, getFocusWidget);
  560. return getFocusWidget;
  561. }
  562. },
  563. /**
  564. * when a widget calls this method, it will get focus immediately.
  565. */
  566. requestFocus: function(){
  567. if (this == ccui.Widget._focusedWidget)
  568. return;
  569. this.dispatchFocusEvent(ccui.Widget._focusedWidget, this);
  570. },
  571. /**
  572. * no matter what widget object you call this method on , it will return you the exact one focused widget
  573. */
  574. getCurrentFocusedWidget: function(){
  575. return ccui.Widget._focusedWidget;
  576. },
  577. /**
  578. * call this method with parameter true to enable the Android Dpad focus navigation feature
  579. * @note it doesn't implemented on Web
  580. * @param {Boolean} enable set true to enable dpad focus navigation, otherwise disable dpad focus navigation
  581. */
  582. enableDpadNavigation: function(enable){
  583. },
  584. /**
  585. * <p>
  586. * When a widget lose/get focus, this method will be called. Be Caution when you provide your own version, <br/>
  587. * you must call widget.setFocused(true/false) to change the focus state of the current focused widget;
  588. * </p>
  589. */
  590. onFocusChanged: null,
  591. /**
  592. * use this function to manually specify the next focused widget regards to each direction
  593. */
  594. onNextFocusedWidget: null,
  595. /**
  596. * Sends the touch event to widget's parent, its subclass will override it, e.g. ccui.ScrollView, ccui.PageView
  597. * @param {Number} eventType
  598. * @param {ccui.Widget} sender
  599. * @param {cc.Touch} touch
  600. */
  601. interceptTouchEvent: function(eventType, sender, touch){
  602. var widgetParent = this.getWidgetParent();
  603. if (widgetParent)
  604. widgetParent.interceptTouchEvent(eventType,sender,touch);
  605. },
  606. /**
  607. * This method is called when a focus change event happens
  608. * @param {ccui.Widget} widgetLostFocus
  609. * @param {ccui.Widget} widgetGetFocus
  610. */
  611. onFocusChange: function(widgetLostFocus, widgetGetFocus){
  612. //only change focus when there is indeed a get&lose happens
  613. if (widgetLostFocus)
  614. widgetLostFocus.setFocused(false);
  615. if (widgetGetFocus)
  616. widgetGetFocus.setFocused(true);
  617. },
  618. /**
  619. * Dispatch a EventFocus through a EventDispatcher
  620. * @param {ccui.Widget} widgetLostFocus
  621. * @param {ccui.Widget} widgetGetFocus
  622. */
  623. dispatchFocusEvent: function(widgetLostFocus, widgetGetFocus){
  624. //if the widgetLoseFocus doesn't get focus, it will use the previous focused widget instead
  625. if (widgetLostFocus && !widgetLostFocus.isFocused())
  626. widgetLostFocus = ccui.Widget._focusedWidget;
  627. if (widgetGetFocus != widgetLostFocus){
  628. if (widgetGetFocus && widgetGetFocus.onFocusChanged)
  629. widgetGetFocus.onFocusChanged(widgetLostFocus, widgetGetFocus);
  630. if (widgetLostFocus && widgetGetFocus.onFocusChanged)
  631. widgetLostFocus.onFocusChanged(widgetLostFocus, widgetGetFocus);
  632. cc.eventManager.dispatchEvent(new cc.EventFocus(widgetLostFocus, widgetGetFocus));
  633. }
  634. },
  635. /**
  636. * Sets whether the widget is bright. The default value is true, a widget is default to bright
  637. * @param {Boolean} bright true if the widget is bright, false if the widget is dark.
  638. */
  639. setBright: function (bright) {
  640. this._bright = bright;
  641. if (this._bright) {
  642. this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE;
  643. this.setBrightStyle(ccui.Widget.BRIGHT_STYLE_NORMAL);
  644. } else
  645. this._onPressStateChangedToDisabled();
  646. },
  647. /**
  648. * To set the bright style of ccui.Widget.
  649. * @param {Number} style BRIGHT_NORMAL the widget is normal state, BRIGHT_HIGHLIGHT the widget is height light state.
  650. */
  651. setBrightStyle: function (style) {
  652. if (this._brightStyle == style) {
  653. return;
  654. }
  655. style = style || ccui.Widget.BRIGHT_STYLE_NORMAL;
  656. this._brightStyle = style;
  657. switch (this._brightStyle) {
  658. case ccui.Widget.BRIGHT_STYLE_NORMAL:
  659. this._onPressStateChangedToNormal();
  660. break;
  661. case ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT:
  662. this._onPressStateChangedToPressed();
  663. break;
  664. default:
  665. break;
  666. }
  667. },
  668. _onPressStateChangedToNormal: function () {
  669. },
  670. _onPressStateChangedToPressed: function () {
  671. },
  672. _onPressStateChangedToDisabled: function () {
  673. },
  674. /**
  675. * A call back function when widget lost of focus.
  676. */
  677. didNotSelectSelf: function () {
  678. },
  679. /**
  680. * <p>
  681. * The callback of touch began event. <br/>
  682. * If the bounding box of ccui.Widget contains the touch point, it will do the following things: <br/>
  683. * 1. sets highlight state, <br/>
  684. * 2. sends event to parent widget by interceptTouchEvent <br/>
  685. * 3. calls the callback of touch began event. <br/>
  686. * 4. returns true, <br/>
  687. * otherwise returns false directly. <br/>
  688. * </p>
  689. * @override
  690. * @param {cc.Touch} touch
  691. * @param {cc.Event} event
  692. * @returns {boolean}
  693. */
  694. onTouchBegan: function (touch, event) {
  695. this._hit = false;
  696. if (this.isVisible() && this.isEnabled() && this._isAncestorsEnabled() && this._isAncestorsVisible(this) ){
  697. var touchPoint = touch.getLocation();
  698. this._touchBeganPosition.x = touchPoint.x;
  699. this._touchBeganPosition.y = touchPoint.y;
  700. if(this.hitTest(this._touchBeganPosition) && this.isClippingParentContainsPoint(this._touchBeganPosition))
  701. this._hit = true;
  702. }
  703. if (!this._hit) {
  704. return false;
  705. }
  706. this.setHighlighted(true);
  707. var widgetParent = this.getWidgetParent();
  708. if (widgetParent)
  709. widgetParent.interceptTouchEvent(ccui.Widget.TOUCH_BEGAN, this, touch);
  710. this._pushDownEvent();
  711. return true;
  712. },
  713. /**
  714. * <p>
  715. * The callback of touch moved event. <br/>
  716. * It sets the highlight state by touch, sends event to parent widget by interceptTouchEvent and calls the callback of touch moved event.
  717. * </p>
  718. * @param {cc.Touch} touch
  719. * @param {cc.Event} event
  720. */
  721. onTouchMoved: function (touch, event) {
  722. var touchPoint = touch.getLocation();
  723. this._touchMovePosition.x = touchPoint.x;
  724. this._touchMovePosition.y = touchPoint.y;
  725. this.setHighlighted(this.hitTest(touchPoint));
  726. var widgetParent = this.getWidgetParent();
  727. if (widgetParent)
  728. widgetParent.interceptTouchEvent(ccui.Widget.TOUCH_MOVED, this, touch);
  729. this._moveEvent();
  730. },
  731. /**
  732. * <p>
  733. * The callback of touch end event
  734. * It sends event to parent widget by interceptTouchEvent,
  735. * calls the callback of touch end event (highlight= true) or touch canceled event (highlight= false).
  736. * sets the highlight state to false ,
  737. * </p>
  738. * @param touch
  739. * @param event
  740. */
  741. onTouchEnded: function (touch, event) {
  742. var touchPoint = touch.getLocation();
  743. this._touchEndPosition.x = touchPoint.x;
  744. this._touchEndPosition.y = touchPoint.y;
  745. var widgetParent = this.getWidgetParent();
  746. if (widgetParent)
  747. widgetParent.interceptTouchEvent(ccui.Widget.TOUCH_ENDED, this, touch);
  748. var highlight = this._highlight;
  749. this.setHighlighted(false);
  750. if (highlight)
  751. this._releaseUpEvent();
  752. else
  753. this._cancelUpEvent();
  754. },
  755. /**
  756. * A call back function called when widget is selected, and on touch canceled.
  757. * @param {cc.Point} touchPoint
  758. */
  759. onTouchCancelled: function (touchPoint) {
  760. this.setHighlighted(false);
  761. this._cancelUpEvent();
  762. },
  763. /**
  764. * A call back function called when widget is selected, and on touch long clicked.
  765. * @param {cc.Point} touchPoint
  766. */
  767. onTouchLongClicked: function (touchPoint) {
  768. this.longClickEvent();
  769. },
  770. //call back function called widget's state changed to dark.
  771. _pushDownEvent: function () {
  772. if (this._touchEventCallback)
  773. this._touchEventCallback(this, ccui.Widget.TOUCH_BEGAN);
  774. if (this._touchEventListener && this._touchEventSelector)
  775. this._touchEventSelector.call(this._touchEventListener, this, ccui.Widget.TOUCH_BEGAN);
  776. },
  777. _moveEvent: function () {
  778. if (this._touchEventCallback)
  779. this._touchEventCallback(this, ccui.Widget.TOUCH_MOVED);
  780. if (this._touchEventListener && this._touchEventSelector)
  781. this._touchEventSelector.call(this._touchEventListener, this, ccui.Widget.TOUCH_MOVED);
  782. },
  783. _releaseUpEvent: function () {
  784. if (this._touchEventCallback)
  785. this._touchEventCallback(this, ccui.Widget.TOUCH_ENDED);
  786. if (this._touchEventListener && this._touchEventSelector)
  787. this._touchEventSelector.call(this._touchEventListener, this, ccui.Widget.TOUCH_ENDED);
  788. },
  789. _cancelUpEvent: function () {
  790. if (this._touchEventCallback)
  791. this._touchEventCallback(this, ccui.Widget.TOUCH_CANCELED);
  792. if (this._touchEventListener && this._touchEventSelector)
  793. this._touchEventSelector.call(this._touchEventListener, this, ccui.Widget.TOUCH_CANCELED);
  794. },
  795. longClickEvent: function () {
  796. //TODO it will implement in v3.1
  797. },
  798. /**
  799. * Sets the touch event target/selector of the ccui.Widget
  800. * @param {Function} selector
  801. * @param {Object} target
  802. */
  803. addTouchEventListener: function (selector, target) {
  804. if(target === undefined)
  805. this._touchEventCallback = selector;
  806. else {
  807. this._touchEventSelector = selector;
  808. this._touchEventListener = target;
  809. }
  810. },
  811. /**
  812. * Checks a point if is in widget's space
  813. * @param {cc.Point} pt
  814. * @returns {boolean} true if the point is in widget's space, false otherwise.
  815. */
  816. hitTest: function (pt) {
  817. var bb = cc.rect(0,0, this._contentSize.width, this._contentSize.height);
  818. return cc.rectContainsPoint(bb, this.convertToNodeSpace(pt));
  819. },
  820. /**
  821. * returns whether clipping parent widget contains point.
  822. * @param {cc.Point} pt location point
  823. * @returns {Boolean}
  824. */
  825. isClippingParentContainsPoint: function(pt){
  826. this._affectByClipping = false;
  827. var parent = this.getParent();
  828. var clippingParent = null;
  829. while (parent) {
  830. if (parent instanceof ccui.Layout) {
  831. if (parent.isClippingEnabled()) {
  832. this._affectByClipping = true;
  833. clippingParent = parent;
  834. break;
  835. }
  836. }
  837. parent = parent.getParent();
  838. }
  839. if (!this._affectByClipping)
  840. return true;
  841. if (clippingParent) {
  842. if (clippingParent.hitTest(pt))
  843. return clippingParent.isClippingParentContainsPoint(pt);
  844. return false;
  845. }
  846. return true;
  847. },
  848. /**
  849. * Calls the checkChildInfo of widget's parent, its subclass will override it.
  850. * @param {number} handleState
  851. * @param {ccui.Widget} sender
  852. * @param {cc.Point} touchPoint
  853. */
  854. checkChildInfo: function (handleState, sender, touchPoint) {
  855. var widgetParent = this.getWidgetParent();
  856. if (widgetParent)
  857. widgetParent.checkChildInfo(handleState, sender, touchPoint);
  858. },
  859. /**
  860. * Changes the position (x,y) of the widget .
  861. * The original point (0,0) is at the left-bottom corner of screen.
  862. * @override
  863. * @param {cc.Point|Number} pos
  864. * @param {Number} [posY]
  865. */
  866. setPosition: function (pos, posY) {
  867. if (this._running) {
  868. var widgetParent = this.getWidgetParent();
  869. if (widgetParent) {
  870. var pSize = widgetParent.getContentSize();
  871. if (pSize.width <= 0 || pSize.height <= 0) {
  872. this._positionPercent.x = 0;
  873. this._positionPercent.y = 0;
  874. } else {
  875. if (posY == undefined) {
  876. this._positionPercent.x = pos / pSize.width;
  877. this._positionPercent.y = posY / pSize.height;
  878. } else {
  879. this._positionPercent.x = pos.x / pSize.width;
  880. this._positionPercent.y = pos.y / pSize.height;
  881. }
  882. }
  883. }
  884. }
  885. cc.Node.prototype.setPosition.call(this, pos, posY);
  886. },
  887. setPositionX: function (x) {
  888. if (this._running) {
  889. var widgetParent = this.getWidgetParent();
  890. if (widgetParent) {
  891. var pw = widgetParent.width;
  892. if (pw <= 0)
  893. this._positionPercent.x = 0;
  894. else
  895. this._positionPercent.x = x / pw;
  896. }
  897. }
  898. cc.Node.prototype.setPositionX.call(this, x);
  899. },
  900. setPositionY: function (y) {
  901. if (this._running) {
  902. var widgetParent = this.getWidgetParent();
  903. if (widgetParent) {
  904. var ph = widgetParent.height;
  905. if (ph <= 0)
  906. this._positionPercent.y = 0;
  907. else
  908. this._positionPercent.y = y / ph;
  909. }
  910. }
  911. cc.Node.prototype.setPositionY.call(this, y);
  912. },
  913. /**
  914. * Changes the position (x,y) of the widget
  915. * @param {cc.Point} percent
  916. */
  917. setPositionPercent: function (percent) {
  918. this._positionPercent = percent;
  919. if (this._running) {
  920. var widgetParent = this.getWidgetParent();
  921. if (widgetParent) {
  922. var parentSize = widgetParent.getSize();
  923. this.setPosition(parentSize.width * this._positionPercent.x, parentSize.height * this._positionPercent.y);
  924. }
  925. }
  926. },
  927. _setXPercent: function (percent) {
  928. this._positionPercent.x = percent;
  929. if (this._running) {
  930. var widgetParent = this.getWidgetParent();
  931. if (widgetParent)
  932. this.setPositionX(widgetParent.width * percent);
  933. }
  934. },
  935. _setYPercent: function (percent) {
  936. this._positionPercent.y = percent;
  937. if (this._running) {
  938. var widgetParent = this.getWidgetParent();
  939. if (widgetParent)
  940. this.setPositionY(widgetParent.height * percent);
  941. }
  942. },
  943. /**
  944. * Gets the percent (x,y) of the widget
  945. * @returns {cc.Point} The percent (x,y) of the widget in OpenGL coordinates
  946. */
  947. getPositionPercent: function () {
  948. return cc.p(this._positionPercent);
  949. },
  950. _getXPercent: function () {
  951. return this._positionPercent.x;
  952. },
  953. _getYPercent: function () {
  954. return this._positionPercent.y;
  955. },
  956. /**
  957. * Changes the position type of the widget
  958. * @param {Number} type the position type of widget
  959. */
  960. setPositionType: function (type) {
  961. this._positionType = type;
  962. },
  963. /**
  964. * Gets the position type of the widget
  965. * @returns {Number} the position type of widget
  966. */
  967. getPositionType: function () {
  968. return this._positionType;
  969. },
  970. /**
  971. * Sets whether the widget should be flipped horizontally or not.
  972. * @param {Boolean} flipX true if the widget should be flipped horizontally, false otherwise.
  973. */
  974. setFlippedX: function (flipX) {
  975. this._flippedX = flipX;
  976. this._updateFlippedX();
  977. },
  978. /**
  979. * <p>
  980. * Returns the flag which indicates whether the widget is flipped horizontally or not. <br/>
  981. * It only flips the texture of the widget, and not the texture of the widget's children. <br/>
  982. * Also, flipping the texture doesn't alter the anchorPoint. <br/>
  983. * If you want to flip the anchorPoint too, and/or to flip the children too use: <br/>
  984. * widget.setScaleX(sprite.getScaleX() * -1);
  985. * </p>
  986. * @returns {Boolean} true if the widget is flipped horizontally, false otherwise.
  987. */
  988. isFlippedX: function () {
  989. return this._flippedX;
  990. },
  991. /**
  992. * Sets whether the widget should be flipped vertically or not.
  993. * @param {Boolean} flipY true if the widget should be flipped vertically, false otherwise.
  994. */
  995. setFlippedY: function (flipY) {
  996. this._flippedY = flipY;
  997. this._updateFlippedY();
  998. },
  999. /**
  1000. * <p>
  1001. * Return the flag which indicates whether the widget is flipped vertically or not. <br/>
  1002. * It only flips the texture of the widget, and not the texture of the widget's children. <br/>
  1003. * Also, flipping the texture doesn't alter the anchorPoint. <br/>
  1004. * If you want to flip the anchorPoint too, and/or to flip the children too use: <br/>
  1005. * widget.setScaleY(widget.getScaleY() * -1);
  1006. * </p>
  1007. * @returns {Boolean} true if the widget is flipped vertically, false otherwise.
  1008. */
  1009. isFlippedY: function () {
  1010. return this._flippedY;
  1011. },
  1012. _updateFlippedX: function () {
  1013. },
  1014. _updateFlippedY: function () {
  1015. },
  1016. _adaptRenderers: function(){
  1017. },
  1018. /**
  1019. * Determines if the widget is bright
  1020. * @returns {boolean} true if the widget is bright, false if the widget is dark.
  1021. */
  1022. isBright: function () {
  1023. return this._bright;
  1024. },
  1025. /**
  1026. * Determines if the widget is enabled
  1027. * @returns {boolean}
  1028. */
  1029. isEnabled: function () {
  1030. return this._enabled;
  1031. },
  1032. /**
  1033. * Gets the left boundary position of this widget.
  1034. * @returns {number}
  1035. */
  1036. getLeftBoundary: function () {
  1037. return this.getPositionX() - this._getAnchorX() * this._contentSize.width;
  1038. },
  1039. /**
  1040. * Gets the bottom boundary position of this widget.
  1041. * @returns {number}
  1042. */
  1043. getBottomBoundary: function () {
  1044. return this.getPositionY() - this._getAnchorY() * this._contentSize.height;
  1045. },
  1046. /**
  1047. * Gets the right boundary position of this widget.
  1048. * @returns {number}
  1049. */
  1050. getRightBoundary: function () {
  1051. return this.getLeftBoundary() + this._contentSize.width;
  1052. },
  1053. /**
  1054. * Gets the top boundary position of this widget.
  1055. * @returns {number}
  1056. */
  1057. getTopBoundary: function () {
  1058. return this.getBottomBoundary() + this._contentSize.height;
  1059. },
  1060. /**
  1061. * Gets the position of touch began event.
  1062. * @returns {cc.Point}
  1063. */
  1064. getTouchBeganPosition: function(){
  1065. return cc.p(this._touchBeganPosition);
  1066. },
  1067. /**
  1068. * Gets the position of touch moved event
  1069. * @returns {cc.Point}
  1070. */
  1071. getTouchMovePosition: function(){
  1072. return cc.p(this._touchMovePosition);
  1073. },
  1074. /**
  1075. * Gets the position of touch end event
  1076. * @returns {cc.Point}
  1077. */
  1078. getTouchEndPosition:function(){
  1079. return cc.p(this._touchEndPosition);
  1080. },
  1081. /**
  1082. * get widget type
  1083. * @returns {ccui.Widget.TYPE_WIDGET|ccui.Widget.TYPE_CONTAINER}
  1084. */
  1085. getWidgetType: function () {
  1086. return this._widgetType;
  1087. },
  1088. /**
  1089. * Gets LayoutParameter of widget.
  1090. * @param {ccui.LayoutParameter} parameter
  1091. */
  1092. setLayoutParameter: function (parameter) {
  1093. if(!parameter)
  1094. return;
  1095. this._layoutParameterDictionary[parameter.getLayoutType()] = parameter;
  1096. this._layoutParameterType = parameter.getLayoutType();
  1097. },
  1098. /**
  1099. * Gets layout parameter
  1100. * @param {ccui.LayoutParameter.NONE|ccui.LayoutParameter.LINEAR|ccui.LayoutParameter.RELATIVE} type
  1101. * @returns {ccui.LayoutParameter}
  1102. */
  1103. getLayoutParameter: function (type) {
  1104. type = type || this._layoutParameterType;
  1105. return this._layoutParameterDictionary[type];
  1106. },
  1107. /**
  1108. * Returns the "class name" of widget.
  1109. * @returns {string}
  1110. */
  1111. getDescription: function () {
  1112. return "Widget";
  1113. },
  1114. /**
  1115. * Clones a new widget.
  1116. * @returns {ccui.Widget}
  1117. */
  1118. clone: function () {
  1119. var clonedWidget = this._createCloneInstance();
  1120. clonedWidget._copyProperties(this);
  1121. clonedWidget._copyClonedWidgetChildren(this);
  1122. return clonedWidget;
  1123. },
  1124. _createCloneInstance: function () {
  1125. return ccui.Widget.create();
  1126. },
  1127. _copyClonedWidgetChildren: function (model) {
  1128. var widgetChildren = model.getChildren();
  1129. for (var i = 0; i < widgetChildren.length; i++) {
  1130. var locChild = widgetChildren[i];
  1131. if (locChild instanceof ccui.Widget)
  1132. this.addChild(locChild.clone());
  1133. }
  1134. },
  1135. _copySpecialProperties: function (model) {
  1136. },
  1137. _copyProperties: function (widget) {
  1138. this.setEnabled(widget.isEnabled());
  1139. this.setVisible(widget.isVisible());
  1140. this.setBright(widget.isBright());
  1141. this.setTouchEnabled(widget.isTouchEnabled());
  1142. this.setLocalZOrder(widget.getLocalZOrder());
  1143. this.setTag(widget.getTag());
  1144. this.setName(widget.getName());
  1145. this.setActionTag(widget.getActionTag());
  1146. this._ignoreSize = widget._ignoreSize;
  1147. this.setContentSize(widget._contentSize);
  1148. this._customSize.width = widget._customSize.width;
  1149. this._customSize.height = widget._customSize.height;
  1150. this._copySpecialProperties(widget);
  1151. this._sizeType = widget.getSizeType();
  1152. this._sizePercent.x = widget._sizePercent.x;
  1153. this._sizePercent.y = widget._sizePercent.y;
  1154. this._positionType = widget._positionType;
  1155. this._positionPercent.x = widget._positionPercent.x;
  1156. this._positionPercent.y = widget._positionPercent.y;
  1157. this.setPosition(widget.getPosition());
  1158. this.setAnchorPoint(widget.getAnchorPoint());
  1159. this.setScaleX(widget.getScaleX());
  1160. this.setScaleY(widget.getScaleY());
  1161. this.setRotation(widget.getRotation());
  1162. this.setRotationX(widget.getRotationX());
  1163. this.setRotationY(widget.getRotationY());
  1164. this.setFlippedX(widget.isFlippedX());
  1165. this.setFlippedY(widget.isFlippedY());
  1166. this.setColor(widget.getColor());
  1167. this.setOpacity(widget.getOpacity());
  1168. this._touchEventCallback = widget._touchEventCallback;
  1169. this._touchEventListener = widget._touchEventListener;
  1170. this._touchEventSelector = widget._touchEventSelector;
  1171. this._focused = widget._focused;
  1172. this._focusEnabled = widget._focusEnabled;
  1173. for (var key in widget._layoutParameterDictionary) {
  1174. var parameter = widget._layoutParameterDictionary[key];
  1175. if (parameter)
  1176. this.setLayoutParameter(parameter.clone());
  1177. }
  1178. this._onSizeChanged();
  1179. },
  1180. /*temp action*/
  1181. setActionTag: function (tag) {
  1182. this._actionTag = tag;
  1183. },
  1184. getActionTag: function () {
  1185. return this._actionTag;
  1186. },
  1187. /**
  1188. * Gets the left boundary position of this widget.
  1189. * @deprecated since v3.0, please use getLeftBoundary instead.
  1190. * @returns {number}
  1191. */
  1192. getLeftInParent: function(){
  1193. cc.log("getLeftInParent is deprecated. Please use getLeftBoundary instead.");
  1194. return this.getLeftBoundary();
  1195. },
  1196. /**
  1197. * Gets the bottom boundary position of this widget.
  1198. * @deprecated since v3.0, please use getBottomBoundary instead.
  1199. * @returns {number}
  1200. */
  1201. getBottomInParent: function(){
  1202. cc.log("getBottomInParent is deprecated. Please use getBottomBoundary instead.");
  1203. return this.getBottomBoundary();
  1204. },
  1205. /**
  1206. * Gets the right boundary position of this widget.
  1207. * @deprecated since v3.0, please use getRightBoundary instead.
  1208. * @returns {number}
  1209. */
  1210. getRightInParent: function(){
  1211. cc.log("getRightInParent is deprecated. Please use getRightBoundary instead.");
  1212. return this.getRightBoundary();
  1213. },
  1214. /**
  1215. * Gets the top boundary position of this widget.
  1216. * @deprecated since v3.0, please use getTopBoundary instead.
  1217. * @returns {number}
  1218. */
  1219. getTopInParent: function(){
  1220. cc.log("getTopInParent is deprecated. Please use getTopBoundary instead.");
  1221. return this.getTopBoundary();
  1222. },
  1223. /**
  1224. * Gets the touch end point of widget when widget is selected.
  1225. * @deprecated since v3.0, please use getTouchEndPosition instead.
  1226. * @returns {cc.Point} the touch end point.
  1227. */
  1228. getTouchEndPos: function () {
  1229. cc.log("getTouchEndPos is deprecated. Please use getTouchEndPosition instead.");
  1230. return this.getTouchEndPosition();
  1231. },
  1232. /**
  1233. *Gets the touch move point of widget when widget is selected.
  1234. * @deprecated since v3.0, please use getTouchMovePosition instead.
  1235. * @returns {cc.Point} the touch move point.
  1236. */
  1237. getTouchMovePos: function () {
  1238. cc.log("getTouchMovePos is deprecated. Please use getTouchMovePosition instead.");
  1239. return this.getTouchMovePosition();
  1240. },
  1241. /**
  1242. * Checks a point if in parent's area.
  1243. * @deprecated since v3.0, please use isClippingParentContainsPoint instead.
  1244. * @param {cc.Point} pt
  1245. * @returns {Boolean}
  1246. */
  1247. clippingParentAreaContainPoint: function (pt) {
  1248. cc.log("clippingParentAreaContainPoint is deprecated. Please use isClippingParentContainsPoint instead.");
  1249. this.isClippingParentContainsPoint(pt);
  1250. },
  1251. /**
  1252. * Gets the touch began point of widget when widget is selected.
  1253. * @deprecated since v3.0, please use getTouchBeganPosition instead.
  1254. * @returns {cc.Point} the touch began point.
  1255. */
  1256. getTouchStartPos: function () {
  1257. cc.log("getTouchStartPos is deprecated. Please use getTouchBeganPosition instead.");
  1258. return this.getTouchBeganPosition();
  1259. },
  1260. /**
  1261. * Changes the size that is widget's size
  1262. * @deprecated since v3.0, please use setContentSize instead.
  1263. * @param {cc.Size} size that is widget's size
  1264. */
  1265. setSize: function (size) {
  1266. this.setContentSize(size);
  1267. },
  1268. /**
  1269. * Returns size of widget
  1270. * @deprecated since v3.0, please use getContentSize instead.
  1271. * @returns {cc.Size}
  1272. */
  1273. getSize: function () {
  1274. return this.getContentSize();
  1275. },
  1276. /**
  1277. * Adds a node for widget (this function is deleted in -x)
  1278. * @param {cc.Node} node
  1279. * @param {Number} zOrder
  1280. * @param {Number} tag
  1281. * @deprecated since v3.0, please use addChild instead.
  1282. */
  1283. addNode: function (node, zOrder, tag) {
  1284. if (node instanceof ccui.Widget) {
  1285. cc.log("Please use addChild to add a Widget.");
  1286. return;
  1287. }
  1288. cc.Node.prototype.addChild.call(this, node, zOrder, tag);
  1289. this._nodes.push(node);
  1290. },
  1291. /**
  1292. * Gets node by tag
  1293. * @deprecated since v3.0, please use getChildByTag instead.
  1294. * @param {Number} tag
  1295. * @returns {cc.Node}
  1296. */
  1297. getNodeByTag: function (tag) {
  1298. var _nodes = this._nodes;
  1299. for (var i = 0; i < _nodes.length; i++) {
  1300. var node = _nodes[i];
  1301. if (node && node.getTag() == tag) {
  1302. return node;
  1303. }
  1304. }
  1305. return null;
  1306. },
  1307. /**
  1308. * Returns all children.
  1309. * @deprecated since v3.0, please use getChildren instead.
  1310. * @returns {Array}
  1311. */
  1312. getNodes: function () {
  1313. return this._nodes;
  1314. },
  1315. /**
  1316. * Removes a node from ccui.Widget
  1317. * @deprecated since v3.0, please use removeChild instead.
  1318. * @param {cc.Node} node
  1319. * @param {Boolean} cleanup
  1320. */
  1321. removeNode: function (node, cleanup) {
  1322. cc.Node.prototype.removeChild.call(this, node, cleanup);
  1323. cc.arrayRemoveObject(this._nodes, node);
  1324. },
  1325. /**
  1326. * Removes node by tag
  1327. * @deprecated since v3.0, please use removeChildByTag instead.
  1328. * @param {Number} tag
  1329. * @param {Boolean} [cleanup]
  1330. */
  1331. removeNodeByTag: function (tag, cleanup) {
  1332. var node = this.getNodeByTag(tag);
  1333. if (!node)
  1334. cc.log("cocos2d: removeNodeByTag(tag = %d): child not found!", tag);
  1335. else
  1336. this.removeNode(node);
  1337. },
  1338. /**
  1339. * Removes all node
  1340. * @deprecated since v3.0, please use removeAllChildren instead.
  1341. */
  1342. removeAllNodes: function () {
  1343. for (var i = 0; i < this._nodes.length; i++) {
  1344. var node = this._nodes[i];
  1345. cc.Node.prototype.removeChild.call(this, node);
  1346. }
  1347. this._nodes.length = 0;
  1348. },
  1349. _findLayout: function(){
  1350. var layout = this._parent;
  1351. while(layout){
  1352. if(layout._doLayout){
  1353. layout._doLayoutDirty = true;
  1354. break;
  1355. }else
  1356. layout = layout._parent;
  1357. }
  1358. },
  1359. _updateChildrenDisplayedRGBA: function(){
  1360. this.setColor(this.getColor());
  1361. this.setOpacity(this.getOpacity());
  1362. }
  1363. });
  1364. var _p = ccui.Widget.prototype;
  1365. // Extended properties
  1366. /** @expose */
  1367. _p.xPercent;
  1368. cc.defineGetterSetter(_p, "xPercent", _p._getXPercent, _p._setXPercent);
  1369. /** @expose */
  1370. _p.yPercent;
  1371. cc.defineGetterSetter(_p, "yPercent", _p._getYPercent, _p._setYPercent);
  1372. /** @expose */
  1373. _p.widthPercent;
  1374. cc.defineGetterSetter(_p, "widthPercent", _p._getWidthPercent, _p._setWidthPercent);
  1375. /** @expose */
  1376. _p.heightPercent;
  1377. cc.defineGetterSetter(_p, "heightPercent", _p._getHeightPercent, _p._setHeightPercent);
  1378. /** @expose */
  1379. _p.widgetParent;
  1380. cc.defineGetterSetter(_p, "widgetParent", _p.getWidgetParent);
  1381. /** @expose */
  1382. _p.enabled;
  1383. cc.defineGetterSetter(_p, "enabled", _p.isEnabled, _p.setEnabled);
  1384. /** @expose */
  1385. _p.focused;
  1386. cc.defineGetterSetter(_p, "focused", _p.isFocused, _p.setFocused);
  1387. /** @expose */
  1388. _p.sizeType;
  1389. cc.defineGetterSetter(_p, "sizeType", _p.getSizeType, _p.setSizeType);
  1390. /** @expose */
  1391. _p.widgetType;
  1392. cc.defineGetterSetter(_p, "widgetType", _p.getWidgetType);
  1393. /** @expose */
  1394. _p.touchEnabled;
  1395. cc.defineGetterSetter(_p, "touchEnabled", _p.isTouchEnabled, _p.setTouchEnabled);
  1396. /** @expose */
  1397. _p.updateEnabled;
  1398. cc.defineGetterSetter(_p, "updateEnabled", _p.isUpdateEnabled, _p.setUpdateEnabled);
  1399. /** @expose */
  1400. _p.bright;
  1401. cc.defineGetterSetter(_p, "bright", _p.isBright, _p.setBright);
  1402. /** @expose */
  1403. _p.name;
  1404. cc.defineGetterSetter(_p, "name", _p.getName, _p.setName);
  1405. /** @expose */
  1406. _p.actionTag;
  1407. cc.defineGetterSetter(_p, "actionTag", _p.getActionTag, _p.setActionTag);
  1408. /** @expose */
  1409. _p.opacity;
  1410. cc.defineGetterSetter(_p, "opacity", _p.getOpacity, _p.setOpacity);
  1411. _p = null;
  1412. /**
  1413. * allocates and initializes a UIWidget.
  1414. * @deprecated
  1415. * @return {ccui.Widget}
  1416. * @example
  1417. * // example
  1418. * var uiWidget = ccui.Widget.create();
  1419. */
  1420. ccui.Widget.create = function () {
  1421. return new ccui.Widget();
  1422. };
  1423. ccui.Widget._focusedWidget = null; //both layout & widget will be stored in this variable
  1424. /**
  1425. * Gets the focused widget of current stage.
  1426. * @function
  1427. * @returns {null|ccui.Widget}
  1428. */
  1429. ccui.Widget.getCurrentFocusedWidget = function(){
  1430. return ccui.Widget._focusedWidget;
  1431. };
  1432. // Constants
  1433. //bright style
  1434. /**
  1435. * None bright style of ccui.Widget.
  1436. * @constant
  1437. * @type {number}
  1438. */
  1439. ccui.Widget.BRIGHT_STYLE_NONE = -1;
  1440. /**
  1441. * Normal bright style of ccui.Widget.
  1442. * @constant
  1443. * @type {number}
  1444. */
  1445. ccui.Widget.BRIGHT_STYLE_NORMAL = 0;
  1446. /**
  1447. * Light bright style of ccui.Widget.
  1448. * @constant
  1449. * @type {number}
  1450. */
  1451. ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT = 1;
  1452. //widget type
  1453. /**
  1454. * The type code of Widget for ccui controls.
  1455. * @constant
  1456. * @type {number}
  1457. */
  1458. ccui.Widget.TYPE_WIDGET = 0;
  1459. /**
  1460. * The type code of Container for ccui controls.
  1461. * @constant
  1462. * @type {number}
  1463. */
  1464. ccui.Widget.TYPE_CONTAINER = 1;
  1465. //Focus Direction
  1466. /**
  1467. * The left of Focus direction for ccui.Widget
  1468. * @constant
  1469. * @type {number}
  1470. */
  1471. ccui.Widget.LEFT = 0;
  1472. /**
  1473. * The right of Focus direction for ccui.Widget
  1474. * @constant
  1475. * @type {number}
  1476. */
  1477. ccui.Widget.RIGHT = 1;
  1478. /**
  1479. * The up of Focus direction for ccui.Widget
  1480. * @constant
  1481. * @type {number}
  1482. */
  1483. ccui.Widget.UP = 0;
  1484. /**
  1485. * The down of Focus direction for ccui.Widget
  1486. * @constant
  1487. * @type {number}
  1488. */
  1489. ccui.Widget.DOWN = 1;
  1490. //texture resource type
  1491. /**
  1492. * The image file texture type of ccui.Widget loads.
  1493. * @constant
  1494. * @type {number}
  1495. */
  1496. ccui.Widget.LOCAL_TEXTURE = 0;
  1497. /**
  1498. * The sprite frame texture type of ccui.Widget loads.
  1499. * @constant
  1500. * @type {number}
  1501. */
  1502. ccui.Widget.PLIST_TEXTURE = 1;
  1503. //touch event type
  1504. /**
  1505. * The touch began type of ccui.Widget's touch event
  1506. * @constant
  1507. * @type {number}
  1508. */
  1509. ccui.Widget.TOUCH_BEGAN = 0;
  1510. /**
  1511. * The touch moved type of ccui.Widget's touch event
  1512. * @constant
  1513. * @type {number}
  1514. */
  1515. ccui.Widget.TOUCH_MOVED = 1;
  1516. /**
  1517. * The touch end type of ccui.Widget's touch event
  1518. * @constant
  1519. * @type {number}
  1520. */
  1521. ccui.Widget.TOUCH_ENDED = 2;
  1522. /**
  1523. * The touch canceled type of ccui.Widget's touch event
  1524. * @constant
  1525. * @type {number}
  1526. */
  1527. ccui.Widget.TOUCH_CANCELED = 3;
  1528. //size type
  1529. /**
  1530. * The absolute of ccui.Widget's size type.
  1531. * @constant
  1532. * @type {number}
  1533. */
  1534. ccui.Widget.SIZE_ABSOLUTE = 0;
  1535. /**
  1536. * The percent of ccui.Widget's size type.
  1537. * @constant
  1538. * @type {number}
  1539. */
  1540. ccui.Widget.SIZE_PERCENT = 1;
  1541. //position type
  1542. /**
  1543. * The absolute of ccui.Widget's position type.
  1544. * @constant
  1545. * @type {number}
  1546. */
  1547. ccui.Widget.POSITION_ABSOLUTE = 0;
  1548. /**
  1549. * The percent of ccui.Widget's position type.
  1550. * @constant
  1551. * @type {number}
  1552. */
  1553. ccui.Widget.POSITION_PERCENT = 1;
  1554. /**
  1555. * The widget focus event.
  1556. * @class
  1557. * @extends cc.Event
  1558. */
  1559. cc.EventFocus = cc.Event.extend(/** @lends cc.EventFocus# */{
  1560. _widgetGetFocus: null,
  1561. _widgetLoseFocus: null,
  1562. /**
  1563. * Constructor function.
  1564. * @param {ccui.Widget} widgetLoseFocus
  1565. * @param {ccui.Widget} widgetGetFocus
  1566. */
  1567. ctor: function(widgetLoseFocus, widgetGetFocus){
  1568. this._widgetGetFocus = widgetGetFocus;
  1569. this._widgetLoseFocus = widgetLoseFocus;
  1570. }
  1571. });