00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #pragma once
00024
00025 #include <Ogre.h>
00026 #include "CanvasPrerequisites.h"
00027
00028 #ifndef SK_IGNORE_STDINT_DOT_H
00029 #define SK_IGNORE_STDINT_DOT_H
00030 #endif
00031
00032 #include <core/SkBitmap.h>
00033 #include <core/SkDevice.h>
00034 #include <core/SkPaint.h>
00035 #include <core/SkRect.h>
00036 #include <core/SkPath.h>
00037
00038 #include <stack>
00039
00040
00041
00042 namespace Ogre
00043 {
00044 namespace Canvas
00045 {
00046 enum DrawingOperator
00047 {
00048 DrawingOperator_Copy = SkXfermode::kSrcOver_Mode,
00049 DrawingOperator_SourceOver = SkXfermode::kSrcOver_Mode,
00050 DrawingOperator_SourceIn = SkXfermode::kSrcIn_Mode,
00051 DrawingOperator_SourceOut = SkXfermode::kSrc_Mode,
00052 DrawingOperator_SourceATop = SkXfermode::kSrcATop_Mode,
00053 DrawingOperator_DestOver = SkXfermode::kDstOver_Mode,
00054 DrawingOperator_DestIn = SkXfermode::kDstIn_Mode,
00055 DrawingOperator_DestOut = SkXfermode::kDst_Mode,
00056 DrawingOperator_DestATop = SkXfermode::kDstATop_Mode,
00057 DrawingOperator_Xor = SkXfermode::kXor_Mode,
00058 DrawingOperator_PlusDarker = SkXfermode::kDarken_Mode,
00059 DrawingOperator_Highlight = SkXfermode::kSrcOver_Mode,
00060 DrawingOperator_PlusLighter = SkXfermode::kLighten_Mode,
00061 DrawingOperator_Clear = SkXfermode::kClear_Mode
00062 };
00063
00064 enum LineCap
00065 {
00066 LineCap_Butt = SkPaint::kButt_Cap,
00067 LineCap_Round = SkPaint::kRound_Cap,
00068 LineCap_Square = SkPaint::kSquare_Cap
00069 };
00070
00071 enum LineJoin
00072 {
00073 LineJoin_Round = SkPaint::kRound_Join,
00074 LineJoin_Bevel = SkPaint::kBevel_Join,
00075 LineJoin_Miter = SkPaint::kMiter_Join
00076 };
00077
00078 enum LineDash
00079 {
00080 LineDash_Solid,
00081 LineDash_Dotted,
00082 LineDash_Dashed
00083 };
00084
00085 enum Repetition
00086 {
00087 Repetition_Repeat,
00088 Repetition_RepeatX,
00089 Repetition_RepeatY,
00090 Repetition_NoRepeat
00091 };
00092
00093
00094 enum TextAlign
00095 {
00096 TextAlign_Left = SkPaint::kLeft_Align,
00097 TextAlign_Center = SkPaint::kCenter_Align,
00098 TextAlign_Right = SkPaint::kRight_Align
00099 };
00100
00101 enum TextBaseline
00102 {
00103 TextBaseline_Top,
00104 TextBaseline_Hanging,
00105 TextBaseline_Middle,
00106 TextBaseline_Alphabetic,
00107 TextBaseline_Ideographic,
00108 TextBaseline_Bottom
00109 };
00110
00111 enum ColourSource
00112 {
00113 ColourSource_Uniform,
00114 ColourSource_Gradient,
00115 ColourSource_Pattern
00116 };
00117
00118 enum GradientType
00119 {
00120 GradientType_Linear,
00121 GradientType_Radial,
00122 GradientType_RadialWithFocalPoint
00123 };
00124
00125 class _OgreCanvasExport TextMetrics
00126 {
00127 public:
00128 TextMetrics(float xBearing, float yBearing, float width, float height, float xAdvance, float yAdvance);
00129 float getFullWidth();
00130 float getFullHeight();
00131
00132 Ogre::Real xBearing;
00133 Ogre::Real yBearing;
00134 Ogre::Real width;
00135 Ogre::Real height;
00136 Ogre::Real xAdvance;
00137 Ogre::Real yAdvance;
00138 };
00139
00140 class _OgreCanvasExport Colour
00141 {
00142 public:
00143 Colour(const Ogre::ColourValue& color);
00144 Colour(unsigned int r, unsigned int g, unsigned int b, Ogre::Real a = 1.0);
00145 Colour(const std::string& hexa);
00146
00147 Ogre::Real r;
00148 Ogre::Real g;
00149 Ogre::Real b;
00150 Ogre::Real a;
00151 };
00152
00153 typedef std::pair<Ogre::Real, Ogre::ColourValue> ColorStop;
00154
00155 class _OgreCanvasExport Gradient
00156 {
00157
00158 public:
00159 Gradient(float x0, float y0, float x1, float y1);
00160 Gradient(float x0, float y0, float radius0, float x1, float y1, float radius1);
00161 ~Gradient();
00162
00163 void addColorStop(float offset, Ogre::ColourValue color);
00164 friend class Context;
00165
00166 protected:
00167 SkShader* getShader();
00168 void createShader();
00169 void deleteShader();
00170
00171 static bool colorStopsSorting(ColorStop a, ColorStop b);
00172
00173 SkShader* mShader;
00174 GradientType mType;
00175 Ogre::Vector2 mP0;
00176 Ogre::Vector2 mP1;
00177 Ogre::Real mRadius0;
00178 Ogre::Real mRadius1;
00179 std::vector<ColorStop> mColorStops;
00180
00181 };
00182
00183 class _OgreCanvasExport Pattern
00184 {
00185 public:
00186 Pattern(const Ogre::Image& img, Repetition repeat);
00187 ~Pattern();
00188
00189 friend class Context;
00190
00191 protected:
00192 SkShader* getShader() { return mShader; }
00193 SkShader* mShader;
00194 };
00195
00196 class _OgreCanvasExport ImageData
00197 {
00198 public:
00199 ImageData();
00200
00201 int width();
00202 int height();
00203 unsigned char* data();
00204
00205 protected:
00206 unsigned char* mData;
00207 };
00208
00209 class _OgreCanvasExport State
00210 {
00211 public:
00212 State();
00213
00214
00215 Ogre::Real globalAlpha;
00216 DrawingOperator globalCompositeOperation;
00217
00218
00219 Ogre::Real lineWidth;
00220 LineCap lineCap;
00221 LineJoin lineJoin;
00222 Ogre::Real miterLimit;
00223 LineDash lineDash;
00224
00225
00226 ColourSource strokeColourSource;
00227 Ogre::ColourValue strokeColour;
00228 Gradient* strokeGradient;
00229 Pattern* strokePattern;
00230
00231 ColourSource fillColourSource;
00232 Ogre::ColourValue fillColour;
00233 Gradient* fillGradient;
00234 Pattern* fillPattern;
00235
00236
00237 Ogre::Real shadowOffsetX;
00238 Ogre::Real shadowOffsetY;
00239 Ogre::Real shadowBlur;
00240 Ogre::ColourValue shadowColor;
00241
00242
00243
00244
00245
00246 Ogre::Real textSize;
00247 TextAlign textAlign;
00248 TextBaseline textBaseline;
00249
00250
00251
00252
00253 };
00254
00255 class _OgreCanvasExport Context
00256 {
00257 public:
00258 Context(unsigned int _width, unsigned int _height, bool _enableAlpha = true);
00259 ~Context();
00260
00261
00262 void save();
00263 void restore();
00264 void reset();
00265
00266
00267 void scale(float x, float y);
00268 void scale(const Ogre::Vector2& scaling);
00269
00270 void rotate(float radian);
00271 void rotate(const Ogre::Radian& angle);
00272
00273 void translate(float x, float y);
00274 void translate(const Ogre::Vector2& translation);
00275
00276 void transform(float m11, float m12, float m21, float m22, float dx, float dy);
00277 void transform(const Ogre::Matrix3& transform);
00278
00279 void setTransform(float m11, float m12, float m21, float m22, float dx, float dy);
00280 void setTransform(const Ogre::Matrix3& transform);
00281
00282
00283 void drawImage(const Ogre::Image& image, float dstX, float dstY, float dstWidth = 0, float dstHeight = 0);
00284 void drawImage(const Ogre::Image& image, const Ogre::Vector2& pos, const Ogre::Vector2& dim = Ogre::Vector2::ZERO);
00285
00286 void drawImage(const Ogre::Image& image, float srcX, float srcY, float srcWidth, float srcHeight, float dstX, float dstY, float dstWidth, float dstHeight);
00287 void drawImage(const Ogre::Image& image, const Ogre::Vector2& srcPos, const Ogre::Vector2& srcDim, const Ogre::Vector2& dstPos, const Ogre::Vector2& dstDim);
00288
00289
00290 void globalAlpha(float alpha);
00291 float globalAlpha();
00292
00293 void globalCompositeOperation(DrawingOperator op);
00294 DrawingOperator globalCompositeOperation();
00295
00296
00297 void lineWidth(float width);
00298 float lineWidth();
00299
00300 void lineCap(LineCap lineCap);
00301 LineCap lineCap();
00302
00303 void lineJoin(LineJoin lineJoin);
00304 LineJoin lineJoin();
00305
00306 void miterLimit(float limit);
00307 float miterLimit();
00308
00309 void lineDash(LineDash lineDash);
00310 LineDash lineDash();
00311
00312
00313 void strokeStyle(Ogre::ColourValue color);
00314 void strokeStyle(Gradient* gradient);
00315 void strokeStyle(Pattern* pattern);
00316
00317 void fillStyle(Ogre::ColourValue color);
00318 void fillStyle(Gradient* gradient);
00319 void fillStyle(Pattern* pattern);
00320
00321 void shadowOffsetX(float x);
00322 void shadowOffsetY(float y);
00323 void shadowBlur(float blur);
00324 void shadowColor(Ogre::ColourValue color);
00325
00326 Gradient* createLinearGradient(float x0, float y0, float x1, float y1);
00327 Gradient* createLinearGradient(const Ogre::Vector2& pos0, const Ogre::Vector2& pos1);
00328
00329 Gradient* createRadialGradient(float x0, float y0, float radius0, float x1, float y1, float radius1);
00330 Gradient* createRadialGradient(const Ogre::Vector2& pos0, float radius0, const Ogre::Vector2& pos1, float radius1);
00331
00332 Pattern* createPattern(const Ogre::Image& image, Repetition = Repetition_Repeat);
00333
00334
00335 void beginPath();
00336 void closePath();
00337 void fill();
00338 void stroke();
00339 void clip();
00340
00341 void moveTo(float x, float y);
00342 void moveTo(const Ogre::Vector2& pos);
00343
00344 void lineTo(float x, float y);
00345 void lineTo(const Ogre::Vector2& pos);
00346
00347 void quadraticCurveTo(float cpx, float cpy, float x, float y);
00348 void quadraticCurveTo(const Ogre::Vector2& controlPoint, const Ogre::Vector2& pos);
00349
00350 void bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y);
00351 void bezierCurveTo(const Ogre::Vector2& controlPoint1, const Ogre::Vector2& controlPoint2, const Ogre::Vector2& pos);
00352
00353 void arcTo(float x1, float y1, float x2, float y2, float radius);
00354 void arcTo(const Ogre::Vector2& pos1, const Ogre::Vector2& pos2, float radius);
00355
00356 void arc(float x, float y, float radius, float startRadian, float endRadian, bool anticlockwise);
00357 void arc(const Ogre::Vector2& pos, float radius, Ogre::Radian start, Ogre::Radian end, bool anticlockwise);
00358
00359 void rect(float x, float y, float w, float h);
00360 void rect(const Ogre::Vector2& pos, const Ogre::Vector2& dim);
00361
00362 bool isPointInPath(float x, float y);
00363 bool isPointInPath(const Ogre::Vector2& pos);
00364
00365
00366 void textSize(float size);
00367 float textSize();
00368
00369 void textFont(const std::string& family);
00370 std::string textFont();
00371
00372 void textAlign(TextAlign align);
00373 TextAlign textAlign();
00374
00375 void textBaseline(TextBaseline baseline);
00376 TextBaseline textBaseline();
00377
00378 void fillText(const std::string& text, Ogre::Real x, Ogre::Real y, Ogre::Real maxWidth = -1);
00379 void fillText(const std::string& text, const Ogre::Vector2& pos, Ogre::Real maxWidth = -1);
00380
00381 void strokeText(const std::string& text, Ogre::Real x, Ogre::Real y, Ogre::Real maxWidth = -1);
00382 void strokeText(const std::string& text, const Ogre::Vector2& pos, Ogre::Real maxWidth = -1);
00383
00384 TextMetrics measureText(const std::string& text);
00385
00386
00387 void clearRect(Ogre::Real x, Ogre::Real y, Ogre::Real w, Ogre::Real h);
00388 void clearRect(const Ogre::Vector2& pos, const Ogre::Vector2& dim);
00389
00390 void fillRect(Ogre::Real x, Ogre::Real y, Ogre::Real w, Ogre::Real h);
00391 void fillRect(const Ogre::Vector2& pos, const Ogre::Vector2& dim);
00392
00393 void strokeRect(Ogre::Real x, Ogre::Real y, Ogre::Real w, Ogre::Real h);
00394 void strokeRect(const Ogre::Vector2& pos, const Ogre::Vector2& dim);
00395
00396 ImageData createImageData(Ogre::Real width, Ogre::Real height);
00397 ImageData createImageData(const Ogre::Vector2& dim);
00398
00399 ImageData createImageData(ImageData imageData);
00400
00401 ImageData getImageData(Ogre::Real x, Ogre::Real y, Ogre::Real width, Ogre::Real height);
00402 ImageData getImageData(const Ogre::Vector2& pos, const Ogre::Vector2& dim);
00403
00404 void putImageData(ImageData imageData, Ogre::Real x, Ogre::Real y, Ogre::Real dirtyX = 0, Ogre::Real dirtyY = 0);
00405 void putImageData(ImageData imageData, const Ogre::Vector2& pos, const Ogre::Vector2& dirty = Ogre::Vector2::ZERO);
00406
00407
00408 unsigned char* getData();
00409 void saveToFile(const std::string& _filename);
00410 unsigned int width() { return mWidth; }
00411 unsigned int height() { return mHeight; }
00412
00413 protected:
00414 void applyFillStyle();
00415 void applyStrokeStyle();
00416 bool pathHasCurrentPoint(SkPath& path);
00417
00418 State& getCurrentState();
00419
00420 unsigned int mWidth;
00421 unsigned int mHeight;
00422 bool mAlphaEnable;
00423 SkPath mPath;
00424 SkBitmap* mBitmap;
00425 SkDevice* mDevice;
00426 SkCanvas* mCanvas;
00427 SkPaint* mFillStyle;
00428 SkPaint* mStrokeStyle;
00429
00430 std::stack<State> mStates;
00431 };
00432
00433 class _OgreCanvasExport ColourConverter
00434 {
00435 public:
00436 static Ogre::ColourValue fromHexa(const std::string& text);
00437 static Ogre::ColourValue fromRGBA(unsigned int r, unsigned int g, unsigned int b, unsigned int a = 255);
00438 };
00439 }
00440 }