From 9a27aacbab1fe08b096e0702202332b8e50b2085 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 15 Jul 2013 18:44:22 +0000 Subject: [PATCH] ios keyboard stuff --- .../Salmon Engine.xcodeproj/project.pbxproj | 8 ++++ include/GUIManager/ButtonWidget.h | 2 + include/GUIManager/GUIManager.h | 3 +- include/Utils/IosApi/IosApi.h | 3 ++ .../IosApi/ObjC/ViewControllerTemplate.h | 5 +- src/GUIManager/ButtonWidget.cpp | 19 +++++++- src/GUIManager/GUIManager.cpp | 17 +++++-- src/Utils/IosApi/IosApi.mm | 23 +++++++++ src/Utils/IosApi/ObjC/GLKViewTemplate.mm | 10 ++-- .../IosApi/ObjC/ViewControllerTemplate.mm | 48 ++++++++++++++++++- 10 files changed, 128 insertions(+), 10 deletions(-) diff --git a/iOS/Salmon Engine/Salmon Engine.xcodeproj/project.pbxproj b/iOS/Salmon Engine/Salmon Engine.xcodeproj/project.pbxproj index 3c1f6fd..79bcff6 100644 --- a/iOS/Salmon Engine/Salmon Engine.xcodeproj/project.pbxproj +++ b/iOS/Salmon Engine/Salmon Engine.xcodeproj/project.pbxproj @@ -83,6 +83,8 @@ 4CD41A2A16C2D23C0071E598 /* ViewControllerTemplate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4CD41A2716C2D23C0071E598 /* ViewControllerTemplate.h */; }; 4CD41A3116C2D2490071E598 /* GLKViewTemplate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4CD41A2E16C2D2490071E598 /* GLKViewTemplate.mm */; }; 4CD41A3216C2D2490071E598 /* ViewControllerTemplate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4CD41A2F16C2D2490071E598 /* ViewControllerTemplate.mm */; }; + 4CDAB6F8176A60620075FC65 /* KeyboardWidget.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CDAB6F7176A60620075FC65 /* KeyboardWidget.cpp */; }; + 4CDAB6FA176A60750075FC65 /* KeyboardWidget.h in Headers */ = {isa = PBXBuildFile; fileRef = 4CDAB6F9176A60750075FC65 /* KeyboardWidget.h */; }; 4CEE2DD215CDC55B00C4D5F5 /* SalmonRenderGLES20.h in Headers */ = {isa = PBXBuildFile; fileRef = 4CEE2DD115CDC55B00C4D5F5 /* SalmonRenderGLES20.h */; }; 4CEE2DD515CDC57200C4D5F5 /* SalmonRenderGLES20.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2DD415CDC57200C4D5F5 /* SalmonRenderGLES20.cpp */; }; 4CF8747916B65210009B1214 /* ButtonWidget.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CF8747616B65210009B1214 /* ButtonWidget.cpp */; }; @@ -178,6 +180,8 @@ 4CD41A2716C2D23C0071E598 /* ViewControllerTemplate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewControllerTemplate.h; sourceTree = ""; }; 4CD41A2E16C2D2490071E598 /* GLKViewTemplate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GLKViewTemplate.mm; sourceTree = ""; }; 4CD41A2F16C2D2490071E598 /* ViewControllerTemplate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ViewControllerTemplate.mm; sourceTree = ""; }; + 4CDAB6F7176A60620075FC65 /* KeyboardWidget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KeyboardWidget.cpp; path = ../../../src/GUIManager/KeyboardWidget.cpp; sourceTree = ""; }; + 4CDAB6F9176A60750075FC65 /* KeyboardWidget.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KeyboardWidget.h; path = ../../../include/GUIManager/KeyboardWidget.h; sourceTree = ""; }; 4CEE2DD115CDC55B00C4D5F5 /* SalmonRenderGLES20.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SalmonRenderGLES20.h; path = ../../../include/Render/SalmonRender/SalmonRenderGLES20.h; sourceTree = ""; }; 4CEE2DD415CDC57200C4D5F5 /* SalmonRenderGLES20.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SalmonRenderGLES20.cpp; path = ../../../src/Render/SalmonRender/SalmonRenderGLES20.cpp; sourceTree = ""; }; 4CF8747616B65210009B1214 /* ButtonWidget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ButtonWidget.cpp; path = ../../../src/GUIManager/ButtonWidget.cpp; sourceTree = ""; }; @@ -744,6 +748,7 @@ 4CF8747416B65183009B1214 /* GUIManager */ = { isa = PBXGroup; children = ( + 4CDAB6F7176A60620075FC65 /* KeyboardWidget.cpp */, 4CF8747616B65210009B1214 /* ButtonWidget.cpp */, 4CF8747716B65210009B1214 /* GUIManager.cpp */, 4CF8747816B65210009B1214 /* WidgetXmlParsers.cpp */, @@ -770,6 +775,7 @@ 4CF8748216B65249009B1214 /* GUIManager */ = { isa = PBXGroup; children = ( + 4CDAB6F9176A60750075FC65 /* KeyboardWidget.h */, 4CF8748716B652A4009B1214 /* ButtonWidget.h */, 4CF8748816B652A4009B1214 /* GUIManager.h */, 4CF8748916B652A4009B1214 /* WidgetTemplatesImpl.h */, @@ -834,6 +840,7 @@ 4CD41A2A16C2D23C0071E598 /* ViewControllerTemplate.h in Headers */, 4C8024B516D4BD9F001A4521 /* Network.h in Headers */, 4C8024B616D4BD9F001A4521 /* SignalSender.h in Headers */, + 4CDAB6FA176A60750075FC65 /* KeyboardWidget.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -928,6 +935,7 @@ 4C8024BA16D4BDBC001A4521 /* Network.cpp in Sources */, 4C470853175405220069C707 /* JpegHelper.cpp in Sources */, 4C470854175405220069C707 /* TgaLoader.cpp in Sources */, + 4CDAB6F8176A60620075FC65 /* KeyboardWidget.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/include/GUIManager/ButtonWidget.h b/include/GUIManager/ButtonWidget.h index 75cceff..586c216 100644 --- a/include/GUIManager/ButtonWidget.h +++ b/include/GUIManager/ButtonWidget.h @@ -208,6 +208,8 @@ public: virtual void OnTapDown(vec2 pos); void SetText(const std::string& newText); + void OnSetAllText(std::string newText); //For iOS only + std::string GetText(); static TEdit* CreateEdit(vec2 posFrom, vec2 posTo); static TEdit* CreateEditWithFiller(vec2 posFrom, vec2 posTo, boost::function fillerFunc); diff --git a/include/GUIManager/GUIManager.h b/include/GUIManager/GUIManager.h index ee076cd..8a6ab52 100644 --- a/include/GUIManager/GUIManager.h +++ b/include/GUIManager/GUIManager.h @@ -142,6 +142,7 @@ protected: public: boost::signal KeyPressedSignal; + boost::signal SetTextSignal; //boost::signal PostUpdateSignal; TGUIManager(); @@ -170,7 +171,7 @@ public: void OnMove(vec2 shift); - void ShowKeyboard(); + void ShowKeyboard(const std::string text = ""); void HideKeyboard(); diff --git a/include/Utils/IosApi/IosApi.h b/include/Utils/IosApi/IosApi.h index 7905aa6..3290e38 100644 --- a/include/Utils/IosApi/IosApi.h +++ b/include/Utils/IosApi/IosApi.h @@ -21,6 +21,9 @@ std::string IosGetFileReadPath(const std::string& filename); std::string IosGetFilePathUserData(const std::string& filename); void IosSwitchToScreen(); + +void ShowKeyboard(std::string text); +void HideKeyboard(); } //namespace SE diff --git a/include/Utils/IosApi/ObjC/ViewControllerTemplate.h b/include/Utils/IosApi/ObjC/ViewControllerTemplate.h index 18bd45b..381498f 100644 --- a/include/Utils/IosApi/ObjC/ViewControllerTemplate.h +++ b/include/Utils/IosApi/ObjC/ViewControllerTemplate.h @@ -9,8 +9,11 @@ #import #import -@interface ViewControllerTemplate : GLKViewController +@interface ViewControllerTemplate : GLKViewController +{ +} +@property (nonatomic, readonly) UITextField* hiddenTextField; //for keyboard - (void) appInitCaller; diff --git a/src/GUIManager/ButtonWidget.cpp b/src/GUIManager/ButtonWidget.cpp index a168f78..cf9585a 100644 --- a/src/GUIManager/ButtonWidget.cpp +++ b/src/GUIManager/ButtonWidget.cpp @@ -643,7 +643,8 @@ void TEdit::OnTapDown(vec2 pos) } - ResourceManager->GUIManager.ShowKeyboard(); + ResourceManager->GUIManager.ShowKeyboard(GetText()); + ResourceManager->GUIManager.SetTextSignal.connect(boost::bind(&TEdit::OnSetAllText, this, _1)); } void TEdit::OnKeyPress(int key) @@ -676,6 +677,22 @@ void TEdit::SetText(const std::string& newText) RefreshTextTriangleList(); } } + +void TEdit::OnSetAllText(std::string newText) +{ + if (newText != TextParams.Text) + { + TextParams.Text = newText; + + RefreshTextTriangleList(); + } +} + + +std::string TEdit::GetText() +{ + return TextParams.Text; +} //============================ Check Box ============================ diff --git a/src/GUIManager/GUIManager.cpp b/src/GUIManager/GUIManager.cpp index f46d518..5de1d9d 100644 --- a/src/GUIManager/GUIManager.cpp +++ b/src/GUIManager/GUIManager.cpp @@ -7,6 +7,13 @@ 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"; + + +void GuiManagerSetKeyboardText(std::string newText) +{ + ResourceManager->GUIManager.SetTextSignal(newText); +} + TGUIManager::TGUIManager() @@ -371,23 +378,27 @@ void TGUIManager::OnMove(vec2 shift) } -void TGUIManager::ShowKeyboard() +void TGUIManager::ShowKeyboard(const std::string text) { + SE::ShowKeyboard(text); + /* if (!KeyboardIsOnScreen) { MoveWidget("Keyboard", vec2(0, 216)); KeyboardIsOnScreen = true; - } + }*/ } void TGUIManager::HideKeyboard() { + ResourceManager->GUIManager.SetTextSignal.disconnect_all_slots(); + /* if (KeyboardIsOnScreen) { MoveWidget("Keyboard", vec2(0, -216)); KeyboardIsOnScreen = false; - } + }*/ } void TGUIManager::PrintWidgetList() diff --git a/src/Utils/IosApi/IosApi.mm b/src/Utils/IosApi/IosApi.mm index bb2526f..1658462 100644 --- a/src/Utils/IosApi/IosApi.mm +++ b/src/Utils/IosApi/IosApi.mm @@ -8,11 +8,15 @@ #import #import +#import #include "include/Utils/Utils.h" GLKView* defaultView = 0; //Need to be set up by App! +UITextField* extKeyboardTextView = 0; + + namespace SE { @@ -58,6 +62,25 @@ void IosSwitchToScreen() { [defaultView bindDrawable]; } + +void ShowKeyboard(std::string text) +{ + extKeyboardTextView.text = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding]; + [extKeyboardTextView becomeFirstResponder]; +} + +void HideKeyboard() +{ + [extKeyboardTextView resignFirstResponder]; +} + +void GuiManagerSetKeyboardText(std::string newText); //Find it in GUIManager.cpp + +void SetKeyboardText(const char* newText) +{ + GuiManagerSetKeyboardText(newText); + NSLog(@"text: %s", newText); +} } //namespace SE diff --git a/src/Utils/IosApi/ObjC/GLKViewTemplate.mm b/src/Utils/IosApi/ObjC/GLKViewTemplate.mm index 41247d0..5d4275d 100644 --- a/src/Utils/IosApi/ObjC/GLKViewTemplate.mm +++ b/src/Utils/IosApi/ObjC/GLKViewTemplate.mm @@ -9,6 +9,13 @@ #import "include/Utils/IosApi/ObjC/GLKViewTemplate.h" #include "include/Utils/IosApi/IosWrapper.h" + +#include +namespace SE +{ +void ShowKeyboard(std::string text); +} + @implementation GLKViewTemplate - (id)initWithFrame:(CGRect)frame @@ -16,7 +23,6 @@ self = [super initWithFrame:frame]; if (self) { - touchMoved = false; } @@ -66,6 +72,4 @@ } - - @end diff --git a/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm b/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm index 9593e54..3e10b49 100644 --- a/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm +++ b/src/Utils/IosApi/ObjC/ViewControllerTemplate.mm @@ -12,6 +12,15 @@ extern GLKView* defaultView; //Find this in IosApi.mm +extern UITextField* extKeyboardTextView; //Find this in IosApi.mm + +namespace SE +{ + +void SetKeyboardText(const char* newText); + + +} @interface ViewControllerTemplate () { } @@ -28,6 +37,8 @@ extern GLKView* defaultView; //Find this in IosApi.mm @synthesize context = _context; @synthesize effect = _effect; +@synthesize hiddenTextField = _hiddenTextField; + - (void)viewDidLoad { @@ -55,6 +66,19 @@ extern GLKView* defaultView; //Find this in IosApi.mm initWithTarget:self action:@selector(respondToPinch:)]; [self.view addGestureRecognizer:recognizer]; + + //Keyboard + + _hiddenTextField = [[UITextField alloc] initWithFrame:CGRectMake(-200,-200,0,0)]; + + [self.view addSubview:_hiddenTextField]; + + extKeyboardTextView = _hiddenTextField; + + _hiddenTextField.delegate = self; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveKeyboardNotification:) name:UITextFieldTextDidChangeNotification object:nil]; + } - (void)viewDidUnload @@ -67,6 +91,8 @@ extern GLKView* defaultView; //Find this in IosApi.mm [EAGLContext setCurrentContext:nil]; } self.context = nil; + + _hiddenTextField = nil; } - (void)didReceiveMemoryWarning @@ -99,6 +125,26 @@ extern GLKView* defaultView; //Find this in IosApi.mm SE::AppOnScale(gestureRecognizer.scale); } +- (void) onReceiveKeyboardNotification:(NSNotification*) notification +{ + if (notification.name == UITextFieldTextDidChangeNotification) + { + UITextField* textField = notification.object; + const char* text = [[textField text] UTF8String]; + SE::SetKeyboardText(text); + } +} + +- (void)textFieldDidBeginEditing:(UITextField *)textField +{ + NSLog(@"Begin"); +} + +- (void)textFieldDidEndEditing:(UITextField *)textField +{ + NSLog(@"End"); +} + #pragma mark - GLKView and GLKViewController delegate methods @@ -115,5 +161,5 @@ extern GLKView* defaultView; //Find this in IosApi.mm } - @end +