diff --git a/Submodules/UIKit/include/UIGestureRecognizer.h b/Submodules/UIKit/include/UIGestureRecognizer.h index eee51e9..e968e47 100644 --- a/Submodules/UIKit/include/UIGestureRecognizer.h +++ b/Submodules/UIKit/include/UIGestureRecognizer.h @@ -32,9 +32,9 @@ class UIGestureRecognizerDelegate { class UIGestureRecognizer: public enable_shared_from_this { public: std::weak_ptr delegate; - std::function onStateChanged = [](auto state){}; + std::function)> onStateChanged = [](auto self){}; - UIGestureRecognizer(std::function onStateChanged = [](auto) {}); + UIGestureRecognizer(std::function)> onStateChanged = [](auto) {}); virtual ~UIGestureRecognizer(); bool isEnabled() { return _isEnabled; } @@ -55,11 +55,13 @@ class UIGestureRecognizer: public enable_shared_from_this { virtual void pressesEnded(std::vector> presses, std::shared_ptr event); virtual void pressesCancelled(std::vector> presses, std::shared_ptr event); +protected: + UIGestureRecognizerState _state = UIGestureRecognizerState::possible; + private: bool _isEnabled = true; std::weak_ptr _view; std::vector> _allTouches; - UIGestureRecognizerState _state = UIGestureRecognizerState::possible; void _touchesBegan(std::vector> touches, std::shared_ptr event); void _touchesMoved(std::vector> touches, std::shared_ptr event); diff --git a/Submodules/UIKit/lib/UIGestureRecognizer.cpp b/Submodules/UIKit/lib/UIGestureRecognizer.cpp index c5f2558..eacfb1b 100644 --- a/Submodules/UIKit/lib/UIGestureRecognizer.cpp +++ b/Submodules/UIKit/lib/UIGestureRecognizer.cpp @@ -4,7 +4,7 @@ namespace NXKit { -UIGestureRecognizer::UIGestureRecognizer(std::function onStateChanged): +UIGestureRecognizer::UIGestureRecognizer(std::function)> onStateChanged): onStateChanged(onStateChanged) { } @@ -22,7 +22,7 @@ void UIGestureRecognizer::setEnabled(bool enabled) { void UIGestureRecognizer::setState(UIGestureRecognizerState state) { if (this->_state == state) return; this->_state = state; - onStateChanged(state); + onStateChanged(shared_from_this()); switch (state) { case UIGestureRecognizerState::began: cancelOtherGestureRecognizersThatShouldNotRecognizeSimultaneously(); @@ -65,7 +65,7 @@ void UIGestureRecognizer::_touchesBegan(std::vector> to if (touches.back()->_hasBeenCancelledByAGestureRecognizer) return; if (firstTouch && _state == UIGestureRecognizerState::possible) - onStateChanged(_state); + onStateChanged(shared_from_this()); } void UIGestureRecognizer::_touchesMoved(std::vector> touches, std::shared_ptr event) { diff --git a/Submodules/UIKit/lib/UIPanGestureRecognizer.cpp b/Submodules/UIKit/lib/UIPanGestureRecognizer.cpp index f9cdfda..75d6920 100644 --- a/Submodules/UIKit/lib/UIPanGestureRecognizer.cpp +++ b/Submodules/UIKit/lib/UIPanGestureRecognizer.cpp @@ -46,7 +46,8 @@ void UIPanGestureRecognizer::touchesBegan(std::vector> initialTouchPoint = trackingTouch->locationIn(nullptr); touchesMovedTimestamp = trackingTouch->timestamp(); previousTouchesMovedTimestamp = trackingTouch->timestamp(); - onStateChanged(UIGestureRecognizerState::possible); + _state = UIGestureRecognizerState::possible; + onStateChanged(shared_from_this()); } } @@ -67,7 +68,8 @@ void UIPanGestureRecognizer::touchesMoved(std::vector> } else if (state() == UIGestureRecognizerState::changed) { previousTouchesMovedTimestamp = touchesMovedTimestamp; touchesMovedTimestamp = trackingTouch->timestamp(); - onStateChanged(UIGestureRecognizerState::changed); + _state = UIGestureRecognizerState::changed; + onStateChanged(shared_from_this()); } } } diff --git a/app/Screens/TestViewController/TestViewController.cpp b/app/Screens/TestViewController/TestViewController.cpp index 2d9d5ba..48bf1e8 100644 --- a/app/Screens/TestViewController/TestViewController.cpp +++ b/app/Screens/TestViewController/TestViewController.cpp @@ -136,10 +136,11 @@ void TestViewController::loadView() { rootView->addSubview(button); auto tapGesture = new_shared(); - tapGesture->onStateChanged = [button](UIGestureRecognizerState status) { - if (status == NXKit::UIGestureRecognizerState::ended) { + tapGesture->onStateChanged = [](auto gesture) { + if (gesture->state() == NXKit::UIGestureRecognizerState::ended) { static bool toggle = false; toggle = !toggle; + auto button = std::static_pointer_cast(gesture->view().lock()); button->setText(toggle ? "You did it!!!!" : "Another text, keep trying!!!!!"); } }; @@ -160,12 +161,13 @@ void TestViewController::loadView() { blur->addSubview(dragMeViewLabel); auto panGesture = new_shared(); - panGesture->onStateChanged = [this, panGesture](UIGestureRecognizerState status) { + panGesture->onStateChanged = [this](auto gesture) { + auto panGesture = std::static_pointer_cast(gesture); static NXPoint initial; auto _view = panGesture->view().lock(); if (!_view) return; - switch (status) { + switch (panGesture->state()) { case UIGestureRecognizerState::began: { initial = _view->frame().origin; break; diff --git a/app/Screens/YogaTestViewController/YogaTestViewController.cpp b/app/Screens/YogaTestViewController/YogaTestViewController.cpp index 6a3d1d8..02ae97e 100644 --- a/app/Screens/YogaTestViewController/YogaTestViewController.cpp +++ b/app/Screens/YogaTestViewController/YogaTestViewController.cpp @@ -9,6 +9,7 @@ YogaTestViewController::YogaTestViewController() { void YogaTestViewController::loadView() { auto rootView = new_shared(); + //MARK: - HEADER! auto header = new_shared(); header->setBackgroundColor(UIColor::systemRed); // header->setFrame({ 20, 20, 300, 40 }); @@ -18,6 +19,53 @@ void YogaTestViewController::loadView() { }); rootView->addSubview(header); + + + //MARK: - Content! + auto contentBox = new_shared(); + auto contentContent1 = new_shared(); + contentContent1->setBackgroundColor(UIColor::systemTeal); + auto contentContent2 = new_shared(); + contentContent2->setBackgroundColor(UIColor::systemMint); + + contentContent1->configureLayout([](auto layout) { + layout->setSize({ 200, 100 }); + }); + contentContent2->configureLayout([](auto layout) { + layout->setSize({ 200, 100 }); + }); + contentBox->configureLayout([](std::shared_ptr layout) { + layout->setSize({ 200, 200 }); + layout->setFlexDirection(YGFlexDirectionColumn); + layout->setJustifyContent(YGJustifySpaceBetween); + }); + + contentBox->addSubview(contentContent1); + contentBox->addSubview(contentContent2); + rootView->addSubview(contentBox); + + auto tap = new_shared(); + tap->onStateChanged = [](auto tap) { + if (tap->state() != UIGestureRecognizerState::ended) return; + std::shared_ptr view = tap->view().lock(); + + static bool toggle = false; + toggle = !toggle; + + UIView::animate(2, [view]() { + view->configureLayout([](std::shared_ptr layout) { + layout->setFlexDirection(toggle ? YGFlexDirection::YGFlexDirectionColumnReverse : YGFlexDirection::YGFlexDirectionColumn); + }); + view->setNeedsLayout(); + view->layoutIfNeeded(); + }); + }; + contentBox->addGestureRecognizer(tap); + contentBox->tag = "Content box"; + + + + //MARK: - Footer! auto footer = new_shared(); footer->setBackgroundColor(UIColor::systemBlue); // footer->setFrame({ 20, 420, 300, 40 }); @@ -30,8 +78,10 @@ void YogaTestViewController::loadView() { rootView->configureLayout([](std::shared_ptr layout) { layout->setFlexDirection(YGFlexDirectionColumn); layout->setJustifyContent(YGJustifySpaceBetween); + layout->setAlignContent(YGAlign::YGAlignCenter); }); + auto dragMeViewLabel = new_shared(); dragMeViewLabel->setText("Drag me!"); dragMeViewLabel->setFontWeight(600); @@ -48,12 +98,13 @@ void YogaTestViewController::loadView() { blur->addSubview(dragMeViewLabel); auto panGesture = new_shared(); - panGesture->onStateChanged = [this, panGesture](UIGestureRecognizerState status) { + panGesture->onStateChanged = [this](auto gesture) { + auto panGesture = std::static_pointer_cast(gesture); static NXPoint initial; auto _view = panGesture->view().lock(); if (!_view) return; - switch (status) { + switch (panGesture->state()) { case UIGestureRecognizerState::began: { initial = _view->frame().origin; break;