From 5c6d5cf1b4a22ae6d35b9c081bf34afec263ba63 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 17 Mar 2015 15:58:02 -0400 Subject: Made some nice changes to the map editor widget Center the map edit widget Map edit zooming in should happen around the cursor Disallow editing outside the mapedit widget Add some scroll space around the mapedit widget --- tools/mapedit/src/frame.cpp | 4 +- tools/mapedit/src/widget.cpp | 95 +++++++++++++++++++++++++++++++++----------- tools/mapedit/src/widget.h | 6 +-- tools/mapedit/src/world.cpp | 2 +- 4 files changed, 78 insertions(+), 29 deletions(-) (limited to 'tools') diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index 2a553b6..6fb70c8 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp @@ -134,7 +134,7 @@ MapeditFrame::MapeditFrame(std::unique_ptr world) : wxFrame(NULL, wxID_AN // Layout 3: Splitter between map editor and properties editor wxSplitterWindow* layout1 = new wxSplitterWindow(this, wxID_ANY); - mapTree = new wxTreeCtrl(layout1, MAP_EDITOR_TREE, wxDefaultPosition, wxSize(200, 0), wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS); + mapTree = new wxTreeCtrl(layout1, MAP_EDITOR_TREE, wxDefaultPosition, wxSize(150, 0), wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS); wxTreeItemId mapTreeRoot = mapTree->AddRoot("root"); populateMapTree(mapTreeRoot, this->world->getRootMaps()); @@ -152,7 +152,7 @@ MapeditFrame::MapeditFrame(std::unique_ptr world) : wxFrame(NULL, wxID_AN mapEditor->frame = this; // Set up property editor - wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY); + wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY, wxDefaultPosition, wxSize(-1, 100)); titleBox = new UndoableTextBox(propertyEditor, MAP_TITLE_TEXTBOX, currentMap->getTitle(), "Edit Map Title", this); titleBox->SetMaxLength(40); diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index 61a8d65..cb10489 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp @@ -1,9 +1,14 @@ #include "widget.h" #include "frame.h" -IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxScrolledWindow) +const int EDITOR_SPACING_X = MAP_WIDTH * TILE_WIDTH / 2; +const int EDITOR_SPACING_Y = MAP_HEIGHT * TILE_HEIGHT / 2; +const int EDITOR_WIDTH = MAP_WIDTH * TILE_WIDTH + EDITOR_SPACING_X * 2; +const int EDITOR_HEIGHT = MAP_HEIGHT * TILE_HEIGHT + EDITOR_SPACING_Y * 2; -BEGIN_EVENT_TABLE(MapeditWidget, wxScrolledWindow) +IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxScrolledCanvas) + +BEGIN_EVENT_TABLE(MapeditWidget, wxScrolledCanvas) EVT_PAINT(MapeditWidget::OnPaint) EVT_LEFT_DOWN(MapeditWidget::OnClick) EVT_RIGHT_DOWN(MapeditWidget::OnRightClick) @@ -18,7 +23,7 @@ MapeditWidget::MapeditWidget() } MapeditWidget::MapeditWidget(wxWindow* parent, wxWindowID winid, Map* map, TileWidget* tileWidget, const wxPoint& pos, const wxSize& size) - : wxScrolledWindow(parent, winid, pos, size), map(map), tileWidget(tileWidget) + : wxScrolledCanvas(parent, winid, pos, size), map(map), tileWidget(tileWidget) { Init(); } @@ -30,12 +35,21 @@ void MapeditWidget::Init() this->FitInside(); this->SetScrollRate(5, 5); + SetVirtualSize(EDITOR_WIDTH, EDITOR_HEIGHT); + + int cW, cH; + GetClientSize(&cW, &cH); + mousePos.x = cW / 2; + mousePos.y = cH / 2; + +// Scroll(GAME_WIDTH*1.5-mousePos.x, GAME_HEIGHT*1.5-mousePos.y); + SetZoomSize(2); } wxSize MapeditWidget::DoGetBestSize() const { - return {GAME_WIDTH*2, GAME_HEIGHT*2}; + return {EDITOR_WIDTH*scale, EDITOR_HEIGHT*scale}; } void MapeditWidget::OnPaint(wxPaintEvent&) @@ -43,12 +57,18 @@ void MapeditWidget::OnPaint(wxPaintEvent&) wxPaintDC dc(this); wxMemoryDC tiles_dc; tiles_dc.SelectObject(tiles); - int vX, vY; + + int vX, vY, vXX, vXY, vW, vH, cW, cH; GetViewStart(&vX, &vY); - int vXX, vYX; - GetScrollPixelsPerUnit(&vXX, &vYX); + GetVirtualSize(&vW, &vH); + GetClientSize(&cW, &cH); + GetScrollPixelsPerUnit(&vXX, &vXY); vX *= vXX; - vY *= vYX; + vY *= vXY; + + dc.SetPen(*wxGREY_PEN); + dc.SetBrush(*wxGREY_BRUSH); + dc.DrawRectangle(0, 0, cW, cH); for (int y=0; ygetSelected(); } - dc.StretchBlit(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); + wxPoint pos {(x*TILE_WIDTH + EDITOR_SPACING_X)*scale - vX, (y*TILE_HEIGHT + EDITOR_SPACING_Y) * scale - vY}; + + dc.StretchBlit(pos.x, pos.y, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); } } @@ -71,7 +93,7 @@ void MapeditWidget::OnPaint(wxPaintEvent&) wxBitmap sprite = object->object->getSprite(); tiles_dc.SelectObject(sprite); - wxPoint pos {(int) object->position.first*scale-vX, (int) object->position.second*scale-vY}; + wxPoint pos {(object->position.first + EDITOR_SPACING_X)*scale-vX, (object->position.second + EDITOR_SPACING_Y)*scale-vY}; wxSize size {object->object->getWidth()*scale, object->object->getHeight()*scale}; dc.StretchBlit(pos.x, pos.y, size.GetWidth(), size.GetHeight(), &tiles_dc, 0, 0, object->object->getWidth(), object->object->getHeight()); @@ -99,9 +121,9 @@ void MapeditWidget::OnPaint(wxPaintEvent&) tiles_dc.SelectObject(wxNullBitmap); tiles_dc.SelectObject(sprite); - std::pair startPos = map->getWorld()->getStartingPosition(); + std::pair startPos = map->getWorld()->getStartingPosition(); - wxPoint pos {(int) startPos.first*scale-vX, (int) startPos.second*scale-vY}; + wxPoint pos {(startPos.first + EDITOR_SPACING_X)*scale-vX, (startPos.second + EDITOR_SPACING_Y)*scale-vY}; wxSize size {PLAYER_WIDTH[currentPlayer]*scale, PLAYER_HEIGHT[currentPlayer]*scale}; dc.StretchBlit(pos.x, pos.y, size.GetWidth(), size.GetHeight(), &tiles_dc, 0, 0, PLAYER_WIDTH[currentPlayer], PLAYER_HEIGHT[currentPlayer]); @@ -127,10 +149,10 @@ void MapeditWidget::OnPaint(wxPaintEvent&) } else if (editMode == EditTiles) { int tile = tileWidget->getSelected(); - int x = (mousePos.x + vX) / (TILE_WIDTH * scale); - int y = (mousePos.y + vY) / (TILE_HEIGHT * scale); + int x = (mousePos.x + vX - EDITOR_SPACING_X*scale) / (TILE_WIDTH * scale); + int y = (mousePos.y + vY - EDITOR_SPACING_Y*scale) / (TILE_HEIGHT * scale); - wxPoint pos {x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY}; + wxPoint pos {(x*TILE_WIDTH + EDITOR_SPACING_X)*scale-vX, (y*TILE_HEIGHT + EDITOR_SPACING_Y)*scale-vY}; wxSize size {TILE_WIDTH*scale, TILE_HEIGHT*scale}; tiles_dc.SelectObject(wxNullBitmap); @@ -184,8 +206,8 @@ void MapeditWidget::SetTile(wxPoint pos) vX *= vXX; vY *= vYX; - int x = (pos.x + vX) / (TILE_WIDTH * scale); - int y = (pos.y + vY) / (TILE_HEIGHT * scale); + int x = (pos.x + vX - EDITOR_SPACING_X*scale) / (TILE_WIDTH * scale); + int y = (pos.y + vY - EDITOR_SPACING_Y*scale) / (TILE_HEIGHT * scale); changeBuffer.insert({x,y}); @@ -194,6 +216,8 @@ void MapeditWidget::SetTile(wxPoint pos) void MapeditWidget::OnClick(wxMouseEvent& event) { + if (!mouseIsIn) return; + mouseIsDown = true; int vX, vY; @@ -338,14 +362,29 @@ void MapeditWidget::OnRightClick(wxMouseEvent& event) void MapeditWidget::OnMouseMove(wxMouseEvent& event) { mousePos = event.GetPosition(); - mouseIsIn = true; - if (editMode == EditTiles) + int vX, vY, vW, vH; + GetViewStart(&vX, &vY); + GetVirtualSize(&vW, &vH); + int vXX, vYX; + GetScrollPixelsPerUnit(&vXX, &vYX); + vX *= vXX; + vY *= vYX; + + if ((mousePos.x+vX >= EDITOR_SPACING_X*scale) && (mousePos.x+vX < (EDITOR_WIDTH-EDITOR_SPACING_X)*scale) + && (mousePos.y+vY >= EDITOR_SPACING_Y*scale) && (mousePos.y+vY < (EDITOR_HEIGHT-EDITOR_SPACING_Y)*scale)) { - if (mouseIsDown) + mouseIsIn = true; + + if (editMode == EditTiles) { - SetTile(event.GetPosition()); + if (mouseIsDown) + { + SetTile(event.GetPosition()); + } } + } else { + mouseIsIn = false; } Refresh(); @@ -406,9 +445,19 @@ void MapeditWidget::ZoomOut() void MapeditWidget::SetZoomSize(int zoom) { - scale = zoom; + int vX, vY, vXX, vXY; + GetViewStart(&vX, &vY); + GetScrollPixelsPerUnit(&vXX, &vXY); + vX *= vXX; + vY *= vXY; - SetVirtualSize(MAP_WIDTH*TILE_WIDTH*scale, MAP_HEIGHT*TILE_HEIGHT*scale); + int newViewStartX = (vX + mousePos.x) / scale * zoom - mousePos.x; + int newViewStartY = (vY + mousePos.y) / scale * zoom - mousePos.y; + + SetVirtualSize(EDITOR_WIDTH * zoom, EDITOR_HEIGHT * zoom); + Scroll(newViewStartX / vXX, newViewStartY / vXY); + + scale = zoom; Refresh(); } diff --git a/tools/mapedit/src/widget.h b/tools/mapedit/src/widget.h index 4ae22c7..77b840e 100644 --- a/tools/mapedit/src/widget.h +++ b/tools/mapedit/src/widget.h @@ -21,7 +21,7 @@ enum EditMode { EditEntities }; -class MapeditWidget : public wxScrolledWindow { +class MapeditWidget : public wxScrolledCanvas { public: MapeditWidget(); MapeditWidget(wxWindow* parent, wxWindowID winid, Map* map, TileWidget* tileWidget, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize); @@ -54,8 +54,8 @@ class MapeditWidget : public wxScrolledWindow { wxBitmap tiles; TileWidget* tileWidget; bool mouseIsDown = false; - int scale; - wxPoint mousePos; + int scale = 1; + wxPoint mousePos {GAME_WIDTH/2, GAME_HEIGHT/2}; bool mouseIsIn = false; EditMode editMode = EditTiles; int currentPlayer = 0; diff --git a/tools/mapedit/src/world.cpp b/tools/mapedit/src/world.cpp index db1201e..fb0bb36 100644 --- a/tools/mapedit/src/world.cpp +++ b/tools/mapedit/src/world.cpp @@ -114,7 +114,7 @@ World::World(std::string filename) xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); if (key != 0) { - map->setRightmap(atoi((char*) key)); + map->setRightmap(atoi((char*) key), false); } xmlFree(key); } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entities")) -- cgit 1.4.1