Browse Source

Cleaner fix for console crashes: only collect messages when posted, and print during PostUpdate.
Slightly darker error text color.
Updated script API & wiki.

Lasse Öörni 12 years ago
parent
commit
2a9be0b23e
4 changed files with 43 additions and 48 deletions
  1. 1 1
      Bin/Data/UI/DefaultStyle.xml
  2. 13 12
      Docs/ScriptAPI.dox
  3. 25 33
      Source/Engine/Engine/Console.cpp
  4. 4 2
      Source/Engine/Engine/Console.h

+ 1 - 1
Bin/Data/UI/DefaultStyle.xml

@@ -180,7 +180,7 @@
     </element>
     <element type="ConsoleText" style="Text" auto="false" />
     <element type="ConsoleHighlightedText" style="Text" auto="false">
-        <attribute name="Color" value="1 0 0 1" />
+        <attribute name="Color" value="0.75 0 0 1" />
     </element>
     <element type="ConsoleLineEdit" style="LineEdit" auto="false">
         <attribute name="Top Left Color" value="0 0 0 0.5" />

+ 13 - 12
Docs/ScriptAPI.dox

@@ -59,8 +59,8 @@ namespace Urho3D
 - void PrintCallStack(bool arg0 = false)
 - String GetPath(const String&)
 - String GetFileName(const String&)
-- String GetExtension(const String&)
-- String GetFileNameAndExtension(const String&)
+- String GetExtension(const String&, bool arg1 = true)
+- String GetFileNameAndExtension(const String&, bool arg1 = false)
 - String ReplaceExtension(const String&, const String&)
 - String AddTrailingSlash(const String&)
 - String RemoveTrailingSlash(const String&)
@@ -279,17 +279,17 @@ Properties:<br>
 String
 
 Methods:<br>
-- void Replace(uint8, uint8)
-- void Replace(const String&, const String&)
-- String Replaced(uint8, uint8) const
-- String Replaced(const String&, const String&) const
+- void Replace(uint8, uint8, bool arg2 = true)
+- void Replace(const String&, const String&, bool arg2 = true)
+- String Replaced(uint8, uint8, bool arg2 = true) const
+- String Replaced(const String&, const String&, bool arg2 = true) const
 - void Resize(uint)
-- uint Find(const String&, uint arg1 = 0) const
-- uint Find(uint8, uint arg1 = 0) const
-- uint FindLast(const String&, uint arg1 = 0xffffffff) const
-- uint FindLast(uint8, uint arg1 = 0xffffffff) const
-- bool StartsWith(const String&) const
-- bool EndsWith(const String&) const
+- uint Find(const String&, uint arg1 = 0, bool arg2 = true) const
+- uint Find(uint8, uint arg1 = 0, bool arg2 = true) const
+- uint FindLast(const String&, uint arg1 = 0xffffffff, bool arg2 = true) const
+- uint FindLast(uint8, uint arg1 = 0xffffffff, bool arg2 = true) const
+- bool StartsWith(const String&, bool arg1 = true) const
+- bool EndsWith(const String&, bool arg1 = true) const
 - String Substring(uint) const
 - String Substring(uint, uint) const
 - String ToUpper() const
@@ -675,6 +675,7 @@ Methods:<br>
 - float HitDistance(const BoundingBox&) const
 - float HitDistance(const Frustum&, bool arg1 = true) const
 - float HitDistance(const Vector3&, const Vector3&, const Vector3&) const
+- Ray Transformed(const Matrix3x4&) const
 
 Properties:<br>
 - Vector3 origin

+ 25 - 33
Source/Engine/Engine/Console.cpp

@@ -23,6 +23,7 @@
 #include "Precompiled.h"
 #include "Console.h"
 #include "Context.h"
+#include "CoreEvents.h"
 #include "EngineEvents.h"
 #include "Font.h"
 #include "Graphics.h"
@@ -51,8 +52,7 @@ static const int DEFAULT_HISTORY_SIZE = 16;
 Console::Console(Context* context) :
     Object(context),
     historyRows_(DEFAULT_HISTORY_SIZE),
-    historyPosition_(0),
-    inLogMessage_(false)
+    historyPosition_(0)
 {
     UI* ui = GetSubsystem<UI>();
     UIElement* uiRoot = ui->GetRoot();
@@ -81,6 +81,8 @@ Console::Console(Context* context) :
     SubscribeToEvent(lineEdit_, E_TEXTFINISHED, HANDLER(Console, HandleTextFinished));
     SubscribeToEvent(lineEdit_, E_UNHANDLEDKEY, HANDLER(Console, HandleLineEditKey));
     SubscribeToEvent(E_SCREENMODE, HANDLER(Console, HandleScreenMode));
+    SubscribeToEvent(E_LOGMESSAGE, HANDLER(Console, HandleLogMessage));
+    SubscribeToEvent(E_POSTUPDATE, HANDLER(Console, HandlePostUpdate));
 }
 
 Console::~Console()
