Skip to content

Commit

Permalink
UITapGestureRecognizer impl
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 17, 2024
1 parent 9ea6010 commit 8d104df
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_library(UIKit
lib/UIPress.cpp
lib/UIPressesEvent.cpp
lib/UIResponder.cpp
lib/UITapGestureRecognizer.cpp
lib/UITouch.cpp
lib/UIView.cpp
lib/UIViewAnimationGroup.cpp
Expand Down
1 change: 1 addition & 0 deletions Submodules/UIKit/include/UIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
#include <UIImageView.h>
#include <UILabel.h>
#include <UIViewController.h>
#include <UITapGestureRecognizer.h>
22 changes: 22 additions & 0 deletions Submodules/UIKit/include/UITapGestureRecognizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <UIGestureRecognizer.h>
#include <Geometry.h>

namespace NXKit {

class UITapGestureRecognizer: public UIGestureRecognizer {
public:
void touchesBegan(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) override;
void touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) override;
void touchesEnded(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) override;

void pressesBegan(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event) override;
void pressesEnded(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event) override;

private:
std::shared_ptr<UITouch> trackingTouch;
NXPoint startPoint;
};

}
55 changes: 55 additions & 0 deletions Submodules/UIKit/lib/UITapGestureRecognizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <UITapGestureRecognizer.h>
#include <UITouch.h>
#include <UIPress.h>

namespace NXKit {

#define THRESHOLD 10

void UITapGestureRecognizer::touchesBegan(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) {
UIGestureRecognizer::touchesBegan(touches, event);
if (!trackingTouch) {
trackingTouch = touches[0];
startPoint = trackingTouch->locationIn(this->view().lock());
}
}

void UITapGestureRecognizer::touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) {
UIGestureRecognizer::touchesMoved(touches, event);
if (trackingTouch == touches[0]) {
NXPoint diff = startPoint - trackingTouch->locationIn(this->view().lock());
if (abs(diff.x) >= THRESHOLD || abs(diff.y) >= THRESHOLD) {
setState(UIGestureRecognizerState::failed);
trackingTouch = nullptr;
}
}
}

void UITapGestureRecognizer::touchesEnded(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) {
UIGestureRecognizer::touchesEnded(touches, event);
if (trackingTouch == touches[0]) {
setState(UIGestureRecognizerState::ended);
trackingTouch = nullptr;
}
}

void UITapGestureRecognizer::pressesBegan(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event) {
UIGestureRecognizer::pressesBegan(presses, event);
if (presses.empty()) return;

if (presses[0]->key()->type() == UIPressType::select) {
setState(UIGestureRecognizerState::began);
}
}

void UITapGestureRecognizer::pressesEnded(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event) {
UIGestureRecognizer::pressesEnded(presses, event);

if (presses.empty()) return;

if (presses[0]->key()->type() == UIPressType::select) {
setState(UIGestureRecognizerState::ended);
}
}

}
21 changes: 20 additions & 1 deletion app/Screens/TestViewController/TestViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void TestViewController::loadView() {
subview->setBackgroundColor(UIColor::tint);
subview->layer()->setCornerRadius(10);
subview->setAlpha(0.5);
// subview->setClipsToBounds(true);
// subview->setClipsToBounds(true);

rootView->addSubview(subview);

Expand Down Expand Up @@ -129,6 +129,25 @@ void TestViewController::loadView() {

switchLabelText(label2);

auto button = new_shared<UILabel>();
button->setText("Press me!");
button->setFontWeight(600);
button->setTextAlignment(NSTextAlignment::center);
button->setFrame({ 100, 300, 200, 60 });
button->layer()->setCornerRadius(12);
button->setBackgroundColor(UIColor::tint);
rootView->addSubview(button);

auto tapGesture = new_shared<UITapGestureRecognizer>();
tapGesture->onStateChanged = [button](UIGestureRecognizerState status) {
if (status == NXKit::UIGestureRecognizerState::ended) {
static bool toggle = false;
toggle = !toggle;
button->setText(toggle ? "You did it!!!!" : "Another text, keep trying!!!!!");
}
};
button->addGestureRecognizer(tapGesture);

setView(rootView);
}

Expand Down

0 comments on commit 8d104df

Please sign in to comment.