blob: 1322fe49b3d7d2f534b9b9b12fdd9aa309b43815 [file] [log] [blame]
reed@android.comf76bacf2009-05-13 14:00:33 +00001#include "SkGroupShape.h"
2
3SkGroupShape::SkGroupShape() {}
4
5SkGroupShape::~SkGroupShape() {
6 this->removeAllShapes();
7}
8
9int SkGroupShape::countShapes() const {
10 return fList.count();
11}
12
13SkShape* SkGroupShape::getShape(int index) const {
14 if ((unsigned)index < (unsigned)fList.count()) {
15 return fList[index];
16 }
17 return NULL;
18}
19
20SkShape* 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
37void SkGroupShape::removeShape(int index) {
38 if ((unsigned)index < (unsigned)fList.count()) {
39 fList[index]->unref();
40 fList.remove(index);
41 }
42}
43
44void SkGroupShape::removeAllShapes() {
45 fList.unrefAll();
46 fList.reset();
47}
48
49///////////////////////////////////////////////////////////////////////////////
50
51void 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
60SkFlattenable::Factory SkGroupShape::getFactory() {
61 return CreateProc;
62}
63
64void 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
75SkGroupShape::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
84SkFlattenable* SkGroupShape::CreateProc(SkFlattenableReadBuffer& buffer) {
85 return SkNEW_ARGS(SkGroupShape, (buffer));
86}
87