Sfoglia il codice sorgente

Merge pull request #1191 from sgrenier/next

Next
Steve Grenier 12 anni fa
parent
commit
2b0b0241bf

+ 25 - 2
gameplay/src/Container.cpp

@@ -100,6 +100,9 @@ Container* Container::create(Layout::Type type)
     case Layout::LAYOUT_VERTICAL:
         layout = VerticalLayout::create();
         break;
+    default:
+        layout = AbsoluteLayout::create();
+        break;
     }
 
     Container* container = new Container();
@@ -112,8 +115,28 @@ Container* Container::create(Theme::Style* style, Properties* properties, Theme*
 {
     GP_ASSERT(properties);
 
-    const char* layoutString = properties->getString("layout");
-    Container* container = Container::create(getLayoutType(layoutString));
+    // Parse layout
+    Container* container;
+    Properties* layoutNS = properties->getNamespace("layout", true, false);
+    if (layoutNS)
+    {
+        Layout::Type layoutType = getLayoutType(layoutNS->getString("type"));
+        container = Container::create(layoutType);
+        switch (layoutType)
+        {
+        case Layout::LAYOUT_FLOW:
+            static_cast<FlowLayout*>(container->getLayout())->setSpacing(layoutNS->getInt("horizontalSpacing"), layoutNS->getInt("verticalSpacing"));
+            break;
+        case Layout::LAYOUT_VERTICAL:
+            static_cast<VerticalLayout*>(container->getLayout())->setSpacing(layoutNS->getInt("spacing"));
+            break;
+        }
+    }
+    else
+    {
+        container = Container::create(getLayoutType(properties->getString("layout")));
+    }
+
     container->initialize(style, properties);
     container->_scroll = getScroll(properties->getString("scroll"));
     container->_scrollBarsAutoHide = properties->getBool("scrollBarsAutoHide");

+ 20 - 3
gameplay/src/FlowLayout.cpp

@@ -8,7 +8,7 @@ namespace gameplay
 
 static FlowLayout* __instance;
 
-FlowLayout::FlowLayout()
+FlowLayout::FlowLayout() : _horizontalSpacing(0), _verticalSpacing(0)
 {
 }
 
@@ -36,6 +36,22 @@ Layout::Type FlowLayout::getType()
     return Layout::LAYOUT_FLOW;
 }
 
+int FlowLayout::getHorizontalSpacing() const
+{
+    return _horizontalSpacing;
+}
+
+int FlowLayout::getVerticalSpacing() const
+{
+    return _verticalSpacing;
+}
+
+void FlowLayout::setSpacing(int horizontalSpacing, int verticalSpacing)
+{
+    _horizontalSpacing = horizontalSpacing;
+    _verticalSpacing = verticalSpacing;
+}
+
 void FlowLayout::update(const Container* container, const Vector2& offset)
 {
     GP_ASSERT(container);
@@ -71,7 +87,8 @@ void FlowLayout::update(const Container* container, const Vector2& offset)
         if (xPosition + bounds.width >= clipWidth)
         {
             xPosition = margin.left;
-            rowY += tallestHeight;
+            rowY += tallestHeight + _verticalSpacing;
+            tallestHeight = 0;
         }
 
         yPosition = rowY + margin.top;
@@ -79,7 +96,7 @@ void FlowLayout::update(const Container* container, const Vector2& offset)
         control->setPosition(xPosition, yPosition);
         control->update(container, offset);
 
-        xPosition += bounds.width + margin.right;
+        xPosition += bounds.width + margin.right + _horizontalSpacing;
 
         float height = bounds.height + margin.top + margin.bottom;
         if (height > tallestHeight)

+ 32 - 0
gameplay/src/FlowLayout.h

@@ -23,6 +23,28 @@ public:
      */
     Layout::Type getType();
 
+    /**
+     * Returns the horizontal spacing between controls in the layout.
+     *
+     * @return The horizontal spacing between controls.
+     */
+    int getHorizontalSpacing() const;
+
+    /**
+     * Returns the vertical spacing between controls in the layout.
+     *
+     * @return The vertical spacing between controls.
+     */
+    int getVerticalSpacing() const;
+
+    /**
+     * Sets the spacing to add between controls in the layout.
+     *
+     * @param horizontalSpacing The horizontal spacing between controls.
+     * @param verticalSpacing The vertical spacing between controls.
+     */
+    void setSpacing(int horizontalSpacing, int verticalSpacing);
+
 protected:
 
     /**
@@ -33,6 +55,16 @@ protected:
      */
     void update(const Container* container, const Vector2& offset);
 
+    /**
+     * Horizontal spacing between controls.
+     */
+    int _horizontalSpacing;
+
+    /**
+     * Vertical spacing between controls.
+     */
+    int _verticalSpacing;
+
 private:
 
     /**

+ 39 - 16
gameplay/src/Form.cpp

@@ -107,23 +107,46 @@ Form* Form::create(const char* url)
     // Create new form with given ID, theme and layout.
     std::string themeFile;
     formProperties->getPath("theme", &themeFile);
-    const char* layoutString = formProperties->getString("layout");
-        
-    Layout* layout;
-    switch (getLayoutType(layoutString))
+
+    // Parse layout
+    Layout* layout = NULL;
+    Properties* layoutNS = formProperties->getNamespace("layout", true, false);
+    if (layoutNS)
     {
-    case Layout::LAYOUT_ABSOLUTE:
-        layout = AbsoluteLayout::create();
-        break;
-    case Layout::LAYOUT_FLOW:
-        layout = FlowLayout::create();
-        break;
-    case Layout::LAYOUT_VERTICAL:
-        layout = VerticalLayout::create();
-        break;
-    default:
-        GP_ERROR("Unsupported layout type '%d'.", getLayoutType(layoutString));
-        break;
+        Layout::Type layoutType = getLayoutType(layoutNS->getString("type"));
+        switch (layoutType)
+        {
+        case Layout::LAYOUT_ABSOLUTE:
+            layout = AbsoluteLayout::create();
+            break;
+        case Layout::LAYOUT_FLOW:
+            layout = FlowLayout::create();
+            static_cast<FlowLayout*>(layout)->setSpacing(layoutNS->getInt("horizontalSpacing"), layoutNS->getInt("verticalSpacing"));
+            break;
+        case Layout::LAYOUT_VERTICAL:
+            layout = VerticalLayout::create();
+            static_cast<VerticalLayout*>(layout)->setSpacing(layoutNS->getInt("spacing"));
+            break;
+        }
+    }
+    else
+    {
+        switch (getLayoutType(formProperties->getString("layout")))
+        {
+        case Layout::LAYOUT_ABSOLUTE:
+            layout = AbsoluteLayout::create();
+            break;
+        case Layout::LAYOUT_FLOW:
+            layout = FlowLayout::create();
+            break;
+        case Layout::LAYOUT_VERTICAL:
+            layout = VerticalLayout::create();
+            break;
+        }
+    }
+    if (layout == NULL)
+    {
+        GP_ERROR("Unsupported layout type for form: %s", url);
     }
 
     Theme* theme = Theme::create(themeFile.c_str());

+ 11 - 15
gameplay/src/Properties.cpp

@@ -594,30 +594,26 @@ void Properties::rewind()
     _namespacesItr = _namespaces.end();
 }
 
-Properties* Properties::getNamespace(const char* id, bool searchNames) const
+Properties* Properties::getNamespace(const char* id, bool searchNames, bool recurse) const
 {
     GP_ASSERT(id);
 
-    Properties* ret = NULL;
-    std::vector<Properties*>::const_iterator it;
-    
-    for (it = _namespaces.begin(); it < _namespaces.end(); ++it)
+    for (std::vector<Properties*>::const_iterator it = _namespaces.begin(); it < _namespaces.end(); ++it)
     {
-        ret = *it;
-        if (strcmp(searchNames ? ret->_namespace.c_str() : ret->_id.c_str(), id) == 0)
-        {
-            return ret;
-        }
+        Properties* p = *it;
+        if (strcmp(searchNames ? p->_namespace.c_str() : p->_id.c_str(), id) == 0)
+            return p;
         
-        // Search recursively.
-        ret = ret->getNamespace(id, searchNames);
-        if (ret != NULL)
+        if (recurse)
         {
-            return ret;
+            // Search recursively.
+            p = p->getNamespace(id, searchNames, true);
+            if (p)
+                return p;
         }
     }
 
-    return ret;
+    return NULL;
 }
 
 const char* Properties::getNamespace() const

+ 6 - 4
gameplay/src/Properties.h

@@ -182,18 +182,20 @@ public:
     void rewind();
 
     /**
-     * Get a specific namespace by ID or name. This method will perform
-     * a depth-first search on all namespaces and inner namespaces within
-     * this Property.
+     * Get a specific namespace by ID or name. This method will optionally
+     * perform a depth-first search on all namespaces and inner namespaces
+     * within this Property.
      *
      * @param id The ID or name of the namespace to find.
      * @param searchNames If true, namespace names are used in the search,
      *      instead of namespace IDs. By default this parameter is false
      *      and namespace IDs are searched.
+     * @param recurse If true, perform a depth-first search, otherwise search
+     *      only the immediate child namespaces.
      * 
      * @return A properties object with the given ID or name.
      */
-    Properties* getNamespace(const char* id, bool searchNames = false) const;
+    Properties* getNamespace(const char* id, bool searchNames = false, bool recurse = true) const;
 
     /**
      * Get the name of this Property's namespace.

+ 12 - 2
gameplay/src/VerticalLayout.cpp

@@ -4,7 +4,7 @@
 namespace gameplay
 {
 
-VerticalLayout::VerticalLayout() : _bottomToTop(false)
+VerticalLayout::VerticalLayout() : _bottomToTop(false), _spacing(0)
 {
 }
 
@@ -32,6 +32,16 @@ Layout::Type VerticalLayout::getType()
     return Layout::LAYOUT_VERTICAL;
 }
 
+int VerticalLayout::getSpacing() const
+{
+    return _spacing;
+}
+
+void VerticalLayout::setSpacing(int spacing)
+{
+    _spacing = spacing;
+}
+
 void VerticalLayout::update(const Container* container, const Vector2& offset)
 {
     GP_ASSERT(container);
@@ -75,7 +85,7 @@ void VerticalLayout::update(const Container* container, const Vector2& offset)
             control->setPosition(margin.left, yPosition);
             control->update(container, offset);
 
-            yPosition += bounds.height + margin.bottom;
+            yPosition += bounds.height + margin.bottom + _spacing;
         }
 
         i += iter;

+ 19 - 0
gameplay/src/VerticalLayout.h

@@ -40,6 +40,20 @@ public:
      */
     Layout::Type getType();
 
+    /**
+     * Returns the vertical spacing between controls in the layout.
+     *
+     * @return The vertical spacing between controls.
+     */
+    int getSpacing() const;
+
+    /**
+     * Sets the vertical spacing to add between controls in the layout.
+     *
+     * @param spacing The vertical spacing between controls.
+     */
+    void setSpacing(int spacing);
+
 protected:
 
     /**
@@ -69,6 +83,11 @@ protected:
      */
     bool _bottomToTop;
 
+    /**
+     * Spacing between controls in the layout.
+     */
+    int _spacing;
+
 private:
 
     /**

+ 113 - 0
gameplay/src/lua/lua_FlowLayout.cpp

@@ -18,9 +18,12 @@ void luaRegister_FlowLayout()
     const luaL_Reg lua_members[] = 
     {
         {"addRef", lua_FlowLayout_addRef},
+        {"getHorizontalSpacing", lua_FlowLayout_getHorizontalSpacing},
         {"getRefCount", lua_FlowLayout_getRefCount},
         {"getType", lua_FlowLayout_getType},
+        {"getVerticalSpacing", lua_FlowLayout_getVerticalSpacing},
         {"release", lua_FlowLayout_release},
+        {"setSpacing", lua_FlowLayout_setSpacing},
         {NULL, NULL}
     };
     const luaL_Reg* lua_statics = NULL;
@@ -106,6 +109,41 @@ int lua_FlowLayout_addRef(lua_State* state)
     return 0;
 }
 
+int lua_FlowLayout_getHorizontalSpacing(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                FlowLayout* instance = getInstance(state);
+                int result = instance->getHorizontalSpacing();
+
+                // Push the return value onto the stack.
+                lua_pushinteger(state, result);
+
+                return 1;
+            }
+
+            lua_pushstring(state, "lua_FlowLayout_getHorizontalSpacing - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_FlowLayout_getRefCount(lua_State* state)
 {
     // Get the number of parameters.
@@ -176,6 +214,41 @@ int lua_FlowLayout_getType(lua_State* state)
     return 0;
 }
 
+int lua_FlowLayout_getVerticalSpacing(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                FlowLayout* instance = getInstance(state);
+                int result = instance->getVerticalSpacing();
+
+                // Push the return value onto the stack.
+                lua_pushinteger(state, result);
+
+                return 1;
+            }
+
+            lua_pushstring(state, "lua_FlowLayout_getVerticalSpacing - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_FlowLayout_release(lua_State* state)
 {
     // Get the number of parameters.
@@ -208,4 +281,44 @@ int lua_FlowLayout_release(lua_State* state)
     return 0;
 }
 
+int lua_FlowLayout_setSpacing(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 3:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                lua_type(state, 2) == LUA_TNUMBER &&
+                lua_type(state, 3) == LUA_TNUMBER)
+            {
+                // Get parameter 1 off the stack.
+                int param1 = (int)luaL_checkint(state, 2);
+
+                // Get parameter 2 off the stack.
+                int param2 = (int)luaL_checkint(state, 3);
+
+                FlowLayout* instance = getInstance(state);
+                instance->setSpacing(param1, param2);
+                
+                return 0;
+            }
+
+            lua_pushstring(state, "lua_FlowLayout_setSpacing - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 3).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 }

+ 3 - 0
gameplay/src/lua/lua_FlowLayout.h

@@ -7,9 +7,12 @@ namespace gameplay
 // Lua bindings for FlowLayout.
 int lua_FlowLayout__gc(lua_State* state);
 int lua_FlowLayout_addRef(lua_State* state);
+int lua_FlowLayout_getHorizontalSpacing(lua_State* state);
 int lua_FlowLayout_getRefCount(lua_State* state);
 int lua_FlowLayout_getType(lua_State* state);
+int lua_FlowLayout_getVerticalSpacing(lua_State* state);
 int lua_FlowLayout_release(lua_State* state);
+int lua_FlowLayout_setSpacing(lua_State* state);
 
 void luaRegister_FlowLayout();
 

+ 42 - 1
gameplay/src/lua/lua_Properties.cpp

@@ -632,9 +632,50 @@ int lua_Properties_getNamespace(lua_State* state)
             lua_error(state);
             break;
         }
+        case 4:
+        {
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
+                    lua_type(state, 3) == LUA_TBOOLEAN &&
+                    lua_type(state, 4) == LUA_TBOOLEAN)
+                {
+                    // Get parameter 1 off the stack.
+                    const char* param1 = gameplay::ScriptUtil::getString(2, false);
+
+                    // Get parameter 2 off the stack.
+                    bool param2 = gameplay::ScriptUtil::luaCheckBool(state, 3);
+
+                    // Get parameter 3 off the stack.
+                    bool param3 = gameplay::ScriptUtil::luaCheckBool(state, 4);
+
+                    Properties* instance = getInstance(state);
+                    void* returnPtr = (void*)instance->getNamespace(param1, param2, param3);
+                    if (returnPtr)
+                    {
+                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
+                        object->instance = returnPtr;
+                        object->owns = false;
+                        luaL_getmetatable(state, "Properties");
+                        lua_setmetatable(state, -2);
+                    }
+                    else
+                    {
+                        lua_pushnil(state);
+                    }
+
+                    return 1;
+                }
+            } while (0);
+
+            lua_pushstring(state, "lua_Properties_getNamespace - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
         default:
         {
-            lua_pushstring(state, "Invalid number of parameters (expected 1, 2 or 3).");
+            lua_pushstring(state, "Invalid number of parameters (expected 1, 2, 3 or 4).");
             lua_error(state);
             break;
         }

+ 73 - 0
gameplay/src/lua/lua_VerticalLayout.cpp

@@ -20,9 +20,11 @@ void luaRegister_VerticalLayout()
         {"addRef", lua_VerticalLayout_addRef},
         {"getBottomToTop", lua_VerticalLayout_getBottomToTop},
         {"getRefCount", lua_VerticalLayout_getRefCount},
+        {"getSpacing", lua_VerticalLayout_getSpacing},
         {"getType", lua_VerticalLayout_getType},
         {"release", lua_VerticalLayout_release},
         {"setBottomToTop", lua_VerticalLayout_setBottomToTop},
+        {"setSpacing", lua_VerticalLayout_setSpacing},
         {NULL, NULL}
     };
     const luaL_Reg* lua_statics = NULL;
@@ -178,6 +180,41 @@ int lua_VerticalLayout_getRefCount(lua_State* state)
     return 0;
 }
 
+int lua_VerticalLayout_getSpacing(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                VerticalLayout* instance = getInstance(state);
+                int result = instance->getSpacing();
+
+                // Push the return value onto the stack.
+                lua_pushinteger(state, result);
+
+                return 1;
+            }
+
+            lua_pushstring(state, "lua_VerticalLayout_getSpacing - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_VerticalLayout_getType(lua_State* state)
 {
     // Get the number of parameters.
@@ -281,4 +318,40 @@ int lua_VerticalLayout_setBottomToTop(lua_State* state)
     return 0;
 }
 
+int lua_VerticalLayout_setSpacing(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 2:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                lua_type(state, 2) == LUA_TNUMBER)
+            {
+                // Get parameter 1 off the stack.
+                int param1 = (int)luaL_checkint(state, 2);
+
+                VerticalLayout* instance = getInstance(state);
+                instance->setSpacing(param1);
+                
+                return 0;
+            }
+
+            lua_pushstring(state, "lua_VerticalLayout_setSpacing - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 2).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 }

+ 2 - 0
gameplay/src/lua/lua_VerticalLayout.h

@@ -9,9 +9,11 @@ int lua_VerticalLayout__gc(lua_State* state);
 int lua_VerticalLayout_addRef(lua_State* state);
 int lua_VerticalLayout_getBottomToTop(lua_State* state);
 int lua_VerticalLayout_getRefCount(lua_State* state);
+int lua_VerticalLayout_getSpacing(lua_State* state);
 int lua_VerticalLayout_getType(lua_State* state);
 int lua_VerticalLayout_release(lua_State* state);
 int lua_VerticalLayout_setBottomToTop(lua_State* state);
+int lua_VerticalLayout_setSpacing(lua_State* state);
 
 void luaRegister_VerticalLayout();