2 Commits

Author SHA256 Message Date
2f3fdf83ae added extra functions to Texture 2026-02-02 07:41:16 +01:00
236da3059f added camera controls to editor 2026-01-25 11:44:06 +01:00
11 changed files with 445 additions and 15 deletions

View File

@@ -148,6 +148,176 @@ void TSE::Texture::GetPixel(const Vector2 &pos, Color &c) const
GetPixel(pos.x, pos.y, c); GetPixel(pos.x, pos.y, c);
} }
TSE::Texture TSE::Texture::CutOut(const Vector2 &pos, Vector2 &size) const
{
Texture result = Texture(0,0);
if(bmp == nullptr)
{
TSE_ERROR("Failed to cut out texture. Source bitmap was nullptr.");
Texture::makeError(result);
return result;
}
const int left = static_cast<int>(pos.x);
const int top = static_cast<int>(pos.y);
const int width = static_cast<int>(size.x);
const int height = static_cast<int>(size.y);
if(width <= 0 || height <= 0)
{
TSE_ERROR("Failed to cut out texture. Size must be greater than 0.");
Texture::makeError(result);
return result;
}
const int texWidth = static_cast<int>(Width());
const int texHeight = static_cast<int>(Height());
if(left < 0 || top < 0 || left + width > texWidth || top + height > texHeight)
{
TSE_ERROR("Failed to cut out texture. Region out of bounds.");
Texture::makeError(result);
return result;
}
FIBITMAP* cut = FreeImage_Copy(bmp, left, top, left + width, top + height);
if(cut == nullptr)
{
TSE_ERROR("Failed to cut out texture. FreeImage_Copy returned nullptr.");
Texture::makeError(result);
return result;
}
Texture out = Texture(width, height, Bpp);
if(out.bmp != nullptr)
FreeImage_Unload(out.bmp);
out.bmp = cut;
out.imagePtr = FreeImage_GetBits(cut);
out.Bpp = Bpp;
out.chanels = chanels;
out.Size = Vector2(width, height);
out.Apply();
return out;
}
TSE::Texture TSE::Texture::Upscale(const Vector2 &size) const
{
Texture result = Texture(0,0);
if(bmp == nullptr)
{
TSE_ERROR("Failed to upscale texture. Source bitmap was nullptr.");
Texture::makeError(result);
return result;
}
const int width = static_cast<int>(size.x);
const int height = static_cast<int>(size.y);
if(width <= 0 || height <= 0)
{
TSE_ERROR("Failed to upscale texture. Size must be greater than 0.");
Texture::makeError(result);
return result;
}
FIBITMAP* scaled = FreeImage_Rescale(bmp, width, height, FILTER_NEAREST);
if(scaled == nullptr)
{
TSE_ERROR("Failed to upscale texture. FreeImage_Rescale returned nullptr.");
Texture::makeError(result);
return result;
}
Texture out = Texture(width, height, Bpp);
if(out.bmp != nullptr)
FreeImage_Unload(out.bmp);
out.bmp = scaled;
out.imagePtr = FreeImage_GetBits(scaled);
out.Bpp = Bpp;
out.chanels = chanels;
out.Size = Vector2(width, height);
out.Apply();
return out;
}
TSE::Texture TSE::Texture::Downscale(const Vector2 &size) const
{
Texture result = Texture(0,0);
if(bmp == nullptr)
{
TSE_ERROR("Failed to downscale texture. Source bitmap was nullptr.");
Texture::makeError(result);
return result;
}
const int width = static_cast<int>(size.x);
const int height = static_cast<int>(size.y);
if(width <= 0 || height <= 0)
{
TSE_ERROR("Failed to downscale texture. Size must be greater than 0.");
Texture::makeError(result);
return result;
}
FIBITMAP* scaled = FreeImage_Rescale(bmp, width, height, FILTER_NEAREST);
if(scaled == nullptr)
{
TSE_ERROR("Failed to downscale texture. FreeImage_Rescale returned nullptr.");
Texture::makeError(result);
return result;
}
Texture out = Texture(width, height, Bpp);
if(out.bmp != nullptr)
FreeImage_Unload(out.bmp);
out.bmp = scaled;
out.imagePtr = FreeImage_GetBits(scaled);
out.Bpp = Bpp;
out.chanels = chanels;
out.Size = Vector2(width, height);
out.Apply();
return out;
}
void TSE::Texture::PasteIn(const Vector2 &pos, Texture &t)
{
if(bmp == nullptr)
{
TSE_ERROR("Failed to paste texture. Destination bitmap was nullptr.");
return;
}
if(t.bmp == nullptr)
{
TSE_ERROR("Failed to paste texture. Source bitmap was nullptr.");
return;
}
if(Bpp != t.Bpp)
{
TSE_ERROR("Failed to paste texture. Source and destination Bpp do not match.");
return;
}
const int left = static_cast<int>(pos.x);
const int top = static_cast<int>(pos.y);
const int srcWidth = static_cast<int>(t.Width());
const int srcHeight = static_cast<int>(t.Height());
const int texWidth = static_cast<int>(Width());
const int texHeight = static_cast<int>(Height());
if(left < 0 || top < 0 || left + srcWidth > texWidth || top + srcHeight > texHeight)
{
TSE_ERROR("Failed to paste texture. Region out of bounds.");
return;
}
if(!FreeImage_Paste(bmp, t.bmp, left, top, 256))
{
TSE_ERROR("Failed to paste texture. FreeImage_Paste returned false.");
return;
}
imagePtr = FreeImage_GetBits(bmp);
Apply();
}
void TSE::Texture::SetPixelNoApply(const Vector2 &pos, const Color &c) void TSE::Texture::SetPixelNoApply(const Vector2 &pos, const Color &c)
{ {
SetPixelNoApply(pos.x, pos.y, c); SetPixelNoApply(pos.x, pos.y, c);

View File

@@ -38,6 +38,10 @@ namespace TSE
byte* GetImagePtr() const; byte* GetImagePtr() const;
void SetPixel(const Vector2& pos, const Color& c); void SetPixel(const Vector2& pos, const Color& c);
void GetPixel(const Vector2& pos, Color& c) const; void GetPixel(const Vector2& pos, Color& c) const;
Texture CutOut(const Vector2& pos, Vector2& size) const;
Texture Upscale(const Vector2& size) const;
Texture Downscale(const Vector2& size) const;
void PasteIn(const Vector2& pos, Texture& t);
void SetPixelNoApply(const Vector2& pos, const Color& c); void SetPixelNoApply(const Vector2& pos, const Color& c);
void ToSprite(Sprite& s); void ToSprite(Sprite& s);
void SetChanels(const byte& ch); void SetChanels(const byte& ch);

View File

@@ -17,6 +17,7 @@ void TSE::ICursorHandler::Enable()
if(!enabled()) if(!enabled())
{ {
IInputManager::instance()->RegisterCursorHandler(this); IInputManager::instance()->RegisterCursorHandler(this);
Enabled = true;
} }
} }
@@ -25,6 +26,7 @@ void TSE::ICursorHandler::Disable()
if(enabled()) if(enabled())
{ {
IInputManager::instance()->UnregisterCursorHandler(this); IInputManager::instance()->UnregisterCursorHandler(this);
Enabled = false;
} }
} }

