Browse Source

Avoid hardcoding of highlighted console text style. Console subscribes to handle log message only when it itself is ready to do so. Removed redundant row container vector in the Console class. Hide the Console's linedit and preventing it to gain focus when there is no AngelScript or LuaScript subsystem to handle the E_CONSOLECOMMAND event. Fixed UIElement to send E_ELEMENTREMOVED event for RemoveChildAtIndex() method. Fixed Sample.lua to handle the ESC key to dismiss the Console when it is visible.

Wei Tjong Yao 12 years ago
parent
commit
6bef92973d

+ 2 - 3
Bin/Data/LuaScripts/Utilities/Sample.lua

@@ -82,12 +82,11 @@ function HandleKeyDown(eventType, eventData)
     local key = eventData:GetInt("Key")
     -- Close console (if open) or exit when ESC is pressed
     if key == KEY_ESC then
-        local ui = GetUI()
-        if ui:GetFocusElement() == nil then
+        local console = GetConsole()
+        if not console:IsVisible() then
             local engine = GetEngine()
             engine:Exit()
         else
-            local console = GetConsole()
             console:SetVisible(false)
         end
 

+ 3 - 0
Bin/Data/UI/DefaultStyle.xml

@@ -179,6 +179,9 @@
         <attribute name="Layout Border" value="4 4 4 4" />
     </element>
     <element type="ConsoleText" style="Text" auto="false" />
+    <element type="ConsoleHighlightedText" style="Text" auto="false">
+        <attribute name="Color" value="1 0 0 1" />
+    </element>
     <element type="ConsoleLineEdit" style="LineEdit" auto="false">
         <attribute name="Top Left Color" value="0 0 0 0.5" />
         <attribute name="Top Right Color" value="0 0 0 0.5" />

+ 4 - 2
Source/Engine/Engine/CMakeLists.txt

@@ -29,8 +29,10 @@ file (GLOB H_FILES *.h)
 set (SOURCE_FILES ${CPP_FILES} ${H_FILES})
 
 # Define dependency libs
-set (LIBS ../Audio ../Container ../Core ../Graphics ../Input ../IO ../Math ../Navigation ../Network ../Physics ../Resource 
-    ../Scene ../UI)
+set (LIBS ../Audio ../Container ../Core ../Graphics ../Input ../IO ../Math ../Navigation ../Network ../Physics ../Resource ../Scene ../Script ../UI)
+if (ENABLE_LUA)
+    set (LIBS ${LIBS} ../../Extras/LuaScript)
+endif ()
 set (INCLUDE_DIRS_ONLY ../../ThirdParty/Bullet/src ../../ThirdParty/kNet/include)
 
 # Setup target

+ 50 - 18
Source/Engine/Engine/Console.cpp

@@ -31,7 +31,11 @@
 #include "IOEvents.h"
 #include "LineEdit.h"
 #include "Log.h"
+#ifdef ENABLE_LUA
+#include "LuaScript.h"
+#endif
 #include "ResourceCache.h"
+#include "Script.h"
 #include "Text.h"
 #include "UI.h"
 #include "UIEvents.h"
@@ -77,7 +81,6 @@ 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));
 }
 
 Console::~Console()
@@ -93,8 +96,9 @@ void Console::SetDefaultStyle(XMLFile* style)
     background_->SetDefaultStyle(style);
     background_->SetStyle("ConsoleBackground");
 
-    for (unsigned i = 0; i < rows_.Size(); ++i)
-        rows_[i]->SetStyle("ConsoleText");
+    const Vector<SharedPtr<UIElement> >& children = rowContainer_->GetChildren();
+    for (unsigned i = 0; i < children.Size(); ++i)
+        children[i]->SetStyle("ConsoleText");
 
     lineEdit_->SetStyle("ConsoleLineEdit");
 
@@ -103,8 +107,17 @@ void Console::SetDefaultStyle(XMLFile* style)
 
 void Console::SetVisible(bool enable)
 {
+    // Check if we have script subsystem
+    bool hasScriptSubsystem = GetSubsystem<Script>();
+    #ifdef ENABLE_LUA
+    if (!hasScriptSubsystem)
+        hasScriptSubsystem = GetSubsystem<LuaScript>();
+    #endif
+
+    lineEdit_->SetVisible(hasScriptSubsystem);
     background_->SetVisible(enable);
-    if (enable)
+
+    if (enable && hasScriptSubsystem)
         GetSubsystem<UI>()->SetFocusElement(lineEdit_);
     else
         lineEdit_->SetFocus(false);
@@ -120,18 +133,27 @@ void Console::SetNumRows(unsigned rows)
     if (!rows)
         return;
 
-    rowContainer_->RemoveAllChildren();
+    rowContainer_->DisableLayoutUpdate();
 
-    rows_.Resize(rows);
-    for (unsigned i = 0; i < rows_.Size(); ++i)
+    int delta = rowContainer_->GetNumChildren() - rows;
+    if (delta > 0)
+    {
+        for (int i = 0; i < delta; ++i)
+            rowContainer_->RemoveChildAtIndex(0);
+    }
+    else
     {
-        if (!rows_[i])
-            rows_[i] = new Text(context_);
-        rowContainer_->AddChild(rows_[i]);
-        rows_[i]->SetStyle("ConsoleText");
+        for (int i = 0; i > delta; --i)
+            rowContainer_->CreateChild<Text>();
     }
 
+    rowContainer_->EnableLayoutUpdate();
+    rowContainer_->UpdateLayout();
+
     UpdateElements();
+
+    if (!HasSubscribedToEvent(E_LOGMESSAGE))
+        SubscribeToEvent(E_LOGMESSAGE, HANDLER(Console, HandleLogMessage));
 }
 
 void Console::SetNumHistoryRows(unsigned rows)
