1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_drawinglayer.hxx"
30 
31 #include <drawinglayer/attribute/materialattribute3d.hxx>
32 #include <basegfx/color/bcolor.hxx>
33 
34 //////////////////////////////////////////////////////////////////////////////
35 
36 namespace drawinglayer
37 {
38 	namespace attribute
39 	{
40 		class ImpMaterialAttribute3D
41 		{
42 		public:
43 			// refcounter
44 			sal_uInt32								mnRefCount;
45 
46 			// materialAttribute3D definitions
47 			basegfx::BColor							maColor;				// object color
48 			basegfx::BColor							maSpecular;				// material specular color
49 			basegfx::BColor							maEmission;				// material emissive color
50 			sal_uInt16								mnSpecularIntensity;	// material specular intensity [0..128]
51 
52 			ImpMaterialAttribute3D(const basegfx::BColor& rColor, const basegfx::BColor& rSpecular, const basegfx::BColor& rEmission, sal_uInt16 nSpecularIntensity)
53 			:	mnRefCount(0),
54                 maColor(rColor),
55 				maSpecular(rSpecular),
56 				maEmission(rEmission),
57 				mnSpecularIntensity(nSpecularIntensity)
58 			{
59 			}
60 
61 			ImpMaterialAttribute3D(const basegfx::BColor& rColor)
62 			:	mnRefCount(0),
63                 maColor(rColor),
64 				maSpecular(1.0, 1.0, 1.0),
65 				maEmission(),
66 				mnSpecularIntensity(15)
67 			{
68 			}
69 
70 			// data read access
71 			const basegfx::BColor& getColor() const { return maColor; }
72 			const basegfx::BColor& getSpecular() const { return maSpecular; }
73 			const basegfx::BColor& getEmission() const { return maEmission; }
74 			sal_uInt16 getSpecularIntensity() const { return mnSpecularIntensity; }
75 
76 			bool operator==(const ImpMaterialAttribute3D& rCandidate) const
77 			{
78 				return (getColor() == rCandidate.getColor()
79 					&& getSpecular() == rCandidate.getSpecular()
80 					&& getEmission() == rCandidate.getEmission()
81 					&& getSpecularIntensity() == rCandidate.getSpecularIntensity());
82 			}
83 
84             static ImpMaterialAttribute3D* get_global_default()
85             {
86                 static ImpMaterialAttribute3D* pDefault = 0;
87 
88                 if(!pDefault)
89                 {
90                     pDefault = new ImpMaterialAttribute3D(
91                         basegfx::BColor(),
92                         basegfx::BColor(),
93                         basegfx::BColor(),
94                         0);
95 
96                     // never delete; start with RefCount 1, not 0
97     			    pDefault->mnRefCount++;
98                 }
99 
100                 return pDefault;
101             }
102 		};
103 
104         MaterialAttribute3D::MaterialAttribute3D(
105             const basegfx::BColor& rColor,
106             const basegfx::BColor& rSpecular,
107             const basegfx::BColor& rEmission,
108             sal_uInt16 nSpecularIntensity)
109 		:	mpMaterialAttribute3D(new ImpMaterialAttribute3D(
110                 rColor, rSpecular, rEmission, nSpecularIntensity))
111 		{
112 		}
113 
114 		MaterialAttribute3D::MaterialAttribute3D(
115             const basegfx::BColor& rColor)
116 		:	mpMaterialAttribute3D(new ImpMaterialAttribute3D(rColor))
117 		{
118 		}
119 
120 		MaterialAttribute3D::MaterialAttribute3D()
121         :	mpMaterialAttribute3D(ImpMaterialAttribute3D::get_global_default())
122 		{
123 			mpMaterialAttribute3D->mnRefCount++;
124 		}
125 
126 		MaterialAttribute3D::MaterialAttribute3D(const MaterialAttribute3D& rCandidate)
127 		:	mpMaterialAttribute3D(rCandidate.mpMaterialAttribute3D)
128 		{
129 			mpMaterialAttribute3D->mnRefCount++;
130 		}
131 
132 		MaterialAttribute3D::~MaterialAttribute3D()
133 		{
134 			if(mpMaterialAttribute3D->mnRefCount)
135 			{
136 				mpMaterialAttribute3D->mnRefCount--;
137 			}
138 			else
139 			{
140 				delete mpMaterialAttribute3D;
141 			}
142 		}
143 
144         bool MaterialAttribute3D::isDefault() const
145         {
146             return mpMaterialAttribute3D == ImpMaterialAttribute3D::get_global_default();
147         }
148 
149         MaterialAttribute3D& MaterialAttribute3D::operator=(const MaterialAttribute3D& rCandidate)
150 		{
151 			if(rCandidate.mpMaterialAttribute3D != mpMaterialAttribute3D)
152 			{
153 				if(mpMaterialAttribute3D->mnRefCount)
154 				{
155 					mpMaterialAttribute3D->mnRefCount--;
156 				}
157 				else
158 				{
159 					delete mpMaterialAttribute3D;
160 				}
161 
162 				mpMaterialAttribute3D = rCandidate.mpMaterialAttribute3D;
163 				mpMaterialAttribute3D->mnRefCount++;
164 			}
165 
166 			return *this;
167 		}
168 
169 		bool MaterialAttribute3D::operator==(const MaterialAttribute3D& rCandidate) const
170 		{
171 			if(rCandidate.mpMaterialAttribute3D == mpMaterialAttribute3D)
172 			{
173 				return true;
174 			}
175 
176 			if(rCandidate.isDefault() != isDefault())
177 			{
178 				return false;
179 			}
180 
181 			return (*rCandidate.mpMaterialAttribute3D == *mpMaterialAttribute3D);
182 		}
183 
184 		const basegfx::BColor& MaterialAttribute3D::getColor() const
185 		{
186 			return mpMaterialAttribute3D->getColor();
187 		}
188 
189 		const basegfx::BColor& MaterialAttribute3D::getSpecular() const
190 		{
191 			return mpMaterialAttribute3D->getSpecular();
192 		}
193 
194 		const basegfx::BColor& MaterialAttribute3D::getEmission() const
195 		{
196 			return mpMaterialAttribute3D->getEmission();
197 		}
198 
199 		sal_uInt16 MaterialAttribute3D::getSpecularIntensity() const
200 		{
201 			return mpMaterialAttribute3D->getSpecularIntensity();
202 		}
203 	} // end of namespace attribute
204 } // end of namespace drawinglayer
205 
206 //////////////////////////////////////////////////////////////////////////////
207 // eof
208