diff options
Diffstat (limited to 'tools/mapedit/src/widget.cpp')
| -rw-r--r-- | tools/mapedit/src/widget.cpp | 110 |
1 files changed, 99 insertions, 11 deletions
| diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index 6cbedcd..61a8d65 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp | |||
| @@ -55,6 +55,11 @@ void MapeditWidget::OnPaint(wxPaintEvent&) | |||
| 55 | for (int x=0; x<MAP_WIDTH; x++) | 55 | for (int x=0; x<MAP_WIDTH; x++) |
| 56 | { | 56 | { |
| 57 | int tile = map->getTileAt(x, y); | 57 | int tile = map->getTileAt(x, y); |
| 58 | if (changeBuffer.find({x,y}) != end(changeBuffer)) | ||
| 59 | { | ||
| 60 | tile = tileWidget->getSelected(); | ||
| 61 | } | ||
| 62 | |||
| 58 | 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); | 63 | 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); |
| 59 | } | 64 | } |
| 60 | } | 65 | } |
| @@ -151,6 +156,21 @@ void MapeditWidget::OnPaint(wxPaintEvent&) | |||
| 151 | dc.SetPen(pen); | 156 | dc.SetPen(pen); |
| 152 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | 157 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 153 | dc.DrawRectangle(pos.x, pos.y, size.GetWidth(), size.GetHeight()); | 158 | dc.DrawRectangle(pos.x, pos.y, size.GetWidth(), size.GetHeight()); |
| 159 | } else if ((editMode == EditEntities) && (movingEntity != nullptr)) | ||
| 160 | { | ||
| 161 | wxBitmap sprite = movingEntity->object->getSprite(); | ||
| 162 | tiles_dc.SelectObject(wxNullBitmap); | ||
| 163 | tiles_dc.SelectObject(sprite); | ||
| 164 | |||
| 165 | wxPoint pos {mousePos.x - movingEntity->object->getWidth()/2*scale, mousePos.y - movingEntity->object->getHeight()/2*scale}; | ||
| 166 | wxSize size {movingEntity->object->getWidth()*scale, movingEntity->object->getHeight()*scale}; | ||
| 167 | |||
| 168 | dc.StretchBlit(pos.x, pos.y, size.GetWidth(), size.GetHeight(), &tiles_dc, 0, 0, movingEntity->object->getWidth(), movingEntity->object->getHeight()); | ||
| 169 | |||
| 170 | wxPen pen(*wxGREEN, 2); | ||
| 171 | dc.SetPen(pen); | ||
| 172 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | ||
| 173 | dc.DrawRectangle(pos.x, pos.y, size.GetWidth(), size.GetHeight()); | ||
| 154 | } | 174 | } |
| 155 | } | 175 | } |
| 156 | } | 176 | } |
| @@ -167,7 +187,8 @@ void MapeditWidget::SetTile(wxPoint pos) | |||
| 167 | int x = (pos.x + vX) / (TILE_WIDTH * scale); | 187 | int x = (pos.x + vX) / (TILE_WIDTH * scale); |
| 168 | int y = (pos.y + vY) / (TILE_HEIGHT * scale); | 188 | int y = (pos.y + vY) / (TILE_HEIGHT * scale); |
| 169 | 189 | ||
| 170 | map->setTileAt(x, y, tileWidget->getSelected()); | 190 | changeBuffer.insert({x,y}); |
| 191 | |||
| 171 | Refresh(); | 192 | Refresh(); |
| 172 | } | 193 | } |
| 173 | 194 | ||
| @@ -191,17 +212,42 @@ void MapeditWidget::OnClick(wxMouseEvent& event) | |||
| 191 | { | 212 | { |
| 192 | int x = (event.GetPosition().x + vX) / scale - (addingEntity->getWidth() / 2); | 213 | int x = (event.GetPosition().x + vX) / scale - (addingEntity->getWidth() / 2); |
| 193 | int y = (event.GetPosition().y + vY) / scale - (addingEntity->getHeight() / 2); | 214 | int y = (event.GetPosition().y + vY) / scale - (addingEntity->getHeight() / 2); |
| 194 | 215 | ||
| 195 | auto data = std::make_shared<MapObjectEntry>(); | 216 | auto data = std::make_shared<MapObjectEntry>(); |
| 196 | data->object = addingEntity; | 217 | data->object = addingEntity; |
| 197 | data->position = std::make_pair(x,y); | 218 | data->position = std::make_pair(x,y); |
| 198 | map->addObject(data); | ||
| 199 | 219 | ||
| 200 | addingEntity = nullptr; | 220 | frame->commitAction(std::make_shared<Undoable>("Add " + addingEntity->getType(), [=] () { |
| 221 | map->addObject(data); | ||
| 222 | |||
| 223 | Refresh(); | ||
| 224 | }, [=] () { | ||
| 225 | map->removeObject(data); | ||
| 226 | |||
| 227 | Refresh(); | ||
| 228 | })); | ||
| 201 | 229 | ||
| 202 | frame->SetIsAddingEntity(false); | 230 | frame->SetIsAddingEntity(false); |
| 231 | addingEntity = nullptr; | ||
| 232 | } else if (movingEntity != nullptr) | ||
| 233 | { | ||
| 234 | int x = (event.GetPosition().x + vX) / scale - (movingEntity->object->getWidth() / 2); | ||
| 235 | int y = (event.GetPosition().y + vY) / scale - (movingEntity->object->getHeight() / 2); | ||
| 236 | auto oldPos = movingEntity->position; | ||
| 237 | MapObjectEntry* me = movingEntity; | ||
| 203 | 238 | ||
| 204 | Refresh(); | 239 | frame->commitAction(std::make_shared<Undoable>("Move " + movingEntity->object->getType(), [=] () { |
| 240 | me->position = std::make_pair(x,y); | ||
| 241 | |||
| 242 | Refresh(); | ||
| 243 | }, [=] () { | ||
| 244 | me->position = oldPos; | ||
| 245 | |||
| 246 | Refresh(); | ||
| 247 | })); | ||
| 248 | |||
| 249 | frame->SetIsAddingEntity(false); | ||
| 250 | movingEntity = nullptr; | ||
| 205 | } else { | 251 | } else { |
| 206 | int x = (event.GetPosition().x + vX) / scale; | 252 | int x = (event.GetPosition().x + vX) / scale; |
| 207 | int y = (event.GetPosition().y + vY) / scale; | 253 | int y = (event.GetPosition().y + vY) / scale; |
| @@ -211,9 +257,7 @@ void MapeditWidget::OnClick(wxMouseEvent& event) | |||
| 211 | if ((x > selectedEntity->position.first) && (x < selectedEntity->position.first + selectedEntity->object->getWidth()) | 257 | if ((x > selectedEntity->position.first) && (x < selectedEntity->position.first + selectedEntity->object->getWidth()) |
| 212 | && (y > selectedEntity->position.second) && (y < selectedEntity->position.second + selectedEntity->object->getHeight())) | 258 | && (y > selectedEntity->position.second) && (y < selectedEntity->position.second + selectedEntity->object->getHeight())) |
| 213 | { | 259 | { |
| 214 | addingEntity = selectedEntity->object; | 260 | movingEntity = selectedEntity.get(); |
| 215 | map->removeObject(selectedEntity); | ||
| 216 | selectedEntity.reset(); | ||
| 217 | frame->SetIsAddingEntity(true); | 261 | frame->SetIsAddingEntity(true); |
| 218 | } else { | 262 | } else { |
| 219 | selectedEntity.reset(); | 263 | selectedEntity.reset(); |
| @@ -241,12 +285,23 @@ void MapeditWidget::OnClick(wxMouseEvent& event) | |||
| 241 | { | 285 | { |
| 242 | int x = (event.GetPosition().x + vX) / scale - (PLAYER_WIDTH[currentPlayer] / 2); | 286 | int x = (event.GetPosition().x + vX) / scale - (PLAYER_WIDTH[currentPlayer] / 2); |
| 243 | int y = (event.GetPosition().y + vY) / scale - (PLAYER_HEIGHT[currentPlayer] / 2); | 287 | int y = (event.GetPosition().y + vY) / scale - (PLAYER_HEIGHT[currentPlayer] / 2); |
| 288 | auto oldPos = map->getWorld()->getStartingPosition(); | ||
| 289 | auto oldSMap = map->getWorld()->getStartingMap(); | ||
| 290 | |||
| 291 | frame->commitAction(std::make_shared<Undoable>("Set Starting Position", [=] () { | ||
| 292 | map->getWorld()->setStart(map, {x, y}); | ||
| 293 | frame->SetStartposLabel(); | ||
| 294 | |||
| 295 | Refresh(); | ||
| 296 | }, [=] () { | ||
| 297 | map->getWorld()->setStart(oldSMap, oldPos); | ||
| 298 | frame->SetStartposLabel(); | ||
| 299 | |||
| 300 | Refresh(); | ||
| 301 | })); | ||
| 244 | 302 | ||
| 245 | map->getWorld()->setStart(map, {x, y}); | ||
| 246 | isSettingPos = false; | 303 | isSettingPos = false; |
| 247 | frame->SetIsSettingStart(false); | 304 | frame->SetIsSettingStart(false); |
| 248 | |||
| 249 | Refresh(); | ||
| 250 | } | 305 | } |
| 251 | 306 | ||
| 252 | event.Skip(); | 307 | event.Skip(); |
| @@ -299,6 +354,34 @@ void MapeditWidget::OnMouseMove(wxMouseEvent& event) | |||
| 299 | void MapeditWidget::OnMouseUp(wxMouseEvent&) | 354 | void MapeditWidget::OnMouseUp(wxMouseEvent&) |
| 300 | { | 355 | { |
| 301 | mouseIsDown = false; | 356 | mouseIsDown = false; |
| 357 | |||
| 358 | if (editMode == EditTiles) | ||
| 359 | { | ||
| 360 | std::map<std::pair<int, int>, int> localChangeBuffer; | ||
| 361 | for (auto assign : changeBuffer) | ||
| 362 | { | ||
| 363 | localChangeBuffer[assign] = map->getTileAt(assign.first, assign.second); | ||
| 364 | } | ||
| 365 | |||
| 366 | int localSelection = tileWidget->getSelected(); | ||
| 367 | frame->commitAction(std::make_shared<Undoable>("Paint Map", [=] () { | ||
| 368 | for (auto assign : localChangeBuffer) | ||
| 369 | { | ||
| 370 | map->setTileAt(assign.first.first, assign.first.second, localSelection); | ||
| 371 | } | ||
| 372 | |||
| 373 | Refresh(); | ||
| 374 | }, [=] () { | ||
| 375 | for (auto assign : localChangeBuffer) | ||
| 376 | { | ||
| 377 | map->setTileAt(assign.first.first, assign.first.second, assign.second); | ||
| 378 | } | ||
| 379 | |||
| 380 | Refresh(); | ||
| 381 | })); | ||
| 382 | |||
| 383 | changeBuffer.clear(); | ||
| 384 | } | ||
| 302 | } | 385 | } |
| 303 | 386 | ||
| 304 | void MapeditWidget::OnMouseOut(wxMouseEvent&) | 387 | void MapeditWidget::OnMouseOut(wxMouseEvent&) |
| @@ -350,6 +433,7 @@ void MapeditWidget::StartAddingEntity(MapObject* object) | |||
| 350 | void MapeditWidget::CancelAddingEntity() | 433 | void MapeditWidget::CancelAddingEntity() |
| 351 | { | 434 | { |
| 352 | addingEntity = nullptr; | 435 | addingEntity = nullptr; |
| 436 | movingEntity = nullptr; | ||
| 353 | } | 437 | } |
| 354 | 438 | ||
| 355 | void MapeditWidget::SetIsSettingStart(bool isSetting) | 439 | void MapeditWidget::SetIsSettingStart(bool isSetting) |
| @@ -369,6 +453,10 @@ void MapeditWidget::SetIsSettingStart(bool isSetting) | |||
| 369 | void MapeditWidget::SetMap(Map* map) | 453 | void MapeditWidget::SetMap(Map* map) |
| 370 | { | 454 | { |
| 371 | this->map = map; | 455 | this->map = map; |
| 456 | selectedEntity = nullptr; | ||
| 457 | addingEntity = nullptr; | ||
| 458 | movingEntity = nullptr; | ||
| 459 | isSettingPos = false; | ||
| 372 | 460 | ||
| 373 | Refresh(); | 461 | Refresh(); |
| 374 | } | 462 | } |
