1#include "layer_selector.h"
2
3#include "editor.h"
4
5#include <engine/shared/config.h>
6
7void CLayerSelector::OnInit(CEditor *pEditor)
8{
9 CEditorComponent::OnInit(pEditor);
10
11 m_SelectionOffset = 0;
12}
13
14bool CLayerSelector::SelectByTile()
15{
16 // ctrl+right click a map index to select the layer that has a tile there
17 if(Ui()->HotItem() != Editor()->MapView())
18 return false;
19 if(!Input()->ModifierIsPressed() || !Ui()->MouseButtonClicked(Index: 1))
20 return false;
21 if(!g_Config.m_EdLayerSelector)
22 return false;
23
24 int MatchedGroup = -1;
25 int MatchedLayer = -1;
26 int Matches = 0;
27 bool IsFound = false;
28 for(const auto &HoverTile : m_vHoverTiles)
29 {
30 if(!Map()->m_vpGroups[HoverTile.m_Group]->m_Visible ||
31 !Map()->m_vpGroups[HoverTile.m_Group]->m_vpLayers[HoverTile.m_Layer]->m_Visible)
32 continue;
33
34 if(MatchedGroup == -1)
35 {
36 MatchedGroup = HoverTile.m_Group;
37 MatchedLayer = HoverTile.m_Layer;
38 }
39 if(++Matches > m_SelectionOffset)
40 {
41 m_SelectionOffset++;
42 MatchedGroup = HoverTile.m_Group;
43 MatchedLayer = HoverTile.m_Layer;
44 IsFound = true;
45 break;
46 }
47 }
48 if(MatchedGroup != -1 && MatchedLayer != -1)
49 {
50 if(!IsFound)
51 m_SelectionOffset = 1;
52 Map()->SelectLayer(LayerIndex: MatchedLayer, GroupIndex: MatchedGroup);
53 return true;
54 }
55 return false;
56}
57
58void CLayerSelector::UpdateHoveredTiles()
59{
60 const vec2 UpdatedMousePos = Ui()->UpdatedMousePos();
61
62 m_vHoverTiles.clear();
63 for(size_t g = 0; g < Map()->m_vpGroups.size(); g++)
64 {
65 const std::shared_ptr<CLayerGroup> pGroup = Map()->m_vpGroups[g];
66 for(size_t l = 0; l < pGroup->m_vpLayers.size(); l++)
67 {
68 const std::shared_ptr<CLayer> pLayer = pGroup->m_vpLayers[l];
69 int LayerType = pLayer->m_Type;
70 if(LayerType != LAYERTYPE_TILES &&
71 LayerType != LAYERTYPE_FRONT &&
72 LayerType != LAYERTYPE_TELE &&
73 LayerType != LAYERTYPE_SPEEDUP &&
74 LayerType != LAYERTYPE_SWITCH &&
75 LayerType != LAYERTYPE_TUNE)
76 continue;
77
78 std::shared_ptr<CLayerTiles> pTiles = std::static_pointer_cast<CLayerTiles>(r: pLayer);
79 pGroup->MapScreen();
80 float aPoints[4];
81 pGroup->Mapping(pPoints: aPoints);
82 float WorldWidth = aPoints[2] - aPoints[0];
83 float WorldHeight = aPoints[3] - aPoints[1];
84 CUIRect Rect;
85 Rect.x = aPoints[0] + WorldWidth * (UpdatedMousePos.x / Graphics()->WindowWidth());
86 Rect.y = aPoints[1] + WorldHeight * (UpdatedMousePos.y / Graphics()->WindowHeight());
87 Rect.w = 0;
88 Rect.h = 0;
89 CIntRect r;
90 pTiles->Convert(Rect, pOut: &r);
91 pTiles->Clamp(pRect: &r);
92 int x = r.x;
93 int y = r.y;
94
95 if(x < 0 || x >= pTiles->m_Width)
96 continue;
97 if(y < 0 || y >= pTiles->m_Height)
98 continue;
99
100 if(pTiles->GetTile(x, y).m_Index > 0)
101 m_vHoverTiles.emplace_back(args&: g, args&: l);
102 }
103 }
104 Ui()->MapScreen();
105}
106