- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- File List
- File Members

# CGraphics.hpp

Go to the documentation of this file.

    00001 #ifndef __CGraphics__
    00002 #define __CGraphics__
    00003
    00004 #include <MathTools/CPoint.hpp>
    00005 #include <MathTools/CRect.hpp>
    00006 #include <MathTools/CDimension.hpp>
    00007 #include <MathTools/CPolygon.hpp>
    00008 #include <MathTools/CMathTools.hpp>
    00009 #include <Testing/CTrace.hpp>
    00010 #include "../Windowing/CClip.hpp"
    00011 #include "../Fonts/CFont.hpp"
    00012 #include "IImage.hpp"
    00013 #include "CPen.hpp"
    00014 #include "CBrush.hpp"
    00015 #include "CNativeImage.hpp"
    00016
    00017 //  ===========================================================================
    00018
    00019 using Exponent::MathTools::CPoint;
    00020 using Exponent::MathTools::CRect;
    00021 using Exponent::MathTools::CDimension;
    00022 using Exponent::MathTools::CPolygon;
    00023 using Exponent::MathTools::CMathTools;
    00024 using Exponent::Testing::CTrace;
    00025 using Exponent::GUI::Graphics::IImage;
    00026 using Exponent::GUI::Graphics::CPen;
    00027 using Exponent::GUI::Graphics::CBrush;
    00028 using Exponent::GUI::Graphics::CNativeImage;
    00029 using Exponent::GUI::Windowing::CClip;
    00030 using Exponent::GUI::Fonts::CFont;
    00031
    00032 //  ===========================================================================
    00033
    00034 #ifdef WIN32
    00035     #pragma comment (lib, "Msimg32.lib")
    00036 #endif
    00037
    00038 //  ===========================================================================
    00039
    00040 namespace Exponent
    00041 {
    00042     namespace GUI
    00043     {
    00044         namespace Graphics
    00045         {
    00087             class CGraphics
    00088             {
    00089             public:
    00090
    00091 //  ===========================================================================
    00092
    00097                 enum ETextAlignment
    00098                 {
    00099                     e_leftTopAlign = 0,
    00100                     e_centerTopAlign,
    00101                     e_rightTopAlign,
    00102                     e_leftCenterAlign,
    00103                     e_centerCenterAlign,
    00104                     e_rightCenterAlign,
    00105                     e_leftBottomAlign,
    00106                     e_centerBottomAlign,
    00107                     e_rightBottomAlign,
    00108                     e_defaultLabelText,
    00109                     e_defaultSingleLine,
    00110                 };
    00111
    00115                 CGraphics();
    00116
    00120                 virtual ~CGraphics();
    00121
    00122 //  ===========================================================================
    00123
    00124                 #ifdef WIN32
    00125
    00131                     void initialise(SWindowHandle *windowHandle, HDC drawContext, const CDimension &area);
    00132                 #else
    00133
    00139                     void initialise(SWindowHandle *windowHandle, CGContextRef drawContext, const CDimension &area);
    00140                 #endif
    00141
    00145                 void uninitialise();
    00146
    00147 //  ===========================================================================
    00148
    00194                 void beginAlphaDrawing(const double alpha);
    00195
    00202                 void endAlphaDrawing(const CRect &drawTo = CRect(0, 0, 0, 0), const CRect &offset = CRect(0, 0, 0, 0));
    00203
    00204 //  ===========================================================================
    00205
    00210                 void setBackgroundColour(const CColour &colour) { m_nativeImage->setBackgroundColour(colour); }
    00211
    00212 //  ===========================================================================
    00213
    00218                 void drawPixel(const CPoint &point);
    00219
    00225                 void drawAntiAliasedPixel(const CPoint &point, const long alpha);
    00226
    00231                 void moveTo(const CPoint &point);
    00232
    00238                 void drawLine(const CPoint &start, const CPoint &end);
    00239
    00244                 void drawLine(const CPoint &toThisPoint);
    00245
    00251                 void drawAntiAliasedLine(const CPoint &start, const CPoint &end);
    00252
    00253 //  ===========================================================================
    00254
    00259                 void drawRectangle(const CRect &area);
    00260
    00266                 void drawRoundedRectangle(const CRect &rect, const CDimension &roundingEffect);
    00267
    00272                 void drawPolygon(const CPolygon &polygon);
    00273
    00278                 void drawAntiAliasedPolygon(const CPolygon &polygon);
    00279
    00284                 void drawEllipse(const CRect &area);
    00285
    00293                 void drawArc(const CRect &area, const CPoint &startPos, const CPoint &endPos, const bool clockwise = true);
    00294
    00295 //  ===========================================================================
    00296
    00301                 void fillRectangle(const CRect &rect);
    00302
    00308                 void fillRoundedRectangle(const CRect &rect, const CDimension &roundingEffect);
    00309
    00314                 void fillEllipse(const CRect &rect);
    00315
    00320                 void fillPolygon(const CPolygon &polygon);
    00321
    00322 //  ===========================================================================
    00323
    00328                 void drawFilledBorderedRectangle(const CRect &area);
    00329
    00333                 void drawGradientFilledRectangle(const CRect &area);
    00334
    00335 //  ===========================================================================
    00336
    00343                 void drawImage(IImage *image, const CRect &drawTo, const CRect &offset);
    00344
    00352                 void drawImageWithAlpha(IImage *image, const CRect &drawTo, const CRect &offset, const double alpha);
    00353
    00354 //  ===========================================================================
    00355
    00363                 void drawText(const CString &text, const CRect &area, const CFont *theFont, const ETextAlignment drawOptions = e_defaultLabelText);
    00364
    00369                 void setTextColour(const CAlphaColour &textColour = CAlphaColour::CALPHACOLOUR_BLACK) { m_textColour = textColour; }
    00370
    00371 //  ===========================================================================
    00372
    00377                 CPen *getMutablePen() const;
    00378
    00383                 const CPen *getPen() const;
    00384
    00389                 CBrush *getMutableBrush() const;
    00390
    00395                 const CBrush *getBrush() const;
    00396
    00401                 const CNativeImage *getNativeImage() const { return m_nativeImage; }
    00402
    00407                 CNativeImage *getMutableNativeImage() const { return m_nativeImage; }
    00408
    00409 //  ===========================================================================
    00410
    00415                 void setDrawingAreaOffset(const CPoint &point) { m_graphicsOffset = point; }
    00416
    00421                 void appendToDrawingAreaOffset(const CPoint &point) { m_graphicsOffset.offset(point.getXPosition(), point.getYPosition()); }
    00422
    00427                 void negateFromDrawingArea(const CPoint &point)  { m_graphicsOffset.offset(-point.getXPosition(), -point.getYPosition()); }
    00428
    00432                 void resetDrawingArea() { m_graphicsOffset.setPoint(0, 0); }
    00433
    00438                 const CPoint &getDrawingAreaOffset() const { return m_graphicsOffset;}
    00439
    00440 //  ===========================================================================
    00441
    00446                 void setUpdateArea(const CRect &area) { m_updateArea = area; }
    00447
    00452                 const CRect &getUpdateArea() const { return m_updateArea; }
    00453
    00454 //  ===========================================================================
    00455
    00460                 CClip &getClippingRegion();
    00461
    00462 //  ===========================================================================
    00463
    00464             protected:
    00465
    00466 //  ===========================================================================
    00467
    00468                 #ifdef WIN32
    00469
    00474                     void drawWUAALine(const CPoint &start, const CPoint &end);
    00475
    00476 //  ===========================================================================
    00477
    00484                     FORCEINLINE void drawAAPixelIncreasingX(const long x, const long y, const unsigned long alpha)
    00485                     {
    00486                         CPoint myPoint(x, y);
    00487                         myPoint.offset(m_graphicsOffset);
    00488
    00489                         // Store positions
    00490                         const long xx = myPoint.getXPosition();
    00491                         const long yy = myPoint.getYPosition();
    00492
    00493                         // First we need to get the colour of the pixel to be overwritten
    00494                         static CColour backgroundColour[3];         // Make these true statics
    00495                         static CAlphaColour blend[3];
    00496                         static COLORREF colours[3] = { GetPixel(m_drawContext, xx, yy), GetPixel(m_drawContext, xx + 1, yy), GetPixel(m_drawContext, xx - 1, yy) };
    00497
    00498                         // Get all the alpha pixels
    00499                         backgroundColour[0].setFromColourRef(colours[0]);
    00500                         backgroundColour[1].setFromColourRef(colours[1]);
    00501                         backgroundColour[2].setFromColourRef(colours[2]);
    00502                         CAlphaColour current = m_pen->getColour();
    00503
    00504                         // Now we do the actual alpha blending
    00505                         CAlphaColour::alphaBlend(blend[0], backgroundColour[0], current, 255);
    00506                         CAlphaColour::alphaBlend(blend[1], backgroundColour[1], current, alpha);
    00507                         CAlphaColour::alphaBlend(blend[2], backgroundColour[2], current, 255 - alpha);
    00508
    00509                         // Draw the three pixels
    00510                         m_pen->setColour(blend[0]);
    00511                         SetPixelV(m_drawContext, xx, yy, blend[0].getAsColourRef());
    00512
    00513                         m_pen->setColour(blend[1]);
    00514                         SetPixelV(m_drawContext, xx + 1, yy, blend[1].getAsColourRef());
    00515
    00516                         m_pen->setColour(blend[2]);
    00517                         SetPixelV(m_drawContext, xx - 1, yy, blend[2].getAsColourRef());
    00518
    00519                         // Resotre the old colour
    00520                         m_pen->setColour(current);
    00521                     }
    00522
    00529                     FORCEINLINE void drawAAPixelIncreasingY(const long x, const long y, const unsigned long alpha)
    00530                     {
    00531                         CPoint myPoint(x, y);
    00532                         myPoint.offset(m_graphicsOffset);
    00533
    00534                         // Store positions
    00535                         const long xx = myPoint.getXPosition();
    00536                         const long yy = myPoint.getYPosition();
    00537
    00538                         // First we need to get the colour of the pixel to be overwritten
    00539                         static CColour backgroundColour[3];         // Make these true statics
    00540                         static CAlphaColour blend[3];
    00541                         static COLORREF colours[3] = { GetPixel(m_drawContext, xx, yy), GetPixel(m_drawContext, xx, yy + 1), GetPixel(m_drawContext, xx, yy - 1) };
    00542
    00543                         // Get all the alpha pixels
    00544                         backgroundColour[0].setFromColourRef(colours[0]);
    00545                         backgroundColour[1].setFromColourRef(colours[1]);
    00546                         backgroundColour[2].setFromColourRef(colours[2]);
    00547                         CAlphaColour current = m_pen->getColour();
    00548
    00549                         // Now we do the actual alpha blending
    00550                         CAlphaColour::alphaBlend(blend[0], backgroundColour[0], current, 255);
    00551                         CAlphaColour::alphaBlend(blend[1], backgroundColour[1], current, alpha);
    00552                         CAlphaColour::alphaBlend(blend[2], backgroundColour[2], current, 255 - alpha);
    00553
    00554                         // Draw the three pixels
    00555                         m_pen->setColour(blend[0]);
    00556                         SetPixelV(m_drawContext, xx, yy, blend[0].getAsColourRef());
    00557
    00558                         m_pen->setColour(blend[1]);
    00559                         SetPixelV(m_drawContext, xx, yy + 1, blend[1].getAsColourRef());
    00560
    00561                         m_pen->setColour(blend[2]);
    00562                         SetPixelV(m_drawContext, xx, yy - 1, blend[2].getAsColourRef());
    00563
    00564                         // Resotre the old colour
    00565                         m_pen->setColour(current);
    00566                     }
    00567
    00572                     struct SAlphaGraphics
    00573                     {
    00574                         CPen *m_oldPen;
    00575                         CBrush *m_oldBrush;
    00576                         double m_alphaValue;
    00577                         CNativeImage *m_alphaImage;
    00578                         CClip m_alphaClip;
    00579                     };
    00580                 #endif
    00581
    00582 //  ===========================================================================
    00583
    00584                 CPen *m_pen;
    00585                 CBrush *m_brush;
    00586                 CNativeImage *m_nativeImage;
    00587                 CClip m_clip;
    00589                 CPoint m_graphicsOffset;
    00590                 CRect m_updateArea;
    00591                 CDimension m_rootArea;
    00593                 CAlphaColour m_textColour;
    00595                 bool m_isAlphaBlending;
    00597                 #ifdef WIN32
    00598                     HDC m_drawContext;
    00599                     SAlphaGraphics m_alphaGraphics;
    00600                     bool m_hasGdiPlus;
    00601                     ULONG_PTR m_gdiPlusToken;
    00602                 #else
    00603                     CGContextRef m_theContext;
    00604                 #endif
    00605
    00606 //  ===========================================================================
    00607
    00608             };
    00609         }
    00610     }
    00611 }
    00612 #endif  // End of CGraphics.hpp