@@ -162,6 +184,11 @@ bool Console::IsVisible() const
     return background_ ? background_->IsVisible() : false;
 }
 
+unsigned Console::GetNumRows() const
+{
+    rowContainer_->GetNumChildren();
+}
+
 const String& Console::GetHistoryRow(unsigned index) const
 {
     return index < history_.Size() ? history_[index] : String::EMPTY;
@@ -238,12 +265,14 @@ void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
 
 void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
 {
-    // If the rows are not fully initialized yet, or we are recursing here, do not write the message
-    if (inLogMessage_ || rows_.Empty() || !rows_.Back())
+    // If we are recursing here, do not write the message
+    if (inLogMessage_)
         return;
 
     inLogMessage_ = true;
 
+    rowContainer_->DisableLayoutUpdate();
+
     using namespace LogMessage;
 
     int level = eventData[P_LEVEL].GetInt();
@@ -252,15 +281,18 @@ void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
     Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
     for (unsigned i = 0; i < rows.Size(); ++i)
     {
-        // Remove the first row, change its text and re-add to the bottom
-        Text* text = static_cast<Text*>(rowContainer_->GetChild(0));
-        rowContainer_->RemoveChild(text);
+        // Remove the first row
+        rowContainer_->RemoveChildAtIndex(0);
+        // Create a new row at the bottom
+        Text* text = rowContainer_->CreateChild<Text>();
         text->SetText(rows[i]);
         // Make error message highlight
-        text->SetColor(level == LOG_ERROR ? Color::RED : Color::WHITE);
-        rowContainer_->AddChild(text);
+        text->SetStyle(level == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
     }
 
+    rowContainer_->EnableLayoutUpdate();
+    rowContainer_->UpdateLayout();
+
     inLogMessage_ = false;
 }
 

+ 1 - 3
Source/Engine/Engine/Console.h

@@ -68,7 +68,7 @@ public:
     /// Return whether is visible.
     bool IsVisible() const;
     /// Return number of displayed rows.
-    unsigned GetNumRows() const { return rows_.Size(); }
+    unsigned GetNumRows() const;
     /// Return history maximum size.
     unsigned GetNumHistoryRows() const { return historyRows_; }
     /// Return current history position.
@@ -90,8 +90,6 @@ private:
     SharedPtr<BorderImage> background_;
     /// Container for text rows.
     SharedPtr<UIElement> rowContainer_;
-    /// Text rows.
-    Vector<SharedPtr<Text> > rows_;
     /// Line edit.
     SharedPtr<LineEdit> lineEdit_;
     /// Command history.

+ 14 - 0
Source/Engine/UI/UIElement.cpp

@@ -1244,6 +1244,20 @@ void UIElement::RemoveChildAtIndex(unsigned index)
     if (index >= children_.Size())
         return;
 
+    // Send change event if not already being destroyed
+    UIElement* sender = Refs() > 0 ? GetElementEventSender() : 0;
+    if (sender)
+    {
+        using namespace ElementRemoved;
+
+        VariantMap eventData;
+        eventData[P_ROOT] = (void*)GetRoot();
+        eventData[P_PARENT] = (void*)this;
+        eventData[P_ELEMENT] = (void*)children_[index];
+
+        sender->SendEvent(E_ELEMENTREMOVED, eventData);
+    }
+
     children_[index]->Detach();
     children_.Erase(index);
     UpdateLayout();

+ 4 - 5
Source/Tools/Urho3D/Urho3D.cpp

@@ -24,6 +24,9 @@
 #include "Engine.h"
 #include "FileSystem.h"
 #include "Log.h"
+#ifdef ENABLE_LUA
+#include "LuaScript.h"
+#endif
 #include "Main.h"
 #include "ProcessUtils.h"
 #include "ResourceCache.h"
@@ -31,10 +34,6 @@
 #include "Script.h"
 #include "ScriptFile.h"
 
-#ifdef ENABLE_LUA
-#include "LuaScript.h"
-#endif
-
 #include "DebugNew.h"
 
 using namespace Urho3D;
@@ -145,7 +144,7 @@ void Urho::Start()
         context_->RegisterSubsystem(new Script(context_));
 
         // Hold a shared pointer to the script file to make sure it is not unloaded during runtime
-        scriptFile_ = context_->GetSubsystem<ResourceCache>()->GetResource<ScriptFile>(scriptFileName_);
+        scriptFile_ = GetSubsystem<ResourceCache>()->GetResource<ScriptFile>(scriptFileName_);
 
         // If script loading is successful, proceed to main loop
         if (scriptFile_ && scriptFile_->Execute("void Start()"))