UIImageView.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 __UIIMAGEVIEW_H__
  22. #define __UIIMAGEVIEW_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. struct CC_DLL ResourceData;
  31. namespace ui {
  32. class Scale9Sprite;
  33. /**
  34. * @brief A widget to display images.
  35. */
  36. class CC_GUI_DLL ImageView : public Widget , public cocos2d::BlendProtocol
  37. {
  38. DECLARE_CLASS_GUI_INFO
  39. public:
  40. /**
  41. * Default constructor
  42. * @js ctor
  43. * @lua new
  44. */
  45. ImageView();
  46. /**
  47. * Default destructor
  48. * @js NA
  49. * @lua NA
  50. */
  51. virtual ~ImageView();
  52. /**
  53. * Create a empty ImageView.
  54. */
  55. static ImageView* create();
  56. /**
  57. * Create a imageview with a image name.
  58. *
  59. * @param imageFileName file name of texture.
  60. * @param texType @see `Widget::TextureResType`
  61. * @return A ImageView instance.
  62. */
  63. static ImageView* create(const std::string& imageFileName, TextureResType texType = TextureResType::LOCAL);
  64. /**
  65. * Load texture for imageview.
  66. *
  67. * @param fileName file name of texture.
  68. * @param texType @see `Widget::TextureResType`
  69. */
  70. void loadTexture(const std::string& fileName,TextureResType texType = TextureResType::LOCAL);
  71. /**
  72. * Updates the texture rect of the ImageView in points.
  73. * It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size.
  74. */
  75. void setTextureRect(const Rect& rect);
  76. /**
  77. * Enable scale9 renderer.
  78. *
  79. * @param enabled Set to true will use scale9 renderer, false otherwise.
  80. */
  81. void setScale9Enabled(bool enabled);
  82. /**
  83. * Query whether button is using scale9 renderer or not.
  84. *@return whether button use scale9 renderer or not.
  85. */
  86. bool isScale9Enabled()const;
  87. /**
  88. * Sets capInsets for imageview.
  89. * The capInsets affects the ImageView's renderer only if `setScale9Enabled(true)` is called.
  90. *
  91. * @param capInsets capinsets for imageview
  92. */
  93. void setCapInsets(const Rect &capInsets);
  94. /**
  95. * Get ImageView's capInsets size.
  96. * @return Query capInsets size in Rect
  97. * @see `setCapInsets(const Rect&)`
  98. */
  99. const Rect& getCapInsets()const;
  100. /**
  101. * Sets the source blending function.
  102. *
  103. * @param blendFunc A structure with source and destination factor to specify pixel arithmetic. e.g. {GL_ONE, GL_ONE}, {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}.
  104. * @js NA
  105. * @lua NA
  106. */
  107. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  108. /**
  109. * Returns the blending function that is currently being used.
  110. *
  111. * @return A BlendFunc structure with source and destination factor which specified pixel arithmetic.
  112. * @js NA
  113. * @lua NA
  114. */
  115. virtual const BlendFunc &getBlendFunc() const override;
  116. //override methods.
  117. virtual void ignoreContentAdaptWithSize(bool ignore) override;
  118. virtual std::string getDescription() const override;
  119. virtual Size getVirtualRendererSize() const override;
  120. virtual Node* getVirtualRenderer() override;
  121. ResourceData getRenderFile();
  122. virtual void setGLProgram(GLProgram* glProgram) override;
  123. virtual void setGLProgramState(cocos2d::GLProgramState* glProgramState) override;
  124. CC_CONSTRUCTOR_ACCESS:
  125. //initializes state of widget.
  126. virtual bool init() override;
  127. virtual bool init(const std::string& imageFileName, TextureResType texType = TextureResType::LOCAL);
  128. protected:
  129. virtual void initRenderer() override;
  130. virtual void onSizeChanged() override;
  131. virtual void adaptRenderers() override;
  132. void loadTexture(SpriteFrame* spriteframe);
  133. void setupTexture();
  134. void imageTextureScaleChangedWithSize();
  135. virtual Widget* createCloneInstance() override;
  136. virtual void copySpecialProperties(Widget* model) override;
  137. protected:
  138. bool _scale9Enabled;
  139. bool _prevIgnoreSize;
  140. Rect _capInsets;
  141. Scale9Sprite* _imageRenderer;
  142. TextureResType _imageTexType;
  143. Size _imageTextureSize;
  144. bool _imageRendererAdaptDirty;
  145. std::string _textureFile;
  146. };
  147. }
  148. NS_CC_END
  149. // end of ui group
  150. /// @}
  151. #endif /* defined(__CocoGUI__ImageView__) */