axmol/core/ui/UIImageView.h

183 lines
5.3 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __UIIMAGEVIEW_H__
#define __UIIMAGEVIEW_H__
#include "ui/UIWidget.h"
#include "ui/GUIExport.h"
/**
* @addtogroup ui
* @{
*/
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
struct AX_DLL ResourceData;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace ui
{
class Scale9Sprite;
2019-11-23 20:27:39 +08:00
/**
* @brief A widget to display images.
*/
2022-08-08 18:02:17 +08:00
class AX_GUI_DLL ImageView : public Widget, public ax::BlendProtocol
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
DECLARE_CLASS_GUI_INFO
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
/**
* Default constructor
* @js ctor
* @lua new
*/
ImageView();
/**
* Default destructor
* @js NA
* @lua NA
*/
virtual ~ImageView();
/**
* Create a empty ImageView.
*/
static ImageView* create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Create a imageview with a image name.
*
* @param imageFileName file name of texture.
* @param texType @see `Widget::TextureResType`
* @return A ImageView instance.
*/
static ImageView* create(std::string_view imageFileName, TextureResType texType = TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
/**
* Load texture for imageview.
*
* @param fileName file name of texture.
* @param texType @see `Widget::TextureResType`
*/
void loadTexture(std::string_view fileName, TextureResType texType = TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
/**
* Updates the texture rect of the ImageView in points.
* It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size.
*/
void setTextureRect(const Rect& rect);
/**
* Enable scale9 renderer.
*
* @param enabled Set to true will use scale9 renderer, false otherwise.
*/
void setScale9Enabled(bool enabled);
/**
* Query whether button is using scale9 renderer or not.
*@return whether button use scale9 renderer or not.
*/
2021-12-25 10:04:45 +08:00
bool isScale9Enabled() const;
2019-11-23 20:27:39 +08:00
/**
* Sets capInsets for imageview.
* The capInsets affects the ImageView's renderer only if `setScale9Enabled(true)` is called.
*
* @param capInsets capinsets for imageview
*/
2021-12-25 10:04:45 +08:00
void setCapInsets(const Rect& capInsets);
2019-11-23 20:27:39 +08:00
/**
* Get ImageView's capInsets size.
* @return Query capInsets size in Rect
* @see `setCapInsets(const Rect&)`
*/
2021-12-25 10:04:45 +08:00
const Rect& getCapInsets() const;
2019-11-23 20:27:39 +08:00
/**
* Sets the source blending function.
*
2021-12-25 10:04:45 +08:00
* @param blendFunc A structure with source and destination factor to specify pixel arithmetic. e.g.
* {BlendFactor::ONE, BlendFactor::ONE}, {BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA}.
2019-11-23 20:27:39 +08:00
* @js NA
* @lua NA
*/
2021-12-25 10:04:45 +08:00
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
2019-11-23 20:27:39 +08:00
/**
* Returns the blending function that is currently being used.
*
* @return A BlendFunc structure with source and destination factor which specified pixel arithmetic.
* @js NA
* @lua NA
*/
2021-12-25 10:04:45 +08:00
virtual const BlendFunc& getBlendFunc() const override;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// override methods.
2019-11-23 20:27:39 +08:00
virtual void ignoreContentAdaptWithSize(bool ignore) override;
virtual std::string getDescription() const override;
2021-10-23 23:27:14 +08:00
virtual Vec2 getVirtualRendererSize() const override;
2019-11-23 20:27:39 +08:00
virtual Node* getVirtualRenderer() override;
ResourceData getRenderFile();
// initializes state of widget.
virtual bool init() override;
virtual bool init(std::string_view imageFileName, TextureResType texType = TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
protected:
virtual void initRenderer() override;
virtual void onSizeChanged() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void adaptRenderers() override;
void loadTexture(SpriteFrame* spriteframe);
void setupTexture();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void imageTextureScaleChangedWithSize();
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
bool _scale9Enabled;
bool _prevIgnoreSize;
Rect _capInsets;
Scale9Sprite* _imageRenderer;
TextureResType _imageTexType;
2021-10-23 23:27:14 +08:00
Vec2 _imageTextureSize;
2019-11-23 20:27:39 +08:00
bool _imageRendererAdaptDirty;
std::string _textureFile;
};
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_AX_END
2019-11-23 20:27:39 +08:00
// end of ui group
/// @}
#endif /* defined(__CocoGUI__ImageView__) */