Skip to content

Commit

Permalink
- Refactored edition actions into a separate namespace for reusability
Browse files Browse the repository at this point in the history
- Direct consequence of the previous : smooth addition of the Edit Menu
  • Loading branch information
Stepland committed Mar 28, 2019
1 parent 713bb70 commit bba736d
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 108 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ set(SOURCE_FILES
SoundEffect.h
TimeSelection.h
NotesClipboard.cpp
NotesClipboard.h ChartWithHistory.cpp ChartWithHistory.h Preferences.cpp Preferences.h)
NotesClipboard.h ChartWithHistory.cpp ChartWithHistory.h Preferences.cpp Preferences.h EditActions.cpp EditActions.h)

#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${FEIS_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
Expand Down
96 changes: 96 additions & 0 deletions EditActions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Created by Syméon on 28/03/2019.
//

#include "EditActions.h"

void EditActions::undo(std::optional<EditorState>& ed, NotificationsQueue& nq) {
if (ed and ed->chart) {
auto previous = ed->chart->history.get_previous();
if (previous) {
nq.push(std::make_shared<UndoNotification>(**previous));
(*previous)->undoAction(*ed);
ed->densityGraph.should_recompute = true;
}
}
}

void EditActions::redo(std::optional<EditorState>& ed, NotificationsQueue& nq) {
if (ed and ed->chart) {
auto next = ed->chart->history.get_next();
if (next) {
nq.push(std::make_shared<RedoNotification>(**next));
(*next)->doAction(*ed);
ed->densityGraph.should_recompute = true;
}
}
}


void EditActions::cut(std::optional<EditorState>& ed, NotificationsQueue& nq) {
if (ed and ed->chart and (not ed->chart->selectedNotes.empty())) {

std::stringstream ss;
ss << "Cut " << ed->chart->selectedNotes.size() << " note";
if (ed->chart->selectedNotes.size() > 1) {
ss << "s";
}
nq.push(std::make_shared<TextNotification>(ss.str()));

ed->chart->notesClipboard.copy(ed->chart->selectedNotes);
for (auto note : ed->chart->selectedNotes) {
ed->chart->ref.Notes.erase(note);
}
ed->chart->history.push(std::make_shared<ToggledNotes>(ed->chart->selectedNotes,false));
ed->chart->selectedNotes.clear();
}
}

void EditActions::copy(std::optional<EditorState>& ed, NotificationsQueue& nq) {
if (ed and ed->chart and (not ed->chart->selectedNotes.empty())) {

std::stringstream ss;
ss << "Copied " << ed->chart->selectedNotes.size() << " note";
if (ed->chart->selectedNotes.size() > 1) {
ss << "s";
}
nq.push(std::make_shared<TextNotification>(ss.str()));

ed->chart->notesClipboard.copy(ed->chart->selectedNotes);
}
}

void EditActions::paste(std::optional<EditorState>& ed, NotificationsQueue& nq) {
if (ed and ed->chart and (not ed->chart->notesClipboard.empty())) {

int tick_offset = static_cast<int>(ed->getCurrentTick());
std::set<Note> pasted_notes = ed->chart->notesClipboard.paste(tick_offset);

std::stringstream ss;
ss << "Pasted " << pasted_notes.size() << " note";
if (pasted_notes.size() > 1) {
ss << "s";
}
nq.push(std::make_shared<TextNotification>(ss.str()));

for (auto note : pasted_notes) {
ed->chart->ref.Notes.insert(note);
}
ed->chart->selectedNotes = pasted_notes;
ed->chart->history.push(std::make_shared<ToggledNotes>(ed->chart->selectedNotes,true));
}

}

void EditActions::delete_(std::optional<EditorState>& ed, NotificationsQueue& nq) {
if (ed and ed->chart) {
if (not ed->chart->selectedNotes.empty()) {
ed->chart->history.push(std::make_shared<ToggledNotes>(ed->chart->selectedNotes,false));
nq.push(std::make_shared<TextNotification>("Deleted selected notes"));
for (auto note : ed->chart->selectedNotes) {
ed->chart->ref.Notes.erase(note);
}
ed->chart->selectedNotes.clear();
}
}
}
24 changes: 24 additions & 0 deletions EditActions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by Syméon on 28/03/2019.
//

#ifndef FEIS_EDITACTIONS_H
#define FEIS_EDITACTIONS_H

#include "NotificationsQueue.h"
#include "EditorState.h"

namespace EditActions {

void undo(std::optional<EditorState>& ed, NotificationsQueue& nq);
void redo(std::optional<EditorState>& ed, NotificationsQueue& nq);

void cut(std::optional<EditorState>& ed, NotificationsQueue& nq);
void copy(std::optional<EditorState>& ed, NotificationsQueue& nq);
void paste(std::optional<EditorState>& ed, NotificationsQueue& nq);
void delete_(std::optional<EditorState>& ed, NotificationsQueue& nq);

};


#endif //FEIS_EDITACTIONS_H
29 changes: 28 additions & 1 deletion EditorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,21 @@ saveChangesResponses EditorState::alertSaveChanges() {
}
}

/*
* Saves if asked and returns false if user canceled
*/
bool EditorState::saveChangesOrCancel() {
switch(alertSaveChanges()) {
case saveChangesYes:
ESHelper::save(*this);
case saveChangesNo:
case saveChangesDidNotDisplayDialog:
return true;
case saveChangesCancel:
return false;
}
}

/*
* This SCREAAAAMS for optimisation, but in the meantime it works !
*/
Expand Down Expand Up @@ -737,7 +752,19 @@ void ESHelper::openFromFile(std::optional<EditorState> &ed, std::filesystem::pat
}

/*
* Returns the newly created chart if there is
* returns true if user saved or if saving wasn't necessary
* returns false if user canceled
*/
bool ESHelper::saveOrCancel(std::optional<EditorState>& ed) {
if (ed) {
return ed->saveChangesOrCancel();
} else {
return true;
}
}

/*
* Returns the newly created chart if there is one
*/
std::optional<Chart> ESHelper::NewChartDialog::display(EditorState &editorState) {

Expand Down
3 changes: 3 additions & 0 deletions EditorState.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class EditorState {
void displayLinearView();

saveChangesResponses alertSaveChanges();
bool saveChangesOrCancel();

void updateVisibleNotes();
std::set<Note> visibleNotes;
Expand All @@ -119,6 +120,8 @@ namespace ESHelper {
void open(std::optional<EditorState>& ed);
void openFromFile(std::optional<EditorState>& ed, std::filesystem::path path);

bool saveOrCancel(std::optional<EditorState>& ed);

class NewChartDialog {

public:
Expand Down
Loading

0 comments on commit bba736d

Please sign in to comment.