Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions loader/include/Geode/cocos/base_nodes/CCNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <Geode/utils/casts.hpp>
#include <Geode/utils/function.hpp>
#include <Geode/utils/ZStringView.hpp>
#include <Geode/ui/NodeEvent.hpp>

namespace geode {
class Layout;
Expand Down Expand Up @@ -1152,6 +1153,46 @@ class CC_DLL CCNode : public CCObject
GEODE_DLL geode::comm::ListenerHandle* getEventListener(std::string_view id);
GEODE_DLL size_t getEventListenerCount();

template <class Callback>
geode::comm::ListenerHandle* addOnEnterCallback(
Callback&& callback,
int priority = 0
) {
return this->addEventListener("", geode::NodeEvent(this, geode::NodeEventType::OnEnter), std::forward<Callback>(callback), priority);
}

template <class Callback>
geode::comm::ListenerHandle* addOnEnterTransitionDidFinishCallback(
Callback&& callback,
int priority = 0
) {
return this->addEventListener("", geode::NodeEvent(this, geode::NodeEventType::OnEnterTransitionDidFinish), std::forward<Callback>(callback), priority);
}

template <class Callback>
geode::comm::ListenerHandle* addOnExitCallback(
Callback&& callback,
int priority = 0
) {
return this->addEventListener("", geode::NodeEvent(this, geode::NodeEventType::OnExit), std::forward<Callback>(callback), priority);
}

template <class Callback>
geode::comm::ListenerHandle* addOnExitTransitionDidStartCallback(
Callback&& callback,
int priority = 0
) {
return this->addEventListener("", geode::NodeEvent(this, geode::NodeEventType::OnExitTransitionDidStart), std::forward<Callback>(callback), priority);
}

template <class Callback>
geode::comm::ListenerHandle* addCleanupCallback(
Callback&& callback,
int priority = 0
) {
return this->addEventListener("", geode::NodeEvent(this, geode::NodeEventType::OnCleanup), std::forward<Callback>(callback), priority);
}

/**
* Get child at index. Checks bounds. A negative
* index will get the child starting from the end
Expand Down
9 changes: 9 additions & 0 deletions loader/include/Geode/cocos/menu_nodes/CCMenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ THE SOFTWARE.
#include "../base_nodes/CCNode.h"
#include "../include/CCProtocols.h"
#include "../cocoa/CCArray.h"
#include <Geode/ui/NodeEvent.hpp>

NS_CC_BEGIN

Expand Down Expand Up @@ -112,6 +113,14 @@ class CC_DLL CCMenuItem : public CCNodeRGBA
/** set the target/selector of the menu item*/
void setTarget(CCObject *rec, SEL_MenuHandler selector);

template <class Callback>
geode::comm::ListenerHandle* addActivateCallback(
Callback&& callback,
int priority = 0
) {
return this->addEventListener("", geode::MenuItemActivatedEvent(this), std::forward<Callback>(callback), priority);
}

public:
CCObject* m_pListener;
SEL_MenuHandler m_pfnSelector;
Expand Down
35 changes: 35 additions & 0 deletions loader/include/Geode/ui/NodeEvent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "../loader/Event.hpp"

namespace geode {

enum class NodeEventType {
/// Fired when a CCNode is added to the scene tree.
OnEnter,

/// Fired when a CCNode is removed from the scene tree.
OnExit,

/// Fired when a CCNode is already added to the scene tree but after a scene transition.
OnEnterTransitionDidFinish,

/// Fired when a CCNode is about to be removed from the scene tree before a scene transition.
OnExitTransitionDidStart,

/// Fired when a CCNode is cleaned up (Schedules removed and Actions stopped).
OnCleanup,
};

/// Fired when a CCNode has a specific Event in NodeEventType.
class NodeEvent : public GlobalEvent<NodeEvent, bool(), cocos2d::CCNode*, NodeEventType> {
public:
using GlobalEvent::GlobalEvent;
};

/// Fired when a CCMenuItem is Activated (after the callback).
class MenuItemActivatedEvent : public Event<MenuItemActivatedEvent, bool(cocos2d::CCMenuItem*), cocos2d::CCMenuItem*> {
public:
using Event::Event;
};
}
45 changes: 45 additions & 0 deletions loader/src/loader/ScriptEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <Geode/ui/NodeEvent.hpp>

using namespace geode::prelude;

namespace geode {
class ScriptEngine : public CCScriptEngineProtocol {

ccScriptType getScriptType() {
// Javascript engine is called without needing to change m_nLuaID
return cocos2d::ccScriptType::kScriptTypeJavascript;
}

int executeNodeEvent(CCNode* node, int action) {
Comment thread
altalk23 marked this conversation as resolved.
NodeEvent(node, static_cast<NodeEventType>(action)).send();
return -1;
}

int executeMenuItemEvent(CCMenuItem* menuItem) {
MenuItemActivatedEvent(menuItem).send(menuItem);
return -1;
}

void removeScriptObjectByCCObject(CCObject* object) {}
void removeScriptHandler(int handler) {}
int reallocateScriptHandler(int handler) { return -1; }
int executeString(const char* codes) { return -1; }
int executeScriptFile(const char* filename) { return -1; }
int executeGlobalFunction(const char* functionName) { return -1; }
int executeNotificationEvent(CCNotificationCenter* notificationCenter, const char* name) { return -1; }
int executeCallFuncActionEvent(CCCallFunc* action, CCObject* target) { return -1; }
int executeSchedule(int handler, float dt, CCNode* node) { return -1; }
int executeLayerTouchesEvent(CCLayer* layer, int eventType, CCSet* touches) { return -1; }
int executeLayerTouchEvent(CCLayer* layer, int eventType, CCTouch* touch) { return -1; }
int executeLayerKeypadEvent(CCLayer* layer, int eventType) { return -1; }
int executeAccelerometerEvent(CCLayer* layer, CCAcceleration* accelerationValue) { return -1; }
int executeEvent(int handler, const char* eventName, CCObject* eventSource, const char* eventSourceClassName) { return -1; }
int executeEventWithArgs(int handler, CCArray* args) { return -1; }
bool handleAssert(const char* msg) { return false; }
bool parseConfig(ConfigType type, const gd::string& str) { return false; }
};
}

$on_mod(Loaded) {
CCScriptEngineManager::sharedManager()->setScriptEngine(new geode::ScriptEngine());
}
Loading