Explorar o código

Merge pull request #121 from blackberry-gaming/next

Next
Sean Paul Taylor %!s(int64=13) %!d(string=hai) anos
pai
achega
470b7df8ef

+ 23 - 1
gameplay/src/Container.cpp

@@ -34,7 +34,7 @@ Container::Container()
       _lastX(0), _lastY(0),
       _startTimeX(0), _startTimeY(0), _lastTime(0),
       _velocity(Vector2::zero()), _friction(1.0f),
-      _goingRight(false), _goingDown(false)
+      _goingRight(false), _goingDown(false), _zIndexDefault(0)
 {
 }
 
@@ -144,6 +144,11 @@ void Container::addControls(Theme* theme, Properties* properties)
         if (control)
         {
             addControl(control);
+
+            if (control->getZIndex() == -1)
+            {
+                control->setZIndex(_zIndexDefault++);
+            }
         }
 
         // Get the next control.
@@ -304,6 +309,9 @@ void Container::update(const Rectangle& clip, const Vector2& offset)
         _viewportClipBounds.width -= _scrollBarVertical->getRegion().width;
     }
 
+    // Sort controls by Z-Order.
+    std::sort(_controls.begin(), _controls.end(), &sortControlsByZOrder);
+
     GP_ASSERT(_layout);
     _layout->update(this);
 
@@ -326,7 +334,9 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
         needsClear = false;
     }
 
+    spriteBatch->begin();
     Control::drawBorder(spriteBatch, clip);
+    spriteBatch->end();
 
     std::vector<Control*>::const_iterator it;
     Rectangle boundsUnion = Rectangle::empty();
@@ -346,6 +356,8 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
         // Draw scroll bars.
         Rectangle clipRegion(_viewportClipBounds);
 
+        spriteBatch->begin();
+
         if (_scrollBarBounds.height > 0 && (_scrolling || _velocity.y) && _scrollBarTopCap && _scrollBarVertical && _scrollBarBottomCap)
         {
             const Rectangle& topRegion = _scrollBarTopCap->getRegion();
@@ -406,6 +418,8 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
             spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, rightUVs.u1, rightUVs.v1, rightUVs.u2, rightUVs.v2, rightColor, clipRegion);
         }
 
+        spriteBatch->end();
+
         if (_velocity.isZero())
         {
             _dirty = false;
@@ -795,4 +809,12 @@ Container::Scroll Container::getScroll(const char* scroll)
     return Container::SCROLL_NONE;
 }
 
+bool sortControlsByZOrder(Control* c1, Control* c2)
+{
+    if (c1->getZIndex() < c2->getZIndex())
+        return true;
+
+    return false;
+}
+
 }

+ 5 - 0
gameplay/src/Container.h

@@ -301,8 +301,13 @@ protected:
 private:
 
     Container(const Container& copy);
+
+    int _zIndexDefault;
 };
 
+// Sort funtion for use with _controls.sort(), based on Z-Order.
+bool sortControlsByZOrder(Control* c1, Control* c2);
+
 }
 
 #endif

+ 26 - 0
gameplay/src/Control.cpp

@@ -43,6 +43,15 @@ void Control::initialize(Theme::Style* style, Properties* properties)
     _autoWidth = properties->getBool("autoWidth");
     _autoHeight = properties->getBool("autoHeight");
 
+    if (properties->exists("zIndex"))
+    {
+        _zIndex = properties->getInt("zIndex");
+    }
+    else
+    {
+        _zIndex = -1;
+    }
+
     Vector2 position;
     Vector2 size;
     if (properties->exists("position"))
@@ -608,6 +617,20 @@ bool Control::getConsumeTouchEvents()
     return _consumeTouchEvents;
 }
 
+int Control::getZIndex() const
+{
+    return _zIndex;
+}
+
+void Control::setZIndex(int zIndex)
+{
+    if (zIndex != _zIndex)
+    {
+        _zIndex = zIndex;
+        _dirty = true;
+    }
+}
+
 void Control::addListener(Control::Listener* listener, int eventFlags)
 {
     GP_ASSERT(listener);
@@ -888,8 +911,11 @@ void Control::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsCl
         GL_ASSERT( glDisable(GL_SCISSOR_TEST) );
     }
 
+    spriteBatch->begin();
     drawBorder(spriteBatch, clip);
     drawImages(spriteBatch, clip);
+    spriteBatch->end();
+
     drawText(clip);
     _dirty = false;
 }

+ 6 - 0
gameplay/src/Control.h

@@ -659,6 +659,10 @@ public:
      */
     Theme::Style* getStyle() const;
 
+    int getZIndex() const;
+
+    void setZIndex(int zOrder);
+
     /**
      * Add a listener to be notified of specific events affecting
      * this control.  Event types can be OR'ed together.
@@ -889,6 +893,8 @@ protected:
      */
     float _opacity;
 
