Skip to content

Commit

Permalink
Responder chain impl
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 17, 2024
1 parent 871f575 commit 9ea6010
Show file tree
Hide file tree
Showing 26 changed files with 1,414 additions and 245 deletions.
7 changes: 7 additions & 0 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ add_library(UIKit
lib/UIApplicationMain.cpp
lib/UIBlurView.cpp
lib/UIColor.cpp
lib/UIEvent.cpp
lib/UIGestureRecognizer.cpp
lib/UIImage.cpp
lib/UIImageView.cpp
lib/UIKey.cpp
lib/UILabel.cpp
lib/UIPress.cpp
lib/UIPressesEvent.cpp
lib/UIResponder.cpp
lib/UITouch.cpp
lib/UIView.cpp
lib/UIViewAnimationGroup.cpp
lib/UIViewAnimationOptions.cpp
Expand Down
4 changes: 2 additions & 2 deletions Submodules/UIKit/include/UIApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UIApplication {
void handleEventsIfNeeded();
void handleSDLQuit();

// void sendEvent(std::shared_ptr<UIEvent> event);
void sendEvent(std::shared_ptr<UIEvent> event);

// TODO: Need to remove
// static GPU_Target* currentRenderer;
Expand All @@ -33,4 +33,4 @@ class UIApplication {
void handleSDLEvent(SDL_Event e);
};

}
}
28 changes: 28 additions & 0 deletions Submodules/UIKit/include/UIEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <Timer.h>
#include <set>

namespace NXKit {

class UITouch;
class UIEvent {
public:
std::set<std::shared_ptr<UITouch>> allTouches() { return _allTouches; }
Timer timestamp() { return _timestamp; }

UIEvent();
virtual ~UIEvent() {}
private:
UIEvent(std::shared_ptr<UITouch> touch);

static std::vector<std::shared_ptr<UIEvent>> activeEvents;
std::set<std::shared_ptr<UITouch>> _allTouches;
Timer _timestamp = Timer();

friend class UIApplication;
friend class UIWindow;
};

}

84 changes: 84 additions & 0 deletions Submodules/UIKit/include/UIGestureRecognizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include <memory>
#include <vector>
#include <functional>
#include <tools/SharedBase.hpp>

namespace NXKit {

class UITouch;
class UIPress;
class UIPressesEvent;

enum class UIGestureRecognizerState {
possible,
began,
recognized,
changed,
ended,
cancelled,
failed
};

class UIView;
class UIEvent;
class UIGestureRecognizer;
class UIGestureRecognizerDelegate {
public:
virtual bool gestureRecognizerShouldRecognizeSimultaneouslyWith(std::shared_ptr<UIGestureRecognizer> gestureRecognizer, std::shared_ptr<UIGestureRecognizer> otherGestureRecognizer) { return false; }
};

class UIGestureRecognizer: public enable_shared_from_this<UIGestureRecognizer> {
public:
std::weak_ptr<UIGestureRecognizerDelegate> delegate;
std::function<void(UIGestureRecognizerState)> onStateChanged = [](auto state){};

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

bool isEnabled() { return _isEnabled; }
void setEnabled(bool enabled);

UIGestureRecognizerState state() { return _state; }
void setState(UIGestureRecognizerState state);

std::weak_ptr<UIView> view() { return _view; }

virtual void touchesBegan(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);
virtual void touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);
virtual void touchesEnded(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);
virtual void touchesCancelled(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);

virtual void pressesBegan(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);
virtual void pressesChanged(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);
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);

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);
void _touchesEnded(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);
void _touchesCancelled(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event);

void _pressesBegan(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);
void _pressesChanged(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);
void _pressesEnded(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);
void _pressesCancelled(std::vector<std::shared_ptr<UIPress>> presses, std::shared_ptr<UIPressesEvent> event);

bool recognitionCondition();

void addTouch(std::shared_ptr<UITouch> touch);
void removeTouch(std::shared_ptr<UITouch> touch);
void cancelOtherGestureRecognizersThatShouldNotRecognizeSimultaneously();

friend class UIView;
};

}

44 changes: 44 additions & 0 deletions Submodules/UIKit/include/UIKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <string>
#include <UIKeyboardHIDUsage.h>
#include <tools/Tools.hpp>

namespace NXKit {

enum class UIKeyModifierFlags {
alphaShift = 1 << 0,
shift = 1 << 1,
control = 1 << 2,
alternate = 1 << 3,
command = 1 << 4,
numericPad = 1 << 5,
};

enum class UIPressType {
none,
upArrow,
downArrow,
leftArrow,
rightArrow,
select,
menu,
};

struct UIKey {
public:
UIPressType type();
std::string characters() { return _characters; }
OptionSet<UIKeyModifierFlags> modifierFlags() { return _modifierFlags; }
UIKeyboardHIDUsage keyCode() { return _keyCode; }

private:
std::string _characters;
OptionSet<UIKeyModifierFlags> _modifierFlags;
UIKeyboardHIDUsage _keyCode;

friend class UIApplication;
};

}

Loading

0 comments on commit 9ea6010

Please sign in to comment.