From 3e989517ec46c40a82671620577c40765c94a41f Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 15 Mar 2015 11:19:20 -0400 Subject: Added basic ability to set tiles in map editor --- tools/mapedit/src/widget.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tools/mapedit/src/widget.cpp (limited to 'tools/mapedit/src/widget.cpp') diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp new file mode 100644 index 0000000..030e556 --- /dev/null +++ b/tools/mapedit/src/widget.cpp @@ -0,0 +1,91 @@ +#include "widget.h" + +IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxControl) + +BEGIN_EVENT_TABLE(MapeditWidget, wxControl) + EVT_PAINT(MapeditWidget::OnPaint) + EVT_LEFT_DOWN(MapeditWidget::OnClick) + EVT_LEFT_UP(MapeditWidget::OnMouseUp) + EVT_MOTION(MapeditWidget::OnMouseMove) +END_EVENT_TABLE() + +MapeditWidget::MapeditWidget() +{ + Init(); +} + +MapeditWidget::MapeditWidget(wxWindow* parent, wxWindowID winid, Map* map, TileWidget* tileWidget, const wxPoint& pos, const wxSize& size) + : wxControl(parent, winid, pos, size), map(map), tileWidget(tileWidget) +{ + Init(); +} + +void MapeditWidget::Init() +{ + tiles = wxBitmap(wxImage("../../../res/tiles.png")); +} + +wxSize MapeditWidget::DoGetBestSize() const +{ + return {GAME_WIDTH*2, GAME_HEIGHT*2}; +} + +void MapeditWidget::OnPaint(wxPaintEvent& event) +{ + wxPaintDC dc(this); + wxMemoryDC tiles_dc; + tiles_dc.SelectObject(tiles); + wxRegionIterator upd(GetUpdateRegion()); + int vW = upd.GetW(); + int vH = upd.GetH(); + + double endWidth = TILE_WIDTH * ((double) vW / (double) GAME_WIDTH); + double endHeight = TILE_HEIGHT * ((double) vH / (double) GAME_HEIGHT); + + for (int y=0; ymapdata[x+y*MAP_WIDTH]; + dc.StretchBlit(x*endWidth, y*endHeight, endWidth, endHeight, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); + } + } +} + +void MapeditWidget::SetTile(wxPoint pos) +{ + wxRegionIterator upd(GetUpdateRegion()); + int vW = upd.GetW(); + int vH = upd.GetH(); + + double endWidth = TILE_WIDTH * ((double) vW / (double) GAME_WIDTH); + double endHeight = TILE_HEIGHT * ((double) vH / (double) GAME_HEIGHT); + + int x = pos.x / endWidth; + int y = pos.y / endHeight; + + map->mapdata[x+y*MAP_WIDTH] = tileWidget->getSelected(); + Refresh(); +} + +void MapeditWidget::OnClick(wxMouseEvent& event) +{ + mouseIsDown = true; + + SetTile(event.GetPosition()); + + event.Skip(); +} + +void MapeditWidget::OnMouseMove(wxMouseEvent& event) +{ + if (mouseIsDown) + { + SetTile(event.GetPosition()); + } +} + +void MapeditWidget::OnMouseUp(wxMouseEvent& event) +{ + mouseIsDown = false; +} -- cgit 1.4.1