@@ -116,7 +118,9 @@ void Console::SetVisible(bool enable)
 
     lineEdit_->SetVisible(hasScriptSubsystem);
     background_->SetVisible(enable);
-
+    // Ensure the background has no empty space when shown without the lineedit
+    background_->SetHeight(background_->GetMinHeight());
+    
     if (enable && hasScriptSubsystem)
         GetSubsystem<UI>()->SetFocusElement(lineEdit_);
     else
@@ -154,9 +158,6 @@ void Console::SetNumRows(unsigned rows)
     rowContainer_->UpdateLayout();
 
     UpdateElements();
-
-    if (!HasSubscribedToEvent(E_LOGMESSAGE))
-        SubscribeToEvent(E_LOGMESSAGE, HANDLER(Console, HandleLogMessage));
 }
 
 void Console::SetNumHistoryRows(unsigned rows)
@@ -173,6 +174,7 @@ void Console::UpdateElements()
     int width = GetSubsystem<Graphics>()->GetWidth();
     const IntRect& border = background_->GetLayoutBorder();
     background_->SetFixedWidth(width);
+    background_->SetHeight(background_->GetMinHeight());
     rowContainer_->SetFixedWidth(width - border.left_ - border.right_);
     lineEdit_->SetFixedHeight(lineEdit_->GetTextElement()->GetRowHeight());
 }
@@ -268,46 +270,36 @@ void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
 
 void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
 {
-    // If we are recursing here, do not write the message
-    if (inLogMessage_ || !rowContainer_->GetNumChildren())
-        return;
-
-    inLogMessage_ = true;
-
     using namespace LogMessage;
 
     int level = eventData[P_LEVEL].GetInt();
+    // The message may be multi-line, so split to rows in that case
     Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
     
-    rowContainer_->DisableLayoutUpdate();
+    for (unsigned i = 0; i < rows.Size(); ++i)
+        pendingRows_.Push(MakePair(level, rows[i]));
+}
+
+void Console::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
+{
+    if (!rowContainer_->GetNumChildren())
+        return;
     
-    const Vector<SharedPtr<UIElement> >& children = rowContainer_->GetChildren();
+    rowContainer_->DisableLayoutUpdate();
     
-    for (unsigned i = 0; i < rows.Size(); ++i)
+    for (unsigned i = 0; i < pendingRows_.Size(); ++i)
     {
-        if (children.Size() > 1)
-        {
-            for (unsigned j = 0; j < children.Size() - 1; ++j)
-            {
-                // Scroll text and styles upward. Do not create or remove elements, as we could be in a UI internal function
-                // while a message is being logged
-                Text* current = StaticCast<Text>(children[j]);
-                Text* next = StaticCast<Text>(children[j + 1]);
-                current->SetStyle(next->GetAppliedStyle());
-                current->SetText(next->GetText());
-            }
-        }
-
-        Text* text = StaticCast<Text>(children.Back());
-        text->SetText(rows[i]);
+        rowContainer_->RemoveChildAtIndex(0);
+        Text* text = rowContainer_->CreateChild<Text>();
+        text->SetText(pendingRows_[i].second_);
         // Make error message highlight
-        text->SetStyle(level == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
+        text->SetStyle(pendingRows_[i].first_ == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
     }
     
+    pendingRows_.Clear();
+    
     rowContainer_->EnableLayoutUpdate();
     rowContainer_->UpdateLayout();
-
-    inLogMessage_ = false;
 }
 
 }

+ 4 - 2
Source/Engine/Engine/Console.h

@@ -85,6 +85,8 @@ private:
     void HandleScreenMode(StringHash eventType, VariantMap& eventData);
     /// Handle a log message.
     void HandleLogMessage(StringHash eventType, VariantMap& eventData);
+    /// Handle the application post-update.
+    void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
 
     /// Background.
     SharedPtr<BorderImage> background_;
@@ -94,14 +96,14 @@ private:
     SharedPtr<LineEdit> lineEdit_;
     /// Command history.
     Vector<String> history_;
+    /// Pending log message rows.
+    Vector<Pair<int, String> > pendingRows_;
     /// Current row being edited.
     String currentRow_;
     /// Command history maximum rows.
     unsigned historyRows_;
     /// Command history current position.
     unsigned historyPosition_;
-    /// Currently printing a log message flag.
-    bool inLogMessage_;
 };
 
 }