axmol/cocos/ui/UILoadingBar.h

212 lines
5.8 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.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __UILOADINGBAR_H__
#define __UILOADINGBAR_H__
#include "ui/UIWidget.h"
#include "ui/GUIExport.h"
NS_CC_BEGIN
/**
* @addtogroup ui
* @{
*/
struct CC_DLL ResourceData;
2021-12-25 10:04:45 +08:00
namespace ui
{
class Scale9Sprite;
2019-11-23 20:27:39 +08:00
/**
*@brief Visual indicator of progress in some operation.
* Displays a bar to the user representing how far the operation has progressed.
*
*/
class CC_GUI_DLL LoadingBar : public Widget
{
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:
/**
* Loading bar progress direction.
*/
enum class Direction
{
LEFT,
RIGHT
};
/**
* Default constructor.
* @js ctor
* @lua new
*/
LoadingBar();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Default destructor.
* @js NA
* @lua NA
*/
virtual ~LoadingBar();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Create an empty LoadingBar.
*@return A LoadingBar instance.
*/
static LoadingBar* create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Create a LoadingBar with a texture name and a predefined progress value.
*
* @param textureName LoadingBar background texture name.
* @param percentage A percentage in float.
* @return A LoadingBar instance.
*/
2021-12-26 23:26:34 +08:00
static LoadingBar* create(std::string_view textureName, float percentage = 0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Create a LoadingBar with a texture name along with its texture type and a predefined progress value.
*
* @param textureName LoadingBar background texture name.
* @param texType LoadingBar background texture type.
* @param percentage A percentage in float, default value is 0.
* @return A LoadingBar instance.
*/
2021-12-26 23:26:34 +08:00
static LoadingBar* create(std::string_view textureName, TextureResType texType, float percentage = 0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Change the progress direction of LoadingBar.
*
* @see Direction `LEFT` means progress left to right, `RIGHT` otherwise.
* @param direction Direction
*/
void setDirection(Direction direction);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get the progress direction of LoadingBar.
*
* @see Direction `LEFT` means progress left to right, `RIGHT` otherwise.
* @return LoadingBar progress direction.
*/
2021-12-25 10:04:45 +08:00
Direction getDirection() const;
2019-11-23 20:27:39 +08:00
/**
* Load texture for LoadingBar.
*
* @param texture File name of texture.
* @param texType Texture resource type,@see TextureResType.
*/
2021-12-26 23:26:34 +08:00
void loadTexture(std::string_view texture, TextureResType texType = TextureResType::LOCAL);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Changes the progress value of LoadingBar.
*
* @param percent Percent value from 1 to 100.
*/
void setPercent(float percent);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get the progress value of LoadingBar.
*
* @return Progress value from 1 to 100.
*/
float getPercent() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Enable scale9 renderer.
*
* @param enabled Set to true will use scale9 renderer, false otherwise.
*/
void setScale9Enabled(bool enabled);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether LoadingBar is using scale9 renderer or not.
* @return Whether LoadingBar uses scale9 renderer or not.
*/
2021-12-25 10:04:45 +08:00
bool isScale9Enabled() const;
2019-11-23 20:27:39 +08:00
/**
* Set capInsets for LoadingBar.
* This setting only take effect when enable scale9 renderer.
* @param capInsets CapInset in `Rect`.
*/
2021-12-25 10:04:45 +08:00
void setCapInsets(const Rect& capInsets);
2019-11-23 20:27:39 +08:00
/**
* @brief Query LoadingBar's capInsets.
* @return CapInsets of LoadingBar.
*/
2021-12-25 10:04:45 +08:00
const Rect& getCapInsets() const;
// override methods.
2019-11-23 20:27:39 +08:00
virtual void ignoreContentAdaptWithSize(bool ignore) 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;
virtual std::string getDescription() const override;
2021-12-25 10:04:45 +08:00
ResourceData getRenderFile();
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
void setScale9Scale();
void updateProgressBar();
void barRendererScaleChangedWithSize();
void setupTexture();
void handleSpriteFlipX();
void loadTexture(SpriteFrame* spriteframe);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void adaptRenderers() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
Direction _direction;
float _percent;
float _totalLength;
Scale9Sprite* _barRenderer;
TextureResType _renderBarTexType;
2021-10-23 23:27:14 +08:00
Vec2 _barRendererTextureSize;
2019-11-23 20:27:39 +08:00
Rect _originalRect;
bool _scale9Enabled;
bool _prevIgnoreSize;
Rect _capInsets;
bool _barRendererAdaptDirty;
std::string _textureFile;
};
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
// end of ui group
/// @}
NS_CC_END
#endif /* defined(__CocoGUI__LoadingBar__) */