Browse Source

Use ListView as row container for the Console class. Closes #298.

Yao Wei Tjong 姚伟忠 11 years ago
parent
commit
5730660a80

+ 43 - 17
Source/Engine/Engine/Console.cpp

@@ -28,9 +28,11 @@
 #include "Font.h"
 #include "Graphics.h"
 #include "GraphicsEvents.h"
+#include "Input.h"
 #include "InputEvents.h"
 #include "IOEvents.h"
 #include "LineEdit.h"
+#include "ListView.h"
 #include "Log.h"
 #include "ResourceCache.h"
 #include "Text.h"
@@ -66,9 +68,7 @@ Console::Console(Context* context) :
     background_->SetPriority(200); // Show on top of the debug HUD
     background_->SetLayout(LM_VERTICAL);
 
-    rowContainer_ = new UIElement(context_);
-    rowContainer_->SetClipChildren(true);
-    rowContainer_->SetLayout(LM_VERTICAL);
+    rowContainer_ = new ListView(context_);
     background_->AddChild(rowContainer_);
 
     lineEdit_ = new LineEdit(context_);
@@ -98,10 +98,10 @@ void Console::SetDefaultStyle(XMLFile* style)
 
     background_->SetDefaultStyle(style);
     background_->SetStyle("ConsoleBackground");
+    rowContainer_->SetStyle("ListView");
 
-    const Vector<SharedPtr<UIElement> >& children = rowContainer_->GetChildren();
-    for (unsigned i = 0; i < children.Size(); ++i)
-        children[i]->SetStyle("ConsoleText");
+    for (unsigned i = 0; i < rowContainer_->GetNumItems(); ++i)
+        rowContainer_->GetItem(i)->SetStyle("ConsoleText");
     
     lineEdit_->SetStyle("ConsoleLineEdit");
     
@@ -110,8 +110,8 @@ void Console::SetDefaultStyle(XMLFile* style)
 
 void Console::SetVisible(bool enable)
 {
+    Input* input = GetSubsystem<Input>();
     background_->SetVisible(enable);
-
     if (enable)
     {
         // Check if we have handler for E_CONSOLECOMMAND every time here in case the handler is being added later dynamically
@@ -123,9 +123,18 @@ void Console::SetVisible(bool enable)
 
         // Ensure the background has no empty space when shown without the lineedit
         background_->SetHeight(background_->GetMinHeight());
+
+        // Show OS mouse
+        savedMouseVisibility_ = input->IsMouseVisible();
+        input->SetMouseVisible(true);
     }
     else
+    {
         lineEdit_->SetFocus(false);
+
+        // Restore OS mouse visibility
+        input->SetMouseVisible(savedMouseVisibility_);
+    }
 }
 
 void Console::Toggle()
@@ -133,30 +142,31 @@ void Console::Toggle()
     SetVisible(!IsVisible());
 }
 
-void Console::SetNumRows(unsigned rows)
+void Console::SetNumBufferedRows(unsigned rows)
 {
-    if (!rows)
+    if (rows < displayedRows_)
         return;
 
     rowContainer_->DisableLayoutUpdate();
 
-    int delta = rowContainer_->GetNumChildren() - rows;
+    int delta = rowContainer_->GetNumItems() - rows;
     if (delta > 0)
     {
         // We have more, remove oldest rows first
         for (int i = 0; i < delta; ++i)
-            rowContainer_->RemoveChildAtIndex(0);
+            rowContainer_->RemoveItem((unsigned)0);
     }
     else
     {
         // We have less, add more rows at the bottom
         for (int i = 0; i > delta; --i)
         {
-            Text* text = rowContainer_->CreateChild<Text>();
+            Text* text = new Text(context_);
             // If style is already set, apply here to ensure proper height of the console when
             // amount of rows is changed
             if (background_->GetDefaultStyle())
                 text->SetStyle("ConsoleText");
+            rowContainer_->AddItem(text);
         }
     }
 
@@ -166,6 +176,19 @@ void Console::SetNumRows(unsigned rows)
     UpdateElements();
 }
 
+void Console::SetNumRows(unsigned rows)
+{
+    if (!rows)
+        return;
+    displayedRows_ = rows;
+    if (!GetNumBufferedRows())
+        SetNumBufferedRows(2 * rows);
+    const IntRect& border = rowContainer_->GetScrollPanel()->GetBorder();
+    unsigned height = rows * rowContainer_->GetItem((unsigned)0)->GetHeight() + border.top_; border.bottom_;
+    rowContainer_->SetMinHeight(height);
+    rowContainer_->SetHeight(height);
+}
+
 void Console::SetNumHistoryRows(unsigned rows)
 {
     historyRows_ = rows;
@@ -199,9 +222,9 @@ bool Console::IsVisible() const
     return background_ && background_->IsVisible();
 }
 
-unsigned Console::GetNumRows() const
+unsigned Console::GetNumBufferedRows() const
 {
-    return rowContainer_->GetNumChildren();
+    rowContainer_->GetNumItems();
 }
 
 const String& Console::GetHistoryRow(unsigned index) const
