2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2023-12-30 12:42:53 -05:00
|
|
|
#include <matjson.hpp>
|
2022-12-04 11:39:40 -05:00
|
|
|
#include "casts.hpp"
|
2022-11-28 12:09:39 -05:00
|
|
|
#include "general.hpp"
|
2022-12-01 15:42:49 -05:00
|
|
|
#include "../DefaultInclude.hpp"
|
2022-07-30 12:24:03 -04:00
|
|
|
#include <cocos2d.h>
|
|
|
|
#include <functional>
|
|
|
|
#include <type_traits>
|
2022-12-01 15:42:49 -05:00
|
|
|
#include "../loader/Event.hpp"
|
2024-06-01 08:38:21 -04:00
|
|
|
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>
|
|
|
|
#include <Geode/binding/CCMenuItemToggler.hpp>
|
2023-02-08 10:25:07 -05:00
|
|
|
#include "MiniFunction.hpp"
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
// support converting ccColor3B / ccColor4B to / from json
|
2023-01-27 18:25:19 -05:00
|
|
|
|
|
|
|
template <>
|
2023-12-30 12:42:53 -05:00
|
|
|
struct matjson::Serialize<cocos2d::ccColor3B> {
|
|
|
|
static matjson::Value GEODE_DLL to_json(cocos2d::ccColor3B const& color);
|
|
|
|
static cocos2d::ccColor3B GEODE_DLL from_json(matjson::Value const& color);
|
2024-01-24 09:17:42 -05:00
|
|
|
static bool GEODE_DLL is_json(matjson::Value const& json);
|
2023-01-27 18:25:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2023-12-30 12:42:53 -05:00
|
|
|
struct matjson::Serialize<cocos2d::ccColor4B> {
|
|
|
|
static matjson::Value GEODE_DLL to_json(cocos2d::ccColor4B const& color);
|
|
|
|
static cocos2d::ccColor4B GEODE_DLL from_json(matjson::Value const& color);
|
2024-01-24 09:17:42 -05:00
|
|
|
static bool GEODE_DLL is_json(matjson::Value const& json);
|
2023-01-27 18:25:19 -05:00
|
|
|
};
|
2022-11-28 12:09:39 -05:00
|
|
|
|
|
|
|
// operators for CC geometry
|
2023-03-22 15:49:29 -04:00
|
|
|
namespace cocos2d {
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCPoint& operator*=(cocos2d::CCPoint& pos, float mul) {
|
2022-11-28 12:09:39 -05:00
|
|
|
pos.x *= mul;
|
|
|
|
pos.y *= mul;
|
|
|
|
return pos;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize& operator*=(cocos2d::CCSize& size, float mul) {
|
2022-11-28 12:09:39 -05:00
|
|
|
size.width *= mul;
|
|
|
|
size.height *= mul;
|
|
|
|
return size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator*(cocos2d::CCSize const& size, cocos2d::CCPoint const& point) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return {
|
|
|
|
size.width * point.x,
|
|
|
|
size.height * point.y,
|
|
|
|
};
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator*=(cocos2d::CCRect& rect, float mul) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.origin *= mul;
|
|
|
|
rect.size *= mul;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator*(cocos2d::CCRect const& rect, float mul) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return {
|
|
|
|
rect.origin.x * mul,
|
|
|
|
rect.origin.y * mul,
|
|
|
|
rect.size.width * mul,
|
|
|
|
rect.size.height * mul,
|
|
|
|
};
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCPoint operator/=(cocos2d::CCPoint& pos, float div) {
|
2022-11-28 12:09:39 -05:00
|
|
|
pos.x /= div;
|
|
|
|
pos.y /= div;
|
|
|
|
return pos;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator/=(cocos2d::CCSize& size, float div) {
|
2022-11-28 12:09:39 -05:00
|
|
|
size.width /= div;
|
|
|
|
size.height /= div;
|
|
|
|
return size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator/=(cocos2d::CCRect& rect, float div) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.origin /= div;
|
|
|
|
rect.size /= div;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCPoint operator+=(cocos2d::CCPoint& pos, cocos2d::CCPoint const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
pos.x += add.x;
|
|
|
|
pos.y += add.y;
|
|
|
|
return pos;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator+=(cocos2d::CCSize& size, cocos2d::CCPoint const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
size.width += add.x;
|
|
|
|
size.height += add.y;
|
|
|
|
return size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator+=(cocos2d::CCSize& size, cocos2d::CCSize const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
size.width += add.width;
|
|
|
|
size.height += add.height;
|
|
|
|
return size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator+=(cocos2d::CCRect& rect, cocos2d::CCPoint const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.origin += add;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator+=(cocos2d::CCRect& rect, cocos2d::CCSize const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.size += add;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator+=(cocos2d::CCRect& rect, cocos2d::CCRect const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.origin += add.origin;
|
|
|
|
rect.size += add.size;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCPoint operator-=(cocos2d::CCPoint& pos, cocos2d::CCPoint const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
pos.x -= add.x;
|
|
|
|
pos.y -= add.y;
|
|
|
|
return pos;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator-=(cocos2d::CCSize& size, cocos2d::CCPoint const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
size.width -= add.x;
|
|
|
|
size.height -= add.y;
|
|
|
|
return size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator-=(cocos2d::CCSize& size, cocos2d::CCSize const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
size.width -= add.width;
|
|
|
|
size.height -= add.height;
|
|
|
|
return size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator-=(cocos2d::CCRect& rect, cocos2d::CCPoint const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.origin -= add;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator-=(cocos2d::CCRect& rect, cocos2d::CCSize const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.size -= add;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCRect operator-=(cocos2d::CCRect& rect, cocos2d::CCRect const& add) {
|
2022-11-28 12:09:39 -05:00
|
|
|
rect.origin -= add.origin;
|
|
|
|
rect.size -= add.size;
|
|
|
|
return rect;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator-(cocos2d::CCSize const& size, float f) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {size.width - f, size.height - f};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr cocos2d::CCSize operator-(cocos2d::CCSize const& size) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {-size.width, -size.height};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator==(cocos2d::CCPoint const& p1, cocos2d::CCPoint const& p2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return p1.x == p2.x && p1.y == p2.y;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator!=(cocos2d::CCPoint const& p1, cocos2d::CCPoint const& p2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return p1.x != p2.x || p1.y != p2.y;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator==(cocos2d::CCSize const& s1, cocos2d::CCSize const& s2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return s1.width == s2.width && s1.height == s2.height;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator!=(cocos2d::CCSize const& s1, cocos2d::CCSize const& s2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return s1.width != s2.width || s1.height != s2.height;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator<(cocos2d::CCSize const& s1, cocos2d::CCSize const& s2) {
|
2024-07-28 10:16:41 -04:00
|
|
|
return s1.width < s2.width && s1.height < s2.height;
|
2024-04-23 15:24:08 -04:00
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator<=(cocos2d::CCSize const& s1, cocos2d::CCSize const& s2) {
|
2024-07-28 10:16:41 -04:00
|
|
|
return s1.width <= s2.width && s1.height <= s2.height;
|
2024-04-23 15:24:08 -04:00
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator>(cocos2d::CCSize const& s1, cocos2d::CCSize const& s2) {
|
2024-07-28 10:16:41 -04:00
|
|
|
return s1.width > s2.width && s1.height > s2.height;
|
2024-04-23 15:24:08 -04:00
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator>=(cocos2d::CCSize const& s1, cocos2d::CCSize const& s2) {
|
2024-07-28 10:16:41 -04:00
|
|
|
return s1.width >= s2.width && s1.height >= s2.height;
|
2024-04-23 15:24:08 -04:00
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator==(cocos2d::CCRect const& r1, cocos2d::CCRect const& r2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return r1.origin == r2.origin && r1.size == r2.size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator!=(cocos2d::CCRect const& r1, cocos2d::CCRect const& r2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return r1.origin != r2.origin || r1.size != r2.size;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator==(cocos2d::ccColor4B const& c1, cocos2d::ccColor4B const& c2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b && c1.a == c2.a;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator!=(cocos2d::ccColor4B const& c1, cocos2d::ccColor4B const& c2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return c1.r != c2.r || c1.g != c2.g || c1.b != c2.b || c1.a != c2.a;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator==(cocos2d::ccColor3B const& c1, cocos2d::ccColor3B const& c2) {
|
2023-03-15 16:36:13 -04:00
|
|
|
return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator!=(cocos2d::ccColor3B const& c1, cocos2d::ccColor3B const& c2) {
|
2022-11-28 12:09:39 -05:00
|
|
|
return c1.r != c2.r || c1.g != c2.g || c1.b != c2.b;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator==(cocos2d::ccHSVValue const& c1, cocos2d::ccHSVValue const& c2) {
|
2023-03-15 16:36:13 -04:00
|
|
|
return c1.h == c2.h && c1.s == c2.s && c1.v == c2.v &&
|
|
|
|
c1.absoluteSaturation == c2.absoluteSaturation &&
|
|
|
|
c1.absoluteBrightness == c2.absoluteBrightness;
|
|
|
|
}
|
2024-09-12 06:53:48 -04:00
|
|
|
static constexpr bool operator!=(cocos2d::ccHSVValue const& c1, cocos2d::ccHSVValue const& c2) {
|
2023-03-15 16:36:13 -04:00
|
|
|
return !(c1 == c2);
|
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
2022-12-01 15:42:49 -05:00
|
|
|
// Ref & Bug
|
2022-11-28 12:09:39 -05:00
|
|
|
namespace geode {
|
|
|
|
/**
|
|
|
|
* A smart pointer to a managed CCObject-deriving class. Retains shared
|
|
|
|
* ownership over the managed instance. Releases the object when the Ref
|
|
|
|
* is destroyed, or assigned another object or nullptr.
|
|
|
|
*
|
|
|
|
* Use-cases include, for example, non-CCNode class members, or nodes that
|
|
|
|
* are not always in the scene tree.
|
2023-01-21 07:17:33 -05:00
|
|
|
*
|
|
|
|
* @tparam T A type that inherits from CCObject.
|
2022-11-28 12:09:39 -05:00
|
|
|
*
|
2023-01-24 14:50:40 -05:00
|
|
|
* @example
|
2023-01-21 07:17:33 -05:00
|
|
|
* class MyNode : public CCNode {
|
2022-11-28 12:09:39 -05:00
|
|
|
* protected:
|
|
|
|
* // no need to manually call retain or
|
|
|
|
* // release on this array; Ref manages it
|
|
|
|
* // for you :3
|
|
|
|
* Ref<CCArray> m_list = CCArray::create();
|
2023-01-21 07:17:33 -05:00
|
|
|
*
|
|
|
|
* bool init() {
|
|
|
|
* if (!CCNode::init())
|
|
|
|
* return false;
|
|
|
|
*
|
|
|
|
* // No need to do m_list = CCArray::create()
|
|
|
|
* // or m_list->retain() :3
|
|
|
|
*
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
*
|
2023-01-24 14:50:40 -05:00
|
|
|
* @example
|
|
|
|
* // Save a child from the current layer into a menu
|
|
|
|
* Ref<CCMenu> menu = static_cast<CCMenu*>(this->getChildByID("main-menu"));
|
2023-01-21 07:17:33 -05:00
|
|
|
*
|
2023-01-24 14:50:40 -05:00
|
|
|
* // Remove the menu from its parent
|
|
|
|
* menu->removeFromParent();
|
2023-01-21 07:17:33 -05:00
|
|
|
*
|
2023-01-24 14:50:40 -05:00
|
|
|
* // Menu will still point to a valid CCMenu as long as the menu variable exist
|
2022-11-28 12:09:39 -05:00
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class Ref final {
|
|
|
|
static_assert(
|
|
|
|
std::is_base_of_v<cocos2d::CCObject, T>,
|
|
|
|
"Ref can only be used with a CCObject-inheriting class!"
|
|
|
|
);
|
|
|
|
|
|
|
|
T* m_obj = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a Ref of an object. The object will be retained and
|
|
|
|
* managed until Ref goes out of scope
|
2023-01-21 07:17:33 -05:00
|
|
|
* @param obj Object to construct the Ref from
|
2022-11-28 12:09:39 -05:00
|
|
|
*/
|
|
|
|
Ref(T* obj) : m_obj(obj) {
|
|
|
|
CC_SAFE_RETAIN(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref(Ref<T> const& other) : Ref(other.data()) {}
|
|
|
|
|
2024-05-25 12:18:51 -04:00
|
|
|
Ref(Ref<T>&& other) noexcept : m_obj(other.m_obj) {
|
2022-11-28 12:09:39 -05:00
|
|
|
other.m_obj = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an empty Ref (the managed object will be null)
|
|
|
|
*/
|
|
|
|
Ref() = default;
|
|
|
|
|
|
|
|
~Ref() {
|
|
|
|
CC_SAFE_RELEASE(m_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap the managed object with another object. The managed object
|
|
|
|
* will be released, and the new object retained
|
|
|
|
* @param other The new object to swap to
|
|
|
|
*/
|
|
|
|
void swap(T* other) {
|
|
|
|
CC_SAFE_RELEASE(m_obj);
|
|
|
|
m_obj = other;
|
|
|
|
CC_SAFE_RETAIN(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the managed object
|
|
|
|
* @returns The managed object
|
|
|
|
*/
|
|
|
|
T* data() const {
|
|
|
|
return m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() const {
|
|
|
|
return m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator*() const {
|
|
|
|
return m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() const {
|
|
|
|
return m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator=(T* obj) {
|
|
|
|
this->swap(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<T>& operator=(Ref<T> const& other) {
|
|
|
|
this->swap(other.data());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<T>& operator=(Ref<T>&& other) {
|
2024-06-17 06:56:12 -04:00
|
|
|
m_obj = other.data();
|
|
|
|
other.m_obj = nullptr;
|
2022-11-28 12:09:39 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(T* other) const {
|
|
|
|
return m_obj == other;
|
|
|
|
}
|
2022-12-04 11:39:40 -05:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
bool operator==(Ref<T> const& other) const {
|
|
|
|
return m_obj == other.m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(T* other) const {
|
|
|
|
return m_obj != other;
|
|
|
|
}
|
2022-12-04 11:39:40 -05:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
bool operator!=(Ref<T> const& other) const {
|
|
|
|
return m_obj != other.m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for containers
|
|
|
|
bool operator<(Ref<T> const& other) const {
|
|
|
|
return m_obj < other.m_obj;
|
|
|
|
}
|
2023-03-07 01:50:20 -05:00
|
|
|
bool operator<=(Ref<T> const& other) const {
|
|
|
|
return m_obj <= other.m_obj;
|
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
bool operator>(Ref<T> const& other) const {
|
|
|
|
return m_obj > other.m_obj;
|
|
|
|
}
|
2023-03-07 01:50:20 -05:00
|
|
|
bool operator>=(Ref<T> const& other) const {
|
|
|
|
return m_obj >= other.m_obj;
|
|
|
|
}
|
2022-11-28 12:09:39 -05:00
|
|
|
};
|
2022-12-07 14:21:50 -05:00
|
|
|
|
2023-04-07 13:07:16 -04:00
|
|
|
class WeakRefPool;
|
|
|
|
|
|
|
|
class GEODE_DLL WeakRefController final {
|
|
|
|
private:
|
|
|
|
cocos2d::CCObject* m_obj;
|
|
|
|
|
|
|
|
WeakRefController(WeakRefController const&) = delete;
|
|
|
|
WeakRefController(WeakRefController&&) = delete;
|
|
|
|
|
|
|
|
friend class WeakRefPool;
|
2023-03-30 15:07:57 -04:00
|
|
|
|
|
|
|
public:
|
2023-04-07 13:07:16 -04:00
|
|
|
WeakRefController() = default;
|
|
|
|
|
|
|
|
bool isManaged();
|
|
|
|
void swap(cocos2d::CCObject* other);
|
|
|
|
cocos2d::CCObject* get() const;
|
|
|
|
};
|
2023-03-30 15:07:57 -04:00
|
|
|
|
2023-04-07 13:07:16 -04:00
|
|
|
class GEODE_DLL WeakRefPool final {
|
|
|
|
std::unordered_map<cocos2d::CCObject*, std::shared_ptr<WeakRefController>> m_pool;
|
|
|
|
|
2023-03-30 15:07:57 -04:00
|
|
|
void check(cocos2d::CCObject* obj);
|
2023-04-07 13:07:16 -04:00
|
|
|
|
|
|
|
friend class WeakRefController;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static WeakRefPool* get();
|
|
|
|
|
|
|
|
std::shared_ptr<WeakRefController> manage(cocos2d::CCObject* obj);
|
2023-03-30 15:07:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A smart pointer to a managed CCObject-deriving class. Like Ref, except
|
|
|
|
* only holds a weak reference to the targeted object. When all non-weak
|
|
|
|
* references (Refs, manual retain() calls) to the object are dropped, so
|
|
|
|
* are all weak references.
|
|
|
|
*
|
|
|
|
* In essence, WeakRef is like a raw pointer, except that you can know if
|
|
|
|
* the pointer is still valid or not, as WeakRef::lock() returns nullptr if
|
|
|
|
* the pointed-to-object has already been freed.
|
|
|
|
*
|
|
|
|
* Note that an object pointed to by WeakRef is only released once some
|
|
|
|
* WeakRef pointing to it checks for it after all other references to the
|
|
|
|
* object have been dropped. If you store WeakRefs in a global map, you may
|
|
|
|
* want to periodically lock all of them to make sure any memory that should
|
|
|
|
* be freed is freed.
|
|
|
|
*
|
|
|
|
* @tparam T A type that inherits from CCObject.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class WeakRef final {
|
|
|
|
static_assert(
|
|
|
|
std::is_base_of_v<cocos2d::CCObject, T>,
|
|
|
|
"WeakRef can only be used with a CCObject-inheriting class!"
|
|
|
|
);
|
|
|
|
|
2023-04-07 13:07:16 -04:00
|
|
|
std::shared_ptr<WeakRefController> m_controller;
|
|
|
|
|
|
|
|
WeakRef(std::shared_ptr<WeakRefController> obj) : m_controller(obj) {}
|
2023-03-30 15:07:57 -04:00
|
|
|
|
2024-09-10 14:09:09 -04:00
|
|
|
friend class std::hash<WeakRef<T>>;
|
|
|
|
|
|
|
|
|
2023-03-30 15:07:57 -04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a WeakRef of an object. A weak reference is one that will
|
|
|
|
* be valid as long as the object is referenced by other strong
|
|
|
|
* references (such as Ref or manual retain calls), but once all strong
|
|
|
|
* references are dropped, so are all weak references. The object is
|
|
|
|
* freed once no strong references exist to it, and any WeakRef pointing
|
|
|
|
* to it is freed or locked
|
|
|
|
* @param obj Object to construct the WeakRef from
|
|
|
|
*/
|
2023-04-07 13:07:16 -04:00
|
|
|
WeakRef(T* obj) : m_controller(WeakRefPool::get()->manage(obj)) {}
|
2023-03-30 15:07:57 -04:00
|
|
|
|
2023-04-07 13:07:16 -04:00
|
|
|
WeakRef(WeakRef<T> const& other) : WeakRef(other.m_controller) {}
|
2023-03-30 15:07:57 -04:00
|
|
|
|
2023-04-07 13:07:16 -04:00
|
|
|
WeakRef(WeakRef<T>&& other) : m_controller(std::move(other.m_controller)) {
|
|
|
|
other.m_controller = nullptr;
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an empty WeakRef (the object will be null)
|
|
|
|
*/
|
|
|
|
WeakRef() = default;
|
|
|
|
~WeakRef() {
|
2023-04-07 13:07:16 -04:00
|
|
|
// If the WeakRef is moved, m_controller is null
|
|
|
|
if (m_controller) {
|
|
|
|
m_controller->isManaged();
|
|
|
|
}
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock the WeakRef, returning a Ref if the pointed object is valid or
|
|
|
|
* a null Ref if the object has been freed
|
|
|
|
*/
|
|
|
|
Ref<T> lock() const {
|
2023-04-07 13:07:16 -04:00
|
|
|
if (m_controller->isManaged()) {
|
|
|
|
return Ref(static_cast<T*>(m_controller->get()));
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
return Ref<T>(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the WeakRef points to a valid object
|
|
|
|
*/
|
|
|
|
bool valid() const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->isManaged();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap the managed object with another object. The managed object
|
|
|
|
* will be released, and the new object retained
|
|
|
|
* @param other The new object to swap to
|
|
|
|
*/
|
|
|
|
void swap(T* other) {
|
2023-04-07 13:07:16 -04:00
|
|
|
m_controller->swap(other);
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<T> operator=(T* obj) {
|
|
|
|
this->swap(obj);
|
|
|
|
return this->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakRef<T>& operator=(WeakRef<T> const& other) {
|
2023-04-07 13:07:16 -04:00
|
|
|
this->swap(static_cast<T*>(other.m_controller->get()));
|
2023-03-30 15:07:57 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakRef<T>& operator=(WeakRef<T>&& other) {
|
2024-06-17 06:56:12 -04:00
|
|
|
m_controller = std::move(other.m_controller);
|
2023-03-30 15:07:57 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const noexcept {
|
|
|
|
return this->valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(T* other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() == other;
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(WeakRef<T> const& other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() == other.m_controller->get();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(T* other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() != other;
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(WeakRef<T> const& other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() != other.m_controller->get();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// for containers
|
|
|
|
bool operator<(WeakRef<T> const& other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() < other.m_controller->get();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
bool operator<=(WeakRef<T> const& other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() <= other.m_controller->get();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
bool operator>(WeakRef<T> const& other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() > other.m_controller->get();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
bool operator>=(WeakRef<T> const& other) const {
|
2023-04-07 13:07:16 -04:00
|
|
|
return m_controller->get() >= other.m_controller->get();
|
2023-03-30 15:07:57 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-07 14:21:50 -05:00
|
|
|
template <class Filter>
|
|
|
|
class EventListenerNode : public cocos2d::CCNode {
|
|
|
|
protected:
|
|
|
|
EventListener<Filter> m_listener;
|
2022-12-13 15:39:45 -05:00
|
|
|
|
|
|
|
EventListenerNode(EventListener<Filter>&& listener)
|
|
|
|
: m_listener(std::move(listener)) {}
|
2022-12-07 14:21:50 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
static EventListenerNode* create(EventListener<Filter> listener) {
|
2022-12-13 15:39:45 -05:00
|
|
|
auto ret = new EventListenerNode(std::move(listener));
|
2024-06-23 05:36:16 -04:00
|
|
|
if (ret->init()) {
|
2022-12-07 14:21:50 -05:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2024-06-23 05:36:16 -04:00
|
|
|
delete ret;
|
2022-12-07 14:21:50 -05:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-03-23 13:14:09 -04:00
|
|
|
static EventListenerNode* create(typename Filter::Callback callback, Filter filter = Filter()) {
|
|
|
|
auto ret = new EventListenerNode(EventListener<Filter>(callback, filter));
|
2024-06-23 05:36:16 -04:00
|
|
|
if (ret->init()) {
|
2022-12-07 14:21:50 -05:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2024-06-23 05:36:16 -04:00
|
|
|
delete ret;
|
2022-12-07 14:21:50 -05:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class C>
|
|
|
|
static EventListenerNode* create(
|
2022-12-10 11:30:14 -05:00
|
|
|
C* cls, typename EventListener<Filter>::template MemberFn<C> callback
|
2022-12-07 14:21:50 -05:00
|
|
|
) {
|
|
|
|
// for some reason msvc won't let me just call EventListenerNode::create...
|
|
|
|
// it claims no return value...
|
|
|
|
// despite me writing return EventListenerNode::create()......
|
2022-12-13 15:39:45 -05:00
|
|
|
auto ret = new EventListenerNode(EventListener<Filter>(cls, callback));
|
2024-06-23 05:36:16 -04:00
|
|
|
if (ret->init()) {
|
2022-12-07 14:21:50 -05:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2024-06-23 05:36:16 -04:00
|
|
|
delete ret;
|
2022-12-07 14:21:50 -05:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
2024-03-05 11:25:25 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple `CCObject` wrapper for a non-`CCObject` type
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
requires (!std::is_base_of_v<T, cocos2d::CCObject>)
|
|
|
|
class ObjWrapper : public cocos2d::CCObject {
|
|
|
|
protected:
|
|
|
|
T m_value;
|
|
|
|
|
|
|
|
ObjWrapper(T&& value) : m_value(std::forward<T>(value)) {
|
|
|
|
this->autorelease();
|
|
|
|
}
|
|
|
|
ObjWrapper(T const& value) : m_value(value) {
|
|
|
|
this->autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct an object wrapper
|
|
|
|
*/
|
|
|
|
static ObjWrapper* create(T&& value) {
|
|
|
|
return new ObjWrapper(std::forward<T>(value));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Construct an object wrapper
|
|
|
|
*/
|
|
|
|
static ObjWrapper* create(T const& value) {
|
|
|
|
return new ObjWrapper(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @note This returns a const& to allow move-only types to be returned!
|
|
|
|
T const& getValue() const& {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
void setValue(T&& value) {
|
|
|
|
m_value = std::forward<T>(value);
|
|
|
|
}
|
|
|
|
};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cocos2d utils
|
2022-07-30 12:24:03 -04:00
|
|
|
namespace geode::cocos {
|
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Get child at index. Checks bounds. A negative
|
2022-07-30 12:24:03 -04:00
|
|
|
* index will get the child starting from the end
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns Child at index cast to the given type,
|
2022-07-30 12:24:03 -04:00
|
|
|
* or nullptr if index exceeds bounds
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T = cocos2d::CCNode>
|
2022-08-06 14:46:18 -04:00
|
|
|
static T* getChild(cocos2d::CCNode* x, int i) {
|
2022-07-30 12:24:03 -04:00
|
|
|
// start from end for negative index
|
|
|
|
if (i < 0) i = x->getChildrenCount() + i;
|
|
|
|
// check if backwards index is out of bounds
|
|
|
|
if (i < 0) return nullptr;
|
|
|
|
// check if forwards index is out of bounds
|
|
|
|
if (static_cast<int>(x->getChildrenCount()) <= i) return nullptr;
|
2022-10-22 13:19:58 -04:00
|
|
|
return static_cast<T*>(x->getChildren()->objectAtIndex(i));
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Get nth child that is a given type. Checks bounds.
|
|
|
|
* @returns Child at index cast to the given type,
|
2022-07-30 12:24:03 -04:00
|
|
|
* or nullptr if index exceeds bounds
|
|
|
|
*/
|
2024-10-13 08:16:31 -04:00
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class Type = cocos2d::CCNode>
|
2024-10-13 08:16:31 -04:00
|
|
|
[[deprecated("Use CCNode::getChildByType instead")]]
|
2023-02-12 06:01:12 -05:00
|
|
|
static Type* getChildOfType(cocos2d::CCNode* node, int index) {
|
2024-10-13 07:19:07 -04:00
|
|
|
return node->getChildByType<Type>(index);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:46:18 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Return a node, or create a default one if it's
|
2022-08-06 14:46:18 -04:00
|
|
|
* nullptr. Syntactic sugar function
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T, class... Args>
|
2022-08-06 14:46:18 -04:00
|
|
|
static T* nodeOrDefault(T* node, Args... args) {
|
|
|
|
return node ? node : T::create(args...);
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Get bounds for a set of nodes. Based on content
|
2022-07-30 12:24:03 -04:00
|
|
|
* size
|
|
|
|
* @param nodes Nodes to calculate coverage of
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns Rectangle fitting all nodes. Origin
|
2022-07-30 12:24:03 -04:00
|
|
|
* will be <= 0 and size will be >= 0
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCRect calculateNodeCoverage(std::vector<cocos2d::CCNode*> const& nodes);
|
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Get bounds for a set of nodes. Based on content
|
2022-07-30 12:24:03 -04:00
|
|
|
* size
|
|
|
|
* @param nodes Nodes to calculate coverage of
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns Rectangle fitting all nodes. Origin
|
2022-07-30 12:24:03 -04:00
|
|
|
* will be <= 0 and size will be >= 0
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCRect calculateNodeCoverage(cocos2d::CCArray* nodes);
|
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Get bounds for a set of nodes. Based on content
|
2022-07-30 12:24:03 -04:00
|
|
|
* size
|
|
|
|
* @param parent Parent whose children to calculate
|
|
|
|
* coverage of
|
|
|
|
* @returns Rectangle fitting all the parent's children.
|
|
|
|
* Origin will be <= 0 and size will be >= 0
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCRect calculateChildCoverage(cocos2d::CCNode* parent);
|
|
|
|
|
2022-10-23 10:54:42 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Create a CCScene from a layer and switch to it with the default fade
|
2022-10-23 10:54:42 -04:00
|
|
|
* transition
|
|
|
|
* @param layer Layer to create a scene from
|
|
|
|
* @returns Created scene (not the fade transition)
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCScene* switchToScene(cocos2d::CCLayer* layer);
|
|
|
|
|
2023-02-08 10:25:07 -05:00
|
|
|
using CreateLayerFunc = utils::MiniFunction<cocos2d::CCLayer*()>;
|
2022-10-26 10:56:52 -04:00
|
|
|
|
|
|
|
/**
|
2022-12-04 11:39:40 -05:00
|
|
|
* Reload textures, overwriting the scene to return to after the loading
|
2022-10-26 10:56:52 -04:00
|
|
|
* screen is finished
|
2022-12-04 11:39:40 -05:00
|
|
|
* @param returnTo A function that returns a new layer. After loading is
|
|
|
|
* finished, the game switches to the given layer instead of MenuLayer.
|
2022-10-26 10:56:52 -04:00
|
|
|
* Leave nullptr to enable default behaviour
|
|
|
|
*/
|
|
|
|
GEODE_DLL void reloadTextures(CreateLayerFunc returnTo = nullptr);
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
/**
|
|
|
|
* Rescale node to fit inside given size
|
|
|
|
* @param node Node to rescale
|
|
|
|
* @param size Size to fit inside
|
|
|
|
* @param def Default size
|
|
|
|
* @param min Minimum size
|
|
|
|
*/
|
2022-12-04 11:39:40 -05:00
|
|
|
GEODE_DLL void limitNodeSize(cocos2d::CCNode* node, cocos2d::CCSize const& size, float def, float min);
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2024-09-10 14:13:42 -04:00
|
|
|
/**
|
|
|
|
* Rescale node to fit inside given width
|
|
|
|
* @param node Node to rescale
|
|
|
|
* @param width Width to fit inside
|
|
|
|
* @param def Default scale
|
|
|
|
* @param min Minimum scale
|
|
|
|
*/
|
|
|
|
GEODE_DLL void limitNodeWidth(cocos2d::CCNode* node, float width, float def, float min);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rescale node to fit inside given height
|
|
|
|
* @param node Node to rescale
|
|
|
|
* @param height Height to fit inside
|
|
|
|
* @param def Default scale
|
|
|
|
* @param min Minimum scale
|
|
|
|
*/
|
|
|
|
GEODE_DLL void limitNodeHeight(cocos2d::CCNode* node, float height, float def, float min);
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Checks if a node is visible (recursively
|
2022-07-30 12:24:03 -04:00
|
|
|
* checks parent visibility)
|
|
|
|
* @param node Node to check if visible
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns True if node is visibile. Does
|
2022-07-30 12:24:03 -04:00
|
|
|
* not take into account if node is off-screen
|
|
|
|
*/
|
|
|
|
GEODE_DLL bool nodeIsVisible(cocos2d::CCNode* node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a node by tag by traversing
|
|
|
|
* children recursively
|
2022-10-30 14:59:20 -04:00
|
|
|
*
|
2022-07-30 12:24:03 -04:00
|
|
|
* @param node Parent node
|
|
|
|
* @param tag Target tag
|
|
|
|
* @return Child node with specified tag, or
|
|
|
|
* null if there is none
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCNode* getChildByTagRecursive(cocos2d::CCNode* node, int tag);
|
|
|
|
|
2022-11-19 00:04:14 -05:00
|
|
|
/**
|
2022-12-04 11:39:40 -05:00
|
|
|
* Get first node that conforms to the predicate
|
2022-11-19 00:04:14 -05:00
|
|
|
* by traversing children recursively
|
2022-12-04 11:39:40 -05:00
|
|
|
*
|
2022-11-19 00:04:14 -05:00
|
|
|
* @param node Parent node
|
|
|
|
* @param predicate Predicate used to evaluate nodes
|
2022-12-04 11:39:40 -05:00
|
|
|
* @return Child node if one is found, or null if
|
2022-11-19 00:04:14 -05:00
|
|
|
* there is none
|
|
|
|
*/
|
|
|
|
template <class Type = cocos2d::CCNode>
|
2023-02-08 10:25:07 -05:00
|
|
|
Type* findFirstChildRecursive(cocos2d::CCNode* node, utils::MiniFunction<bool(Type*)> predicate) {
|
2024-06-06 06:31:22 -04:00
|
|
|
if (cast::typeinfo_cast<Type*>(node) && predicate(static_cast<Type*>(node)))
|
2022-11-19 00:19:23 -05:00
|
|
|
return static_cast<Type*>(node);
|
2022-11-19 00:04:14 -05:00
|
|
|
|
|
|
|
auto children = node->getChildren();
|
|
|
|
if (!children) return nullptr;
|
|
|
|
|
|
|
|
for (int i = 0; i < children->count(); ++i) {
|
2022-11-19 00:15:01 -05:00
|
|
|
auto newParent = static_cast<cocos2d::CCNode*>(children->objectAtIndex(i));
|
2022-11-19 00:04:14 -05:00
|
|
|
auto child = findFirstChildRecursive(newParent, predicate);
|
2022-12-04 11:39:40 -05:00
|
|
|
if (child) return child;
|
2022-11-19 00:04:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-02-16 15:58:40 -05:00
|
|
|
/**
|
|
|
|
* Checks if a node has the given sprite frame
|
|
|
|
* name either in the sprite or in the sprite inside
|
|
|
|
* the button.
|
|
|
|
*
|
|
|
|
* @param node Node to check
|
|
|
|
* @param name Name of the sprite frame to search for
|
|
|
|
* @returns True if the node has the given sprite frame
|
|
|
|
* name
|
|
|
|
*/
|
2024-02-26 09:58:35 -05:00
|
|
|
GEODE_DLL bool isSpriteFrameName(cocos2d::CCNode* node, const char* name);
|
2024-02-16 15:58:40 -05:00
|
|
|
|
2024-02-16 13:28:49 -05:00
|
|
|
/**
|
|
|
|
* Get the first child that has the given sprite frame
|
|
|
|
* name either in the sprite or in the sprite inside
|
|
|
|
* the button.
|
|
|
|
*
|
|
|
|
* @param parent Parent node to search in
|
|
|
|
* @param name Name of the sprite frame to search for
|
|
|
|
* @returns Child with the given sprite frame name, or
|
|
|
|
* nullptr if there is none
|
|
|
|
*/
|
2024-02-26 09:58:35 -05:00
|
|
|
GEODE_DLL cocos2d::CCNode* getChildBySpriteFrameName(cocos2d::CCNode* parent, const char* name);
|
2024-02-16 13:28:49 -05:00
|
|
|
|
2024-04-28 16:44:20 -04:00
|
|
|
/**
|
|
|
|
* Checks if a node has the given sprite name either
|
|
|
|
* in the sprite or in the sprite inside the button.
|
|
|
|
*
|
|
|
|
* @param node Node to check
|
|
|
|
* @param name Name of the sprite to search for
|
|
|
|
* @returns True if the node has the given sprite name
|
|
|
|
*/
|
|
|
|
GEODE_DLL bool isSpriteName(cocos2d::CCNode* node, const char* name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the first child that has the given sprite name
|
|
|
|
* either in the sprite or in the sprite inside the
|
|
|
|
* button.
|
|
|
|
*
|
|
|
|
* @param parent Parent node to search in
|
|
|
|
* @param name Name of the sprite to search for
|
|
|
|
* @returns Child with the given sprite name, or
|
|
|
|
* nullptr if there is none
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCNode* getChildBySpriteName(cocos2d::CCNode* parent, const char* name);
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Checks if a given file exists in CCFileUtils
|
|
|
|
* search paths.
|
2022-07-30 12:24:03 -04:00
|
|
|
* @param filename File to check
|
|
|
|
* @returns True if file exists
|
|
|
|
* @example if (fileExistsInSearchPaths("mySprite.png"_spr)) {
|
|
|
|
* CCSprite::create("mySprite.png"_spr);
|
|
|
|
* } else {
|
|
|
|
* CCSprite::create("fallback.png");
|
|
|
|
* }
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
GEODE_DLL bool fileExistsInSearchPaths(char const* filename);
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
inline void ccDrawColor4B(cocos2d::ccColor4B const& color) {
|
|
|
|
cocos2d::ccDrawColor4B(color.r, color.g, color.b, color.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor4B invert4B(cocos2d::ccColor4B const& color) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {
|
|
|
|
static_cast<GLubyte>(255 - color.r),
|
|
|
|
static_cast<GLubyte>(255 - color.g),
|
|
|
|
static_cast<GLubyte>(255 - color.b),
|
|
|
|
color.a};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor3B invert3B(cocos2d::ccColor3B const& color) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {
|
|
|
|
static_cast<GLubyte>(255 - color.r),
|
|
|
|
static_cast<GLubyte>(255 - color.g),
|
|
|
|
static_cast<GLubyte>(255 - color.b)};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor3B lighten3B(cocos2d::ccColor3B const& color, int amount) {
|
|
|
|
return {
|
|
|
|
static_cast<GLubyte>(utils::clamp(color.r + amount, 0, 255)),
|
|
|
|
static_cast<GLubyte>(utils::clamp(color.g + amount, 0, 255)),
|
|
|
|
static_cast<GLubyte>(utils::clamp(color.b + amount, 0, 255)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor3B darken3B(cocos2d::ccColor3B const& color, int amount) {
|
|
|
|
return lighten3B(color, -amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor3B to3B(cocos2d::ccColor4B const& color) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {color.r, color.g, color.b};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor4B to4B(cocos2d::ccColor3B const& color, GLubyte alpha = 255) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {color.r, color.g, color.b, alpha};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline cocos2d::ccColor4F to4F(cocos2d::ccColor4B const& color) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
2024-07-29 09:18:17 -04:00
|
|
|
[[deprecated("This function may have unintended behavior, use cc3bFromHexString or manually expand the color instead")]]
|
2022-11-28 12:09:39 -05:00
|
|
|
constexpr cocos2d::ccColor3B cc3x(int hexValue) {
|
|
|
|
if (hexValue <= 0xf)
|
2022-12-04 11:39:40 -05:00
|
|
|
return cocos2d::ccColor3B{
|
|
|
|
static_cast<GLubyte>(hexValue * 17),
|
|
|
|
static_cast<GLubyte>(hexValue * 17),
|
|
|
|
static_cast<GLubyte>(hexValue * 17)};
|
2022-11-28 12:09:39 -05:00
|
|
|
if (hexValue <= 0xff)
|
2022-12-04 11:39:40 -05:00
|
|
|
return cocos2d::ccColor3B{
|
|
|
|
static_cast<GLubyte>(hexValue),
|
|
|
|
static_cast<GLubyte>(hexValue),
|
|
|
|
static_cast<GLubyte>(hexValue)};
|
2022-11-28 12:09:39 -05:00
|
|
|
if (hexValue <= 0xfff)
|
2022-12-04 11:39:40 -05:00
|
|
|
return cocos2d::ccColor3B{
|
|
|
|
static_cast<GLubyte>((hexValue >> 8 & 0xf) * 17),
|
|
|
|
static_cast<GLubyte>((hexValue >> 4 & 0xf) * 17),
|
|
|
|
static_cast<GLubyte>((hexValue >> 0 & 0xf) * 17)};
|
2022-11-28 12:09:39 -05:00
|
|
|
else
|
2022-12-04 11:39:40 -05:00
|
|
|
return cocos2d::ccColor3B{
|
|
|
|
static_cast<GLubyte>(hexValue >> 16 & 0xff),
|
|
|
|
static_cast<GLubyte>(hexValue >> 8 & 0xff),
|
|
|
|
static_cast<GLubyte>(hexValue >> 0 & 0xff)};
|
2022-11-28 12:09:39 -05:00
|
|
|
}
|
|
|
|
|
2024-02-10 06:02:31 -05:00
|
|
|
/**
|
2024-02-10 06:36:11 -05:00
|
|
|
* Parse a ccColor3B from a hexadecimal string. The string may contain
|
2024-02-10 06:02:31 -05:00
|
|
|
* a leading '#'
|
|
|
|
* @param hexValue The string to parse into a color
|
|
|
|
* @param permissive If true, strings like "f" are considered valid
|
|
|
|
* representations of the color white. Useful for UIs that allow entering
|
|
|
|
* a hex color. Empty strings evaluate to pure white
|
|
|
|
* @returns A ccColor3B if it could be succesfully parsed, or an error
|
|
|
|
* indicating the failure reason
|
|
|
|
*/
|
|
|
|
GEODE_DLL Result<cocos2d::ccColor3B> cc3bFromHexString(std::string const& hexValue, bool permissive = false);
|
|
|
|
/**
|
2024-02-10 06:36:11 -05:00
|
|
|
* Parse a ccColor4B from a hexadecimal string. The string may contain
|
2024-02-10 06:02:31 -05:00
|
|
|
* a leading '#'
|
|
|
|
* @param hexValue The string to parse into a color
|
|
|
|
* @param requireAlpha Require the alpha component to be passed. If false,
|
|
|
|
* alpha defaults to 255
|
|
|
|
* @param permissive If true, strings like "f" are considered valid
|
|
|
|
* representations of the color white. Useful for UIs that allow entering
|
|
|
|
* a hex color. Empty strings evaluate to pure white
|
|
|
|
* @returns A ccColor4B if it could be succesfully parsed, or an error
|
|
|
|
* indicating the failure reason
|
|
|
|
*/
|
|
|
|
GEODE_DLL Result<cocos2d::ccColor4B> cc4bFromHexString(std::string const& hexValue, bool requireAlpha = false, bool permissive = false);
|
2022-11-28 12:09:39 -05:00
|
|
|
GEODE_DLL std::string cc3bToHexString(cocos2d::ccColor3B const& color);
|
|
|
|
GEODE_DLL std::string cc4bToHexString(cocos2d::ccColor4B const& color);
|
|
|
|
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_pointer_v<T>>>
|
|
|
|
static cocos2d::CCArray* vectorToCCArray(std::vector<T> const& vec) {
|
|
|
|
auto res = cocos2d::CCArray::createWithCapacity(vec.size());
|
|
|
|
for (auto const& item : vec)
|
|
|
|
res->addObject(item);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename C, typename = std::enable_if_t<std::is_pointer_v<C>>>
|
2023-02-08 10:25:07 -05:00
|
|
|
static cocos2d::CCArray* vectorToCCArray(std::vector<T> const& vec, utils::MiniFunction<C(T)> convFunc) {
|
2022-11-28 12:09:39 -05:00
|
|
|
auto res = cocos2d::CCArray::createWithCapacity(vec.size());
|
|
|
|
for (auto const& item : vec)
|
|
|
|
res->addObject(convFunc(item));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_pointer_v<T>>>
|
|
|
|
std::vector<T> ccArrayToVector(cocos2d::CCArray* arr) {
|
|
|
|
return std::vector<T>(
|
2022-12-04 11:39:40 -05:00
|
|
|
reinterpret_cast<T*>(arr->data->arr), reinterpret_cast<T*>(arr->data->arr) + arr->data->num
|
2022-11-28 12:09:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename K, typename V,
|
|
|
|
typename = std::enable_if_t<std::is_same_v<K, std::string> || std::is_same_v<K, intptr_t>>>
|
|
|
|
static cocos2d::CCDictionary* mapToCCDict(std::map<K, V> const& map) {
|
|
|
|
auto res = cocos2d::CCDictionary::create();
|
|
|
|
for (auto const& [key, value] : map)
|
|
|
|
res->setObject(value, key);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename K, typename V, typename C,
|
|
|
|
typename = std::enable_if_t<std::is_same_v<C, std::string> || std::is_same_v<C, intptr_t>>>
|
2023-02-08 10:25:07 -05:00
|
|
|
static cocos2d::CCDictionary* mapToCCDict(std::map<K, V> const& map, utils::MiniFunction<C(K)> convFunc) {
|
2022-11-28 12:09:39 -05:00
|
|
|
auto res = cocos2d::CCDictionary::create();
|
|
|
|
for (auto const& [key, value] : map)
|
|
|
|
res->setObject(value, convFunc(key));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-03-11 09:40:03 -05:00
|
|
|
/**
|
|
|
|
* Gets the mouse position in cocos2d coordinates.
|
|
|
|
* On mobile platforms this will probably return (0, 0)
|
|
|
|
* @returns The mouse position
|
|
|
|
*/
|
|
|
|
GEODE_DLL cocos2d::CCPoint getMousePos();
|
2022-10-13 05:56:23 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
// std specializations
|
2022-10-13 05:56:23 -04:00
|
|
|
namespace std {
|
2022-11-28 12:09:39 -05:00
|
|
|
// enables using Ref as the key in unordered_map etc.
|
2022-12-04 11:39:40 -05:00
|
|
|
template <class T>
|
2022-11-28 12:09:39 -05:00
|
|
|
struct hash<geode::Ref<T>> {
|
|
|
|
size_t operator()(geode::Ref<T> const& ref) const {
|
|
|
|
return std::hash<T*>()(ref.data());
|
|
|
|
}
|
|
|
|
};
|
2024-09-10 14:09:09 -04:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct std::hash<geode::WeakRef<T>> {
|
|
|
|
size_t operator()(geode::WeakRef<T> const& ref) const {
|
2024-10-17 10:23:39 -04:00
|
|
|
// the explicit template argument is needed here because it would otherwise cast to WeakRef and recurse
|
|
|
|
return hash<std::shared_ptr<geode::WeakRefController>>{}(ref.m_controller);
|
2024-09-10 14:09:09 -04:00
|
|
|
}
|
|
|
|
};
|
2022-10-13 05:56:23 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 12:09:39 -05:00
|
|
|
// more utils
|
2022-10-13 05:56:23 -04:00
|
|
|
namespace geode::cocos {
|
2022-10-09 09:17:14 -04:00
|
|
|
struct GEODE_DLL CCArrayInserter {
|
|
|
|
public:
|
|
|
|
CCArrayInserter(cocos2d::CCArray* p) : m_array(p) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-09 09:17:14 -04:00
|
|
|
cocos2d::CCArray* m_array;
|
|
|
|
|
|
|
|
auto& operator=(cocos2d::CCObject* value) {
|
|
|
|
m_array->addObject(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& operator*() {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& operator++() {
|
|
|
|
return *this;
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
};
|
|
|
|
|
2024-01-09 08:47:23 -05:00
|
|
|
template <class T>
|
|
|
|
concept CocosObjectPtr = std::is_pointer_v<T> && std::is_convertible_v<T, cocos2d::CCObject const*>;
|
|
|
|
|
|
|
|
template <class K>
|
|
|
|
concept CocosDictionaryKey = std::same_as<K, int> || std::same_as<K, intptr_t> || std::same_as<K, gd::string> || std::same_as<K, std::string>;
|
|
|
|
|
2024-01-09 00:13:44 -05:00
|
|
|
/**
|
|
|
|
* A templated wrapper over CCArray, providing easy iteration and indexing.
|
|
|
|
* This will keep ownership of the given CCArray*.
|
|
|
|
*
|
|
|
|
* @tparam Type Pointer to a type that inherits CCObject.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* CCArrayExt<GameObject*> objects = PlayLayer::get()->m_objects;
|
|
|
|
* // Easy indexing, giving you the type you assigned
|
|
|
|
* GameObject* myObj = objects[2];
|
|
|
|
*
|
2024-01-09 08:47:23 -05:00
|
|
|
* // Easy iteration using C++ range-based for loops
|
2024-01-09 00:13:44 -05:00
|
|
|
* for (auto* obj : objects) {
|
|
|
|
* log::info("{}", obj->m_objectID);
|
|
|
|
* }
|
|
|
|
*/
|
2024-01-09 08:47:23 -05:00
|
|
|
template <CocosObjectPtr Type>
|
2022-07-30 12:24:03 -04:00
|
|
|
class CCArrayExt {
|
2022-10-09 09:17:14 -04:00
|
|
|
protected:
|
2022-10-14 10:02:53 -04:00
|
|
|
Ref<cocos2d::CCArray> m_arr;
|
2024-01-09 00:13:44 -05:00
|
|
|
using T = std::remove_pointer_t<Type>;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-09 09:17:14 -04:00
|
|
|
public:
|
2024-06-17 23:15:07 -04:00
|
|
|
using value_type = T*;
|
2023-02-14 14:54:45 -05:00
|
|
|
using iterator = T**;
|
|
|
|
using const_iterator = const T**;
|
|
|
|
|
2022-10-14 10:02:53 -04:00
|
|
|
CCArrayExt() : m_arr(cocos2d::CCArray::create()) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-02-14 14:54:45 -05:00
|
|
|
CCArrayExt(cocos2d::CCArray* arr)
|
|
|
|
: m_arr(arr) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-14 10:02:53 -04:00
|
|
|
CCArrayExt(CCArrayExt const& a) : m_arr(a.m_arr) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
CCArrayExt(CCArrayExt&& a) : m_arr(a.m_arr) {
|
|
|
|
a.m_arr = nullptr;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-14 10:02:53 -04:00
|
|
|
~CCArrayExt() {}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2023-02-14 14:54:45 -05:00
|
|
|
T** begin() const {
|
2022-10-26 09:20:39 -04:00
|
|
|
if (!m_arr) {
|
2023-02-14 14:54:45 -05:00
|
|
|
return nullptr;
|
2022-10-26 09:20:39 -04:00
|
|
|
}
|
2023-02-14 14:54:45 -05:00
|
|
|
return reinterpret_cast<T**>(m_arr->data->arr);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-02-14 14:54:45 -05:00
|
|
|
T** end() const {
|
2022-10-26 09:20:39 -04:00
|
|
|
if (!m_arr) {
|
2023-02-14 14:54:45 -05:00
|
|
|
return nullptr;
|
2022-10-26 09:20:39 -04:00
|
|
|
}
|
2023-02-14 14:54:45 -05:00
|
|
|
return reinterpret_cast<T**>(m_arr->data->arr) + m_arr->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rbegin() const {
|
|
|
|
return std::reverse_iterator(this->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rend() const {
|
|
|
|
return std::reverse_iterator(this->begin());
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-12-04 11:39:40 -05:00
|
|
|
|
2022-10-26 09:20:39 -04:00
|
|
|
size_t size() const {
|
|
|
|
return m_arr ? m_arr->count() : 0;
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-09-18 03:09:17 -04:00
|
|
|
T* operator[](size_t index) {
|
2022-10-22 13:19:58 -04:00
|
|
|
return static_cast<T*>(m_arr->objectAtIndex(index));
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
void push_back(T* item) {
|
|
|
|
m_arr->addObject(item);
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
T* pop_back() {
|
2022-07-30 12:24:03 -04:00
|
|
|
T ret = m_arr->lastObject();
|
|
|
|
m_arr->removeLastObject();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
cocos2d::CCArray* inner() {
|
|
|
|
return m_arr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename K, typename T>
|
|
|
|
struct CCDictIterator {
|
|
|
|
public:
|
|
|
|
CCDictIterator(cocos2d::CCDictElement* p) : m_ptr(p) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
cocos2d::CCDictElement* m_ptr;
|
|
|
|
|
|
|
|
std::pair<K, T> operator*() {
|
2024-01-09 00:13:44 -05:00
|
|
|
if constexpr (std::is_same_v<K, std::string> || std::is_same_v<K, gd::string>) {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {m_ptr->getStrKey(), static_cast<T>(m_ptr->getObject())};
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
else {
|
2022-12-04 11:39:40 -05:00
|
|
|
return {m_ptr->getIntKey(), static_cast<T>(m_ptr->getObject())};
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& operator++() {
|
2022-10-22 13:19:58 -04:00
|
|
|
m_ptr = static_cast<decltype(m_ptr)>(m_ptr->hh.next);
|
2022-07-30 12:24:03 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
friend bool operator==(CCDictIterator<K, T> const& a, CCDictIterator<K, T> const& b) {
|
|
|
|
return a.m_ptr == b.m_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend bool operator!=(CCDictIterator<K, T> const& a, CCDictIterator<K, T> const& b) {
|
|
|
|
return a.m_ptr != b.m_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator!=(int b) {
|
|
|
|
return m_ptr != nullptr;
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename K, typename T>
|
|
|
|
struct CCDictEntry {
|
|
|
|
K m_key;
|
|
|
|
cocos2d::CCDictionary* m_dict;
|
|
|
|
|
|
|
|
CCDictEntry(K key, cocos2d::CCDictionary* dict) : m_key(key), m_dict(dict) {}
|
|
|
|
|
|
|
|
T operator->() {
|
2022-10-22 13:19:58 -04:00
|
|
|
return static_cast<T>(m_dict->objectForKey(m_key));
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
operator T() {
|
2022-10-22 13:19:58 -04:00
|
|
|
return static_cast<T>(m_dict->objectForKey(m_key));
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CCDictEntry& operator=(T f) {
|
|
|
|
m_dict->setObject(f, m_key);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-09 00:13:44 -05:00
|
|
|
/**
|
|
|
|
* A templated wrapper over CCDictionary, providing easy iteration and indexing.
|
|
|
|
* This will keep ownership of the given CCDictionary*.
|
|
|
|
*
|
|
|
|
* @tparam Key Type of the key. MUST only be int or gd::string or std::string.
|
|
|
|
* @tparam ValuePtr Pointer to a type that inherits CCObject.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* CCDictionaryExt<std::string, GJGameLevel*> levels = getSomeDict();
|
|
|
|
* // Easy indexing, giving you the type you assigned
|
|
|
|
* GJGameLevel* myLvl = levels["Cube Adventures"];
|
|
|
|
*
|
2024-01-09 08:47:23 -05:00
|
|
|
* // Easy iteration using C++ range-based for loops
|
2024-01-09 00:13:44 -05:00
|
|
|
* for (auto [name, level] : levels) {
|
|
|
|
* log::info("{}: {}", name, level->m_levelID);
|
|
|
|
* }
|
|
|
|
*/
|
2024-01-09 08:47:23 -05:00
|
|
|
template <CocosDictionaryKey Key, CocosObjectPtr ValuePtr>
|
2022-07-30 12:24:03 -04:00
|
|
|
struct CCDictionaryExt {
|
2022-10-30 14:59:20 -04:00
|
|
|
protected:
|
2024-01-09 00:13:44 -05:00
|
|
|
Ref<cocos2d::CCDictionary> m_dict;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
public:
|
2024-01-09 00:13:44 -05:00
|
|
|
CCDictionaryExt() : m_dict(cocos2d::CCDictionary::create()) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2024-01-09 00:13:44 -05:00
|
|
|
CCDictionaryExt(cocos2d::CCDictionary* dict) : m_dict(dict) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2024-01-09 00:13:44 -05:00
|
|
|
CCDictionaryExt(CCDictionaryExt const& d) : m_dict(d.m_dict) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
CCDictionaryExt(CCDictionaryExt&& d) : m_dict(d.m_dict) {
|
|
|
|
d.m_dict = nullptr;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
auto begin() {
|
2024-01-09 00:13:44 -05:00
|
|
|
return CCDictIterator<Key, ValuePtr>(m_dict->m_pElements);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
// do not use this
|
|
|
|
auto end() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
size_t size() {
|
|
|
|
return m_dict->count();
|
|
|
|
}
|
|
|
|
|
2024-01-09 00:13:44 -05:00
|
|
|
auto operator[](const Key& key) {
|
|
|
|
auto ret = static_cast<ValuePtr>(m_dict->objectForKey(key));
|
2024-01-09 07:41:22 -05:00
|
|
|
if (!ret) m_dict->setObject(cocos2d::CCNode::create(), key);
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2024-01-09 00:13:44 -05:00
|
|
|
return CCDictEntry<Key, ValuePtr>(key, m_dict);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2024-01-09 00:20:10 -05:00
|
|
|
bool contains(const Key& key) {
|
|
|
|
return m_dict->objectForKey(key) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count(const Key& key) {
|
|
|
|
return this->contains(key) ? 1 : 0;
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2024-01-09 08:47:23 -05:00
|
|
|
|
|
|
|
cocos2d::CCDictionary* inner() {
|
|
|
|
return m_dict;
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
};
|
2024-01-24 03:30:38 -05:00
|
|
|
|
2024-06-01 08:38:21 -04:00
|
|
|
struct CCMenuItemExt {
|
|
|
|
private:
|
|
|
|
template <class Node>
|
|
|
|
class LambdaCallback : public cocos2d::CCObject {
|
|
|
|
public:
|
|
|
|
utils::MiniFunction<void(Node*)> m_callback;
|
|
|
|
|
|
|
|
static LambdaCallback* create(utils::MiniFunction<void(Node*)>&& callback) {
|
|
|
|
auto ret = new (std::nothrow) LambdaCallback();
|
2024-06-23 05:36:16 -04:00
|
|
|
if (ret->init(std::forward<std::remove_reference_t<decltype(callback)>>(callback))) {
|
2024-06-01 08:38:21 -04:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(utils::MiniFunction<void(Node*)>&& callback) {
|
|
|
|
m_callback = std::forward<std::remove_reference_t<decltype(callback)>>(callback);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute(cocos2d::CCNode* node) {
|
|
|
|
m_callback(static_cast<Node*>(node));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
static cocos2d::CCMenuItem* create(
|
|
|
|
utils::MiniFunction<void(cocos2d::CCMenuItem*)>&& callback
|
|
|
|
) {
|
|
|
|
auto item = cocos2d::CCMenuItem::create();
|
|
|
|
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cocos2d::CCMenuItemSprite* createSprite(
|
|
|
|
cocos2d::CCNode* normalSprite,
|
|
|
|
cocos2d::CCNode* selectedSprite,
|
|
|
|
utils::MiniFunction<void(cocos2d::CCMenuItemSprite*)>&& callback
|
|
|
|
) {
|
|
|
|
auto item = cocos2d::CCMenuItemSprite::create(normalSprite, selectedSprite);
|
|
|
|
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cocos2d::CCMenuItemSprite* createSprite(
|
|
|
|
cocos2d::CCNode* normalSprite,
|
|
|
|
cocos2d::CCNode* selectedSprite,
|
|
|
|
cocos2d::CCNode* disabledSprite,
|
|
|
|
utils::MiniFunction<void(cocos2d::CCMenuItemSprite*)>&& callback
|
|
|
|
) {
|
|
|
|
auto item = cocos2d::CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite);
|
|
|
|
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemSpriteExtra* createSpriteExtra(
|
|
|
|
cocos2d::CCNode* normalSprite,
|
|
|
|
utils::MiniFunction<void(CCMenuItemSpriteExtra*)>&& callback
|
|
|
|
) {
|
|
|
|
auto item = CCMenuItemSpriteExtra::create(normalSprite, nullptr, nullptr);
|
|
|
|
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemSpriteExtra* createSpriteExtraWithFilename(
|
|
|
|
std::string_view normalSpriteName,
|
|
|
|
float scale,
|
|
|
|
utils::MiniFunction<void(CCMenuItemSpriteExtra*)>&& callback
|
|
|
|
) {
|
|
|
|
auto sprite = cocos2d::CCSprite::create(normalSpriteName.data());
|
|
|
|
sprite->setScale(scale);
|
|
|
|
|
|
|
|
return createSpriteExtra(sprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemSpriteExtra* createSpriteExtraWithFrameName(
|
|
|
|
std::string_view normalSpriteName,
|
|
|
|
float scale,
|
|
|
|
utils::MiniFunction<void(CCMenuItemSpriteExtra*)>&& callback
|
|
|
|
) {
|
|
|
|
auto sprite = cocos2d::CCSprite::createWithSpriteFrameName(normalSpriteName.data());
|
|
|
|
sprite->setScale(scale);
|
|
|
|
|
|
|
|
return createSpriteExtra(sprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemToggler* createToggler(
|
|
|
|
cocos2d::CCNode* onSprite,
|
|
|
|
cocos2d::CCNode* offSprite,
|
|
|
|
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
|
|
|
|
) {
|
2024-08-01 08:05:33 -04:00
|
|
|
auto item = CCMenuItemToggler::create(offSprite, onSprite, nullptr, nullptr);
|
2024-06-01 08:38:21 -04:00
|
|
|
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemToggler* createTogglerWithStandardSprites(
|
|
|
|
float scale,
|
|
|
|
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
|
|
|
|
) {
|
|
|
|
auto offSprite = cocos2d::CCSprite::createWithSpriteFrameName("GJ_checkOff_001.png");
|
|
|
|
auto onSprite = cocos2d::CCSprite::createWithSpriteFrameName("GJ_checkOn_001.png");
|
|
|
|
|
|
|
|
offSprite->setScale(scale);
|
|
|
|
onSprite->setScale(scale);
|
|
|
|
|
2024-08-01 08:14:50 -04:00
|
|
|
return createToggler(onSprite, offSprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
2024-06-01 08:38:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemToggler* createTogglerWithFilename(
|
|
|
|
std::string_view onSpriteName,
|
|
|
|
std::string_view offSpriteName,
|
|
|
|
float scale,
|
|
|
|
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
|
|
|
|
) {
|
|
|
|
auto offSprite = cocos2d::CCSprite::create(offSpriteName.data());
|
|
|
|
auto onSprite = cocos2d::CCSprite::create(onSpriteName.data());
|
|
|
|
|
|
|
|
offSprite->setScale(scale);
|
|
|
|
onSprite->setScale(scale);
|
|
|
|
|
2024-08-01 08:14:50 -04:00
|
|
|
return createToggler(onSprite, offSprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
2024-06-01 08:38:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static CCMenuItemToggler* createTogglerWithFrameName(
|
|
|
|
std::string_view onSpriteName,
|
|
|
|
std::string_view offSpriteName,
|
|
|
|
float scale,
|
|
|
|
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
|
|
|
|
) {
|
|
|
|
auto offSprite = cocos2d::CCSprite::createWithSpriteFrameName(offSpriteName.data());
|
|
|
|
auto onSprite = cocos2d::CCSprite::createWithSpriteFrameName(onSpriteName.data());
|
|
|
|
|
|
|
|
offSprite->setScale(scale);
|
|
|
|
onSprite->setScale(scale);
|
|
|
|
|
2024-08-01 08:14:50 -04:00
|
|
|
return createToggler(onSprite, offSprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
2024-06-01 08:38:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Node>
|
|
|
|
static void assignCallback(
|
|
|
|
cocos2d::CCMenuItem* item,
|
|
|
|
utils::MiniFunction<void(Node*)>&& callback
|
|
|
|
) {
|
|
|
|
auto lambda = LambdaCallback<Node>::create(std::forward<std::remove_reference_t<decltype(callback)>>(callback));
|
|
|
|
item->setTarget(lambda, menu_selector(LambdaCallback<Node>::execute));
|
|
|
|
item->setUserObject("lambda-callback", lambda);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-19 15:29:24 -04:00
|
|
|
// CCCallFunc alternative that accepts a lambda (or any function object)
|
|
|
|
template <std::invocable F>
|
|
|
|
class CallFuncExtImpl : public cocos2d::CCActionInstant {
|
|
|
|
public:
|
|
|
|
static CallFuncExtImpl* create(const F& func) {
|
|
|
|
auto ret = new CallFuncExtImpl;
|
|
|
|
ret->m_func = func;
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CallFuncExtImpl* create(F&& func) {
|
|
|
|
auto ret = new CallFuncExtImpl;
|
|
|
|
ret->m_func = std::move(func);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F m_func;
|
|
|
|
|
|
|
|
void update(float) override {
|
|
|
|
if (m_func) this->m_func();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// small hack to allow template deduction
|
|
|
|
struct CallFuncExt {
|
|
|
|
template <std::invocable F>
|
2024-10-21 03:35:34 -04:00
|
|
|
static auto create(F&& func) {
|
|
|
|
using Fd = std::decay_t<F>;
|
|
|
|
|
|
|
|
return CallFuncExtImpl<Fd>::create(std::forward<F>(func));
|
2024-10-19 15:29:24 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-24 05:39:07 -05:00
|
|
|
void GEODE_DLL handleTouchPriorityWith(cocos2d::CCNode* node, int priority, bool force = false);
|
|
|
|
void GEODE_DLL handleTouchPriority(cocos2d::CCNode* node, bool force = false);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|