From 26ff8dcc8f4928a774a490f98d8540f6c543fa70 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 3 Oct 2013 23:04:16 +1000 Subject: [PATCH] Created RegionPager --- editor/region_pager.cpp | 55 +++++++++++++++++++++++++++++++++++++++++ editor/region_pager.hpp | 36 +++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 editor/region_pager.cpp create mode 100644 editor/region_pager.hpp diff --git a/editor/region_pager.cpp b/editor/region_pager.cpp new file mode 100644 index 0000000..335b015 --- /dev/null +++ b/editor/region_pager.cpp @@ -0,0 +1,55 @@ +#include "region_pager.hpp" + +#include + +RegionPager::RegionPager() { + // +} + +RegionPager::~RegionPager() { + if (onDelete) { + for (auto& i : regionList) { + onDelete(&i); + } + } +} + +Region* RegionPager::NewRegion(int x, int y) { + for (auto& i : regionList) { + if (i.GetX() == x && i.GetY() == y) { + throw(std::runtime_error("Duplicate Regions detected")); + } + } + + regionList.push_front({x, y, regionWidth, regionHeight}); + if (onNew) { + onNew(®ionList.front()); + } + return ®ionList.front(); +} + +Region* RegionPager::GetRegion(int x, int y) { + for (auto& i : regionList) { + if (i.GetX() == x && i.GetY() == y) { + return &i; + } + } + //create, insert and return + regionList.push_front({x, y, regionWidth, regionHeight}); + if (onNew) { + onNew(®ionList.front()); + } + return ®ionList.front(); +} + +void RegionPager::DeleteRegion(int x, int y) { + for (std::list::iterator i = regionList.begin(); i != regionList.end(); i++) { + if (i->GetX() == x && i->GetY() == y) { + if (onDelete) { + onDelete(&(*i)); + } + regionList.erase(i); + break; + } + } +} diff --git a/editor/region_pager.hpp b/editor/region_pager.hpp new file mode 100644 index 0000000..f001897 --- /dev/null +++ b/editor/region_pager.hpp @@ -0,0 +1,36 @@ +#ifndef REGIONPAGER_HPP_ +#define REGIONPAGER_HPP_ + +#include "region.hpp" + +#include + +class RegionPager { +public: + RegionPager(); + ~RegionPager(); + + //these parameters MUST be multiples of the width & height + Region* NewRegion(int x, int y); + Region* GetRegion(int x, int y); + void DeleteRegion(int x, int y); + + void SetOnDelete(void(*f)(Region* const)) { onDelete = f; } + void SetOnNew(void(*f)(Region* const)) { onNew = f; } + + //accessors and mutators + int SetWidth(int i) { return regionWidth = i; } + int SetHeight(int i) { return regionHeight = i; } + + int GetWidth() const { return regionWidth; } + int GetHeight() const { return regionHeight; } + + std::list* GetRegions() { return ®ionList; } +private: + std::list regionList; + int regionWidth = 0, regionHeight = 0; + void (*onDelete)(Region* const) = nullptr; + void (*onNew)(Region* const) = nullptr; +}; + +#endif