summary refs log tree commit diff stats
path: root/tools/mapedit/src/widget.cpp
blob: 47efa189f895e05033cc3da46d862fb98f852c68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "widget.h"

IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxScrolledWindow)

BEGIN_EVENT_TABLE(MapeditWidget, wxScrolledWindow)
	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)
  : wxScrolledWindow(parent, winid, pos, size), map(map), tileWidget(tileWidget)
{
  Init();
}

void MapeditWidget::Init()
{
  tiles = wxBitmap(wxImage("../../../res/tiles.png"));
  
  this->FitInside();
  this->SetScrollRate(5, 5);
  
  SetZoomSize(2);
}

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);
  int vX, vY;
  GetViewStart(&vX, &vY);

  for (int y=0; y<MAP_HEIGHT; y++)
  {
    for (int x=0; x<MAP_WIDTH; x++)
    {
      int tile = map->mapdata[x+y*MAP_WIDTH];
      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);
    }
  }
}

void MapeditWidget::SetTile(wxPoint pos)
{
  int vX, vY;
  GetViewStart(&vX, &vY);

  int x = (pos.x + vX) / (TILE_WIDTH * scale);
  int y = (pos.y + vY) / (TILE_HEIGHT * scale);
  
  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;
}

void MapeditWidget::ZoomIn()
{
  SetZoomSize(scale+1);
}

void MapeditWidget::ZoomOut()
{
  SetZoomSize(scale-1);
}

void MapeditWidget::SetZoomSize(int zoom)
{
  scale = zoom;
  
  SetVirtualSize(MAP_WIDTH*TILE_WIDTH*scale, MAP_HEIGHT*TILE_HEIGHT*scale);
  
  GetParent()->Refresh();
}