Drag and drop ImGui

This commit is contained in:
CatChow0 2024-04-04 10:50:40 +02:00
parent 6d2cb2531b
commit 4a3b540b6f
6 changed files with 128 additions and 16 deletions

View File

@ -220,10 +220,9 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam
m_initialWindowHeight = newHeight; m_initialWindowHeight = newHeight;
} }
} }
case WM_DROPFILES: case WM_DROPFILES:
{ {
HDROP hDrop = reinterpret_cast<HDROP>(wparam); HDROP hDrop = reinterpret_cast<HDROP>(wparam);
UINT numFiles = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0); UINT numFiles = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0);
if (numFiles > 0) { if (numFiles > 0) {
@ -233,13 +232,13 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam
WCHAR filePath[MAX_PATH]; WCHAR filePath[MAX_PATH];
DragQueryFile(hDrop, i, filePath, MAX_PATH); DragQueryFile(hDrop, i, filePath, MAX_PATH);
std::wcout << L"File dropped: " << filePath << std::endl; std::wcout << L"File dropped: " << filePath << std::endl;
m_Application->AddKobject(filePath);
} }
} }
DragFinish(hDrop); DragFinish(hDrop);
return 0; return 0;
} }
// Any other messages send to the default message handler as our application won't make use of them. // Any other messages send to the default message handler as our application won't make use of them.
default: default:
{ {

View File

@ -362,6 +362,13 @@ void ApplicationClass::Shutdown()
} }
m_terrainChunk.clear(); m_terrainChunk.clear();
for (auto object : m_object)
{
object->Shutdown();
delete object;
}
m_object.clear();
// Release the multitexture shader object. // Release the multitexture shader object.
if (m_MultiTextureShader) if (m_MultiTextureShader)
{ {
@ -559,10 +566,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
lightPosition[i] = m_Lights[i].GetPosition(); lightPosition[i] = m_Lights[i].GetPosition();
} }
// Render the model using the multitexture shader. // Render the model using the multitexture shader.
m_Model->Render(m_Direct3D->GetDeviceContext()); m_Model->Render(m_Direct3D->GetDeviceContext());
@ -626,6 +629,30 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
} }
} }
for (auto object : m_object)
{
scaleMatrix = object->GetScaleMatrix();
if (object->m_demoSpinning)
rotateMatrix = XMMatrixRotationY(rotation);
else
{
rotateMatrix = object->GetRotateMatrix();
}
translateMatrix = object->GetTranslateMatrix();
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
object->Render(m_Direct3D->GetDeviceContext());
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
diffuseColor, lightPosition);
if (!result)
{
return false;
}
}
// Render terrain // Render terrain
for (auto chunk : m_terrainChunk) for (auto chunk : m_terrainChunk)
{ {
@ -717,6 +744,31 @@ void ApplicationClass::GenerateTerrain()
} }
void ApplicationClass::AddKobject(WCHAR* filepath)
{
char modelFilename[128];
char textureFilename[128];
char textureFilename2[128];
filesystem::path p(filepath);
string filename = p.stem().string();
size_t convertedChars = 0;
wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), filepath, _TRUNCATE);
// Set the name of the texture file that we will be loading.
strcpy_s(textureFilename, "stone01.tga");
strcpy_s(textureFilename2, "moss01.tga");
Object* newObject = new Object();
newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2);
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f));
newObject->SetName(filename);
m_object.push_back(newObject);
}
void ApplicationClass::AddCube() void ApplicationClass::AddCube()
{ {
char modelFilename[128]; char modelFilename[128];
@ -739,13 +791,13 @@ void ApplicationClass::AddCube()
m_cubes.push_back(newCube); m_cubes.push_back(newCube);
} }
void ApplicationClass::DeleteCube(int index) void ApplicationClass::DeleteKobject(int index)
{ {
if (index < m_cubes.size()) if (index < m_object.size())
{ {
m_cubes[index]->Shutdown(); m_object[index]->Shutdown();
delete m_cubes[index]; delete m_object[index];
m_cubes.erase(m_cubes.begin() + index); m_object.erase(m_object.begin() + index);
} }
} }

View File

