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_tiles.h"
4
5#include "image.h"
6
7#include <engine/graphics.h>
8#include <engine/keys.h>
9#include <engine/shared/config.h>
10
11#include <game/editor/editor.h>
12#include <game/editor/editor_actions.h>
13#include <game/editor/enums.h>
14
15#include <iterator>
16#include <numeric>
17
18CLayerTiles::CLayerTiles(CEditorMap *pMap, int w, int h) :
19 CLayer(pMap, LAYERTYPE_TILES)
20{
21 m_aName[0] = '\0';
22 m_Width = w;
23 m_Height = h;
24 m_Image = -1;
25 m_HasGame = false;
26 m_Color.r = 255;
27 m_Color.g = 255;
28 m_Color.b = 255;
29 m_Color.a = 255;
30 m_ColorEnv = -1;
31 m_ColorEnvOffset = 0;
32
33 m_HasTele = false;
34 m_HasSpeedup = false;
35 m_HasFront = false;
36 m_HasSwitch = false;
37 m_HasTune = false;
38 m_AutomapperConfig = -1;
39 m_AutomapperReference = -1;
40 m_Seed = 0;
41 m_AutoAutomapper = false;
42
43 m_pTiles = new CTile[m_Width * m_Height];
44 mem_zero(block: m_pTiles, size: (size_t)m_Width * m_Height * sizeof(CTile));
45}
46
47CLayerTiles::CLayerTiles(const CLayerTiles &Other) :
48 CLayer(Other)
49{
50 m_Width = Other.m_Width;
51 m_Height = Other.m_Height;
52 m_pTiles = new CTile[m_Width * m_Height];
53 mem_copy(dest: m_pTiles, source: Other.m_pTiles, size: (size_t)m_Width * m_Height * sizeof(CTile));
54
55 m_Image = Other.m_Image;
56 m_HasGame = Other.m_HasGame;
57 m_Color = Other.m_Color;
58 m_ColorEnv = Other.m_ColorEnv;
59 m_ColorEnvOffset = Other.m_ColorEnvOffset;
60
61 m_AutomapperConfig = Other.m_AutomapperConfig;
62 m_AutomapperReference = Other.m_AutomapperReference;
63 m_Seed = Other.m_Seed;
64 m_AutoAutomapper = Other.m_AutoAutomapper;
65 m_HasTele = Other.m_HasTele;
66 m_HasSpeedup = Other.m_HasSpeedup;
67 m_HasFront = Other.m_HasFront;
68 m_HasSwitch = Other.m_HasSwitch;
69 m_HasTune = Other.m_HasTune;
70
71 str_copy(dst&: m_aFilename, src: Other.m_aFilename);
72}
73
74CLayerTiles::~CLayerTiles()
75{
76 delete[] m_pTiles;
77}
78
79CTile CLayerTiles::GetTile(int x, int y) const
80{
81 return m_pTiles[y * m_Width + x];
82}
83
84void CLayerTiles::SetTile(int x, int y, CTile Tile)
85{
86 auto CurrentTile = m_pTiles[y * m_Width + x];
87 SetTileIgnoreHistory(x, y, Tile);
88 RecordStateChange(x, y, Previous: CurrentTile, Tile);
89
90 if(m_FillGameTile != -1 && m_LiveGameTiles)
91 {
92 std::shared_ptr<CLayerTiles> pLayer = Map()->m_pGameLayer;
93 if(m_FillGameTile == TILE_TELECHECKIN || m_FillGameTile == TILE_TELECHECKINEVIL)
94 {
95 if(!Map()->m_pTeleLayer)
96 {
97 std::shared_ptr<CLayerTele> pLayerTele = std::make_shared<CLayerTele>(args: Map(), args&: m_Width, args&: m_Height);
98 Map()->MakeTeleLayer(pLayer: pLayerTele);
99 Map()->m_pGameGroup->AddLayer(pLayer: pLayerTele);
100 int GameGroupIndex = std::find(first: Map()->m_vpGroups.begin(), last: Map()->m_vpGroups.end(), val: Map()->m_pGameGroup) - Map()->m_vpGroups.begin();
101 int LayerIndex = Map()->m_vpGroups[GameGroupIndex]->m_vpLayers.size() - 1;
102 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionAddLayer>(args: Map(), args&: GameGroupIndex, args&: LayerIndex));
103 }
104
105 pLayer = Map()->m_pTeleLayer;
106 }
107
108 bool HasTile = Tile.m_Index != 0;
109 pLayer->SetTile(x, y, Tile: CTile{.m_Index: (unsigned char)(HasTile ? m_FillGameTile : TILE_AIR)});
110 }
111}
112
113void CLayerTiles::SetTileIgnoreHistory(int x, int y, CTile Tile) const
114{
115 m_pTiles[y * m_Width + x] = Tile;
116}
117
118void CLayerTiles::RecordStateChange(int x, int y, CTile Previous, CTile Tile)
119{
120 STileStateChange &Change = m_TilesHistory[y][x];
121 if(Change.m_Changed)
122 {
123 Change.m_Current = Tile;
124 }
125 else
126 {
127 Change = STileStateChange{.m_Changed: true, .m_Previous: Previous, .m_Current: Tile};
128 }
129}
130
131void CLayerTiles::PrepareForSave()
132{
133 for(int y = 0; y < m_Height; y++)
134 for(int x = 0; x < m_Width; x++)
135 m_pTiles[y * m_Width + x].m_Flags &= TILEFLAG_XFLIP | TILEFLAG_YFLIP | TILEFLAG_ROTATE;
136
137 if(m_Image != -1 && m_Color.a == 255)
138 {
139 for(int y = 0; y < m_Height; y++)
140 for(int x = 0; x < m_Width; x++)
141 m_pTiles[y * m_Width + x].m_Flags |= Map()->m_vpImages[m_Image]->m_aTileFlags[m_pTiles[y * m_Width + x].m_Index];
142 }
143}
144
145void CLayerTiles::MakePalette() const
146{
147 for(int y = 0; y < m_Height; y++)
148 for(int x = 0; x < m_Width; x++)
149 m_pTiles[y * m_Width + x].m_Index = y * 16 + x;
150}
151
152void CLayerTiles::Render(const CEditorMap *pRenderMap)
153{
154 IGraphics::CTextureHandle Texture;
155 bool RenderOpaque = false;
156 ColorRGBA Color = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
157 if(m_HasGame)
158 {
159 Texture = Editor()->GetEntitiesTexture();
160 }
161 else if(m_HasFront)
162 {
163 Texture = Editor()->GetFrontTexture();
164 }
165 else if(m_HasTele)
166 {
167 Texture = Editor()->GetTeleTexture();
168 }
169 else if(m_HasSpeedup)
170 {
171 Texture = Editor()->GetSpeedupTexture();
172 }
173 else if(m_HasSwitch)
174 {
175 Texture = Editor()->GetSwitchTexture();
176 }
177 else if(m_HasTune)
178 {
179 Texture = Editor()->GetTuneTexture();
180 }
181 else
182 {
183 if(m_Image >= 0 && (size_t)m_Image < pRenderMap->m_vpImages.size())
184 {
185 const auto &pImage = pRenderMap->m_vpImages[m_Image];
186 if(pImage->m_Width % 16 == 0 && pImage->m_Height % 16 == 0)
187 {
188 Texture = pImage->m_Texture;
189 }
190 RenderOpaque = true;
191 }
192
193 ColorRGBA ColorEnv = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
194 pRenderMap->m_EnvelopeEvaluator.EnvelopeEval(TimeOffsetMillis: m_ColorEnvOffset, EnvelopeIndex: m_ColorEnv, Result&: ColorEnv, Channels: 4);
195 Color = ColorRGBA(m_Color.r / 255.0f, m_Color.g / 255.0f, m_Color.b / 255.0f, m_Color.a / 255.0f).Multiply(Other: ColorEnv);
196 if(Color.a <= 254.0f / 255.0f)
197 {
198 RenderOpaque = false;
199 }
200 }
201 Graphics()->TextureSet(Texture);
202
203 if(RenderOpaque)
204 {
205 Graphics()->BlendNone();
206 Editor()->RenderMap()->RenderTilemap(pTiles: m_pTiles, w: m_Width, h: m_Height, Scale: 32.0f, Color, RenderFlags: LAYERRENDERFLAG_OPAQUE);
207 Graphics()->BlendNormal();
208 Editor()->RenderMap()->RenderTilemap(pTiles: m_pTiles, w: m_Width, h: m_Height, Scale: 32.0f, Color, RenderFlags: LAYERRENDERFLAG_TRANSPARENT);
209 }
210 else
211 {
212 Editor()->RenderMap()->RenderTilemap(pTiles: m_pTiles, w: m_Width, h: m_Height, Scale: 32.0f, Color, RenderFlags: 0);
213 }
214
215 // Render DDRace Layers
216 if(m_RenderOverlays)
217 {
218 int OverlayRenderFlags = (g_Config.m_ClTextEntitiesEditor ? OVERLAYRENDERFLAG_TEXT : 0) | OVERLAYRENDERFLAG_EDITOR;
219 if(m_HasTele)
220 Editor()->RenderMap()->RenderTeleOverlay(pTele: static_cast<CLayerTele *>(this)->m_pTeleTile, w: m_Width, h: m_Height, Scale: 32.0f, OverlayRenderFlags);
221 if(m_HasSpeedup)
222 Editor()->RenderMap()->RenderSpeedupOverlay(pSpeedup: static_cast<CLayerSpeedup *>(this)->m_pSpeedupTile, w: m_Width, h: m_Height, Scale: 32.0f, OverlayRenderFlags);
223 if(m_HasSwitch)
224 Editor()->RenderMap()->RenderSwitchOverlay(pSwitch: static_cast<CLayerSwitch *>(this)->m_pSwitchTile, w: m_Width, h: m_Height, Scale: 32.0f, OverlayRenderFlags);
225 if(m_HasTune)
226 Editor()->RenderMap()->RenderTuneOverlay(pTune: static_cast<CLayerTune *>(this)->m_pTuneTile, w: m_Width, h: m_Height, Scale: 32.0f, OverlayRenderFlags);
227 }
228}
229
230int CLayerTiles::ConvertX(float x) const { return (int)(x / 32.0f); }
231int CLayerTiles::ConvertY(float y) const { return (int)(y / 32.0f); }
232
233void CLayerTiles::Convert(CUIRect Rect, CIntRect *pOut) const
234{
235 pOut->x = ConvertX(x: Rect.x);
236 pOut->y = ConvertY(y: Rect.y);
237 pOut->w = ConvertX(x: Rect.x + Rect.w + 31) - pOut->x;
238 pOut->h = ConvertY(y: Rect.y + Rect.h + 31) - pOut->y;
239}
240
241void CLayerTiles::Snap(CUIRect *pRect) const
242{
243 CIntRect Out;
244 Convert(Rect: *pRect, pOut: &Out);
245 pRect->x = Out.x * 32.0f;
246 pRect->y = Out.y * 32.0f;
247 pRect->w = Out.w * 32.0f;
248 pRect->h = Out.h * 32.0f;
249}
250
251void CLayerTiles::Clamp(CIntRect *pRect) const
252{
253 if(pRect->x < 0)
254 {
255 pRect->w += pRect->x;
256 pRect->x = 0;
257 }
258
259 if(pRect->y < 0)
260 {
261 pRect->h += pRect->y;
262 pRect->y = 0;
263 }
264
265 if(pRect->x + pRect->w > m_Width)
266 pRect->w = m_Width - pRect->x;
267
268 if(pRect->y + pRect->h > m_Height)
269 pRect->h = m_Height - pRect->y;
270
271 if(pRect->h < 0)
272 pRect->h = 0;
273 if(pRect->w < 0)
274 pRect->w = 0;
275}
276
277bool CLayerTiles::IsEntitiesLayer() const
278{
279 return Map()->m_pGameLayer.get() == this || Map()->m_pTeleLayer.get() == this || Map()->m_pSpeedupLayer.get() == this || Map()->m_pFrontLayer.get() == this || Map()->m_pSwitchLayer.get() == this || Map()->m_pTuneLayer.get() == this;
280}
281
282bool CLayerTiles::IsEmpty() const
283{
284 for(int y = 0; y < m_Height; y++)
285 {
286 for(int x = 0; x < m_Width; x++)
287 {
288 if(GetTile(x, y).m_Index != 0)
289 {
290 return false;
291 }
292 }
293 }
294 return true;
295}
296
297void CLayerTiles::BrushSelecting(CUIRect Rect)
298{
299 Snap(pRect: &Rect);
300
301 Rect.Draw(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.4f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
302
303 char aBuf[16];
304 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d⨯%d", ConvertX(x: Rect.w), ConvertY(y: Rect.h));
305 TextRender()->Text(x: Rect.x + 3.0f, y: Rect.y + 3.0f, Size: Editor()->m_ShowPicker ? 15.0f : Editor()->MapView()->ScaleLength(Value: 15.0f), pText: aBuf, LineWidth: -1.0f);
306}
307
308template<typename T>
309static void InitGrabbedLayer(std::shared_ptr<T> &pLayer, CLayerTiles *pThisLayer)
310{
311 pLayer->m_Image = pThisLayer->m_Image;
312 pLayer->m_HasGame = pThisLayer->m_HasGame;
313 pLayer->m_HasFront = pThisLayer->m_HasFront;
314 pLayer->m_HasTele = pThisLayer->m_HasTele;
315 pLayer->m_HasSpeedup = pThisLayer->m_HasSpeedup;
316 pLayer->m_HasSwitch = pThisLayer->m_HasSwitch;
317 pLayer->m_HasTune = pThisLayer->m_HasTune;
318 if(pThisLayer->Editor()->m_BrushColorEnabled)
319 {
320 pLayer->m_Color = pThisLayer->m_Color;
321 pLayer->m_Color.a = 255;
322 }
323}
324
325int CLayerTiles::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
326{
327 CIntRect r;
328 Convert(Rect, pOut: &r);
329 Clamp(pRect: &r);
330
331 if(!r.w || !r.h)
332 return 0;
333
334 // create new layers
335 if(m_HasTele)
336 {
337 std::shared_ptr<CLayerTele> pGrabbed = std::make_shared<CLayerTele>(args: pBrush->Map(), args&: r.w, args&: r.h);
338 InitGrabbedLayer(pLayer&: pGrabbed, pThisLayer: this);
339
340 pBrush->AddLayer(pLayer: pGrabbed);
341
342 for(int y = 0; y < r.h; y++)
343 {
344 for(int x = 0; x < r.w; x++)
345 {
346 // copy the tiles
347 pGrabbed->m_pTiles[y * pGrabbed->m_Width + x] = GetTile(x: r.x + x, y: r.y + y);
348
349 // copy the tele data
350 if(!Editor()->m_ShowPicker)
351 {
352 pGrabbed->m_pTeleTile[y * pGrabbed->m_Width + x] = static_cast<CLayerTele *>(this)->m_pTeleTile[(r.y + y) * m_Width + (r.x + x)];
353 unsigned char TgtIndex = pGrabbed->m_pTeleTile[y * pGrabbed->m_Width + x].m_Type;
354 if(IsValidTeleTile(Index: TgtIndex))
355 {
356 if(IsTeleTileNumberUsed(Index: TgtIndex, Checkpoint: false))
357 Editor()->m_TeleNumber = pGrabbed->m_pTeleTile[y * pGrabbed->m_Width + x].m_Number;
358 else if(IsTeleTileNumberUsed(Index: TgtIndex, Checkpoint: true))
359 Editor()->m_TeleCheckpointNumber = pGrabbed->m_pTeleTile[y * pGrabbed->m_Width + x].m_Number;
360 }
361 }
362 else
363 {
364 const CTile &Tile = pGrabbed->m_pTiles[y * pGrabbed->m_Width + x];
365 if(IsValidTeleTile(Index: Tile.m_Index) && IsTeleTileNumberUsedAny(Index: Tile.m_Index))
366 {
367 pGrabbed->m_pTeleTile[y * pGrabbed->m_Width + x].m_Type = Tile.m_Index;
368 pGrabbed->m_pTeleTile[y * pGrabbed->m_Width + x].m_Number = IsTeleTileCheckpoint(Index: Tile.m_Index) ? Editor()->m_TeleCheckpointNumber : Editor()->m_TeleNumber;
369 }
370 }
371 }
372 }
373
374 pGrabbed->m_TeleNumber = Editor()->m_TeleNumber;
375 pGrabbed->m_TeleCheckpointNumber = Editor()->m_TeleCheckpointNumber;
376
377 str_copy(dst&: pGrabbed->m_aFilename, src: pGrabbed->Map()->m_aFilename);
378 }
379 else if(m_HasSpeedup)
380 {
381 std::shared_ptr<CLayerSpeedup> pGrabbed = std::make_shared<CLayerSpeedup>(args: pBrush->Map(), args&: r.w, args&: r.h);
382 InitGrabbedLayer(pLayer&: pGrabbed, pThisLayer: this);
383
384 pBrush->AddLayer(pLayer: pGrabbed);
385
386 for(int y = 0; y < r.h; y++)
387 {
388 for(int x = 0; x < r.w; x++)
389 {
390 // copy the tiles
391 pGrabbed->m_pTiles[y * pGrabbed->m_Width + x] = GetTile(x: r.x + x, y: r.y + y);
392
393 // copy the speedup data
394 if(!Editor()->m_ShowPicker)
395 {
396 pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x] = static_cast<CLayerSpeedup *>(this)->m_pSpeedupTile[(r.y + y) * m_Width + (r.x + x)];
397 if(IsValidSpeedupTile(Index: pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_Type))
398 {
399 Editor()->m_SpeedupAngle = pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_Angle;
400 Editor()->m_SpeedupForce = pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_Force;
401 Editor()->m_SpeedupMaxSpeed = pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_MaxSpeed;
402 }
403 }
404 else
405 {
406 const CTile &Tile = pGrabbed->m_pTiles[y * pGrabbed->m_Width + x];
407 if(IsValidSpeedupTile(Index: Tile.m_Index))
408 {
409 pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_Type = Tile.m_Index;
410 pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_Angle = Editor()->m_SpeedupAngle;
411 pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_Force = Editor()->m_SpeedupForce;
412 pGrabbed->m_pSpeedupTile[y * pGrabbed->m_Width + x].m_MaxSpeed = Editor()->m_SpeedupMaxSpeed;
413 }
414 }
415 }
416 }
417
418 pGrabbed->m_SpeedupForce = Editor()->m_SpeedupForce;
419 pGrabbed->m_SpeedupMaxSpeed = Editor()->m_SpeedupMaxSpeed;
420 pGrabbed->m_SpeedupAngle = Editor()->m_SpeedupAngle;
421 str_copy(dst&: pGrabbed->m_aFilename, src: pGrabbed->Map()->m_aFilename);
422 }
423 else if(m_HasSwitch)
424 {
425 std::shared_ptr<CLayerSwitch> pGrabbed = std::make_shared<CLayerSwitch>(args: pBrush->Map(), args&: r.w, args&: r.h);
426 InitGrabbedLayer(pLayer&: pGrabbed, pThisLayer: this);
427
428 pBrush->AddLayer(pLayer: pGrabbed);
429
430 for(int y = 0; y < r.h; y++)
431 {
432 for(int x = 0; x < r.w; x++)
433 {
434 // copy the tiles
435 pGrabbed->m_pTiles[y * pGrabbed->m_Width + x] = GetTile(x: r.x + x, y: r.y + y);
436
437 // copy the switch data
438 if(!Editor()->m_ShowPicker)
439 {
440 pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x] = static_cast<CLayerSwitch *>(this)->m_pSwitchTile[(r.y + y) * m_Width + (r.x + x)];
441 if(IsValidSwitchTile(Index: pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Type))
442 {
443 Editor()->m_SwitchNumber = pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Number;
444 Editor()->m_SwitchDelay = pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Delay;
445 }
446 }
447 else
448 {
449 const CTile &Tile = pGrabbed->m_pTiles[y * pGrabbed->m_Width + x];
450 if(IsValidSwitchTile(Index: Tile.m_Index))
451 {
452 pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Type = Tile.m_Index;
453 pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Number = Editor()->m_SwitchNumber;
454 pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Delay = Editor()->m_SwitchDelay;
455 pGrabbed->m_pSwitchTile[y * pGrabbed->m_Width + x].m_Flags = Tile.m_Flags;
456 }
457 }
458 }
459 }
460
461 pGrabbed->m_SwitchNumber = Editor()->m_SwitchNumber;
462 pGrabbed->m_SwitchDelay = Editor()->m_SwitchDelay;
463 str_copy(dst&: pGrabbed->m_aFilename, src: pGrabbed->Map()->m_aFilename);
464 }
465
466 else if(m_HasTune)
467 {
468 std::shared_ptr<CLayerTune> pGrabbed = std::make_shared<CLayerTune>(args: pBrush->Map(), args&: r.w, args&: r.h);
469 InitGrabbedLayer(pLayer&: pGrabbed, pThisLayer: this);
470
471 pBrush->AddLayer(pLayer: pGrabbed);
472
473 // copy the tiles
474 for(int y = 0; y < r.h; y++)
475 {
476 for(int x = 0; x < r.w; x++)
477 {
478 pGrabbed->m_pTiles[y * pGrabbed->m_Width + x] = GetTile(x: r.x + x, y: r.y + y);
479
480 if(!Editor()->m_ShowPicker)
481 {
482 pGrabbed->m_pTuneTile[y * pGrabbed->m_Width + x] = static_cast<CLayerTune *>(this)->m_pTuneTile[(r.y + y) * m_Width + (r.x + x)];
483 if(IsValidTuneTile(Index: pGrabbed->m_pTuneTile[y * pGrabbed->m_Width + x].m_Type))
484 {
485 Editor()->m_TuningNumber = pGrabbed->m_pTuneTile[y * pGrabbed->m_Width + x].m_Number;
486 }
487 }
488 else
489 {
490 const CTile &Tile = pGrabbed->m_pTiles[y * pGrabbed->m_Width + x];
491 if(IsValidTuneTile(Index: Tile.m_Index))
492 {
493 pGrabbed->m_pTuneTile[y * pGrabbed->m_Width + x].m_Type = Tile.m_Index;
494 pGrabbed->m_pTuneTile[y * pGrabbed->m_Width + x].m_Number = Editor()->m_TuningNumber;
495 }
496 }
497 }
498 }
499
500 pGrabbed->m_TuningNumber = Editor()->m_TuningNumber;
501 str_copy(dst&: pGrabbed->m_aFilename, src: pGrabbed->Map()->m_aFilename);
502 }
503 else // game, front and tiles layers
504 {
505 std::shared_ptr<CLayerTiles> pGrabbed;
506 if(m_HasGame)
507 {
508 pGrabbed = std::make_shared<CLayerGame>(args: pBrush->Map(), args&: r.w, args&: r.h);
509 }
510 else if(m_HasFront)
511 {
512 pGrabbed = std::make_shared<CLayerFront>(args: pBrush->Map(), args&: r.w, args&: r.h);
513 }
514 else
515 {
516 pGrabbed = std::make_shared<CLayerTiles>(args: pBrush->Map(), args&: r.w, args&: r.h);
517 }
518 InitGrabbedLayer(pLayer&: pGrabbed, pThisLayer: this);
519
520 pBrush->AddLayer(pLayer: pGrabbed);
521
522 // copy the tiles
523 for(int y = 0; y < r.h; y++)
524 for(int x = 0; x < r.w; x++)
525 pGrabbed->m_pTiles[y * pGrabbed->m_Width + x] = GetTile(x: r.x + x, y: r.y + y);
526 str_copy(dst&: pGrabbed->m_aFilename, src: pGrabbed->Map()->m_aFilename);
527 }
528
529 return 1;
530}
531
532void CLayerTiles::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect)
533{
534 if(m_Readonly || (!Empty && pBrush->m_Type != LAYERTYPE_TILES))
535 return;
536
537 Snap(pRect: &Rect);
538
539 int sx = ConvertX(x: Rect.x);
540 int sy = ConvertY(y: Rect.y);
541 int w = ConvertX(x: Rect.w);
542 int h = ConvertY(y: Rect.h);
543
544 CLayerTiles *pLt = static_cast<CLayerTiles *>(pBrush);
545
546 bool Destructive = Editor()->m_BrushDrawDestructive || Empty || pLt->IsEmpty();
547
548 for(int y = 0; y < h; y++)
549 {
550 for(int x = 0; x < w; x++)
551 {
552 int fx = x + sx;
553 int fy = y + sy;
554
555 if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height)
556 continue;
557
558 bool HasTile = GetTile(x: fx, y: fy).m_Index;
559 if(!Empty && pLt->GetTile(x: x % pLt->m_Width, y: y % pLt->m_Height).m_Index == TILE_THROUGH_CUT)
560 {
561 if(m_HasGame && Map()->m_pFrontLayer)
562 {
563 HasTile = HasTile || Map()->m_pFrontLayer->GetTile(x: fx, y: fy).m_Index;
564 }
565 else if(m_HasFront)
566 {
567 HasTile = HasTile || Map()->m_pGameLayer->GetTile(x: fx, y: fy).m_Index;
568 }
569 }
570
571 if(!Destructive && HasTile)
572 continue;
573
574 SetTile(x: fx, y: fy, Tile: Empty ? CTile{.m_Index: TILE_AIR} : pLt->m_pTiles[(y * pLt->m_Width + x % pLt->m_Width) % (pLt->m_Width * pLt->m_Height)]);
575 }
576 }
577 FlagModified(x: sx, y: sy, w, h);
578}
579
580void CLayerTiles::BrushDraw(CLayer *pBrush, vec2 WorldPos)
581{
582 if(m_Readonly)
583 return;
584
585 CLayerTiles *pTileLayer = static_cast<CLayerTiles *>(pBrush);
586 int sx = ConvertX(x: WorldPos.x);
587 int sy = ConvertY(y: WorldPos.y);
588
589 bool Destructive = Editor()->m_BrushDrawDestructive || pTileLayer->IsEmpty();
590
591 for(int y = 0; y < pTileLayer->m_Height; y++)
592 for(int x = 0; x < pTileLayer->m_Width; x++)
593 {
594 int fx = x + sx;
595 int fy = y + sy;
596
597 if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height)
598 continue;
599
600 bool HasTile = GetTile(x: fx, y: fy).m_Index;
601 if(pTileLayer->CLayerTiles::GetTile(x, y).m_Index == TILE_THROUGH_CUT)
602 {
603 if(m_HasGame && Map()->m_pFrontLayer)
604 {
605 HasTile = HasTile || Map()->m_pFrontLayer->GetTile(x: fx, y: fy).m_Index;
606 }
607 else if(m_HasFront)
608 {
609 HasTile = HasTile || Map()->m_pGameLayer->GetTile(x: fx, y: fy).m_Index;
610 }
611 }
612
613 if(!Destructive && HasTile)
614 continue;
615
616 SetTile(x: fx, y: fy, Tile: pTileLayer->CLayerTiles::GetTile(x, y));
617 }
618
619 FlagModified(x: sx, y: sy, w: pTileLayer->m_Width, h: pTileLayer->m_Height);
620}
621
622void CLayerTiles::BrushFlipX()
623{
624 BrushFlipXImpl(pTiles: m_pTiles);
625
626 if(m_HasTele || m_HasSpeedup || m_HasTune)
627 return;
628
629 bool Rotate = !(m_HasGame || m_HasFront || m_HasSwitch) || Editor()->IsAllowPlaceUnusedTiles();
630 for(int y = 0; y < m_Height; y++)
631 for(int x = 0; x < m_Width; x++)
632 if(!Rotate && !IsRotatableTile(Index: m_pTiles[y * m_Width + x].m_Index))
633 m_pTiles[y * m_Width + x].m_Flags = 0;
634 else
635 m_pTiles[y * m_Width + x].m_Flags ^= (m_pTiles[y * m_Width + x].m_Flags & TILEFLAG_ROTATE) ? TILEFLAG_YFLIP : TILEFLAG_XFLIP;
636}
637
638void CLayerTiles::BrushFlipY()
639{
640 BrushFlipYImpl(pTiles: m_pTiles);
641
642 if(m_HasTele || m_HasSpeedup || m_HasTune)
643 return;
644
645 bool Rotate = !(m_HasGame || m_HasFront || m_HasSwitch) || Editor()->IsAllowPlaceUnusedTiles();
646 for(int y = 0; y < m_Height; y++)
647 for(int x = 0; x < m_Width; x++)
648 if(!Rotate && !IsRotatableTile(Index: m_pTiles[y * m_Width + x].m_Index))
649 m_pTiles[y * m_Width + x].m_Flags = 0;
650 else
651 m_pTiles[y * m_Width + x].m_Flags ^= (m_pTiles[y * m_Width + x].m_Flags & TILEFLAG_ROTATE) ? TILEFLAG_XFLIP : TILEFLAG_YFLIP;
652}
653
654void CLayerTiles::BrushRotate(float Amount)
655{
656 int Rotation = (round_to_int(f: 360.0f * Amount / (pi * 2)) / 90) % 4; // 0=0°, 1=90°, 2=180°, 3=270°
657 if(Rotation < 0)
658 Rotation += 4;
659
660 if(Rotation == 1 || Rotation == 3)
661 {
662 // 90° rotation
663 CTile *pTempData = new CTile[m_Width * m_Height];
664 mem_copy(dest: pTempData, source: m_pTiles, size: (size_t)m_Width * m_Height * sizeof(CTile));
665 CTile *pDst = m_pTiles;
666 bool Rotate = !(m_HasGame || m_HasFront) || Editor()->IsAllowPlaceUnusedTiles();
667 for(int x = 0; x < m_Width; ++x)
668 for(int y = m_Height - 1; y >= 0; --y, ++pDst)
669 {
670 *pDst = pTempData[y * m_Width + x];
671 if(!Rotate && !IsRotatableTile(Index: pDst->m_Index))
672 pDst->m_Flags = 0;
673 else
674 {
675 if(pDst->m_Flags & TILEFLAG_ROTATE)
676 pDst->m_Flags ^= (TILEFLAG_YFLIP | TILEFLAG_XFLIP);
677 pDst->m_Flags ^= TILEFLAG_ROTATE;
678 }
679 }
680
681 std::swap(a&: m_Width, b&: m_Height);
682 delete[] pTempData;
683 }
684
685 if(Rotation == 2 || Rotation == 3)
686 {
687 BrushFlipX();
688 BrushFlipY();
689 }
690}
691
692std::shared_ptr<CLayer> CLayerTiles::Duplicate() const
693{
694 return std::make_shared<CLayerTiles>(args: *this);
695}
696
697const char *CLayerTiles::TypeName() const
698{
699 return "tiles";
700}
701
702void CLayerTiles::Resize(int NewW, int NewH)
703{
704 CTile *pNewData = new CTile[NewW * NewH];
705 mem_zero(block: pNewData, size: (size_t)NewW * NewH * sizeof(CTile));
706
707 // copy old data
708 for(int y = 0; y < std::min(a: NewH, b: m_Height); y++)
709 mem_copy(dest: &pNewData[y * NewW], source: &m_pTiles[y * m_Width], size: std::min(a: m_Width, b: NewW) * sizeof(CTile));
710
711 // replace old
712 delete[] m_pTiles;
713 m_pTiles = pNewData;
714 m_Width = NewW;
715 m_Height = NewH;
716
717 // resize tele layer if available
718 if(m_HasGame && Map()->m_pTeleLayer && (Map()->m_pTeleLayer->m_Width != NewW || Map()->m_pTeleLayer->m_Height != NewH))
719 Map()->m_pTeleLayer->Resize(NewW, NewH);
720
721 // resize speedup layer if available
722 if(m_HasGame && Map()->m_pSpeedupLayer && (Map()->m_pSpeedupLayer->m_Width != NewW || Map()->m_pSpeedupLayer->m_Height != NewH))
723 Map()->m_pSpeedupLayer->Resize(NewW, NewH);
724
725 // resize front layer
726 if(m_HasGame && Map()->m_pFrontLayer && (Map()->m_pFrontLayer->m_Width != NewW || Map()->m_pFrontLayer->m_Height != NewH))
727 Map()->m_pFrontLayer->Resize(NewW, NewH);
728
729 // resize switch layer if available
730 if(m_HasGame && Map()->m_pSwitchLayer && (Map()->m_pSwitchLayer->m_Width != NewW || Map()->m_pSwitchLayer->m_Height != NewH))
731 Map()->m_pSwitchLayer->Resize(NewW, NewH);
732
733 // resize tune layer if available
734 if(m_HasGame && Map()->m_pTuneLayer && (Map()->m_pTuneLayer->m_Width != NewW || Map()->m_pTuneLayer->m_Height != NewH))
735 Map()->m_pTuneLayer->Resize(NewW, NewH);
736}
737
738void CLayerTiles::Shift(EShiftDirection Direction)
739{
740 ShiftImpl(pTiles: m_pTiles, Direction, ShiftBy: Map()->m_ShiftBy);
741}
742
743void CLayerTiles::ShowInfo()
744{
745 CScreenRect ScreenRect = Graphics()->GetScreen();
746 Graphics()->TextureSet(Texture: Editor()->Client()->GetDebugFont());
747 Graphics()->QuadsBegin();
748
749 int StartY = std::max(a: 0, b: (int)(ScreenRect.m_TopLeft.y / 32.0f) - 1);
750 int StartX = std::max(a: 0, b: (int)(ScreenRect.m_TopLeft.x / 32.0f) - 1);
751 int EndY = std::min(a: (int)(ScreenRect.m_BottomRight.y / 32.0f) + 1, b: m_Height);
752 int EndX = std::min(a: (int)(ScreenRect.m_BottomRight.x / 32.0f) + 1, b: m_Width);
753
754 for(int y = StartY; y < EndY; y++)
755 for(int x = StartX; x < EndX; x++)
756 {
757 int c = x + y * m_Width;
758 if(m_pTiles[c].m_Index)
759 {
760 char aBuf[4];
761 if(Editor()->m_ShowTileInfo == CEditor::SHOW_TILE_HEXADECIMAL)
762 {
763 str_hex(dst: aBuf, dst_size: sizeof(aBuf), data: &m_pTiles[c].m_Index, data_size: 1);
764 aBuf[2] = '\0'; // would otherwise be a space
765 }
766 else
767 {
768 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", m_pTiles[c].m_Index);
769 }
770 Graphics()->QuadsText(x: x * 32, y: y * 32, Size: 16.0f, pText: aBuf);
771
772 char aFlags[4] = {m_pTiles[c].m_Flags & TILEFLAG_XFLIP ? 'X' : ' ',
773 m_pTiles[c].m_Flags & TILEFLAG_YFLIP ? 'Y' : ' ',
774 m_pTiles[c].m_Flags & TILEFLAG_ROTATE ? 'R' : ' ',
775 0};
776 Graphics()->QuadsText(x: x * 32, y: y * 32 + 16, Size: 16.0f, pText: aFlags);
777 }
778 x += m_pTiles[c].m_Skip;
779 }
780
781 Graphics()->QuadsEnd();
782 Graphics()->MapScreen(ScreenRect);
783}
784
785void CLayerTiles::FillGameTiles(EGameTileOp Fill)
786{
787 if(!CanFillGameTiles())
788 return;
789
790 auto GameTileOpToIndex = [](EGameTileOp Op) -> int {
791 switch(Op)
792 {
793 case EGameTileOp::AIR: return TILE_AIR;
794 case EGameTileOp::HOOKABLE: return TILE_SOLID;
795 case EGameTileOp::DEATH: return TILE_DEATH;
796 case EGameTileOp::UNHOOKABLE: return TILE_NOHOOK;
797 case EGameTileOp::HOOKTHROUGH: return TILE_THROUGH_CUT;
798 case EGameTileOp::FREEZE: return TILE_FREEZE;
799 case EGameTileOp::UNFREEZE: return TILE_UNFREEZE;
800 case EGameTileOp::DEEP_FREEZE: return TILE_DFREEZE;
801 case EGameTileOp::DEEP_UNFREEZE: return TILE_DUNFREEZE;
802 case EGameTileOp::BLUE_CHECK_TELE: return TILE_TELECHECKIN;
803 case EGameTileOp::RED_CHECK_TELE: return TILE_TELECHECKINEVIL;
804 case EGameTileOp::LIVE_FREEZE: return TILE_LFREEZE;
805 case EGameTileOp::LIVE_UNFREEZE: return TILE_LUNFREEZE;
806 default: return -1;
807 }
808 };
809
810 int Result = GameTileOpToIndex(Fill);
811 if(Result > -1)
812 {
813 std::shared_ptr<CLayerGroup> pGroup = Map()->m_vpGroups[Map()->m_SelectedGroup];
814 m_FillGameTile = Result;
815 const int OffsetX = -pGroup->m_OffsetX / 32;
816 const int OffsetY = -pGroup->m_OffsetY / 32;
817
818 std::vector<std::shared_ptr<IEditorAction>> vpActions;
819 std::shared_ptr<CLayerTiles> pGLayer = Map()->m_pGameLayer;
820 int GameLayerIndex = std::find(first: Map()->m_pGameGroup->m_vpLayers.begin(), last: Map()->m_pGameGroup->m_vpLayers.end(), val: pGLayer) - Map()->m_pGameGroup->m_vpLayers.begin();
821 int GameGroupIndex = std::find(first: Map()->m_vpGroups.begin(), last: Map()->m_vpGroups.end(), val: Map()->m_pGameGroup) - Map()->m_vpGroups.begin();
822
823 if(Result != TILE_TELECHECKIN && Result != TILE_TELECHECKINEVIL)
824 {
825 if(pGLayer->m_Width < m_Width + OffsetX || pGLayer->m_Height < m_Height + OffsetY)
826 {
827 std::map<int, std::shared_ptr<CLayer>> SavedLayers;
828 SavedLayers[LAYERTYPE_TILES] = pGLayer->Duplicate();
829 SavedLayers[LAYERTYPE_GAME] = SavedLayers[LAYERTYPE_TILES];
830
831 int PrevW = pGLayer->m_Width;
832 int PrevH = pGLayer->m_Height;
833 const int NewW = pGLayer->m_Width < m_Width + OffsetX ? m_Width + OffsetX : pGLayer->m_Width;
834 const int NewH = pGLayer->m_Height < m_Height + OffsetY ? m_Height + OffsetY : pGLayer->m_Height;
835 pGLayer->Resize(NewW, NewH);
836 vpActions.push_back(x: std::make_shared<CEditorActionEditLayerTilesProp>(args: Map(), args&: GameGroupIndex, args&: GameLayerIndex, args: ETilesProp::WIDTH, args&: PrevW, args: NewW));
837 const std::shared_ptr<CEditorActionEditLayerTilesProp> &Action1 = std::static_pointer_cast<CEditorActionEditLayerTilesProp>(r: vpActions[vpActions.size() - 1]);
838 vpActions.push_back(x: std::make_shared<CEditorActionEditLayerTilesProp>(args: Map(), args&: GameGroupIndex, args&: GameLayerIndex, args: ETilesProp::HEIGHT, args&: PrevH, args: NewH));
839 const std::shared_ptr<CEditorActionEditLayerTilesProp> &Action2 = std::static_pointer_cast<CEditorActionEditLayerTilesProp>(r: vpActions[vpActions.size() - 1]);
840
841 Action1->SetSavedLayers(SavedLayers);
842 Action2->SetSavedLayers(SavedLayers);
843 }
844
845 int Changes = 0;
846 for(int y = OffsetY < 0 ? -OffsetY : 0; y < m_Height; y++)
847 {
848 for(int x = OffsetX < 0 ? -OffsetX : 0; x < m_Width; x++)
849 {
850 if(GetTile(x, y).m_Index)
851 {
852 pGLayer->SetTile(x: x + OffsetX, y: y + OffsetY, Tile: CTile{.m_Index: (unsigned char)Result});
853 Changes++;
854 }
855 }
856 }
857
858 vpActions.push_back(x: std::make_shared<CEditorBrushDrawAction>(args: Map(), args&: GameGroupIndex));
859 char aDisplay[256];
860 str_format(buffer: aDisplay, buffer_size: sizeof(aDisplay), format: "Construct '%s' game tiles (x%d)", GAME_TILE_OP_NAMES[(int)Fill], Changes);
861 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args: Map(), args&: vpActions, args&: aDisplay, args: true));
862 }
863 else
864 {
865 if(!Map()->m_pTeleLayer)
866 {
867 std::shared_ptr<CLayerTele> pLayer = std::make_shared<CLayerTele>(args: Map(), args&: m_Width, args&: m_Height);
868 Map()->MakeTeleLayer(pLayer);
869 Map()->m_pGameGroup->AddLayer(pLayer);
870
871 vpActions.push_back(x: std::make_shared<CEditorActionAddLayer>(args: Map(), args&: GameGroupIndex, args: Map()->m_pGameGroup->m_vpLayers.size() - 1));
872
873 if(m_Width != pGLayer->m_Width || m_Height > pGLayer->m_Height)
874 {
875 std::map<int, std::shared_ptr<CLayer>> SavedLayers;
876 SavedLayers[LAYERTYPE_TILES] = pGLayer->Duplicate();
877 SavedLayers[LAYERTYPE_GAME] = SavedLayers[LAYERTYPE_TILES];
878
879 int NewW = pGLayer->m_Width;
880 int NewH = pGLayer->m_Height;
881 if(m_Width > pGLayer->m_Width)
882 {
883 NewW = m_Width;
884 }
885 if(m_Height > pGLayer->m_Height)
886 {
887 NewH = m_Height;
888 }
889
890 int PrevW = pGLayer->m_Width;
891 int PrevH = pGLayer->m_Height;
892 pLayer->Resize(NewW, NewH);
893 vpActions.push_back(x: std::make_shared<CEditorActionEditLayerTilesProp>(args: Map(), args&: GameGroupIndex, args&: GameLayerIndex, args: ETilesProp::WIDTH, args&: PrevW, args&: NewW));
894 const std::shared_ptr<CEditorActionEditLayerTilesProp> &Action1 = std::static_pointer_cast<CEditorActionEditLayerTilesProp>(r: vpActions[vpActions.size() - 1]);
895 vpActions.push_back(x: std::make_shared<CEditorActionEditLayerTilesProp>(args: Map(), args&: GameGroupIndex, args&: GameLayerIndex, args: ETilesProp::HEIGHT, args&: PrevH, args&: NewH));
896 const std::shared_ptr<CEditorActionEditLayerTilesProp> &Action2 = std::static_pointer_cast<CEditorActionEditLayerTilesProp>(r: vpActions[vpActions.size() - 1]);
897
898 Action1->SetSavedLayers(SavedLayers);
899 Action2->SetSavedLayers(SavedLayers);
900 }
901 }
902
903 std::shared_ptr<CLayerTele> pTLayer = Map()->m_pTeleLayer;
904 int TeleLayerIndex = std::find(first: Map()->m_pGameGroup->m_vpLayers.begin(), last: Map()->m_pGameGroup->m_vpLayers.end(), val: pTLayer) - Map()->m_pGameGroup->m_vpLayers.begin();
905
906 if(pTLayer->m_Width < m_Width + OffsetX || pTLayer->m_Height < m_Height + OffsetY)
907 {
908 std::map<int, std::shared_ptr<CLayer>> SavedLayers;
909 SavedLayers[LAYERTYPE_TILES] = pTLayer->Duplicate();
910 SavedLayers[LAYERTYPE_TELE] = SavedLayers[LAYERTYPE_TILES];
911
912 int PrevW = pTLayer->m_Width;
913 int PrevH = pTLayer->m_Height;
914 int NewW = pTLayer->m_Width < m_Width + OffsetX ? m_Width + OffsetX : pTLayer->m_Width;
915 int NewH = pTLayer->m_Height < m_Height + OffsetY ? m_Height + OffsetY : pTLayer->m_Height;
916 pTLayer->Resize(NewW, NewH);
917 std::shared_ptr<CEditorActionEditLayerTilesProp> Action1, Action2;
918 vpActions.push_back(x: Action1 = std::make_shared<CEditorActionEditLayerTilesProp>(args: Map(), args&: GameGroupIndex, args&: TeleLayerIndex, args: ETilesProp::WIDTH, args&: PrevW, args&: NewW));
919 vpActions.push_back(x: Action2 = std::make_shared<CEditorActionEditLayerTilesProp>(args: Map(), args&: GameGroupIndex, args&: TeleLayerIndex, args: ETilesProp::HEIGHT, args&: PrevH, args&: NewH));
920
921 Action1->SetSavedLayers(SavedLayers);
922 Action2->SetSavedLayers(SavedLayers);
923 }
924
925 int Changes = 0;
926 for(int y = OffsetY < 0 ? -OffsetY : 0; y < m_Height; y++)
927 {
928 for(int x = OffsetX < 0 ? -OffsetX : 0; x < m_Width; x++)
929 {
930 if(GetTile(x, y).m_Index)
931 {
932 auto TileIndex = (y + OffsetY) * pTLayer->m_Width + x + OffsetX;
933 Changes++;
934
935 STeleTileStateChange::SData Previous{
936 .m_Number: pTLayer->m_pTeleTile[TileIndex].m_Number,
937 .m_Type: pTLayer->m_pTeleTile[TileIndex].m_Type,
938 .m_Index: pTLayer->m_pTiles[TileIndex].m_Index};
939
940 pTLayer->m_pTiles[TileIndex].m_Index = TILE_AIR + Result;
941 pTLayer->m_pTeleTile[TileIndex].m_Number = 1;
942 pTLayer->m_pTeleTile[TileIndex].m_Type = TILE_AIR + Result;
943
944 STeleTileStateChange::SData Current{
945 .m_Number: pTLayer->m_pTeleTile[TileIndex].m_Number,
946 .m_Type: pTLayer->m_pTeleTile[TileIndex].m_Type,
947 .m_Index: pTLayer->m_pTiles[TileIndex].m_Index};
948
949 pTLayer->RecordStateChange(x: x + OffsetX, y: y + OffsetY, Previous, Current);
950 }
951 }
952 }
953
954 vpActions.push_back(x: std::make_shared<CEditorBrushDrawAction>(args: Map(), args&: GameGroupIndex));
955 char aDisplay[256];
956 str_format(buffer: aDisplay, buffer_size: sizeof(aDisplay), format: "Construct 'tele' game tiles (x%d)", Changes);
957 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args: Map(), args&: vpActions, args&: aDisplay, args: true));
958 }
959 }
960}
961
962bool CLayerTiles::CanFillGameTiles() const
963{
964 const bool EntitiesLayer = IsEntitiesLayer();
965 if(EntitiesLayer)
966 return false;
967
968 std::shared_ptr<CLayerGroup> pGroup = Map()->m_vpGroups[Map()->m_SelectedGroup];
969
970 // Game tiles can only be constructed if the layer is relative to the game layer
971 return !(pGroup->m_OffsetX % 32) && !(pGroup->m_OffsetY % 32) && pGroup->m_ParallaxX == 100 && pGroup->m_ParallaxY == 100;
972}
973
974CUi::EPopupMenuFunctionResult CLayerTiles::RenderProperties(CUIRect *pToolBox)
975{
976 CUIRect Button;
977
978 const bool EntitiesLayer = IsEntitiesLayer();
979
980 if(CanFillGameTiles())
981 {
982 pToolBox->HSplitBottom(Cut: 12.0f, pTop: pToolBox, pBottom: &Button);
983 static int s_GameTilesButton = 0;
984
985 auto GameTileToOp = [](int TileIndex) -> EGameTileOp {
986 switch(TileIndex)
987 {
988 case TILE_AIR: return EGameTileOp::AIR;
989 case TILE_SOLID: return EGameTileOp::HOOKABLE;
990 case TILE_DEATH: return EGameTileOp::DEATH;
991 case TILE_NOHOOK: return EGameTileOp::UNHOOKABLE;
992 case TILE_THROUGH_CUT: return EGameTileOp::HOOKTHROUGH;
993 case TILE_FREEZE: return EGameTileOp::FREEZE;
994 case TILE_UNFREEZE: return EGameTileOp::UNFREEZE;
995 case TILE_DFREEZE: return EGameTileOp::DEEP_FREEZE;
996 case TILE_DUNFREEZE: return EGameTileOp::DEEP_UNFREEZE;
997 case TILE_TELECHECKIN: return EGameTileOp::BLUE_CHECK_TELE;
998 case TILE_TELECHECKINEVIL: return EGameTileOp::RED_CHECK_TELE;
999 case TILE_LFREEZE: return EGameTileOp::LIVE_FREEZE;
1000 case TILE_LUNFREEZE: return EGameTileOp::LIVE_UNFREEZE;
1001 default: return EGameTileOp::AIR;
1002 }
1003 };
1004
1005 char aBuf[128] = "Game tiles";
1006 if(m_LiveGameTiles)
1007 {
1008 auto TileOp = GameTileToOp(m_FillGameTile);
1009 if(TileOp != EGameTileOp::AIR)
1010 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Game tiles: %s", GAME_TILE_OP_NAMES[(size_t)TileOp]);
1011 }
1012 if(Editor()->DoButton_Editor(pId: &s_GameTilesButton, pText: aBuf, Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Construct game tiles from this layer."))
1013 Editor()->PopupSelectGametileOpInvoke(x: Editor()->Ui()->MouseX(), y: Editor()->Ui()->MouseY());
1014 const int Selected = Editor()->PopupSelectGameTileOpResult();
1015 FillGameTiles(Fill: (EGameTileOp)Selected);
1016 }
1017
1018 if(Map()->m_pGameLayer.get() != this)
1019 {
1020 if(m_Image >= 0 && (size_t)m_Image < Map()->m_vpImages.size() && Map()->m_vpImages[m_Image]->m_Automapper.IsLoaded() && m_AutomapperConfig != -1)
1021 {
1022 pToolBox->HSplitBottom(Cut: 2.0f, pTop: pToolBox, pBottom: nullptr);
1023 pToolBox->HSplitBottom(Cut: 12.0f, pTop: pToolBox, pBottom: &Button);
1024 if(m_Seed != 0)
1025 {
1026 CUIRect ButtonAuto;
1027 Button.VSplitRight(Cut: 16.0f, pLeft: &Button, pRight: &ButtonAuto);
1028 Button.VSplitRight(Cut: 2.0f, pLeft: &Button, pRight: nullptr);
1029 static int s_AutomapperButtonAuto = 0;
1030 if(Editor()->DoButton_Editor(pId: &s_AutomapperButtonAuto, pText: "A", Checked: m_AutoAutomapper, pRect: &ButtonAuto, Flags: BUTTONFLAG_LEFT, pToolTip: "Automatically run the automapper after modifications."))
1031 {
1032 m_AutoAutomapper = !m_AutoAutomapper;
1033 FlagModified(x: 0, y: 0, w: m_Width, h: m_Height);
1034 if(!m_TilesHistory.empty()) // Sometimes pressing that button causes the automap to run so we should be able to undo that
1035 {
1036 // record undo
1037 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionTileChanges>(args: Map(), args&: Map()->m_SelectedGroup, args&: Map()->m_vSelectedLayers[0], args: "Auto map", args&: m_TilesHistory));
1038 ClearHistory();
1039 }
1040 }
1041 }
1042
1043 static int s_AutomapperButton = 0;
1044 if(Editor()->DoButton_Editor(pId: &s_AutomapperButton, pText: "Automap", Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Run the automapper."))
1045 {
1046 Map()->m_vpImages[m_Image]->m_Automapper.Proceed(pLayer: this, pGameLayer: Map()->m_pGameLayer.get(), ReferenceId: m_AutomapperReference, ConfigId: m_AutomapperConfig, Seed: m_Seed);
1047 // record undo
1048 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionTileChanges>(args: Map(), args&: Map()->m_SelectedGroup, args&: Map()->m_vSelectedLayers[0], args: "Auto map", args&: m_TilesHistory));
1049 ClearHistory();
1050 return CUi::POPUP_CLOSE_CURRENT;
1051 }
1052 }
1053 }
1054
1055 CProperty aProps[] = {
1056 {"Width", m_Width, PROPTYPE_INT, 2, 100000},
1057 {"Height", m_Height, PROPTYPE_INT, 2, 100000},
1058 {"Shift", 0, PROPTYPE_SHIFT, 0, 0},
1059 {"Shift by", Map()->m_ShiftBy, PROPTYPE_INT, 1, 100000},
1060 {"Image", m_Image, PROPTYPE_IMAGE, 0, 0},
1061 {"Color", PackColor(Color: m_Color), PROPTYPE_COLOR, 0, 0},
1062 {"Color Env", m_ColorEnv + 1, PROPTYPE_ENVELOPE, 0, 0},
1063 {"Color TO", m_ColorEnvOffset, PROPTYPE_INT, -1000000, 1000000},
1064 {"Auto Rule", m_AutomapperConfig, PROPTYPE_AUTOMAPPER, m_Image, 0},
1065 {"Reference", m_AutomapperReference, PROPTYPE_AUTOMAPPER_REFERENCE, 0, 0},
1066 {"Live Gametiles", m_LiveGameTiles, PROPTYPE_BOOL, 0, 1},
1067 {"Seed", m_Seed, PROPTYPE_INT, 0, 1000000000},
1068 {nullptr},
1069 };
1070
1071 if(EntitiesLayer) // remove the image and color properties if this is a game layer
1072 {
1073 aProps[(int)ETilesProp::IMAGE].m_pName = nullptr;
1074 aProps[(int)ETilesProp::COLOR].m_pName = nullptr;
1075 aProps[(int)ETilesProp::AUTOMAPPER].m_pName = nullptr;
1076 aProps[(int)ETilesProp::AUTOMAPPER_REFERENCE].m_pName = nullptr;
1077 }
1078 if(m_Image == -1)
1079 {
1080 aProps[(int)ETilesProp::AUTOMAPPER].m_pName = nullptr;
1081 aProps[(int)ETilesProp::AUTOMAPPER_REFERENCE].m_pName = nullptr;
1082 aProps[(int)ETilesProp::SEED].m_pName = nullptr;
1083 }
1084
1085 static int s_aIds[(int)ETilesProp::NUM_PROPS] = {0};
1086 int NewVal = 0;
1087 auto [State, Prop] = Editor()->DoPropertiesWithState<ETilesProp>(pToolbox: pToolBox, pProps: aProps, pIds: s_aIds, pNewVal: &NewVal);
1088
1089 Map()->m_LayerTilesPropTracker.Begin(pObject: this, Prop, State);
1090 Map()->m_EditorHistory.BeginBulk();
1091
1092 if(Prop == ETilesProp::WIDTH)
1093 {
1094 if(NewVal > 1000 && !Editor()->m_LargeLayerWasWarned)
1095 {
1096 Editor()->m_PopupEventType = CEditor::POPEVENT_LARGELAYER;
1097 Editor()->m_PopupEventActivated = true;
1098 Editor()->m_LargeLayerWasWarned = true;
1099 }
1100 Resize(NewW: NewVal, NewH: m_Height);
1101 }
1102 else if(Prop == ETilesProp::HEIGHT)
1103 {
1104 if(NewVal > 1000 && !Editor()->m_LargeLayerWasWarned)
1105 {
1106 Editor()->m_PopupEventType = CEditor::POPEVENT_LARGELAYER;
1107 Editor()->m_PopupEventActivated = true;
1108 Editor()->m_LargeLayerWasWarned = true;
1109 }
1110 Resize(NewW: m_Width, NewH: NewVal);
1111 }
1112 else if(Prop == ETilesProp::SHIFT)
1113 {
1114 Shift(Direction: (EShiftDirection)NewVal);
1115 }
1116 else if(Prop == ETilesProp::SHIFT_BY)
1117 {
1118 Map()->m_ShiftBy = NewVal;
1119 }
1120 else if(Prop == ETilesProp::IMAGE)
1121 {
1122 m_Image = NewVal;
1123 if(NewVal == -1)
1124 {
1125 m_Image = -1;
1126 }
1127 else
1128 {
1129 m_Image = NewVal % Map()->m_vpImages.size();
1130 m_AutomapperConfig = -1;
1131
1132 if(Map()->m_vpImages[m_Image]->m_Width % 16 != 0 || Map()->m_vpImages[m_Image]->m_Height % 16 != 0)
1133 {
1134 Editor()->m_PopupEventType = CEditor::POPEVENT_IMAGEDIV16;
1135 Editor()->m_PopupEventActivated = true;
1136 m_Image = -1;
1137 }
1138 }
1139 }
1140 else if(Prop == ETilesProp::COLOR)
1141 {
1142 m_Color = UnpackColor(PackedColor: NewVal);
1143 }
1144 else if(Prop == ETilesProp::COLOR_ENV)
1145 {
1146 int Index = std::clamp(val: NewVal - 1, lo: -1, hi: (int)Map()->m_vpEnvelopes.size() - 1);
1147 const int Step = (Index - m_ColorEnv) % 2;
1148 if(Step != 0)
1149 {
1150 for(; Index >= -1 && Index < (int)Map()->m_vpEnvelopes.size(); Index += Step)
1151 {
1152 if(Index == -1 || Map()->m_vpEnvelopes[Index]->GetChannels() == 4)
1153 {
1154 m_ColorEnv = Index;
1155 break;
1156 }
1157 }
1158 }
1159 }
1160 else if(Prop == ETilesProp::COLOR_ENV_OFFSET)
1161 {
1162 m_ColorEnvOffset = NewVal;
1163 }
1164 else if(Prop == ETilesProp::SEED)
1165 {
1166 m_Seed = NewVal;
1167 }
1168 else if(Prop == ETilesProp::AUTOMAPPER)
1169 {
1170 if(m_Image >= 0 && Map()->m_vpImages[m_Image]->m_Automapper.ConfigNamesNum() > 0 && NewVal >= 0)
1171 m_AutomapperConfig = NewVal % Map()->m_vpImages[m_Image]->m_Automapper.ConfigNamesNum();
1172 else
1173 m_AutomapperConfig = -1;
1174 }
1175 else if(Prop == ETilesProp::AUTOMAPPER_REFERENCE)
1176 {
1177 m_AutomapperReference = NewVal;
1178 }
1179 else if(Prop == ETilesProp::LIVE_GAMETILES)
1180 {
1181 m_LiveGameTiles = NewVal != 0;
1182 }
1183
1184 Map()->m_LayerTilesPropTracker.End(Prop, State);
1185
1186 // Check if modified property could have an effect on automapper
1187 if((State == EEditState::END || State == EEditState::ONE_GO) && HasAutomapEffect(Prop))
1188 {
1189 FlagModified(x: 0, y: 0, w: m_Width, h: m_Height);
1190
1191 // Record undo if automapper was ran
1192 if(m_AutoAutomapper && !m_TilesHistory.empty())
1193 {
1194 Map()->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionTileChanges>(args: Map(), args&: Map()->m_SelectedGroup, args&: Map()->m_vSelectedLayers[0], args: "Auto map", args&: m_TilesHistory));
1195 ClearHistory();
1196 }
1197 }
1198
1199 // End undo bulk, taking the first action display as the displayed text in the history
1200 // This is usually the resulting text of the edit layer tiles prop action
1201 // Since we may also squeeze a tile changes action, we want both to appear as one, thus using a bulk
1202 Map()->m_EditorHistory.EndBulk(DisplayToUse: 0);
1203
1204 return CUi::POPUP_KEEP_OPEN;
1205}
1206
1207CUi::EPopupMenuFunctionResult CLayerTiles::RenderCommonProperties(SCommonPropState &State, CEditorMap *pEditorMap, CUIRect *pToolbox, std::vector<std::shared_ptr<CLayerTiles>> &vpLayers, std::vector<int> &vLayerIndices)
1208{
1209 CEditor *pEditor = pEditorMap->Editor();
1210 if(State.m_Modified)
1211 {
1212 CUIRect Commit;
1213 pToolbox->HSplitBottom(Cut: 20.0f, pTop: pToolbox, pBottom: &Commit);
1214 static int s_CommitButton = 0;
1215 if(pEditor->DoButton_Editor(pId: &s_CommitButton, pText: "Commit", Checked: 0, pRect: &Commit, Flags: BUTTONFLAG_LEFT, pToolTip: "Apply the changes."))
1216 {
1217 bool HasModifiedSize = (State.m_Modified & SCommonPropState::MODIFIED_SIZE) != 0;
1218 bool HasModifiedColor = (State.m_Modified & SCommonPropState::MODIFIED_COLOR) != 0;
1219
1220 std::vector<std::shared_ptr<IEditorAction>> vpActions;
1221 int j = 0;
1222 int GroupIndex = pEditorMap->m_SelectedGroup;
1223 for(auto &pLayer : vpLayers)
1224 {
1225 int LayerIndex = vLayerIndices[j++];
1226 if(HasModifiedSize)
1227 {
1228 std::map<int, std::shared_ptr<CLayer>> SavedLayers;
1229 SavedLayers[LAYERTYPE_TILES] = pLayer->Duplicate();
1230 if(pLayer->m_HasGame || pLayer->m_HasFront || pLayer->m_HasSwitch || pLayer->m_HasSpeedup || pLayer->m_HasTune || pLayer->m_HasTele)
1231 { // Need to save all entities layers when any entity layer
1232 if(pEditorMap->m_pFrontLayer && !pLayer->m_HasFront)
1233 SavedLayers[LAYERTYPE_FRONT] = pEditorMap->m_pFrontLayer->Duplicate();
1234 if(pEditorMap->m_pTeleLayer && !pLayer->m_HasTele)
1235 SavedLayers[LAYERTYPE_TELE] = pEditorMap->m_pTeleLayer->Duplicate();
1236 if(pEditorMap->m_pSwitchLayer && !pLayer->m_HasSwitch)
1237 SavedLayers[LAYERTYPE_SWITCH] = pEditorMap->m_pSwitchLayer->Duplicate();
1238 if(pEditorMap->m_pSpeedupLayer && !pLayer->m_HasSpeedup)
1239 SavedLayers[LAYERTYPE_SPEEDUP] = pEditorMap->m_pSpeedupLayer->Duplicate();
1240 if(pEditorMap->m_pTuneLayer && !pLayer->m_HasTune)
1241 SavedLayers[LAYERTYPE_TUNE] = pEditorMap->m_pTuneLayer->Duplicate();
1242 if(!pLayer->m_HasGame)
1243 SavedLayers[LAYERTYPE_GAME] = pEditorMap->m_pGameLayer->Duplicate();
1244 }
1245
1246 int PrevW = pLayer->m_Width;
1247 int PrevH = pLayer->m_Height;
1248 pLayer->Resize(NewW: State.m_Width, NewH: State.m_Height);
1249
1250 if(PrevW != State.m_Width)
1251 {
1252 std::shared_ptr<CEditorActionEditLayerTilesProp> pAction;
1253 vpActions.push_back(x: pAction = std::make_shared<CEditorActionEditLayerTilesProp>(args&: pEditorMap, args&: GroupIndex, args&: LayerIndex, args: ETilesProp::WIDTH, args&: PrevW, args&: State.m_Width));
1254 pAction->SetSavedLayers(SavedLayers);
1255 }
1256
1257 if(PrevH != State.m_Height)
1258 {
1259 std::shared_ptr<CEditorActionEditLayerTilesProp> pAction;
1260 vpActions.push_back(x: pAction = std::make_shared<CEditorActionEditLayerTilesProp>(args&: pEditorMap, args&: GroupIndex, args&: LayerIndex, args: ETilesProp::HEIGHT, args&: PrevH, args&: State.m_Height));
1261 pAction->SetSavedLayers(SavedLayers);
1262 }
1263 }
1264
1265 if(HasModifiedColor && !pLayer->IsEntitiesLayer())
1266 {
1267 const int PackedColor = PackColor(Color: pLayer->m_Color);
1268 pLayer->m_Color = UnpackColor(PackedColor: State.m_Color);
1269 vpActions.push_back(x: std::make_shared<CEditorActionEditLayerTilesProp>(args&: pEditorMap, args&: GroupIndex, args&: LayerIndex, args: ETilesProp::COLOR, args: PackedColor, args&: State.m_Color));
1270 }
1271
1272 pLayer->FlagModified(x: 0, y: 0, w: pLayer->m_Width, h: pLayer->m_Height);
1273 }
1274 State.m_Modified = 0;
1275
1276 char aDisplay[256];
1277 str_format(buffer: aDisplay, buffer_size: sizeof(aDisplay), format: "Edit %d layers common properties: %s", (int)vpLayers.size(), HasModifiedColor && HasModifiedSize ? "color, size" : (HasModifiedColor ? "color" : "size"));
1278 pEditorMap->m_EditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args&: pEditorMap, args&: vpActions, args&: aDisplay));
1279 }
1280 }
1281 else
1282 {
1283 for(auto &pLayer : vpLayers)
1284 {
1285 if(pLayer->m_Width > State.m_Width)
1286 State.m_Width = pLayer->m_Width;
1287 if(pLayer->m_Height > State.m_Height)
1288 State.m_Height = pLayer->m_Height;
1289 }
1290
1291 State.m_Color = PackColor(Color: vpLayers[0]->m_Color);
1292 }
1293
1294 {
1295 CUIRect Warning;
1296 pToolbox->HSplitTop(Cut: 13.0f, pTop: &Warning, pBottom: pToolbox);
1297 Warning.HMargin(Cut: 0.5f, pOtherRect: &Warning);
1298
1299 pEditor->TextRender()->TextColor(Color: ColorRGBA(1.0f, 0.0f, 0.0f, 1.0f));
1300 SLabelProperties Props;
1301 Props.m_MaxWidth = Warning.w;
1302 pEditor->Ui()->DoLabel(pRect: &Warning, pText: "Editing multiple layers", Size: 9.0f, Align: TEXTALIGN_ML, LabelProps: Props);
1303 pEditor->TextRender()->TextColor(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
1304 pToolbox->HSplitTop(Cut: 2.0f, pTop: nullptr, pBottom: pToolbox);
1305 }
1306
1307 CProperty aProps[] = {
1308 {"Width", State.m_Width, PROPTYPE_INT, 2, 100000},
1309 {"Height", State.m_Height, PROPTYPE_INT, 2, 100000},
1310 {"Shift", 0, PROPTYPE_SHIFT, 0, 0},
1311 {"Shift by", pEditorMap->m_ShiftBy, PROPTYPE_INT, 1, 100000},
1312 {"Color", State.m_Color, PROPTYPE_COLOR, 0, 0},
1313 {nullptr},
1314 };
1315
1316 static int s_aIds[(int)ETilesCommonProp::NUM_PROPS] = {0};
1317 int NewVal = 0;
1318 auto [PropState, Prop] = pEditor->DoPropertiesWithState<ETilesCommonProp>(pToolbox, pProps: aProps, pIds: s_aIds, pNewVal: &NewVal);
1319
1320 pEditorMap->m_LayerTilesCommonPropTracker.m_vpLayers = vpLayers;
1321 pEditorMap->m_LayerTilesCommonPropTracker.m_vLayerIndices = vLayerIndices;
1322
1323 pEditorMap->m_LayerTilesCommonPropTracker.Begin(pObject: nullptr, Prop, State: PropState);
1324
1325 if(Prop == ETilesCommonProp::WIDTH)
1326 {
1327 if(NewVal > 1000 && !pEditor->m_LargeLayerWasWarned)
1328 {
1329 pEditor->m_PopupEventType = CEditor::POPEVENT_LARGELAYER;
1330 pEditor->m_PopupEventActivated = true;
1331 pEditor->m_LargeLayerWasWarned = true;
1332 }
1333 State.m_Width = NewVal;
1334 }
1335 else if(Prop == ETilesCommonProp::HEIGHT)
1336 {
1337 if(NewVal > 1000 && !pEditor->m_LargeLayerWasWarned)
1338 {
1339 pEditor->m_PopupEventType = CEditor::POPEVENT_LARGELAYER;
1340 pEditor->m_PopupEventActivated = true;
1341 pEditor->m_LargeLayerWasWarned = true;
1342 }
1343 State.m_Height = NewVal;
1344 }
1345 else if(Prop == ETilesCommonProp::SHIFT)
1346 {
1347 for(auto &pLayer : vpLayers)
1348 pLayer->Shift(Direction: (EShiftDirection)NewVal);
1349 }
1350 else if(Prop == ETilesCommonProp::SHIFT_BY)
1351 {
1352 pEditorMap->m_ShiftBy = NewVal;
1353 }
1354 else if(Prop == ETilesCommonProp::COLOR)
1355 {
1356 State.m_Color = NewVal;
1357 }
1358
1359 pEditorMap->m_LayerTilesCommonPropTracker.End(Prop, State: PropState);
1360
1361 if(PropState == EEditState::END || PropState == EEditState::ONE_GO)
1362 {
1363 if(Prop == ETilesCommonProp::WIDTH || Prop == ETilesCommonProp::HEIGHT)
1364 {
1365 State.m_Modified |= SCommonPropState::MODIFIED_SIZE;
1366 }
1367 else if(Prop == ETilesCommonProp::COLOR)
1368 {
1369 State.m_Modified |= SCommonPropState::MODIFIED_COLOR;
1370 }
1371 }
1372
1373 return CUi::POPUP_KEEP_OPEN;
1374}
1375
1376void CLayerTiles::FlagModified(int x, int y, int w, int h)
1377{
1378 Map()->OnModify();
1379 if(m_Seed != 0 && m_AutomapperConfig != -1 && m_AutoAutomapper && m_Image >= 0)
1380 {
1381 Map()->m_vpImages[m_Image]->m_Automapper.ProceedLocalized(pLayer: this, pGameLayer: Map()->m_pGameLayer.get(), ReferenceId: m_AutomapperReference, ConfigId: m_AutomapperConfig, Seed: m_Seed, X: x, Y: y, Width: w, Height: h);
1382 }
1383}
1384
1385bool CLayerTiles::IsEnvelopeUsed(int EnvelopeIndex) const
1386{
1387 return m_ColorEnv == EnvelopeIndex;
1388}
1389
1390bool CLayerTiles::IsImageUsed(int ImageIndex) const
1391{
1392 return m_Image == ImageIndex;
1393}
1394
1395void CLayerTiles::ModifyImageIndex(const FIndexModifyFunction &IndexModifyFunction)
1396{
1397 IndexModifyFunction(&m_Image);
1398}
1399
1400void CLayerTiles::ModifyEnvelopeIndex(const FIndexModifyFunction &IndexModifyFunction)
1401{
1402 IndexModifyFunction(&m_ColorEnv);
1403}
1404
1405void CLayerTiles::ShowPreventUnusedTilesWarning()
1406{
1407 if(!Editor()->m_PreventUnusedTilesWasWarned)
1408 {
1409 Editor()->m_PopupEventType = CEditor::POPEVENT_PREVENTUNUSEDTILES;
1410 Editor()->m_PopupEventActivated = true;
1411 Editor()->m_PreventUnusedTilesWasWarned = true;
1412 }
1413}
1414
1415bool CLayerTiles::HasAutomapEffect(ETilesProp Prop)
1416{
1417 switch(Prop)
1418 {
1419 case ETilesProp::WIDTH:
1420 case ETilesProp::HEIGHT:
1421 case ETilesProp::SHIFT:
1422 case ETilesProp::IMAGE:
1423 case ETilesProp::AUTOMAPPER:
1424 case ETilesProp::SEED:
1425 return true;
1426 default:
1427 return false;
1428 }
1429 return false;
1430}
1431