Commit e48e7639 authored by Leeor Dicker's avatar Leeor Dicker

Added a draggable MiniMap window that can be toggled on and off.

- Pulled out minimap drawing code from EditorState and moved to MiniMap instead.
- Moved camera positioning logic from EditorState to MiniMap.
- Pulled out legacy middle mouse button handler code.
parent 0c6b4f4d
......@@ -102,6 +102,7 @@
<ClInclude Include="..\..\src\GameField.h" />
<ClInclude Include="..\..\src\Map.h" />
<ClInclude Include="..\..\src\Menu.h" />
<ClInclude Include="..\..\src\MiniMap.h" />
<ClInclude Include="..\..\src\Pattern.h" />
<ClInclude Include="..\..\src\StartState.h" />
<ClInclude Include="..\..\src\TextField.h" />
......@@ -121,6 +122,7 @@
<ClCompile Include="..\..\src\main.cpp" />
<ClCompile Include="..\..\src\Map.cpp" />
<ClCompile Include="..\..\src\Menu.cpp" />
<ClCompile Include="..\..\src\MiniMap.cpp" />
<ClCompile Include="..\..\src\StartState.cpp" />
<ClCompile Include="..\..\src\TextField.cpp" />
<ClCompile Include="..\..\src\TilePalette.cpp" />
......
......@@ -81,6 +81,9 @@
<ClInclude Include="..\..\src\ToolBar.h">
<Filter>Header Files\UI</Filter>
</ClInclude>
<ClInclude Include="..\..\src\MiniMap.h">
<Filter>Header Files\UI</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\EditorState.cpp">
......@@ -128,6 +131,9 @@
<ClCompile Include="..\..\src\ToolBar.cpp">
<Filter>Source Files\UI</Filter>
</ClCompile>
<ClCompile Include="..\..\src\MiniMap.cpp">
<Filter>Source Files\UI</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Landlord.rc">
......
This diff is collapsed.
......@@ -6,6 +6,7 @@
#include "Entity.h"
#include "Map.h"
#include "Menu.h"
#include "MiniMap.h"
#include "TilePalette.h"
#include "ToolBar.h"
......@@ -88,7 +89,6 @@ private:
void handleLeftButtonDown(int x, int y);
void handleRightButtonDown();
void handleMiddleButtonDown();
void saveUndo();
......@@ -96,27 +96,28 @@ private:
void restorePreviousState();
void updateUI();
void drawMiniMap();
void createMiniMap();
void toolbar_event(ToolBar::ToolBarAction _act);
private:
// RESOURCES
Timer mTimer;
Rectangle_2d mSelectorRect;
Font mFont;
Point_2d mMouseCoords;
Image mMousePointer;
TilePalette mTilePalette;
// PRIMITIVES
Point_2d mMouseCoords;
Point_2df mScrollVector;
Rectangle_2d mSelectorRect;
Rectangle_2d mCellInspectRect;
Font mFont;
// UI ELEMENTS
TilePalette mTilePalette;
ToolBar mToolBar;
MiniMap mMiniMap;
Button mBtnLinkOkay;
Button mBtnLinkCancel;
......@@ -125,34 +126,25 @@ private:
TextField mTxtLinkDestX;
TextField mTxtLinkDestY;
Rectangle_2d mCellInspectRect;
// MAP CONTROLS
Cell* mLinkCell;
Map mMap;
GameField mFieldUndo;
Map mMap;
std::string mMapSavePath;
Image* mMiniMap;
EditState mEditState;
EditState mPreviousEditState;
// FLAGS
bool mDrawDebug;
bool mLeftButtonDown;
bool mRightButtonDown;
bool mPlacingCollision; /**< Flag indicating whether or not to place or clear collision on mouse drags. */
bool mHideUi; /**< Flag indicating that only the map be drawn. */
bool mDrawMiniMap; /**< Flag indicating that we should draw the minimap. */
bool mMapChanged; /**< Used to determine if the map changed. */
State* mReturnState;
};
#endif
\ No newline at end of file
......@@ -77,6 +77,7 @@ public:
protected:
friend class EditorState;
friend class MiniMap;
void drawBg(bool draw);
void drawBgDetail(bool draw);
......
#include "MiniMap.h"
#include "Common.h"
MiniMap::MiniMap():
mFont(nullptr),
mSurface(nullptr),
mMiniMap(nullptr),
mMap(nullptr),
mDragging(false),
mLeftButtonDown(false),
mMovingCamera(false),
mHidden(false)
{
init();
}
MiniMap::~MiniMap()
{
EventHandler& e = Utility<EventHandler>::get();
e.mouseButtonDown().Disconnect(this, &MiniMap::onMouseDown);
e.mouseButtonUp().Disconnect(this, &MiniMap::onMouseUp);
e.mouseMotion().Disconnect(this, &MiniMap::onMouseMotion);
}
void MiniMap::init()
{
EventHandler& e = Utility<EventHandler>::get();
e.mouseButtonDown().Connect(this, &MiniMap::onMouseDown);
e.mouseButtonUp().Connect(this, &MiniMap::onMouseUp);
e.mouseMotion().Connect(this, &MiniMap::onMouseMotion);
Renderer& r = Utility<Renderer>::get();
mRect.x(2);
mRect.y(34);
}
void MiniMap::hidden(bool _b)
{
mHidden = _b;
}
void MiniMap::adjustCamera(int x, int y)
{
Renderer& r = Utility<Renderer>::get();
mMap->setCamera((mMap->tileset().width() * (x - mRect.x() + 4)) - (r.width() / 2), (mMap->tileset().height() * (y - mRect.y() - 21)) - (r.height() / 2));
}
void MiniMap::onMouseDown(MouseButton b, int x, int y)
{
if (b != BUTTON_LEFT || hidden())
return;
if (isPointInRect(x, y, rect().x(), rect().y(), rect().w(), 17))
mDragging = true;
if (isPointInRect(x, y, mRect.x() + 4, mRect.y() + 21, mMiniMap->width(), mMiniMap->height()))
{
mMovingCamera = true;
adjustCamera(x, y);
}
mLeftButtonDown = true;
}
void MiniMap::onMouseUp(MouseButton b, int x, int y)
{
if (b != BUTTON_LEFT || hidden())
return;
mDragging = false;
mLeftButtonDown = false;
mMovingCamera = false;
}
void MiniMap::onMouseMotion(int x, int y, int relX, int relY)
{
if (mDragging)
{
mRect(mRect.x() + relX, mRect.y() + relY, mRect.w(), mRect.h());
return;
}
if(mMovingCamera)
{
adjustCamera(x, y);
return;
}
}
void MiniMap::update()
{
if (hidden())
return;
Renderer& r = Utility<Renderer>::get();
r.drawBoxFilled(rect(), 180, 180, 180);
r.drawBox(rect(), 0, 0, 0);
if (mFont)
{
r.drawText(*mFont, "MiniMap", rect().x() + 2, rect().y() + 2, 0, 0, 0);
r.drawText(*mFont, "MiniMap", rect().x() + 3, rect().y() + 2, 0, 0, 0);
}
Point_2d pt(0, 0);
Point_2d upperLeft = mMap->getGridCoords(pt);
pt.x() += r.width();
pt.y() += r.height();
Point_2d lowerRight = mMap->getGridCoords(pt);
r.drawBoxFilled(mRect.x() + 4, mRect.y() + 21, mMiniMap->width(), mMiniMap->height(), 255, 0, 255);
r.drawImage(*mMiniMap, mRect.x() + 4, mRect.y() + 21);
Rectangle_2d rect(mRect.x() + 4 + upperLeft.x(), mRect.y() + 21 + upperLeft.y(), lowerRight.x() - upperLeft.x(), lowerRight.y() - upperLeft.y());
r.drawBox(rect, 255, 255, 255);
}
void MiniMap::map(Map* _m)
{
mMap = _m;
createMiniMap();
mRect(mRect.x(), mRect.y(), mMap->width() + 8, mMap->height() + 25);
}
void MiniMap::update_minimap()
{
createMiniMap();
cout << "MiniMap::update_minimap() called." << endl;
}
void MiniMap::createMiniMap()
{
Uint32 rmask, gmask, bmask, amask;
// Set up channel masks.
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; }
else { rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; }
mSurface = SDL_CreateRGBSurface(0, mMap->width(), mMap->height(), 32, rmask, gmask, bmask, amask);
if (!mSurface)
return;
Color_4ub _c;
for (int y = 0; y < mMap->height(); y++)
{
for (int x = 0; x < mMap->width(); x++)
{
Cell& cell = mMap->field().cell(x, y);
_c = mMap->tileset().averageColor(cell.index(Cell::LAYER_BASE));
DrawPixel(mSurface, x, y, _c.red(), _c.green(), _c.blue(), _c.alpha());
if (cell.index(Cell::LAYER_BASE_DETAIL) != -1)
{
_c = mMap->tileset().averageColor(cell.index(Cell::LAYER_BASE_DETAIL));
DrawPixel(mSurface, x, y, _c.red(), _c.green(), _c.blue(), _c.alpha());
}
if (cell.index(Cell::LAYER_DETAIL) != -1)
{
_c = mMap->tileset().averageColor(cell.index(Cell::LAYER_DETAIL));
DrawPixel(mSurface, x, y, _c.red(), _c.green(), _c.blue(), _c.alpha());
}
if (cell.index(Cell::LAYER_FOREGROUND) != -1)
{
_c = mMap->tileset().averageColor(cell.index(Cell::LAYER_FOREGROUND));
DrawPixel(mSurface, x, y, _c.red(), _c.green(), _c.blue(), _c.alpha());
}
}
}
if (mMiniMap)
delete mMiniMap;
mMiniMap = new Image(mSurface->pixels, mSurface->format->BytesPerPixel, mSurface->w, mSurface->h);
SDL_FreeSurface(mSurface);
mSurface = nullptr;
}
#pragma once
#include "NAS2D/NAS2D.h"
#include "Map.h"
using namespace NAS2D;
class MiniMap
{
public:
MiniMap();
~MiniMap();
Rectangle_2d& rect() { return mRect; }
void font(Font* _f) { mFont = _f; }
void map(Map* _m);
void hidden(bool _b);
bool hidden() const { return mHidden; }
bool dragging() const { return mDragging; }
bool moving_camera() const { return mMovingCamera; }
void update();
void update_minimap();
private:
void init();
void onMouseDown(MouseButton b, int x, int y);
void onMouseUp(MouseButton b, int x, int y);
void onMouseMotion(int x, int y, int relX, int relY);
void createMiniMap();
void adjustCamera(int x, int y);
private:
Rectangle_2d mRect;
Font* mFont;
SDL_Surface* mSurface;
Image* mMiniMap;
Map* mMap;
bool mDragging;
bool mLeftButtonDown;
bool mMovingCamera;
bool mHidden;
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment