Skip to content

Commit

Permalink
#18, #22 Categories partly handled, tiles categorised as "floor" are in
Browse files Browse the repository at this point in the history
top level category, all others are in sub tile group.

Sub tilegroup support added
  • Loading branch information
LupusUmbrae committed Jan 14, 2014
1 parent 6945cf7 commit 04d20de
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/menus/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MenuItem {
utils::MapTexture* iconHover);
virtual ~MenuItem();

//! Get teh icon (if hovers set returns the hover image)
//! Get the icon (if hovers set returns the hover image)
utils::MapTexture* getIcon();
//! Set whether the mouse is hovering over this item
void setHover(bool hovering);
Expand Down
44 changes: 27 additions & 17 deletions include/menus/TileGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
// SDL Includes

// Map Includes
#include "MenuItem.h"
#include "../IDisplay.h"
#include "../utils/MapTextures.h"
#include "../utils/textures/Text.h"
#include "../utils/logger.h"
#include "../actions/IAction.h"
#include "../actions/ActionQueue.h"
Expand All @@ -29,6 +31,13 @@ namespace menu {
*/
class TileGroup: public display::IDisplay {
public:
//! Set the static images for tile groups
/*!
*
*
*@param groupOpen Icon to show that the group is open
*@param groupClosed Icon to show that the group is closed
*/
static void setImages(utils::MapTexture* groupOpen,
utils::MapTexture* groupClosed);

Expand All @@ -38,22 +47,27 @@ class TileGroup: public display::IDisplay {
* @param groupName Name of the group to be displayed
* @param tiles The tiles in this group
*/
TileGroup(utils::MapTexture* groupName,
std::vector<utils::MapTexture*> tiles);
TileGroup(utils::MapTexture* groupName, std::vector<MenuItem*> tiles,
SDL_Renderer* renderer);

void handleEvents(SDL_Event event);

void render();

//! Re-draw the group with the new location
/*!
* Redraw the group and return it's height.
*
* @param x X position on the screen of this group
* @param y Y position of the screen of this group
* @param w Width that this group can use
* @return height of the groups
*/
int redraw(int x, int y, int w);

bool hasChanged() {
return changed;
}
void addGroup(std::vector<MenuItem*> newGroup, std::string category);

int getHeight() {
return height;
}
bool hasChanged();

//! Is the group currently open?
bool isOpen();
Expand All @@ -63,35 +77,31 @@ class TileGroup: public display::IDisplay {
return groupName;
}

//! get the tiles
std::vector<utils::MapTexture*> getTiles() {
return tiles;
}

private:
//! Group close icon
static utils::MapTexture* groupClosed;
//! Group open icon
static utils::MapTexture* groupOpen;

//! Draws the group.
void draw();

//! action to be passed to the queue
action::IAction action;

int height;

//! Size of the tiles
int scale = 20;

//! Group name to be displayed
utils::MapTexture* groupName;

//! Tile images
std::vector<utils::MapTexture*> tiles;
std::vector<MenuItem*> tiles;

std::vector<TileGroup*> subGroups;

//! Tile images with location on screen
std::map<SDL_Rect*, utils::MapTexture*> tileMap;
std::map<SDL_Rect*, MenuItem*> tileMap;

//! Location for the open/close icon and name
SDL_Rect* nameRect = new SDL_Rect();
Expand Down
2 changes: 2 additions & 0 deletions include/utils/JsonProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <SDL2/SDL.h>

// Map Includes
#include "../actions/IAction.h"
#include "../menus/TileGroup.h"
#include "../menus/MenuItem.h"
#include "../mapping/Tile.h"
#include "textures/Image.h"
#include "textures/Text.h"
Expand Down
Binary file added resources/tilesets/set1/objects/chest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions resources/tilesets/set1/set.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"name" : "tile4",
"filename" : "stone4.png",
"category" : "floor"
},
{
"name" : "chest",
"filename" : "objects/chest.png",
"category" : "chests"
}
]
}
Expand Down
Binary file added resources/verdana.ttf
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ bool Map::loadResources() {
* Fonts
*/

font = TTF_OpenFont("resources/arial.ttf", 10);
font = TTF_OpenFont("resources/verdana.ttf", 10);
if (font == NULL) {
std::cout << "TTF_OpenFont Error: " << TTF_GetError() << std::endl;
return false;
}

font2 = TTF_OpenFont("resources/arial.ttf", 15);
font2 = TTF_OpenFont("resources/verdana.ttf", 15);
if (font == NULL) {
std::cout << "TTF_OpenFont Error: " << TTF_GetError() << std::endl;
return false;
Expand Down
1 change: 1 addition & 0 deletions src/menus/LeftMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void LeftMenu::handleEvents(SDL_Event event) {
void LeftMenu::draw() {
int y = areaRect->y;

// Tell each tile group to re-draw itself
for (TileGroup* curGroup : groups) {
y += curGroup->redraw(areaRect->x, y, areaRect->w);
y += SPACER;
Expand Down
2 changes: 1 addition & 1 deletion src/menus/MenuItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ MenuItem::~MenuItem() {
}

utils::MapTexture* MenuItem::getIcon() {
if (hover) {
if (hover && iconHover != NULL) {
return iconHover;
}

Expand Down
67 changes: 53 additions & 14 deletions src/menus/TileGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ void TileGroup::setImages(utils::MapTexture* groupOpen,
TileGroup::groupClosed = groupClosed;
}

TileGroup::TileGroup(utils::MapTexture* groupName,
std::vector<utils::MapTexture*> tiles) {
TileGroup::TileGroup(utils::MapTexture* groupName, std::vector<MenuItem*> tiles,
SDL_Renderer* renderer) {
this->groupName = groupName;
this->tiles = tiles;
this->renderer = renderer;
}

void TileGroup::handleEvents(SDL_Event event) {

if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {

if (this->open) {

SDL_Rect* tileRect;
utils::MapTexture* tileTex;
MenuItem* tileItem;

for (auto itemIter = tileMap.begin(); itemIter != tileMap.end();
++itemIter) {

tileRect = itemIter->first;
tileTex = itemIter->second;
tileItem = itemIter->second;

if (curX >= tileRect->x
&& (curX <= (tileRect->x + scale))) {
if (curY >= tileRect->y
&& (curY <= (tileRect->y + scale))) {
action.setAction(action::CHANGE_TILE);
action.setObject(tileTex);
action::ActionQueue::getInstance().addAction(
&action);
&tileItem->action);
break;
}
}
Expand All @@ -58,13 +58,17 @@ void TileGroup::handleEvents(SDL_Event event) {
this->changed = true;
}
}

for (TileGroup* curGroup : subGroups) {
curGroup->handleEvents(event);
}
}
}
}

void TileGroup::render() {
SDL_Rect* tileRect;
utils::MapTexture* tileTex;
MenuItem* tileItem;

if (open) {
groupOpen->render(nameRect->x, nameRect->y, 10, 10);
Expand All @@ -77,11 +81,16 @@ void TileGroup::render() {
for (auto itemIter = tileMap.begin(); itemIter != tileMap.end();
++itemIter) {
tileRect = itemIter->first;
tileTex = itemIter->second;
tileItem = itemIter->second;

tileItem->getIcon()->render(tileRect->x, tileRect->y, scale, scale);
}

tileTex->render(tileRect->x, tileRect->y, scale, scale);
for (TileGroup* curGroup : subGroups) {
curGroup->render();
}
}

}

int TileGroup::redraw(int x, int y, int w) {
Expand All @@ -94,6 +103,30 @@ int TileGroup::redraw(int x, int y, int w) {
return areaRect->h;
}

void TileGroup::addGroup(std::vector<MenuItem*> newGroup,
std::string category) {
utils::Text* groupName = new utils::Text(renderer);
groupName->createText(category);

TileGroup* group = new TileGroup(groupName, newGroup, renderer);
subGroups.push_back(group);
}

bool TileGroup::hasChanged() {
changed = this->changed;

if (!changed) {
for (TileGroup* curGroup : subGroups) {
changed = curGroup->hasChanged();
if (changed) {
break;
}
}
}

return changed;
}

bool TileGroup::isOpen() {
return this->open;
}
Expand Down Expand Up @@ -122,27 +155,33 @@ void TileGroup::draw() {
renY += 15;

SDL_Rect* tileRect;
for (utils::MapTexture* curTex : tiles) {

for (MenuItem* curItem : tiles) {
tileRect = new SDL_Rect();

tileRect->x = renX;
tileRect->y = renY;
tileMap.insert(std::make_pair(tileRect, curTex));
tileMap.insert(std::make_pair(tileRect, curItem));
renX += scale + SPACER;

if (renX >= (areaRect->w - (scale + SPACER))) {
renX = this->areaRect->x + SPACER;
renY += scale + SPACER;
}
}

renY += scale;

for (TileGroup* curGroup : subGroups) {
renY += curGroup->redraw(startX + 5, renY, areaRect->w - 5);
}
} else {
renY += 10;
}

areaRect->x = startX;
areaRect->y = startY;
areaRect->w = this->areaRect->w;

areaRect->h = renY - startY;
}

Expand Down
41 changes: 38 additions & 3 deletions src/utils/JsonProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ std::vector<menu::TileGroup*> JsonProcessor::loadTilesets(std::string rootDir) {
Json::Reader reader;
Json::Value root;

std::map<std::string, std::vector<menu::MenuItem*> > categories;

std::vector<menu::TileGroup*> groups;
std::vector<utils::MapTexture*> tiles;
std::vector<menu::MenuItem*> tiles;
menu::TileGroup* curGroup;
utils::Image* curTile;
utils::Text* groupName;

menu::MenuItem* menuItem;

DIR* dir;
struct dirent* ent;

std::stringstream setFolder;
std::stringstream jsonFolder;
std::stringstream imageLocation;
std::stringstream uniqueName;
std::string category;

if ((dir = opendir(rootDir.c_str())) != NULL) {
std::ifstream ifile;
Expand All @@ -48,6 +53,7 @@ std::vector<menu::TileGroup*> JsonProcessor::loadTilesets(std::string rootDir) {
if (ifile) {
if (reader.parse(ifile, root)) {
tiles.clear();
categories.clear();
// Create tile set
groupName = new utils::Text(renderer);
groupName->createText(root["name"].asString());
Expand All @@ -64,11 +70,40 @@ std::vector<menu::TileGroup*> JsonProcessor::loadTilesets(std::string rootDir) {
uniqueName << ent->d_name << "."
<< imagesToLoad[i].get("name", "").asString();

category =
imagesToLoad[i].get("category", "unknown").asString();

curTile->loadImage(imageLocation.str());
curTile->setUniqueName(uniqueName.str());
tiles.push_back(curTile);
menuItem = new menu::MenuItem(
imagesToLoad[i].get("name", "").asString(),
NULL,
action::IAction(action::CHANGE_TILE, curTile),
curTile, NULL);

if (categories.find(category) == categories.end()) {
categories.insert(
std::make_pair(category,
std::vector<menu::MenuItem*>()));
}
categories.at(category).push_back(menuItem);

}

// make safe!!
for (menu::MenuItem* curItem : categories.find("floor")->second) {
tiles.push_back(curItem);
}

curGroup = new menu::TileGroup(groupName, tiles, renderer);

categories.erase("floor");

for (auto itemIter = categories.begin();
itemIter != categories.end(); ++itemIter) {
curGroup->addGroup(itemIter->second, itemIter->first);
}
curGroup = new menu::TileGroup(groupName, tiles);

groups.push_back(curGroup);

} else {
Expand Down

1 comment on commit 04d20de

@LupusUmbrae
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#23 also adds basic category functionality to tilesets

Please sign in to comment.