+    int _zIndex;
+
 private:
 
     /*

+ 18 - 39
gameplay/src/Form.cpp

@@ -86,6 +86,10 @@ Form* Form::create(const char* url)
     form->_layout = layout;
     form->_theme = theme;
 
+    // Get default projection matrix.
+    Game* game = Game::getInstance();
+    Matrix::createOrthographicOffCenter(0, game->getWidth(), game->getHeight(), 0, 0, 1, &form->_defaultProjectionMatrix);
+
     const char* styleName = formProperties->getString("style");
     form->initialize(theme->getStyle(styleName), formProperties);
 
@@ -113,9 +117,6 @@ Form* Form::create(const char* url)
     // Add all the controls to the form.
     form->addControls(theme, formProperties);
 
-    Game* game = Game::getInstance();
-    Matrix::createOrthographicOffCenter(0, game->getWidth(), game->getHeight(), 0, 0, 1, &form->_defaultProjectionMatrix);
-
     SAFE_DELETE(properties);
 
     __forms.push_back(form);
@@ -191,6 +192,19 @@ void Form::setSize(float width, float height)
         _spriteBatch = SpriteBatch::create(_frameBuffer->getRenderTarget()->getTexture());
         GP_ASSERT(_spriteBatch);
 
+        // Clear FBO.
+        _frameBuffer->bind();
+        Game* game = Game::getInstance();
+        Rectangle prevViewport = game->getViewport();
+        game->setViewport(Rectangle(0, 0, width, height));
+        _theme->setProjectionMatrix(_projectionMatrix);
+        GL_ASSERT( glClearColor(0, 0, 0, 0) );
+        GL_ASSERT( glClear(GL_COLOR_BUFFER_BIT) );
+        GL_ASSERT( glClearColor(0, 0, 0, 1) );
+        _theme->setProjectionMatrix(_defaultProjectionMatrix);
+        FrameBuffer::bindDefault();
+        game->setViewport(prevViewport);
+
         _bounds.width = width;
         _bounds.height = height;
         _dirty = true;
@@ -437,7 +451,7 @@ void Form::draw()
 
         GP_ASSERT(_theme);
         _theme->setProjectionMatrix(_projectionMatrix);
-        draw(_theme->getSpriteBatch(), _viewportClipBounds);
+        Container::draw(_theme->getSpriteBatch(), Rectangle(0, 0, _bounds.width, _bounds.height), _skin == NULL, _bounds.height);
         _theme->setProjectionMatrix(_defaultProjectionMatrix);
 
         // Rebind the default framebuffer and game viewport.
@@ -466,41 +480,6 @@ void Form::draw()
     }
 }
 
-void Form::draw(SpriteBatch* spriteBatch, const Rectangle& clip)
-{
-    GP_ASSERT(spriteBatch);
-
-    std::vector<Control*>::const_iterator it;
-
-    // Batch each font individually.
-    std::set<Font*>::const_iterator fontIter;
-    for (fontIter = _theme->_fonts.begin(); fontIter != _theme->_fonts.end(); fontIter++)
-    {
-        Font* font = *fontIter;
-        if (font)
-        {
-            font->begin();
-        }
-    }
-
-    // Batch for all themed border and background sprites.
-    spriteBatch->begin();
-
-    Container::draw(spriteBatch, Rectangle(0, 0, _bounds.width, _bounds.height), _skin == NULL, _bounds.height);
-
-    // Done all batching.
-    spriteBatch->end();
-
-    for (fontIter = _theme->_fonts.begin(); fontIter != _theme->_fonts.end(); fontIter++)
-    {
-        Font* font = *fontIter;
-        if (font)
-        {
-            font->end();
-        }
-    }
-}
-
 void Form::initializeQuad(Mesh* mesh)
 {
     // Release current model.

+ 0 - 8
gameplay/src/Form.h

@@ -166,14 +166,6 @@ private:
      */
     void initializeQuad(Mesh* mesh);
 
-    /**
-     * Draw this form into the current framebuffer.
-     *
-     * @param spriteBatch The sprite batch containing this form's theme texture.
-     * @param clip The form's clipping rectangle.
-     */
-    void draw(SpriteBatch* spriteBatch, const Rectangle& clip);
-
     /**
      * Propagate touch events to enabled forms.
      *

+ 2 - 0
gameplay/src/Label.cpp

@@ -86,7 +86,9 @@ void Label::drawText(const Rectangle& clip)
     // Draw the text.
     if (_font)
     {
+        _font->begin();
         _font->drawText(_text.c_str(), _textBounds, _textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_viewportClipBounds);
+        _font->end();
     }
 
     _dirty = false;