View File

@@ -17,6 +17,7 @@ void TSE::IKeyInputHandler::Enable()
if(!enabled()) if(!enabled())
{ {
IInputManager::instance()->RegisterKeyHandler(this); IInputManager::instance()->RegisterKeyHandler(this);
Enabled = true;
} }
} }
@@ -25,6 +26,7 @@ void TSE::IKeyInputHandler::Disable()
if(enabled()) if(enabled())
{ {
IInputManager::instance()->UnregisterKeyHandler(this); IInputManager::instance()->UnregisterKeyHandler(this);
Enabled = false;
} }
} }

View File

@@ -0,0 +1,172 @@
#include "basicEditorCamera.hpp"
#include "elements/Transformable.hpp"
#include "utils/Time.hpp"
#include "BehaviourScripts/Camera.hpp"
#include <cmath>
void TSE::EDITOR::basicEditorCamera::OnUpdate()
{
auto euler = baseObject->GetEuler();
auto finalrot = rotDelta * rotationMultiplier * Time::deltaTime();
rotDelta = Vector2::zero;
if(mouseInputEnabled)
{
euler.y += finalrot.x;
euler.x += finalrot.y;
}
baseObject->SetEuler(euler);
Vector3 move;
if(keyInputEnabled && SceneView::IsHovered)
delta = delta + keydelta * keyMultiplier;
move = baseObject->right() * delta.x;
move = move + baseObject->up() * delta.y;
move = move + baseObject->forward() * delta.z;
delta = Vector3::zero;
move = move * movementMultiplier * Time::deltaTime();
baseObject->position = baseObject->position + move;
}
void TSE::EDITOR::basicEditorCamera::Start()
{
ICursorHandler::Enable();
IKeyInputHandler::Enable();
}
void TSE::EDITOR::basicEditorCamera::OnKeyDown(const Key &key, const Modifier &mods)
{
switch (key)
{
case Key::LeftShift:
isShiftPressed = true;
break;
case Key::W:
keydelta = keydelta + Vector3::forward * keyMultiplier;
break;
case Key::A:
keydelta = keydelta + Vector3::left * keyMultiplier;
break;
case Key::S:
keydelta = keydelta + Vector3::back * keyMultiplier;
break;
case Key::D:
keydelta = keydelta + Vector3::right * keyMultiplier;
break;
case Key::E:
keydelta = keydelta + Vector3::up * keyMultiplier;
break;
case Key::Q:
keydelta = keydelta + Vector3::down * keyMultiplier;
break;
default:
return;
}
}
void TSE::EDITOR::basicEditorCamera::OnKeyUp(const Key &key, const Modifier &mods)
{
switch (key)
{
case Key::LeftShift:
isShiftPressed = false;
break;
case Key::W:
keydelta = keydelta - Vector3::forward * keyMultiplier;
break;
case Key::A:
keydelta = keydelta - Vector3::left * keyMultiplier;
break;
case Key::S:
keydelta = keydelta - Vector3::back * keyMultiplier;
break;
case Key::D:
keydelta = keydelta - Vector3::right * keyMultiplier;
break;
case Key::E:
keydelta = keydelta - Vector3::up * keyMultiplier;
break;
case Key::Q:
keydelta = keydelta - Vector3::down * keyMultiplier;
break;
default:
return;
}
}
void TSE::EDITOR::basicEditorCamera::OnMouseButtonDown(const MouseBtn &btn, const Modifier &mods)
{
if(!mouseInputEnabled) return;
switch (btn)
{
case MouseBtn::RightMouse:
holdingRightClick = true;
break;
case MouseBtn::LeftMouse:
holdingLeftClick = true;
break;
}
}
void TSE::EDITOR::basicEditorCamera::OnMouseButtonUp(const MouseBtn &btn, const Modifier &mods)
{
if(!mouseInputEnabled) return;
switch (btn)
{
case MouseBtn::RightMouse:
holdingRightClick = false;
break;
case MouseBtn::LeftMouse:
holdingLeftClick = false;
break;
default:
return;
}
}
void TSE::EDITOR::basicEditorCamera::OnMousePosition(const Vector2 &pos)
{
if(!mouseInputEnabled || !SceneView::IsHovered) return;
if(isShiftPressed && holdingLeftClick)
{
auto tmp = pos - lastMousepos;
tmp.x *= -1;
delta = delta + tmp;
}
else if(holdingRightClick)
{
rotDelta = rotDelta + (pos - lastMousepos);
}
lastMousepos = pos;
}
void TSE::EDITOR::basicEditorCamera::OnMouseScroll(const Vector2 &mdelta)
{
if(!mouseInputEnabled || !SceneView::IsHovered) return;
Camera* editorCam = (Camera*)Transformable::Find(".EditorCamera")->GetBehaviourScript(CAMERA);
float scale = editorCam->GetRenderScale();
scale += mdelta.y * zoomMultiplier;
scale = std::clamp(scale, zoomMin, zoomMax);
editorCam->SetRenderScale(scale);
}
void TSE::EDITOR::basicEditorCamera::CustomDraw(const bool &debug)
{
ImGui::DragFloat("Rotation Speed", &rotationMultiplier, 0.1f);
ImGui::DragFloat("Movement Speed", &movementMultiplier, 0.1f);
ImGui::DragFloat("Zoom Speed", &zoomMultiplier, 0.1f);
ImGui::DragFloat("key Speed", &keyMultiplier, 0.1f);
ImGui::Checkbox("Keybord Input", &keyInputEnabled);
ImGui::Checkbox("Mouse Input", &mouseInputEnabled);
}

