UIWidget.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 __UIWIDGET_H__
  22. #define __UIWIDGET_H__
  23. #include "2d/CCProtectedNode.h"
  24. #include "ui/UILayoutParameter.h"
  25. #include "ui/GUIDefine.h"
  26. #include "ui/GUIExport.h"
  27. #include "ui/UIWidget.h"
  28. #include "base/CCMap.h"
  29. /**
  30. * @addtogroup ui
  31. * @{
  32. */
  33. NS_CC_BEGIN
  34. class EventListenerTouchOneByOne;
  35. class Camera;
  36. namespace ui {
  37. class LayoutComponent;
  38. /**
  39. * Touch event type.
  40. *@deprecated use `Widget::TouchEventType` instead
  41. */
  42. typedef enum
  43. {
  44. TOUCH_EVENT_BEGAN,
  45. TOUCH_EVENT_MOVED,
  46. TOUCH_EVENT_ENDED,
  47. TOUCH_EVENT_CANCELED
  48. }TouchEventType;
  49. /**
  50. * Touch event callback.
  51. *@deprecated use `Widget::ccWidgetTouchCallback` instead
  52. */
  53. typedef void (Ref::*SEL_TouchEvent)(Ref*,TouchEventType);
  54. #define toucheventselector(_SELECTOR) (SEL_TouchEvent)(&_SELECTOR)
  55. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  56. #ifdef ABSOLUTE
  57. #undef ABSOLUTE
  58. #endif
  59. #endif
  60. /**
  61. *@brief Base class for all ui widgets.
  62. * This class inherent from `ProtectedNode` and `LayoutParameterProtocol`.
  63. * If you want to implements your own ui widget, you should subclass it.
  64. */
  65. class CC_GUI_DLL Widget : public ProtectedNode, public LayoutParameterProtocol
  66. {
  67. public:
  68. /**
  69. * Widget focus direction.
  70. */
  71. enum class FocusDirection
  72. {
  73. LEFT,
  74. RIGHT,
  75. UP,
  76. DOWN
  77. };
  78. /**
  79. * Widget position type for layout.
  80. */
  81. enum class PositionType
  82. {
  83. ABSOLUTE,
  84. PERCENT
  85. };
  86. /**
  87. * Widget size type for layout.
  88. */
  89. enum class SizeType
  90. {
  91. ABSOLUTE,
  92. PERCENT
  93. };
  94. /**
  95. * Touch event type.
  96. */
  97. enum class TouchEventType
  98. {
  99. BEGAN,
  100. MOVED,
  101. ENDED,
  102. CANCELED
  103. };
  104. /**
  105. * Texture resource type.
  106. * - LOCAL: It means the texture is loaded from image.
  107. * - PLIST: It means the texture is loaded from texture atlas.
  108. */
  109. enum class TextureResType
  110. {
  111. LOCAL = 0,
  112. PLIST = 1
  113. };
  114. /**
  115. * Widget bright style.
  116. */
  117. enum class BrightStyle
  118. {
  119. NONE = -1,
  120. NORMAL,
  121. HIGHLIGHT
  122. };
  123. /**
  124. * Widget touch event callback.
  125. */
  126. typedef std::function<void(Ref*,Widget::TouchEventType,Touch*)> ccWidgetTouchCallback;
  127. /**
  128. * Widget click event callback.
  129. */
  130. typedef std::function<void(Ref*)> ccWidgetClickCallback;
  131. /**
  132. * Widget custom event callback.
  133. * It is mainly used together with Cocos Studio.
  134. */
  135. typedef std::function<void(Ref*, int)> ccWidgetEventCallback;
  136. /**
  137. * Default constructor
  138. * @js ctor
  139. * @lua new
  140. */
  141. Widget(void);
  142. /**
  143. * Default destructor
  144. * @js NA
  145. * @lua NA
  146. */
  147. virtual ~Widget();
  148. /**
  149. * Create and return a empty Widget instance pointer.
  150. */
  151. static Widget* create();
  152. /**
  153. * Sets whether the widget is enabled
  154. *
  155. * true if the widget is enabled, widget may be touched , false if the widget is disabled, widget cannot be touched.
  156. *
  157. * Note: If you want to change the widget's appearance to disabled state, you should also call `setBright(false)`.
  158. *
  159. * The default value is true, a widget is default to enable touch.
  160. *
  161. * @param enabled Set to true to enable touch, false otherwise.
  162. */
  163. virtual void setEnabled(bool enabled);
  164. /**
  165. * Determines if the widget is enabled or not.
  166. *
  167. * @return true if the widget is enabled, false if the widget is disabled.
  168. */
  169. bool isEnabled() const;
  170. /**
  171. * Sets whether the widget is bright
  172. *
  173. * The default value is true, a widget is default to bright
  174. *
  175. * @param bright true if the widget is bright, false if the widget is dark.
  176. */
  177. void setBright(bool bright);
  178. /**
  179. * Determines if the widget is bright
  180. *
  181. * @return true if the widget is bright, false if the widget is dark.
  182. */
  183. bool isBright() const;
  184. /**
  185. * Sets whether the widget is touch enabled.
  186. *
  187. * The default value is false, a widget is default to touch disabled.
  188. *
  189. * @param enabled True if the widget is touch enabled, false if the widget is touch disabled.
  190. */
  191. virtual void setTouchEnabled(bool enabled);
  192. /**
  193. * To set the bright style of widget.
  194. *
  195. * @see BrightStyle
  196. *
  197. * @param style BrightStyle::NORMAL means the widget is in normal state, BrightStyle::HIGHLIGHT means the widget is in highlight state.
  198. */
  199. void setBrightStyle(BrightStyle style);
  200. /**
  201. * Determines if the widget is touch enabled
  202. *
  203. * @return true if the widget is touch enabled, false if the widget is touch disabled.
  204. */
  205. bool isTouchEnabled() const;
  206. /**
  207. * Determines if the widget is highlighted
  208. *
  209. * @return true if the widget is highlighted, false if the widget is not highlighted.
  210. */
  211. bool isHighlighted() const;
  212. /**
  213. * Sets whether the widget is highlighted
  214. *
  215. * The default value is false, a widget is default to not highlighted
  216. *
  217. * @param highlight true if the widget is highlighted, false if the widget is not highlighted.
  218. */
  219. void setHighlighted(bool highlight);
  220. /**
  221. * Gets the left boundary position of this widget in parent's coordination system.
  222. * @deprecated use `getLeftBoundary` instead.
  223. * @return The left boundary position of this widget.
  224. */
  225. CC_DEPRECATED_ATTRIBUTE float getLeftInParent(){return this->getLeftBoundary();}
  226. /**
  227. * Gets the left boundary position of this widget in parent's coordination system.
  228. * @return The left boundary position of this widget.
  229. */
  230. float getLeftBoundary() const;
  231. /**
  232. * Gets the bottom boundary position of this widget in parent's coordination system.
  233. * @deprecated use `getBottomBoundary` instead.
  234. * @return The bottom boundary position of this widget.
  235. */
  236. CC_DEPRECATED_ATTRIBUTE float getBottomInParent(){return this->getBottomBoundary();}
  237. /**
  238. * Gets the bottom boundary position of this widget in parent's coordination system.
  239. * @return The bottom boundary position of this widget.
  240. */
  241. float getBottomBoundary() const;
  242. /**
  243. * Gets the right boundary position of this widget in parent's coordination system.
  244. * @deprecated use `getRightBoundary` instead.
  245. * @return The right boundary position of this widget.
  246. */
  247. CC_DEPRECATED_ATTRIBUTE float getRightInParent(){return this->getRightBoundary();}
  248. /**
  249. * Gets the right boundary position of this widget in parent's coordination system.
  250. * @return The right boundary position of this widget.
  251. */
  252. float getRightBoundary() const;
  253. /**
  254. * Gets the top boundary position of this widget in parent's coordination system.
  255. * @deprecated use `getTopBoundary` instead.
  256. * @return The top boundary position of this widget.
  257. */
  258. CC_DEPRECATED_ATTRIBUTE float getTopInParent(){return this->getTopBoundary();}
  259. /**
  260. * Gets the top boundary position of this widget in parent's coordination system.
  261. * @return The top boundary position of this widget.
  262. */
  263. float getTopBoundary() const;
  264. /**
  265. * @js NA
  266. */
  267. virtual void visit(cocos2d::Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  268. /**
  269. * Sets the touch event target/selector to the widget
  270. */
  271. CC_DEPRECATED_ATTRIBUTE void addTouchEventListener(Ref* target,SEL_TouchEvent selector);
  272. /**
  273. * Set a callback to touch vent listener.
  274. *@param callback The callback in `ccWidgetEventCallback.`
  275. */
  276. void addTouchEventListener(const ccWidgetTouchCallback& callback);
  277. /**
  278. * Set a click event handler to the widget.
  279. * @param callback The callback in `ccWidgetClickCallback`.
  280. */
  281. void addClickEventListener(const ccWidgetClickCallback& callback);
  282. /**
  283. * Set a event handler to the widget in order to use cocostudio editor and framework
  284. * @param callback The callback in `ccWidgetEventCallback`.
  285. * @lua NA
  286. */
  287. virtual void addCCSEventListener(const ccWidgetEventCallback& callback);
  288. /**/
  289. /**
  290. * Changes the position (x,y) of the widget in OpenGL coordinates
  291. *
  292. * Usually we use p(x,y) to compose a Vec2 object.
  293. * The original point (0,0) is at the left-bottom corner of screen.
  294. *
  295. * @param pos The position (x,y) of the widget in OpenGL coordinates
  296. */
  297. virtual void setPosition(const Vec2 &pos) override;
  298. /**
  299. * Set the percent(x,y) of the widget in OpenGL coordinates
  300. *
  301. * @param percent The percent (x,y) of the widget in OpenGL coordinates
  302. */
  303. void setPositionPercent(const Vec2 &percent);
  304. /**
  305. * Gets the percent (x,y) of the widget in OpenGL coordinates
  306. *
  307. * @see setPosition(const Vec2&)
  308. *
  309. * @return The percent (x,y) of the widget in OpenGL coordinates
  310. */
  311. const Vec2& getPositionPercent();
  312. /**
  313. * Changes the position type of the widget
  314. *
  315. * @see `PositionType`
  316. *
  317. * @param type the position type of widget
  318. */
  319. void setPositionType(PositionType type);
  320. /**
  321. * Gets the position type of the widget
  322. *
  323. * @see `PositionType`
  324. *
  325. * @return type the position type of widget
  326. */
  327. PositionType getPositionType() const;
  328. /**
  329. * Sets whether the widget should be flipped horizontally or not.
  330. *
  331. * @param flippedX true if the widget should be flipped horizontally, false otherwise.
  332. */
  333. virtual void setFlippedX(bool flippedX);
  334. /**
  335. * Returns the flag which indicates whether the widget is flipped horizontally or not.
  336. *
  337. * It not only flips the texture of the widget, but also the texture of the widget's children.
  338. * Also, flipping relies on widget's anchor point.
  339. * Internally, it just use setScaleX(-1) to flip the widget.
  340. *
  341. * @return true if the widget is flipped horizontally, false otherwise.
  342. */
  343. virtual bool isFlippedX()const{return _flippedX;};
  344. /**
  345. * Sets whether the widget should be flipped vertically or not.
  346. *
  347. * @param flippedY true if the widget should be flipped vertically, false otherwise.
  348. */
  349. virtual void setFlippedY(bool flippedY);
  350. /**
  351. * Return the flag which indicates whether the widget is flipped vertically or not.
  352. *
  353. * It not only flips the texture of the widget, but also the texture of the widget's children.
  354. * Also, flipping relies on widget's anchor point.
  355. * Internally, it just use setScaleY(-1) to flip the widget.
  356. *
  357. * @return true if the widget is flipped vertically, false otherwise.
  358. */
  359. virtual bool isFlippedY()const{return _flippedY;};
  360. /** @deprecated Use isFlippedX() instead */
  361. CC_DEPRECATED_ATTRIBUTE bool isFlipX() { return isFlippedX(); };
  362. /** @deprecated Use setFlippedX() instead */
  363. CC_DEPRECATED_ATTRIBUTE void setFlipX(bool flipX) { setFlippedX(flipX); };
  364. /** @deprecated Use isFlippedY() instead */
  365. CC_DEPRECATED_ATTRIBUTE bool isFlipY() { return isFlippedY(); };
  366. /** @deprecated Use setFlippedY() instead */
  367. CC_DEPRECATED_ATTRIBUTE void setFlipY(bool flipY) { setFlippedY(flipY); };
  368. //override the setScale function of Node
  369. virtual void setScaleX(float scaleX) override;
  370. virtual void setScaleY(float scaleY) override;
  371. virtual void setScale(float scale) override;
  372. virtual void setScale(float scalex, float scaley) override;
  373. using Node::setScaleZ;
  374. virtual float getScaleX() const override;
  375. virtual float getScaleY() const override;
  376. virtual float getScale() const override;
  377. using Node::getScaleZ;
  378. /**
  379. * Checks a point if in parent's area.
  380. *
  381. * @param pt A point in `Vec2`.
  382. * @deprecated use `isClippingParentContainsPoint` instead.
  383. * @return true if the point is in parent's area, false otherwise.
  384. */
  385. CC_DEPRECATED_ATTRIBUTE bool clippingParentAreaContainPoint(const Vec2 &pt){return this->isClippingParentContainsPoint(pt);}
  386. /**
  387. * Checks a point if in parent's area.
  388. *
  389. * @param pt A point in `Vec2`.
  390. * @return true if the point is in parent's area, false otherwise.
  391. */
  392. bool isClippingParentContainsPoint(const Vec2& pt);
  393. /**
  394. * Gets the touch began point of widget when widget is selected.
  395. * @deprecated use `getTouchBeganPosition` instead.
  396. * @return the touch began point.
  397. */
  398. CC_DEPRECATED_ATTRIBUTE const Vec2& getTouchStartPos()const{return this->getTouchBeganPosition();}
  399. /**
  400. * Gets the touch began point of widget when widget is selected.
  401. * @return the touch began point.
  402. */
  403. const Vec2& getTouchBeganPosition()const;
  404. /*
  405. * Gets the touch move point of widget when widget is selected.
  406. * @deprecated use `getTouchMovePosition` instead.
  407. * @return the touch move point.
  408. */
  409. CC_DEPRECATED_ATTRIBUTE const Vec2& getTouchMovePos()const{ return this->getTouchMovePosition();}
  410. /*
  411. * Gets the touch move point of widget when widget is selected.
  412. * @return the touch move point.
  413. */
  414. const Vec2& getTouchMovePosition()const;
  415. /*
  416. * Gets the touch end point of widget when widget is selected.
  417. * @deprecated use `getTouchEndPosition` instead.
  418. * @return the touch end point.
  419. */
  420. CC_DEPRECATED_ATTRIBUTE const Vec2& getTouchEndPos()const{return this->getTouchEndPosition();}
  421. /*
  422. * Gets the touch end point of widget when widget is selected.
  423. * @return the touch end point.
  424. */
  425. const Vec2& getTouchEndPosition()const;
  426. /**
  427. * Changes the size that is widget's size
  428. * @deprecated use `setContentSize` instead.
  429. * @param size that is widget's size
  430. */
  431. CC_DEPRECATED_ATTRIBUTE virtual void setSize(const Size &size);
  432. /**
  433. * Changes the size that is widget's size
  434. * @param contentSize A content size in `Size`.
  435. */
  436. virtual void setContentSize(const Size& contentSize) override;
  437. /**
  438. * Changes the percent that is widget's percent size
  439. *
  440. * @param percent that is widget's percent size
  441. */
  442. virtual void setSizePercent(const Vec2 &percent);
  443. /**
  444. * Changes the size type of widget.
  445. *
  446. * @see `SizeType`
  447. *
  448. * @param type that is widget's size type
  449. */
  450. void setSizeType(SizeType type);
  451. /**
  452. * Gets the size type of widget.
  453. *
  454. * @see `SizeType`
  455. */
  456. SizeType getSizeType() const;
  457. /**
  458. * Get the size of widget
  459. *
  460. * @return Widget content size.
  461. */
  462. CC_DEPRECATED_ATTRIBUTE const Size& getSize() const;
  463. /**
  464. * Get the user defined widget size.
  465. *@return User defined size.
  466. */
  467. const Size& getCustomSize() const;
  468. /**
  469. * Get the content size of widget.
  470. * @warning This API exists mainly for keeping back compatibility.
  471. * @return
  472. */
  473. virtual const Size& getLayoutSize() {return _contentSize;};
  474. /**
  475. * Get size percent of widget.
  476. *
  477. * @return Percent size.
  478. */
  479. const Vec2& getSizePercent();
  480. /**
  481. * Checks a point is in widget's content space.
  482. * This function is used for determining touch area of widget.
  483. *
  484. * @param pt The point in `Vec2`.
  485. * @param camera The camera look at widget, used to convert GL screen point to near/far plane.
  486. * @param p Point to a Vec3 for store the intersect point, if don't need them set to nullptr.
  487. * @return true if the point is in widget's content space, false otherwise.
  488. */
  489. virtual bool hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const;
  490. /**
  491. * A callback which will be called when touch began event is issued.
  492. *@param touch The touch info.
  493. *@param unusedEvent The touch event info.
  494. *@return True if user want to handle touches, false otherwise.
  495. */
  496. virtual bool onTouchBegan(Touch *touch, Event *unusedEvent);
  497. /**
  498. * A callback which will be called when touch moved event is issued.
  499. *@param touch The touch info.
  500. *@param unusedEvent The touch event info.
  501. */
  502. virtual void onTouchMoved(Touch *touch, Event *unusedEvent);
  503. /**
  504. * A callback which will be called when touch ended event is issued.
  505. *@param touch The touch info.
  506. *@param unusedEvent The touch event info.
  507. */
  508. virtual void onTouchEnded(Touch *touch, Event *unusedEvent);
  509. /**
  510. * A callback which will be called when touch cancelled event is issued.
  511. *@param touch The touch info.
  512. *@param unusedEvent The touch event info.
  513. */
  514. virtual void onTouchCancelled(Touch *touch, Event *unusedEvent);
  515. /**
  516. * Sets a LayoutParameter to widget.
  517. *
  518. * @see LayoutParameter
  519. * @param parameter LayoutParameter pointer
  520. */
  521. void setLayoutParameter(LayoutParameter* parameter);
  522. /**
  523. * Gets LayoutParameter of widget.
  524. *
  525. * @see LayoutParameter
  526. * @return LayoutParameter
  527. */
  528. LayoutParameter* getLayoutParameter()const override;
  529. /**
  530. * Gets LayoutParameter of widget.
  531. *
  532. * @see LayoutParameter
  533. * @deprecated use `getLayoutParameter()` instead.
  534. * @param type Relative or Linear
  535. * @return LayoutParameter
  536. */
  537. CC_DEPRECATED_ATTRIBUTE LayoutParameter* getLayoutParameter(LayoutParameter::Type type);
  538. /**
  539. * Toggle whether ignore user defined content size for widget.
  540. * Set true will ignore user defined content size which means
  541. * the widget size is always equal to the return value of `getVirtualRendererSize`.
  542. *
  543. * @param ignore set member variable _ignoreSize to ignore
  544. */
  545. virtual void ignoreContentAdaptWithSize(bool ignore);
  546. /**
  547. * Query whether the widget ignores user defined content size or not
  548. *
  549. * @return True means ignore user defined content size, false otherwise.
  550. */
  551. bool isIgnoreContentAdaptWithSize() const;
  552. /**
  553. * Gets position of widget in world space.
  554. *
  555. * @return Position of widget in world space.
  556. */
  557. Vec2 getWorldPosition()const;
  558. /**
  559. * Gets the inner Renderer node of widget.
  560. *
  561. * For example, a button's Virtual Renderer is it's texture renderer.
  562. *
  563. * @return Node pointer.
  564. */
  565. virtual Node* getVirtualRenderer();
  566. /**
  567. * Get the virtual renderer's size
  568. *@return Widget virtual renderer size.
  569. */
  570. virtual Size getVirtualRendererSize() const;
  571. /**
  572. * Returns the string representation of widget class name
  573. * @return get the class description.
  574. */
  575. virtual std::string getDescription() const override;
  576. /**
  577. * Create a new widget copy of the original one.
  578. * @return A cloned widget copy of original.
  579. */
  580. Widget* clone();
  581. /**
  582. * @lua NA
  583. */
  584. virtual void onEnter() override;
  585. /**
  586. * @lua NA
  587. */
  588. virtual void onExit() override;
  589. /**
  590. * Update all children's contents size and position recursively.
  591. * @see `updateSizeAndPosition(const Size&)`
  592. */
  593. void updateSizeAndPosition();
  594. /**
  595. * Update all children's contents size and position recursively.
  596. */
  597. void updateSizeAndPosition(const Size& parentSize);
  598. /**
  599. * Set the tag of action.
  600. *@param tag A integer tag value.
  601. */
  602. void setActionTag(int tag);
  603. /**
  604. * Get the action tag.
  605. *@return Action tag.
  606. */
  607. int getActionTag()const;
  608. /**
  609. * @brief Allow widget touch events to propagate to its parents. Set false will disable propagation
  610. * @param isPropagate True to allow propagation, false otherwise.
  611. * @since v3.3
  612. */
  613. void setPropagateTouchEvents(bool isPropagate);
  614. /**
  615. * Return whether the widget is propagate touch events to its parents or not
  616. * @return whether touch event propagation is allowed or not.
  617. * @since v3.3
  618. */
  619. bool isPropagateTouchEvents()const;
  620. /**
  621. * Toggle widget swallow touch option.
  622. * @brief Specify widget to swallow touches or not
  623. * @param swallow True to swallow touch, false otherwise.
  624. * @since v3.3
  625. */
  626. void setSwallowTouches(bool swallow);
  627. /**
  628. * Return whether the widget is swallowing touch or not
  629. * @return Whether touch is swallowed.
  630. * @since v3.3
  631. */
  632. bool isSwallowTouches()const;
  633. /**
  634. * Query whether widget is focused or not.
  635. *@return whether the widget is focused or not
  636. */
  637. bool isFocused()const;
  638. /**
  639. * Toggle widget focus status.
  640. *@param focus pass true to let the widget get focus or pass false to let the widget lose focus
  641. */
  642. void setFocused(bool focus);
  643. /**
  644. * Query widget's focus enable state.
  645. *@return true represent the widget could accept focus, false represent the widget couldn't accept focus
  646. */
  647. bool isFocusEnabled()const;
  648. /**
  649. * Allow widget to accept focus.
  650. *@param enable pass true/false to enable/disable the focus ability of a widget
  651. */
  652. void setFocusEnabled(bool enable);
  653. /**
  654. * When a widget is in a layout, you could call this method to get the next focused widget within a specified direction.
  655. * If the widget is not in a layout, it will return itself
  656. *@param direction the direction to look for the next focused widget in a layout
  657. *@param current the current focused widget
  658. *@return the next focused widget in a layout
  659. */
  660. virtual Widget* findNextFocusedWidget(FocusDirection direction, Widget* current);
  661. /**
  662. * when a widget calls this method, it will get focus immediately.
  663. */
  664. void requestFocus();
  665. /**
  666. * Return a current focused widget in your UI scene.
  667. * No matter what widget object you call this method on , it will return you the exact one focused widget.
  668. * @param isWidget if your set isWidget to true, it will return the _realFocusedWidget which is always a widget
  669. * otherwise, it will return a widget or a layout.
  670. * @deprecated use `getCurrentFocusedWidget` instead.
  671. */
  672. CC_DEPRECATED_ATTRIBUTE Widget* getCurrentFocusedWidget(bool isWidget);
  673. /**
  674. * Return a current focused widget in your UI scene.
  675. * No matter what widget object you call this method on , it will return you the exact one focused widget.
  676. */
  677. static Widget* getCurrentFocusedWidget();
  678. /*
  679. * Call this method with parameter true to enable the Android Dpad focus navigation feature
  680. *@param enable set true to enable dpad focus navigation, otherwise disenable dpad focus navigation
  681. */
  682. static void enableDpadNavigation(bool enable);
  683. /**
  684. * When a widget lose/get focus, this method will be called. Be Caution when you provide your own version,
  685. * you must call widget->setFocused(true/false) to change the focus state of the current focused widget;
  686. */
  687. std::function<void(Widget*,Widget*)> onFocusChanged;
  688. /**
  689. * use this function to manually specify the next focused widget regards to each direction
  690. */
  691. std::function<Widget*(FocusDirection)> onNextFocusedWidget;
  692. /**
  693. *Toggle use unify size.
  694. *@param enable True to use unify size, false otherwise.
  695. */
  696. void setUnifySizeEnabled(bool enable);
  697. /**
  698. * Query whether unify size enable state.
  699. *@return true represent the widget use Unify Size, false represent the widget couldn't use Unify Size
  700. */
  701. bool isUnifySizeEnabled()const;
  702. /**
  703. * Set callback name.
  704. *@param callbackName A string representation of callback name.
  705. */
  706. void setCallbackName(const std::string& callbackName) { _callbackName = callbackName; }
  707. /**
  708. * Query callback name.
  709. *@return The callback name.
  710. */
  711. const std::string& getCallbackName() const{ return _callbackName; }
  712. /**
  713. * Set callback type.
  714. * @param callbackType A string representation of callback type.
  715. */
  716. void setCallbackType(const std::string& callbackType) { _callbackType = callbackType; }
  717. /**
  718. * Query callback type.
  719. *@return Callback type string.
  720. */
  721. const std::string& getCallbackType() const{ return _callbackType; }
  722. /**
  723. * Toggle layout component enable.
  724. *@param enable Layout Component of a widget
  725. */
  726. void setLayoutComponentEnabled(bool enable);
  727. /**
  728. * Query whether layout component is enabled or not.
  729. *@return true represent the widget use Layout Component, false represent the widget couldn't use Layout Component.
  730. */
  731. bool isLayoutComponentEnabled()const;
  732. CC_CONSTRUCTOR_ACCESS:
  733. //initializes state of widget.
  734. virtual bool init() override;
  735. /*
  736. * @brief Sends the touch event to widget's parent, if a widget wants to handle touch event under another widget,
  737. * it must override this function.
  738. * @param event the touch event type, it could be BEGAN/MOVED/CANCELED/ENDED
  739. * @param parent
  740. * @param point
  741. */
  742. virtual void interceptTouchEvent(TouchEventType event, Widget* sender, Touch *touch);
  743. /**
  744. *@brief Propagate touch events to its parents
  745. */
  746. void propagateTouchEvent(TouchEventType event, Widget* sender, Touch *touch);
  747. friend class PageView;
  748. /**
  749. * This method is called when a focus change event happens
  750. *@param widgetLostFocus The widget which lose its focus
  751. *@param widgetGetFocus The widget which get its focus
  752. */
  753. void onFocusChange(Widget* widgetLostFocus, Widget* widgetGetFocus);
  754. /**
  755. * Dispatch a EventFocus through a EventDispatcher
  756. *@param widgetLoseFocus The widget which lose its focus
  757. *@param widgetGetFocus he widget which get its focus
  758. */
  759. void dispatchFocusEvent(Widget* widgetLoseFocus, Widget* widgetGetFocus);
  760. protected:
  761. /**
  762. * Get a normal state GLProgramState
  763. *@since v3.4
  764. */
  765. GLProgramState* getNormalGLProgramState(Texture2D* texture)const;
  766. /**
  767. * Get a disabled state GLProgramState
  768. *@since v3.4
  769. */
  770. GLProgramState* getGrayGLProgramState(Texture2D* texture)const;
  771. //call back function called when size changed.
  772. virtual void onSizeChanged();
  773. //initializes renderer of widget.
  774. virtual void initRenderer();
  775. //call back function called widget's state changed to normal.
  776. virtual void onPressStateChangedToNormal();
  777. //call back function called widget's state changed to selected.
  778. virtual void onPressStateChangedToPressed();
  779. //call back function called widget's state changed to dark.
  780. virtual void onPressStateChangedToDisabled();
  781. void pushDownEvent(Touch* touch = nullptr);
  782. void moveEvent(Touch* touch = nullptr);
  783. virtual void releaseUpEvent(Touch* touch = nullptr);
  784. virtual void cancelUpEvent(Touch* touch = nullptr);
  785. virtual void adaptRenderers(){};
  786. void updateChildrenDisplayedRGBA();
  787. void copyProperties(Widget* model);
  788. virtual Widget* createCloneInstance();
  789. virtual void copySpecialProperties(Widget* model);
  790. virtual void copyClonedWidgetChildren(Widget* model);
  791. Widget* getWidgetParent();
  792. void updateContentSizeWithTextureSize(const Size& size);
  793. bool isAncestorsEnabled();
  794. Widget* getAncestorWidget(Node* node);
  795. bool isAncestorsVisible(Node* node);
  796. /** @deprecated Use getAncestorWidget instead. */
  797. CC_DEPRECATED_ATTRIBUTE Widget* getAncensterWidget(Node* node);
  798. void cleanupWidget();
  799. LayoutComponent* getOrCreateLayoutComponent();
  800. protected:
  801. bool _usingLayoutComponent;
  802. bool _unifySize;
  803. bool _enabled;
  804. bool _bright;
  805. bool _touchEnabled;
  806. bool _highlight;
  807. bool _affectByClipping;
  808. bool _ignoreSize;
  809. bool _propagateTouchEvents;
  810. BrightStyle _brightStyle;
  811. SizeType _sizeType;
  812. PositionType _positionType;
  813. //used for search widget by action tag in UIHelper class
  814. int _actionTag;
  815. Size _customSize;
  816. Vec2 _sizePercent;
  817. Vec2 _positionPercent;
  818. bool _hitted;
  819. // weak reference of the camera which made the widget passed the hit test when response touch begin event
  820. // it's useful in the next touch move/end events
  821. const Camera *_hittedByCamera;
  822. EventListenerTouchOneByOne* _touchListener;
  823. Vec2 _touchBeganPosition;
  824. Vec2 _touchMovePosition;
  825. Vec2 _touchEndPosition;
  826. bool _flippedX;
  827. bool _flippedY;
  828. //use map to enable switch back and forth for user layout parameters
  829. Map<int,LayoutParameter*> _layoutParameterDictionary;
  830. LayoutParameter::Type _layoutParameterType;
  831. bool _focused;
  832. bool _focusEnabled;
  833. /**
  834. * store the only one focused widget
  835. */
  836. static Widget *_focusedWidget; //both layout & widget will be stored in this variable
  837. Ref* _touchEventListener;
  838. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  839. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  840. #elif _MSC_VER >= 1400 //vs 2005 or higher
  841. #pragma warning (push)
  842. #pragma warning (disable: 4996)
  843. #endif
  844. SEL_TouchEvent _touchEventSelector;
  845. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  846. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  847. #elif _MSC_VER >= 1400 //vs 2005 or higher
  848. #pragma warning (pop)
  849. #endif
  850. ccWidgetTouchCallback _touchEventCallback;
  851. ccWidgetClickCallback _clickEventListener;
  852. ccWidgetEventCallback _ccEventCallback;
  853. std::string _callbackType;
  854. std::string _callbackName;
  855. private:
  856. class FocusNavigationController;
  857. static FocusNavigationController* _focusNavigationController;
  858. };
  859. }
  860. NS_CC_END
  861. // end of ui group
  862. /// @}
  863. #endif /* defined(__Widget__) */