1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "layer_quads.h"
4
5#include "image.h"
6
7#include <game/editor/editor.h>
8#include <game/editor/editor_actions.h>
9
10#include <limits>
11
12CLayerQuads::CLayerQuads(CEditorMap *pMap) :
13 CLayer(pMap, LAYERTYPE_QUADS)
14{
15 m_aName[0] = '\0';
16 m_Image = -1;
17}
18
19CLayerQuads::CLayerQuads(const CLayerQuads &Other) :
20 CLayer(Other)
21{
22 m_Image = Other.m_Image;
23 m_vQuads = Other.m_vQuads;
24}
25
26CLayerQuads::~CLayerQuads() = default;
27
28void CLayerQuads::Render(const CEditorMap *pRenderMap)
29{
30 if(m_Image >= 0 && (size_t)m_Image < pRenderMap->m_vpImages.size())
31 {
32 Graphics()->TextureSet(Texture: pRenderMap->m_vpImages[m_Image]->m_Texture);
33 }
34 else
35 {
36 Graphics()->TextureClear();
37 }
38
39 Editor()->RenderMap()->ForceRenderQuads(pQuads: m_vQuads.data(), NumQuads: m_vQuads.size(), pEnvEval: &pRenderMap->m_EnvelopeEvaluator, Alpha: 1.0f);
40}
41
42CQuad *CLayerQuads::NewQuad(int x, int y, int Width, int Height)
43{
44 Map()->OnModify();
45
46 m_vQuads.emplace_back();
47 CQuad *pQuad = &m_vQuads[m_vQuads.size() - 1];
48
49 pQuad->m_PosEnv = -1;
50 pQuad->m_ColorEnv = -1;
51 pQuad->m_PosEnvOffset = 0;
52 pQuad->m_ColorEnvOffset = 0;
53
54 Width /= 2;
55 Height /= 2;
56 pQuad->m_aPoints[0].x = i2fx(v: x - Width);
57 pQuad->m_aPoints[0].y = i2fx(v: y - Height);
58 pQuad->m_aPoints[1].x = i2fx(v: x + Width);
59 pQuad->m_aPoints[1].y = i2fx(v: y - Height);
60 pQuad->m_aPoints[2].x = i2fx(v: x - Width);
61 pQuad->m_aPoints[2].y = i2fx(v: y + Height);
62 pQuad->m_aPoints[3].x = i2fx(v: x + Width);
63 pQuad->m_aPoints[3].y = i2fx(v: y + Height);
64
65 pQuad->m_aPoints[4].x = i2fx(v: x); // pivot
66 pQuad->m_aPoints[4].y = i2fx(v: y);
67
68 pQuad->m_aTexcoords[0].x = i2fx(v: 0);
69 pQuad->m_aTexcoords[0].y = i2fx(v: 0);
70
71 pQuad->m_aTexcoords[1].x = i2fx(v: 1);
72 pQuad->m_aTexcoords[1].y = i2fx(v: 0);
73
74 pQuad->m_aTexcoords[2].x = i2fx(v: 0);
75 pQuad->m_aTexcoords[2].y = i2fx(v: 1);
76
77 pQuad->m_aTexcoords[3].x = i2fx(v: 1);
78 pQuad->m_aTexcoords[3].y = i2fx(v: 1);
79
80 std::fill(first: std::begin(arr&: pQuad->m_aColors), last: std::end(arr&: pQuad->m_aColors), value: CColor{255, 255, 255, 255});
81
82 return pQuad;
83}
84
85void CLayerQuads::BrushSelecting(CUIRect Rect)
86{
87 Rect.DrawOutline(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
88}
89
90int CLayerQuads::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
91{
92 // create new layers
93 std::shared_ptr<CLayerQuads> pGrabbed = std::make_shared<CLayerQuads>(args: pBrush->Map());
94 pGrabbed->m_Image = m_Image;
95 pBrush->AddLayer(pLayer: pGrabbed);
96
97 for(const auto &Quad : m_vQuads)
98 {
99 float PointX = fx2f(v: Quad.m_aPoints[4].x);
100 float PointY = fx2f(v: Quad.m_aPoints[4].y);
101
102 if(PointX > Rect.x && PointX < Rect.x + Rect.w && PointY > Rect.y && PointY < Rect.y + Rect.h)
103 {
104 CQuad NewQuad = Quad;
105 for(auto &Point : NewQuad.m_aPoints)
106 {
107 Point.x -= f2fx(v: Rect.x);
108 Point.y -= f2fx(v: Rect.y);
109 }
110
111 pGrabbed->m_vQuads.push_back(x: NewQuad);
112 }
113 }
114
115 return pGrabbed->m_vQuads.empty() ? 0 : 1;
116}
117
118void CLayerQuads::BrushPlace(CLayer *pBrush, vec2 WorldPos)
119{
120 if(m_Readonly)
121 return;
122
123 const CLayerQuads *pQuadLayer = static_cast<const CLayerQuads *>(pBrush);
124 std::vector<CQuad> vAddedQuads;
125 vAddedQuads.reserve(n: pQuadLayer->m_vQuads.size());
126 for(const auto &Quad : pQuadLayer->m_vQuads)
127 {
128 CQuad NewQuad = Quad;
129 for(auto &Point : NewQuad.m_aPoints)
130 {
131 Point.x += f2fx(v: WorldPos.x);
132 Point.y += f2fx(v: WorldPos.y);
133 }
134 vAddedQuads.push_back(x: NewQuad);
135 }
136 m_vQuads.insert(position: m_vQuads.end(), first: vAddedQuads.begin(), last: vAddedQuads.end());
137 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionQuadPlace>(args: Map(), args&: Map()->m_SelectedGroup, args&: Map()->m_vSelectedLayers[0], args&: vAddedQuads));
138 Map()->OnModify();
139}
140
141void CLayerQuads::BrushFlipX()
142{
143 // calculate bounding box
144 int LeftBound = std::numeric_limits<int>::max();
145 int RightBound = std::numeric_limits<int>::min();
146 for(auto &Quad : m_vQuads)
147 {
148 for(int PointId = 0; PointId < 4; ++PointId)
149 {
150 LeftBound = std::min(a: Quad.m_aPoints[PointId].x, b: LeftBound);
151 RightBound = std::max(a: Quad.m_aPoints[PointId].x, b: RightBound);
152 }
153 }
154
155 // flip box
156 for(auto &Quad : m_vQuads)
157 {
158 for(auto &Point : Quad.m_aPoints)
159 {
160 Point.x = RightBound - (Point.x - LeftBound);
161 }
162 }
163 Map()->OnModify();
164}
165
166void CLayerQuads::BrushFlipY()
167{
168 // calculate bounding box
169 int TopBound = std::numeric_limits<int>::max();
170 int BottomBound = std::numeric_limits<int>::min();
171 for(auto &Quad : m_vQuads)
172 {
173 for(int PointId = 0; PointId < 4; ++PointId)
174 {
175 TopBound = std::min(a: Quad.m_aPoints[PointId].y, b: TopBound);
176 BottomBound = std::max(a: Quad.m_aPoints[PointId].y, b: BottomBound);
177 }
178 }
179
180 // flip box
181 for(auto &Quad : m_vQuads)
182 {
183 for(auto &Point : Quad.m_aPoints)
184 {
185 Point.y = BottomBound - (Point.y - TopBound);
186 }
187 }
188 Map()->OnModify();
189}
190
191void CLayerQuads::BrushRotate(float Amount)
192{
193 vec2 Center;
194 GetSize(pWidth: &Center.x, pHeight: &Center.y);
195 Center.x /= 2;
196 Center.y /= 2;
197
198 const ivec2 CenterFixed = ivec2(f2fx(v: Center.x), f2fx(v: Center.y));
199
200 for(auto &Quad : m_vQuads)
201 {
202 RotatePoints(Center: CenterFixed, pPoints: Quad.m_aPoints, NumPoints: std::size(Quad.m_aPoints), Rotation: Amount);
203 }
204}
205
206void CLayerQuads::GetSize(float *pWidth, float *pHeight)
207{
208 *pWidth = 0;
209 *pHeight = 0;
210
211 for(const auto &Quad : m_vQuads)
212 {
213 for(const auto &Point : Quad.m_aPoints)
214 {
215 *pWidth = std::max(a: *pWidth, b: fx2f(v: Point.x));
216 *pHeight = std::max(a: *pHeight, b: fx2f(v: Point.y));
217 }
218 }
219}
220
221CUi::EPopupMenuFunctionResult CLayerQuads::RenderProperties(CUIRect *pToolBox)
222{
223 CProperty aProps[] = {
224 {"Image", m_Image, PROPTYPE_IMAGE, -1, 0},
225 {nullptr},
226 };
227
228 static int s_aIds[(int)ELayerQuadsProp::NUM_PROPS] = {0};
229 int NewVal = 0;
230 auto [State, Prop] = Editor()->DoPropertiesWithState<ELayerQuadsProp>(pToolbox: pToolBox, pProps: aProps, pIds: s_aIds, pNewVal: &NewVal);
231 if(Prop != ELayerQuadsProp::NONE && (State == EEditState::END || State == EEditState::ONE_GO))
232 {
233 Map()->OnModify();
234 }
235
236 Map()->m_LayerQuadPropTracker.Begin(pObject: this, Prop, State);
237
238 if(Prop == ELayerQuadsProp::IMAGE)
239 {
240 if(NewVal >= 0)
241 m_Image = NewVal % Map()->m_vpImages.size();
242 else
243 m_Image = -1;
244 }
245
246 Map()->m_LayerQuadPropTracker.End(Prop, State);
247
248 return CUi::POPUP_KEEP_OPEN;
249}
250
251bool CLayerQuads::IsEnvelopeUsed(int EnvelopeIndex) const
252{
253 return std::any_of(first: m_vQuads.begin(), last: m_vQuads.end(), pred: [&](const auto &Quad) {
254 return Quad.m_PosEnv == EnvelopeIndex || Quad.m_ColorEnv == EnvelopeIndex;
255 });
256}
257
258bool CLayerQuads::IsImageUsed(int ImageIndex) const
259{
260 return m_Image == ImageIndex;
261}
262
263void CLayerQuads::ModifyImageIndex(const FIndexModifyFunction &IndexModifyFunction)
264{
265 IndexModifyFunction(&m_Image);
266}
267
268void CLayerQuads::ModifyEnvelopeIndex(const FIndexModifyFunction &IndexModifyFunction)
269{
270 for(auto &Quad : m_vQuads)
271 {
272 IndexModifyFunction(&Quad.m_PosEnv);
273 IndexModifyFunction(&Quad.m_ColorEnv);
274 }
275}
276
277std::shared_ptr<CLayer> CLayerQuads::Duplicate() const
278{
279 return std::make_shared<CLayerQuads>(args: *this);
280}
281
282int CLayerQuads::SwapQuads(int Index0, int Index1)
283{
284 if(Index0 < 0 || Index0 >= (int)m_vQuads.size())
285 return Index0;
286 if(Index1 < 0 || Index1 >= (int)m_vQuads.size())
287 return Index0;
288 if(Index0 == Index1)
289 return Index0;
290 Map()->OnModify();
291 std::swap(a&: m_vQuads[Index0], b&: m_vQuads[Index1]);
292 return Index1;
293}
294
295const char *CLayerQuads::TypeName() const
296{
297 return "quads";
298}
299