44using namespace geode ::prelude;
55
66bool DragButton::init (CCNode* node, std::function<void ()> onPress) {
7- if (!CCLayer::init ()) return false ;
7+ if (!CCLayer::init ()) return false ;
88 this ->setTouchEnabled (true );
9- this ->setAnchorPoint ({.5f , .5f });
9+ this ->setAnchorPoint ({.5f , .5f });
1010 this ->ignoreAnchorPointForPosition (false );
11- this ->scheduleUpdate ();
12- if (node) {
13- this ->setContentSize (node->getScaledContentSize ());
14- this ->addChildAtPosition (node, Anchor::Center, CCPoint{0 , 0 });
15- }
16- this ->m_onPress = onPress;
17- return true ;
11+ this ->scheduleUpdate ();
12+ if (node) {
13+ this ->setContentSize (node->getScaledContentSize ());
14+ this ->addChildAtPosition (node, Anchor::Center, CCPoint{0 , 0 });
15+ }
16+ this ->m_onPress = onPress;
17+ return true ;
1818}
1919
2020bool DragButton::ccTouchBegan (CCTouch* touch, CCEvent* event) {
21- if (!this ->m_bVisible ) return false ;
22- CCPoint local = convertToNodeSpace (touch->getLocation ());
23- if (this ->getContentWidth () > local.x && local.x > 0 && this ->getContentHeight () > local.y && local.y > 0 ) {
21+ if (!this ->m_bVisible ) return false ;
22+ CCPoint local = convertToNodeSpace (touch->getLocation ());
23+ if (this ->getContentWidth () > local.x && local.x > 0 && this ->getContentHeight () > local.y && local.y > 0 ) {
2424 this ->stopActionByTag (123 );
2525 auto action = CCEaseSineOut::create (CCScaleTo::create (.3f , .8f ));
2626 action->setTag (123 );
2727 this ->runAction (action);
2828
29- m_diff = this ->getParent ()->convertToNodeSpace (touch->getLocation ()) - this ->getPosition ();
30- return true ;
31- }
32- return false ;
29+ m_diff = this ->getParent ()->convertToNodeSpace (touch->getLocation ()) - this ->getPosition ();
30+ return true ;
31+ }
32+ return false ;
3333}
3434
3535void DragButton::ccTouchMoved (CCTouch* touch, CCEvent* event) {
3636 auto pos = this ->getParent ()->convertToNodeSpace (touch->getLocation ()) - m_diff;
37- auto winSize = CCDirector::get ()->getWinSize ();
38- pos.x = std::clamp (pos.x , 0 .f , winSize.width );
39- pos.y = std::clamp (pos.y , 0 .f , winSize.height );
40- this ->setPosition (pos);
37+ this ->setPosition (pos);
4138}
4239
4340void DragButton::ccTouchEnded (CCTouch* touch, CCEvent* event) {
@@ -46,45 +43,46 @@ void DragButton::ccTouchEnded(CCTouch* touch, CCEvent* event) {
4643 action->setTag (123 );
4744 this ->runAction (action);
4845
49- if ((touch->getLocation () - touch->getStartLocation ()).getLength () > 3 .f ) return ;
50- if (this ->m_onPress ) this ->m_onPress ();
46+ if ((touch->getLocation () - touch->getStartLocation ()).getLength () > 3 .f ) return ;
47+ if (this ->m_onPress ) this ->m_onPress ();
5148}
5249
5350void DragButton::ccTouchCancelled (CCTouch* touch, CCEvent* event) {
54- this ->ccTouchEnded (touch, event);
55- }
56-
57- void DragButton::setCallback (std::function<void ()> onPress) {
58- this ->m_onPress = onPress;
59- }
60-
61- std::function<void ()> DragButton::getCallback () {
62- return this ->m_onPress ;
51+ this ->ccTouchEnded (touch, event);
6352}
6453
6554void DragButton::registerWithTouchDispatcher () {
6655 CCTouchDispatcher::get ()->addTargetedDelegate (this , -512 , true );
6756}
6857
6958DragButton* DragButton::create (CCNode* node, std::function<void ()> onPress) {
70- auto ret = new DragButton;
71- if (ret->init (node, onPress)) {
72- ret->autorelease ();
73- return ret;
74- }
75- delete ret;
76- return nullptr ;
59+ auto ret = new DragButton;
60+ if (ret->init (node, onPress)) {
61+ ret->autorelease ();
62+ return ret;
63+ }
64+ delete ret;
65+ return nullptr ;
7766}
7867
7968void DragButton::update (float dt) {
80- const Settings& settings = DevTools::get ()->getSettings ();
81- if (PlayLayer::get () && !CCScene::get ()->getChildByType <PauseLayer>(0 ) && !settings.buttonInGame ) {
82- setVisible (false );
83- } else if (auto lel = LevelEditorLayer::get (); lel && ((lel->m_playbackMode == PlaybackMode::Playing && !settings.buttonInGame ) || !settings.buttonInEditor )) {
84- setVisible (false );
85- } else {
86- setVisible (true );
87- }
69+ const Settings& settings = DevTools::get ()->getSettings ();
70+ if (PlayLayer::get () && !CCScene::get ()->getChildByType <PauseLayer>(0 ) && !settings.buttonInGame ) {
71+ setVisible (false );
72+ } else if (auto lel = LevelEditorLayer::get (); lel && ((lel->m_playbackMode == PlaybackMode::Playing && !settings.buttonInGame ) || !settings.buttonInEditor )) {
73+ setVisible (false );
74+ } else {
75+ setVisible (true );
76+ }
77+ }
78+
79+ void DragButton::setPosition (cocos2d::CCPoint const & pos_) {
80+ auto winSize = CCDirector::get ()->getWinSize ();
81+ float pad = 5 .f ;
82+ auto pos = pos_;
83+ pos.x = std::clamp (pos.x , pad, winSize.width - pad);
84+ pos.y = std::clamp (pos.y , pad, winSize.height - pad);
85+ CCNode::setPosition (pos);
8886}
8987
9088DragButton* DragButton::get () {
0 commit comments