reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame^] | 1 | #include "SkGroupShape.h" |
| 2 | |
| 3 | SkGroupShape::SkGroupShape() {} |
| 4 | |
| 5 | SkGroupShape::~SkGroupShape() { |
| 6 | this->removeAllShapes(); |
| 7 | } |
| 8 | |
| 9 | int SkGroupShape::countShapes() const { |
| 10 | return fList.count(); |
| 11 | } |
| 12 | |
| 13 | SkShape* SkGroupShape::getShape(int index) const { |
| 14 | if ((unsigned)index < (unsigned)fList.count()) { |
| 15 | return fList[index]; |
| 16 | } |
| 17 | return NULL; |
| 18 | } |
| 19 | |
| 20 | SkShape* SkGroupShape::addShape(int index, SkShape* shape) { |
| 21 | int count = fList.count(); |
| 22 | if (NULL == shape || index < 0 || index > count) { |
| 23 | return shape; |
| 24 | } |
| 25 | |
| 26 | shape->ref(); |
| 27 | SkShape** spot; |
| 28 | if (index == count) { |
| 29 | spot = fList.append(); |
| 30 | } else { |
| 31 | spot = fList.insert(index); |
| 32 | } |
| 33 | *spot = shape; |
| 34 | return shape; |
| 35 | } |
| 36 | |
| 37 | void SkGroupShape::removeShape(int index) { |
| 38 | if ((unsigned)index < (unsigned)fList.count()) { |
| 39 | fList[index]->unref(); |
| 40 | fList.remove(index); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void SkGroupShape::removeAllShapes() { |
| 45 | fList.unrefAll(); |
| 46 | fList.reset(); |
| 47 | } |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
| 51 | void SkGroupShape::onDraw(SkCanvas* canvas) { |
| 52 | SkShape** iter = fList.begin(); |
| 53 | SkShape** stop = fList.end(); |
| 54 | while (iter < stop) { |
| 55 | (*iter)->draw(canvas); |
| 56 | iter++; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | SkFlattenable::Factory SkGroupShape::getFactory() { |
| 61 | return CreateProc; |
| 62 | } |
| 63 | |
| 64 | void SkGroupShape::flatten(SkFlattenableWriteBuffer& buffer) { |
| 65 | this->INHERITED::flatten(buffer); |
| 66 | |
| 67 | int count = fList.count(); |
| 68 | buffer.write32(count); |
| 69 | for (int i = 0; i < count; i++) { |
| 70 | buffer.writeFunctionPtr((void*)fList[i]->getFactory()); |
| 71 | fList[i]->flatten(buffer); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | SkGroupShape::SkGroupShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer){ |
| 76 | int count = buffer.readS32(); |
| 77 | for (int i = 0; i < count; i++) { |
| 78 | SkFlattenable::Factory fact = |
| 79 | (SkFlattenable::Factory)buffer.readFunctionPtr(); |
| 80 | this->appendShape((SkShape*)fact(buffer))->unref(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | SkFlattenable* SkGroupShape::CreateProc(SkFlattenableReadBuffer& buffer) { |
| 85 | return SkNEW_ARGS(SkGroupShape, (buffer)); |
| 86 | } |
| 87 | |