-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef685ac
commit a337144
Showing
8 changed files
with
400 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
source/core/inc/tactile/core/cmd/layer/create_layer_command.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0) | ||
|
||
#pragma once | ||
|
||
#include "tactile/base/layer/layer_type.hpp" | ||
#include "tactile/base/prelude.hpp" | ||
#include "tactile/core/cmd/command.hpp" | ||
#include "tactile/core/entity/entity.hpp" | ||
|
||
namespace tactile { | ||
|
||
class MapDocument; | ||
|
||
/** | ||
* A command for creating new map layers. | ||
*/ | ||
class CreateLayerCommand final : public ICommand | ||
{ | ||
public: | ||
TACTILE_DELETE_COPY(CreateLayerCommand); | ||
TACTILE_DEFAULT_MOVE(CreateLayerCommand); | ||
|
||
/** | ||
* Creates a command. | ||
* | ||
* \param document The parent map document. | ||
* \param type The type of the new layer. | ||
*/ | ||
CreateLayerCommand(MapDocument* document, LayerType type); | ||
|
||
~CreateLayerCommand() noexcept override; | ||
|
||
void undo() override; | ||
|
||
void redo() override; | ||
|
||
private: | ||
MapDocument* mDocument; | ||
LayerType mType; | ||
EntityID mParentLayerId; | ||
EntityID mLayerId; | ||
bool mWasLayerAdded; | ||
}; | ||
|
||
} // namespace tactile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
source/core/src/tactile/core/cmd/layer/create_layer_command.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0) | ||
|
||
#include "tactile/core/cmd/layer/create_layer_command.hpp" | ||
|
||
#include "tactile/core/debug/validation.hpp" | ||
#include "tactile/core/document/document_info.hpp" | ||
#include "tactile/core/document/map_document.hpp" | ||
#include "tactile/core/entity/registry.hpp" | ||
#include "tactile/core/layer/group_layer.hpp" | ||
#include "tactile/core/layer/layer.hpp" | ||
#include "tactile/core/layer/object_layer.hpp" | ||
#include "tactile/core/layer/tile_layer.hpp" | ||
#include "tactile/core/log/logger.hpp" | ||
#include "tactile/core/map/map.hpp" | ||
|
||
namespace tactile { | ||
|
||
CreateLayerCommand::CreateLayerCommand(MapDocument* document, const LayerType type) | ||
: mDocument {require_not_null(document, "null document")}, | ||
mType {type}, | ||
mParentLayerId {kInvalidEntity}, | ||
mLayerId {kInvalidEntity}, | ||
mWasLayerAdded {false} | ||
{} | ||
|
||
CreateLayerCommand::~CreateLayerCommand() noexcept | ||
{ | ||
try { | ||
if (!mWasLayerAdded && mLayerId != kInvalidEntity) { | ||
auto& registry = mDocument->get_registry(); | ||
switch (mType) { | ||
case LayerType::kTileLayer: destroy_tile_layer(registry, mLayerId); break; | ||
case LayerType::kObjectLayer: destroy_object_layer(registry, mLayerId); break; | ||
case LayerType::kGroupLayer: destroy_group_layer(registry, mLayerId); break; | ||
} | ||
} | ||
} | ||
catch (const std::exception& error) { | ||
TACTILE_LOG_ERROR("CreateLayerCommand threw unexpected exception: {}", error.what()); | ||
} | ||
} | ||
|
||
void CreateLayerCommand::undo() | ||
{ | ||
TACTILE_LOG_DEBUG("Removing layer {}", entity_to_string(mLayerId)); | ||
|
||
auto& registry = mDocument->get_registry(); | ||
|
||
const auto& document_info = registry.get<CDocumentInfo>(); | ||
const auto map_id = document_info.root; | ||
|
||
remove_layer_from_map(registry, map_id, mLayerId).value(); | ||
|
||
mWasLayerAdded = false; | ||
} | ||
|
||
void CreateLayerCommand::redo() | ||
{ | ||
TACTILE_LOG_DEBUG("Creating layer..."); | ||
|
||
auto& registry = mDocument->get_registry(); | ||
|
||
const auto& document_info = registry.get<CDocumentInfo>(); | ||
const auto map_id = document_info.root; | ||
|
||
auto& map = registry.get<CMap>(map_id); | ||
|
||
if (mLayerId == kInvalidEntity) { | ||
mParentLayerId = map.active_layer; | ||
mLayerId = add_layer_to_map(registry, map_id, mType).value(); | ||
} | ||
else { | ||
const auto old_active_layer = map.active_layer; | ||
map.active_layer = mParentLayerId; | ||
|
||
append_layer_to_map(registry, map_id, mLayerId); | ||
|
||
map.active_layer = old_active_layer; | ||
} | ||
|
||
mWasLayerAdded = true; | ||
} | ||
|
||
} // namespace tactile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.