Objects have names now

minor

minor
This commit is contained in:
Nomango 2019-02-07 23:54:19 +08:00 committed by Nomango
parent d3f8c7afef
commit 55f28806c9
20 changed files with 244 additions and 150 deletions

View File

@ -52,6 +52,17 @@ namespace easy2d
}
}
ActionPtr ActionManager::GetAction(String const & name)
{
if (actions_.IsEmpty())
return nullptr;
for (auto action = actions_.First().Get(); action; action = action->NextItem().Get())
if (action->IsName(name))
return action;
return nullptr;
}
void ActionManager::ResumeAllActions()
{
if (actions_.IsEmpty())

View File

@ -33,6 +33,11 @@ namespace easy2d
ActionPtr const& action
);
// 获取动作
ActionPtr GetAction(
String const& name
);
// 继续所有暂停动作
void ResumeAllActions();

View File

@ -43,11 +43,11 @@ namespace easy2d
inline Array() { size_ = capacity_ = 0; data_ = nullptr; }
inline Array(std::initializer_list<_Ty> const& list) { size_ = capacity_ = 0; data_ = nullptr; operator=(list); }
inline Array(const Array<_Ty>& src) { size_ = capacity_ = 0; data_ = nullptr; operator=(src); }
inline Array(Array<_Ty>&& src) { size_ = capacity_ = 0; data_ = nullptr; operator=(src); }
inline Array(Array<_Ty>&& src) { size_ = capacity_ = 0; data_ = nullptr; operator=(std::forward<Array<_Ty>&&>(src)); }
inline ~Array() { if (data_) _destroy_all(); }
inline Array<_Ty>& operator=(const Array<_Ty>& src) { if (&src == this) { return *this; } clear(); reserve(src.size_); std::uninitialized_copy(src.begin(), src.end(), begin()); size_ = src.size_; return *this; }
inline Array<_Ty>& operator=(Array<_Ty>&& src) { clear(); size_ = src.size_; capacity_ = src.capacity_; data_ = src.data_; src.data_ = src.size_ = src.capacity_ = 0; return *this; }
inline Array<_Ty>& operator=(Array<_Ty>&& src) { clear(); size_ = src.size_; capacity_ = src.capacity_; data_ = src.data_; src.size_ = src.capacity_ = 0; src.data_ = nullptr; return *this; }
inline Array<_Ty>& operator=(std::initializer_list<_Ty> const& list) { clear(); reserve((int)list.size()); std::uninitialized_copy(list.begin(), list.end(), begin()); size_ = (int)list.size(); return *this; }
inline bool empty() const { return size_ == 0; }

View File

