Functional translation widget in global and local space

This commit is contained in:
2016-02-01 15:47:56 +01:00
parent a22bc00d46
commit 65f5bc673f
7 changed files with 131 additions and 25 deletions
+1 -1
Submodule assets updated: e4dc9529f2...091ad5c01b
+19
View File
@@ -19,6 +19,7 @@
#include "../Core/ResourceManager.h"
#include "../Core/EPause.h"
#include "../Core/EKeyDown.h"
#include "../Core/ELockMouse.h"
#include "../Core/EFileDropped.h"
#include "../Rendering/Texture.h"
@@ -34,6 +35,12 @@ public:
Scale
};
enum class WidgetSpace
{
Global,
Local
};
void Draw();
void SelectEntity(EntityWrapper entity);
@@ -75,6 +82,9 @@ public:
// Called when the user selects a widget mode.
typedef std::function<void(WidgetMode)> OnWidgetMode_t;
void SetWidgetModeCallback(OnWidgetMode_t f) { m_OnWidgetMode = f; }
// Called when the user selects a widget space.
typedef std::function<void(WidgetSpace)> OnWidgetSpace_t;
void SetWidgetSpaceCallback(OnWidgetSpace_t f) { m_OnWidgetSpace = f; }
private:
World* m_World;
@@ -95,10 +105,12 @@ private:
EntityWrapper m_CurrentlyDragging = EntityWrapper::Invalid;
std::string m_LastErrorMessage;
WidgetMode m_CurrentWidgetMode = WidgetMode::Translate;
WidgetSpace m_CurrentWidgetSpace = WidgetSpace::Global;
std::set<std::string> m_ModalsToOpen;
std::map<std::string, boost::any> m_ModalData;
std::string m_DroppedFile = "";
bool m_Paused = false;
bool m_MouseLocked = false;
// Callbacks
OnEntitySelectedCallback_t m_OnEntitySelected = nullptr;
@@ -111,6 +123,7 @@ private:
OnComponentAttach_t m_OnComponentAttach = nullptr;
OnComponentDelete_t m_OnComponentDelete = nullptr;
OnWidgetMode_t m_OnWidgetMode = nullptr;
OnWidgetSpace_t m_OnWidgetSpace = nullptr;
// Events
EventRelay<EditorGUI, Events::KeyDown> m_EKeyDown;
@@ -121,6 +134,10 @@ private:
bool OnPause(const Events::Pause& e);
EventRelay<EditorGUI, Events::Resume> m_EResume;
bool OnResume(const Events::Resume& e);
EventRelay<EditorGUI, Events::LockMouse> m_ELockMouse;
bool OnLockMouse(const Events::LockMouse& e);
EventRelay<EditorGUI, Events::UnlockMouse> m_EUnlockMouse;
bool OnUnlockMouse(const Events::UnlockMouse& e);
// Utility functions
boost::filesystem::path fileOpenDialog();
@@ -128,6 +145,8 @@ private:
const std::string formatEntityName(EntityWrapper entity);
GLuint tryLoadTexture(std::string filePath);
void openModal(const std::string& modal);
void setWidgetMode(WidgetMode mode);
void toggleWidgetSpace();
static bool compareCharArray(const char* c1, const char* c2);
// Entity file handling methods
+2
View File
@@ -41,6 +41,7 @@ private:
double m_LastTime = 0.f;
bool m_Enabled = true;
EditorGUI::WidgetMode m_WidgetMode = EditorGUI::WidgetMode::Translate;
EditorGUI::WidgetSpace m_WidgetSpace = EditorGUI::WidgetSpace::Global;
EntityWrapper m_Widget = EntityWrapper::Invalid;
EntityWrapper m_CurrentSelection = EntityWrapper::Invalid;
@@ -57,6 +58,7 @@ private:
void OnEntityChangeName(EntityWrapper entity, const std::string& name);
void OnComponentAttach(EntityWrapper entity, const std::string& componentType);
void OnComponentDelete(EntityWrapper entity, const std::string& componentType);
void OnWidgetSpace(EditorGUI::WidgetSpace widgetSpace);
// Events
EventRelay<EditorSystem, Events::MousePress> m_EMousePress;
-4
View File
@@ -15,10 +15,6 @@ Space=Jump
LeftControl=Crouch
LeftShift=Sprint
F1=ToggleEditor
1=EditorToolMove
2=EditorToolRotate
3=EditorToolScale
X=EditorToggleTransformSpace
C=ConnectToServer
N=SwitchToServer
M=SwitchToClient
+79 -8
View File
@@ -9,6 +9,8 @@ EditorGUI::EditorGUI(World* world, EventBroker* eventBroker)
EVENT_SUBSCRIBE_MEMBER(m_EFileDropped, &EditorGUI::OnFileDropped);
EVENT_SUBSCRIBE_MEMBER(m_EPause, &EditorGUI::OnPause);
EVENT_SUBSCRIBE_MEMBER(m_EResume, &EditorGUI::OnResume);
EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &EditorGUI::OnLockMouse);
EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &EditorGUI::OnUnlockMouse);
}
void EditorGUI::Draw()
@@ -40,26 +42,49 @@ void EditorGUI::drawTools()
return;
}
// Widget modes
createWidgetToolButton(WidgetMode::Translate);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Translate");
ImGui::SetTooltip("Translate (W)");
}
ImGui::SameLine();
createWidgetToolButton(WidgetMode::Rotate);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Rotate");
ImGui::SetTooltip("Rotate (E)");
}
ImGui::SameLine();
createWidgetToolButton(WidgetMode::Scale);
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Scale");
ImGui::SetTooltip("Scale (R)");
}
ImGui::SameLine();
ImGui::ItemSize(ImVec2(5, 0));
// Widget space
ImGui::SameLine();
GLuint spaceTexture = 0;
if (m_CurrentWidgetSpace == WidgetSpace::Global) {
spaceTexture = tryLoadTexture("Textures/Icons/Global.png");
} else if (m_CurrentWidgetSpace == WidgetSpace::Local) {
spaceTexture = tryLoadTexture("Textures/Icons/Local.png");
}
if (ImGui::ImageButton((void*)spaceTexture, ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) {
toggleWidgetSpace();
}
if (ImGui::IsItemHovered()) {
if (m_CurrentWidgetSpace == WidgetSpace::Global) {
ImGui::SetTooltip("Widget space: Global (X)");
} else if (m_CurrentWidgetSpace == WidgetSpace::Local) {
ImGui::SetTooltip("Widget space: Local (X)");
}
}
ImGui::SameLine();
ImGui::ItemSize(ImVec2(5, 0));
// Play button
ImGui::SameLine();
static bool paused = false;
if (ImGui::ImageButton((void*)tryLoadTexture("Textures/Icons/Play.png"), ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), (!m_Paused) ? ImVec4(0, 1, 0, 1) : ImVec4(1, 1, 1, 1))) {
Events::Resume e;
e.World = m_World;
@@ -549,10 +574,7 @@ void EditorGUI::createWidgetToolButton(WidgetMode mode)
(m_CurrentWidgetMode == mode) ? ImVec4(0, 1, 0, 1) : ImVec4(1, 1, 1, 1)
)
) {
if (m_OnWidgetMode != nullptr) {
m_OnWidgetMode(mode);
}
m_CurrentWidgetMode = mode;
setWidgetMode(mode);
}
}
@@ -582,6 +604,22 @@ bool EditorGUI::OnKeyDown(const Events::KeyDown& e)
}
}
if (!m_MouseLocked) {
if (e.KeyCode == GLFW_KEY_W) {
setWidgetMode(WidgetMode::Translate);
}
if (e.KeyCode == GLFW_KEY_E) {
setWidgetMode(WidgetMode::Rotate);
}
if (e.KeyCode == GLFW_KEY_R) {
setWidgetMode(WidgetMode::Scale);
}
if (e.KeyCode == GLFW_KEY_X) {
toggleWidgetSpace();
}
}
return true;
}
@@ -612,6 +650,18 @@ bool EditorGUI::OnResume(const Events::Resume& e)
return true;
}
bool EditorGUI::OnLockMouse(const Events::LockMouse& e)
{
m_MouseLocked = true;
return true;
}
bool EditorGUI::OnUnlockMouse(const Events::UnlockMouse& e)
{
m_MouseLocked = false;
return true;
}
boost::filesystem::path EditorGUI::fileOpenDialog()
{
namespace bfs = boost::filesystem;
@@ -683,6 +733,27 @@ void EditorGUI::openModal(const std::string& modal)
m_ModalsToOpen.insert(modal);
}
void EditorGUI::setWidgetMode(WidgetMode mode)
{
if (m_OnWidgetMode != nullptr) {
m_OnWidgetMode(mode);
}
m_CurrentWidgetMode = mode;
}
void EditorGUI::toggleWidgetSpace()
{
if (m_CurrentWidgetSpace == WidgetSpace::Global) {
m_CurrentWidgetSpace = WidgetSpace::Local;
} else if (m_CurrentWidgetSpace == WidgetSpace::Local) {
m_CurrentWidgetSpace = WidgetSpace::Global;
}
if (m_OnWidgetSpace != nullptr) {
m_OnWidgetSpace(m_CurrentWidgetSpace);
}
}
bool EditorGUI::compareCharArray(const char* c1, const char* c2)
{
return strcmp(c1, c2) < 0;
+23 -6
View File
@@ -31,6 +31,7 @@ EditorSystem::EditorSystem(World* world, EventBroker* eventBroker, IRenderer* re
m_EditorGUI->SetComponentAttachCallback(std::bind(&EditorSystem::OnComponentAttach, this, std::placeholders::_1, std::placeholders::_2));
m_EditorGUI->SetComponentDeleteCallback(std::bind(&EditorSystem::OnComponentDelete, this, std::placeholders::_1, std::placeholders::_2));
m_EditorGUI->SetWidgetModeCallback(std::bind(&EditorSystem::setWidgetMode, this, std::placeholders::_1));
m_EditorGUI->SetWidgetSpaceCallback(std::bind(&EditorSystem::OnWidgetSpace, this, std::placeholders::_1));
EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &EditorSystem::OnMousePress);
EVENT_SUBSCRIBE_MEMBER(m_EWidgetDelta, &EditorSystem::OnWidgetDelta);
@@ -65,7 +66,12 @@ void EditorSystem::Update(double dt)
m_EditorStats->Draw(actualDelta);
if (m_CurrentSelection.Valid() && m_Widget.Valid()) {
(glm::vec3&)m_Widget["Transform"]["Position"] = Transform::AbsolutePosition(m_CurrentSelection.World, m_CurrentSelection.ID);
(glm::vec3&)m_Widget["Transform"]["Position"] = Transform::AbsolutePosition(m_CurrentSelection);
if (m_WidgetSpace == EditorGUI::WidgetSpace::Local) {
(glm::vec3&)m_Widget["Transform"]["Orientation"] = Transform::AbsoluteOrientationEuler(m_CurrentSelection);
} else {
(glm::vec3&)m_Widget["Transform"]["Orientation"] = glm::vec3(0, 0, 0);
}
}
m_EditorWorldSystemPipeline->Update(actualDelta);
@@ -165,6 +171,11 @@ void EditorSystem::OnComponentDelete(EntityWrapper entity, const std::string& co
}
}
void EditorSystem::OnWidgetSpace(EditorGUI::WidgetSpace widgetSpace)
{
m_WidgetSpace = widgetSpace;
}
bool EditorSystem::OnMousePress(const Events::MousePress& e)
{
ImGuiIO& io = ImGui::GetIO();
@@ -181,12 +192,18 @@ bool EditorSystem::OnMousePress(const Events::MousePress& e)
bool EditorSystem::OnWidgetDelta(const Events::WidgetDelta& e)
{
if (m_CurrentSelection.Valid()) {
glm::quat parentOrientation;
EntityWrapper parent = m_CurrentSelection.Parent();
if (parent.Valid()) {
parentOrientation = glm::inverse(Transform::AbsoluteOrientation(parent.World, parent.ID));
if (m_WidgetSpace == EditorGUI::WidgetSpace::Global) {
glm::quat parentOrientation;
EntityWrapper parent = m_CurrentSelection.Parent();
if (parent.Valid()) {
parentOrientation = glm::inverse(Transform::AbsoluteOrientation(parent));
}
(glm::vec3&)m_CurrentSelection["Transform"]["Position"] += parentOrientation * e.Translation;
} else if (m_WidgetSpace == EditorGUI::WidgetSpace::Local) {
glm::quat selectionOri = glm::quat((glm::vec3)m_CurrentSelection["Transform"]["Orientation"]);
glm::vec3 localTranslation = selectionOri * e.Translation;
(glm::vec3&)m_CurrentSelection["Transform"]["Position"] += localTranslation;
}
(glm::vec3&)m_CurrentSelection["Transform"]["Position"] += parentOrientation * e.Translation;
m_EditorGUI->SetDirty(m_CurrentSelection);
}
return true;
+7 -6
View File
@@ -24,16 +24,17 @@ void EditorWidgetSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper
Events::WidgetDelta e;
// Widget axes should have a common parent
EntityWrapper moveEntity = entity.Parent();
if (!moveEntity.Valid()) {
moveEntity = entity;
EntityWrapper widgetBase = entity.Parent();
if (!widgetBase.Valid()) {
widgetBase = entity;
}
glm::vec3 moveEntityPos = moveEntity["Transform"]["Position"];
glm::vec3 widgetBasePos = widgetBase["Transform"]["Position"];
glm::quat widgetBaseOri = glm::quat((glm::vec3)widgetBase["Transform"]["Orientation"]);
auto camera = m_PickData.Camera;
glm::vec3 axis = (glm::vec3)cEditorWidget["Axis"];
glm::vec2 axisScreen = camera->WorldToScreen(moveEntityPos + axis, m_Renderer->GetViewportSize()) - camera->WorldToScreen(moveEntityPos, m_Renderer->GetViewportSize());
ImGui::Text("axisScreen: (%f, %f)", axisScreen.x, axisScreen.y);
glm::vec3 axisOriented = widgetBaseOri * axis;
glm::vec2 axisScreen = camera->WorldToScreen(widgetBasePos + axisOriented, m_Renderer->GetViewportSize()) - camera->WorldToScreen(widgetBasePos, m_Renderer->GetViewportSize());
float dot = glm::dot(m_MouseDelta, glm::normalize(axisScreen)) / glm::length(axisScreen);
glm::vec3 worldMovement = dot * axis;