@ -11,6 +11,7 @@
#include "lightshaderclass.h" #include "lightshaderclass.h"
#include "lightclass.h" #include "lightclass.h"
#include <vector> #include <vector>
#include <filesystem>
#include "multitextureshaderclass.h" #include "multitextureshaderclass.h"
#include "bitmapclass.h" #include "bitmapclass.h"
@ -53,11 +54,13 @@ public:
void SetSpeed(float speed) { this->speed = speed; }; void SetSpeed(float speed) { this->speed = speed; };
void AddCube(); void AddCube();
void DeleteCube(int index); void DeleteKobject(int index);
int GetCubeCount() const { return m_cubes.size(); }; int GetCubeCount() const { return m_cubes.size(); };
int GetTerrainCubeCount() const { return m_terrainChunk.size(); }; int GetTerrainCubeCount() const { return m_terrainChunk.size(); };
std::vector<Object*> GetCubes() const { return m_cubes; }; std::vector<Object*> GetCubes() const { return m_cubes; };
std::vector<Object*> GetTerrainCubes() const { return m_terrainChunk; }; std::vector<Object*> GetTerrainCubes() const { return m_terrainChunk; };
std::vector<Object*> GetKobjects() const { return m_object; };
void AddKobject(WCHAR* filepath);
void GenerateTerrain(); void GenerateTerrain();
void DeleteTerrain(); void DeleteTerrain();
@ -89,6 +92,7 @@ private:
FpsClass* m_Fps; FpsClass* m_Fps;
TextClass* m_FpsString; TextClass* m_FpsString;
int m_previousFps; int m_previousFps;
std::vector<Object*> m_object;
}; };
#endif #endif

View File

@ -81,15 +81,16 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app)
{ {
ImGui::Begin("Objects"); ImGui::Begin("Objects");
int index = 0; int index = 0;
for (auto& object : app->GetCubes()) for (auto& object : app->GetKobjects())
{ {
std::string headerName = "Object " + std::to_string(index); std::string headerName = object->GetName() + " " + std::to_string(index);
if (ImGui::CollapsingHeader(headerName.c_str())) if (ImGui::CollapsingHeader(headerName.c_str()))
{ {
XMVECTOR position = object->GetPosition(); XMVECTOR position = object->GetPosition();
XMVECTOR rotation = object->GetRotation(); XMVECTOR rotation = object->GetRotation();
XMVECTOR scale = object->GetScale(); XMVECTOR scale = object->GetScale();
float pos[3] = { XMVectorGetX(position), XMVectorGetY(position), XMVectorGetZ(position) }; float pos[3] = { XMVectorGetX(position), XMVectorGetY(position), XMVectorGetZ(position) };
std::string posLabel = "Position##" + std::to_string(index); std::string posLabel = "Position##" + std::to_string(index);
if (ImGui::DragFloat3(posLabel.c_str(), pos)) if (ImGui::DragFloat3(posLabel.c_str(), pos))
@ -117,7 +118,7 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app)
std::string deleteLabel = "Delete##" + std::to_string(index); std::string deleteLabel = "Delete##" + std::to_string(index);
if (ImGui::Button(deleteLabel.c_str())) if (ImGui::Button(deleteLabel.c_str()))
{ {
app->DeleteCube(index); app->DeleteKobject(index);
} }
ImGui::Separator(); ImGui::Separator();

View File

@ -117,4 +117,48 @@ void Object::SetScale(XMVECTOR scale)
matrix._22 = XMVectorGetY(scale); matrix._22 = XMVectorGetY(scale);
matrix._33 = XMVectorGetZ(scale); matrix._33 = XMVectorGetZ(scale);
m_scaleMatrix = XMLoadFloat4x4(&matrix); m_scaleMatrix = XMLoadFloat4x4(&matrix);
}
void Object::UpdateWorldMatrix()
{
m_worldMatrix = m_scaleMatrix * m_rotateMatrix * m_translateMatrix;
}
void Object::UpdateSRMatrix()
{
m_srMatrix = m_scaleMatrix * m_rotateMatrix;
}
void Object::UpdateTranslateMatrix()
{
m_translateMatrix = XMMatrixTranslationFromVector(GetPosition());
}
void Object::UpdateRotateMatrix()
{
m_rotateMatrix = XMMatrixRotationRollPitchYawFromVector(GetRotation());
}
void Object::UpdateScaleMatrix()
{
m_scaleMatrix = XMMatrixScalingFromVector(GetScale());
}
void Object::Update()
{
UpdateWorldMatrix();
UpdateSRMatrix();
UpdateTranslateMatrix();
UpdateRotateMatrix();
UpdateScaleMatrix();
}
std::string Object::GetName()
{
return m_name;
}
void Object::SetName(std::string name)
{
m_name = name;
} }

View File

@ -27,6 +27,17 @@ public:
XMVECTOR GetRotation(); XMVECTOR GetRotation();
XMVECTOR GetScale(); XMVECTOR GetScale();
void UpdateWorldMatrix();
void UpdateSRMatrix();
void UpdateScaleMatrix();
void UpdateRotateMatrix();
void UpdateTranslateMatrix();
void Update();
std::string GetName();
void SetName(std::string name);
public : public :
bool m_demoSpinning = false; bool m_demoSpinning = false;
@ -37,4 +48,5 @@ private:
XMMATRIX m_srMatrix; XMMATRIX m_srMatrix;
XMMATRIX m_worldMatrix; XMMATRIX m_worldMatrix;
std::string m_name;
}; };