@ -63,7 +63,7 @@ namespace easy2d
{
for (auto listener = listeners_.First(); listener; listener = listener->NextItem())
{
if (listener->GetName() == listener_name)
if (listener->IsName(listener_name))
{
listener->Start();
}
@ -74,7 +74,7 @@ namespace easy2d
{
for (auto listener = listeners_.First(); listener; listener = listener->NextItem())
{
if (listener->GetName() == listener_name)
if (listener->IsName(listener_name))
{
listener->Stop();
}
@ -88,7 +88,7 @@ namespace easy2d
{
next = listener->NextItem();
if (listener->GetName() == listener_name)
if (listener->IsName(listener_name))
{
listeners_.Remove(listener);
}

View File

@ -26,9 +26,9 @@ namespace easy2d
EventListener::EventListener(EventType type, EventCallback const & callback, String const & name)
: type_(type)
, callback_(callback)
, name_(name)
, running_(true)
{
SetName(name);
}
EventListener::~EventListener()
@ -50,14 +50,4 @@ namespace easy2d
return running_;
}
String const & EventListener::GetName() const
{
return name_;
}
void EventListener::SetName(String const & name)
{
name_ = name;
}
}

View File

@ -49,15 +49,10 @@ namespace easy2d
void Stop();
void SetName(String const& name);
bool IsRunning() const;
String const& GetName() const;
protected:
bool running_;
String name_;
EventType type_;
EventCallback callback_;
};

View File

@ -60,7 +60,7 @@ namespace easy2d
HRESULT hr = S_OK;
D2DBitmapPtr bitmap;
if (res.IsFile())
if (res.IsFileType())
{
if (!File(res.GetFileName()).Exists())
{

View File

@ -58,7 +58,7 @@ namespace easy2d
HRESULT hr = S_OK;
Transcoder transcoder;
if (res.IsFile())
if (res.IsFileType())
{
if (!File(res.GetFileName()).Exists())
{

View File

@ -364,9 +364,9 @@ namespace easy2d
void Node::SetName(String const& name)
{
if (name_ != name)
if (!IsName(name))
{
name_ = name;
Object::SetName(name);
hash_name_ = std::hash<String>{}(name);
}
}
@ -511,7 +511,7 @@ namespace easy2d
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
{
if (child->hash_name_ == hash_code && child->name_ == name)
if (child->hash_name_ == hash_code && child->IsName(name))
{
children.push_back(child);
}
@ -525,7 +525,7 @@ namespace easy2d
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
{
if (child->hash_name_ == hash_code && child->name_ == name)
if (child->hash_name_ == hash_code && child->IsName(name))
{
return child;
}
@ -582,7 +582,7 @@ namespace easy2d
{
next = child->NextItem().Get();
if (child->hash_name_ == hash_code && child->name_ == child_name)
if (child->hash_name_ == hash_code && child->IsName(child_name))
{
RemoveChild(child);
}

View File

@ -61,9 +61,6 @@ namespace easy2d
// 获取显示状态
bool IsVisible() const { return visible_; }
// »ñÈ¡Ãû³Æ
String const& GetName() const { return name_; }
// 获取名称的 Hash 值
size_t GetHashName() const { return hash_name_; }
@ -377,7 +374,6 @@ namespace easy2d
int z_order_;
float opacity_;
float display_opacity_;
String name_;
size_t hash_name_;
Transform transform_;
Point anchor_;

View File

@ -31,6 +31,7 @@ namespace easy2d
Object::Object()
: tracing_leak_(false)
, user_data_(nullptr)
, name_(nullptr)
{
#ifdef E2D_DEBUG
@ -41,6 +42,9 @@ namespace easy2d
Object::~Object()
{
if (name_)
delete name_;
#ifdef E2D_DEBUG
Object::__RemoveObjectFromTracingList(this);
@ -58,6 +62,27 @@ namespace easy2d
user_data_ = data;
}
void Object::SetName(String const & name)
{
if (IsName(name))
return;
if (name.empty())
{
if (name_)
name_->clear();
return;
}
if (!name_)
{
name_ = new (std::nothrow) String(name);
return;
}
*name_ = name;
}
void Object::StartTracingLeaks()
{
tracing_leaks = true;

View File

@ -36,6 +36,12 @@ namespace easy2d
void SetUserData(void* data);
void SetName(String const& name);
inline String GetName() const { if (name_) return *name_; return String(); }
inline bool IsName(String const& name) const { return (name_ && (*name_ == name)); }
static void StartTracingLeaks();
static void StopTracingLeaks();
@ -50,5 +56,6 @@ namespace easy2d
private:
bool tracing_leak_;
void* user_data_;
String* name_;
};
}

View File

@ -25,8 +25,18 @@ namespace easy2d
{
Resource::Resource(LPCWSTR file_name)
: type_(Type::File)
, file_name_(file_name)
, file_name_(nullptr)
{
if (file_name)
file_name_ = new (std::nothrow) String(file_name);
}
Resource::Resource(String const& file_name)
: type_(Type::File)
, file_name_(nullptr)
{
if (!file_name.empty())
file_name_ = new (std::nothrow) String(file_name);
}
Resource::Resource(LPCWSTR name, LPCWSTR type)
@ -36,17 +46,85 @@ namespace easy2d
{
}
Resource::Resource(Resource const & rhs)
{
operator=(rhs);
}
Resource::Resource(Resource && rhs)
{
operator=(std::forward<Resource&&>(rhs));
}
Resource::~Resource()
{
if (IsFileType() && file_name_)
delete file_name_;
}
size_t Resource::GetHashCode() const
{
if (type_ == Type::File)
return std::hash<LPCWSTR>{}(file_name_);
return std::hash<String>{}(GetFileName());
return std::hash<LPCWSTR>{}(bin_name_);
}
Resource & Resource::operator=(Resource const & rhs)
{
if (&rhs != this)
{
if (IsFileType() && file_name_)
{
delete file_name_;
file_name_ = nullptr;
}
type_ = rhs.type_;
if (IsFileType())
{
if (rhs.file_name_)
{
file_name_ = new (std::nothrow) String(*rhs.file_name_);
}
}
else
{
bin_name_ = rhs.bin_name_;
bin_type_ = rhs.bin_type_;
}
}
return *this;
}
Resource & Resource::operator=(Resource && rhs)
{
if (IsFileType() && file_name_)
{
delete file_name_;
file_name_ = nullptr;
}
type_ = rhs.type_;
if (IsFileType())
{
file_name_ = rhs.file_name_;
rhs.file_name_ = nullptr;
}
else
{
bin_name_ = rhs.bin_name_;
bin_type_ = rhs.bin_type_;
}
return *this;
}
bool Resource::Load(LPVOID& buffer, DWORD& buffer_size) const
{
if (type_ != Type::Binary)
{
E2D_ERROR_LOG(L"Only binary resource can be loaded");
return false;
}
HGLOBAL res_data;
HRSRC res_info;

View File

@ -20,7 +20,6 @@
#pragma once
#include "helper.hpp"
#include "Frames.h"
namespace easy2d
{
@ -34,23 +33,35 @@ namespace easy2d
//
class Resource
{
public:
enum class Type { File, Binary };
public:
Resource(
LPCWSTR file_name /* 文件路径 */
LPCWSTR file_name /* 文件路径 */
);
Resource(
LPCWSTR name, /* 资源名称 */
LPCWSTR type /* 资源类型 */
String const& file_name /* 文件路径 */
);
inline bool IsFile() const { return type_ == Type::File; }
Resource(
LPCWSTR name, /* 资源名称 */
LPCWSTR type /* 资源类型 */
);
inline Type GetType() const { return type_; }
Resource(
Resource const& rhs
);
inline LPCWSTR GetFileName() const { return file_name_; }
Resource(
Resource && rhs
);
virtual ~Resource();
inline bool IsFileType() const { return type_ == Type::File; }
inline String GetFileName() const { if (file_name_) return *file_name_; return String(); }
bool Load(
LPVOID& buffer,
@ -59,13 +70,17 @@ namespace easy2d
size_t GetHashCode() const;
Resource& operator= (Resource const& rhs);
Resource& operator= (Resource && rhs);
private:
Type type_;
union
{
struct
{
LPCWSTR file_name_;
String* file_name_;
};
struct

View File

@ -33,9 +33,9 @@ namespace easy2d
, total_times_(times)
, delay_(delay)
, callback_(func)
, name_(name)
, delta_()
{
SetName(name);
}
void Task::Start()
@ -91,9 +91,4 @@ namespace easy2d
return running_;
}
String const& Task::GetName() const
{
return name_;
}
}

View File

@ -60,21 +60,17 @@ namespace easy2d
// 任务是否正在执行
bool IsRunning() const;
// »ñÈ¡ÈÎÎñÃû³Æ
String const& GetName() const;
protected:
void Update(Duration dt, bool& remove_after_update);
void Reset();
protected:
bool running_;
int run_times_;
int total_times_;
String name_;
Duration delay_;
Duration delta_;
Callback callback_;
bool running_;
int run_times_;
int total_times_;
Duration delay_;
Duration delta_;
Callback callback_;
};
}

View File

@ -59,7 +59,7 @@ namespace easy2d
for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get())
{
if (task->GetName() == name)
if (task->IsName(name))
{
task->Stop();
}
@ -73,7 +73,7 @@ namespace easy2d
for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get())
{
if (task->GetName() == name)
if (task->IsName(name))
{
task->Start();
}
@ -89,7 +89,7 @@ namespace easy2d
for (auto task = tasks_.First(); task; task = next)
{
next = task->NextItem();
if (task->GetName() == name)
if (task->IsName(name))
{
tasks_.Remove(task);
}

View File

@ -28,16 +28,20 @@ namespace easy2d
{
namespace
{
String Search(LPCWSTR file_name, List<String> const& paths)
Resource LocateRes(Resource const& res, List<String> const& paths)
{
for (const auto& path : paths)
if (res.IsFileType())
{
if (modules::Shlwapi::Get().PathFileExistsW((path + file_name).c_str()))
String file_name = res.GetFileName();
for (const auto& path : paths)
{
return path + file_name;
if (modules::Shlwapi::Get().PathFileExistsW((path + file_name).c_str()))
{
return Resource{ path + file_name };
}
}
}
return file_name;
return res;
}
}
@ -46,15 +50,7 @@ namespace easy2d
ImagePtr ptr = new (std::nothrow) Image;
if (ptr)
{
if (image.IsFile())
{
String path = Search(image.GetFileName(), search_paths_);
ptr->Load(path.c_str());
}
else
ptr->Load(image);
if (ptr->IsValid())
if (ptr->Load(LocateRes(image, search_paths_)))
{
res_.insert(std::make_pair(id, ptr));
return true;
@ -78,38 +74,28 @@ namespace easy2d
if (images.empty())
return 0;
int total = 0;
Array<ImagePtr> image_arr;
image_arr.reserve(images.size());
for (const auto& image : images)
{
ImagePtr ptr = new (std::nothrow) Image;
if (ptr)
{
if (image.IsFile())
{
String path = Search(image.GetFileName(), search_paths_);
ptr->Load(path.c_str());
}
else
ptr->Load(image);
if (ptr->IsValid())
if (ptr->Load(LocateRes(image, search_paths_)))
{
image_arr.push_back(ptr);
++total;
}
}
}
if (total)
if (!image_arr.empty())
{
FramesPtr frames = new (std::nothrow) Frames(image_arr);
if (frames)
{
res_.insert(std::make_pair(id, frames));
return total;
return frames->GetFrames().size();
}
}
return 0;
@ -134,62 +120,66 @@ namespace easy2d
if (cols <= 0 || rows <= 0)
return 0;
int total = 0;
FramesPtr frames = new (std::nothrow) Frames;
if (frames)
ImagePtr raw = new (std::nothrow) Image;
if (!raw || !raw->Load(LocateRes(image, search_paths_)))
return 0;
float raw_width = raw->GetSourceWidth();
float raw_height = raw->GetSourceHeight();
float width = raw_width / cols;
float height = raw_height / rows;
Array<ImagePtr> image_arr;
image_arr.reserve(rows * cols);
for (int i = 0; i < rows; i++)
{
ImagePtr raw = new (std::nothrow) Image;
if (!raw || !raw->Load(image))
return 0;
float raw_width = raw->GetSourceWidth();
float raw_height = raw->GetSourceHeight();
float width = raw_width / cols;
float height = raw_height / rows;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
if (ptr)
{
ptr->Crop(Rect{ i * width, j * height, width, height });
frames->Add(ptr);
++total;
}
}
}
}
if (total)
res_.insert(std::make_pair(id, frames));
return total;
}
int ResLoader::AddFrames(String const & id, Resource const & image, Array<Rect> const & crop_rects)
{
int total = 0;
FramesPtr frames = new (std::nothrow) Frames;
if (frames)
{
ImagePtr raw = new (std::nothrow) Image;
if (!raw || !raw->Load(image))
return 0;
for (const auto& rect : crop_rects)
for (int j = 0; j < cols; j++)
{
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
if (ptr)
{
ptr->Crop(rect);
frames->Add(ptr);
++total;
ptr->Crop(Rect{ i * width, j * height, width, height });
image_arr.push_back(ptr);
}
}
}
if (total)
FramesPtr frames = new (std::nothrow) Frames(image_arr);
if (frames)
{
res_.insert(std::make_pair(id, frames));
return total;
return frames->GetFrames().size();
}
return 0;
}
int ResLoader::AddFrames(String const & id, Resource const & image, Array<Rect> const & crop_rects)
{
ImagePtr raw = new (std::nothrow) Image;
if (!raw || !raw->Load(LocateRes(image, search_paths_)))
return 0;
Array<ImagePtr> image_arr;
image_arr.reserve(crop_rects.size());
for (const auto& rect : crop_rects)
{
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
if (ptr)
{
ptr->Crop(rect);
image_arr.push_back(ptr);
}
}
FramesPtr frames = new (std::nothrow) Frames(image_arr);
if (frames)
{
res_.insert(std::make_pair(id, frames));
return frames->GetFrames().size();
}
return 0;
}
bool ResLoader::AddFrames(String const & id, FramesPtr const & frames)
@ -207,16 +197,7 @@ namespace easy2d
MusicPtr ptr = new (std::nothrow) Music;
if (ptr)
{
bool valid = false;
if (music.IsFile())
{
String path = Search(music.GetFileName(), search_paths_);
valid = ptr->Load(path.c_str());
}
else
valid = ptr->Load(music);
if (valid)
if (ptr->Load(LocateRes(music, search_paths_)))
{
res_.insert(std::make_pair(id, ptr));
return true;

View File

@ -49,14 +49,14 @@ namespace easy2d
return wave_format_;
}
HRESULT Transcoder::LoadMediaFile(LPCWSTR file_path, BYTE** wave_data, UINT32* wave_data_size)
HRESULT Transcoder::LoadMediaFile(String const& file_path, BYTE** wave_data, UINT32* wave_data_size)
{
HRESULT hr = S_OK;
IntrusivePtr<IMFSourceReader> reader;
hr = modules::MediaFoundation::Get().MFCreateSourceReaderFromURL(
file_path,
file_path.c_str(),
nullptr,
&reader
);

View File

@ -39,7 +39,7 @@ namespace easy2d
const WAVEFORMATEX* GetWaveFormatEx() const;
HRESULT LoadMediaFile(
LPCWSTR file_path,
String const& file_path,
BYTE** wave_data,
UINT32* wave_data_size
);