-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComponentSprite.cpp
More file actions
113 lines (92 loc) · 2.51 KB
/
ComponentSprite.cpp
File metadata and controls
113 lines (92 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "ComponentSprite.h"
#include "imgui\imgui.h"
#include "ResourceFileTexture.h"
#include "Application.h"
#include "Assets.h"
#include "GameObject.h"
#include "ModuleEditor.h"
#include "ModuleRenderer3D.h"
#include "DebugDraw.h"
ComponentSprite::ComponentSprite(ComponentType type, GameObject* game_object) : Component(type, game_object)
{
aabb.SetNegativeInfinity();
bounding_box.SetNegativeInfinity();
}
ComponentSprite::~ComponentSprite()
{}
void ComponentSprite::Update()
{
if(texture && active)
App->renderer3D->AddToDrawSprite(this);
}
void ComponentSprite::OnInspector(bool debug)
{
if (ImGui::CollapsingHeader("Sprite", ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::IsItemClicked(1))
ImGui::OpenPopup("delete##sprite");
if (ImGui::BeginPopup("delete##sprite"))
{
if (ImGui::MenuItem("Delete"))
Remove();
ImGui::EndPopup();
}
//Show texture
if (texture)
{
ImGui::Image((ImTextureID)texture->GetTexture(), ImVec2(50, 50));
ImGui::Text("Width: %i Height: %i", width, height);
}
//Change texture
ImGui::Text("Change sprite: ");
if (ImGui::BeginMenu("###sprite_change_tex"))
{
ChangeTexture();
ImGui::EndMenu();
}
}
}
void ComponentSprite::OnTransformModified()
{
math::OBB ob = aabb.Transform(game_object->GetGlobalMatrix());
bounding_box = ob.MinimalEnclosingAABB();
game_object->bounding_box = &bounding_box;
}
unsigned int ComponentSprite::GetTextureId() const
{
if (texture)
return texture->GetTexture();
return 0;
}
void ComponentSprite::ChangeTexture()
{
vector<string> textures;
App->editor->assets->GetAllFilesByType(FileType::IMAGE, textures);
for (vector<string>::iterator it = textures.begin(); it != textures.end(); ++it)
{
if (ImGui::MenuItem((*it).data()))
{
string u_sampler2d = App->resource_manager->FindFile(*it);
ResourceFileTexture* rc_tmp = (ResourceFileTexture*)App->resource_manager->LoadResource(u_sampler2d, ResourceFileType::RES_TEXTURE);
if (rc_tmp)
{
if (texture)
texture->Unload();
texture = rc_tmp;
width = texture->GetWidth();
height = texture->GetHeight();
size.x = width / 100.0f;
size.y = height / 100.0f;
float max_size = (width > height) ? width : height;
max_size = (max_size / 100.0f) * 0.5f;
aabb = math::AABB(math::vec(-max_size), math::vec(max_size));
OnTransformModified();
}
else
{
LOG("[ERROR] Loading failure on sprite %s", (*it).data());
App->editor->DisplayWarning(WarningType::W_ERROR, "Loading failure on sprite %s", (*it).data());
}
}
}
}