11#pragma once
22
33#include < Geode/Geode.hpp>
4- #include < alphalaneous.alphas_geode_utils/include/NodeModding.h >
4+ #include < Geode/modify/CCNode.hpp >
55#include " HPTParser.hpp"
6+ #include " TouchObject.hpp"
67
78using namespace geode ::prelude;
89
@@ -20,168 +21,7 @@ struct UISchedule {
2021 }
2122};
2223
23- class HPTCCNode ;
24-
25- struct TouchObject : public CCNode , CCTouchDelegate {
26- CCNode* m_self;
27- bool m_clicked;
28- bool m_hovering;
29- CCNode* m_parentLayer = nullptr ;
30-
31- std::vector<std::shared_ptr<HPTNode>> m_onClick;
32- std::vector<std::shared_ptr<HPTNode>> m_onRelease;
33- std::vector<std::shared_ptr<HPTNode>> m_onActivate;
34- std::vector<std::shared_ptr<HPTNode>> m_onHover;
35- std::vector<std::shared_ptr<HPTNode>> m_onExit;
36-
37- static TouchObject* create (CCNode* self) {
38- auto ret = new TouchObject ();
39- if (ret->init (self)) {
40- ret->autorelease ();
41- return ret;
42- }
43- delete ret;
44- return nullptr ;
45- }
46-
47- CCNode* getSceneChildContainingNode () {
48- if (m_parentLayer) {
49- if (CCScene::get () != m_parentLayer->getParent ()) return nullptr ;
50- return m_parentLayer;
51- }
52-
53- auto current = m_self;
54- while (current && current->getParent () != CCScene::get ()) {
55- current = current->getParent ();
56- }
57- m_parentLayer = current;
58- if (m_parentLayer && CCScene::get () != m_parentLayer->getParent ()) return nullptr ;
59- return current;
60- }
61-
62- bool isLastAlert () {
63- bool shouldCheck = false ;
64- bool lastAlert = false ;
65-
66- if (auto child = getSceneChildContainingNode ()) {
67- if (!child) return false ;
68- for (auto c : CCArrayExt<CCNode*>(child->getChildren ())) {
69- if (!c) continue ;
70- if (!AlphaUtils::Cocos::hasNode (m_self, c)) {
71- shouldCheck = true ;
72- }
73- if (shouldCheck) {
74- if (typeinfo_cast<FLAlertLayer*>(c) || typeinfo_cast<CCBlockLayer*>(c)) {
75- if (AlphaUtils::Cocos::hasNode (m_self, c)) continue ;
76- lastAlert = true ;
77- }
78- }
79- }
80- }
81- return lastAlert;
82- }
83-
84- bool isHoverable (CCNode* node) {
85- if (!CCScene::get () || !node || isLastAlert ()) return false ;
86- auto worldPos = node->convertToWorldSpaceAR (CCPointZero);
87-
88- auto sceneChild = getSceneChildContainingNode ();
89- if (!sceneChild) return false ;
90-
91- for (auto child : CCArrayExt<CCNode*>(CCScene::get ()->getChildren ())) {
92- if (child->getZOrder () <= sceneChild->getZOrder ()) continue ;
93- if (child->boundingBox ().containsPoint (worldPos) && nodeIsVisible (child)) {
94- return false ;
95- }
96- }
97- return true ;
98- }
99-
100- void checkMouse (float ) {
101- if (!nodeIsVisible (m_self)) return ;
102- if (!m_self->getParent ()) return ;
103-
104- auto nodeMouse = m_self->getParent ()->convertToNodeSpace (getMousePos ());
105-
106- bool isValid = m_self->boundingBox ().containsPoint (nodeMouse) && isHoverable (m_self);
107-
108- checkTouch (!isValid);
109- }
110-
111- void checkTouch (bool shouldExit) {
112- if (!shouldExit && !m_hovering) {
113- m_hovering = true ;
114- parseForEach (m_onHover);
115- }
116- if (shouldExit && m_hovering) {
117- m_hovering = false ;
118- parseForEach (m_onExit);
119- }
120- }
121-
122- bool init (CCNode* self) {
123- m_self = self;
124- schedule (schedule_selector (TouchObject::checkMouse));
125- return true ;
126- }
127-
128- void parseForEach (std::vector<std::shared_ptr<HPTNode>> vec) {
129- for (const auto & v : vec) {
130- v->reparse ();
131- }
132- }
133-
134- bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent) override {
135- if (m_self->boundingBox ().containsPoint (pTouch->getLocation ()) && getSceneChildContainingNode ()) {
136- parseForEach (m_onClick);
137- m_clicked = true ;
138- return true ;
139- }
140- return false ;
141- }
142-
143- void ccTouchMoved (CCTouch *pTouch, CCEvent *pEvent) override {
144- if (m_self->boundingBox ().containsPoint (pTouch->getLocation ()) && getSceneChildContainingNode ()) {
145- if (!m_clicked) {
146- m_clicked = true ;
147- parseForEach (m_onClick);
148- }
149- }
150- else {
151- if (m_clicked) {
152- m_clicked = false ;
153- parseForEach (m_onRelease);
154- }
155- }
156- }
157-
158- void ccTouchEnded (CCTouch *pTouch, CCEvent *pEvent) override {
159- m_clicked = false ;
160- parseForEach (m_onActivate);
161- parseForEach (m_onRelease);
162- }
163-
164- void ccTouchCancelled (CCTouch *pTouch, CCEvent *pEvent) override {
165- m_clicked = false ;
166- parseForEach (m_onRelease);
167- }
168-
169- void onEnter () override {
170- CCNode::onEnter ();
171- CCDirector::get ()->getTouchDispatcher ()->addTargetedDelegate (this , 0 , true );
172- }
173-
174- void onExit () override {
175- CCNode::onExit ();
176- CCDirector::get ()->getTouchDispatcher ()->removeDelegate (this );
177- }
178-
179- ~TouchObject () {
180- unscheduleAllSelectors ();
181- }
182- };
183-
184- class $nodeModify(HPTCCNode, CCNode) {
24+ class $modify(HPTCCNode, CCNode) {
18525
18626 struct Fields {
18727 TouchObject* m_touchObject;
@@ -203,8 +43,6 @@ class $nodeModify(HPTCCNode, CCNode) {
20343 }
20444 }
20545
206- void modify () {}
207-
20846 void setOwner (std::shared_ptr<HPTNode> node) {
20947 m_fields->m_owned .push_back (node);
21048 }
@@ -250,6 +88,27 @@ class $nodeModify(HPTCCNode, CCNode) {
25088 erase (fields->m_touchObject ->m_onHover );
25189 erase (fields->m_touchObject ->m_onExit );
25290 }
91+
92+ // resetByPackJson(packName);
93+ }
94+
95+ void resetByPackJson (const std::string& packName) {
96+ auto fields = m_fields.self ();
97+
98+ auto erase = [&packName] (std::vector<matjson::Value>& vec) {
99+ vec.erase (
100+ std::remove_if (vec.begin (), vec.end (), [&packName](matjson::Value value){ return value[" _pack-name" ] == packName; }),
101+ vec.end ()
102+ );
103+ };
104+
105+ if (fields->m_touchObject ) {
106+ erase (fields->m_touchObject ->m_onClickJson );
107+ erase (fields->m_touchObject ->m_onReleaseJson );
108+ erase (fields->m_touchObject ->m_onActivateJson );
109+ erase (fields->m_touchObject ->m_onHoverJson );
110+ erase (fields->m_touchObject ->m_onExitJson );
111+ }
253112 }
254113
255114 void setSchedule (std::shared_ptr<HPTNode> node) {
@@ -295,5 +154,67 @@ class $nodeModify(HPTCCNode, CCNode) {
295154 enableTouch ();
296155 fields->m_touchObject ->m_onExit .push_back (node);
297156 }
157+
158+ void setOnClick (matjson::Value value) {
159+ auto fields = m_fields.self ();
160+ enableTouch ();
161+ fields->m_touchObject ->m_onClickJson .push_back (value);
162+ auto & override = fields->m_touchObject ->m_cancelOriginalClick ;
163+
164+ if (!override ) {
165+ if (value.contains (" override" )) {
166+ matjson::Value overrideVal = value[" override" ];
167+ if (overrideVal.isBool ()) {
168+ override = overrideVal.asBool ().unwrapOr (false );
169+ }
170+ }
171+ }
172+ }
173+
174+ void setOnRelease (matjson::Value value) {
175+ auto fields = m_fields.self ();
176+ enableTouch ();
177+ fields->m_touchObject ->m_onReleaseJson .push_back (value);
178+
179+ auto & override = fields->m_touchObject ->m_cancelOriginalRelease ;
180+
181+ if (!override ) {
182+ if (value.contains (" override" )) {
183+ matjson::Value overrideVal = value[" override" ];
184+ if (overrideVal.isBool ()) {
185+ override = overrideVal.asBool ().unwrapOr (false );
186+ }
187+ }
188+ }
189+ }
190+
191+ void setOnActivate (matjson::Value value) {
192+ auto fields = m_fields.self ();
193+ enableTouch ();
194+ fields->m_touchObject ->m_onActivateJson .push_back (value);
195+
196+ auto & override = fields->m_touchObject ->m_cancelOriginalActivate ;
197+
198+ if (!override ) {
199+ if (value.contains (" override" )) {
200+ matjson::Value overrideVal = value[" override" ];
201+ if (overrideVal.isBool ()) {
202+ override = overrideVal.asBool ().unwrapOr (false );
203+ }
204+ }
205+ }
206+ }
207+
208+ void setOnHover (matjson::Value value) {
209+ auto fields = m_fields.self ();
210+ enableTouch ();
211+ fields->m_touchObject ->m_onHoverJson .push_back (value);
212+ }
213+
214+ void setOnExit (matjson::Value value) {
215+ auto fields = m_fields.self ();
216+ enableTouch ();
217+ fields->m_touchObject ->m_onExitJson .push_back (value);
218+ }
298219};
299220
0 commit comments