| reed@android.com | 86d4008 | 2010-02-12 17:17:10 +0000 | [diff] [blame^] | 1 | /* |
| 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" |
| 23 | #include "SkPoint.h" |
| 24 | #include "SkRect.h" |
| 25 | #include "SkSize.h" |
| 26 | |
| 27 | class SkCanvas; |
| 28 | class SkPicture; |
| 29 | |
| 30 | |
| 31 | struct SkLength { |
| 32 | enum SkLengthType { Undefined, Auto, Relative, Percent, Fixed, Static, Intrinsic, MinIntrinsic }; |
| 33 | SkLengthType type; |
| 34 | SkScalar value; |
| 35 | SkLength() { |
| 36 | type = Undefined; |
| 37 | value = 0; |
| 38 | } |
| 39 | bool defined() const { |
| 40 | if (type == Undefined) |
| 41 | return false; |
| 42 | return true; |
| 43 | } |
| 44 | float calcFloatValue(float max) const { |
| 45 | switch (type) { |
| 46 | case Percent: |
| 47 | return (max * value) / 100.0f; |
| 48 | case Fixed: |
| 49 | return value; |
| 50 | default: |
| 51 | return value; |
| 52 | } |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | class SkLayer : public SkRefCnt { |
| 57 | |
| 58 | public: |
| 59 | SkLayer(); |
| 60 | SkLayer(const SkLayer&); |
| 61 | virtual ~SkLayer(); |
| 62 | |
| 63 | // setters |
| 64 | |
| 65 | void setSize(SkScalar w, SkScalar h) { m_size.set(w, h); } |
| 66 | void setOpacity(SkScalar opacity) { m_opacity = opacity; } |
| 67 | void setTranslation(SkScalar x, SkScalar y) { m_translation.set(x, y); } |
| 68 | void setRotation(SkScalar a) { m_angleTransform = a; m_doRotation = true; } |
| 69 | void setScale(SkScalar x, SkScalar y) { m_scale.set(x, y); } |
| 70 | void setPosition(SkScalar x, SkScalar y) { m_position.set(x, y); } |
| 71 | void setAnchorPoint(SkScalar x, SkScalar y) { m_anchorPoint.set(x, y); } |
| 72 | virtual void setBackgroundColor(SkColor color) { m_backgroundColor = color; m_backgroundColorSet = true; } |
| 73 | void setFixedPosition(SkLength left, SkLength top, SkLength right, SkLength bottom) { |
| 74 | m_fixedLeft = left; |
| 75 | m_fixedTop = top; |
| 76 | m_fixedRight = right; |
| 77 | m_fixedBottom = bottom; |
| 78 | m_isFixed = true; |
| 79 | } |
| 80 | |
| 81 | // getters |
| 82 | |
| 83 | SkPoint position() const { return m_position; } |
| 84 | SkPoint translation() const { return m_translation; } |
| 85 | SkSize size() const { return m_size; } |
| 86 | SkRect bounds() const { |
| 87 | SkRect rect; |
| 88 | rect.set(m_position.fX, m_position.fY, |
| 89 | m_position.fX + m_size.width(), |
| 90 | m_position.fY + m_size.height()); |
| 91 | rect.offset(m_translation.fX, m_translation.fY); |
| 92 | return rect; |
| 93 | } |
| 94 | |
| 95 | // children |
| 96 | |
| 97 | int countChildren() const; |
| 98 | SkLayer* getChild(int index) const; |
| 99 | SkLayer* addChild(SkLayer* child); |
| 100 | void removeChildren(); |
| 101 | |
| 102 | // paint method |
| 103 | |
| 104 | virtual void paintOn(SkPoint offset, SkSize size, SkScalar scale, SkCanvas*) = 0; |
| 105 | |
| 106 | public: |
| 107 | |
| 108 | bool m_doRotation; |
| 109 | bool m_isFixed; |
| 110 | bool m_backgroundColorSet; |
| 111 | |
| 112 | // layers properties |
| 113 | |
| 114 | SkScalar m_angleTransform; |
| 115 | SkScalar m_opacity; |
| 116 | |
| 117 | SkSize m_size; |
| 118 | SkPoint m_position; |
| 119 | SkPoint m_translation; |
| 120 | SkPoint m_anchorPoint; |
| 121 | SkPoint m_scale; |
| 122 | |
| 123 | SkLength m_fixedLeft; |
| 124 | SkLength m_fixedTop; |
| 125 | SkLength m_fixedRight; |
| 126 | SkLength m_fixedBottom; |
| 127 | |
| 128 | SkColor m_backgroundColor; |
| 129 | |
| 130 | SkTDArray<SkLayer*> m_children; |
| 131 | }; |
| 132 | |
| 133 | #endif |