Skip to content

Commit

Permalink
keep selection on restore
Browse files Browse the repository at this point in the history
  • Loading branch information
griffi-gh committed Oct 16, 2024
1 parent 2a049ed commit 2a77e40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/src/gundo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "game.hh"
#include "tms/backend/print.h"
#include "world.hh"
#include <cstdint>
#include <cstdlib>

#ifdef DEBUG
Expand Down Expand Up @@ -75,14 +76,25 @@ void undo_stack::checkpoint(const char *reason, void *snapshot /* = nullptr */)
);
}

const char* undo_stack::restore(bool keep_cam_pos /* = true */) {
const char* undo_stack::restore(uint8_t flags) {
// Save the current camera position
float cx, cy, cz;
if (keep_cam_pos) {
if (flags & UNDO_KEEP_CAM_POS) {
cx = G->cam->_position.x;
cy = G->cam->_position.y;
cz = G->cam->_position.z;
}

// Save the current selection
uint32_t saved_selection_id = 0;
if (flags & UNDO_KEEP_SELECTION) {
if (G->selection.e != nullptr) {
saved_selection_id = G->selection.e->id;
} else {
tms_warnf("undo_restore: no selection to keep");
}
}

if (this->items.size() == 0) {
tms_fatalf("undo_load: no items to load");
}
Expand All @@ -95,8 +107,19 @@ const char* undo_stack::restore(bool keep_cam_pos /* = true */) {

free(item.data);

if (keep_cam_pos) {
if (flags & UNDO_KEEP_CAM_POS) {
G->cam->set_position(cx, cy, cz);
}
if (flags & UNDO_KEEP_SELECTION) {
G->selection.disable();
if (saved_selection_id) {
if (entity *e = W->get_entity_by_id(saved_selection_id)) {
G->selection.select(e);
} else {
tms_warnf("undo_restore: selection entity not found");
}
}
}

return item.reason;
}
10 changes: 9 additions & 1 deletion src/src/gundo.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <vector>

#define MAX_UNDO_ITEMS 100
Expand All @@ -11,6 +12,13 @@ struct undo_item {
size_t size;
};

enum {
UNDO_KEEP_NONE = 0b00,
UNDO_KEEP_CAM_POS = 0b01,
UNDO_KEEP_SELECTION = 0b10,
UNDO_KEEP_ALL = 0b11,
};

struct undo_stack {
private:
std::vector<undo_item> items;
Expand Down Expand Up @@ -39,7 +47,7 @@ struct undo_stack {
// Returns the reason for the last checkpoint
//
// Avoid using this function directly, use P.add_action(ACTION_UNDO_RESTORE, 0) instead
const char* restore(bool keep_cam_pos = true);
const char* restore(uint8_t flags = UNDO_KEEP_ALL);
};

extern struct undo_stack undo;
Expand Down

0 comments on commit 2a77e40

Please sign in to comment.