SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Material.hpp
1 //
2 // Material.hpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 #ifndef Material_hpp
29 #define Material_hpp
30 
31 #include <stdio.h>
32 #include "SUAPI-CppWrapper/model/Entity.hpp"
33 
34 #include <SketchUpAPI/model/material.h>
35 
36 namespace CW {
37 
38 // Forward declarations
39 class String;
40 class Color;
41 class Texture;
42 
43 class Material :public Entity {
44  private:
45  static SUMaterialRef create_material();
46 
47  /**
48  * Creates a SUMaterialRef derived from an existing Material object.
49  * @param mat - Material object to derive the new SUMaterialRef object from
50  * @return if the Material object is already attached to a model, its SUMaterialRef object will be returned. If the Material object has not been attached to a model, a new SUMaterialRef object will be created. Bear in mind all properties will not be copied in the latter case
51  */
52  static SUMaterialRef copy_reference(const Material& mat);
53 
54  public:
55  Material();
56  Material(SUMaterialRef material_ref, bool attached = true);
57 
58  /** Copy constructor */
59  Material(const Material& other);
60 
61  /** Copy assigment operator */
62  Material& operator=(const Material& other);
63 
64  /**
65  * Destructor
66  */
67  ~Material();
68 
69  SUMaterialRef ref() const;
70 
71  operator SUMaterialRef() const;
72  operator SUMaterialRef*();
73 
74  /**
75  * Returns true if this is an invalid object, or if no material is set. An entitity with the default material would return false.
76  */
77  bool operator !() const;
78 
79  /**
80  * Returns a copy of the Material object, which is not attached to a model.
81  */
82  Material copy() const;
83 
84  /**
85  * Returns the color of the material.
86  */
87  Color color() const;
88 
89  /**
90  * Sets the color of the material.
91  */
92  void color(const Color& color);
93 
94  /**
95  * Returns the internal name of the material. The internal name is the unprocessed identifier string stored with the material.
96  * @return string name of the material.
97  */
98  String name() const;
99 
100  /**
101  * Sets the name of the material.
102  * @param string name of the material.
103  */
104  void name(const String& string);
105 
106  /**
107  * Retrieves the name of a material object. This method was added for users who require the functionality of Material.name() prior to SketchUp 2017, API 5.0. If the internal name is encased in square brackets, [], this method will return the name without brackets, otherwise the name will match the name retrieved by SUMaterialGetName.
108  * @since SketchUp 2017, API 5.0
109  * @return string name of the material.
110  */
111  String display_name() const;
112 
113  /**
114  * Returns the alpha value of the material.
115  * @return opacity The alpha value within range [0.0, 1.0].
116  */
117  double opacity() const;
118 
119  /**
120  * Sets the alpha value of the material.
121  * @param alpha The alpha value within range [0.0, 1.0].
122  */
123  void opacity(const double alpha);
124 
125  /**
126  * Returns the texture of the material
127  */
128  Texture texture() const;
129 
130  /**
131  * Sets the texture of the material
132  */
133  void texture(const Texture& texture);
134 
135  /**
136  * Returns the type of the material
137  */
138  SUMaterialType type() const;
139 
140  /**
141  * Sets the texture of the material
142  */
143  void type(const SUMaterialType& material_type);
144 
145  /**
146  * Returns whether the opacity value is used
147  */
148  bool use_alpha() const;
149 
150  /**
151  * Sets whether the opacity value is used
152  */
153  void use_alpha(bool flag);
154 
155  /**
156  * Hash function for use with unordered_map
157  */
159 };
160 
161 } /* namespace CW */
162 
163 namespace std {
164  template <> struct hash<CW::Material>
165  {
166  size_t operator()(const CW::Material& k) const
167  {
168  static const size_t shift = (size_t)log2(1 + sizeof(CW::Material));
169  return (size_t)(k.m_entity.ptr) >> shift;
170  }
171  };
172 
173 }
174 
175 #endif /* Material_hpp */
bool use_alpha() const
Definition: Material.cpp:298
Texture texture() const
Definition: Material.cpp:248
Material & operator=(const Material &other)
Definition: Material.cpp:88
Material copy() const
Definition: Material.cpp:134
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
STL namespace.
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
double opacity() const
Definition: Material.cpp:219
Color color() const
Definition: Material.cpp:149
Definition: Color.hpp:34
bool operator!() const
Definition: Material.cpp:127
String display_name() const
Definition: Material.cpp:191
SUMaterialType type() const
Definition: Material.cpp:278
String name() const
Definition: Material.cpp:172