Skip to content
13 changes: 13 additions & 0 deletions fred2/FictionViewerDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "stdafx.h"
#include "fred.h"
#include "FREDDoc.h"
#include "FictionViewerDlg.h"
#include "missionui/fictionviewer.h"
#include "gamesnd/eventmusic.h"
Expand Down Expand Up @@ -84,6 +85,9 @@ void FictionViewerDlg::OnOK()
{
UpdateData(TRUE);

const auto old_stages = Fiction_viewer_stages;
const int old_music = Mission_music[SCORE_FICTION_VIEWER];

// remove whitespace
m_story_file.Trim();
m_font_file.Trim();
Expand Down Expand Up @@ -122,6 +126,15 @@ void FictionViewerDlg::OnOK()
Mission_music[SCORE_FICTION_VIEWER] = m_fiction_music - 1;
}

const auto same_stage = [](const fiction_viewer_stage& a, const fiction_viewer_stage& b) {
return !strcmp(a.story_filename, b.story_filename) && !strcmp(a.font_filename, b.font_filename) &&
!strcmp(a.voice_filename, b.voice_filename);
};
if (Mission_music[SCORE_FICTION_VIEWER] != old_music || old_stages.size() != Fiction_viewer_stages.size() ||
(!old_stages.empty() && !same_stage(old_stages.front(), Fiction_viewer_stages.front()))) {
set_modified();
}

CDialog::OnOK();
}

Expand Down
13 changes: 13 additions & 0 deletions fred2/eventeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ void event_editor::OnButtonCancel()
audiostream_close_file(m_wave_id, 0);
m_wave_id = -1;

// copy the fields on screen into the editor's own lists first, or an edit to the selected event goes unnoticed
save();
if (query_modified()) {
int z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
if (z == IDCANCEL){
Expand All @@ -868,6 +870,17 @@ void event_editor::OnButtonCancel()
}
}

close_discarding_changes();
}

