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/CMakeLists.txt | 3 ++ tools/mapedit/src/frame.cpp | 30 +++++++++++++ tools/mapedit/src/frame.h | 25 +++++++++++ tools/mapedit/src/main.cpp | 36 ++-------------- tools/mapedit/src/tile_widget.cpp | 76 ++++++++++++++++++++++++++++++++ tools/mapedit/src/tile_widget.h | 32 ++++++++++++++ tools/mapedit/src/widget.cpp | 91 +++++++++++++++++++++++++++++++++++++++ tools/mapedit/src/widget.h | 38 ++++++++++++++++ 8 files changed, 298 insertions(+), 33 deletions(-) create mode 100644 tools/mapedit/src/frame.cpp create mode 100644 tools/mapedit/src/frame.h create mode 100644 tools/mapedit/src/tile_widget.cpp create mode 100644 tools/mapedit/src/tile_widget.h create mode 100644 tools/mapedit/src/widget.cpp create mode 100644 tools/mapedit/src/widget.h (limited to 'tools') diff --git a/tools/mapedit/CMakeLists.txt b/tools/mapedit/CMakeLists.txt index 9a024c8..ffc9f05 100644 --- a/tools/mapedit/CMakeLists.txt +++ b/tools/mapedit/CMakeLists.txt @@ -40,6 +40,9 @@ set(CMAKE_BUILD_TYPE Debug) add_executable(AromatherapyMapEditor src/main.cpp src/map.cpp + src/frame.cpp + src/widget.cpp + src/tile_widget.cpp ) target_link_libraries(AromatherapyMapEditor ${ALL_LIBS}) install(TARGETS AromatherapyMapEditor RUNTIME DESTINATION ${BIN_DIR}) diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp new file mode 100644 index 0000000..858620e --- /dev/null +++ b/tools/mapedit/src/frame.cpp @@ -0,0 +1,30 @@ +#include "frame.h" +#include "widget.h" +#include "tile_widget.h" + +wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) + EVT_MENU(wxID_EXIT, MapeditFrame::OnExit) +wxEND_EVENT_TABLE() + +MapeditFrame::MapeditFrame(Map map) : wxFrame(NULL, wxID_ANY, "Map Editor", wxPoint(50, 50), wxSize(GAME_WIDTH*3, GAME_HEIGHT*2)), map(map) +{ + wxMenu* menuFile = new wxMenu; + menuFile->Append(wxID_EXIT); + + wxMenuBar* menuBar = new wxMenuBar; + menuBar->Append(menuFile, "&File"); + + SetMenuBar(menuBar); + + wxPanel* panel = new wxPanel(this, wxID_ANY); + int clientWidth, clientHeight; + GetClientSize(&clientWidth, &clientHeight); + + TileWidget* tileEdit = new TileWidget(panel, wxID_ANY, 6, wxPoint(0,0), wxSize(TILE_WIDTH*3*6, clientHeight)); + MapeditWidget* wid = new MapeditWidget(panel, wxID_ANY, &this->map, tileEdit, wxPoint(TILE_WIDTH*3*6+8,0), wxSize(GAME_WIDTH*1.5, GAME_HEIGHT*1.5)); +} + +void MapeditFrame::OnExit(wxCommandEvent& event) +{ + Close(true); +} diff --git a/tools/mapedit/src/frame.h b/tools/mapedit/src/frame.h new file mode 100644 index 0000000..e5d8562 --- /dev/null +++ b/tools/mapedit/src/frame.h @@ -0,0 +1,25 @@ +#ifndef FRAME_H +#define FRAME_H + +#include + +#ifndef WX_PRECOMP +#include +#endif + +#include "map.h" + +class MapeditFrame : public wxFrame { + public: + MapeditFrame() : MapeditFrame(Map()) {} + MapeditFrame(Map map); + + private: + void OnExit(wxCommandEvent& event); + + Map map; + + wxDECLARE_EVENT_TABLE(); +}; + +#endif diff --git a/tools/mapedit/src/main.cpp b/tools/mapedit/src/main.cpp index 09be151..28fa010 100644 --- a/tools/mapedit/src/main.cpp +++ b/tools/mapedit/src/main.cpp @@ -5,50 +5,20 @@ #endif #include "map.h" +#include "frame.h" class MapeditApp : public wxApp { public: virtual bool OnInit(); }; -class MapeditFrame : public wxFrame { - public: - MapeditFrame() : MapeditFrame(Map()) {} - MapeditFrame(Map map); - - private: - void OnExit(wxCommandEvent& event); - - Map map; - - wxDECLARE_EVENT_TABLE(); -}; - -wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) - EVT_MENU(wxID_EXIT, MapeditFrame::OnExit) -wxEND_EVENT_TABLE() - wxIMPLEMENT_APP(MapeditApp); bool MapeditApp::OnInit() { + wxInitAllImageHandlers(); + MapeditFrame* frame = new MapeditFrame(); frame->Show(true); return true; } - -MapeditFrame::MapeditFrame(Map map) : wxFrame(NULL, wxID_ANY, "Map Editor", wxPoint(50, 50), wxSize(450, 340)), map(map) -{ - wxMenu* menuFile = new wxMenu; - menuFile->Append(wxID_EXIT); - - wxMenuBar* menuBar = new wxMenuBar; - menuBar->Append(menuFile, "&File"); - - SetMenuBar(menuBar); -} - -void MapeditFrame::OnExit(wxCommandEvent& event) -{ - Close(true); -} diff --git a/tools/mapedit/src/tile_widget.cpp b/tools/mapedit/src/tile_widget.cpp new file mode 100644 index 0000000..423b7c0 --- /dev/null +++ b/tools/mapedit/src/tile_widget.cpp @@ -0,0 +1,76 @@ +#include "tile_widget.h" +#include "map.h" + +IMPLEMENT_DYNAMIC_CLASS(TileWidget,wxControl) + +BEGIN_EVENT_TABLE(TileWidget, wxControl) + EVT_PAINT(TileWidget::OnPaint) + EVT_LEFT_DOWN(TileWidget::OnClick) +END_EVENT_TABLE() + +TileWidget::TileWidget() +{ + Init(); +} + +TileWidget::TileWidget(wxWindow* parent, wxWindowID winid, int numTilesWidth, const wxPoint& pos, const wxSize& size) + : wxControl(parent, winid, pos, size), numTilesWidth(numTilesWidth) +{ + Init(); +} + +int TileWidget::getSelected() +{ + return selected; +} + +void TileWidget::Init() +{ + tiles = wxBitmap(wxImage("../../../res/tiles.png")); + numTiles = 50; +} + +void TileWidget::OnPaint(wxPaintEvent& event) +{ + wxPaintDC dc(this); + wxMemoryDC tiles_dc; + tiles_dc.SelectObject(tiles); + wxRegionIterator upd(GetUpdateRegion()); + int vW = upd.GetW(); + int vH = upd.GetH(); + + wxPen pen(*wxGREEN, 2); + dc.SetPen(pen); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + + double endWidth = (double) vW / (double) numTilesWidth; + double endHeight = TILE_HEIGHT * (endWidth / (double) TILE_WIDTH); + + for (int i=0; i + +#ifndef WX_PRECOMP +#include +#endif + +class TileWidget : public wxControl { + public: + TileWidget(); + TileWidget(wxWindow* parent, wxWindowID winid, int numTilesWidth = 8, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize); + + int getSelected(); + + protected: + void Init(); + void OnPaint(wxPaintEvent& event); + void OnClick(wxMouseEvent& event); + + private: + int numTilesWidth; + wxBitmap tiles; + int numTiles; + int selected = 0; + + DECLARE_DYNAMIC_CLASS(MapeditWidget); + DECLARE_EVENT_TABLE(); +}; + +#endif 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; +} diff --git a/tools/mapedit/src/widget.h b/tools/mapedit/src/widget.h new file mode 100644 index 0000000..f2bfbbe --- /dev/null +++ b/tools/mapedit/src/widget.h @@ -0,0 +1,38 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +#ifndef WX_PRECOMP +#include +#endif + +#include "map.h" +#include "tile_widget.h" + +class MapeditWidget : public wxControl { + public: + MapeditWidget(); + MapeditWidget(wxWindow* parent, wxWindowID winid, Map* map, TileWidget* tileWidget, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize); + + protected: + void Init(); + virtual wxSize DoGetBestSize() const; + void OnPaint(wxPaintEvent& event); + void OnClick(wxMouseEvent& event); + void OnMouseMove(wxMouseEvent& event); + void OnMouseUp(wxMouseEvent& event); + + private: + void SetTile(wxPoint pos); + + Map* const map = nullptr; + wxBitmap tiles; + TileWidget* tileWidget; + bool mouseIsDown = false; + + DECLARE_DYNAMIC_CLASS(MapeditWidget); + DECLARE_EVENT_TABLE(); +}; + +#endif -- cgit 1.4.1