summary refs log tree commit diff stats
path: root/tools/mapedit/src/tile_widget.cpp
blob: 3dccecb2e7081681a654b30bf380ff378168bb25 (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
#include "tile_widget.h"
#include "map.h"

IMPLEMENT_DYNAMIC_CLASS(TileWidget,wxScrolledWindow)

BEGIN_EVENT_TABLE(TileWidget, wxScrolledWindow)
	EVT_PAINT(TileWidget::OnPaint)
  EVT_LEFT_DOWN(TileWidget::OnClick)
END_EVENT_TABLE()

TileWidget::TileWidget()
{
  Init();
}

TileWidget::TileWidget(wxWindow* parent, wxWindowID winid, int width, int scale, const wxPoint& pos, const wxSize& size)
  : wxScrolledWindow(parent, winid, pos, size), numTilesWidth(width), scale(scale)
{
  Init();
}

int TileWidget::getSelected()
{
  return selected;
}

void TileWidget::Init()
{
  tiles = wxBitmap(wxImage("../../../res/tiles.png"));
  
  this->FitInside();
  this->SetScrollRate(5, 5);
  
  SetVirtualSize(numTilesWidth*TILE_WIDTH*scale, (numTiles / numTilesWidth + 1) * TILE_HEIGHT*scale);
}

void TileWidget::OnPaint(wxPaintEvent& event)
{
  wxPaintDC dc(this);
  wxMemoryDC tiles_dc;
  tiles_dc.SelectObject(tiles);
  
  int vX, vY, vW, vH, aW, aH;
  GetViewStart(&vX, &vY);
  GetVirtualSize(&vW, &vH);
  GetSize(&aW, &aH);

  for (int i=0; i<numTiles; i++)
  {
    dc.StretchBlit(i%numTilesWidth*TILE_WIDTH*scale-vX, i/numTilesWidth*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, i%8*TILE_WIDTH, i/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
    
    if (i == selected)
    {
      wxPen pen(*wxGREEN, 2);
      dc.SetPen(pen);
      dc.SetBrush(*wxTRANSPARENT_BRUSH);
      dc.DrawRectangle(i%numTilesWidth*TILE_WIDTH*scale - vX, i/numTilesWidth*TILE_HEIGHT*scale - vY, TILE_WIDTH*scale, TILE_HEIGHT*scale);
    }
  }
}

void TileWidget::OnClick(wxMouseEvent& event)
{
  int vX, vY, vW, vH;
  GetViewStart(&vX, &vY);
  GetVirtualSize(&vW, &vH);
  
  wxPoint pos = event.GetPosition();
  int x = (pos.x + vX) / (TILE_WIDTH * scale);
  int y = (pos.y + vY) / (TILE_HEIGHT * scale);
  
  selected = x+y*numTilesWidth;
  
  Refresh();
  event.Skip();
}