Parcourir la source

Add engine param for enabling touch emulation during initialization.
Adjust sample base class to add screen joystick on desktop platform when touch emulation is enabled.

Yao Wei Tjong 姚伟忠 il y a 11 ans
Parent
commit
6ad8c14346

+ 1 - 0
Docs/GettingStarted.dox

@@ -242,6 +242,7 @@ The engine can be configured using the following command line options.
 -nosound     Disable sound output
 -noip        Disable sound mixing interpolation
 -sm2         Force SM2.0 rendering
+-touch       Touch emulation on desktop platform
 \endverbatim
 
 \section Running_Xcode_AngelScript_Info Mac OS X specific - How to view/edit AngelScript within Xcode

+ 1 - 0
Docs/Reference.dox

@@ -159,6 +159,7 @@ The full list of supported parameters, their datatypes and default values:
 - SoundMixRate (int) %Sound output frequency in Hz. Default 44100.
 - SoundStereo (bool) Stereo sound output mode. Default true.
 - SoundInterpolation (bool) Interpolated sound output mode to improve quality. Default true.
+- TouchEmulation (bool) %Touch emulation on desktop platform. Default false.
 
 \section MainLoop_Frame Main loop iteration
 

+ 6 - 0
Source/Engine/Engine/Engine.cpp

@@ -359,6 +359,10 @@ bool Engine::Initialize(const VariantMap& parameters)
     // Init FPU state of main thread
     InitFPU();
 
+    // Initialize input
+    if (HasParameter(parameters, "TouchEmulation"))
+        GetSubsystem<Input>()->SetTouchEmulation(GetParameter(parameters, "TouchEmulation").GetBool());
+
     #ifdef URHO3D_TESTING
     if (HasParameter(parameters, "TimeOut"))
         timeOut_ = GetParameter(parameters, "TimeOut", 0).GetInt() * 1000000LL;
@@ -795,6 +799,8 @@ VariantMap Engine::ParseParameters(const Vector<String>& arguments)
                 ret["TextureAnisotropy"] = ToInt(value);
                 ++i;
             }
+            else if (argument == "touch")
+                ret["TouchEmulation"] = true;
             #ifdef URHO3D_TESTING
             else if (argument == "timeout" && !value.Empty())
             {

+ 2 - 0
Source/Samples/Sample.h

@@ -94,6 +94,8 @@ private:
     void HandleKeyDown(StringHash eventType, VariantMap& eventData);
     /// Handle scene update event to control camera's pitch and yaw for all samples.
     void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
+    /// Handle touch begin event to initialize touch input on desktop platform.
+    void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
 
     /// Screen joystick index for navigational controls (mobile platforms only).
     unsigned screenJoystickIndex_;

+ 29 - 21
Source/Samples/Sample.inl

@@ -62,8 +62,12 @@ void Sample::Setup()
 
 void Sample::Start()
 {
-    // Initialize touch input on mobile platforms
-    InitTouchInput();
+    if (GetPlatform() == "Android" || GetPlatform() == "iOS")
+        // On mobile platform, enable touch by adding a screen joystick
+        InitTouchInput();
+    else if (GetSubsystem<Input>()->GetNumJoysticks() == 0)
+        // On desktop platform, do not detect touch when we already got a joystick
+        SubscribeToEvent(E_TOUCHBEGIN, HANDLER(Sample, HandleTouchBegin));
 
     // Create logo
     CreateLogo();
@@ -82,27 +86,24 @@ void Sample::Start()
 
 void Sample::InitTouchInput()
 {
-    if (GetPlatform() == "Android" || GetPlatform() == "iOS")
-    {
-        touchEnabled_ = true;
+    touchEnabled_ = true;
 
-        ResourceCache* cache = GetSubsystem<ResourceCache>();
-        Input* input = GetSubsystem<Input>();
-        XMLFile* layout = cache->GetResource<XMLFile>("UI/ScreenJoystick_Samples.xml");
-        const String& patchString = GetScreenJoystickPatchString();
-        if (!patchString.Empty())
-        {
-            // Patch the screen joystick layout further on demand
-            VectorBuffer buffer;
-            buffer.WriteString(patchString);
-            buffer.Seek(0);
-            SharedPtr<XMLFile> patchFile(new XMLFile(context_));
-            if (patchFile->Load(buffer))
-                layout->Patch(patchFile);
-        }
-        screenJoystickIndex_ = input->AddScreenJoystick(layout, cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
-        input->SetScreenJoystickVisible(screenJoystickSettingsIndex_, true);
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    Input* input = GetSubsystem<Input>();
+    XMLFile* layout = cache->GetResource<XMLFile>("UI/ScreenJoystick_Samples.xml");
+    const String& patchString = GetScreenJoystickPatchString();
+    if (!patchString.Empty())
+    {
+        // Patch the screen joystick layout further on demand
+        VectorBuffer buffer;
+        buffer.WriteString(patchString);
+        buffer.Seek(0);
+        SharedPtr<XMLFile> patchFile(new XMLFile(context_));
+        if (patchFile->Load(buffer))
+            layout->Patch(patchFile);
     }
+    screenJoystickIndex_ = input->AddScreenJoystick(layout, cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
+    input->SetScreenJoystickVisible(screenJoystickSettingsIndex_, true);
 }
 
 void Sample::SetLogoVisible(bool enable)
@@ -326,3 +327,10 @@ void Sample::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
         }
     }
 }
+
+void Sample::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
+{
+    // On some platforms like Windows the presence of touch input can only be detected dynamically
+    InitTouchInput();
+    UnsubscribeFromEvent("TouchBegin");
+}

+ 1 - 0
Source/Tools/Urho3DPlayer/Urho3DPlayer.cpp

@@ -113,6 +113,7 @@ void Urho3DPlayer::Setup()
             "-nosound     Disable sound output\n"
             "-noip        Disable sound mixing interpolation\n"
             "-sm2         Force SM2.0 rendering\n"
+            "-touch       Touch emulation on desktop platform\n"
             #endif
         );
     }