Commit c8982c9b authored by Leeor Dicker's avatar Leeor Dicker

Load Map button is disabled until the editor scans the maps directory and maps...

Load Map button is disabled until the editor scans the maps directory and maps have been found. Closes issue #1.

- StartState will now scan the directory for any files within it. If any are found, each is checked for a "<map>" tag which contains a version string matching the map driver version in the code.
- StartState will draw at least one frame before it begins to scan the maps directory so that the user can see something is, in fact, happening.
- StartState will draw one frame while beginning to load a map so that the user can see that something is, in fact, happening.
- Windows message boxes have been reactivated to display error messages when exceptions are thrown.
parent 2061ee57
......@@ -93,7 +93,6 @@
<ItemGroup>
<ClInclude Include="..\..\src\Button.h" />
<ClInclude Include="..\..\src\Common.h" />
<ClInclude Include="..\..\src\ConfigStrings.h" />
<ClInclude Include="..\..\src\Control.h" />
<ClInclude Include="..\..\src\Defaults.h" />
<ClInclude Include="..\..\src\EditorState.h" />
......
......@@ -33,9 +33,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\ConfigStrings.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Defaults.h">
<Filter>Header Files</Filter>
</ClInclude>
......
......@@ -2,7 +2,6 @@
#include "StartState.h"
#include "Common.h"
#include "ConfigStrings.h"
#include <stack>
......
......@@ -11,6 +11,8 @@
#include <string>
extern const std::string MAP_DRIVER_VERSION;
/**
* \class Map
* \brief Implements a basic 2D tile map.
......
......@@ -24,6 +24,7 @@ void setMessage(const std::string& msg)
StartState::StartState(): mFont("fonts/ui-normal.png", 7, 9, 0),
mMousePointer("sys/normal.png"),
mLayoutRect(15, 15, Utility<Renderer>::get().width() - 30, Utility<Renderer>::get().height() - 40),
mScanningMaps(true),
mReturnState(nullptr)
{
......@@ -64,6 +65,7 @@ void StartState::initialize()
mBtnLoadExisting.text("Load Map");
mBtnLoadExisting.position(mLayoutRect.x() + 10, mLayoutRect.y() + mLayoutRect.h() - 35);
mBtnLoadExisting.click().Connect(this, &StartState::button_LoadExisting_click);
mBtnLoadExisting.enabled(false);
mBtnRefreshLists.font(mFont);
mBtnRefreshLists.size(100, 25);
......@@ -80,7 +82,7 @@ void StartState::initialize()
mTxtHeight.font(mFont);
mTxtHeight.width(100);
mTxtHeight.text(c.option(UI_TEXTFIELD_DEFAULT_HEIGHT));
mTxtHeight.text(UI_TEXTFIELD_DEFAULT_HEIGHT);
mTxtHeight.position(mLayoutRect.x() + mLayoutRect.w() / 2 + 210 + mFont.width("Height:") + 5, mLayoutRect.y() + 10);
mTxtHeight.border(TextField::ALWAYS);
......@@ -91,12 +93,10 @@ void StartState::initialize()
mMapFilesMenu.font(mFont);
mMapFilesMenu.position(mLayoutRect.x() + 10, mLayoutRect.y() + 10);
scanDirectory(EDITOR_MAPS_PATH, mMapFilesMenu);
mMapFilesMenu.width(mLayoutRect.w() / 2 - 20);
mTsetFilesMenu.font(mFont);
mTsetFilesMenu.position(mLayoutRect.x() + mLayoutRect.w() / 2 + 10, mLayoutRect.y() + 50);
scanDirectory(EDITOR_TSET_PATH, mTsetFilesMenu);
mTsetFilesMenu.width(mLayoutRect.w() / 2 - 20);
......@@ -106,25 +106,70 @@ void StartState::initialize()
e.keyDown().Connect(this, &StartState::onKeyDown);
e.mouseMotion().Connect(this, &StartState::onMouseMove);
e.quit().Connect(this, &StartState::onQuit);
fillTilesetMenu();
}
void StartState::fillMapMenu()
{
StringList lst = getFileList(EDITOR_MAPS_PATH);
for (size_t i = 0; i < lst.size(); ++i)
{
File xmlFile = Utility<Filesystem>::get().open(EDITOR_MAPS_PATH + lst[i]);
TiXmlDocument doc;
TiXmlElement *root = nullptr;
doc.Parse(xmlFile.raw_bytes());
if (doc.Error())
continue;
if (doc.FirstChildElement("map"))
{
if (doc.FirstChildElement()->Attribute("version") != MAP_DRIVER_VERSION)
{
cout << "Map '" << EDITOR_MAPS_PATH + lst[i] << "' is version mismatched." << endl;
continue;
}
mMapFilesMenu.addItem(lst[i]);
}
}
mBtnLoadExisting.enabled(!mMapFilesMenu.empty());
}
void StartState::fillTilesetMenu()
{
StringList lst = getFileList(EDITOR_TSET_PATH);
for (size_t i = 0; i < lst.size(); ++i)
mTsetFilesMenu.addItem(lst[i]);
}
/**
* Scans a directory for files, ignoring directories, and
* adds them to a Menu.
* Gets a list of files, ignoring directories.
*
* \param directory Path to scan for files.
* \param menu Reference to a Menu object to add file listings to.
*
* \return Returns a StringList.
*/
void StartState::scanDirectory(const string& directory, Menu& menu)
StringList StartState::getFileList(const string& directory)
{
StringList fileList = Utility<Filesystem>::get().directoryList(directory);
StringList returnList;
for(size_t i = 0; i < fileList.size(); i++)
{
if(!Utility<Filesystem>::get().isDirectory(directory + fileList[i]))
menu.addItem(fileList[i]);
}
Filesystem& f = Utility<Filesystem>::get();
for (size_t i = 0; i < fileList.size(); i++)
if (!f.isDirectory(directory + fileList[i]))
returnList.push_back(fileList[i]);
return returnList;
}
......@@ -195,6 +240,11 @@ void StartState::button_LoadExisting_click()
return;
}
/// Doing it this way only so that the user can get feedback about what the app is doing.
/// \todo This would benefit by spinning loading maps into its own thread.
Utility<Renderer>::get().drawText(mFont, "LOADING MAP. PLEASE WAIT...", mLayoutRect.x(), 5, 255, 255, 0);
Utility<Renderer>::get().update();
mReturnState = new EditorState(mapPath);
}
......@@ -204,10 +254,8 @@ void StartState::button_RefreshLists_click()
mMapFilesMenu.dropAllItems();
mTsetFilesMenu.dropAllItems();
Configuration& c = Utility<Configuration>::get();
scanDirectory(EDITOR_MAPS_PATH, mMapFilesMenu);
scanDirectory(EDITOR_TSET_PATH, mTsetFilesMenu);
mScanningMaps = true;
fillTilesetMenu();
}
......@@ -246,6 +294,16 @@ State* StartState::update()
if (!MESSAGE.empty() && MSG_FLASH)
r.drawText(mFont, MESSAGE, 15, r.height() - 15, 255, 0, 0);
/// Doing it this way only so that the user can get feedback about what the app is doing.
/// \todo This would benefit by spinning loading maps into its own thread.
if (mScanningMaps)
{
r.drawText(mFont, "SCANNING MAPS. PLEASE WAIT...", mLayoutRect.x(), 5, 255, 255, 0);
r.update();
fillMapMenu();
mScanningMaps = false;
}
r.drawImage(mMousePointer, mMouseCoords.x(), mMouseCoords.y());
return mReturnState;
......
......@@ -38,7 +38,10 @@ protected:
private:
void scanDirectory(const std::string& directory, Menu& menu);
StringList getFileList(const std::string& directory);
void fillMapMenu();
void fillTilesetMenu();
void button_CreateNew_click();
void button_LoadExisting_click();
......@@ -64,5 +67,7 @@ private:
Menu mMapFilesMenu; /**< Map File List menu. */
Menu mTsetFilesMenu; /**< Tileset File List menu. */
bool mScanningMaps;
State* mReturnState; /**< State to return during updates. */
};
#include "TilePalette.h"
#include "ConfigStrings.h"
#include "Common.h"
const Point_2d PALETTE_DIMENSIONS = Point_2d(196, 300);
......
......@@ -3,7 +3,6 @@
#include "EditorState.h"
#include "StartState.h"
#include "ConfigStrings.h"
#include "Defaults.h"
......@@ -21,9 +20,6 @@ int main(int argc, char *argv[])
{
Game game("Landlord", argv[0], "editor.xml");
game.mount("editor.zip");
throw;
game.go(new StartState());
}
catch(Exception e)
......@@ -39,7 +35,7 @@ int main(int argc, char *argv[])
catch(...)
{
#if defined(WINDOWS)
MessageBoxA(NULL, "An unexpected error occured. Please yell at Lee about\nthis so he can fix it.", "Unhandled Exception", MB_OK | MB_ICONERROR | MB_TASKMODAL);
MessageBoxA(NULL, "An unexpected error occured. Please yell at Lee about\nthis so he can fix it.\n\nAnd don't forget to include steps to reproduce and a\nlog! 'It crashed' will get you slapped.", "Unhandled Exception", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#endif
}
......
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