1 | #include "map_grid.h" |
2 | |
3 | #include <engine/keys.h> |
4 | |
5 | #include "editor.h" |
6 | |
7 | static constexpr int MIN_GRID_FACTOR = 1; |
8 | static constexpr int MAX_GRID_FACTOR = 15; |
9 | |
10 | void CMapGrid::OnReset() |
11 | { |
12 | m_GridActive = false; |
13 | m_GridFactor = 1; |
14 | } |
15 | |
16 | void CMapGrid::OnRender(CUIRect View) |
17 | { |
18 | if(!m_GridActive) |
19 | return; |
20 | |
21 | std::shared_ptr<CLayerGroup> pGroup = Editor()->GetSelectedGroup(); |
22 | if(pGroup) |
23 | { |
24 | pGroup->MapScreen(); |
25 | |
26 | float aGroupPoints[4]; |
27 | pGroup->Mapping(pPoints: aGroupPoints); |
28 | |
29 | const CUIRect *pScreen = Ui()->Screen(); |
30 | |
31 | int LineDistance = GridLineDistance(); |
32 | |
33 | int XOffset = aGroupPoints[0] / LineDistance; |
34 | int YOffset = aGroupPoints[1] / LineDistance; |
35 | int XGridOffset = XOffset % m_GridFactor; |
36 | int YGridOffset = YOffset % m_GridFactor; |
37 | |
38 | Graphics()->TextureClear(); |
39 | Graphics()->LinesBegin(); |
40 | |
41 | for(int i = 0; i < (int)pScreen->w; i++) |
42 | { |
43 | if((i + YGridOffset) % m_GridFactor == 0) |
44 | Graphics()->SetColor(r: 1.0f, g: 0.3f, b: 0.3f, a: 0.3f); |
45 | else |
46 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.15f); |
47 | |
48 | IGraphics::CLineItem Line = IGraphics::CLineItem(LineDistance * XOffset, LineDistance * i + LineDistance * YOffset, pScreen->w + aGroupPoints[2], LineDistance * i + LineDistance * YOffset); |
49 | Graphics()->LinesDraw(pArray: &Line, Num: 1); |
50 | |
51 | if((i + XGridOffset) % m_GridFactor == 0) |
52 | Graphics()->SetColor(r: 1.0f, g: 0.3f, b: 0.3f, a: 0.3f); |
53 | else |
54 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.15f); |
55 | |
56 | Line = IGraphics::CLineItem(LineDistance * i + LineDistance * XOffset, LineDistance * YOffset, LineDistance * i + LineDistance * XOffset, pScreen->h + aGroupPoints[3]); |
57 | Graphics()->LinesDraw(pArray: &Line, Num: 1); |
58 | } |
59 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f); |
60 | Graphics()->LinesEnd(); |
61 | } |
62 | } |
63 | |
64 | int CMapGrid::GridLineDistance() const |
65 | { |
66 | if(Editor()->MapView()->Zoom()->GetValue() <= 10.0f) |
67 | return 4; |
68 | else if(Editor()->MapView()->Zoom()->GetValue() <= 50.0f) |
69 | return 8; |
70 | else if(Editor()->MapView()->Zoom()->GetValue() <= 100.0f) |
71 | return 16; |
72 | else if(Editor()->MapView()->Zoom()->GetValue() <= 250.0f) |
73 | return 32; |
74 | else if(Editor()->MapView()->Zoom()->GetValue() <= 450.0f) |
75 | return 64; |
76 | else if(Editor()->MapView()->Zoom()->GetValue() <= 850.0f) |
77 | return 128; |
78 | else if(Editor()->MapView()->Zoom()->GetValue() <= 1550.0f) |
79 | return 256; |
80 | else |
81 | return 512; |
82 | } |
83 | |
84 | void CMapGrid::SnapToGrid(float &x, float &y) const |
85 | { |
86 | const int GridDistance = GridLineDistance() * m_GridFactor; |
87 | x = (int)((x + (x >= 0 ? 1.0f : -1.0f) * GridDistance / 2) / GridDistance) * GridDistance; |
88 | y = (int)((y + (y >= 0 ? 1.0f : -1.0f) * GridDistance / 2) / GridDistance) * GridDistance; |
89 | } |
90 | |
91 | bool CMapGrid::IsEnabled() const |
92 | { |
93 | return m_GridActive; |
94 | } |
95 | |
96 | void CMapGrid::Toggle() |
97 | { |
98 | m_GridActive = !m_GridActive; |
99 | } |
100 | |
101 | int CMapGrid::Factor() const |
102 | { |
103 | return m_GridFactor; |
104 | } |
105 | |
106 | void CMapGrid::SetFactor(int Factor) |
107 | { |
108 | m_GridFactor = clamp(val: Factor, lo: MIN_GRID_FACTOR, hi: MAX_GRID_FACTOR); |
109 | } |
110 | |
111 | void CMapGrid::(vec2 Position) |
112 | { |
113 | Ui()->DoPopupMenu(pId: &m_PopupGridSettingsId, X: Position.x, Y: Position.y, Width: 120.0f, Height: 37.0f, pContext: this, pfnFunc: PopupGridSettings); |
114 | } |
115 | |
116 | CUi::EPopupMenuFunctionResult CMapGrid::(void *pContext, CUIRect View, bool Active) |
117 | { |
118 | CMapGrid *pMapGrid = static_cast<CMapGrid *>(pContext); |
119 | |
120 | enum |
121 | { |
122 | PROP_SIZE = 0, |
123 | NUM_PROPS, |
124 | }; |
125 | CProperty aProps[] = { |
126 | {.m_pName: "Size" , .m_Value: pMapGrid->Factor(), .m_Type: PROPTYPE_INT, .m_Min: MIN_GRID_FACTOR, .m_Max: MAX_GRID_FACTOR}, |
127 | {.m_pName: nullptr}, |
128 | }; |
129 | |
130 | static int s_aIds[NUM_PROPS]; |
131 | int NewVal; |
132 | int Prop = pMapGrid->Editor()->DoProperties(pToolbox: &View, pProps: aProps, pIds: s_aIds, pNewVal: &NewVal); |
133 | |
134 | if(Prop == PROP_SIZE) |
135 | { |
136 | pMapGrid->SetFactor(NewVal); |
137 | } |
138 | |
139 | CUIRect Button; |
140 | View.HSplitBottom(Cut: 12.0f, pTop: &View, pBottom: &Button); |
141 | |
142 | static char s_DefaultButton; |
143 | if(pMapGrid->Editor()->DoButton_Ex(pId: &s_DefaultButton, pText: "Default" , Checked: 0, pRect: &Button, Flags: 0, pToolTip: "Normal grid size" , Corners: IGraphics::CORNER_ALL)) |
144 | { |
145 | pMapGrid->SetFactor(1); |
146 | } |
147 | |
148 | return CUi::POPUP_KEEP_OPEN; |
149 | } |
150 | |