Skip to content

Commit 4ea9d56

Browse files
ZbizuErza
andcommitted
support ctrl+V in teleport destination
Co-authored-by: Erza <[email protected]>
1 parent a958895 commit 4ea9d56

7 files changed

Lines changed: 69 additions & 36 deletions

File tree

source/common.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <sstream>
2424
#include <random>
25+
#include <regex>
26+
#include <algorithm>
2527

2628
// random generator
2729
std::mt19937& getRandomGenerator()
@@ -196,45 +198,46 @@ std::string wstring2string(const std::wstring& widestring)
196198
return std::string((const char*)s.mb_str(wxConvUTF8));
197199
}
198200

199-
bool posFromClipboard(int& x, int& y, int& z)
201+
bool posFromClipboard(Position& position, const int mapWidth /* = MAP_MAX_WIDTH */, const int mapHeight /* = MAP_MAX_HEIGHT */)
200202
{
201-
bool done = false;
203+
if (!wxTheClipboard->Open()) {
204+
return false;
205+
}
202206

203-
if(wxTheClipboard->Open()) {
204-
if(wxTheClipboard->IsSupported(wxDF_TEXT)) {
205-
std::vector<int> values;
206-
wxTextDataObject data;
207-
wxTheClipboard->GetData(data);
208-
wxString text = data.GetText();
209-
210-
if(text.size() < 50) {
211-
bool r = false;
212-
wxString sv;
213-
214-
for(size_t s = 0; s < text.size(); ++s) {
215-
if(text[s] >= '0' && text[s] <= '9') {
216-
sv << text[s];
217-
r = true;
218-
} else if(r) {
219-
values.push_back(ws2i(sv));
220-
sv.Clear();
221-
r = false;
222-
223-
if(values.size() >= 3)
224-
break;
225-
}
226-
}
227-
}
207+
if (!wxTheClipboard->IsSupported(wxDF_TEXT)) {
208+
wxTheClipboard->Close();
209+
return false;
210+
}
211+
212+
wxTextDataObject data;
213+
wxTheClipboard->GetData(data);
228214

229-
if(values.size() == 3) {
230-
x = values[0];
231-
y = values[1];
232-
z = values[2];
215+
std::string input = data.GetText().ToStdString();
216+
if (input.empty()) {
217+
wxTheClipboard->Close();
218+
return false;
219+
}
220+
221+
bool done = false;
222+
std::smatch matches;
223+
static const std::regex expression = std::regex(R"(.*?(\d+).*?(\d+).*?(\d+).*?)", std::regex_constants::ECMAScript);
224+
if (std::regex_match(input, matches, expression)) {
225+
try {
226+
const int tmpX = std::stoi(matches.str(1));
227+
const int tmpY = std::stoi(matches.str(2));
228+
const int tmpZ = std::stoi(matches.str(3));
229+
230+
const Position pastedPos = Position(tmpX, tmpY, tmpZ);
231+
if (pastedPos.isValid() && tmpX <= mapWidth && tmpY <= mapHeight) {
232+
position.x = tmpX;
233+
position.y = tmpY;
234+
position.z = tmpZ;
233235
done = true;
234236
}
235-
}
236-
wxTheClipboard->Close();
237+
} catch (const std::out_of_range&) {}
237238
}
239+
240+
wxTheClipboard->Close();
238241
return done;
239242
}
240243

source/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define RME_COMMONS_H_
2020

2121
#include "main.h"
22+
#include "position.h"
2223
#include <stdio.h>
2324
#include <stdlib.h>
2425
#include <cmath>
@@ -71,7 +72,7 @@ std::wstring string2wstring(const std::string& utf8string);
7172
std::string wstring2string(const std::wstring& widestring);
7273

7374
// Gets position values from ClipBoard
74-
bool posFromClipboard(int& x, int& y, int& z);
75+
bool posFromClipboard(Position& position, const int mapWidth = MAP_MAX_WIDTH, const int mapHeight = MAP_MAX_HEIGHT);
7576

7677
// Returns 'yes' if the defined value is true or 'no' if it is false.
7778
wxString b2yn(bool v);

source/main_toolbar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ void MainToolBar::OnPositionKeyUp(wxKeyEvent& event)
536536
void MainToolBar::OnPastePositionText(wxClipboardTextEvent& event)
537537
{
538538
Position position;
539-
if (posFromClipboard(position.x, position.y, position.z)) {
539+
const Map& currentMap = g_gui.GetCurrentMap();
540+
if (posFromClipboard(position, currentMap.getWidth(), currentMap.getHeight())) {
540541
x_control->SetIntValue(position.x);
541542
y_control->SetIntValue(position.y);
542543
z_control->SetIntValue(position.z);

source/old_properties_window.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,13 @@ OldPropertiesWindow::OldPropertiesWindow(wxWindow* win_parent, const Map* map, c
329329

330330
wxSizer* possizer = newd wxBoxSizer(wxHORIZONTAL);
331331
x_field = newd wxSpinCtrl(this, wxID_ANY, i2ws(teleport->getX()), wxDefaultPosition, wxSize(-1, 20), wxSP_ARROW_KEYS, 0, map->getWidth(), teleport->getX());
332+
x_field->Bind(wxEVT_CHAR, &OldPropertiesWindow::OnChar, this);
332333
possizer->Add(x_field, wxSizerFlags(3).Expand());
333334
y_field = newd wxSpinCtrl(this, wxID_ANY, i2ws(teleport->getY()), wxDefaultPosition, wxSize(-1, 20), wxSP_ARROW_KEYS, 0, map->getHeight(), teleport->getY());
335+
y_field->Bind(wxEVT_CHAR, &OldPropertiesWindow::OnChar, this);
334336
possizer->Add(y_field, wxSizerFlags(3).Expand());
335337
z_field = newd wxSpinCtrl(this, wxID_ANY, i2ws(teleport->getZ()), wxDefaultPosition, wxSize(-1, 20), wxSP_ARROW_KEYS, 0, MAP_MAX_LAYER, teleport->getZ());
338+
z_field->Bind(wxEVT_CHAR, &OldPropertiesWindow::OnChar, this);
336339
possizer->Add(z_field, wxSizerFlags(2).Expand());
337340

338341
subsizer->Add(possizer, wxSizerFlags(1).Expand());
@@ -593,6 +596,24 @@ void OldPropertiesWindow::OnFocusChange(wxFocusEvent& event)
593596
text->SetSelection(-1, -1);
594597
}
595598

599+
void OldPropertiesWindow::OnChar(wxKeyEvent& evt)
600+
{
601+
if (evt.GetKeyCode() == WXK_CONTROL_V) {
602+
Position position;
603+
const Editor* const editor = g_gui.GetCurrentEditor();
604+
if (posFromClipboard(position, editor->getMapWidth(), editor->getMapHeight())) {
605+
x_field->SetValue(position.x);
606+
y_field->SetValue(position.y);
607+
z_field->SetValue(position.z);
608+
return;
609+
610+
}
611+
612+
}
613+
614+
evt.Skip();
615+
}
616+
596617
void OldPropertiesWindow::OnClickOK(wxCommandEvent& WXUNUSED(event))
597618
{
598619
if(edit_item) {

source/old_properties_window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class OldPropertiesWindow : public ObjectPropertiesWindowBase
3434
virtual ~OldPropertiesWindow();
3535

3636
void OnFocusChange(wxFocusEvent&);
37+
void OnChar(wxKeyEvent& evt);
3738

3839
void OnClickOK(wxCommandEvent&);
3940
void OnClickCancel(wxCommandEvent&);

source/positionctrl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ wxStaticBoxSizer(wxHORIZONTAL, parent, label)
3535
z_field = newd NumberTextCtrl(parent, wxID_ANY, z, 0, maxz, wxTE_PROCESS_ENTER, "Z", wxDefaultPosition, wxSize(35, 20));
3636
z_field->Bind(wxEVT_TEXT_PASTE, &PositionCtrl::OnClipboardText, this);
3737
Add(z_field, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5);
38+
39+
maxWidth = maxx;
40+
maxHeight = maxy;
3841
}
3942

4043
PositionCtrl::~PositionCtrl()
@@ -66,7 +69,7 @@ bool PositionCtrl::Enable(bool enable)
6669
void PositionCtrl::OnClipboardText(wxClipboardTextEvent& evt)
6770
{
6871
Position position;
69-
if(posFromClipboard(position.x, position.y, position.z)) {
72+
if (posFromClipboard(position, maxWidth, maxHeight)) {
7073
x_field->SetIntValue(position.x);
7174
y_field->SetIntValue(position.y);
7275
z_field->SetIntValue(position.z);

source/positionctrl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class PositionCtrl : public wxStaticBoxSizer
4545
NumberTextCtrl* x_field;
4646
NumberTextCtrl* y_field;
4747
NumberTextCtrl* z_field;
48+
49+
private:
50+
int maxWidth, maxHeight;
4851
};
4952

5053
#endif

0 commit comments

Comments
 (0)