summary refs log tree commit diff stats
path: root/tools/mapedit/src/widget.cpp
blob: 030e5567546dde987c73a067e8ed4cfcc5c5a373 (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
#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; y<MAP_HEIGHT; y++)
  {
    for (int x=0; x<MAP_WIDTH; x++)
    {
      int tile = map->mapdata[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;
}