Back to index

nux  3.0.0
TextureArea.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 #include "Nux.h"
00023 #include "TextureArea.h"
00024 #include "NuxGraphics/ImageSurface.h"
00025 
00026 namespace nux
00027 {
00028   NUX_IMPLEMENT_OBJECT_TYPE(TextureArea);
00029 
00030   TextureArea::TextureArea(NUX_FILE_LINE_DECL)
00031   : View(NUX_FILE_LINE_PARAM)
00032   {
00033     mouse_down.connect(sigc::mem_fun(this, &TextureArea::RecvMouseDown));
00034     mouse_up.connect(sigc::mem_fun(this, &TextureArea::RecvMouseUp));
00035     mouse_enter.connect(sigc::mem_fun(this, &TextureArea::RecvMouseEnter));
00036     mouse_leave.connect(sigc::mem_fun(this, &TextureArea::RecvMouseLeave));
00037     mouse_click.connect(sigc::mem_fun(this, &TextureArea::RecvMouseClick));
00038     mouse_drag.connect(sigc::mem_fun(this, &TextureArea::RecvMouseDrag));
00039 
00040     paint_layer_ = new ColorLayer(nux::color::Black);
00041     rotation_2d_.Identity();
00042   }
00043 
00044   TextureArea::~TextureArea()
00045   {
00046     if (paint_layer_)
00047       delete paint_layer_;
00048   }
00049 
00050   void TextureArea::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00051   {
00052     // Ability to rotate the widget around its center
00053     graphics_engine.PushModelViewMatrix(Matrix4::TRANSLATE(-GetBaseX() - GetBaseWidth() / 2, -GetBaseY() - GetBaseHeight() / 2, 0));
00054     graphics_engine.PushModelViewMatrix(Get2DRotation());
00055     graphics_engine.PushModelViewMatrix(Matrix4::TRANSLATE(GetBaseX() + GetBaseWidth() / 2, GetBaseY() + GetBaseHeight() / 2, 0));
00056 
00057     // The TextureArea should not render the accumulated background. That is left to the caller.
00058     // GetPainter().PaintBackground(graphics_engine, GetGeometry());
00059 
00060     if (paint_layer_)
00061     {
00062       paint_layer_->SetGeometry(GetGeometry());
00063       GetPainter().RenderSinglePaintLayer(graphics_engine, GetGeometry(), paint_layer_);
00064     }
00065 
00066     graphics_engine.PopModelViewMatrix();
00067     graphics_engine.PopModelViewMatrix();
00068     graphics_engine.PopModelViewMatrix();
00069   }
00070 
00071   void TextureArea::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
00072   {
00073 
00074   }
00075 
00076   void TextureArea::PostDraw(GraphicsEngine &graphics_engine, bool force_draw)
00077   {
00078 
00079   }
00080 
00081   void TextureArea::SetTexture(BaseTexture *texture)
00082   {
00083     NUX_RETURN_IF_NULL(texture);
00084     delete paint_layer_;
00085 
00086     TexCoordXForm texxform;
00087     texxform.SetTexCoordType(TexCoordXForm::OFFSET_COORD);
00088     texxform.SetWrap(TEXWRAP_REPEAT, TEXWRAP_REPEAT);
00089     paint_layer_ = new TextureLayer(texture->GetDeviceTexture(), texxform, color::White);
00090 
00091     QueueDraw();
00092   }
00093 
00094   void TextureArea::SetColor(const Color &color)
00095   {
00096     delete paint_layer_;
00097     paint_layer_ = new ColorLayer(color);
00098     QueueDraw();
00099   }
00100 
00101   void TextureArea::LoadImageFile(const std::string &filename)
00102   {
00103     if (paint_layer_)
00104     {
00105       delete paint_layer_;
00106       paint_layer_ = NULL;
00107     }
00108 
00109     BaseTexture *texture = LoadTextureFromFile(filename.c_str());
00110 
00111     if (texture)
00112     {
00113       TexCoordXForm texxform;
00114       ROPConfig rop;
00115       rop.Blend = true;
00116       rop.SrcBlend = GL_SRC_ALPHA;
00117       rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;
00118 
00119       paint_layer_ = new TextureLayer(texture->GetDeviceTexture(), texxform, color::White, true, rop);
00120       texture->UnReference();
00121     }
00122     else
00123     {
00124       paint_layer_ = new ColorLayer(nux::color::Black);
00125     }
00126   }
00127 
00128   void TextureArea::SetPaintLayer(AbstractPaintLayer *layer)
00129   {
00130     NUX_SAFE_DELETE(paint_layer_);
00131     paint_layer_ = layer->Clone();
00132 
00133     QueueDraw();
00134   }
00135 
00136   AbstractPaintLayer* TextureArea::GetPaintLayer() const
00137   {
00138     if (paint_layer_ == NULL)
00139       return NULL;
00140     return paint_layer_->Clone();
00141   }
00142 
00143 // void TextureArea::SetTexture(const char* TextureFilename)
00144 // {
00145 //     // Who should delete the texture? This class or the user?
00146 //     m_UserTexture = CreateTextureFromFile(TextureFilename);
00147 //     QueueDraw();
00148 // }
00149 
00150   void TextureArea::RecvMouseDown(int x, int y, long button_flags, long key_flags)
00151   {
00152     sigMouseDown.emit(x, y);
00153     QueueDraw();
00154   }
00155 
00156   void TextureArea::RecvMouseClick(int x, int y, long button_flags, long key_flags)
00157   {
00158 
00159   }
00160 
00161   void TextureArea::RecvMouseUp(int x, int y, long button_flags, long key_flags)
00162   {
00163     QueueDraw();
00164   }
00165 
00166   void TextureArea::RecvMouseEnter(int x, int y, long button_flags, long key_flags)
00167   {
00168 
00169   }
00170 
00171   void TextureArea::RecvMouseLeave(int x, int y, long button_flags, long key_flags)
00172   {
00173 
00174   }
00175 
00176   void TextureArea::RecvMouseDrag(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
00177   {
00178     sigMouseDrag.emit(x, y);
00179   }
00180 
00181   void TextureArea::Set2DRotation(float angle)
00182   {
00183     rotation_2d_.Rotate_z(angle);
00184     QueueDraw();
00185   }
00186 
00187   Matrix4 TextureArea::Get2DRotation() const
00188   {
00189     return rotation_2d_;
00190   }
00191 }