Skip to content

Commit

Permalink
Add map view implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 14, 2024
1 parent 10a54f1 commit 42c5f70
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
2 changes: 2 additions & 0 deletions source/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ target_sources(tactile-core
"src/tactile/core/document/document_manager.cpp"
"src/tactile/core/document/layer_view_impl.cpp"
"src/tactile/core/document/map_document.cpp"
"src/tactile/core/document/map_view_impl.cpp"
"src/tactile/core/document/meta_view_impl.cpp"
"src/tactile/core/document/object_view_impl.cpp"
"src/tactile/core/document/tile_view_impl.cpp"
Expand Down Expand Up @@ -135,6 +136,7 @@ target_sources(tactile-core
"inc/tactile/core/document/document_manager.hpp"
"inc/tactile/core/document/layer_view_impl.hpp"
"inc/tactile/core/document/map_document.hpp"
"inc/tactile/core/document/map_view_impl.hpp"
"inc/tactile/core/document/meta_view_impl.hpp"
"inc/tactile/core/document/object_view_impl.hpp"
"inc/tactile/core/document/tile_view_impl.hpp"
Expand Down
60 changes: 60 additions & 0 deletions source/core/inc/tactile/core/document/map_view_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include "tactile/base/document/map_view.hpp"
#include "tactile/base/prelude.hpp"

namespace tactile {

struct CMap;
struct CMapIdCache;
class MapDocument;

/**
* A map view implementation.
*/
class MapViewImpl final : public IMapView
{
public:
/**
* Creates a view of a map.
*
* \param document The associated map document.
*/
MapViewImpl(const MapDocument* document);

void accept(IDocumentVisitor& visitor) const override;

[[nodiscard]]
auto get_tile_size() const -> Int2 override;

[[nodiscard]]
auto get_extent() const -> MatrixExtent override;

[[nodiscard]]
auto get_next_layer_id() const -> LayerID override;

[[nodiscard]]
auto get_next_object_id() const -> ObjectID override;

[[nodiscard]]
auto layer_count() const -> usize override;

[[nodiscard]]
auto tileset_count() const -> usize override;

[[nodiscard]]
auto component_count() const -> usize override;

private:
const MapDocument* mDocument;

[[nodiscard]]
auto _get_map() const -> const CMap&;

[[nodiscard]]
auto _get_id_cache() const -> const CMapIdCache&;
};

} // namespace tactile
4 changes: 2 additions & 2 deletions source/core/inc/tactile/core/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ struct CMapIdCache final
TileID next_tile_id;

/** The next available object identifier. */
int32 next_object_id;
ObjectID next_object_id;

/** The next available layer identifier. */
int32 next_layer_id;
LayerID next_layer_id;
};

/**
Expand Down
94 changes: 94 additions & 0 deletions source/core/src/tactile/core/document/map_view_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#include "tactile/core/document/map_view_impl.hpp"

#include "tactile/base/document/document_visitor.hpp"
#include "tactile/core/debug/validation.hpp"
#include "tactile/core/document/layer_view_impl.hpp"
#include "tactile/core/document/map_document.hpp"
#include "tactile/core/document/tileset_view_impl.hpp"
#include "tactile/core/entity/registry.hpp"
#include "tactile/core/layer/group_layer.hpp"
#include "tactile/core/map/map.hpp"

namespace tactile {

MapViewImpl::MapViewImpl(const MapDocument* document)
: mDocument {require_not_null(document, "null document")}
{}

void MapViewImpl::accept(IDocumentVisitor& visitor) const
{
const auto& registry = mDocument->get_registry();

const auto& map = _get_map();
const auto& root_layer = registry.get<CGroupLayer>(map.root_layer);

visitor.visit(*this);

for (const auto tileset_id : map.attached_tilesets) {
const TilesetViewImpl tileset_view {mDocument, tileset_id};
tileset_view.accept(visitor);
}

for (const auto layer_id : root_layer.layers) {
const LayerViewImpl layer_view {mDocument, nullptr, layer_id};
layer_view.accept(visitor);
}

// TODO iterate component definitions
}

auto MapViewImpl::get_tile_size() const -> Int2
{
return _get_map().tile_size;
}

auto MapViewImpl::get_extent() const -> MatrixExtent
{
return _get_map().extent;
}

auto MapViewImpl::get_next_layer_id() const -> LayerID
{
return _get_id_cache().next_layer_id;
}

auto MapViewImpl::get_next_object_id() const -> ObjectID
{
return _get_id_cache().next_object_id;
}

auto MapViewImpl::layer_count() const -> usize
{
const auto& registry = mDocument->get_registry();
const auto& map = _get_map();

return count_layers(registry, map.root_layer);
}

auto MapViewImpl::tileset_count() const -> usize
{
return _get_map().attached_tilesets.size();
}

auto MapViewImpl::component_count() const -> usize
{
return 0; // TODO
}

auto MapViewImpl::_get_map() const -> const CMap&
{
const auto& registry = mDocument->get_registry();
const auto map_id = mDocument->get_root_entity();
return registry.get<CMap>(map_id);
}

auto MapViewImpl::_get_id_cache() const -> const CMapIdCache&
{
const auto& registry = mDocument->get_registry();
const auto map_id = mDocument->get_root_entity();
return registry.get<CMapIdCache>(map_id);
}

} // namespace tactile

0 comments on commit 42c5f70

Please sign in to comment.