Files
WickedEngine/Editor/SoftBodyWindow.cpp
T
Turánszki János 21d5f37fee build updates (#655)
2023-03-28 19:30:29 +02:00

146 lines
4.3 KiB
C++

#include "stdafx.h"
#include "SoftBodyWindow.h"
using namespace wi::ecs;
using namespace wi::scene;
void SoftBodyWindow::Create(EditorComponent* _editor)
{
editor = _editor;
wi::gui::Window::Create(ICON_SOFTBODY " Soft Body Physics", wi::gui::Window::WindowControls::COLLAPSE | wi::gui::Window::WindowControls::CLOSE);
SetSize(XMFLOAT2(580, 200));
closeButton.SetTooltip("Delete MeshComponent");
OnClose([=](wi::gui::EventArgs args) {
wi::Archive& archive = editor->AdvanceHistory();
archive << EditorComponent::HISTORYOP_COMPONENT_DATA;
editor->RecordEntity(archive, entity);
editor->GetCurrentScene().softbodies.Remove(entity);
editor->RecordEntity(archive, entity);
editor->optionsWnd.RefreshEntityTree();
});
float x = 95;
float y = 0;
float hei = 18;
float step = hei + 2;
float wid = 170;
infoLabel.Create("");
infoLabel.SetText("Soft body physics must be used together with a MeshComponent, otherwise it will have no effect.\nYou can use the Paint Tool to pin or soften soft body vertices.");
infoLabel.SetSize(XMFLOAT2(100, 90));
AddWidget(&infoLabel);
massSlider.Create(0, 10, 1, 100000, "Mass: ");
massSlider.SetTooltip("Set the mass amount for the physics engine.");
massSlider.SetSize(XMFLOAT2(wid, hei));
massSlider.SetPos(XMFLOAT2(x, y));
massSlider.OnSlide([&](wi::gui::EventArgs args) {
SoftBodyPhysicsComponent* physicscomponent = editor->GetCurrentScene().softbodies.GetComponent(entity);
if (physicscomponent != nullptr)
{
physicscomponent->physicsobject = {};
physicscomponent->mass = args.fValue;
}
});
AddWidget(&massSlider);
frictionSlider.Create(0, 1, 0.5f, 100000, "Friction: ");
frictionSlider.SetTooltip("Set the friction amount for the physics engine.");
frictionSlider.SetSize(XMFLOAT2(wid, hei));
frictionSlider.SetPos(XMFLOAT2(x, y += step));
frictionSlider.OnSlide([&](wi::gui::EventArgs args) {
SoftBodyPhysicsComponent* physicscomponent = editor->GetCurrentScene().softbodies.GetComponent(entity);
if (physicscomponent != nullptr)
{
physicscomponent->physicsobject = {};
physicscomponent->friction = args.fValue;
}
});
AddWidget(&frictionSlider);
restitutionSlider.Create(0, 1, 0, 100000, "Restitution: ");
restitutionSlider.SetTooltip("Set the restitution amount for the physics engine.");
restitutionSlider.SetSize(XMFLOAT2(wid, hei));
restitutionSlider.SetPos(XMFLOAT2(x, y += step));
restitutionSlider.OnSlide([&](wi::gui::EventArgs args) {
SoftBodyPhysicsComponent* physicscomponent = editor->GetCurrentScene().softbodies.GetComponent(entity);
if (physicscomponent != nullptr)
{
physicscomponent->physicsobject = {};
physicscomponent->restitution = args.fValue;
}
});
AddWidget(&restitutionSlider);
SetMinimized(true);
SetVisible(false);
SetEntity(INVALID_ENTITY);
}
void SoftBodyWindow::SetEntity(Entity entity)
{
this->entity = entity;
Scene& scene = editor->GetCurrentScene();
const SoftBodyPhysicsComponent* physicscomponent = scene.softbodies.GetComponent(entity);
if (physicscomponent != nullptr)
{
massSlider.SetValue(physicscomponent->mass);
frictionSlider.SetValue(physicscomponent->friction);
restitutionSlider.SetValue(physicscomponent->restitution);
}
}
void SoftBodyWindow::ResizeLayout()
{
wi::gui::Window::ResizeLayout();
const float padding = 4;
const float width = GetWidgetAreaSize().x;
float y = padding;
float jump = 20;
const float margin_left = 120;
const float margin_right = 40;
auto add = [&](wi::gui::Widget& widget) {
if (!widget.IsVisible())
return;
widget.SetPos(XMFLOAT2(margin_left, y));
widget.SetSize(XMFLOAT2(width - margin_left - margin_right, widget.GetScale().y));
y += widget.GetSize().y;
y += padding;
};
auto add_right = [&](wi::gui::Widget& widget) {
if (!widget.IsVisible())
return;
widget.SetPos(XMFLOAT2(width - margin_right - widget.GetSize().x, y));
y += widget.GetSize().y;
y += padding;
};
auto add_fullwidth = [&](wi::gui::Widget& widget) {
if (!widget.IsVisible())
return;
const float margin_left = padding;
const float margin_right = padding;
widget.SetPos(XMFLOAT2(margin_left, y));
widget.SetSize(XMFLOAT2(width - margin_left - margin_right, widget.GetScale().y));
y += widget.GetSize().y;
y += padding;
};
add_fullwidth(infoLabel);
add(massSlider);
add(frictionSlider);
add(restitutionSlider);
}