浏览代码

Made the debug console manually toggleable.
Registered convenience forms of print() to script (print int / float / bool.)

Lasse Öörni 15 年之前
父节点
当前提交
21e87d6a1b
共有 5 个文件被更改,包括 49 次插入62 次删除
  1. 14 15
      Bin/Data/Scripts/NinjaSnowWar.as
  2. 13 32
      Engine/Engine/Console.cpp
  3. 3 13
      Engine/Engine/Console.h
  4. 18 0
      Engine/Engine/RegisterCommon.cpp
  5. 1 2
      Engine/Engine/RegisterEngine.cpp

+ 14 - 15
Bin/Data/Scripts/NinjaSnowWar.as

@@ -55,7 +55,6 @@ void initConsole()
     Console@ console = engine.createConsole();
     console.setNumRows(16);
     console.setFont(cache.getResource("Font", "cour.ttf"), 12);
-    console.setToggleKey(220);
     BorderImage@ cursor = console.getLineEditElement().getCursorElement();
     cursor.setWidth(4);
     cursor.setTexture(cache.getResource("Texture2D", "Textures/UI.png"));
@@ -142,20 +141,20 @@ void spawnPlayer()
 
 void handleUpdate(StringHash eventType, VariantMap& eventData)
 {
-    if (!console.isVisible())
+    if (input.getKeyPress(220))
+        console.toggle();
+    if (input.getKeyPress(KEY_F1))
+        debugHud.toggleAll();
+    if (input.getKeyPress(KEY_F2))
+        engine.setDebugDrawMode(engine.getDebugDrawMode() ^ DEBUGDRAW_PHYSICS);
+
+    if ((!console.isVisible()) && (input.getKeyPress('P')))
     {
-        if (input.getKeyPress(KEY_F1))
-            debugHud.toggleAll();
-        if (input.getKeyPress(KEY_F2))
-            engine.setDebugDrawMode(engine.getDebugDrawMode() ^ DEBUGDRAW_PHYSICS);
-        if (input.getKeyPress('P'))
-        {
-            paused = !paused;
-            if (paused)
-                messageText.setText("PAUSED");
-            else
-                messageText.setText("");
-        }
+        paused = !paused;
+        if (paused)
+            messageText.setText("PAUSED");
+        else
+            messageText.setText("");
     }
 
     if (!paused)
@@ -186,7 +185,7 @@ void updateControls()
         if (input.getKeyDown(' '))
             playerControls.set(CTRL_JUMP, true);
     }
-    
+
     if (input.getMouseButtonDown(MOUSEB_LEFT))
         playerControls.set(CTRL_FIRE, true);
     if (input.getMouseButtonDown(MOUSEB_RIGHT))

+ 13 - 32
Engine/Engine/Console.cpp

@@ -37,9 +37,7 @@
 
 Console::Console(Engine* engine) :
     mEngine(engine),
-    mFontSize(1),
-    mToggleKey(0),
-    mFocus(false)
+    mFontSize(1)
 {
     LOGINFO("Console created");
     
@@ -56,8 +54,8 @@ Console::Console(Engine* engine) :
     
     mBackground = new BorderImage();
     mBackground->setWidth(uiRoot->getWidth());
-    mBackground->setColor(C_TOPLEFT, Color(0.25f, 0.25f, 0.75f, 0.5f));
-    mBackground->setColor(C_TOPRIGHT, Color(0.25f, 0.25f, 0.75f, 0.5f));
+    mBackground->setColor(C_TOPLEFT, Color(0.25f, 0.25f, 0.5f, 0.5f));
+    mBackground->setColor(C_TOPRIGHT, Color(0.25f, 0.25f, 0.5f, 0.5f));
     mBackground->setColor(C_BOTTOMLEFT, Color(0.5f, 0.5f, 1.0f, 0.5f));
     mBackground->setColor(C_BOTTOMRIGHT, Color(0.5f, 0.5f, 1.0f, 0.5f));
     mBackground->setEnabled(true);
@@ -66,7 +64,7 @@ Console::Console(Engine* engine) :
     
     mLineEdit = new LineEdit();
     mLineEdit->setWidth(uiRoot->getWidth() - 8);
-    mLineEdit->setColor(Color(0.0f, 0.0f, 0.0f, 0.5f));
+    mLineEdit->setColor(Color(0.0f, 0.0f, 0.25f, 0.5f));
     mLineEdit->setDefocusable(false);
     mBackground->addChild(mLineEdit);
     
@@ -74,8 +72,6 @@ Console::Console(Engine* engine) :
     
     updateElements();
     
-    subscribeToEvent(EVENT_UPDATE, EVENT_HANDLER(Console, handleUpdate));
-    subscribeToEvent(EVENT_KEYDOWN, EVENT_HANDLER(Console, handleKeyDown));
     subscribeToEvent(EVENT_TEXTFINISHED, EVENT_HANDLER(Console, handleTextFinished));
 }
 
@@ -119,9 +115,17 @@ void Console::setVisible(bool enable)
     
     mBackground->setVisible(enable);
     if (enable)
-        mFocus = true;
+        mEngine->getUI()->setFocusElement(mLineEdit);
     else
+    {
         mLineEdit->setFocus(false);
+        mLineEdit->setText(std::string());
+    }
+}
+
+void Console::toggle()
+{
+    setVisible(!isVisible());
 }
 
 void Console::setNumRows(unsigned rows)
@@ -156,11 +160,6 @@ void Console::setFont(Font* font, int size)
     updateElements();
 }
 
