Browse Source

Switched to using Urho3D::Vector, and conforming with Urho's contribution requirements.

Enhex 9 years ago
parent
commit
d75cee7e1c
2 changed files with 37 additions and 30 deletions
  1. 27 19
      Source/Urho3D/Engine/Console.cpp
  2. 10 11
      Source/Urho3D/Engine/Console.h

+ 27 - 19
Source/Urho3D/Engine/Console.cpp

@@ -42,6 +42,9 @@
 
 #include "../DebugNew.h"
 
+#include <algorithm>
+#include <vector>
+
 namespace Urho3D
 {
 
@@ -53,7 +56,7 @@ Console::Console(Context* context) :
     autoVisibleOnError_(false),
     historyRows_(DEFAULT_HISTORY_SIZE),
     historyPosition_(0),
-    autoCompleteIter_(autoComplete_.begin()),
+    historyOrAutoCompleteChange(false),
     printing_(false)
 {
     UI* ui = GetSubsystem<UI>();
@@ -278,12 +281,19 @@ const String& Console::GetHistoryRow(unsigned index) const
 
 void Console::AddAutoComplete(const String & option)
 {
-    autoComplete_.insert(option);
+    // sorted insertion
+    Vector<String>::Iterator iter = UpperBound(autoComplete_.Begin(), autoComplete_.End(), option);
+    if(!iter.ptr_)
+        autoComplete_.Push(option);
+    // make sure it isn't a duplicate
+    else if(iter == autoComplete_.Begin() || *(iter-1) != option)
+        autoComplete_.Insert(iter, option);
 }
 
 void Console::RemoveAutoComplete(const String & option)
 {
-    autoComplete_.erase(option);
+    // erase and keep ordered
+    autoComplete_.Erase(LowerBound(autoComplete_.Begin(), autoComplete_.End(), option));
 }
 
 bool Console::PopulateInterpreter()
@@ -377,7 +387,7 @@ void Console::HandleTextFinished(StringHash eventType, VariantMap& eventData)
 
         historyPosition_ = history_.Size(); // reset
 
-        autoCompleteIter_ = autoComplete_.end(); // reset
+        autoCompleteIter_ = autoComplete_.End(); // reset
 
         currentRow_.Clear();
         lineEdit_->SetText(currentRow_);
@@ -393,17 +403,15 @@ void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
 
     bool changed = false;
 
-    auto start_iter = autoCompleteIter_;
-
     switch (eventData[P_KEY].GetInt())
     {
     case KEY_UP:
-        if (autoCompleteIter_ == autoComplete_.begin())
-            autoCompleteIter_ = autoComplete_.end();
+        if (autoCompleteIter_ == autoComplete_.Begin())
+            autoCompleteIter_ = autoComplete_.End();
 
-        if (autoCompleteIter_ != autoComplete_.end())
+        if (autoCompleteIter_ != autoComplete_.End())
         {
-            auto before_begin = --autoComplete_.begin();
+            Vector<String>::Iterator before_begin = --autoComplete_.Begin();
 
             // Search for auto completion that contains the contents of the line
             for (--autoCompleteIter_; autoCompleteIter_ != before_begin; --autoCompleteIter_)
@@ -420,14 +428,14 @@ void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
             if (autoCompleteIter_ == before_begin)
             {
                 // Reset the iterator
-                autoCompleteIter_ = autoComplete_.end();
+                autoCompleteIter_ = autoComplete_.End();
                 // Reset history position
                 historyPosition_ = history_.Size();
             }
         }
         
         // If no more auto complete options and history options left
-        if (autoCompleteIter_ == autoComplete_.end() &&
+        if (autoCompleteIter_ == autoComplete_.End() &&
             historyPosition_ > 0)
         {
             // If line text is not a history, save the current text value to be restored later
@@ -450,15 +458,15 @@ void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
         else
         {
             // Loop over
-            if (autoCompleteIter_ == autoComplete_.end())
-                autoCompleteIter_ = autoComplete_.begin();
+            if (autoCompleteIter_ == autoComplete_.End() || !autoCompleteIter_.ptr_)
+                autoCompleteIter_ = autoComplete_.Begin();
             else
                 ++autoCompleteIter_; // If not starting over, skip checking the currently found completion
 
-            auto start_iter = autoCompleteIter_;
+            Vector<String>::Iterator start_iter = autoCompleteIter_;
 
             // Search for auto completion that contains the contents of the line
-            for (; autoCompleteIter_ != autoComplete_.end(); ++autoCompleteIter_)
+            for (; autoCompleteIter_ != autoComplete_.End(); ++autoCompleteIter_)
             {
                 if (autoCompleteIter_->StartsWith(autoCompleteLine_))
                 {
@@ -469,9 +477,9 @@ void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
             }
 
             // Continue to search the complete range
-            if (autoCompleteIter_ == autoComplete_.end())
+            if (autoCompleteIter_ == autoComplete_.End())
             {
-                for (autoCompleteIter_ = autoComplete_.begin(); autoCompleteIter_ != start_iter; ++autoCompleteIter_)
+                for (autoCompleteIter_ = autoComplete_.Begin(); autoCompleteIter_ != start_iter; ++autoCompleteIter_)
                 {
                     if (autoCompleteIter_->StartsWith(autoCompleteLine_))
                     {
@@ -497,7 +505,7 @@ void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
         {
             lineEdit_->SetText(currentRow_);
             // Set the auto complete iterator according to the currentRow
-            for (autoCompleteIter_ = autoComplete_.begin(); autoCompleteIter_ != autoComplete_.end(); ++autoCompleteIter_)
+            for (autoCompleteIter_ = autoComplete_.Begin(); autoCompleteIter_ != autoComplete_.End(); ++autoCompleteIter_)
                 if (autoCompleteIter_->StartsWith(currentRow_))
                     break;
         }

+ 10 - 11
Source/Urho3D/Engine/Console.h

@@ -23,7 +23,6 @@
 #pragma once
 
 #include "../Core/Object.h"
-#include <set>
 
 namespace Urho3D
 {
@@ -172,21 +171,21 @@ private:
     /// Command history current position.
     unsigned historyPosition_;
 
-    bool historyOrAutoCompleteChange = false;
+    bool historyOrAutoCompleteChange;
 
     /**
-	Command auto complete options.
+    Command auto complete options.
 
-	down arrow key
-	Unless currently going through history options, will loop through next auto complete options.
+    down arrow key
+    Unless currently going through history options, will loop through next auto complete options.
 
-	up arrow key
-	Unless currently going through history options, will go through previous auto complete options.
-	When no previous options are left will start going through history options.
-	*/
-    std::set<String> autoComplete_;
+    up arrow key
+    Unless currently going through history options, will go through previous auto complete options.
+    When no previous options are left will start going through history options.
+    */
+    Vector<String> autoComplete_;
     /// Command auto complete current position.
-    std::set<String>::iterator autoCompleteIter_;
+    Vector<String>::Iterator autoCompleteIter_;
     /// Store the original line which is being auto-completed
     String autoCompleteLine_;