Skip to content

Commit deba388

Browse files
committed
basic undo/redo for moving actors
1 parent 28431fb commit deba388

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include "DOM.hpp"
4+
#include "glm/glm.hpp"
5+
#include "history/EditorHistory.hpp"
6+
7+
struct LMat4HistoryItem : public LEditorHistoryItem
8+
{
9+
private:
10+
// The node that this history item affects.
11+
std::shared_ptr<LBGRenderDOMNode> mAffectedNode;
12+
// The delta that will be applied on undo/redo.
13+
glm::mat4 mDelta;
14+
15+
public:
16+
LMat4HistoryItem(std::shared_ptr<LBGRenderDOMNode> node, glm::mat4 delta);
17+
18+
virtual void Undo() override;
19+
virtual void Redo() override;
20+
};

include/modes/ActorMode.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class LActorMode : public LEditorModeBase
1212
std::weak_ptr<LRoomDOMNode> mManualRoomSelect;
1313

1414
bool mRoomChanged { false };
15-
15+
bool mGizmoWasUsing { false };
16+
glm::mat4 mGizmoDelta { 1.0f };
17+
1618
public:
1719
LActorMode();
1820

src/history/Mat4HistoryItem.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "history/Mat4HistoryItem.hpp"
2+
3+
LMat4HistoryItem::LMat4HistoryItem(std::shared_ptr<LBGRenderDOMNode> node, glm::mat4 delta) : mAffectedNode(node), mDelta(delta)
4+
{
5+
std::cout << "Added mat4 undo item" << std::endl;
6+
}
7+
8+
void LMat4HistoryItem::Undo()
9+
{
10+
if(mAffectedNode != nullptr) (*mAffectedNode->GetMat()) *= glm::inverse(mDelta);
11+
}
12+
13+
void LMat4HistoryItem::Redo()
14+
{
15+
if(mAffectedNode != nullptr) (*mAffectedNode->GetMat()) *= mDelta;
16+
}

src/modes/ActorMode.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "IconsForkAwesome.h"
66
#include "UIUtil.hpp"
77
#include "Options.hpp"
8+
#include "history/Mat4HistoryItem.hpp"
89

910
//#include <DiscordIntegration.hpp>
1011

@@ -138,8 +139,21 @@ void LActorMode::RenderDetailsWindow()
138139
ImGui::Separator();
139140

140141
if(mPreviousSelection == mSelectionManager.GetPrimarySelection()){
141-
if (mSelectionManager.IsMultiSelection())
142+
if (mSelectionManager.IsMultiSelection()){
142143
ImGui::Text("[Multiple Selection]");
144+
/* Some WIP multi selection
145+
ImGui::BeginTabBar("##multiSelectionTabs");
146+
147+
for(auto item : mSelectionManager.GetSelection()){
148+
if(ImGui::BeginTabItem(std::format("{}##{}", item->GetName(), item->GetID()).c_str())){
149+
std::static_pointer_cast<LUIRenderDOMNode>(item)->RenderDetailsUI(0);
150+
ImGui::EndTabItem();
151+
}
152+
}
153+
154+
ImGui::EndTabBar();
155+
*/
156+
}
143157
else if (mSelectionManager.GetPrimarySelection() != nullptr)
144158
std::static_pointer_cast<LUIRenderDOMNode>(mSelectionManager.GetPrimarySelection())->RenderDetailsUI(0);
145159
} else if(mPreviousSelection != nullptr){
@@ -197,19 +211,28 @@ void LActorMode::RenderGizmo(LEditorScene* renderer_scene){
197211

198212
if(mSelectionManager.GetPrimarySelection()->GetNodeType() != EDOMNodeType::Room){
199213
glm::mat4* m = static_cast<LBGRenderDOMNode*>(mSelectionManager.GetPrimarySelection().get())->GetMat();
200-
glm::mat4 delta(1.0);
214+
glm::mat4 delta(1.0f);
201215
if(ImGuizmo::Manipulate(&view[0][0], &proj[0][0], mGizmoMode, ImGuizmo::WORLD, &(*m)[0][0], &delta[0][0], NULL)){
216+
mGizmoDelta *= delta;
202217
for(auto node : mSelectionManager.GetSelection()){
203218
if(node != mSelectionManager.GetPrimarySelection()){
204-
(*dynamic_pointer_cast<LBGRenderDOMNode>(node)->GetMat()) = (*dynamic_pointer_cast<LBGRenderDOMNode>(node)->GetMat()) * delta;
219+
(*dynamic_pointer_cast<LBGRenderDOMNode>(node)->GetMat()) = (*dynamic_pointer_cast<LBGRenderDOMNode>(node)->GetMat()) * mGizmoDelta;
205220
}
206221
EDOMNodeType type = node->GetNodeType();
207222
if(type == EDOMNodeType::PathPoint || type == EDOMNodeType::Event || type == EDOMNodeType::Observer || type == EDOMNodeType::Object){
208223
renderer_scene->UpdateRenderers();
209224
break;
210225
}
211226
}
227+
mGizmoWasUsing = true;
228+
}
229+
230+
if(!ImGuizmo::IsUsing() && mGizmoWasUsing){
231+
mHistoryManager.AddUndoItem(std::make_shared<LMat4HistoryItem>(std::static_pointer_cast<LBGRenderDOMNode>(mSelectionManager.GetPrimarySelection()), mGizmoDelta));
232+
mGizmoDelta = glm::mat4(1.0f);
233+
mGizmoWasUsing = false;
212234
}
235+
213236
} else {
214237
std::shared_ptr<LRoomDOMNode> curRoom = dynamic_pointer_cast<LRoomDOMNode>(mSelectionManager.GetPrimarySelection());
215238
if(prevRoom != curRoom){
@@ -261,6 +284,16 @@ void LActorMode::RenderGizmo(LEditorScene* renderer_scene){
261284

262285
}
263286
}
287+
288+
if(!ImGui::GetIO().WantTextInput){
289+
if(ImGui::IsKeyDown(ImGuiKey_LeftCtrl) && ImGui::IsKeyPressed(ImGuiKey_Z)){
290+
mHistoryManager.PerformUndo();
291+
}
292+
293+
if(ImGui::IsKeyDown(ImGuiKey_LeftCtrl) && ImGui::IsKeyPressed(ImGuiKey_Y)){
294+
mHistoryManager.PerformRedo();
295+
}
296+
}
264297
}
265298

266299
void LActorMode::OnBecomeActive()

src/modes/PathMode.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ void LPathMode::RenderDetailsWindow()
157157
// Show path details
158158
else
159159
{
160-
if (mSelectionManager.IsMultiSelection())
161-
ImGui::Text("[Multiple Selection]");
162-
else if (mSelectionManager.GetPrimarySelection() != nullptr)
163-
std::static_pointer_cast<LUIRenderDOMNode>(mSelectionManager.GetPrimarySelection())->RenderDetailsUI(0);
160+
if(mPreviousSelection == mSelectionManager.GetPrimarySelection()){
161+
if (mSelectionManager.IsMultiSelection())
162+
ImGui::Text("[Multiple Selection]");
163+
else if (mSelectionManager.GetPrimarySelection() != nullptr)
164+
std::static_pointer_cast<LUIRenderDOMNode>(mSelectionManager.GetPrimarySelection())->RenderDetailsUI(0);
165+
} else if(mPreviousSelection != nullptr){
166+
std::static_pointer_cast<LUIRenderDOMNode>(mPreviousSelection)->RenderDetailsUI(0);
167+
}
164168
}
165169

166170
ImGui::End();

0 commit comments

Comments
 (0)