View File

@@ -0,0 +1,60 @@
#pragma once
#define BASIC_EDITOR_CAMERA typeid(basicEditorCamera).name()
#include "elements/BehaviourScript.hpp"
#include "interfaces/ICursorHandler.hpp"
#include "interfaces/IKeyInputHandler.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "imgui/imgui.h"
#include "UI/windows/SceneView.hpp"
namespace TSE::EDITOR
{
class basicEditorCamera : public BehaviourScript, public ICursorHandler, public IKeyInputHandler
{
private:
bool keyInputEnabled = true;
bool mouseInputEnabled = true;
bool isShiftPressed = false;
bool holdingRightClick = false;
bool holdingLeftClick = false;
Vector3 delta = Vector3::zero;
Vector3 keydelta = Vector3::zero;
Vector2 rotDelta = Vector2::zero;
Vector2 lastMousepos;
float keyMultiplier = 0.5f;
float rotationMultiplier = 150;
float movementMultiplier = 64;
float zoomMultiplier = 2;
const float zoomMin = 6;
const float zoomMax = 500;
public:
void OnUpdate() override;
void Start() override;
inline const char* GetName() override
{
return "basicEditorCamera";
}
void OnKeyDown(const Key& key, const Modifier& mods) override;
void OnKeyUp(const Key& key, const Modifier& mods) override;
void OnMouseButtonDown(const MouseBtn& btn, const Modifier& mods) override;
void OnMouseButtonUp(const MouseBtn& btn, const Modifier& mods) override;
void OnMousePosition(const Vector2& pos) override;
void OnMouseScroll(const Vector2& delta) override;
void CustomDraw(const bool& debug) override;
};
} // namespace TSE::EDITOR

