forked from mapmapteam/mapmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapping.h
168 lines (138 loc) · 4.51 KB
/
Mapping.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Mapping.h
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MAPPING_H_
#define MAPPING_H_
#include <QtGlobal>
#include <tr1/memory>
#include "Shape.h"
#include "Paint.h"
#include "UidAllocator.h"
/**
* Mapping is the central concept of this software.
*
* A Mapping represents a relationship between an input Paint and
* and output Shape where the paint (possibly modified by some other
* attributes or an input Shape in the case of TextureMapping) is
* projected on the output shape.
*
* Mapping instances are stacked as layers by the MappingManager. One
* can thus change their opacity level, toggle their visibility, set
* them in "solo" mode and lock them.
*/
class Mapping
{
protected:
/// The input Paint instance.
Paint::ptr _paint;
/// The output Shape instance.
Shape::ptr _shape;
private:
static UidAllocator allocator;
uid _id;
bool _isLocked;
bool _isSolo;
bool _isVisible;
float _opacity;
protected:
/// Constructor.
Mapping(Paint::ptr paint, Shape::ptr shape, uid id=NULL_UID);
public:
typedef std::tr1::shared_ptr<Mapping> ptr;
virtual ~Mapping();
static const UidAllocator& getUidAllocator() { return allocator; }
/**
* Sets up this Mapping: its Paint and its Shape.
* Calls the build() method of its Paint and Shape.
*/
virtual void build() {
_paint->build();
_shape->build();
}
/// The type of the mapping (expressed as a string).
virtual QString getType() const = 0;
/// Returns the paint.
Paint::ptr getPaint() const { return _paint; }
/// Returns the (output) shape.
Shape::ptr getShape() const { return _shape; }
/// Returns true iff the mapping possesses an input (source) shape.
virtual bool hasInputShape() const { return false; }
/// Returns the input (source) shape (if this mapping has one) or a null pointer if not.
virtual Shape::ptr getInputShape() const { return Shape::ptr(); }
uid getId() const { return _id; }
void setLocked(bool locked) { _isLocked = locked; }
void setSolo(bool solo) { _isSolo = solo; }
void setVisible(bool visible) { _isVisible = visible; }
void setOpacity(float opacity) {
Q_ASSERT(0.0f <= opacity && opacity <= 1.0f);
_opacity = opacity;
}
void setPaint(Paint::ptr p) { _paint = p; }
void removePaint() { if (_paint) delete _paint.get(); }
void toggleLocked() { _isLocked = !_isLocked; }
void toggleSolo() { _isSolo = !_isSolo; }
void toggleVisible() { _isVisible = !_isVisible; }
bool isLocked() const { return _isLocked; }
bool isSolo() const { return _isSolo; }
bool isVisible() const { return _isVisible; }
float getOpacity() const { return _opacity; }
};
/**
* Mapping of a Color paint into a shape.
*/
class ColorMapping : public Mapping
{
public:
ColorMapping(Paint::ptr paint, Shape::ptr shape,
uid id=NULL_UID)
: Mapping(paint, shape, id) {}
virtual QString getType() const {
return getShape()->getType() + "_color";
}
};
/**
* Object whose paint is an image texture. In the case of a texture mapping we require
* an additional input shape to specify the area on the image where we pick the pixels.
*/
class TextureMapping : public Mapping
{
private:
Shape::ptr _inputShape;
public:
TextureMapping(Paint::ptr paint,
Shape::ptr shape,
Shape::ptr inputShape, uid id=NULL_UID)
: Mapping(paint, shape, id),
_inputShape(inputShape)
{
// Only supports shape of the same type (for now).
Q_ASSERT(shape->getType() == inputShape->getType());
}
virtual void build() {
Mapping::build();
_inputShape->build();
}
virtual QString getType() const {
return getShape()->getType() + "_texture";
}
public:
virtual bool hasInputShape() const { return true; }
virtual Shape::ptr getInputShape() const { return _inputShape; }
};
#endif /* MAPPING_H_ */