Hazel
Hazel copied to clipboard
Drag a texture image directly into the viewport
Drag a texture image directly into the viewport
Until now, to add a texture to a sprite renderer component, the image has to be dragged onto the "Texture" button in the scene hierarchy panel. Now, in addition, one can drag a texture image directly onto a component in the viewport.
May I suggest that you change it to something like this:
std::filesystem::path filePath = std::filesystem::path(path);
if (filePath.extension().string() == ".hazel")
{
OpenScene(std::filesystem::path(g_AssetPath) / path);
}
else if (filePath.extension().string() == ".png")
{
std::filesystem::path texturePath = std::filesystem::path(g_AssetPath) / path;
Ref<Texture2D> texture = Texture2D::Create(texturePath.string());
if (texture->IsLoaded())
{
if (m_HoveredEntity && m_HoveredEntity.HasComponent<SpriteRendererComponent>())
m_HoveredEntity.GetComponent<SpriteRendererComponent>().Texture = texture;
}
else
{
HZ_WARN("Could not load texture {0}", texturePath.filename().string());
}
}
and keep the rest you had? Because the code you provided will crash if the user tries to drag a texture into the scene and there is no highlighted entity! this fixes that problem and also i personally do not like the style of using the folder structure from 'scenes' and 'textures'... what i mean is, what if somebody has a scene or texture outside of those folders? therefore i made this and you can freely copy it and update your pull request :) Please let me know if this is fine by you :)
May I suggest that you change it to something like this:
std::filesystem::path filePath = std::filesystem::path(path); if (filePath.extension().string() == ".hazel") { OpenScene(std::filesystem::path(g_AssetPath) / path); } else if (filePath.extension().string() == ".png") { std::filesystem::path texturePath = std::filesystem::path(g_AssetPath) / path; Ref<Texture2D> texture = Texture2D::Create(texturePath.string()); if (texture->IsLoaded()) { if (m_HoveredEntity && m_HoveredEntity.HasComponent<SpriteRendererComponent>()) m_HoveredEntity.GetComponent<SpriteRendererComponent>().Texture = texture; } else { HZ_WARN("Could not load texture {0}", texturePath.filename().string()); } }
and keep the rest you had? Because the code you provided will crash if the user tries to drag a texture into the scene and there is no highlighted entity! this fixes that problem and also i personally do not like the style of using the folder structure from 'scenes' and 'textures'... what i mean is, what if somebody has a scene or texture outside of those folders? therefore i made this and you can freely copy it and update your pull request :) Please let me know if this is fine by you :)
Thanks for pointing this out. Initially, I used the extension to identify the action. Then I thought that a scene may not have the ".hazel" extension or a texture may not necessarily be a ".png" file always. Therefore, I changed it to directories.
Regarding the crash, I think, a condition can be added to prevent that.
Bottom line is that it's a design issue and I have no idea whether Cherno will maintain the directory structure or the scene format or the texture format. It will be great if he takes a call on this. In the meantime, I will work on the bug you have pointed out.
Thanks again for your feedback.
@VagueLobster I have added your code in the new commit. I have also fixed the bug you pointed out. Please have a look at the updated version.
I have just looked at the new commit, and it works now, but why keep the old code? Is there a reason for that?
As I mentioned in my first response that I have no idea whether Cherno will maintain the directory structure or the scene format or the texture format. Therefore, I leave it to him to keep it or remove it.
oh okay :) now i get it :D
Overall a nice feature! Please remove comments, remove imgui.ini
from commit, make sure it still merges with no conflicts, and I'll merge it in. 👍
@MajisTcd I think @TheCherno wanted you to remove the comments (delete the comments), not re-use the old code that you commented out! 🙂 the rest is fine!
@TheCherno Can you please check the last update whether it has fixed the issues?
@VagueLobster I was about to respond to you, I was just trying to figure out what exactly I need to do. In my machine, the code works fine. If you think you can still find bugs, I am changing it to your code. However, I am not sure about "delete the comments". If you have got it, can you please tell me which comments I should delete?
@VagueLobster I was about to respond to you, I was just trying to figure out what exactly I need to do. In my machine, the code works fine. If you think you can still find bugs, I am changing it to your code. However, I am not sure about "delete the comments". If you have got it, can you please tell me which comments I should delete?
The bug, i thought i could reproduce is not a bug it seems! Also, maybe it's just me, but i don't like to be forced to have to use the textures
folder to be able to import textures.
I think that the user should have a choice as to where they want their textures to be 🙂
Also the users shouldn't be required either to have their scenes inside scenes
folder, again it should be their free choice!
That was why i changed your code back in September last year! 🙂
@MajisTcd Actually, there is a minor bug in your code, i just found:
Because of this line of code else if (parentPath == "textures" && name != "None")
the user cannot add textures to sprites that is named None! Again the user should have the freedom to choose
whatever they want imo! Of course maybe not in every instance, but this, i don't see any harm in having the freedom 🙂
Also, @TheCherno mentioned in the live stream that he wanted you to remove the imgui.ini
file.
Also why is the Pink Cube.hazel
included in the changes?
Your commits should only say that you've changed 1 file, the EditorLayer.cpp
.
@VagueLobster Thanks for your time and suggestions. This is my first time contributing to a public repository. So I am having some trouble using git. I have kept your code but removed the comment mentioning that it's your code. Please let me know how to acknowledge your contribution and I will be happy to do so.
@VagueLobster Thanks for your time and suggestions. This is my first time contributing to a public repository. So I am having some trouble using git. I have kept your code but removed the comment mentioning that it's your code. Please let me know how to acknowledge your contribution and I will be happy to do so.
No problem 🙂 You don't have to mention me or that I have changed your code, i'm just happy to help 😄
But you should change your repository to have the imgui.ini from Cherno's most recent pull request and NOT changing it any further, otherwise github registers it as a change that must be commited! Also you need to remove the ColoredCube.hazel
from this repository when commiting, because it's not a file that Cherno has made! And it's not necessary 😄 Otherwise it looks good 👍
Oh! Also, you need to make sure your repository that you want to commit has the same files / is up to date with the latest version of Hazel 2D. I don't know if you can do that by just fetching upstream, when you go into your repository of Hazel 2D 🙂