diff --git a/Submodules/UIKit/CMakeLists.txt b/Submodules/UIKit/CMakeLists.txt index bac4217..d6baf5f 100644 --- a/Submodules/UIKit/CMakeLists.txt +++ b/Submodules/UIKit/CMakeLists.txt @@ -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 diff --git a/Submodules/UIKit/include/UIKit.h b/Submodules/UIKit/include/UIKit.h index 6e00ce1..be8f6dc 100644 --- a/Submodules/UIKit/include/UIKit.h +++ b/Submodules/UIKit/include/UIKit.h @@ -9,3 +9,4 @@ #include #include #include +#include diff --git a/Submodules/UIKit/include/UITapGestureRecognizer.h b/Submodules/UIKit/include/UITapGestureRecognizer.h new file mode 100644 index 0000000..b4ef618 --- /dev/null +++ b/Submodules/UIKit/include/UITapGestureRecognizer.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +namespace NXKit { + +class UITapGestureRecognizer: public UIGestureRecognizer { +public: + void touchesBegan(std::vector> touches, std::shared_ptr event) override; + void touchesMoved(std::vector> touches, std::shared_ptr event) override; + void touchesEnded(std::vector> touches, std::shared_ptr event) override; + + void pressesBegan(std::vector> presses, std::shared_ptr event) override; + void pressesEnded(std::vector> presses, std::shared_ptr event) override; + +private: + std::shared_ptr trackingTouch; + NXPoint startPoint; +}; + +} diff --git a/Submodules/UIKit/lib/UITapGestureRecognizer.cpp b/Submodules/UIKit/lib/UITapGestureRecognizer.cpp new file mode 100644 index 0000000..0d5d6e5 --- /dev/null +++ b/Submodules/UIKit/lib/UITapGestureRecognizer.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +namespace NXKit { + +#define THRESHOLD 10 + +void UITapGestureRecognizer::touchesBegan(std::vector> touches, std::shared_ptr event) { + UIGestureRecognizer::touchesBegan(touches, event); + if (!trackingTouch) { + trackingTouch = touches[0]; + startPoint = trackingTouch->locationIn(this->view().lock()); + } +} + +void UITapGestureRecognizer::touchesMoved(std::vector> touches, std::shared_ptr 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> touches, std::shared_ptr event) { + UIGestureRecognizer::touchesEnded(touches, event); + if (trackingTouch == touches[0]) { + setState(UIGestureRecognizerState::ended); + trackingTouch = nullptr; + } +} + +void UITapGestureRecognizer::pressesBegan(std::vector> presses, std::shared_ptr event) { + UIGestureRecognizer::pressesBegan(presses, event); + if (presses.empty()) return; + + if (presses[0]->key()->type() == UIPressType::select) { + setState(UIGestureRecognizerState::began); + } +} + +void UITapGestureRecognizer::pressesEnded(std::vector> presses, std::shared_ptr event) { + UIGestureRecognizer::pressesEnded(presses, event); + + if (presses.empty()) return; + + if (presses[0]->key()->type() == UIPressType::select) { + setState(UIGestureRecognizerState::ended); + } +} + +} diff --git a/app/Screens/TestViewController/TestViewController.cpp b/app/Screens/TestViewController/TestViewController.cpp index ccd14db..d62b929 100644 --- a/app/Screens/TestViewController/TestViewController.cpp +++ b/app/Screens/TestViewController/TestViewController.cpp @@ -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); @@ -129,6 +129,25 @@ void TestViewController::loadView() { switchLabelText(label2); + auto button = new_shared(); + 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(); + 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); }