| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "Node.h"
- #include "ResourceCache.h"
- #include "TileMap2D.h"
- #include "TileMapLayer2D.h"
- #include "TmxFile2D.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- extern const float PIXEL_SIZE;
- extern const char* URHO2D_CATEGORY;
- TileMap2D::TileMap2D(Context* context) :
- Component(context)
- {
- }
- TileMap2D::~TileMap2D()
- {
- }
- void TileMap2D::RegisterObject(Context* context)
- {
- context->RegisterFactory<TileMap2D>(URHO2D_CATEGORY);
- ACCESSOR_ATTRIBUTE(TileMap2D, VAR_RESOURCEREF, "Tmx File", GetTmxFileAttr, SetTmxFileAttr, ResourceRef, ResourceRef(TmxFile2D::GetTypeStatic()), AM_DEFAULT);
- }
- void TileMap2D::SetTmxFile(TmxFile2D* tmxFile)
- {
- if (tmxFile == tmxFile_)
- return;
- if (tmxFile_)
- {
- for (unsigned i = 0; i < layers_.Size(); ++i)
- {
- if (layers_[i])
- layers_[i]->GetNode()->Remove();
- }
- layers_.Clear();
- }
- tmxFile_ = tmxFile;
-
- if (!tmxFile_)
- return;
- unsigned numLayers = tmxFile_->GetNumLayers();
- layers_.Resize(numLayers);
- for (unsigned i = 0; i < numLayers; ++i)
- {
- const TmxLayer2D* tmxLayer = tmxFile_->GetLayer(i);
- SharedPtr<Node> layerNode(GetNode()->CreateChild(tmxLayer->name_, LOCAL));
- layerNode->SetTemporary(true);
- SharedPtr<TileMapLayer2D> layer(layerNode->CreateComponent<TileMapLayer2D>());
- layer->SetTmxLayer(tmxLayer);
- layer->SetDrawOrder(i);
- layers_[i] = layer;
- }
- }
- TmxFile2D* TileMap2D::GetTmxFile() const
- {
- return tmxFile_;
- }
- int TileMap2D::GetWidth() const
- {
- return tmxFile_ ? tmxFile_->GetWidth() : 0;
- }
- int TileMap2D::GetHeight() const
- {
- return tmxFile_ ? tmxFile_->GetHeight() : 0;
- }
- float TileMap2D::GetTileWidth() const
- {
- return tmxFile_ ? tmxFile_->GetTileWidth() : 0.0f;
- }
- float TileMap2D::GetTileHeight() const
- {
- return tmxFile_ ? tmxFile_->GetTileHeight() : 0.0f;
- }
- TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
- {
- if (index >= layers_.Size())
- return 0;
- return layers_[index];
- }
- TileMapLayer2D* TileMap2D::GetLayer(const String& name) const
- {
- for (unsigned i = 0; i < layers_.Size(); ++i)
- if (layers_[i] && name == layers_[i]->GetName())
- return layers_[i];
- return 0;
- }
- void TileMap2D::SetTmxFileAttr(ResourceRef value)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- SetTmxFile(cache->GetResource<TmxFile2D>(value.name_));
- }
- ResourceRef TileMap2D::GetTmxFileAttr() const
- {
- return GetResourceRef(tmxFile_, TmxFile2D::GetTypeStatic());
- }
- }
|