104 lines
3.9 KiB
C++
104 lines
3.9 KiB
C++
#include "Components/CameraComponent.h"
|
|
|
|
#include <algorithm>
|
|
#include <sstream>
|
|
|
|
namespace XCEngine {
|
|
namespace Components {
|
|
|
|
void CameraComponent::SetFieldOfView(float value) {
|
|
m_fieldOfView = std::clamp(value, 1.0f, 179.0f);
|
|
}
|
|
|
|
void CameraComponent::SetOrthographicSize(float value) {
|
|
m_orthographicSize = std::max(0.001f, value);
|
|
}
|
|
|
|
void CameraComponent::SetNearClipPlane(float value) {
|
|
m_nearClipPlane = std::max(0.001f, value);
|
|
if (m_farClipPlane <= m_nearClipPlane) {
|
|
m_farClipPlane = m_nearClipPlane + 0.001f;
|
|
}
|
|
}
|
|
|
|
void CameraComponent::SetFarClipPlane(float value) {
|
|
m_farClipPlane = std::max(m_nearClipPlane + 0.001f, value);
|
|
}
|
|
|
|
void CameraComponent::SetViewportRect(const Math::Rect& value) {
|
|
const float x = std::clamp(value.x, 0.0f, 1.0f);
|
|
const float y = std::clamp(value.y, 0.0f, 1.0f);
|
|
const float width = std::clamp(value.width, 0.0f, 1.0f);
|
|
const float height = std::clamp(value.height, 0.0f, 1.0f);
|
|
const float right = std::min(1.0f, x + width);
|
|
const float bottom = std::min(1.0f, y + height);
|
|
m_viewportRect = Math::Rect(x, y, right - x, bottom - y);
|
|
}
|
|
|
|
void CameraComponent::Serialize(std::ostream& os) const {
|
|
os << "projection=" << static_cast<int>(m_projectionType) << ";";
|
|
os << "fov=" << m_fieldOfView << ";";
|
|
os << "orthoSize=" << m_orthographicSize << ";";
|
|
os << "near=" << m_nearClipPlane << ";";
|
|
os << "far=" << m_farClipPlane << ";";
|
|
os << "depth=" << m_depth << ";";
|
|
os << "primary=" << (m_primary ? 1 : 0) << ";";
|
|
os << "clearMode=" << static_cast<int>(m_clearMode) << ";";
|
|
os << "stackType=" << static_cast<int>(m_stackType) << ";";
|
|
os << "cullingMask=" << m_cullingMask << ";";
|
|
os << "viewportRect=" << m_viewportRect.x << "," << m_viewportRect.y << "," << m_viewportRect.width << "," << m_viewportRect.height << ";";
|
|
os << "clearColor=" << m_clearColor.r << "," << m_clearColor.g << "," << m_clearColor.b << "," << m_clearColor.a << ";";
|
|
}
|
|
|
|
void CameraComponent::Deserialize(std::istream& is) {
|
|
std::string token;
|
|
while (std::getline(is, token, ';')) {
|
|
if (token.empty()) {
|
|
continue;
|
|
}
|
|
|
|
const size_t eqPos = token.find('=');
|
|
if (eqPos == std::string::npos) {
|
|
continue;
|
|
}
|
|
|
|
const std::string key = token.substr(0, eqPos);
|
|
std::string value = token.substr(eqPos + 1);
|
|
|
|
if (key == "projection") {
|
|
m_projectionType = static_cast<CameraProjectionType>(std::stoi(value));
|
|
} else if (key == "fov") {
|
|
SetFieldOfView(std::stof(value));
|
|
} else if (key == "orthoSize") {
|
|
SetOrthographicSize(std::stof(value));
|
|
} else if (key == "near") {
|
|
SetNearClipPlane(std::stof(value));
|
|
} else if (key == "far") {
|
|
SetFarClipPlane(std::stof(value));
|
|
} else if (key == "depth") {
|
|
m_depth = std::stof(value);
|
|
} else if (key == "primary") {
|
|
m_primary = (std::stoi(value) != 0);
|
|
} else if (key == "clearMode") {
|
|
m_clearMode = static_cast<CameraClearMode>(std::stoi(value));
|
|
} else if (key == "stackType") {
|
|
m_stackType = static_cast<CameraStackType>(std::stoi(value));
|
|
} else if (key == "cullingMask") {
|
|
m_cullingMask = static_cast<uint32_t>(std::stoul(value));
|
|
} else if (key == "viewportRect") {
|
|
std::replace(value.begin(), value.end(), ',', ' ');
|
|
std::istringstream ss(value);
|
|
Math::Rect viewportRect;
|
|
ss >> viewportRect.x >> viewportRect.y >> viewportRect.width >> viewportRect.height;
|
|
SetViewportRect(viewportRect);
|
|
} else if (key == "clearColor") {
|
|
std::replace(value.begin(), value.end(), ',', ' ');
|
|
std::istringstream ss(value);
|
|
ss >> m_clearColor.r >> m_clearColor.g >> m_clearColor.b >> m_clearColor.a;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Components
|
|
} // namespace XCEngine
|