void event_editor::close_discarding_changes()
{
audiostream_close_file(m_wave_id, 0);
m_wave_id = -1;
// the media names were copied for this editor and only OK hands them to the mission
for (auto& msg : m_messages) {
message_free_media_names(msg);
}
theApp.record_window_data(&Events_wnd_data, this);
delete Event_editor_dlg;
Event_editor_dlg = NULL;
Expand Down
2 changes: 2 additions & 0 deletions fred2/eventeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class event_editor : public CDialog, public SexpTreeEditorInterface
void save_event(int e);
void move_handler(int node1, int node2, bool insert_before);
void insert_handler(int old, int node);
void close_discarding_changes();
void close_offering_changes() { OnButtonCancel(); }
int query_modified();
void OnOK(); // default MFC OK behavior
void OnCancel(); // default MFC Cancel behavior
Expand Down
62 changes: 62 additions & 0 deletions fred2/freddoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ bool CFREDDoc::autoload() {

// Load Backup.002
r = load_mission(name, MPF_FAST_RELOAD);
recreate_event_editors();
Update_window = 1;

// Delete Backup.001
Expand Down Expand Up @@ -221,6 +222,7 @@ void CFREDDoc::editor_init_mission() {
reset_mission();
SetModifiedFlag(FALSE);
recreate_dialogs();
recreate_event_editors();
}

bool CFREDDoc::load_mission(const char *pathname, int flags) {
Expand Down Expand Up @@ -410,6 +412,9 @@ bool CFREDDoc::load_mission(const char *pathname, int flags) {
Ship_editor_dialog.bypass_all--;

recreate_dialogs();
if (!(flags & MPF_FAST_RELOAD)) {
recreate_event_editors();
}

// This hook will allow for scripts to know when a mission has been loaded
// which will then allow them to update any LuaEnums that may be related to sexps
Expand Down Expand Up @@ -600,11 +605,13 @@ void CFREDDoc::OnFileImportFSM() {
{
// these aren't done automatically for imports
theApp.AddToRecentFileList((LPCTSTR)dest_path);
SetPathName((LPCTSTR)dest_path, FALSE);
SetTitle((LPCTSTR)Mission_filename);
}
}

recreate_dialogs();
recreate_event_editors();
}

BOOL CFREDDoc::OnNewDocument() {
Expand Down Expand Up @@ -799,6 +806,61 @@ void CFREDDoc::Serialize(CArchive& ar) {
*/
}

static void open_event_editor()
{
Event_editor_dlg = new event_editor;
Event_editor_dlg->Create(event_editor::IDD);
Event_editor_dlg->SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
Event_editor_dlg->ShowWindow(SW_RESTORE);
}

static void open_message_editor()
{
Message_editor_dlg = new CMessageEditorDlg;
Message_editor_dlg->Create(CMessageEditorDlg::IDD);
Message_editor_dlg->SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
Message_editor_dlg->ShowWindow(SW_RESTORE);
}

// Only when the mission is replaced, never on the reload after a save, which keeps the open editors' edits.
void CFREDDoc::recreate_event_editors() {
if (Event_editor_dlg) {
Event_editor_dlg->close_discarding_changes();
open_event_editor();
}

if (Message_editor_dlg) {
Message_editor_dlg->OnCancel();
open_message_editor();
}
}

// an editor's own close prompt deletes it on Yes and No, so a pointer still set means the user chose Cancel
BOOL CFREDDoc::SaveModified()
{
const bool had_event_editor = Event_editor_dlg != nullptr;
const bool had_message_editor = Message_editor_dlg != nullptr;

if (had_event_editor) {
Event_editor_dlg->close_offering_changes();
}
if (had_message_editor) {
Message_editor_dlg->close_offering_changes();
}
if (Event_editor_dlg != nullptr || Message_editor_dlg != nullptr) {
return FALSE;
}

const BOOL result = CDocument::SaveModified();
if (had_event_editor) {
open_event_editor();
}
if (had_message_editor) {
open_message_editor();
}
return result;
}

void CFREDDoc::recreate_dialogs() {
if (Briefing_dialog) {
Briefing_dialog->OnCancel();
Expand Down
2 changes: 2 additions & 0 deletions fred2/freddoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CFREDDoc : public CDocument
* @returns The file index of the loaded Undo item
*/
bool autoload();
void recreate_event_editors();
BOOL SaveModified() override;

/**
* @brief Read in a new mission file from disk
Expand Down
9 changes: 6 additions & 3 deletions fred2/fredview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4160,7 +4160,8 @@ void CFREDView::OnRunFreeSpace()
PROCESS_INFORMATION pi;
char *lpMsgBuf;

if (!FREDDoc_ptr->SaveModified())
// running does not replace the mission, so open editors keep their unapplied edits
if (!FREDDoc_ptr->CDocument::SaveModified())
return;

ZeroMemory(&si, sizeof(si));
Expand Down Expand Up @@ -4307,13 +4308,15 @@ void CFREDView::OnRevert()
if (!FREDDoc_ptr->SaveModified())
return;

const CString path = FREDDoc_ptr->GetPathName();
FREDDoc_ptr->DeleteContents();
FREDDoc_ptr->OnOpenDocument(NULL);
FREDDoc_ptr->OnOpenDocument(path);
}

void CFREDView::OnUpdateRevert(CCmdUI* pCmdUI)
{
pCmdUI->Enable(*Mission_filename);
// New clears only the path; a multi-file import or a failed load clears only the name
pCmdUI->Enable(*Mission_filename && !FREDDoc_ptr->GetPathName().IsEmpty());
}

BOOL CFREDView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
Expand Down
1 change: 1 addition & 0 deletions fred2/management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ void create_new_mission()
{
reset_mission();
*Mission_filename = 0;
FREDDoc_ptr->recreate_event_editors();
FREDDoc_ptr->autosave("nothing");
Undo_count = 0;
}
Expand Down
1 change: 1 addition & 0 deletions fred2/messageeditordlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CMessageEditorDlg : public CDialog
int find_event();
int query_modified();
void OnCancel();
void close_offering_changes() { OnClose(); }
int update(int num);
void update_cur_message();
void OnOK();
Expand Down
26 changes: 23 additions & 3 deletions fred2/setglobalshipflags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "stdafx.h"
#include "fred.h"
#include "SetGlobalShipFlags.h"
#include "FREDDoc.h"

#ifdef _DEBUG
#undef THIS_FILE
Expand Down Expand Up @@ -47,54 +48,73 @@ END_MESSAGE_MAP()
void SetGlobalShipFlags::OnNoShields()
{
int i;
bool changed = false;

for (i=0; i<MAX_SHIPS; i++)
{
if (Ships[i].objnum >= 0)
{
changed |= !Objects[Ships[i].objnum].flags[Object::Object_Flags::No_shields];
Objects[Ships[i].objnum].flags.set(Object::Object_Flags::No_shields);
}
}
if (changed)
set_modified();
}

void SetGlobalShipFlags::OnNoSubspaceDrive()
{
int i;
bool changed = false;

for (i=0; i<MAX_SHIPS; i++)
{
if (Ships[i].objnum >= 0)
{
// only for fighters and bombers
Ships[i].flags.set(Ship::Ship_Flags::No_subspace_drive, Ship_info[Ships[i].ship_info_index].is_fighter_bomber());
const bool value = Ship_info[Ships[i].ship_info_index].is_fighter_bomber();
changed |= Ships[i].flags[Ship::Ship_Flags::No_subspace_drive] != value;
Ships[i].flags.set(Ship::Ship_Flags::No_subspace_drive, value);
}
}
if (changed)
set_modified();
}

void SetGlobalShipFlags::OnPrimitiveSensors()
{
int i;
bool changed = false;

for (i=0; i<MAX_SHIPS; i++)
{
if (Ships[i].objnum >= 0)
{
// only for fighters and bombers
Ships[i].flags.set(Ship::Ship_Flags::Primitive_sensors, Ship_info[Ships[i].ship_info_index].is_fighter_bomber());
const bool value = Ship_info[Ships[i].ship_info_index].is_fighter_bomber();
changed |= Ships[i].flags[Ship::Ship_Flags::Primitive_sensors] != value;
Ships[i].flags.set(Ship::Ship_Flags::Primitive_sensors, value);
}
}
if (changed)
set_modified();
}

void SetGlobalShipFlags::OnAffectedByGravity()
{
int i;
bool changed = false;

for (i=0; i<MAX_SHIPS; i++)
{
if (Ships[i].objnum >= 0)
{
// only for fighters and bombers
Ships[i].flags.set(Ship::Ship_Flags::Affected_by_gravity, Ship_info[Ships[i].ship_info_index].is_fighter_bomber());
const bool value = Ship_info[Ships[i].ship_info_index].is_fighter_bomber();
changed |= Ships[i].flags[Ship::Ship_Flags::Affected_by_gravity] != value;
Ships[i].flags.set(Ship::Ship_Flags::Affected_by_gravity, value);
}
}
if (changed)
set_modified();
}
8 changes: 8 additions & 0 deletions fred2/shieldsysdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "stdafx.h"
#include "FRED.h"
#include "FREDDoc.h"
#include "ShieldSysDlg.h"
#include "ship/ship.h"
#include "mission/missionparse.h"
Expand Down Expand Up @@ -105,6 +106,7 @@ void shield_sys_dlg::OnOK()

OnSelchangeTeam();
OnSelchangeType();
bool changed = false;
for (i=0; i<MAX_SHIPS; i++)
if (Ships[i].objnum >= 0) {
z = Shield_sys_teams[Ships[i].team];
Expand All @@ -114,12 +116,18 @@ void shield_sys_dlg::OnOK()
else if (type_z == 1)
z = 1;

const bool had_no_shields = Objects[Ships[i].objnum].flags[Object::Object_Flags::No_shields];
if (!z)
Objects[Ships[i].objnum].flags.remove(Object::Object_Flags::No_shields);
else if (z == 1)
Objects[Ships[i].objnum].flags.set(Object::Object_Flags::No_shields);
if (Objects[Ships[i].objnum].flags[Object::Object_Flags::No_shields] != had_no_shields)
changed = true;
}

if (changed)
set_modified();

CDialog::OnOK();
}

Expand Down
3 changes: 3 additions & 0 deletions qtfred/src/mission/dialogs/AsteroidEditorDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ bool AsteroidEditorDialogModel::apply()
return false;
}
Asteroid_field = _a_field;
if (query_modified()) {
_editor->missionChanged();
}
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions qtfred/src/mission/dialogs/BriefingEditorDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ bool BriefingEditorDialogModel::apply()
Mission_music[SCORE_BRIEFING] = _briefingMusicIndex - 1;
strcpy_s(The_mission.substitute_briefing_music_name, _subBriefingMusic.c_str());

if (query_modified()) {
_editor->missionChanged();
}
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions qtfred/src/mission/dialogs/CommandBriefingDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ bool CommandBriefingDialogModel::apply()
Cmd_briefs[i] = _wipCommandBrief[i];
}

if (query_modified()) {
_editor->missionChanged();
}
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions qtfred/src/mission/dialogs/DebriefingDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ bool DebriefingDialogModel::apply()
Mission_music[SCORE_DEBRIEFING_AVERAGE] = _averageMusic;
Mission_music[SCORE_DEBRIEFING_FAILURE] = _failureMusic;

if (query_modified()) {
_editor->missionChanged();
}
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions qtfred/src/mission/dialogs/FictionViewerDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ bool FictionViewerDialogModel::apply() {

// Commit working copy to mission
Fiction_viewer_stages = _fictionViewerStages;
if (query_modified()) {
_editor->missionChanged();
}
return true;
}

Expand Down
Loading
Loading