|
| 1 | +#include "PreferencesDialogModel.h" |
| 2 | + |
| 3 | +#include "mission/EditorViewport.h" |
| 4 | +#include "mission/missiongrid.h" |
| 5 | +#include "math/vecmat.h" |
| 6 | + |
| 7 | +namespace fso::fred::dialogs { |
| 8 | + |
| 9 | +PreferencesDialogModel::PreferencesDialogModel(QObject* parent, EditorViewport* viewport) |
| 10 | + : AbstractDialogModel(parent, viewport) |
| 11 | + , _moveShipsWhenUndocking(viewport->Move_ships_when_undocking) |
| 12 | + , _alwaysSaveDisplayNames(viewport->Always_save_display_names) |
| 13 | + , _errorCheckerChecksForPotentialIssues(viewport->Error_checker_checks_potential_issues) |
| 14 | + , _showSexpHelpMissionEvents(viewport->Show_sexp_help_mission_events) |
| 15 | + , _showSexpHelpMissionGoals(viewport->Show_sexp_help_mission_goals) |
| 16 | + , _showSexpHelpMissionCutscenes(viewport->Show_sexp_help_mission_cutscenes) |
| 17 | + , _showSexpHelpShipEditor(viewport->Show_sexp_help_ship_editor) |
| 18 | + , _showSexpHelpWingEditor(viewport->Show_sexp_help_wing_editor) |
| 19 | + , _gridCenterX(static_cast<int>(viewport->The_grid->center.xyz.x)) |
| 20 | + , _gridCenterY(static_cast<int>(viewport->The_grid->center.xyz.y)) |
| 21 | + , _gridCenterZ(static_cast<int>(viewport->The_grid->center.xyz.z)) |
| 22 | +{ |
| 23 | + auto& bindings = ControlBindings::instance(); |
| 24 | + for (const auto& def : bindings.definitions()) { |
| 25 | + _controlKeys.emplace(def.action, bindings.keyFor(def.action)); |
| 26 | + } |
| 27 | + |
| 28 | + // Detect current grid plane from the normal vector (uvec) |
| 29 | + const auto& uvec = viewport->The_grid->gmatrix.vec.uvec; |
| 30 | + if (uvec.xyz.y != 0.0f) { |
| 31 | + _gridPlane = GridPlane::XZ; |
| 32 | + } else if (uvec.xyz.z != 0.0f) { |
| 33 | + _gridPlane = GridPlane::XY; |
| 34 | + } else { |
| 35 | + _gridPlane = GridPlane::YZ; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +bool PreferencesDialogModel::apply() { |
| 40 | + _viewport->Move_ships_when_undocking = _moveShipsWhenUndocking; |
| 41 | + _viewport->Always_save_display_names = _alwaysSaveDisplayNames; |
| 42 | + _viewport->Error_checker_checks_potential_issues = _errorCheckerChecksForPotentialIssues; |
| 43 | + _viewport->Show_sexp_help_mission_events = _showSexpHelpMissionEvents; |
| 44 | + _viewport->Show_sexp_help_mission_goals = _showSexpHelpMissionGoals; |
| 45 | + _viewport->Show_sexp_help_mission_cutscenes = _showSexpHelpMissionCutscenes; |
| 46 | + _viewport->Show_sexp_help_ship_editor = _showSexpHelpShipEditor; |
| 47 | + _viewport->Show_sexp_help_wing_editor = _showSexpHelpWingEditor; |
| 48 | + |
| 49 | + _viewport->saveSettings(); |
| 50 | + |
| 51 | + auto& bindings = ControlBindings::instance(); |
| 52 | + for (const auto& entry : _controlKeys) { |
| 53 | + bindings.setKey(entry.first, entry.second); |
| 54 | + } |
| 55 | + bindings.save(); |
| 56 | + |
| 57 | + _viewport->The_grid->center.xyz.x = static_cast<float>(_gridCenterX); |
| 58 | + _viewport->The_grid->center.xyz.y = static_cast<float>(_gridCenterY); |
| 59 | + _viewport->The_grid->center.xyz.z = static_cast<float>(_gridCenterZ); |
| 60 | + |
| 61 | + switch (_gridPlane) { |
| 62 | + case GridPlane::XY: |
| 63 | + _viewport->The_grid->gmatrix.vec.fvec = vmd_x_vector; |
| 64 | + _viewport->The_grid->gmatrix.vec.rvec = vmd_y_vector; |
| 65 | + break; |
| 66 | + case GridPlane::XZ: |
| 67 | + _viewport->The_grid->gmatrix.vec.fvec = vmd_x_vector; |
| 68 | + _viewport->The_grid->gmatrix.vec.rvec = vmd_z_vector; |
| 69 | + break; |
| 70 | + case GridPlane::YZ: |
| 71 | + _viewport->The_grid->gmatrix.vec.fvec = vmd_y_vector; |
| 72 | + _viewport->The_grid->gmatrix.vec.rvec = vmd_z_vector; |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + modify_grid(_viewport->The_grid); |
| 77 | + |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +void PreferencesDialogModel::reject() { |
| 82 | + // Nothing to do — data sources are not modified until apply() |
| 83 | +} |
| 84 | + |
| 85 | +bool PreferencesDialogModel::getMoveShipsWhenUndocking() const { return _moveShipsWhenUndocking; } |
| 86 | +void PreferencesDialogModel::setMoveShipsWhenUndocking(bool value) { modify(_moveShipsWhenUndocking, value); } |
| 87 | + |
| 88 | +bool PreferencesDialogModel::getAlwaysSaveDisplayNames() const { return _alwaysSaveDisplayNames; } |
| 89 | +void PreferencesDialogModel::setAlwaysSaveDisplayNames(bool value) { modify(_alwaysSaveDisplayNames, value); } |
| 90 | + |
| 91 | +bool PreferencesDialogModel::getErrorCheckerChecksForPotentialIssues() const { return _errorCheckerChecksForPotentialIssues; } |
| 92 | +void PreferencesDialogModel::setErrorCheckerChecksForPotentialIssues(bool value) { modify(_errorCheckerChecksForPotentialIssues, value); } |
| 93 | + |
| 94 | +bool PreferencesDialogModel::getShowSexpHelpMissionEvents() const { return _showSexpHelpMissionEvents; } |
| 95 | +void PreferencesDialogModel::setShowSexpHelpMissionEvents(bool value) { modify(_showSexpHelpMissionEvents, value); } |
| 96 | +bool PreferencesDialogModel::getShowSexpHelpMissionGoals() const { return _showSexpHelpMissionGoals; } |
| 97 | +void PreferencesDialogModel::setShowSexpHelpMissionGoals(bool value) { modify(_showSexpHelpMissionGoals, value); } |
| 98 | +bool PreferencesDialogModel::getShowSexpHelpMissionCutscenes() const { return _showSexpHelpMissionCutscenes; } |
| 99 | +void PreferencesDialogModel::setShowSexpHelpMissionCutscenes(bool value) { modify(_showSexpHelpMissionCutscenes, value); } |
| 100 | +bool PreferencesDialogModel::getShowSexpHelpShipEditor() const { return _showSexpHelpShipEditor; } |
| 101 | +void PreferencesDialogModel::setShowSexpHelpShipEditor(bool value) { modify(_showSexpHelpShipEditor, value); } |
| 102 | +bool PreferencesDialogModel::getShowSexpHelpWingEditor() const { return _showSexpHelpWingEditor; } |
| 103 | +void PreferencesDialogModel::setShowSexpHelpWingEditor(bool value) { modify(_showSexpHelpWingEditor, value); } |
| 104 | + |
| 105 | +QKeySequence PreferencesDialogModel::getControlKey(ControlAction action) const { |
| 106 | + auto it = _controlKeys.find(action); |
| 107 | + Assertion(it != _controlKeys.end(), "Unknown control action!"); |
| 108 | + return it->second; |
| 109 | +} |
| 110 | + |
| 111 | +void PreferencesDialogModel::setControlKey(ControlAction action, const QKeySequence& sequence) { |
| 112 | + auto it = _controlKeys.find(action); |
| 113 | + Assertion(it != _controlKeys.end(), "Unknown control action!"); |
| 114 | + modify(it->second, sequence); |
| 115 | +} |
| 116 | + |
| 117 | +void PreferencesDialogModel::resetControlDefaults() { |
| 118 | + auto& bindings = ControlBindings::instance(); |
| 119 | + for (const auto& def : bindings.definitions()) { |
| 120 | + modify(_controlKeys[def.action], def.defaultKey); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +int PreferencesDialogModel::getGridCenterX() const { return _gridCenterX; } |
| 125 | +int PreferencesDialogModel::getGridCenterY() const { return _gridCenterY; } |
| 126 | +int PreferencesDialogModel::getGridCenterZ() const { return _gridCenterZ; } |
| 127 | +void PreferencesDialogModel::setGridCenterX(int value) { modify(_gridCenterX, value); } |
| 128 | +void PreferencesDialogModel::setGridCenterY(int value) { modify(_gridCenterY, value); } |
| 129 | +void PreferencesDialogModel::setGridCenterZ(int value) { modify(_gridCenterZ, value); } |
| 130 | + |
| 131 | +GridPlane PreferencesDialogModel::getGridPlane() const { return _gridPlane; } |
| 132 | +void PreferencesDialogModel::setGridPlane(GridPlane plane) { modify(_gridPlane, plane); } |
| 133 | + |
| 134 | +void PreferencesDialogModel::resetGrid() { |
| 135 | + modify(_gridCenterX, 0); |
| 136 | + modify(_gridCenterY, 0); |
| 137 | + modify(_gridCenterZ, 0); |
| 138 | + modify(_gridPlane, GridPlane::XZ); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace fso::fred::dialogs |
0 commit comments