Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo functionality #184

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data-shared/textures/ui/btn_undo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data-src/ui/ui_buttons.xcf
Binary file not shown.
34 changes: 32 additions & 2 deletions src/src/game-gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

#include "game.hh"
#include "game-message.hh"
#include "gundo.hh"
#include "main.hh"
#include "settings.hh"
#include "robot.hh"
#include "object_factory.hh"
#include "i1o1gate.hh"
#include "tms/core/wdg.h"
#include "ui.hh"
#include "adventure.hh"
#include "motor.hh"
Expand Down Expand Up @@ -691,6 +694,8 @@ game::menu_handle_event(tms::event *ev)

if (found) {
if (o.e) {
undo.checkpoint("Added object");

entity *e = of::create(o.e->g_id);
if (e) {
menu_objects[gid_to_menu_pos[e->g_id]].highlighted = true;
Expand Down Expand Up @@ -1000,6 +1005,10 @@ game::widget_clicked(principia_wdg *w, uint8_t button_id, int pid)
}
return true;

case GW_UNDO:
P.add_action(ACTION_UNDO_RESTORE, 0);
return true;

case GW_MODE:
{
switch (G->get_mode()) {
Expand Down Expand Up @@ -1613,18 +1622,32 @@ game::init_gui(void)
this->wdg_layervis->priority = 800;
this->wdg_layervis->set_tooltip("Toggle layer visibility");

// TODO griffi-gh: add proper icon for the undo button
this->wdg_undo = this->wm->create_widget(
this->get_surface(), TMS_WDG_BUTTON,
GW_UNDO, AREA_TOP_LEFT,
gui_spritesheet::get_sprite(S_UNDO), 0);
this->wdg_undo->priority = 700;
const char* undo_tooltip =
"Undo"
#ifdef TMS_BACKEND_PC
" (Ctrl+Z)"
#endif
;
this->wdg_undo->set_tooltip(undo_tooltip);

this->wdg_mode = this->wm->create_widget(
this->get_surface(), TMS_WDG_BUTTON,
GW_MODE, AREA_TOP_LEFT,
gui_spritesheet::get_sprite(S_CONFIG), 0);
this->wdg_mode->priority = 700;
this->wdg_mode->priority = 600;
this->wdg_mode->set_tooltip("Change mode");

this->wdg_advanced = this->wm->create_widget(
this->get_surface(), TMS_WDG_BUTTON,
GW_ADVANCED, AREA_TOP_LEFT,
gui_spritesheet::get_sprite(S_ADVUP), 0);
this->wdg_advanced->priority = 600;
this->wdg_advanced->priority = 500;
this->wdg_advanced->set_tooltip("Toggle advanced options");

this->wdg_help = this->wm->create_widget(
Expand Down Expand Up @@ -2373,6 +2396,13 @@ game::refresh_widgets()
}
}

// XXX griffi-gh: Is this the right condition?
// Shouldn't we always show the undo button in sandbox mode? (even without advanced mode)
if (G->state.sandbox && W->is_paused() /*&& G->state.advanced_mode*/) {
this->wdg_undo->add();
this->wdg_undo->faded = undo.amount() == 0;
}

if ((this->state.sandbox || this->state.test_playing || W->is_paused() || W->is_puzzle()) && (W->level.type != LCAT_ADVENTURE || W->is_paused())) {
this->wdg_playpause->add();
}
Expand Down
46 changes: 46 additions & 0 deletions src/src/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "gravityman.hh"
#include "grid.hh"
#include "group.hh"
#include "gundo.hh"
#include "i0o1gate.hh"
#include "i1o1gate.hh"
#include "i2o1gate.hh"
Expand Down Expand Up @@ -67,6 +68,7 @@
#include "world.hh"
#include "player_activator.hh"
#include "gui.hh"
#include <cstddef>
#ifdef DEBUG
/* for print_screen_point_info */
#include "terrain.hh"
Expand Down Expand Up @@ -7183,6 +7185,37 @@ game::open_sandbox(int id_type, uint32_t id)
this->refresh_widgets();
}

void game::open_sandbox_snapshot_mem(const void* snapshot, size_t size) {
tms_assertf(this->state.sandbox,
"FATAL ERROR. Level is not a sandbox. This is a bug.\n"
"open_sandbox_snapshot_mem is only supported while in sandbox mode.\n"
"You probably tried to undo while playing a level, which shouldn't be possible");

W->lb.clear();
W->lb.ensure(size);
memcpy(W->lb.buf, snapshot, size);
W->lb.size = size;

this->reset();
this->state.sandbox = true;
W->open_internal(
size,
W->level_id_type,
W->level.local_id,
W->is_paused(),
true,
W->level.save_id,
false,
true
);

this->apply_level_properties();
this->add_entities(&W->all_entities, &W->groups, &W->connections, &W->cables);
W->begin();

this->refresh_widgets();
}

bool
game::delete_level(int id_type, uint32_t id, uint32_t save_id)
{
Expand Down Expand Up @@ -7974,6 +8007,14 @@ game::handle_input_paused(tms::event *ev, int action)
case TMS_KEY_E:
if (ev->data.key.mod & TMS_MOD_SHIFT
&& this->state.sandbox && this->selection.e) {
// Save undo state if any items are going to be modified
for (c_map::iterator it = this->pairs.begin(); it != this->pairs.end(); ++it) {
if (!it->second->typeselect) {
undo.checkpoint("Connect all");
break;
}
}

entity *saved = this->selection.e;
for (c_map::iterator it = this->pairs.begin(); it != this->pairs.end(); ++it) {
connection *c = it->second;
Expand Down Expand Up @@ -8337,6 +8378,7 @@ game::handle_input_paused(tms::event *ev, int action)
#endif
) {
tms_debugf("IMPORT (%.2f)", dist);
undo.checkpoint("Import partial");
this->import_object(this->multi.import->lvl_id);
}
} else {
Expand Down Expand Up @@ -9594,8 +9636,10 @@ game::check_click_conntype(int x, int y)
: b2Vec2(pt[this->cs_conn->layer].x, pt[this->cs_conn->layer].y);

if ((p1 - point).Length() < w) {
undo.checkpoint("Connection (Fixed)");
this->apply_connection(this->cs_conn, 0);
} else if ((p2 - point).Length() < w) {
undo.checkpoint("Connection (Rotating)");
this->apply_connection(this->cs_conn, 1);
}

Expand Down Expand Up @@ -9803,6 +9847,8 @@ game::check_click_conn(int x, int y)
return true;
}

undo.checkpoint("Connection");

if (W->is_adventure() && W->is_playing()) {
this->apply_connection(c, c->option);
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/src/game.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum {
GW_PLAYPAUSE,
GW_ORTHOGRAPHIC,
GW_LAYERVIS,
GW_UNDO,
GW_MODE,
GW_ADVANCED,
GW_HELP,
Expand Down Expand Up @@ -440,6 +441,7 @@ class game : public pscreen
principia_wdg *wdg_playpause;
principia_wdg *wdg_orthographic;
principia_wdg *wdg_layervis;
principia_wdg *wdg_undo;
principia_wdg *wdg_mode;
principia_wdg *wdg_advanced;
principia_wdg *wdg_help;
Expand Down Expand Up @@ -1105,6 +1107,12 @@ class game : public pscreen
void open_state(int id_type, uint32_t id, uint32_t save_id);
void open_sandbox(int id_type, uint32_t id);
void open_play(int id_type, uint32_t id, pkginfo *pkg, bool test_playing=false, int is_main_puzzle=0);
// Reload state of the *currently open* level from an in-memory snapshot.
// Shit WILL break in unpredictable ways if:
// - save data is compressed
// - save data comes from a different level
// - the level is not a sandbox level
void open_sandbox_snapshot_mem(const void* snapshot, size_t size);

void begin_play(bool has_state=false);

Expand Down
1 change: 1 addition & 0 deletions src/src/gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct sprite_load_data gui_spritesheet::sprites[NUM_SPRITES] = {
{ "data-shared/textures/ui/btn_ccw.png", &atlas },
{ "data-shared/textures/ui/btn_lock.png", &atlas },
{ "data-shared/textures/ui/btn_unlock.png", &atlas },
{ "data-shared/textures/ui/btn_undo.png", &atlas },

{ "data-shared/textures/menu/menu_play.png", &atlas },
{ "data-shared/textures/menu/menu_create.png", &atlas },
Expand Down
1 change: 1 addition & 0 deletions src/src/gui.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ enum {
S_CCW,
S_BTN_LOCK,
S_BTN_UNLOCK,
S_UNDO,

S_MENU_PLAY,
S_MENU_CREATE,
Expand Down
Loading
Loading