-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1598494
commit 719ce2f
Showing
7 changed files
with
296 additions
and
34 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "GrabNodeLayer.hpp" | ||
|
||
bool GrabNodeLayer::init() | ||
{ | ||
if (!CCLayer::init()) | ||
return false; | ||
|
||
this->ignoreAnchorPointForPosition(false); | ||
this->setPosition(CCPointZero); | ||
this->setContentSize(CCPointZero); | ||
this->setAnchorPoint(ccp(0.5f, 0.5f)); | ||
|
||
this->setTouchEnabled(true); | ||
|
||
return true; | ||
} | ||
|
||
CCPoint GrabNodeLayer::getAxisForLocked(LockedAxis axis) | ||
{ | ||
switch (axis) | ||
{ | ||
case LockedAxis::Horizontal: | ||
return ccp(1, 0); | ||
|
||
case LockedAxis::Vertical: | ||
return ccp(0, 1); | ||
|
||
default: | ||
return ccp(1, 1); | ||
} | ||
} | ||
|
||
void GrabNodeLayer::setLockedAxis(LockedAxis axis) | ||
{ | ||
this->axis = axis; | ||
} | ||
|
||
LockedAxis GrabNodeLayer::getLockedAxis() | ||
{ | ||
return axis; | ||
} | ||
|
||
void GrabNodeLayer::setNodeToGrab(CCNode* node) | ||
{ | ||
nodeToGrab = node; | ||
} | ||
|
||
CCNode* GrabNodeLayer::getNodeToGrab() | ||
{ | ||
return nodeToGrab; | ||
} | ||
|
||
void GrabNodeLayer::setOnStartDrag(std::function<void()> callback) | ||
{ | ||
this->onStartDrag = callback; | ||
} | ||
|
||
void GrabNodeLayer::setOnEndDrag(std::function<void()> callback) | ||
{ | ||
this->onEndDrag = callback; | ||
} | ||
|
||
void GrabNodeLayer::registerWithTouchDispatcher(void) | ||
{ | ||
CCTouchDispatcher::get()->addTargetedDelegate(this, INT_MIN + 1, true); | ||
} | ||
|
||
bool GrabNodeLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) | ||
{ | ||
if (nodeToGrab && cocos::nodeIsVisible(this)) | ||
{ | ||
auto bbox = CCRect(convertToWorldSpace(CCPointZero), convertToWorldSpace(getScaledContentSize()) - convertToWorldSpace(CCPointZero)); | ||
|
||
if (pTouch->getLocation() > bbox.origin && pTouch->getLocation() < bbox.origin + bbox.size) | ||
{ | ||
isDragging = true; | ||
startPos = nodeToGrab->getPosition(); | ||
|
||
if (onStartDrag) | ||
onStartDrag(); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void GrabNodeLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) | ||
{ | ||
if (isDragging) | ||
{ | ||
nodeToGrab->setPosition(startPos + (pTouch->getLocation() - pTouch->getStartLocation()) * getAxisForLocked(axis)); | ||
} | ||
} | ||
|
||
void GrabNodeLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) | ||
{ | ||
isDragging = false; | ||
|
||
if (onEndDrag) | ||
onEndDrag(); | ||
} | ||
|
||
void GrabNodeLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) | ||
{ | ||
ccTouchEnded(pTouch, pEvent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include <Geode/Geode.hpp> | ||
|
||
using namespace geode::prelude; | ||
|
||
enum class LockedAxis | ||
{ | ||
Any, | ||
Horizontal, | ||
Vertical, | ||
}; | ||
|
||
class GrabNodeLayer : public CCLayer | ||
{ | ||
private: | ||
bool isDragging = false; | ||
CCNode* nodeToGrab = nullptr; | ||
CCPoint startPos = CCPointZero; | ||
std::function<void()> onStartDrag = nullptr; | ||
std::function<void()> onEndDrag = nullptr; | ||
LockedAxis axis = LockedAxis::Any; | ||
|
||
CCPoint getAxisForLocked(LockedAxis axis); | ||
|
||
public: | ||
virtual bool init(); | ||
|
||
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); | ||
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); | ||
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); | ||
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); | ||
|
||
virtual void registerWithTouchDispatcher(void); | ||
|
||
void setNodeToGrab(CCNode* node); | ||
CCNode* getNodeToGrab(); | ||
|
||
void setLockedAxis(LockedAxis axis); | ||
LockedAxis getLockedAxis(); | ||
|
||
void setOnStartDrag(std::function<void()> callback); | ||
void setOnEndDrag(std::function<void()> callback); | ||
|
||
CREATE_FUNC(GrabNodeLayer); | ||
}; |
Oops, something went wrong.