2019-04-11 14:40:54 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// 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:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// 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.
|
|
|
|
|
|
|
2019-10-11 21:55:29 +08:00
|
|
|
|
#include <kiwano/2d/Sprite.h>
|
2020-08-03 21:55:27 +08:00
|
|
|
|
#include <kiwano/render/RenderContext.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
2020-02-06 16:54:47 +08:00
|
|
|
|
|
2020-07-22 21:08:48 +08:00
|
|
|
|
Sprite::Sprite() {}
|
|
|
|
|
|
|
|
|
|
|
|
Sprite::Sprite(const String& file_path)
|
2020-02-06 16:54:47 +08:00
|
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
|
Load(file_path);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 21:08:48 +08:00
|
|
|
|
Sprite::Sprite(const Resource& res)
|
2020-02-06 16:54:47 +08:00
|
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
|
Load(res);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-10 21:12:37 +08:00
|
|
|
|
Sprite::Sprite(TexturePtr texture)
|
2020-02-06 16:54:47 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
SetTexture(texture);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 21:08:48 +08:00
|
|
|
|
Sprite::Sprite(const String& file_path, const Rect& crop_rect)
|
|
|
|
|
|
: Sprite(file_path)
|
2020-02-11 22:56:12 +08:00
|
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
|
SetCropRect(crop_rect);
|
2020-02-11 22:56:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 21:08:48 +08:00
|
|
|
|
Sprite::Sprite(const Resource& res, const Rect& crop_rect)
|
|
|
|
|
|
: Sprite(res)
|
2020-02-11 22:56:12 +08:00
|
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
|
SetCropRect(crop_rect);
|
2020-02-11 22:56:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-10 21:12:37 +08:00
|
|
|
|
Sprite::Sprite(TexturePtr texture, const Rect& crop_rect)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetTexture(texture);
|
|
|
|
|
|
SetCropRect(crop_rect);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Sprite::~Sprite() {}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-10-10 21:12:37 +08:00
|
|
|
|
bool Sprite::Load(const String& file_path)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
TexturePtr texture = MakePtr<Texture>(file_path);
|
|
|
|
|
|
if (texture && texture->IsValid())
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
SetTexture(texture);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-07-26 00:22:32 +08:00
|
|
|
|
Fail("Sprite::Load failed");
|
2020-01-21 10:09:55 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2019-08-18 17:49:13 +08:00
|
|
|
|
|
2020-10-10 21:12:37 +08:00
|
|
|
|
bool Sprite::Load(const Resource& res)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
TexturePtr texture = MakePtr<Texture>(res);
|
|
|
|
|
|
if (texture && texture->IsValid())
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
SetTexture(texture);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-07-26 00:22:32 +08:00
|
|
|
|
Fail("Sprite::Load failed");
|
2020-01-21 10:09:55 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-10-10 21:12:37 +08:00
|
|
|
|
void Sprite::SetTexture(TexturePtr texture)
|
2020-02-13 12:05:20 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
texture_ = texture;
|
2020-02-13 12:05:20 +08:00
|
|
|
|
|
2020-10-10 21:12:37 +08:00
|
|
|
|
Size texture_size;
|
|
|
|
|
|
if (texture_)
|
2020-10-08 01:22:10 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
texture_size = texture_->GetSize();
|
2020-10-08 01:22:10 +08:00
|
|
|
|
}
|
2020-10-10 21:12:37 +08:00
|
|
|
|
// <20><><EFBFBD>òü<C3B2><C3BC><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
SetCropRect(Rect(Point(), texture_size));
|
2020-10-08 01:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Sprite::OnRender(RenderContext& ctx)
|
|
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
if (texture_ && texture_->IsValid())
|
2020-02-20 18:22:52 +08:00
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
ctx.DrawTexture(*texture_, &crop_rect_, &GetBounds());
|
2020-02-20 18:22:52 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
2019-08-14 21:38:37 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool Sprite::CheckVisibility(RenderContext& ctx) const
|
|
|
|
|
|
{
|
2020-10-10 21:12:37 +08:00
|
|
|
|
return texture_ && texture_->IsValid() && Actor::CheckVisibility(ctx);
|
2019-08-13 14:44:37 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
} // namespace kiwano
|