@@ -299,23 +322,26 @@ void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
 
 void Console::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
 {
-    if (!rowContainer_->GetNumChildren())
+    if (!rowContainer_->GetNumItems())
         return;
     
     printing_ = true;
     rowContainer_->DisableLayoutUpdate();
     
+    Text* text;
     for (unsigned i = 0; i < pendingRows_.Size(); ++i)
     {
-        rowContainer_->RemoveChildAtIndex(0);
-        Text* text = rowContainer_->CreateChild<Text>();
+        rowContainer_->RemoveItem((unsigned)0);
+        text = new Text(context_);
         text->SetText(pendingRows_[i].second_);
         // Make error message highlight
         text->SetStyle(pendingRows_[i].first_ == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
+        rowContainer_->AddItem(text);
     }
     
     pendingRows_.Clear();
     
+    rowContainer_->EnsureItemVisibility(text);
     rowContainer_->EnableLayoutUpdate();
     rowContainer_->UpdateLayout();
     printing_ = false;

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

@@ -31,8 +31,8 @@ class BorderImage;
 class Engine;
 class Font;
 class LineEdit;
+class ListView;
 class Text;
-class UIElement;
 class XMLFile;
 
 /// %Console window with log history and command line prompt.
@@ -54,6 +54,8 @@ public:
     void Toggle();
     /// Automatically set console to visible when receiving an error log message.
     void SetAutoVisibleOnError(bool enable) { autoVisibleOnError_ = enable; }
+    /// Set number of buffered rows.
+    void SetNumBufferedRows(unsigned rows);
     /// Set number of displayed rows.
     void SetNumRows(unsigned rows);
     /// Set command history maximum size, 0 disables history.
@@ -73,8 +75,10 @@ public:
     bool IsVisible() const;
     /// Return true when console is set to automatically visible when receiving an error log message.
     bool IsAutoVisibleOnError() const { return autoVisibleOnError_; }
+    /// Return number of buffered rows.
+    unsigned GetNumBufferedRows() const;
     /// Return number of displayed rows.
-    unsigned GetNumRows() const;
+    unsigned GetNumRows() const { return displayedRows_; }
     /// Return history maximum size.
     unsigned GetNumHistoryRows() const { return historyRows_; }
     /// Return current history position.
@@ -101,7 +105,7 @@ private:
     /// Background.
     SharedPtr<BorderImage> background_;
     /// Container for text rows.
-    SharedPtr<UIElement> rowContainer_;
+    SharedPtr<ListView> rowContainer_;
     /// Line edit.
     SharedPtr<LineEdit> lineEdit_;
     /// Command history.
@@ -110,6 +114,8 @@ private:
     Vector<Pair<int, String> > pendingRows_;
     /// Current row being edited.
     String currentRow_;
+    /// Maximum displayed rows.
+    unsigned displayedRows_;
     /// Command history maximum rows.
     unsigned historyRows_;
     /// Command history current position.
@@ -118,6 +124,8 @@ private:
     bool printing_;
     /// Flag for automatically focusing the line edit on showing the console.
     bool focusOnShow_;
+    /// Saved OS mouse visiblity flag. Used internally to save and restore OS mouse visibility.
+    bool savedMouseVisibility_;
 };
 
 }

+ 3 - 0
Source/Engine/LuaScript/pkgs/Engine/Console.pkg

@@ -6,6 +6,7 @@ class Console : public Object
     void SetVisible(bool enable);
     void Toggle();
     void SetAutoVisibleOnError(bool enable);
+    void SetNumBufferedRows(unsigned rows);
     void SetNumRows(unsigned rows);
     void SetNumHistoryRows(unsigned rows);
     void SetFocusOnShow(bool enable);
@@ -16,6 +17,7 @@ class Console : public Object
     LineEdit* GetLineEdit() const;
     bool IsVisible() const;
     bool IsAutoVisibleOnError() const;
+    unsigned GetNumBufferedRows() const;
     unsigned GetNumRows() const;
     unsigned GetNumHistoryRows() const;
     unsigned GetHistoryPosition() const;
@@ -27,6 +29,7 @@ class Console : public Object
     tolua_readonly tolua_property__get_set LineEdit* lineEdit;
     tolua_property__is_set bool visible;
     tolua_property__is_set bool autoVisibleOnError;
+    tolua_property__get_set unsigned numBufferedRows;
     tolua_property__get_set unsigned numRows;
     tolua_property__get_set unsigned numHistoryRows;
     tolua_readonly tolua_property__get_set unsigned historyPosition;

+ 2 - 0
Source/Engine/Script/EngineAPI.cpp

@@ -45,6 +45,8 @@ static void RegisterConsole(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Console", "bool get_visible() const", asMETHOD(Console, IsVisible), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void set_autoVisibleOnError(bool)", asMETHOD(Console, SetAutoVisibleOnError), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "bool get_autoVisibleOnError() const", asMETHOD(Console, IsAutoVisibleOnError), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Console", "void set_numBufferedRows(uint)", asMETHOD(Console, SetNumBufferedRows), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Console", "uint get_numBufferedRows() const", asMETHOD(Console, GetNumBufferedRows), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void set_numRows(uint)", asMETHOD(Console, SetNumRows), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "uint get_numRows() const", asMETHOD(Console, GetNumRows), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void set_numHistoryRows(uint)", asMETHOD(Console, SetNumHistoryRows), asCALL_THISCALL);

+ 4 - 4
Source/Engine/UI/ListView.h

@@ -130,16 +130,16 @@ public:
     bool GetHierarchyMode() const { return hierarchyMode_; }
     /// Return base indent.
     int GetBaseIndent() const { return baseIndent_; }
+    /// Ensure full visibility of the item.
+    void EnsureItemVisibility(unsigned index);
+    /// Ensure full visibility of the item.
+    void EnsureItemVisibility(UIElement* item);
 
 protected:
     /// Filter implicit attributes in serialization process.
     virtual bool FilterImplicitAttributes(XMLElement& dest) const;
     /// Update selection effect when selection or focus changes.
     void UpdateSelectionEffect();
-    /// Ensure full visibility of the item.
-    void EnsureItemVisibility(unsigned index);
-    /// Ensure full visibility of the item.
-    void EnsureItemVisibility(UIElement* item);
 
     /// Current selection.
     PODVector<unsigned> selections_;