Browse Source

Issue #841
Adds a very basic ImageControl class for including png images in UI forms.

Adam Blake 13 years ago
parent
commit
3c32fa2a6e

+ 2 - 0
gameplay/gameplay.vcxproj

@@ -91,6 +91,7 @@
     <ClCompile Include="src\gameplay-main-windows.cpp" />
     <ClCompile Include="src\HeightField.cpp" />
     <ClCompile Include="src\Image.cpp" />
+    <ClCompile Include="src\ImageControl.cpp" />
     <ClCompile Include="src\Joint.cpp" />
     <ClCompile Include="src\Joystick.cpp" />
     <ClCompile Include="src\Label.cpp" />
@@ -367,6 +368,7 @@
     <ClInclude Include="src\Gesture.h" />
     <ClInclude Include="src\HeightField.h" />
     <ClInclude Include="src\Image.h" />
+    <ClInclude Include="src\ImageControl.h" />
     <ClInclude Include="src\Joint.h" />
     <ClInclude Include="src\Joystick.h" />
     <ClInclude Include="src\Keyboard.h" />

+ 6 - 0
gameplay/gameplay.vcxproj.filters

@@ -843,6 +843,9 @@
     <ClCompile Include="src\Platform.cpp">
       <Filter>src</Filter>
     </ClCompile>
+    <ClCompile Include="src\ImageControl.cpp">
+      <Filter>src</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\Animation.h">
@@ -1670,6 +1673,9 @@
     <ClInclude Include="src\lua\lua_HeightField.h">
       <Filter>src\lua</Filter>
     </ClInclude>
+    <ClInclude Include="src\ImageControl.h">
+      <Filter>src</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="src\Game.inl">

+ 5 - 0
gameplay/src/Container.cpp

@@ -10,6 +10,7 @@
 #include "RadioButton.h"
 #include "Slider.h"
 #include "TextBox.h"
+#include "ImageControl.h"
 #include "Joystick.h"
 #include "Game.h"
 
@@ -165,6 +166,10 @@ void Container::addControls(Theme* theme, Properties* properties)
         {
             control = Joystick::create(controlStyle, controlSpace);
         }
+        else if (controlName == "IMAGE")
+        {
+            control = ImageControl::create(controlStyle, controlSpace);
+        }
         else
         {
             // Ignore - not a valid control name.

+ 88 - 0
gameplay/src/ImageControl.cpp

@@ -0,0 +1,88 @@
+#include "Base.h"
+#include "ImageControl.h"
+
+namespace gameplay
+{
+
+ImageControl::ImageControl() : _image(NULL), _texture(NULL), _batch(NULL)
+{
+}
+
+ImageControl::~ImageControl()
+{
+    SAFE_RELEASE(_image);
+    SAFE_RELEASE(_texture);
+    SAFE_DELETE(_batch);
+}
+
+ImageControl* ImageControl::create(const char* id, Theme::Style* style)
+{
+    GP_ASSERT(style);
+
+    ImageControl* imageControl = new ImageControl();
+    if (id)
+        imageControl->_id = id;
+    imageControl->setStyle(style);
+
+    return imageControl;
+}
+
+ImageControl* ImageControl::create(Theme::Style* style, Properties* properties)
+{
+    ImageControl* imageControl = new ImageControl();
+    imageControl->initialize(style, properties);
+    return imageControl;
+}
+
+void ImageControl::initialize(Theme::Style* style, Properties* properties)
+{
+    GP_ASSERT(properties);
+
+    Control::initialize(style, properties);
+
+    const char* path = properties->getString("path");
+    if (path)
+    {
+        setImage(path);
+    }
+}
+
+void ImageControl::setImage(const char* path)
+{
+    SAFE_RELEASE(_image);
+    SAFE_RELEASE(_texture);
+    SAFE_DELETE(_batch);
+
+    _image = Image::create(path);
+    _texture = Texture::create(_image);
+    _batch = SpriteBatch::create(_texture);
+}
+
+void ImageControl::setImage(Image* image)
+{
+    SAFE_RELEASE(_image);
+    SAFE_RELEASE(_texture);
+    SAFE_DELETE(_batch);
+
+    image->addRef();
+    _image = image;
+    _texture = Texture::create(_image);
+    _batch = SpriteBatch::create(_texture);
+}
+
+void ImageControl::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
+{
+    spriteBatch->finish();
+
+    // An ImageControl is not part of the texture atlas but should use the same projection matrix.
+    _batch->setProjectionMatrix(spriteBatch->getProjectionMatrix());
+
+    _batch->start();
+    _batch->draw(_viewportBounds.x, _viewportBounds.y, _viewportBounds.width, _viewportBounds.height,
+        0.0f, 0.0f, 1.0f, 1.0f, Vector4::one(), _viewportClipBounds);
+    _batch->finish();
+
+    spriteBatch->start();
+}
+
+}

+ 52 - 0
gameplay/src/ImageControl.h

@@ -0,0 +1,52 @@
+#ifndef IMAGECONTROL_H_
+#define IMAGECONTROL_H_
+
+#include "Control.h"
+#include "Theme.h"
+#include "Image.h"
+#include "SpriteBatch.h"
+
+namespace gameplay
+{
+
+class ImageControl : public Control
+{
+    friend class Container;
+
+public:
+
+    static ImageControl* create(const char* id, Theme::Style* style);
+
+    void setImage(const char* path);
+    void setImage(Image* image);
+    //void setImageData(const char* data);
+
+    Image* getImage() const;
+
+protected:
+
+    ImageControl();
+    
+    virtual ~ImageControl();
+
+    static ImageControl* create(Theme::Style* style, Properties* properties);
+
+    virtual void initialize(Theme::Style* style, Properties* properties);
+
+    //void update(const Control* container, const Vector2& offset);
+
+    //void draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, bool cleared, float targetHeight);
+    void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
+
+    Image* _image;
+    Texture* _texture;
+    SpriteBatch* _batch;
+
+private:
+
+    ImageControl(const ImageControl& copy);
+};
+
+}
+
+#endif

+ 1 - 0
gameplay/src/SpriteBatch.cpp

@@ -1,6 +1,7 @@
 #include "Base.h"
 #include "SpriteBatch.h"
 #include "Game.h"
+#include "Material.h"
 
 // Default size of a newly created sprite batch
 #define SPRITE_BATCH_DEFAULT_SIZE 128