axmol/core/2d/ActionInstant.h

401 lines
9.2 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axmol.dev/
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 __CCINSTANT_ACTION_H__
#define __CCINSTANT_ACTION_H__
#include <functional>
#include "2d/Action.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup actions
* @{
*/
/** @class ActionInstant
2021-12-25 10:04:45 +08:00
* @brief Instant actions are immediate actions. They don't have a duration like the IntervalAction actions.
**/
2022-07-16 10:43:05 +08:00
class AX_DLL ActionInstant : public FiniteTimeAction
2019-11-23 20:27:39 +08:00
{
public:
//
// Overrides
//
virtual ActionInstant* clone() const override
{
2022-07-16 10:43:05 +08:00
AX_ASSERT(0);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
virtual ActionInstant* reverse() const override
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AX_ASSERT(0);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
virtual void startWithTarget(Node* target) override;
2019-11-23 20:27:39 +08:00
virtual bool isDone() const override;
/**
* @param dt In seconds.
*/
virtual void step(float dt) override;
/**
* @param time In seconds.
*/
virtual void update(float time) override;
private:
bool _done;
};
/** @class Show
2021-12-25 10:04:45 +08:00
* @brief Show the node.
**/
2022-07-16 10:43:05 +08:00
class AX_DLL Show : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Allocates and initializes the action.
*
* @return An autoreleased Show object.
*/
2021-12-25 10:04:45 +08:00
static Show* create();
2019-11-23 20:27:39 +08:00
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual ActionInstant* reverse() const override;
virtual Show* clone() const override;
Show() {}
2021-12-25 10:04:45 +08:00
virtual ~Show() {}
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(Show);
2019-11-23 20:27:39 +08:00
};
/** @class Hide
2021-12-25 10:04:45 +08:00
* @brief Hide the node.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Hide : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Allocates and initializes the action.
*
* @return An autoreleased Hide object.
*/
2021-12-25 10:04:45 +08:00
static Hide* create();
2019-11-23 20:27:39 +08:00
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual ActionInstant* reverse() const override;
virtual Hide* clone() const override;
Hide() {}
2021-12-25 10:04:45 +08:00
virtual ~Hide() {}
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(Hide);
2019-11-23 20:27:39 +08:00
};
/** @class ToggleVisibility
2021-12-25 10:04:45 +08:00
* @brief Toggles the visibility of a node.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL ToggleVisibility : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Allocates and initializes the action.
*
* @return An autoreleased ToggleVisibility object.
*/
2021-12-25 10:04:45 +08:00
static ToggleVisibility* create();
2019-11-23 20:27:39 +08:00
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual ToggleVisibility* reverse() const override;
virtual ToggleVisibility* clone() const override;
ToggleVisibility() {}
2021-12-25 10:04:45 +08:00
virtual ~ToggleVisibility() {}
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(ToggleVisibility);
2019-11-23 20:27:39 +08:00
};
/** @class RemoveSelf
2021-12-25 10:04:45 +08:00
* @brief Remove the node.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL RemoveSelf : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Create the action.
*
* @param isNeedCleanUp Is need to clean up, the default value is true.
* @return An autoreleased RemoveSelf object.
*/
2021-12-25 10:04:45 +08:00
static RemoveSelf* create(bool isNeedCleanUp = true);
2019-11-23 20:27:39 +08:00
//
// Override
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual RemoveSelf* clone() const override;
virtual RemoveSelf* reverse() const override;
2021-12-25 10:04:45 +08:00
RemoveSelf() : _isNeedCleanUp(true) {}
2021-12-25 10:04:45 +08:00
virtual ~RemoveSelf() {}
2019-11-23 20:27:39 +08:00
/** init the action */
bool init(bool isNeedCleanUp);
protected:
bool _isNeedCleanUp;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(RemoveSelf);
2019-11-23 20:27:39 +08:00
};
/** @class FlipX
2021-12-25 10:04:45 +08:00
* @brief Flips the sprite horizontally.
* @since v0.99.0
*/
2022-07-16 10:43:05 +08:00
class AX_DLL FlipX : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Create the action.
*
* @param x Flips the sprite horizontally if true.
* @return An autoreleased FlipX object.
*/
2021-12-25 10:04:45 +08:00
static FlipX* create(bool x);
2019-11-23 20:27:39 +08:00
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual FlipX* reverse() const override;
virtual FlipX* clone() const override;
2021-12-25 10:04:45 +08:00
FlipX() : _flipX(false) {}
2019-11-23 20:27:39 +08:00
virtual ~FlipX() {}
/** init the action */
bool initWithFlipX(bool x);
protected:
2021-12-25 10:04:45 +08:00
bool _flipX;
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(FlipX);
2019-11-23 20:27:39 +08:00
};
/** @class FlipY
2021-12-25 10:04:45 +08:00
* @brief Flips the sprite vertically.
* @since v0.99.0
*/
2022-07-16 10:43:05 +08:00
class AX_DLL FlipY : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Create the action.
*
* @param y Flips the sprite vertically if true.
* @return An autoreleased FlipY object.
*/
2021-12-25 10:04:45 +08:00
static FlipY* create(bool y);
2019-11-23 20:27:39 +08:00
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual FlipY* reverse() const override;
virtual FlipY* clone() const override;
2021-12-25 10:04:45 +08:00
FlipY() : _flipY(false) {}
2019-11-23 20:27:39 +08:00
virtual ~FlipY() {}
/** init the action */
bool initWithFlipY(bool y);
protected:
2021-12-25 10:04:45 +08:00
bool _flipY;
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(FlipY);
2019-11-23 20:27:39 +08:00
};
/** @class Place
2021-12-25 10:04:45 +08:00
* @brief Places the node in a certain position.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Place : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Creates a Place action with a position.
*
* @param pos A certain position.
* @return An autoreleased Place object.
*/
2021-12-25 10:04:45 +08:00
static Place* create(const Vec2& pos);
2019-11-23 20:27:39 +08:00
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual Place* reverse() const override;
virtual Place* clone() const override;
2021-12-25 10:04:45 +08:00
Place() {}
2021-12-25 10:04:45 +08:00
virtual ~Place() {}
2019-11-23 20:27:39 +08:00
/** Initializes a Place action with a position */
bool initWithPosition(const Vec2& pos);
protected:
Vec2 _position;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(Place);
2019-11-23 20:27:39 +08:00
};
/** @class CallFunc
2021-12-25 10:04:45 +08:00
* @brief Calls a 'callback'.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CallFunc : public ActionInstant
2019-11-23 20:27:39 +08:00
{
public:
/** Creates the action with the callback of type std::function<void()>.
This is the preferred way to create the callback.
* When this function bound in js or lua ,the input param will be changed.
* In js: var create(var func, var this, var [data]) or var create(var func).
* In lua:local create(local funcID).
*
* @param func A callback function need to be executed.
* @return An autoreleased CallFunc object.
*/
2021-12-25 10:04:45 +08:00
static CallFunc* create(const std::function<void()>& func);
2019-11-23 20:27:39 +08:00
public:
/** Executes the callback.
*/
virtual void execute();
//
// Overrides
//
/**
* @param time In seconds.
*/
virtual void update(float time) override;
virtual CallFunc* reverse() const override;
virtual CallFunc* clone() const override;
2021-12-25 10:04:45 +08:00
CallFunc() = default;
2021-12-25 10:04:45 +08:00
virtual ~CallFunc() = default;
2019-11-23 20:27:39 +08:00
/** initializes the action with the std::function<void()>
* @lua NA
*/
bool initWithFunction(const std::function<void()>& func);
protected:
/** function that will be called */
std::function<void()> _function;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(CallFunc);
2019-11-23 20:27:39 +08:00
};
/** @class CallFuncN
2021-12-25 10:04:45 +08:00
* @brief Calls a 'callback' with the node as the first argument. N means Node.
* @js NA
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CallFuncN : public CallFunc
2019-11-23 20:27:39 +08:00
{
public:
/** Creates the action with the callback of type std::function<void()>.
This is the preferred way to create the callback.
*
* @param func A callback function need to be executed.
* @return An autoreleased CallFuncN object.
*/
2021-12-25 10:04:45 +08:00
static CallFuncN* create(const std::function<void(Node*)>& func);
2019-11-23 20:27:39 +08:00
//
// Overrides
//
virtual CallFuncN* clone() const override;
virtual void execute() override;
2021-12-25 10:04:45 +08:00
CallFuncN() : _functionN(nullptr) {}
2021-12-25 10:04:45 +08:00
virtual ~CallFuncN() {}
2019-11-23 20:27:39 +08:00
/** initializes the action with the std::function<void(Node*)> */
bool initWithFunction(const std::function<void(Node*)>& func);
protected:
/** function that will be called with the "sender" as the 1st argument */
std::function<void(Node*)> _functionN;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(CallFuncN);
2019-11-23 20:27:39 +08:00
};
// end of actions group
/// @}
NS_AX_END
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#endif //__CCINSTANT_ACTION_H__