Skip to content

Commit

Permalink
Changed gesture callback approach
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 20, 2024
1 parent 8a48d4f commit 7c95475
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
8 changes: 5 additions & 3 deletions Submodules/UIKit/include/UIGestureRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class UIGestureRecognizerDelegate {
class UIGestureRecognizer: public enable_shared_from_this<UIGestureRecognizer> {
public:
std::weak_ptr<UIGestureRecognizerDelegate> delegate;
std::function<void(UIGestureRecognizerState)> onStateChanged = [](auto state){};
std::function<void(std::shared_ptr<UIGestureRecognizer>)> onStateChanged = [](auto self){};

UIGestureRecognizer(std::function<void(UIGestureRecognizerState)> onStateChanged = [](auto) {});
UIGestureRecognizer(std::function<void(std::shared_ptr<UIGestureRecognizer>)> onStateChanged = [](auto) {});
virtual ~UIGestureRecognizer();

bool isEnabled() { return _isEnabled; }
Expand All @@ -55,11 +55,13 @@ class UIGestureRecognizer: public enable_shared_from_this<UIGestureRecognizer> {
virtual void pressesEnded(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);
virtual void pressesCancelled(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);

protected:
UIGestureRecognizerState _state = UIGestureRecognizerState::possible;

private:
bool _isEnabled = true;
std::weak_ptr<UIView> _view;
std::vector<std::shared_ptr<UITouch>> _allTouches;
UIGestureRecognizerState _state = UIGestureRecognizerState::possible;

void _touchesBegan(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);
void _touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);
Expand Down
6 changes: 3 additions & 3 deletions Submodules/UIKit/lib/UIGestureRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NXKit {

UIGestureRecognizer::UIGestureRecognizer(std::function<void(UIGestureRecognizerState)> onStateChanged):
UIGestureRecognizer::UIGestureRecognizer(std::function<void(std::shared_ptr<UIGestureRecognizer>)> onStateChanged):
onStateChanged(onStateChanged)
{ }

Expand All @@ -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();
Expand Down Expand Up @@ -65,7 +65,7 @@ void UIGestureRecognizer::_touchesBegan(std::vector<std::shared_ptr<UITouch>> to
if (touches.back()->_hasBeenCancelledByAGestureRecognizer) return;

if (firstTouch && _state == UIGestureRecognizerState::possible)
onStateChanged(_state);
onStateChanged(shared_from_this());
}

void UIGestureRecognizer::_touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) {
Expand Down
6 changes: 4 additions & 2 deletions Submodules/UIKit/lib/UIPanGestureRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void UIPanGestureRecognizer::touchesBegan(std::vector<std::shared_ptr<UITouch>>
initialTouchPoint = trackingTouch->locationIn(nullptr);
touchesMovedTimestamp = trackingTouch->timestamp();
previousTouchesMovedTimestamp = trackingTouch->timestamp();
onStateChanged(UIGestureRecognizerState::possible);
_state = UIGestureRecognizerState::possible;
onStateChanged(shared_from_this());
}
}

Expand All @@ -67,7 +68,8 @@ void UIPanGestureRecognizer::touchesMoved(std::vector<std::shared_ptr<UITouch>>
} else if (state() == UIGestureRecognizerState::changed) {
previousTouchesMovedTimestamp = touchesMovedTimestamp;
touchesMovedTimestamp = trackingTouch->timestamp();
onStateChanged(UIGestureRecognizerState::changed);
_state = UIGestureRecognizerState::changed;
onStateChanged(shared_from_this());
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions app/Screens/TestViewController/TestViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ void TestViewController::loadView() {
rootView->addSubview(button);

auto tapGesture = new_shared<UITapGestureRecognizer>();
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<UILabel>(gesture->view().lock());
button->setText(toggle ? "You did it!!!!" : "Another text, keep trying!!!!!");
}
};
Expand All @@ -160,12 +161,13 @@ void TestViewController::loadView() {
blur->addSubview(dragMeViewLabel);

auto panGesture = new_shared<UIPanGestureRecognizer>();
panGesture->onStateChanged = [this, panGesture](UIGestureRecognizerState status) {
panGesture->onStateChanged = [this](auto gesture) {
auto panGesture = std::static_pointer_cast<UIPanGestureRecognizer>(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;
Expand Down
55 changes: 53 additions & 2 deletions app/Screens/YogaTestViewController/YogaTestViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ YogaTestViewController::YogaTestViewController() {
void YogaTestViewController::loadView() {
auto rootView = new_shared<UIView>();

//MARK: - HEADER!
auto header = new_shared<UIView>();
header->setBackgroundColor(UIColor::systemRed);
// header->setFrame({ 20, 20, 300, 40 });
Expand All @@ -18,6 +19,53 @@ void YogaTestViewController::loadView() {
});
rootView->addSubview(header);



//MARK: - Content!
auto contentBox = new_shared<UIView>();
auto contentContent1 = new_shared<UIView>();
contentContent1->setBackgroundColor(UIColor::systemTeal);
auto contentContent2 = new_shared<UIView>();
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<YGLayout> layout) {
layout->setSize({ 200, 200 });
layout->setFlexDirection(YGFlexDirectionColumn);
layout->setJustifyContent(YGJustifySpaceBetween);
});

contentBox->addSubview(contentContent1);
contentBox->addSubview(contentContent2);
rootView->addSubview(contentBox);

auto tap = new_shared<UITapGestureRecognizer>();
tap->onStateChanged = [](auto tap) {
if (tap->state() != UIGestureRecognizerState::ended) return;
std::shared_ptr<UIView> view = tap->view().lock();

static bool toggle = false;
toggle = !toggle;

UIView::animate(2, [view]() {
view->configureLayout([](std::shared_ptr<YGLayout> 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<UIView>();
footer->setBackgroundColor(UIColor::systemBlue);
// footer->setFrame({ 20, 420, 300, 40 });
Expand All @@ -30,8 +78,10 @@ void YogaTestViewController::loadView() {
rootView->configureLayout([](std::shared_ptr<YGLayout> layout) {
layout->setFlexDirection(YGFlexDirectionColumn);
layout->setJustifyContent(YGJustifySpaceBetween);
layout->setAlignContent(YGAlign::YGAlignCenter);
});


auto dragMeViewLabel = new_shared<UILabel>();
dragMeViewLabel->setText("Drag me!");
dragMeViewLabel->setFontWeight(600);
Expand All @@ -48,12 +98,13 @@ void YogaTestViewController::loadView() {
blur->addSubview(dragMeViewLabel);

auto panGesture = new_shared<UIPanGestureRecognizer>();
panGesture->onStateChanged = [this, panGesture](UIGestureRecognizerState status) {
panGesture->onStateChanged = [this](auto gesture) {
auto panGesture = std::static_pointer_cast<UIPanGestureRecognizer>(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;
Expand Down

0 comments on commit 7c95475

Please sign in to comment.