Back to index

nux  3.0.0
Painter.h
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 
00023 #ifndef PAINTER_H
00024 #define PAINTER_H
00025 
00026 #include <string>
00027 #include <iostream>
00028 #include <list>
00029 
00030 #include "Utils.h"
00031 #include "NuxGraphics/GraphicsEngine.h"
00032 #include "PaintLayer.h"
00033 
00034 namespace nux
00035 {
00036 
00037   class TextLineRenderer;
00038   class BaseTexture;
00039 
00040   class GeometryPositioning
00041   {
00042   public:
00043     GeometryPositioning();
00044     GeometryPositioning(
00045       HorizontalAlignment horizontal_aligment,
00046       VerticalAlignment vertical_aligment,
00047       bool stretch_horizontal = FALSE,
00048       bool stretch_vertical = FALSE,
00049       int horizontal_margin = 0,
00050       int vertical_margin = 0);
00051     ~GeometryPositioning();
00052 
00053     bool m_stretch_horizontal;  // if TRUE, the content width will be stretched over the width of the container.
00054     bool m_stretch_vertical;    // if TRUE, the content height will be stretched over the height of the container.
00055 
00056     void SetAlignment(HorizontalAlignment ha, VerticalAlignment va)
00057     {
00058       m_vertical_aligment = va;
00059       m_horizontal_aligment = ha;
00060     }
00061 
00062     VerticalAlignment m_vertical_aligment;
00063     HorizontalAlignment m_horizontal_aligment;
00064 
00065     int m_horizontal_margin;    // Add a margin to the horizontal alignment
00066     int m_vertical_margin;      // Add a margin to the vertical alignment
00067   };
00068 
00070 
00078   Geometry ComputeGeometryPositioning(const Geometry &container_geo, const Geometry &content_geo, GeometryPositioning gctx);
00079 
00081   /*
00082       Class storing the states of User interface controls such button, radio, checkbox, combobox.
00083   */
00084   class InteractState
00085   {
00086   public:
00087 
00088     /*
00089        Default constructor. All states are set to false.
00090     */
00091     InteractState();
00092     InteractState(bool on, bool focus, bool prelight, bool disable);
00093     ~InteractState();
00094 
00095     bool is_on;
00096     bool is_focus;
00097     bool is_prelight;
00098     bool is_disable;
00099   };
00100 
00101 
00102 
00103 
00104 // enum PainterBackgroundType
00105 // {
00106 //     eColorBackground = 0,
00107 //     eShapeBackground,
00108 //     eShapeCornerBackground,
00109 //     eTextureBackground,
00110 // };
00111 
00112   class TextureAlignmentStyle
00113   {
00114   public:
00115     TextureAlignmentStyle()
00116     {
00117       horz_alignment = eTACenter;
00118       vert_alignment = eTACenter;
00119       horizontal_margin = 0;
00120       vertical_margin = 0;
00121     };
00122     TextureAlignmentStyle(TextureAlignment horz_align, TextureAlignment vert_align)
00123     {
00124       horz_alignment = horz_align;
00125       vert_alignment = vert_align;
00126       horizontal_margin = 0;
00127       vertical_margin = 0;
00128     };
00129 
00130     ~TextureAlignmentStyle() {};
00131 
00132     enum TextureAlignment horz_alignment;
00133     enum TextureAlignment vert_alignment;
00134     int horizontal_margin;
00135     int vertical_margin;
00136   };
00137 
00138   class BasePainter
00139   {
00140   public:
00141     BasePainter(WindowThread *window_thread);
00142     virtual ~BasePainter();
00143     int intTest;
00144 
00146     virtual void Draw2DTexture(GraphicsEngine &graphics_engine, BaseTexture *Texture, int x, int y) const;
00147     virtual void Draw2DTextureAligned(GraphicsEngine &graphics_engine, BaseTexture *Texture, const Geometry &g, TextureAlignmentStyle tex_align) const;
00148 
00150 
00152     // TEXT PAINTING  //
00154     virtual int PaintColorTextLineEdit(GraphicsEngine &graphics_engine, const Geometry &g, const NString &Str,
00155                                         Color TextColor,
00156                                         bool WriteAlphaChannel,
00157                                         Color SelectedTextColor,
00158                                         Color SelectedTextBackgroundColor,
00159                                         Color TextBlinkColor,
00160                                         Color CursorColor,
00161                                         bool ShowCursor, unsigned int CursorPosition,
00162                                         int offset = 0,
00163                                         int selection_start = 0, int selection_end = 0) const;
00164 
00165     virtual int PaintTextLineStatic(GraphicsEngine &graphics_engine, ObjectPtr<FontTexture> Font, const Geometry &g,
00166                                      const NString &Str,
00167                                      const Color &color = Color(0.0f, 0.0f, 0.0f, 1.0f),
00168                                      bool WriteAlphaChannel = true,
00169                                      TextAlignment alignment = eAlignTextLeft) const;
00170 
00171     void Paint2DQuadColor(GraphicsEngine &graphics_engine, const Geometry &g, const Color &c0) const;
00172     void Paint2DQuadColor(GraphicsEngine &graphics_engine, const Geometry &g, const Color &c0_top_left, const Color &c1_bottom_left, const Color &c2_bottom_right, const Color &c3_top_right) const;
00173 
00174     void Paint2DQuadColor(GraphicsEngine &graphics_engine, int x, int y, int width, int height, const Color &c0) const;
00175     void Paint2DQuadColor(GraphicsEngine &graphics_engine, int x, int y, int width, int height, const Color &c0_top_left, const Color &c1_bottom_left, const Color &c2_bottom_right, const Color &c3_top_right) const;
00176 
00178 
00183     void Paint2DQuadVGradient(GraphicsEngine &graphics_engine, const Geometry &g, Color TopColor, Color BottomColor) const;
00185 
00190     void Paint2DQuadHGradient(GraphicsEngine &graphics_engine, const Geometry &g, Color LeftColor, Color RightColor) const;
00191 
00192     void Paint2DQuadWireframe(GraphicsEngine &graphics_engine, const Geometry &g, Color c0) const;
00193     void Paint2DQuadWireframe(GraphicsEngine &graphics_engine, const Geometry &g, Color c_top_left, Color c_bottom_left, Color c_bottom_right, Color c_top_right) const;
00194 
00195     void Paint2DQuadWireframe(GraphicsEngine &graphics_engine, int x, int y, int width, int height, Color c0) const;
00196     void Paint2DQuadWireframe(GraphicsEngine &graphics_engine, int x, int y, int width, int height, Color c_top_left, Color c_bottom_left, Color c_bottom_right, Color c_top_right) const;
00197 
00198     void Draw2DTriangleColor(GraphicsEngine &graphics_engine, int x0, int y0,
00199                               int x1, int y1,
00200                               int x2, int y2,
00201                               Color c0);
00202     void Draw2DTriangleColor(GraphicsEngine &graphics_engine, int x0, int y0,
00203                               int x1, int y1,
00204                               int x2, int y2,
00205                               Color c0, Color c1, Color c2);
00206 
00208     // DRAW LINES       //
00210     void Draw2DLine(GraphicsEngine &graphics_engine, int x0, int y0,
00211                      int x1, int y1, Color c0) const;
00212     void Draw2DLine(GraphicsEngine &graphics_engine, int x0, int y0,
00213                      int x1, int y1, Color c0, Color c1) const;
00214 
00216     // Themes         //
00218     void PaintShape(GraphicsEngine &graphics_engine, const Geometry &geo, const Color &c0, UXStyleImageRef style, bool WriteAlpha = true) const;
00219     void PaintShapeCorner(GraphicsEngine &graphics_engine, const Geometry &geo, const Color &c0, UXStyleImageRef style, long corners, bool WriteAlpha = true) const;
00220     void PaintShapeROP(GraphicsEngine &graphics_engine, const Geometry &geo, const Color &c0, UXStyleImageRef style, bool WriteAlpha = true,
00221                         const ROPConfig &ROP = ROPConfig::Default) const;
00222     void PaintShapeCornerROP(GraphicsEngine &graphics_engine,
00223                             const Geometry &geo,
00224                             const Color &c0,
00225                             UXStyleImageRef style,
00226                             long corners,
00227                             bool WriteAlpha = true,
00228                             const ROPConfig &ROP = ROPConfig::Default) const;
00229 
00230     void PaintTextureShape(GraphicsEngine &graphics_engine, const Geometry &geo, UXStyleImageRef style) const;
00231     void PaintTextureShape(GraphicsEngine &graphics_engine, const Geometry &geo, BaseTexture *Texture,
00232                             int border_left, int border_right, int border_top, int border_bottom, bool draw_borders_only, bool premultiply = true) const;
00233 
00235 
00239     void PaintCheckBox(GraphicsEngine &graphics_engine, const Geometry &geo, const InteractState &interaction_state,
00240                         Color check_mark_color = Color(0x0), Color check_box_color = Color(0x0));
00241 
00242     void PaintRadioButton(GraphicsEngine &graphics_engine, const Geometry &geo, const InteractState &interaction_state,
00243                            Color check_mark_color = Color(0x0), Color check_box_color = Color(0x0));
00244 
00245     void PaintHorizontalGradientQuad(GraphicsEngine &graphics_engine, const Geometry &geo, int num_color, float *percentage_array, Color *color_array);
00246 
00247   public:
00248     void PushColorLayer(GraphicsEngine &graphics_engine, const Geometry &geo,
00249                          Color color,
00250                          bool WriteAlpha = false,
00251                          const ROPConfig &ROP = ROPConfig::Default);
00252 
00253     void PushShapeLayer(GraphicsEngine &graphics_engine, Geometry geo,
00254                          UXStyleImageRef imageStyle,
00255                          const Color &color,
00256                          unsigned long Corners = eAllCorners,
00257                          bool WriteAlpha = false,
00258                          const ROPConfig &ROP = ROPConfig::Default);
00259 
00260     void PushSliceScaledTextureLayer(GraphicsEngine &graphics_engine, Geometry geo,
00261                                       UXStyleImageRef imageStyle,
00262                                       const Color &color,
00263                                       unsigned long Corners = eAllCorners,
00264                                       bool WriteAlpha = false,
00265                                       const ROPConfig &ROP = ROPConfig::Default);
00266 
00267     void PushTextureLayer(GraphicsEngine &graphics_engine, Geometry geo,
00268                            ObjectPtr<IOpenGLBaseTexture> DeviceTexture,
00269                            TexCoordXForm texxform,
00270                            const Color &color,
00271                            bool WriteAlpha = false,
00272                            const ROPConfig &ROP = ROPConfig::Default);
00273 
00274     void PushColorizeTextureLayer(GraphicsEngine &graphics_engine, Geometry geo,
00275                               ObjectPtr<IOpenGLBaseTexture> DeviceTexture,
00276                               TexCoordXForm texxform,
00277                               const Color &color,
00278                               bool WriteAlpha,
00279                               const ROPConfig &ROP,
00280                               const Color &blend_color,
00281                               LayerBlendMode layer_blend_mode);
00282 
00283     void PushDrawColorLayer(GraphicsEngine &graphics_engine, const Geometry &geo,
00284                              Color color,
00285                              bool WriteAlpha = false,
00286                              const ROPConfig &ROP = ROPConfig::Default);
00287 
00288     void PushDrawShapeLayer(GraphicsEngine &graphics_engine, Geometry geo,
00289                              UXStyleImageRef imageStyle,
00290                              const Color &color,
00291                              unsigned long Corners = eAllCorners,
00292                              bool WriteAlpha = false,
00293                              const ROPConfig &ROP = ROPConfig::Default);
00294 
00295     void PushDrawSliceScaledTextureLayer(GraphicsEngine &graphics_engine, Geometry geo,
00296                                           UXStyleImageRef imageStyle,
00297                                           const Color &color,
00298                                           unsigned long Corners = eAllCorners,
00299                                           bool WriteAlpha = false,
00300                                           const ROPConfig &ROP = ROPConfig::Default);
00301 
00302     void PushDrawTextureLayer(GraphicsEngine &graphics_engine, Geometry geo,
00303                                ObjectPtr<IOpenGLBaseTexture> DeviceTexture,
00304                                TexCoordXForm texxform,
00305                                const Color &color,
00306                                bool WriteAlpha = false,
00307                                const ROPConfig &ROP = ROPConfig::Default);
00308 
00309     void PushDrawColorizeTextureLayer(GraphicsEngine& graphics_engine, Geometry geo,
00310                               ObjectPtr<IOpenGLBaseTexture> DeviceTexture,
00311                               TexCoordXForm texxform,
00312                               const Color &color,
00313                               bool WriteAlpha,
00314                               const ROPConfig &ROP,
00315                               const Color &blend_color,
00316                               LayerBlendMode layer_blend_mode);
00317     
00318     void PushCompositionLayer (GraphicsEngine &graphics_engine,
00319                             Geometry geo,
00320                             ObjectPtr <IOpenGLBaseTexture> texture0,
00321                             TexCoordXForm texxform0,
00322                             const Color &color0,
00323                             ObjectPtr <IOpenGLBaseTexture> texture1,
00324                             TexCoordXForm texxform1,
00325                             const Color &color1, 
00326                             LayerBlendMode layer_blend_mode,
00327                             bool WriteAlpha,
00328                             const ROPConfig &ROP);
00329 
00330     void PushDrawCompositionLayer (GraphicsEngine &graphics_engine,
00331                             Geometry geo,
00332                             ObjectPtr <IOpenGLBaseTexture> texture0,
00333                             TexCoordXForm texxform0,
00334                             const Color &color0,
00335                             ObjectPtr <IOpenGLBaseTexture> texture1,
00336                             TexCoordXForm texxform1,
00337                             const Color &color1, 
00338                             LayerBlendMode layer_blend_mode,
00339                             bool WriteAlpha,
00340                             const ROPConfig &ROP);
00341 
00342     void PushCompositionLayer (GraphicsEngine &graphics_engine,
00343                             Geometry geo,
00344                             ObjectPtr <IOpenGLBaseTexture> texture0,
00345                             TexCoordXForm texxform0,
00346                             const Color& color0,
00347                             const Color& blend_color,
00348                             LayerBlendMode layer_blend_mode,
00349                             bool WriteAlpha,
00350                             const ROPConfig &ROP);
00351     
00352     void PushDrawCompositionLayer (GraphicsEngine &graphics_engine,
00353                                Geometry geo,
00354                                ObjectPtr <IOpenGLBaseTexture> texture0,
00355                                TexCoordXForm texxform0,
00356                                const Color &color0,
00357                                const Color& blend_color,
00358                                LayerBlendMode layer_blend_mode,
00359                                bool WriteAlpha,
00360                                const ROPConfig &ROP);
00361 
00362     void PushCompositionLayer (GraphicsEngine &graphics_engine,
00363                             Geometry geo,
00364                             const Color& base_color,
00365                             ObjectPtr <IOpenGLBaseTexture> texture0,
00366                             TexCoordXForm texxform0,
00367                             const Color& color0,
00368                             LayerBlendMode layer_blend_mode,
00369                             bool WriteAlpha,
00370                             const ROPConfig &ROP);
00371     
00372     void PushDrawCompositionLayer (GraphicsEngine &graphics_engine,
00373                                Geometry geo,
00374                                const Color& base_color,
00375                                ObjectPtr <IOpenGLBaseTexture> texture0,
00376                                TexCoordXForm texxform0,
00377                                const Color &color0,
00378                                LayerBlendMode layer_blend_mode,
00379                                bool WriteAlpha,
00380                                const ROPConfig &ROP);
00381 
00382 
00383     void PushLayer(GraphicsEngine &graphics_engine, const Geometry &geo, AbstractPaintLayer *layer);
00384     void PushDrawLayer(GraphicsEngine &graphics_engine, const Geometry &geo, AbstractPaintLayer *layer);
00385 
00387 
00390     void RenderSinglePaintLayer(GraphicsEngine &graphics_engine, Geometry geo, AbstractPaintLayer *paint_layer);
00391 
00393     void PopBackground(int level = 1);
00394 
00396 
00401     void PopPaintLayer(int level = 1);
00402     
00404     void EmptyBackgroundStack();
00405     
00407 
00411     void EmptyActivePaintLayerStack();
00412 
00414     void PaintBackground(GraphicsEngine &graphics_engine, const Geometry &geo);
00415 
00417 
00421     void PaintActivePaintLayerStack(GraphicsEngine &graphics_engine, const Geometry &geo);
00422 
00424     void PushBackgroundStack();
00425 
00427 
00430     void PushPaintLayerStack();
00431     
00433     void PopBackgroundStack();
00434 
00436 
00439     void PopPaintLayerStack();
00440 
00441   private:
00442     
00444 
00448     void EmptyPushedPaintLayerStack();
00449     std::list<AbstractPaintLayer*> active_paint_layer_stack_;
00450     std::list<std::list<AbstractPaintLayer*> > pushed_paint_layer_stack_;
00451 
00452     WindowThread *window_thread_; 
00453 
00454   };
00455 
00456 
00457   class PushBackgroundScope
00458   {
00459   public:
00460     PushBackgroundScope(BasePainter &painter, GraphicsEngine &graphics_engine, const Geometry &geo, UXStyleImageRef image_style, bool PushAndDraw = false)
00461       : m_painter(painter)
00462     {
00463       if (PushAndDraw)
00464         m_painter.PushDrawShapeLayer(graphics_engine, geo, image_style, color::White, eAllCorners);
00465       else
00466         m_painter.PushShapeLayer(graphics_engine, geo, image_style, color::White, eAllCorners);
00467     }
00468 
00469     ~PushBackgroundScope()
00470     {
00471       //m_painter.PopBackground();
00472     }
00473 
00474   private:
00475     BasePainter &m_painter;
00476   };
00477 
00478   class PushShapeBackgroundScope
00479   {
00480   public:
00481     PushShapeBackgroundScope(BasePainter &painter, GraphicsEngine &graphics_engine, const Geometry &geo, UXStyleImageRef image_style, const Color &color, bool PushAndDraw = false, bool WriteAlpha = false, const ROPConfig &ROP = ROPConfig::Default)
00482       : m_painter(painter)
00483     {
00484       if (PushAndDraw)
00485         m_painter.PushDrawShapeLayer(graphics_engine, geo, image_style, color, eAllCorners, WriteAlpha, ROP);
00486       else
00487         m_painter.PushShapeLayer(graphics_engine, geo, image_style, color, eAllCorners, WriteAlpha, ROP);
00488     }
00489     ~PushShapeBackgroundScope()
00490     {
00491       m_painter.PopBackground();
00492     }
00493   private:
00494     BasePainter &m_painter;
00495   };
00496 
00497   class PushShapeCornerBackgroundScope
00498   {
00499   public:
00500     PushShapeCornerBackgroundScope(BasePainter &painter, GraphicsEngine &graphics_engine, const Geometry &geo, UXStyleImageRef image_style, const Color &color, long corners, bool PushAndDraw = false, bool WriteAlpha = false, const ROPConfig &ROP = ROPConfig::Default)
00501       : m_painter(painter)
00502     {
00503       if (PushAndDraw)
00504         m_painter.PushDrawShapeLayer(graphics_engine, geo, image_style, color, corners, WriteAlpha, ROP);
00505       else
00506         m_painter.PushShapeLayer(graphics_engine, geo, image_style, color, corners, WriteAlpha, ROP);
00507     }
00508     ~PushShapeCornerBackgroundScope()
00509     {
00510       m_painter.PopBackground();
00511     }
00512   private:
00513     BasePainter &m_painter;
00514   };
00515 
00516   class PushColorBackgroundScope
00517   {
00518   public:
00519     PushColorBackgroundScope(BasePainter &painter, GraphicsEngine &graphics_engine, const Geometry &geo, const Color &color, bool PushAndDraw = false, bool WriteAlpha = false, const ROPConfig &ROP = ROPConfig::Default)
00520       : m_painter(painter)
00521     {
00522       if (PushAndDraw)
00523         m_painter.PushDrawColorLayer(graphics_engine, geo, color, WriteAlpha, ROP);
00524       else
00525         m_painter.PushColorLayer(graphics_engine, geo, color, WriteAlpha, ROP);
00526     }
00527 
00528     ~PushColorBackgroundScope()
00529     {
00530       m_painter.PopBackground();
00531     }
00532   private:
00533     BasePainter &m_painter;
00534   };
00535 
00536 
00537 }
00538 
00539 #endif // PAINTER_H