palette_waypoints.cpp

main 225 lines · 6.7 KB Raw
Alex Alex Commit Map Editor 18/10/2026 23:40
1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18// ============================================================================
19// Waypoint palette
20
21#include "main.h"
22
23#include "gui.h"
24#include "palette_waypoints.h"
25#include "waypoint_brush.h"
26#include "map.h"
27
28BEGIN_EVENT_TABLE(WaypointPalettePanel, PalettePanel)
29EVT_BUTTON(PALETTE_WAYPOINT_ADD_WAYPOINT, WaypointPalettePanel::OnClickAddWaypoint)
30EVT_BUTTON(PALETTE_WAYPOINT_REMOVE_WAYPOINT, WaypointPalettePanel::OnClickRemoveWaypoint)
31
32EVT_LIST_BEGIN_LABEL_EDIT(PALETTE_WAYPOINT_LISTBOX, WaypointPalettePanel::OnBeginEditWaypointLabel)
33EVT_LIST_END_LABEL_EDIT(PALETTE_WAYPOINT_LISTBOX, WaypointPalettePanel::OnEditWaypointLabel)
34EVT_LIST_ITEM_SELECTED(PALETTE_WAYPOINT_LISTBOX, WaypointPalettePanel::OnClickWaypoint)
35END_EVENT_TABLE()
36
37WaypointPalettePanel::WaypointPalettePanel(wxWindow* parent, wxWindowID id) :
38 PalettePanel(parent, id),
39 map(nullptr) {
40 wxSizer* sidesizer = newd wxStaticBoxSizer(wxVERTICAL, this, "Waypoints");
41
42 waypoint_list = newd wxListCtrl(this, PALETTE_WAYPOINT_LISTBOX, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_EDIT_LABELS | wxLC_NO_HEADER);
43 waypoint_list->InsertColumn(0, "UNNAMED", wxLIST_FORMAT_LEFT, 200);
44 sidesizer->Add(waypoint_list, 1, wxEXPAND);
45
46 wxSizer* tmpsizer = newd wxBoxSizer(wxHORIZONTAL);
47 tmpsizer->Add(add_waypoint_button = newd wxButton(this, PALETTE_WAYPOINT_ADD_WAYPOINT, "Add", wxDefaultPosition, wxSize(50, -1)), 1, wxEXPAND);
48 tmpsizer->Add(remove_waypoint_button = newd wxButton(this, PALETTE_WAYPOINT_REMOVE_WAYPOINT, "Remove", wxDefaultPosition, wxSize(70, -1)), 1, wxEXPAND);
49 sidesizer->Add(tmpsizer, 0, wxEXPAND);
50
51 SetSizerAndFit(sidesizer);
52}
53
54WaypointPalettePanel::~WaypointPalettePanel() {
55 ////
56}
57
58void WaypointPalettePanel::OnSwitchIn() {
59 PalettePanel::OnSwitchIn();
60}
61
62void WaypointPalettePanel::OnSwitchOut() {
63 PalettePanel::OnSwitchOut();
64}
65
66void WaypointPalettePanel::SetMap(Map* m) {
67 map = m;
68 this->Enable(m);
69}
70
71void WaypointPalettePanel::SelectFirstBrush() {
72 // SelectWaypointBrush();
73}
74
75Brush* WaypointPalettePanel::GetSelectedBrush() const {
76 long item = waypoint_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
77 g_gui.waypoint_brush->setWaypoint(
78 item == -1 ? nullptr : map->waypoints.getWaypoint(nstr(waypoint_list->GetItemText(item)))
79 );
80 return g_gui.waypoint_brush;
81}
82
83bool WaypointPalettePanel::SelectBrush(const Brush* whatbrush) {
84 ASSERT(whatbrush == g_gui.waypoint_brush);
85 return false;
86}
87
88int WaypointPalettePanel::GetSelectedBrushSize() const {
89 return 0;
90}
91
92PaletteType WaypointPalettePanel::GetType() const {
93 return TILESET_WAYPOINT;
94}
95
96wxString WaypointPalettePanel::GetName() const {
97 return "Waypoint Palette";
98}
99
100void WaypointPalettePanel::OnUpdate() {
101 if (wxTextCtrl* tc = waypoint_list->GetEditControl()) {
102 Waypoint* wp = map->waypoints.getWaypoint(nstr(tc->GetValue()));
103 if (wp && wp->pos == Position()) {
104 if (map->getTile(wp->pos)) {
105 map->getTileL(wp->pos)->decreaseWaypointCount();
106 }
107 map->waypoints.removeWaypoint(wp->name);
108 }
109 }
110 waypoint_list->DeleteAllItems();
111
112 if (!map) {
113 waypoint_list->Enable(false);
114 add_waypoint_button->Enable(false);
115 remove_waypoint_button->Enable(false);
116 } else {
117 waypoint_list->Enable(true);
118 add_waypoint_button->Enable(true);
119 remove_waypoint_button->Enable(true);
120
121 Waypoints& waypoints = map->waypoints;
122
123 for (WaypointMap::const_iterator iter = waypoints.begin(); iter != waypoints.end(); ++iter) {
124 waypoint_list->InsertItem(0, wxstr(iter->second->name));
125 }
126 }
127}
128
129void WaypointPalettePanel::OnClickWaypoint(wxListEvent& event) {
130 if (!map) {
131 return;
132 }
133
134 std::string wpname = nstr(event.GetText());
135 Waypoint* wp = map->waypoints.getWaypoint(wpname);
136 if (wp) {
137 g_gui.SetScreenCenterPosition(wp->pos);
138 g_gui.waypoint_brush->setWaypoint(wp);
139 }
140}
141
142void WaypointPalettePanel::OnBeginEditWaypointLabel(wxListEvent& event) {
143 // We need to disable all hotkeys, so we can type properly
144 g_gui.DisableHotkeys();
145}
146
147void WaypointPalettePanel::OnEditWaypointLabel(wxListEvent& event) {
148 std::string wpname = nstr(event.GetLabel());
149 std::string oldwpname = nstr(waypoint_list->GetItemText(event.GetIndex()));
150 Waypoint* wp = map->waypoints.getWaypoint(oldwpname);
151
152 if (event.IsEditCancelled()) {
153 return;
154 }
155
156 if (wpname == "") {
157 map->waypoints.removeWaypoint(oldwpname);
158 g_gui.RefreshPalettes();
159 } else if (wp) {
160 if (wpname == oldwpname) {
161 ; // do nothing
162 } else {
163 if (map->waypoints.getWaypoint(wpname)) {
164 // Already exists a waypoint with this name!
165 g_gui.SetStatusText("There already is a waypoint with this name.");
166 event.Veto();
167 if (oldwpname == "") {
168 map->waypoints.removeWaypoint(oldwpname);
169 g_gui.RefreshPalettes();
170 }
171 } else {
172 Waypoint* nwp = newd Waypoint(*wp);
173 nwp->name = wpname;
174
175 Waypoint* rwp = map->waypoints.getWaypoint(oldwpname);
176 if (rwp) {
177 if (map->getTile(rwp->pos)) {
178 map->getTileL(rwp->pos)->decreaseWaypointCount();
179 }
180 map->waypoints.removeWaypoint(rwp->name);
181 }
182
183 map->waypoints.addWaypoint(nwp);
184 g_gui.waypoint_brush->setWaypoint(nwp);
185
186 // Refresh other palettes
187 refresh_timer.Start(300, true);
188 }
189 }
190 }
191
192 if (event.IsAllowed()) {
193 g_gui.EnableHotkeys();
194 }
195}
196
197void WaypointPalettePanel::OnClickAddWaypoint(wxCommandEvent& event) {
198 if (map) {
199 map->waypoints.addWaypoint(newd Waypoint());
200 long i = waypoint_list->InsertItem(0, "");
201 waypoint_list->EditLabel(i);
202
203 // g_gui.RefreshPalettes();
204 }
205}
206
207void WaypointPalettePanel::OnClickRemoveWaypoint(wxCommandEvent& event) {
208 if (!map) {
209 return;
210 }
211
212 long item = waypoint_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
213 if (item != -1) {
214 Waypoint* wp = map->waypoints.getWaypoint(nstr(waypoint_list->GetItemText(item)));
215 if (wp) {
216 if (map->getTile(wp->pos)) {
217 map->getTileL(wp->pos)->decreaseWaypointCount();
218 }
219 map->waypoints.removeWaypoint(wp->name);
220 }
221 waypoint_list->DeleteItem(item);
222 refresh_timer.Start(300, true);
223 }
224}
225
Top