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-07-22 21:08:48 +08:00
|
|
|
Sprite::Sprite(FramePtr frame)
|
2020-02-06 16:54:47 +08:00
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
SetFrame(frame);
|
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-01-21 10:09:55 +08:00
|
|
|
Sprite::~Sprite() {}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
bool Sprite::Load(const String& file_path, bool autoresize)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
FramePtr frame = MakePtr<Frame>(file_path);
|
2020-07-26 00:22:32 +08:00
|
|
|
if (frame && frame->IsValid())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-11 22:56:12 +08:00
|
|
|
SetFrame(frame, autoresize);
|
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-02-19 12:09:50 +08:00
|
|
|
bool Sprite::Load(const Resource& res, bool autoresize)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-07-22 21:08:48 +08:00
|
|
|
FramePtr frame = MakePtr<Frame>(res);
|
2020-02-11 22:56:12 +08:00
|
|
|
if (frame)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-11 22:56:12 +08:00
|
|
|
SetFrame(frame, autoresize);
|
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-02-13 12:05:20 +08:00
|
|
|
float Sprite::GetSourceWidth() const
|
|
|
|
|
{
|
|
|
|
|
if (frame_)
|
|
|
|
|
{
|
|
|
|
|
return frame_->GetSourceWidth();
|
|
|
|
|
}
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Sprite::GetSourceHeight() const
|
|
|
|
|
{
|
|
|
|
|
if (frame_)
|
|
|
|
|
{
|
|
|
|
|
return frame_->GetSourceHeight();
|
|
|
|
|
}
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Size Sprite::GetSourceSize() const
|
|
|
|
|
{
|
|
|
|
|
if (frame_)
|
|
|
|
|
{
|
|
|
|
|
return frame_->GetSourceSize();
|
|
|
|
|
}
|
|
|
|
|
return Size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rect Sprite::GetCropRect() const
|
|
|
|
|
{
|
|
|
|
|
if (frame_)
|
|
|
|
|
{
|
|
|
|
|
return frame_->GetCropRect();
|
|
|
|
|
}
|
|
|
|
|
return Rect();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
void Sprite::SetCropRect(const Rect& crop_rect)
|
|
|
|
|
{
|
|
|
|
|
if (frame_)
|
|
|
|
|
{
|
|
|
|
|
frame_->SetCropRect(crop_rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-02-11 22:56:12 +08:00
|
|
|
void Sprite::SetFrame(FramePtr frame, bool autoresize)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (frame_ != frame)
|
|
|
|
|
{
|
|
|
|
|
frame_ = frame;
|
2020-10-08 01:22:10 +08:00
|
|
|
if (autoresize)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-10-08 01:22:10 +08:00
|
|
|
ResetSize();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-10-08 01:22:10 +08:00
|
|
|
void Sprite::ResetSize()
|
|
|
|
|
{
|
|
|
|
|
if (frame_)
|
|
|
|
|
{
|
|
|
|
|
SetSize(frame_->GetSize());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetSize(Size());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
void Sprite::OnRender(RenderContext& ctx)
|
|
|
|
|
{
|
2020-02-20 18:22:52 +08:00
|
|
|
if (frame_ && frame_->IsValid())
|
|
|
|
|
{
|
|
|
|
|
ctx.DrawTexture(*frame_->GetTexture(), &frame_->GetCropRect(), &GetBounds());
|
|
|
|
|
}
|
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
|
|
|
|
|
{
|
|
|
|
|
return frame_ && frame_->IsValid() && Actor::CheckVisibility(ctx);
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
} // namespace kiwano
|