diff options
author | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-05-13 14:00:33 +0000 |
---|---|---|
committer | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-05-13 14:00:33 +0000 |
commit | f76bacff7f66724072c67edb185abf9e3add11a0 (patch) | |
tree | 122df275ab6ffea666b3bb8ebbefaeba1cbe64ef /src/shapes | |
parent | 25e9834c03a050afbf339f457b8c401aecb26c0b (diff) |
add SkSize for dimensions
add SkShape baseclass, in the hopes of having SkPicture inherit from that, and
also using shapes as the extension mechanism for things like animated-gif
git-svn-id: http://skia.googlecode.com/svn/trunk@174 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/shapes')
-rw-r--r-- | src/shapes/SkGroupShape.cpp | 87 | ||||
-rw-r--r-- | src/shapes/SkGroupShape.h | 65 | ||||
-rw-r--r-- | src/shapes/SkRectShape.cpp | 85 | ||||
-rw-r--r-- | src/shapes/SkRectShape.h | 55 |
4 files changed, 292 insertions, 0 deletions
diff --git a/src/shapes/SkGroupShape.cpp b/src/shapes/SkGroupShape.cpp new file mode 100644 index 0000000000..1322fe49b3 --- /dev/null +++ b/src/shapes/SkGroupShape.cpp @@ -0,0 +1,87 @@ +#include "SkGroupShape.h" + +SkGroupShape::SkGroupShape() {} + +SkGroupShape::~SkGroupShape() { + this->removeAllShapes(); +} + +int SkGroupShape::countShapes() const { + return fList.count(); +} + +SkShape* SkGroupShape::getShape(int index) const { + if ((unsigned)index < (unsigned)fList.count()) { + return fList[index]; + } + return NULL; +} + +SkShape* SkGroupShape::addShape(int index, SkShape* shape) { + int count = fList.count(); + if (NULL == shape || index < 0 || index > count) { + return shape; + } + + shape->ref(); + SkShape** spot; + if (index == count) { + spot = fList.append(); + } else { + spot = fList.insert(index); + } + *spot = shape; + return shape; +} + +void SkGroupShape::removeShape(int index) { + if ((unsigned)index < (unsigned)fList.count()) { + fList[index]->unref(); + fList.remove(index); + } +} + +void SkGroupShape::removeAllShapes() { + fList.unrefAll(); + fList.reset(); +} + +/////////////////////////////////////////////////////////////////////////////// + +void SkGroupShape::onDraw(SkCanvas* canvas) { + SkShape** iter = fList.begin(); + SkShape** stop = fList.end(); + while (iter < stop) { + (*iter)->draw(canvas); + iter++; + } +} + +SkFlattenable::Factory SkGroupShape::getFactory() { + return CreateProc; +} + +void SkGroupShape::flatten(SkFlattenableWriteBuffer& buffer) { + this->INHERITED::flatten(buffer); + + int count = fList.count(); + buffer.write32(count); + for (int i = 0; i < count; i++) { + buffer.writeFunctionPtr((void*)fList[i]->getFactory()); + fList[i]->flatten(buffer); + } +} + +SkGroupShape::SkGroupShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer){ + int count = buffer.readS32(); + for (int i = 0; i < count; i++) { + SkFlattenable::Factory fact = + (SkFlattenable::Factory)buffer.readFunctionPtr(); + this->appendShape((SkShape*)fact(buffer))->unref(); + } +} + +SkFlattenable* SkGroupShape::CreateProc(SkFlattenableReadBuffer& buffer) { + return SkNEW_ARGS(SkGroupShape, (buffer)); +} + diff --git a/src/shapes/SkGroupShape.h b/src/shapes/SkGroupShape.h new file mode 100644 index 0000000000..de7574b9eb --- /dev/null +++ b/src/shapes/SkGroupShape.h @@ -0,0 +1,65 @@ +#ifndef SkGroupShape_DEFINED +#define SkGroupShape_DEFINED + +#include "SkShape.h" +#include "SkTDArray.h" + +class SkGroupShape : public SkShape { +public: + SkGroupShape(); + virtual ~SkGroupShape(); + + /** Return the number of child shapes in this group + */ + int countShapes() const; + + /** Return the shape at the specified index. Note this does not affect the + owner count of the index'd shape. If index is out of range, returns NULL + */ + SkShape* getShape(int index) const; + + /** Ref the specified shape, and insert it into the child list at the + specified index. If index == countShapes(), then the shape will be + appended to the child list, otherwise if index is out of range, the + shape is not added. Either way, the shape parameter is returned. + + Child shapes are drawn in order, after the parent, so the shape at index + 0 will be drawn first, and the shape at index countShapes() - 1 will be + drawn last. + */ + SkShape* addShape(int index, SkShape*); + + /** Helper method to append a shape, passing countShapes() for the index + */ + SkShape* appendShape(SkShape* shape) { + return this->addShape(this->countShapes(), shape); + } + + /** Unref the specified index, and remove it from the child list. If index + is out of range, does nothing. + */ + void removeShape(int index); + + /** Unrefs and removes all of the child shapes + */ + void removeAllShapes(); + + // overrides + virtual Factory getFactory(); + virtual void flatten(SkFlattenableWriteBuffer&); + +protected: + // overrides + virtual void onDraw(SkCanvas*); + + SkGroupShape(SkFlattenableReadBuffer&); + +private: + SkTDArray<SkShape*> fList; + + static SkFlattenable* CreateProc(SkFlattenableReadBuffer&); + + typedef SkShape INHERITED; +}; + +#endif diff --git a/src/shapes/SkRectShape.cpp b/src/shapes/SkRectShape.cpp new file mode 100644 index 0000000000..16886d4806 --- /dev/null +++ b/src/shapes/SkRectShape.cpp @@ -0,0 +1,85 @@ +#include "SkRectShape.h" +#include "SkCanvas.h" + +SkPaintShape::SkPaintShape() { + fPaint.setAntiAlias(true); +} + +SkRectShape::SkRectShape() { + fBounds.setEmpty(); + fRadii.set(0, 0); +} + +void SkRectShape::setRect(const SkRect& bounds) { + fBounds = bounds; + fRadii.set(0, 0); +} + +void SkRectShape::setOval(const SkRect& bounds) { + fBounds = bounds; + fRadii.set(-SK_Scalar1, -SK_Scalar1); +} + +void SkRectShape::setCircle(SkScalar cx, SkScalar cy, SkScalar radius) { + fBounds.set(cx - radius, cy - radius, cx + radius, cy + radius); + fRadii.set(-SK_Scalar1, -SK_Scalar1); +} + +void SkRectShape::setRRect(const SkRect& bounds, SkScalar rx, SkScalar ry) { + if (rx < 0) { + rx = 0; + } + if (ry < 0) { + ry = 0; + } + + fBounds = bounds; + fRadii.set(rx, ry); +} + +/////////////////////////////////////////////////////////////////////////////// + +void SkRectShape::onDraw(SkCanvas* canvas) { + const SkPaint& paint = this->paint(); + + if (fRadii.fWidth < 0) { + canvas->drawOval(fBounds, paint); + } else if (fRadii.isZero()) { + canvas->drawRect(fBounds, paint); + } else { + canvas->drawRoundRect(fBounds, fRadii.fWidth, fRadii.fHeight, paint); + } +} + +SkFlattenable::Factory SkRectShape::getFactory() { + return CreateProc; +} + +void SkRectShape::flatten(SkFlattenableWriteBuffer& buffer) { + this->INHERITED::flatten(buffer); + + buffer.writeRect(fBounds); + *(SkSize*)buffer.reserve(sizeof(SkSize)) = fRadii; +} + +SkRectShape::SkRectShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { + buffer.read(&fBounds, sizeof(fBounds)); + buffer.read(&fRadii, sizeof(fRadii)); +} + +SkFlattenable* SkRectShape::CreateProc(SkFlattenableReadBuffer& buffer) { + return SkNEW_ARGS(SkRectShape, (buffer)); +} + +/////////////////////////////////////////////////////////////////////////////// + +void SkPaintShape::flatten(SkFlattenableWriteBuffer& buffer) { + this->INHERITED::flatten(buffer); + + fPaint.flatten(buffer); +} + +SkPaintShape::SkPaintShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { + fPaint.unflatten(buffer); +} + diff --git a/src/shapes/SkRectShape.h b/src/shapes/SkRectShape.h new file mode 100644 index 0000000000..dfc07e95c7 --- /dev/null +++ b/src/shapes/SkRectShape.h @@ -0,0 +1,55 @@ +#ifndef SkRectShape_DEFINED +#define SkRectShape_DEFINED + +#include "SkShape.h" +#include "SkPaint.h" +#include "SkSize.h" + +class SkPaintShape : public SkShape { +public: + SkPaintShape(); + + SkPaint& paint() { return fPaint; } + const SkPaint& paint() const { return fPaint; } + + // overrides + virtual void flatten(SkFlattenableWriteBuffer&); + +protected: + SkPaintShape(SkFlattenableReadBuffer& buffer); + +private: + SkPaint fPaint; + + typedef SkShape INHERITED; +}; + +class SkRectShape : public SkPaintShape { +public: + SkRectShape(); + + void setRect(const SkRect&); + void setOval(const SkRect&); + void setCircle(SkScalar x, SkScalar y, SkScalar radius); + void setRRect(const SkRect&, SkScalar rx, SkScalar ry); + + // overrides + virtual Factory getFactory(); + virtual void flatten(SkFlattenableWriteBuffer&); + +protected: + SkRectShape(SkFlattenableReadBuffer&); + + // overrides + virtual void onDraw(SkCanvas*); + +private: + SkRect fBounds; + SkSize fRadii; + + static SkFlattenable* CreateProc(SkFlattenableReadBuffer&); + + typedef SkPaintShape INHERITED; +}; + +#endif |