View File

@@ -3,6 +3,7 @@
#include "BehaviourScriptRegistry.hpp" #include "BehaviourScriptRegistry.hpp"
#include "BehaviourScripts/AudioListener.hpp" #include "BehaviourScripts/AudioListener.hpp"
#include "BehaviourScripts/AudioSource.hpp" #include "BehaviourScripts/AudioSource.hpp"
#include "BehaviourScripts/basicEditorCamera.hpp"
TSE::EDITOR::EditorSubsystem::EditorSubsystem() : sv(nullptr), editorLayer("") TSE::EDITOR::EditorSubsystem::EditorSubsystem() : sv(nullptr), editorLayer("")
{ {
@@ -34,8 +35,8 @@ TSE::EDITOR::EditorSubsystem::EditorSubsystem() : sv(nullptr), editorLayer("")
editorCam->SetRenderTarget(rt); editorCam->SetRenderTarget(rt);
editorCamera->AddBehaviourScript(editorCam); editorCamera->AddBehaviourScript(editorCam);
// basicCameraControls controls = basicCameraControls(); basicEditorCamera* controls = new basicEditorCamera();
// editorCamera->AddBehaviourScript(&controls); editorCamera->AddBehaviourScript(controls);
editorLayer = Layer(".editor"); editorLayer = Layer(".editor");
editorLayer.AddTransformable(editorCamera); editorLayer.AddTransformable(editorCamera);

View File

@@ -42,6 +42,11 @@ void TSE::EDITOR::HirearchieView::Define()
DisplayLayer(layer); DisplayLayer(layer);
} }
if(!selectedFound && PropertiesView::GetCurrentInspectableType() == InspectableType::Transformable)
{
PropertiesView::ForceClearInspectorElement();
}
} }
if(activatePopUpNamingLayer) if(activatePopUpNamingLayer)
@@ -188,10 +193,6 @@ void TSE::EDITOR::HirearchieView::DisplayLayer(Layer *l)
{ {
DisplayObj(l->GetAllObjects()[i], l); DisplayObj(l->GetAllObjects()[i], l);
} }
if(!selectedFound && PropertiesView::GetCurrentInspectableType() == InspectableType::Transformable)
{
PropertiesView::ForceClearInspectorElement();
}
} }
ImGui::Unindent(20.0f); ImGui::Unindent(20.0f);
} }

View File

