2
0
Эх сурвалжийг харах

Minor code cleanup. Added GetAttribute() to Context, which returns a named attribute.

Lasse Öörni 12 жил өмнө
parent
commit
ebcc766ac0

+ 16 - 0
Engine/Core/Context.cpp

@@ -154,6 +154,22 @@ const String& Context::GetTypeName(ShortStringHash type) const
     return i != factories_.End() ? i->second_->GetTypeName() : String::EMPTY;
 }
 
+AttributeInfo* Context::GetAttribute(ShortStringHash objectType, const char* name)
+{
+    HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes_.Find(objectType);
+    if (i == attributes_.End())
+        return 0;
+    
+    Vector<AttributeInfo>& infos = i->second_;
+    
+    for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
+    {
+        if (!String::Compare(j->name_, name, true))
+            return &(*j);
+    }
+    
+    return 0;
+}
 
 void Context::AddEventReceiver(Object* receiver, StringHash eventType)
 {

+ 6 - 0
Engine/Core/Context.h

@@ -52,6 +52,7 @@ public:
     void RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr);
     /// Remove object attribute.
     void RemoveAttribute(ShortStringHash objectType, const char* name);
+    
     /// Copy base class attributes to derived class.
     void CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType);
     /// Template version of registering an object factory.
@@ -77,8 +78,12 @@ public:
     EventHandler* GetEventHandler() const { return eventHandler_; }
     /// Return object type name from hash, or empty if unknown.
     const String& GetTypeName(ShortStringHash type) const;
+    /// Return a specific attribute description for an object, or null if not found.
+    AttributeInfo* GetAttribute(ShortStringHash objectType, const char* name);
     /// Template version of returning a subsystem.
     template <class T> T* GetSubsystem() const;
+    /// Template version of returning a specific attribute description.
+    template <class T> AttributeInfo* GetAttribute(const char* name);
     
     /// Return attribute descriptions for an object type, or null if none defined.
     const Vector<AttributeInfo>* GetAttributes(ShortStringHash type) const
@@ -156,5 +161,6 @@ template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) {
 template <class T> void Context::RemoveAttribute(const char* name) { RemoveAttribute(T::GetTypeStatic(), name); }
 template <class T, class U> void Context::CopyBaseAttributes() { CopyBaseAttributes(T::GetTypeStatic(), U::GetTypeStatic()); }
 template <class T> T* Context::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
+template <class T> AttributeInfo* Context::GetAttribute(const char* name) { return GetAttribute(T::GetTypeStatic(), name); }
 
 }

+ 1 - 1
Engine/UI/Slider.cpp

@@ -224,7 +224,7 @@ void Slider::Page(const IntVector2& position, int buttons, int qualifiers)
 {
     IntVector2 offsetXY = position - knob_->GetPosition() - knob_->GetSize() / 2;
     int offset = orientation_ == O_HORIZONTAL ? offsetXY.x_ : offsetXY.y_;
-    float length = orientation_ == O_HORIZONTAL ? GetWidth() : GetHeight();
+    float length = (float)(orientation_ == O_HORIZONTAL ? GetWidth() : GetHeight());
 
     using namespace SliderPaged;
     

+ 1 - 9
Engine/UI/Text.cpp

@@ -82,15 +82,7 @@ void Text::RegisterObject(Context* context)
     COPY_BASE_ATTRIBUTES(Text, UIElement);
     
     // Change the default value for UseDerivedOpacity
-    Vector<AttributeInfo>& attributes = const_cast<Vector<AttributeInfo>&>(*context->GetAttributes(GetTypeStatic()));
-    for (unsigned i = 0; i < attributes.Size(); ++i)
-    {
-        if (attributes[i].name_ == "Use Derived Opacity")
-        {
-            attributes[i].defaultValue_ = false;
-            break;
-        }
-    }
+    context->GetAttribute<Text>("Use Derived Opacity")->defaultValue_ = false;
 }
 
 void Text::ApplyAttributes()