summary refs log tree commit diff stats
path: root/tools/mapedit/src/widget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mapedit/src/widget.cpp')
-rw-r--r--tools/mapedit/src/widget.cpp91
1 files changed, 91 insertions, 0 deletions
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 @@
1#include "widget.h"
2
3IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxControl)
4
5BEGIN_EVENT_TABLE(MapeditWidget, wxControl)
6 EVT_PAINT(MapeditWidget::OnPaint)
7 EVT_LEFT_DOWN(MapeditWidget::OnClick)
8 EVT_LEFT_UP(MapeditWidget::OnMouseUp)
9 EVT_MOTION(MapeditWidget::OnMouseMove)
10END_EVENT_TABLE()
11
12MapeditWidget::MapeditWidget()
13{
14 Init();
15}
16
17MapeditWidget::MapeditWidget(wxWindow* parent, wxWindowID winid, Map* map, TileWidget* tileWidget, const wxPoint& pos, const wxSize& size)
18 : wxControl(parent, winid, pos, size), map(map), tileWidget(tileWidget)
19{
20 Init();
21}
22
23void MapeditWidget::Init()
24{
25 tiles = wxBitmap(wxImage("../../../res/tiles.png"));
26}
27
28wxSize MapeditWidget::DoGetBestSize() const
29{
30 return {GAME_WIDTH*2, GAME_HEIGHT*2};
31}
32
33void MapeditWidget::OnPaint(wxPaintEvent& event)
34{
35 wxPaintDC dc(this);
36 wxMemoryDC tiles_dc;
37 tiles_dc.SelectObject(tiles);
38 wxRegionIterator upd(GetUpdateRegion());
39 int vW = upd.GetW();
40 int vH = upd.GetH();
41
42 double endWidth = TILE_WIDTH * ((double) vW / (double) GAME_WIDTH);
43 double endHeight = TILE_HEIGHT * ((double) vH / (double) GAME_HEIGHT);
44
45 for (int y=0; y<MAP_HEIGHT; y++)
46 {
47 for (int x=0; x<MAP_WIDTH; x++)
48 {
49 int tile = map->mapdata[x+y*MAP_WIDTH];
50 dc.StretchBlit(x*endWidth, y*endHeight, endWidth, endHeight, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
51 }
52 }
53}
54
55void MapeditWidget::SetTile(wxPoint pos)
56{
57 wxRegionIterator upd(GetUpdateRegion());
58 int vW = upd.GetW();
59 int vH = upd.GetH();
60
61 double endWidth = TILE_WIDTH * ((double) vW / (double) GAME_WIDTH);
62 double endHeight = TILE_HEIGHT * ((double) vH / (double) GAME_HEIGHT);
63
64 int x = pos.x / endWidth;
65 int y = pos.y / endHeight;
66
67 map->mapdata[x+y*MAP_WIDTH] = tileWidget->getSelected();
68 Refresh();
69}
70
71void MapeditWidget::OnClick(wxMouseEvent& event)
72{
73 mouseIsDown = true;
74
75 SetTile(event.GetPosition());
76
77 event.Skip();
78}
79
80void MapeditWidget::OnMouseMove(wxMouseEvent& event)
81{
82 if (mouseIsDown)
83 {
84 SetTile(event.GetPosition());
85 }
86}
87
88void MapeditWidget::OnMouseUp(wxMouseEvent& event)
89{
90 mouseIsDown = false;
91}