@@ -16,6 +16,14 @@ void TSE::EDITOR::SceneView::Define()
{ {
fb->SetSize({vec2.x, vec2.y}); fb->SetSize({vec2.x, vec2.y});
} }
if(ImGui::IsWindowFocused())
{
IsHovered = true;
}
else
{
IsHovered = false;
}
} }
ImGui::EndChild(); ImGui::EndChild();
} }

View File

@@ -10,6 +10,7 @@ namespace TSE::EDITOR
private: private:
TSE::IRenderTexture* fb; TSE::IRenderTexture* fb;
public: public:
inline static bool IsHovered = false;
SceneView(TSE::IRenderTexture* frameBuffer); SceneView(TSE::IRenderTexture* frameBuffer);
void Define() override; void Define() override;
}; };

View File

@@ -17,19 +17,22 @@ void TSE::GLFW::InputGlfw::KeyCallback(GLFWwindow *window, int key, int scancode
case 1: //down case 1: //down
for(auto handler : ((InputGlfw*)instance())->keyHandler) for(auto handler : ((InputGlfw*)instance())->keyHandler)
{ {
handler->OnKeyDown((Key)key, (Modifier)mods); if(handler->enabled())
handler->OnKeyDown((Key)key, (Modifier)mods);
} }
break; break;
case 0: //up case 0: //up
for(auto handler : ((InputGlfw*)instance())->keyHandler) for(auto handler : ((InputGlfw*)instance())->keyHandler)
{ {
handler->OnKeyUp((Key)key, (Modifier)mods); if(handler->enabled())
handler->OnKeyUp((Key)key, (Modifier)mods);
} }
break; break;
case 2: //hold case 2: //hold
for(auto handler : ((InputGlfw*)instance())->keyHandler) for(auto handler : ((InputGlfw*)instance())->keyHandler)
{ {
handler->OnKeyHold((Key)key, (Modifier)mods); if(handler->enabled())
handler->OnKeyHold((Key)key, (Modifier)mods);
} }
break; break;
} }
@@ -42,19 +45,22 @@ void TSE::GLFW::InputGlfw::MouseButtonCallback(GLFWwindow *window, int button, i
case 1: //down case 1: //down
for(auto handler : ((InputGlfw*)instance())->cursorHandler) for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{ {
handler->OnMouseButtonDown((MouseBtn)button, (Modifier)mods); if(handler->enabled())
handler->OnMouseButtonDown((MouseBtn)button, (Modifier)mods);
} }
break; break;
case 0: //up case 0: //up
for(auto handler : ((InputGlfw*)instance())->cursorHandler) for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{ {
handler->OnMouseButtonUp((MouseBtn)button, (Modifier)mods); if(handler->enabled())
handler->OnMouseButtonUp((MouseBtn)button, (Modifier)mods);
} }
break; break;
case 2: //hold case 2: //hold
for(auto handler : ((InputGlfw*)instance())->cursorHandler) for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{ {
handler->OnMouseButtonHold((MouseBtn)button, (Modifier)mods); if(handler->enabled())
handler->OnMouseButtonHold((MouseBtn)button, (Modifier)mods);
} }
break; break;
} }
@@ -64,7 +70,8 @@ void TSE::GLFW::InputGlfw::CursorPosCallback(GLFWwindow *window, double xpos, do
{ {
for(auto handler : ((InputGlfw*)instance())->cursorHandler) for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{ {
handler->OnMousePosition(Vector2(xpos, ypos)); if(handler->enabled())
handler->OnMousePosition(Vector2(xpos, ypos));
} }
} }
@@ -72,7 +79,8 @@ void TSE::GLFW::InputGlfw::ScrollCallback(GLFWwindow *window, double xoffset, do
{ {
for(auto handler : ((InputGlfw*)instance())->cursorHandler) for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{ {
handler->OnMouseScroll(Vector2(xoffset, yoffset)); if(handler->enabled())
handler->OnMouseScroll(Vector2(xoffset, yoffset));
} }
} }
@@ -106,6 +114,7 @@ void TSE::GLFW::InputGlfw::CharCallback(GLFWwindow *window, unsigned int codepoi
for(auto handler : ((InputGlfw*)instance())->keyHandler) for(auto handler : ((InputGlfw*)instance())->keyHandler)
{ {
handler->OnChar(msg); if(handler->enabled())
handler->OnChar(msg);
} }
} }