Browse Source

Minor fixes on C++ ConsoleInput sample class.

Yao Wei Tjong 姚伟忠 11 years ago
parent
commit
b5f9b52717

+ 1 - 1
Source/Samples/02_HelloGUI/HelloGUI.cpp

@@ -200,7 +200,7 @@ void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For
 
 
 void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
 void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
 {
 {
-    GetSubsystem<Engine>()->Exit();
+    engine_->Exit();
 }
 }
 
 
 void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
 void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)

+ 14 - 5
Source/Samples/26_ConsoleInput/ConsoleInput.cpp

@@ -28,7 +28,7 @@
 #include "Log.h"
 #include "Log.h"
 #include "ProcessUtils.h"
 #include "ProcessUtils.h"
 #include "Random.h"
 #include "Random.h"
-#include "Time.h"
+#include "Timer.h"
 
 
 #include "ConsoleInput.h"
 #include "ConsoleInput.h"
 
 
@@ -67,6 +67,9 @@ void ConsoleInput::Start()
     // Subscribe to console commands and the frame update
     // Subscribe to console commands and the frame update
     SubscribeToEvent(E_CONSOLECOMMAND, HANDLER(ConsoleInput, HandleConsoleCommand));
     SubscribeToEvent(E_CONSOLECOMMAND, HANDLER(ConsoleInput, HandleConsoleCommand));
     SubscribeToEvent(E_UPDATE, HANDLER(ConsoleInput, HandleUpdate));
     SubscribeToEvent(E_UPDATE, HANDLER(ConsoleInput, HandleUpdate));
+
+    // Subscribe key down event
+    SubscribeToEvent(E_KEYDOWN, HANDLER(ConsoleInput, HandleEscKeyDown));
     
     
     // Hide logo to make room for the console
     // Hide logo to make room for the console
     SetLogoVisible(false);
     SetLogoVisible(false);
@@ -102,6 +105,13 @@ void ConsoleInput::HandleUpdate(StringHash eventType, VariantMap& eventData)
         HandleInput(input);
         HandleInput(input);
 }
 }
 
 
+void ConsoleInput::HandleEscKeyDown(StringHash eventType, VariantMap& eventData)
+{
+    // Unlike the other samples, exiting the engine when ESC is pressed instead of just closing the console
+    if (eventData[KeyDown::P_KEY].GetInt() == KEY_ESC)
+        engine_->Exit();
+}
+
 void ConsoleInput::StartGame()
 void ConsoleInput::StartGame()
 {
 {
     Print("Welcome to the Urho adventure game! You are the newest fish in the tank; your\n"
     Print("Welcome to the Urho adventure game! You are the newest fish in the tank; your\n"
@@ -183,9 +193,8 @@ void ConsoleInput::HandleInput(const String& input)
     }
     }
     
     
     if (inputLower == "quit" || inputLower == "exit")
     if (inputLower == "quit" || inputLower == "exit")
-        GetSubsystem<Engine>()->Exit();
-    
-    if (gameOn_)
+        engine_->Exit();
+    else if (gameOn_)
     {
     {
         // Game is on
         // Game is on
         if (inputLower == "help")
         if (inputLower == "help")
@@ -245,7 +254,7 @@ void ConsoleInput::HandleInput(const String& input)
         if (inputLower[0] == 'y')
         if (inputLower[0] == 'y')
             StartGame();
             StartGame();
         else if (inputLower[0] == 'n')
         else if (inputLower[0] == 'n')
-            GetSubsystem<Engine>()->Exit();
+            engine_->Exit();
         else
         else
             Print("Please answer 'y' or 'n'.\n");
             Print("Please answer 'y' or 'n'.\n");
     }
     }

+ 2 - 0
Source/Samples/26_ConsoleInput/ConsoleInput.h

@@ -44,6 +44,8 @@ private:
     void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
     void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
     /// Handle frame update event.
     /// Handle frame update event.
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
+    /// Handle ESC key down event to quit the engine.
+    void HandleEscKeyDown(StringHash eventType, VariantMap& eventData);
     /// Print intro message and initialize the game state.
     /// Print intro message and initialize the game state.
     void StartGame();
     void StartGame();
     /// Print game over message.
     /// Print game over message.

+ 1 - 1
Source/Samples/CMakeLists.txt

@@ -63,4 +63,4 @@ endif ()
 add_subdirectory (23_Water)
 add_subdirectory (23_Water)
 add_subdirectory (24_Urho2DSprite)
 add_subdirectory (24_Urho2DSprite)
 add_subdirectory (25_Urho2DParticle)
 add_subdirectory (25_Urho2DParticle)
-add_subdirectory (26_ConsoleInput)
+add_subdirectory (26_ConsoleInput)

+ 3 - 3
Source/Samples/Sample.inl

@@ -141,10 +141,10 @@ void Sample::HandleKeyDown(StringHash eventType, VariantMap& eventData)
     if (key == KEY_ESC)
     if (key == KEY_ESC)
     {
     {
         Console* console = GetSubsystem<Console>();
         Console* console = GetSubsystem<Console>();
-        if (!console->IsVisible())
-            engine_->Exit();
-        else
+        if (console->IsVisible())
             console->SetVisible(false);
             console->SetVisible(false);
+        else
+            engine_->Exit();
     }
     }
 
 
     // Toggle console with F1
     // Toggle console with F1