From ec2e6830aa9af1195aa2e626864cb374ffeeade3 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Sun, 10 Nov 2013 22:58:35 +0000 Subject: [PATCH] ios stuff --- include/GUIManager/GUIManager.h | 15 ++- include/SalmonEngineInterface.h | 8 +- include/Utils/IosApi/IosWrapper.h | 8 +- include/Utils/IosApi/ObjC/GLKViewTemplate.h | 5 +- src/GUIManager/GUIManager.cpp | 49 ++++++-- src/SalmonEngineInterface.cpp | 16 +-- src/SalmonEngineIos.cpp | 16 +-- src/Utils/IosApi/ObjC/GLKViewTemplate.mm | 112 +++++++++++++++--- .../IosApi/ObjC/ViewControllerTemplate.mm | 5 + 9 files changed, 172 insertions(+), 62 deletions(-) diff --git a/include/GUIManager/GUIManager.h b/include/GUIManager/GUIManager.h index 8212cf9..4ee981e 100644 --- a/include/GUIManager/GUIManager.h +++ b/include/GUIManager/GUIManager.h @@ -17,6 +17,7 @@ namespace SE extern const std::string CONST_HOLD_SIGNAL_NAME; extern const std::string CONST_CLICK_SIGNAL_NAME; extern const std::string CONST_DRAG_SIGNAL_NAME; +extern const std::string CONST_TAPDOWN_SIGNAL_NAME; typedef boost::variant TSignalParam; @@ -42,6 +43,7 @@ struct TWidgetStruct { SignalMap[CONST_HOLD_SIGNAL_NAME] = std::shared_ptr>(new boost::signal()); SignalMap[CONST_CLICK_SIGNAL_NAME] = std::shared_ptr>(new boost::signal()); + SignalMap[CONST_TAPDOWN_SIGNAL_NAME] = std::shared_ptr>(new boost::signal()); SignalMap[CONST_DRAG_SIGNAL_NAME] = std::shared_ptr>(new boost::signal()); } @@ -133,8 +135,8 @@ protected: TWidgetArr::iterator FindWidgetInArr(const std::string& widgetName); - vec2 LastTapPos; - vec2 TotalShift; + std::map LastTapPos; + std::map TotalShift; boost::mutex WidgetListMutex; @@ -165,13 +167,13 @@ public: void AddWidgetTransformTask(TWidgetTransformTask widgetTransformTask); - void OnMouseDown(vec2 pos); + void OnMouseDown(vec2 pos, int touchNumber); - void OnMouseUp(vec2 pos); + void OnMouseUp(vec2 pos, int touchNumber); - void OnMouseUpAfterMove(vec2 pos); + void OnMouseUpAfterMove(vec2 pos, int touchNumber); - void OnMove(vec2 shift); + void OnMove(vec2 shift, int touchNumber); void ShowKeyboard(const std::string text = ""); void HideKeyboard(); @@ -182,6 +184,7 @@ public: std::shared_ptr> GetOnClickSignal(const std::string& widgetName); std::shared_ptr> GetOnHoldSignal(const std::string& widgetName); std::shared_ptr> GetSignal(const std::string& signalName, const std::string& widgetName); + std::shared_ptr> GetOnTapDownSignal(const std::string& widgetName); //TFunctionBinderInterface implementation: void SQ_MoveWidget(const SQChar *widgetName, float x, float y); diff --git a/include/SalmonEngineInterface.h b/include/SalmonEngineInterface.h index 1cecf15..f2d4748 100644 --- a/include/SalmonEngineInterface.h +++ b/include/SalmonEngineInterface.h @@ -116,13 +116,13 @@ public: virtual void UpdateQuick() { } //To process input - this method is called more frequently than Update() - virtual void OuterOnTapDown(vec2 p); + virtual void OuterOnTapDown(vec2 p, int touchNumber); - virtual void OuterOnTapUp(vec2 p); + virtual void OuterOnTapUp(vec2 p, int touchNumber); - virtual void OuterOnTapUpAfterMove(vec2 p); + virtual void OuterOnTapUpAfterMove(vec2 p, int touchNumber); - virtual void OuterOnMove(vec2 shift); + virtual void OuterOnMove(vec2 shift, int touchNumber); virtual void InnerInit() { } diff --git a/include/Utils/IosApi/IosWrapper.h b/include/Utils/IosApi/IosWrapper.h index 202db5e..0e26d97 100644 --- a/include/Utils/IosApi/IosWrapper.h +++ b/include/Utils/IosApi/IosWrapper.h @@ -15,11 +15,11 @@ namespace SE void AppDeinit(); void AppUpdate(int dt); void AppDraw(); -void AppOnTapDown(int posx, int posy); -void AppOnTapUp(int posx, int posy); -void AppOnScroll(int shiftx, int shifty); +void AppOnTapDown(int posx, int posy, int touchNumber); +void AppOnTapUp(int posx, int posy, int touchNumber); +void AppOnScroll(int shiftx, int shifty, int touchNumber); void AppOnScale(float scale); -void AppOnTapUpAfterMove(int posx, int posy); +void AppOnTapUpAfterMove(int posx, int posy, int touchNumber); } \ No newline at end of file diff --git a/include/Utils/IosApi/ObjC/GLKViewTemplate.h b/include/Utils/IosApi/ObjC/GLKViewTemplate.h index 1cbb3ac..f357b96 100644 --- a/include/Utils/IosApi/ObjC/GLKViewTemplate.h +++ b/include/Utils/IosApi/ObjC/GLKViewTemplate.h @@ -11,9 +11,10 @@ @interface GLKViewTemplate : GLKView { - CGPoint touchBeganLocation; + //CGPoint touchBeganLocation[255]; + + //bool touchMoved[255]; - bool touchMoved; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; diff --git a/src/GUIManager/GUIManager.cpp b/src/GUIManager/GUIManager.cpp index ade271c..f716797 100644 --- a/src/GUIManager/GUIManager.cpp +++ b/src/GUIManager/GUIManager.cpp @@ -7,6 +7,7 @@ namespace SE const std::string CONST_HOLD_SIGNAL_NAME = "OnHold"; const std::string CONST_CLICK_SIGNAL_NAME = "OnClick"; const std::string CONST_DRAG_SIGNAL_NAME = "OnDrag"; +const std::string CONST_TAPDOWN_SIGNAL_NAME = "OnTapDown"; boost::mutex KeyMutex; @@ -261,16 +262,20 @@ void TGUIManager::MoveWidgetGroup(const std::string& widgetGroupName, const std: } } -void TGUIManager::OnMouseDown(vec2 pos) +void TGUIManager::OnMouseDown(vec2 pos, int touchNumber) { //Xperimental - need to call widget methods and signals NOT IN "FOR" LOOP + + //*Console<<"OnMouseDown touchNumber == "+tostr(touchNumber)+"\n"; boost::lock_guard guard(WidgetListMutex); TWidgetArr::reverse_iterator i; - LastTapPos = pos; - TotalShift = vec2(0,0); + LastTapPos[touchNumber] = pos; + TotalShift[touchNumber] = vec2(0,0); + + std::vector>> signalMap; for (i = WidgetArr.rbegin(); i != WidgetArr.rend(); ++i) { @@ -280,19 +285,29 @@ void TGUIManager::OnMouseDown(vec2 pos) i->Widget->OnTapDown(pos); i->IsMouseDown = true; + + signalMap.push_back((i->SignalMap[CONST_TAPDOWN_SIGNAL_NAME])); if (! isTransparentForInput) { - return; + break; } } } + + //Keep this outside since signal may affect WidgetArr + BOOST_FOREACH(auto signalPtr, signalMap) + { + (*signalPtr)(TSignalParam(pos)); + } } -void TGUIManager::OnMouseUp(vec2 pos) +void TGUIManager::OnMouseUp(vec2 pos, int touchNumber) { //Xperimental - need to call widget methods and signals NOT IN "FOR" LOOP + + //*Console<<"OnMouseUp touchNumber == "+tostr(touchNumber)+"\n"; boost::lock_guard guard(WidgetListMutex); @@ -326,10 +341,12 @@ void TGUIManager::OnMouseUp(vec2 pos) } - void TGUIManager::OnMouseUpAfterMove(vec2 pos) + void TGUIManager::OnMouseUpAfterMove(vec2 pos, int touchNumber) { //Xperimental - need to call widget methods and signals NOT IN "FOR" LOOP + //*Console<<"OnMouseUpAfterMove touchNumber == "+tostr(touchNumber)+"\n"; + boost::lock_guard guard(WidgetListMutex); TWidgetArr::reverse_iterator i; @@ -353,7 +370,7 @@ void TGUIManager::OnMouseUp(vec2 pos) } } - if (i->Widget->CheckClick(LastTapPos) && !i->Widget->CheckClick(pos)) + if (i->Widget->CheckClick(LastTapPos[touchNumber]) && !i->Widget->CheckClick(pos)) { bool isTransparentForInput = i->Widget->IsTransparentForInput(); i->Widget->OnTapUpAfterMoveOut(pos); @@ -379,17 +396,18 @@ void TGUIManager::OnMouseUp(vec2 pos) } -void TGUIManager::OnMove(vec2 shift) +void TGUIManager::OnMove(vec2 shift, int touchNumber) { //Xperimental - need to call widget methods and signals NOT IN "FOR" LOOP + TWidgetArr::reverse_iterator i; bool moveIsProcessed = false; bool moveOutIsProcessed = false; - TotalShift += shift; + TotalShift[touchNumber] += shift; //LastTapPos += shift; @@ -397,7 +415,7 @@ void TGUIManager::OnMove(vec2 shift) for (i = WidgetArr.rbegin(); i != WidgetArr.rend(); ++i) { - if (!moveIsProcessed && i->Widget->CheckClick(LastTapPos-TotalShift)) + if (!moveIsProcessed && i->Widget->CheckClick(LastTapPos[touchNumber]-TotalShift[touchNumber])) { bool isTransparentForInput = i->Widget->IsTransparentForInput(); @@ -421,7 +439,7 @@ void TGUIManager::OnMove(vec2 shift) for (i = WidgetArr.rbegin(); i != WidgetArr.rend(); ++i) { - if (!moveOutIsProcessed && i->Widget->CheckClick(LastTapPos) && !i->Widget->CheckClick(LastTapPos-TotalShift)) + if (!moveOutIsProcessed && i->Widget->CheckClick(LastTapPos[touchNumber]) && !i->Widget->CheckClick(LastTapPos[touchNumber]-TotalShift[touchNumber])) { bool isTransparentForInput = i->Widget->IsTransparentForInput(); @@ -494,6 +512,15 @@ std::shared_ptr> TGUIManager::GetOnHoldSignal } +std::shared_ptr> TGUIManager::GetOnTapDownSignal(const std::string& widgetName) +{ + + TWidgetArr::iterator i = FindWidgetInArr(widgetName); + + return i->SignalMap[CONST_TAPDOWN_SIGNAL_NAME]; + +} + std::shared_ptr> TGUIManager::GetSignal(const std::string& signalName, const std::string& widgetName) { diff --git a/src/SalmonEngineInterface.cpp b/src/SalmonEngineInterface.cpp index 0091169..9fb0062 100644 --- a/src/SalmonEngineInterface.cpp +++ b/src/SalmonEngineInterface.cpp @@ -119,27 +119,27 @@ void TApplicationAncestor::OuterUpdate(cardinal timer) } -void TApplicationAncestor::OuterOnTapDown(vec2 p) +void TApplicationAncestor::OuterOnTapDown(vec2 p, int touchNumber) { - ResourceManager->GUIManager.OnMouseDown(p); + ResourceManager->GUIManager.OnMouseDown(p, touchNumber); InnerOnTapDown(p); } -void TApplicationAncestor::OuterOnTapUp(vec2 p) +void TApplicationAncestor::OuterOnTapUp(vec2 p, int touchNumber) { - ResourceManager->GUIManager.OnMouseUp(p); + ResourceManager->GUIManager.OnMouseUp(p, touchNumber); InnerOnTapUp(p); } -void TApplicationAncestor::OuterOnTapUpAfterMove(vec2 p) +void TApplicationAncestor::OuterOnTapUpAfterMove(vec2 p, int touchNumber) { - ResourceManager->GUIManager.OnMouseUpAfterMove(p); + ResourceManager->GUIManager.OnMouseUpAfterMove(p, touchNumber); InnerOnTapUpAfterMove(p); } -void TApplicationAncestor::OuterOnMove(vec2 shift) +void TApplicationAncestor::OuterOnMove(vec2 shift, int touchNumber) { - ResourceManager->GUIManager.OnMove(shift); + ResourceManager->GUIManager.OnMove(shift, touchNumber); InnerOnMove(shift); } diff --git a/src/SalmonEngineIos.cpp b/src/SalmonEngineIos.cpp index c6f1eb3..4576d8e 100644 --- a/src/SalmonEngineIos.cpp +++ b/src/SalmonEngineIos.cpp @@ -73,27 +73,27 @@ void DestroyEngine() } - void AppOnTapDown(int posx, int posy) + void AppOnTapDown(int posx, int posy, int touchNumber) { - App->OuterOnTapDown(vec2(posx, posy)); + App->OuterOnTapDown(vec2(posx, posy), touchNumber); } - void AppOnTapUp(int posx, int posy) + void AppOnTapUp(int posx, int posy, int touchNumber) { - App->OuterOnTapUp(vec2(posx, posy)); + App->OuterOnTapUp(vec2(posx, posy), touchNumber); } - void AppOnTapUpAfterMove(int posx, int posy) + void AppOnTapUpAfterMove(int posx, int posy, int touchNumber) { - App->OuterOnTapUpAfterMove(vec2(posx, posy)); + App->OuterOnTapUpAfterMove(vec2(posx, posy), touchNumber); } - void AppOnScroll(int shiftx, int shifty) + void AppOnScroll(int shiftx, int shifty, int touchNumber) { - App->OuterOnMove(vec2(shiftx, shifty)); + App->OuterOnMove(vec2(shiftx, shifty), touchNumber); } diff --git a/src/Utils/IosApi/ObjC/GLKViewTemplate.mm b/src/Utils/IosApi/ObjC/GLKViewTemplate.mm index 5d4275d..21df83e 100644 --- a/src/Utils/IosApi/ObjC/GLKViewTemplate.mm +++ b/src/Utils/IosApi/ObjC/GLKViewTemplate.mm @@ -11,6 +11,17 @@ #include +#include + +struct TTouchHashData +{ + CGPoint first; + bool second; + int number; +}; + +std::map touchHash; + namespace SE { void ShowKeyboard(std::string text); @@ -23,53 +34,116 @@ void ShowKeyboard(std::string text); self = [super initWithFrame:frame]; if (self) { - touchMoved = false; + self.multipleTouchEnabled = YES; } return self; } +- (void) AddTouchToHash:(UITouch*)touch +{ + touchHash[touch].first = [touch locationInView:self]; + touchHash[touch].second = false; + + for (int n = 0; n < 255; n++) + { + bool nExists = false; + + for (auto i = touchHash.begin(); i != touchHash.end(); ++i) + { + if (i->second.number == n) + { + nExists = true; + } + } + + if (nExists == false) + { + touchHash[touch].number = n; + return; + } + } +} + +- (void) RemoveTouchFromHash:(UITouch*)touch +{ + +} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - CGPoint location = [[[touches allObjects] objectAtIndex:0] locationInView:self]; + //NSLog(@"touchesBegan"); - touchBeganLocation = location; - - touchMoved = false; - - SE::AppOnTapDown(location.x, self.bounds.size.height - location.y); + for (NSUInteger i = 0; i < [[touches allObjects] count]; i++) + { + UITouch *aTouch = [[touches allObjects] objectAtIndex:i]; + + CGPoint location = [aTouch locationInView:self]; + + [self AddTouchToHash:aTouch]; + + SE::AppOnTapDown(location.x, self.bounds.size.height - location.y, touchHash[aTouch].number); + + } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - CGPoint location = [[[touches allObjects] objectAtIndex:0] locationInView:self]; - - CGPoint prevLocation = [[[touches allObjects] objectAtIndex:0] previousLocationInView:self]; - - if (abs(touchBeganLocation.x - location.x) > 10 || abs(touchBeganLocation.y - location.y) > 10) + for (NSUInteger i = 0; i < [[touches allObjects] count]; i++) { - touchMoved = true; - } + UITouch *aTouch = [[touches allObjects] objectAtIndex:i]; + + + + CGPoint location = [aTouch locationInView:self]; - SE::AppOnScroll(prevLocation.x - location.x, -(prevLocation.y - location.y)); + CGPoint prevLocation = [aTouch previousLocationInView:self]; + + if (abs(touchHash[aTouch].first.x - location.x) > 10 || abs(touchHash[aTouch].first.y - location.y) > 10) + { + touchHash[aTouch].second = true; + } + + SE::AppOnScroll(prevLocation.x - location.x, -(prevLocation.y - location.y), touchHash[aTouch].number); + } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { - CGPoint location = [[[touches allObjects] objectAtIndex:0] locationInView:self]; + //NSLog(@"touchesEnded"); - if (touchMoved) + for (NSUInteger i = 0; i < [[touches allObjects] count]; i++) { - SE::AppOnTapUpAfterMove(location.x, self.bounds.size.height - location.y); + UITouch *aTouch = [[touches allObjects] objectAtIndex:i]; + + CGPoint location = [aTouch locationInView:self]; + + if (touchHash[aTouch].second) + { + SE::AppOnTapUpAfterMove(location.x, self.bounds.size.height - location.y, touchHash[aTouch].number); } else { - SE::AppOnTapUp(location.x, self.bounds.size.height - location.y); + SE::AppOnTapUp(location.x, self.bounds.size.height - location.y, touchHash[aTouch].number); + } + touchHash.erase(aTouch); + } } +- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event +{ + //NSLog(@"touchesCancelled"); + + + for (NSUInteger i = 0; i < [[touches allObjects] count]; i++) + { + UITouch *aTouch = [[touches allObjects] objectAtIndex:i]; + + touchHash.erase(aTouch); + } +} @end diff --git a/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm b/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm index 868584d..8dfc5c7 100644 --- a/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm +++ b/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm @@ -65,8 +65,13 @@ void SetKeyboardText(const char* newText); UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(respondToPinch:)]; + + recognizer.delaysTouchesEnded = NO; + [self.view addGestureRecognizer:recognizer]; + self.view.multipleTouchEnabled = YES; + //Keyboard _hiddenTextField = [[UITextField alloc] initWithFrame:CGRectMake(-200,-200,0,0)];