blob: 36eccc78732646d0d5e2362f9df1615311aff2d7 [file] [log] [blame]
reed@android.com86d40082010-02-12 17:17:10 +00001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkLayer_DEFINED
18#define SkLayer_DEFINED
19
20#include "SkRefCnt.h"
21#include "SkTDArray.h"
22#include "SkColor.h"
reed@android.com81dc3312010-02-18 19:32:03 +000023#include "SkMatrix.h"
reed@android.com86d40082010-02-12 17:17:10 +000024#include "SkPoint.h"
25#include "SkRect.h"
26#include "SkSize.h"
27
28class SkCanvas;
29class SkPicture;
30
31
32struct SkLength {
33 enum SkLengthType { Undefined, Auto, Relative, Percent, Fixed, Static, Intrinsic, MinIntrinsic };
34 SkLengthType type;
35 SkScalar value;
36 SkLength() {
37 type = Undefined;
38 value = 0;
39 }
40 bool defined() const {
41 if (type == Undefined)
42 return false;
43 return true;
44 }
45 float calcFloatValue(float max) const {
46 switch (type) {
47 case Percent:
48 return (max * value) / 100.0f;
49 case Fixed:
50 return value;
51 default:
52 return value;
53 }
54 }
55};
56
57class SkLayer : public SkRefCnt {
58
59public:
60 SkLayer();
61 SkLayer(const SkLayer&);
62 virtual ~SkLayer();
63
reed@android.com81dc3312010-02-18 19:32:03 +000064 // deprecated
reed@android.com86d40082010-02-12 17:17:10 +000065 void setTranslation(SkScalar x, SkScalar y) { m_translation.set(x, y); }
66 void setRotation(SkScalar a) { m_angleTransform = a; m_doRotation = true; }
67 void setScale(SkScalar x, SkScalar y) { m_scale.set(x, y); }
reed@android.com81dc3312010-02-18 19:32:03 +000068 SkPoint position() const { return m_position; }
69 SkPoint translation() const { return m_translation; }
70 SkSize size() const { return m_size; }
71 SkRect bounds() const {
72 SkRect rect;
73 rect.set(m_position.fX, m_position.fY,
74 m_position.fX + m_size.width(),
75 m_position.fY + m_size.height());
76 rect.offset(m_translation.fX, m_translation.fY);
77 return rect;
78 }
79
80 const SkSize& getSize() const { return m_size; }
81 void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); }
82
83 SkScalar getOpacity() const { return m_opacity; }
84 void setOpacity(SkScalar opacity) { m_opacity = opacity; }
85
86 const SkPoint& getPosition() const { return m_position; }
reed@android.com86d40082010-02-12 17:17:10 +000087 void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); }
reed@android.com81dc3312010-02-18 19:32:03 +000088
89 const SkPoint& getAnchorPoint() const { return m_anchorPoint; }
reed@android.com86d40082010-02-12 17:17:10 +000090 void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); }
reed@android.com81dc3312010-02-18 19:32:03 +000091
reed@android.com86d40082010-02-12 17:17:10 +000092 virtual void setBackgroundColor(SkColor color) { m_backgroundColor = color; m_backgroundColorSet = true; }
reed@android.com81dc3312010-02-18 19:32:03 +000093
reed@android.com86d40082010-02-12 17:17:10 +000094 void setFixedPosition(SkLength left, SkLength top, SkLength right, SkLength bottom) {
95 m_fixedLeft = left;
96 m_fixedTop = top;
97 m_fixedRight = right;
98 m_fixedBottom = bottom;
99 m_isFixed = true;
100 }
101
reed@android.com81dc3312010-02-18 19:32:03 +0000102 const SkMatrix& getMatrix() const { return fMatrix; }
103 void setMatrix(const SkMatrix&);
reed@android.com86d40082010-02-12 17:17:10 +0000104
reed@android.com81dc3312010-02-18 19:32:03 +0000105 const SkMatrix& getChildrenMatrix() const { return fChildrenMatrix; }
106 void setChildrenMatrix(const SkMatrix&);
reed@android.com86d40082010-02-12 17:17:10 +0000107
108 // children
109
110 int countChildren() const;
111 SkLayer* getChild(int index) const;
112 SkLayer* addChild(SkLayer* child);
113 void removeChildren();
114
115 // paint method
116
reed@android.com81dc3312010-02-18 19:32:03 +0000117 virtual void draw(SkCanvas*, SkScalar opacity, const SkRect* viewPort);
118
119protected:
120 virtual void onSetupCanvas(SkCanvas*, SkScalar opacity, const SkRect*);
121 virtual void onDraw(SkCanvas*, SkScalar opacity, const SkRect* viewPort);
122
123private:
124 SkMatrix fMatrix;
125 SkMatrix fChildrenMatrix;
reed@android.com86d40082010-02-12 17:17:10 +0000126
127public:
128
129 bool m_doRotation;
130 bool m_isFixed;
131 bool m_backgroundColorSet;
132
133 // layers properties
134
135 SkScalar m_angleTransform;
136 SkScalar m_opacity;
137
138 SkSize m_size;
139 SkPoint m_position;
140 SkPoint m_translation;
141 SkPoint m_anchorPoint;
142 SkPoint m_scale;
143
144 SkLength m_fixedLeft;
145 SkLength m_fixedTop;
146 SkLength m_fixedRight;
147 SkLength m_fixedBottom;
148
149 SkColor m_backgroundColor;
150
151 SkTDArray<SkLayer*> m_children;
152};
153
154#endif