-void Console::setToggleKey(int key)
-{
-    mToggleKey = key;
-}
-
 bool Console::isVisible() const
 {
     if (!mBackground)
@@ -191,24 +190,6 @@ void Console::updateElements()
         mBackground->setWidth(maxWidth);
 }
 
-void Console::handleUpdate(StringHash eventType, VariantMap& eventData)
-{
-    // Focus now if console has been made visible
-    if (mFocus)
-    {
-        mEngine->getUI()->setFocusElement(mLineEdit);
-        mFocus = false;
-    }
-}
-
-void Console::handleKeyDown(StringHash eventType, VariantMap& eventData)
-{
-    using namespace KeyDown;
-    
-    if (eventData[P_KEY].getInt() == mToggleKey)
-        setVisible(!isVisible());
-}
-
 void Console::handleTextFinished(StringHash eventType, VariantMap& eventData)
 {
     using namespace TextFinished;

+ 3 - 13
Engine/Engine/Console.h

@@ -46,21 +46,19 @@ public:
     //! Write a log message
     virtual void write(const std::string& message);
     
-    //! Show/hide. Showing automatically focuses the line edit
+    //! Show or hide. Showing automatically focuses the line edit
     void setVisible(bool enable);
+    //! Toggle visibility
+    void toggle();
     //! Set number of rows
     void setNumRows(unsigned rows);
     //! Set font to use
     void setFont(Font* font, int size);
-    //! Set key for toggling
-    void setToggleKey(int key);
     
     //! Return whether is visible
     bool isVisible() const;
     //! Return number of rows
     unsigned getNumRows() const { return mRows.size(); }
-    //! Return key for toggling
-    int getToggleKey() const { return mToggleKey; }
     //! Return background element
     BorderImage* getBackgroundElement() const { return mBackground; }
     //! Return line edit element
@@ -69,10 +67,6 @@ public:
 private:
     //! Update layout
     void updateElements();
-    //! Handle update
-    void handleUpdate(StringHash eventType, VariantMap& eventData);
-    //! Handle key press
-    void handleKeyDown(StringHash eventType, VariantMap& eventData);
     //! Handle enter pressed on the line edit
     void handleTextFinished(StringHash eventType, VariantMap& eventData);
     
@@ -88,10 +82,6 @@ private:
     SharedPtr<LineEdit> mLineEdit;
     //! Font size
     int mFontSize;
-    //! Key for toggling
-    int mToggleKey;
-    //! Focus flag
-    bool mFocus;
 };
 
 #endif // ENGINE_CONSOLE_H

+ 18 - 0
Engine/Engine/RegisterCommon.cpp

@@ -80,6 +80,21 @@ static void Print(const std::string& str)
     LOGRAW(str + "\n");
 }
 
+static void PrintInt(int value)
+{
+    LOGRAW(toString(value) + "\n");
+}
+
+static void PrintFloat(float value)
+{
+    LOGRAW(toString(value) + "\n");
+}
+
+static void PrintBool(bool value)
+{
+    LOGRAW(toString(value) + "\n");
+}
+
 static void LogDebug(const std::string& str)
 {
     LOGDEBUG(str);
@@ -103,6 +118,9 @@ static void LogError(const std::string& str)
 static void registerLog(asIScriptEngine* engine)
 {
     engine->RegisterGlobalFunction("void print(const string& in)", asFUNCTION(Print), asCALL_CDECL);
+    engine->RegisterGlobalFunction("void print(int)", asFUNCTION(PrintInt), asCALL_CDECL);
+    engine->RegisterGlobalFunction("void print(float)", asFUNCTION(PrintFloat), asCALL_CDECL);
+    engine->RegisterGlobalFunction("void print(bool)", asFUNCTION(PrintBool), asCALL_CDECL);
     engine->RegisterGlobalFunction("void logDebug(const string& in)", asFUNCTION(LogDebug), asCALL_CDECL);
     engine->RegisterGlobalFunction("void logInfo(const string& in)", asFUNCTION(LogInfo), asCALL_CDECL);
     engine->RegisterGlobalFunction("void logWarning(const string& in)", asFUNCTION(LogWarning), asCALL_CDECL);

+ 1 - 2
Engine/Engine/RegisterEngine.cpp

@@ -356,12 +356,11 @@ static void registerConsole(asIScriptEngine* engine)
     engine->RegisterObjectBehaviour("Console", asBEHAVE_ADDREF, "void f()", asMETHOD(Console, addRef), asCALL_THISCALL);
     engine->RegisterObjectBehaviour("Console", asBEHAVE_RELEASE, "void f()", asMETHOD(Console, releaseRef), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void setVisible(bool)", asMETHOD(Console, setVisible), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Console", "void toggle()", asMETHOD(Console, toggle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void setNumRows(uint)", asMETHOD(Console, setNumRows), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void setFont(Font@+, int)", asMETHOD(Console, setFont), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Console", "void setToggleKey(int)", asMETHOD(Console, setToggleKey), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "bool isVisible() const", asMETHOD(Console, isVisible), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "uint getNumRows() const", asMETHOD(Console, getNumRows), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Console", "int getToggleKey() const", asMETHOD(Console, getToggleKey), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "BorderImage@+ getBackgroundElement() const", asMETHOD(Console, getBackgroundElement), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "LineEdit@+ getLineEditElement() const", asMETHOD(Console, getLineEditElement), asCALL_THISCALL);