From 483934415fc01a8a8c6600e96f8f14bf6ec2a826 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 13:42:41 +0100 Subject: [PATCH 01/12] QtFRED: move docked ships together object_moved() set the Docked_already_handled flag on all objects before it called dock_move_docked_objects(). Thus that function returned immediately, and the docked partners of a moved ship stayed in place. FRED2 removes the flag. QtFRED now does the same. Some paths did not call object_moved() at all: - The transform toolbar did not call it. - A cancelled drag restored only the marked objects. - The Orient editor called it only when the position changed, not for a rotation. - An undock with "move ships when undocking" moved one ship, but not its other docked partners. The partners now move after the undock, so that the ship that stays does not move. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/mission/EditorViewport.cpp | 6 ++++++ .../mission/dialogs/ObjectOrientEditorDialogModel.cpp | 4 ++-- .../ShipEditor/ShipInitialStatusDialogModel.cpp | 8 ++++++++ qtfred/src/mission/object.cpp | 2 +- qtfred/src/ui/FredView.cpp | 10 ++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/qtfred/src/mission/EditorViewport.cpp b/qtfred/src/mission/EditorViewport.cpp index e4ffec3bc0e..5f23e74f9f4 100644 --- a/qtfred/src/mission/EditorViewport.cpp +++ b/qtfred/src/mission/EditorViewport.cpp @@ -1756,6 +1756,12 @@ void EditorViewport::cancel_drag() { objp = GET_NEXT(objp); } + // Put the docked partners back with the restored objects + for (objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp)) { + if (objp->flags[Object::Object_Flags::Marked]) + object_moved(objp); + } + button_down = false; moved = false; Dup_drag = 0; diff --git a/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp b/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp index c14044f7e35..a1490c1b8b9 100644 --- a/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp @@ -237,8 +237,8 @@ bool ObjectOrientEditorDialogModel::apply() } } - // Notify the engine about moved objects - if (change_pos || relativeToOrigin) { + // Notify the engine about moved objects. A rotation also moves the docked partners. + if (change_pos || change_orient || _pointTo || relativeToOrigin) { for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) { if (ptr->flags[Object::Object_Flags::Marked]) { object_moved(ptr); diff --git a/qtfred/src/mission/dialogs/ShipEditor/ShipInitialStatusDialogModel.cpp b/qtfred/src/mission/dialogs/ShipEditor/ShipInitialStatusDialogModel.cpp index 13aaadde5a9..b96cdbe571e 100644 --- a/qtfred/src/mission/dialogs/ShipEditor/ShipInitialStatusDialogModel.cpp +++ b/qtfred/src/mission/dialogs/ShipEditor/ShipInitialStatusDialogModel.cpp @@ -336,20 +336,28 @@ void ShipInitialStatusDialogModel::undock(object* objp1, object* objp2) ship_num = get_ship_from_obj(OBJ_INDEX(objp1)); other_ship_num = get_ship_from_obj(OBJ_INDEX(objp2)); + object* moved_objp = nullptr; if (_moveShipsWhenUndocking) { if (ship_class_compare(Ships[ship_num].ship_info_index, Ships[other_ship_num].ship_info_index) <= 0) { vm_vec_scale_add2(&objp2->pos, &v, ship_class_get_length(&Ship_info[Ships[objp2->instance].ship_info_index])); + moved_objp = objp2; } else { vm_vec_scale_add2(&objp1->pos, &v, ship_class_get_length(&Ship_info[Ships[objp1->instance].ship_info_index]) * -1.0f); + moved_objp = objp1; } } ai_do_objects_undocked_stuff(objp1, objp2); + // The moved ship can have other docked partners. Move them after the undock, so that the + // ship that stays does not move too. + if (moved_objp != nullptr) + object_moved(moved_objp); + // check to see if one of these ships has an arrival cue of false. If so, then // reset it back to default value of true. be sure to correctly update before // and after setting data. diff --git a/qtfred/src/mission/object.cpp b/qtfred/src/mission/object.cpp index c7b58bc4e2f..c4a6781e8cf 100644 --- a/qtfred/src/mission/object.cpp +++ b/qtfred/src/mission/object.cpp @@ -20,7 +20,7 @@ void object_moved(object *objp) { // reset the already-handled flag (inefficient, but it's FRED, so who cares) for (int i = 0; i < MAX_OBJECTS; i++) - Objects[i].flags.set(Object::Object_Flags::Docked_already_handled); + Objects[i].flags.remove(Object::Object_Flags::Docked_already_handled); // move all docked objects docked to me dock_move_docked_objects(objp); diff --git a/qtfred/src/ui/FredView.cpp b/qtfred/src/ui/FredView.cpp index a17fef0d342..b0c8d4b7337 100644 --- a/qtfred/src/ui/FredView.cpp +++ b/qtfred/src/ui/FredView.cpp @@ -1624,6 +1624,16 @@ void FredView::onTransformEditingFinished() { } } + // Move the docked partners with the changed objects + if (isMulti) { + for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { + if (p->flags[Object::Object_Flags::Marked]) + object_moved(p); + } + } else { + object_moved(&Objects[curObj]); + } + fred->missionChanged(); } From 2c305c0acdf1e3134a299c684c2d478888408360 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 13:44:18 +0100 Subject: [PATCH 02/12] QtFRED: skip locked objects in the selection commands A click and a box selection in the viewport ignore the objects that Lock Marked Objects locked. Other selection commands still marked the locked objects, so the user could then move or change them. Now these commands skip the locked objects: - Next and previous object. Before, a locked object also stopped the cycle. - Select All and Invert Selection in the Scene Browser. - Group selection. - The Select Wing commands: Mark Wing in the menu, and Select Wing in the context menu and in the context toolbar. A selection by name still marks a locked object, for example in the Scene Browser tree, with Next and Prev in the editor dialogs, or in a context menu. FRED2 does the same. A check in Editor::markObject() would break the callers that expect the mark to succeed. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/mission/Editor.cpp | 27 +++++++++++++++++++ qtfred/src/mission/Editor.h | 3 +++ .../src/mission/dialogs/SceneBrowserModel.cpp | 5 +++- qtfred/src/ui/FredView.cpp | 14 +++++----- qtfred/src/ui/FredView.h | 1 + 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/qtfred/src/mission/Editor.cpp b/qtfred/src/mission/Editor.cpp index aa6b37a05be..bc2dbac7065 100644 --- a/qtfred/src/mission/Editor.cpp +++ b/qtfred/src/mission/Editor.cpp @@ -1727,11 +1727,28 @@ void Editor::select_next_object() } setupCurrentObjectIndices(OBJ_INDEX(ptr)); } else { + // Skip the locked objects. Like a click in the viewport, this command does not select them. + const object* start = ptr; + while (ptr->flags[Object::Object_Flags::Locked_from_editing]) { + ptr = GET_NEXT(ptr); + if (ptr == END_OF_LIST(&obj_used_list)) + ptr = GET_NEXT(ptr); + if (ptr == start) + return; // all objects are locked + } + if (getNumMarked()) unmarkObject(currentObject); markObject(OBJ_INDEX(ptr)); } } +void Editor::unmarkLockedObjects() +{ + for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) { + if (ptr->flags[Object::Object_Flags::Marked] && ptr->flags[Object::Object_Flags::Locked_from_editing]) + unmarkObject(OBJ_INDEX(ptr)); + } +} void Editor::select_previous_object() { int arr[MAX_OBJECTS], i = 0, n = 0; @@ -1766,6 +1783,16 @@ void Editor::select_previous_object() } setupCurrentObjectIndices(arr[i]); } else { + // Skip the locked objects. Like a click in the viewport, this command does not select them. + const int start = i; + while (Objects[arr[i]].flags[Object::Object_Flags::Locked_from_editing]) { + i--; + if (i < 0) + i = n - 1; + if (i == start) + return; // all objects are locked + } + if (getNumMarked()) unmarkObject(currentObject); markObject(arr[i]); diff --git a/qtfred/src/mission/Editor.h b/qtfred/src/mission/Editor.h index be5c65ca992..5566c799bc1 100644 --- a/qtfred/src/mission/Editor.h +++ b/qtfred/src/mission/Editor.h @@ -209,6 +209,9 @@ class Editor : public QObject { * @param[in] wing Index of the wing to mark */ void mark_wing(int wing); + // Unmark the marked objects that are locked. The Select Wing commands call this function after mark_wing(), + // because like a click in the viewport, they do not select a locked object. + void unmarkLockedObjects(); bool query_single_wing_marked(); diff --git a/qtfred/src/mission/dialogs/SceneBrowserModel.cpp b/qtfred/src/mission/dialogs/SceneBrowserModel.cpp index f2037901b7c..ee37f8d192f 100644 --- a/qtfred/src/mission/dialogs/SceneBrowserModel.cpp +++ b/qtfred/src/mission/dialogs/SceneBrowserModel.cpp @@ -416,6 +416,9 @@ void SceneBrowserModel::selectAll() if (!_filterIff.isEmpty() && team >= 0 && team < _filterIff.size() && !_filterIff[team]) continue; } + // Like a click in the viewport, Select All does not select a locked object + if (ptr->flags[Object::Object_Flags::Locked_from_editing]) + continue; _editor->markObject(OBJ_INDEX(ptr)); } _updatingFromBrowser = false; @@ -442,7 +445,7 @@ void SceneBrowserModel::invertSelection() int objNum = OBJ_INDEX(ptr); if (ptr->flags[Object::Object_Flags::Marked]) _editor->unmarkObject(objNum); - else + else if (!ptr->flags[Object::Object_Flags::Locked_from_editing]) _editor->markObject(objNum); } _updatingFromBrowser = false; diff --git a/qtfred/src/ui/FredView.cpp b/qtfred/src/ui/FredView.cpp index b0c8d4b7337..2fc4fde799f 100644 --- a/qtfred/src/ui/FredView.cpp +++ b/qtfred/src/ui/FredView.cpp @@ -1188,9 +1188,7 @@ void FredView::onUpdateContextToolbar() { addBtn(tr("Edit Wing"), &FredView::on_actionWings_triggered); auto* selWingAct = new QAction(tr("Select Wing"), _contextToolBar); int capturedWing = effectiveWingNum; - connect(selWingAct, &QAction::triggered, this, [this, capturedWing]() { - fred->mark_wing(capturedWing); - }); + connect(selWingAct, &QAction::triggered, this, [this, capturedWing]() { selectWing(capturedWing); }); _contextToolBar->addAction(selWingAct); } } else if (effectiveType == OBJ_WAYPOINT) { @@ -2060,7 +2058,7 @@ void FredView::initializePopupMenus() { if (query_valid_object(obj) && (Objects[obj].type == OBJ_SHIP || Objects[obj].type == OBJ_START)) { int wing = Ships[Objects[obj].instance].wingnum; if (wing >= 0) { - fred->mark_wing(wing); + selectWing(wing); } } }); @@ -2947,7 +2945,7 @@ void FredView::onGroupSelected(int group) { auto objp = GET_FIRST(&obj_used_list); while (objp != END_OF_LIST(&obj_used_list)) { if (objp->type == OBJ_SHIP) { - if (Ships[objp->instance].group & group) { + if ((Ships[objp->instance].group & group) && !objp->flags[Object::Object_Flags::Locked_from_editing]) { fred->markObject(OBJ_INDEX(objp)); } } @@ -2955,6 +2953,10 @@ void FredView::onGroupSelected(int group) { objp = GET_NEXT(objp); } } +void FredView::selectWing(int wing) { + fred->mark_wing(wing); + fred->unmarkLockedObjects(); +} void FredView::onSetGroup(int group) { bool err = false; @@ -3009,7 +3011,7 @@ void FredView::on_actionPrev_Object_triggered(bool) { } void FredView::on_actionMark_Wing_triggered(bool) { if (fred->cur_wing != -1) { - fred->mark_wing(fred->cur_wing); + selectWing(fred->cur_wing); } } void FredView::on_actionError_Checker_triggered(bool) { diff --git a/qtfred/src/ui/FredView.h b/qtfred/src/ui/FredView.h index 6f738721ed1..ab15c28d02b 100644 --- a/qtfred/src/ui/FredView.h +++ b/qtfred/src/ui/FredView.h @@ -247,6 +247,7 @@ class FredView: public QMainWindow, public IDialogProvider { void populateCreatePropSubmenu(); void openLayerManagerDialog(); void ensureViewportFocus(); + void selectWing(int wing); void enforceSideDockAreas(); void onGroupSelected(int group); From 2b5990e75e46f83bb746c76e81335dd70c8b3f5a Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 13:45:43 +0100 Subject: [PATCH 03/12] Editors: use one field of view for the viewport The QtFRED projection matrix used a field of view of 0.485, but the g3 view matrix used 0.5. The labels, the mouse picking, and Zoom Extents use the g3 view matrix, so they did not align with the rendered objects. The offset increased at the edges of the viewport. The new constant EDITOR_DEFAULT_FOV in missioneditor/common.h replaces the two copies of FRED_DEFAULT_HTL_FOV and the 0.5 literals: - QtFRED: the projection matrix, the view matrix that FredRenderer sets before and after it draws the scene, Zoom Extents, and the frame that the briefing map gives back to the main viewport. - FRED2: the projection and view matrices (already 0.485), Zoom Extents, and the frame at initialization. Co-Authored-By: Claude Opus 5.5 --- code/missioneditor/common.h | 4 ++++ fred2/fredrender.cpp | 7 +++---- fred2/fredview.cpp | 6 +++--- fred2/management.cpp | 2 +- qtfred/src/mission/EditorViewport.cpp | 6 +++--- qtfred/src/mission/FredRenderer.cpp | 8 ++++---- qtfred/src/ui/widgets/BriefingMapWidget.cpp | 3 ++- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/code/missioneditor/common.h b/code/missioneditor/common.h index 698f4d88b68..b2f56dd5d93 100644 --- a/code/missioneditor/common.h +++ b/code/missioneditor/common.h @@ -9,6 +9,10 @@ constexpr float DEFAULT_NEBULA_RANGE = 3000.0f; // Smallest meaningful change in an orientation input field (degrees) constexpr float ORIENT_INPUT_THRESHOLD = 0.01f; +// Field of view (zoom) of the editor viewport. The projection matrix and the g3 view matrix must use the same +// value. If they do not, the labels and the mouse picking do not align with the rendered objects. +constexpr float EDITOR_DEFAULT_FOV = 0.485f; + // Normalize a degree value into the range [-180, 180] float normalize_degrees(float deg); diff --git a/fred2/fredrender.cpp b/fred2/fredrender.cpp index 8e0ac6902d4..24832273d21 100644 --- a/fred2/fredrender.cpp +++ b/fred2/fredrender.cpp @@ -79,7 +79,6 @@ static char THIS_FILE[] = __FILE__; #define FRED_COLOUR_WHITE 0xffffff #define FRED_COLOUR_YELLOW_GREEN 0xc8ff00 -const float FRED_DEFAULT_HTL_FOV = 0.485f; const float FRED_BRIEFING_HTL_FOV = 0.325f; const float FRED_DEAFULT_HTL_DRAW_DIST = 300000.0f; @@ -844,7 +843,7 @@ void fred_disable_htl() { } void fred_enable_htl() { - gr_set_proj_matrix((4.0f / 9.0f) * PI * (Briefing_dialog ? Briefing_window_FOV : FRED_DEFAULT_HTL_FOV), gr_screen.aspect*(float) gr_screen.clip_width / (float) gr_screen.clip_height, 1.0f, FRED_DEAFULT_HTL_DRAW_DIST); + gr_set_proj_matrix((4.0f / 9.0f) * PI * (Briefing_dialog ? Briefing_window_FOV : EDITOR_DEFAULT_FOV), gr_screen.aspect*(float) gr_screen.clip_width / (float) gr_screen.clip_height, 1.0f, FRED_DEAFULT_HTL_DRAW_DIST); gr_set_view_matrix(&Eye_position, &Eye_matrix); } @@ -1712,7 +1711,7 @@ void render_frame() { font::set_font(font::FONT1); light_reset(); - g3_set_view_matrix(&eye_pos, &eye_orient, (Briefing_dialog ? Briefing_window_FOV : FRED_DEFAULT_HTL_FOV)); + g3_set_view_matrix(&eye_pos, &eye_orient, (Briefing_dialog ? Briefing_window_FOV : EDITOR_DEFAULT_FOV)); Viewer_pos = eye_pos; // for starfield code fred_enable_htl(); @@ -1821,7 +1820,7 @@ void render_frame() { gr_set_clip(0, 0, True_rw, True_rh); g3_start_frame(0); // ** Accounted for - g3_set_view_matrix(&eye_pos, &eye_orient, (Briefing_dialog ? Briefing_window_FOV : FRED_DEFAULT_HTL_FOV)); + g3_set_view_matrix(&eye_pos, &eye_orient, (Briefing_dialog ? Briefing_window_FOV : EDITOR_DEFAULT_FOV)); } void render_models(void) { diff --git a/fred2/fredview.cpp b/fred2/fredview.cpp index 15b7f251aff..2d35a449c94 100644 --- a/fred2/fredview.cpp +++ b/fred2/fredview.cpp @@ -2317,7 +2317,7 @@ void view_universe(int just_marked) dist = std::max(radius, fl_sqrt(20.0f)) + 1.0f; vm_vec_scale_add(&view_pos, ¢er, &view_orient.vec.fvec, -dist); - g3_set_view_matrix(&view_pos, &view_orient, 0.5f); + g3_set_view_matrix(&view_pos, &view_orient, EDITOR_DEFAULT_FOV); ptr = GET_FIRST(&obj_used_list); while (ptr != END_OF_LIST(&obj_used_list)) { @@ -2330,7 +2330,7 @@ void view_universe(int just_marked) while (v.codes & CC_OFF) { // is point off screen? dist += 5.0f; // zoom out a little and check again. vm_vec_scale_add(&view_pos, ¢er, &view_orient.vec.fvec, -dist); - g3_set_view_matrix(&view_pos, &view_orient, 0.5f); + g3_set_view_matrix(&view_pos, &view_orient, EDITOR_DEFAULT_FOV); g3_rotate_vertex(&v, &ptr->pos); if (g3_project_vertex(&v) & PF_OVERFLOW) Int3(); @@ -2342,7 +2342,7 @@ void view_universe(int just_marked) dist *= 1.1f; vm_vec_scale_add(&view_pos, ¢er, &view_orient.vec.fvec, -dist); - g3_set_view_matrix(&view_pos, &view_orient, 0.5f); + g3_set_view_matrix(&view_pos, &view_orient, EDITOR_DEFAULT_FOV); Update_window = 1; } diff --git a/fred2/management.cpp b/fred2/management.cpp index 1c7f7ff0800..8d408a6456a 100644 --- a/fred2/management.cpp +++ b/fred2/management.cpp @@ -470,7 +470,7 @@ bool fred_init(std::unique_ptr&& graphicsOps) gr_reset_clip(); g3_start_frame(0); - g3_set_view_matrix(&eye_pos, &eye_orient, 0.5f); + g3_set_view_matrix(&eye_pos, &eye_orient, EDITOR_DEFAULT_FOV); Fred_main_wnd -> init_tools(); diff --git a/qtfred/src/mission/EditorViewport.cpp b/qtfred/src/mission/EditorViewport.cpp index 5f23e74f9f4..f5b9f731dda 100644 --- a/qtfred/src/mission/EditorViewport.cpp +++ b/qtfred/src/mission/EditorViewport.cpp @@ -1778,7 +1778,7 @@ void EditorViewport::view_universe(bool just_marked) { dist = std::max(radius, fl_sqrt(20.0f)) + 1.0f; vm_vec_scale_add(&camera.view_pos, ¢er, &camera.view_orient.vec.fvec, -dist); - g3_set_view_matrix(&camera.view_pos, &camera.view_orient, 0.5f); + g3_set_view_matrix(&camera.view_pos, &camera.view_orient, EDITOR_DEFAULT_FOV); ptr = GET_FIRST(&obj_used_list); while (ptr != END_OF_LIST(&obj_used_list)) { @@ -1791,7 +1791,7 @@ void EditorViewport::view_universe(bool just_marked) { while (v.codes & CC_OFF) { dist += 5.0f; vm_vec_scale_add(&camera.view_pos, ¢er, &camera.view_orient.vec.fvec, -dist); - g3_set_view_matrix(&camera.view_pos, &camera.view_orient, 0.5f); + g3_set_view_matrix(&camera.view_pos, &camera.view_orient, EDITOR_DEFAULT_FOV); g3_rotate_vertex(&v, &ptr->pos); if (g3_project_vertex(&v) & PF_OVERFLOW) Int3(); @@ -1803,7 +1803,7 @@ void EditorViewport::view_universe(bool just_marked) { dist *= 1.1f; vm_vec_scale_add(&camera.view_pos, ¢er, &camera.view_orient.vec.fvec, -dist); - g3_set_view_matrix(&camera.view_pos, &camera.view_orient, 0.5f); + g3_set_view_matrix(&camera.view_pos, &camera.view_orient, EDITOR_DEFAULT_FOV); needsUpdate(); } diff --git a/qtfred/src/mission/FredRenderer.cpp b/qtfred/src/mission/FredRenderer.cpp index 84b1abcd938..67b904b8eb2 100644 --- a/qtfred/src/mission/FredRenderer.cpp +++ b/qtfred/src/mission/FredRenderer.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "mission/object.h" #include "prop/prop.h" @@ -36,14 +37,13 @@ namespace { const float CONVERT_DEGREES = 57.29578f; // conversion factor from radians to degrees -const float FRED_DEFAULT_HTL_FOV = 0.485f; const float FRED_DEAFULT_HTL_DRAW_DIST = 300000.0f; const int FRED_COLOUR_WHITE = 0xffffff; const int FRED_COLOUR_YELLOW_GREEN = 0xc8ff00; void enable_htl() { - gr_set_proj_matrix((4.0f / 9.0f) * PI * FRED_DEFAULT_HTL_FOV, + gr_set_proj_matrix((4.0f / 9.0f) * PI * EDITOR_DEFAULT_FOV, gr_screen.aspect * static_cast(gr_screen.clip_width) / static_cast(gr_screen.clip_height), 1.0f, @@ -993,7 +993,7 @@ void FredRenderer::render_frame(int cur_object_index, font::set_font(font::FONT1); light_reset(); - g3_set_view_matrix(&_viewport->camera.eye_pos, &_viewport->camera.eye_orient, 0.5f); + g3_set_view_matrix(&_viewport->camera.eye_pos, &_viewport->camera.eye_orient, EDITOR_DEFAULT_FOV); // Force max star detail so the editor always shows the full Num_stars count // regardless of the player's graphics quality setting (Detail.num_stars can be 0). @@ -1126,7 +1126,7 @@ void FredRenderer::render_frame(int cur_object_index, gr_reset_clip(); g3_start_frame(0); // ** Accounted for - g3_set_view_matrix(&_viewport->camera.eye_pos, &_viewport->camera.eye_orient, 0.5f); + g3_set_view_matrix(&_viewport->camera.eye_pos, &_viewport->camera.eye_orient, EDITOR_DEFAULT_FOV); } void FredRenderer::resize(int width, int height) { // Make sure the following call targets the right view port diff --git a/qtfred/src/ui/widgets/BriefingMapWidget.cpp b/qtfred/src/ui/widgets/BriefingMapWidget.cpp index 2f6389d79ad..e7b5f7b74ab 100644 --- a/qtfred/src/ui/widgets/BriefingMapWidget.cpp +++ b/qtfred/src/ui/widgets/BriefingMapWidget.cpp @@ -19,6 +19,7 @@ #include "graphics/2d.h" #include "render/3d.h" #include "mission/missionbriefcommon.h" +#include "missioneditor/common.h" #include #include @@ -612,7 +613,7 @@ void BriefingMapWidget::restoreMainViewportFrame(bool wasActive) { gr_screen_resize(static_cast(mainSize.first) * devicePixelRatio(), static_cast(mainSize.second) * devicePixelRatio()); g3_start_frame(0); - g3_set_view_matrix(&_viewport->camera.eye_pos, &_viewport->camera.eye_orient, 0.5f); + g3_set_view_matrix(&_viewport->camera.eye_pos, &_viewport->camera.eye_orient, EDITOR_DEFAULT_FOV); } bool BriefingMapWidget::event(QEvent* evt) { From 967dc96e4a0644de61afa3fe21a47859adc4b96a Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 13:48:54 +0100 Subject: [PATCH 04/12] QtFRED: fix the X/Y/Z boxes of the transform toolbar The boxes were not usable (wiki #31). updateUI() runs on each idle tick and calls ensureViewportFocus(). That function keeps the focus only on the children of a QDockWidget, and the transform toolbar is a QToolBar. Thus the viewport took the focus from a box immediately, and the focus loss applied a value that the user did not finish. Now a box keeps the focus while the user types. Enter applies the value and gives the focus back to the viewport. Escape and all other focus changes discard the value. A change of the current object or of the mode also gives the focus back. Enter applies the value only if the object and the mode did not change after the box got the focus. An edit of one box wrote all three boxes to the marked objects (wiki #10). The boxes showed one decimal, so the other two axes changed to rounded values. Now: - Only the axis of the edited box changes. A rotation changes only one angle of each object. - Enter with an unchanged value does not write the rounded value. With more than one marked object and Local OFF, Enter sets the value on all the marked objects, also if it did not change. - The boxes show two decimals. A click in the viewport now takes the focus. RenderWindow forwards the mouse events with sendEvent(), and Qt does not move the focus for such events. While a box has the focus, it also gets the keypad keys of the camera controls. The box shows these digits, but they have no effect until the user pushes Enter. Wiki #11 (the selected text stays highlighted) is not reproduced. QLineEdit removes the selection on each focus change, except a window or popup change. This commit does not change that behavior. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/ui/FredView.cpp | 207 +++++++++++++++---------- qtfred/src/ui/FredView.h | 14 +- qtfred/src/ui/widgets/renderwidget.cpp | 4 + 3 files changed, 145 insertions(+), 80 deletions(-) diff --git a/qtfred/src/ui/FredView.cpp b/qtfred/src/ui/FredView.cpp index 2fc4fde799f..bfff16bd00f 100644 --- a/qtfred/src/ui/FredView.cpp +++ b/qtfred/src/ui/FredView.cpp @@ -87,6 +87,9 @@ SCP_string cmdline_build_string(); namespace { +// Number of decimals in the X/Y/Z boxes of the transform toolbar +constexpr int TRANSFORM_BOX_DECIMALS = 2; + template void copyActionSettings(QAction* action, T* target) { Q_ASSERT(action->isCheckable()); @@ -1341,12 +1344,12 @@ void FredView::initializeTransformBar() { lbl->setContentsMargins(4, 0, 2, 0); _transformToolBar->addWidget(lbl); sb = new QDoubleSpinBox(_transformToolBar); - sb->setDecimals(1); - sb->setRange(-99999.9, 99999.9); + sb->setDecimals(TRANSFORM_BOX_DECIMALS); + sb->setRange(-99999.99, 99999.99); sb->setFixedWidth(90); sb->setKeyboardTracking(false); _transformToolBar->addWidget(sb); - connect(sb, &QDoubleSpinBox::editingFinished, this, &FredView::onTransformEditingFinished); + sb->installEventFilter(this); }; makeSpinBox(_transformLabelA, tr("X"), _transformA); @@ -1397,6 +1400,12 @@ void FredView::onUpdateTransformBar() { const int rawType = valid ? Objects[curObj].type : -1; const bool isShip = valid && (rawType == OBJ_SHIP || rawType == OBJ_START); + // A change of the current object or of the mode makes the typed value incorrect. Give the focus back to + // the viewport before the code below changes the ranges, so that the box shows the new value. + if (transformBoxAxis(QApplication::focusWidget()) >= 0 && !transformEditIsCurrent()) { + ui->centralWidget->setFocus(Qt::OtherFocusReason); + } + // ---- Spin box labels and ranges ---------------------------------------- if (rotateMode) { _transformLabel->setText(tr("Ori")); @@ -1411,9 +1420,9 @@ void FredView::onUpdateTransformBar() { _transformLabelA->setText(tr("X")); _transformLabelB->setText(tr("Y")); _transformLabelC->setText(tr("Z")); - _transformA->setRange(-99999.9, 99999.9); - _transformB->setRange(-99999.9, 99999.9); - _transformC->setRange(-99999.9, 99999.9); + _transformA->setRange(-99999.99, 99999.99); + _transformB->setRange(-99999.99, 99999.99); + _transformC->setRange(-99999.99, 99999.99); } const bool editable = valid && !selectMode; @@ -1547,90 +1556,124 @@ void FredView::onUpdateTransformBar() { } } -void FredView::onTransformEditingFinished() { - const int curObj = fred->currentObject; +bool FredView::eventFilter(QObject* watched, QEvent* event) { + const int axis = transformBoxAxis(watched); + if (axis < 0) { + return QMainWindow::eventFilter(watched, event); + } + auto* box = static_cast(watched); + + switch (event->type()) { + case QEvent::FocusIn: { + // The focus also comes back after a popup or a window change. The edit then continues, so keep the record. + const auto reason = static_cast(event)->reason(); + if (reason != Qt::PopupFocusReason && reason != Qt::ActiveWindowFocusReason) { + _transformEdit = {fred->currentObject, _viewport->Editing_mode, box->value()}; + } + break; + } + case QEvent::ShortcutOverride: { + // Do not let a menu shortcut or a control binding take Enter or Escape from the box + const int key = static_cast(event)->key(); + if (key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Escape) { + event->accept(); + return true; + } + break; + } + case QEvent::KeyPress: { + // Enter applies the value. Escape discards it. Both give the focus back to the viewport, so that the + // keyboard controls work again. All other focus changes discard the value. + const int key = static_cast(event)->key(); + if (key == Qt::Key_Return || key == Qt::Key_Enter) { + box->interpretText(); + applyTransformValue(axis, box->value()); + } else if (key != Qt::Key_Escape) { + break; + } + ui->centralWidget->setFocus(Qt::OtherFocusReason); + return true; + } + default: + break; + } + return QMainWindow::eventFilter(watched, event); +} + +int FredView::transformBoxAxis(const QObject* object) const { + if (object == nullptr) return -1; + if (object == _transformA) return 0; + if (object == _transformB) return 1; + if (object == _transformC) return 2; + return -1; +} + +bool FredView::transformEditIsCurrent() const { + return fred->currentObject == _transformEdit.object && _viewport->Editing_mode == _transformEdit.mode; +} + +void FredView::applyTransformValue(int axis, double value) { + // The selection or the mode can change while the box has the focus. Then the value is not for this object. + if (!transformEditIsCurrent()) return; + + const int curObj = fred->currentObject; if (!query_valid_object(curObj)) return; - const bool rotateMode = _viewport->Editing_mode == CursorMode::Rotating; - const bool localMode = !_viewport->Group_rotate; // Local ON = individual = !Group_rotate - const int numMarked = fred->getNumMarked(); - const bool isMulti = numMarked > 1; + // Change only the axis of the edited box. The other boxes show rounded values. If the code also wrote them, + // the object would move on the axes that the user did not change. + const bool rotateMode = _viewport->Editing_mode == CursorMode::Rotating; + const bool isMulti = fred->getNumMarked() > 1; + // Local ON (= !Group_rotate) with more than one object: add the change of the current object to + // each marked object. Otherwise, set the value on each marked object. + const bool relative = isMulti && !_viewport->Group_rotate; + // More than one object with Local OFF: Enter sets the value on all the marked objects, also if it did not change + const bool alignAll = isMulti && !relative; + + // If the user did not change the value, do not write the rounded value back to the object + const double scale = std::pow(10.0, TRANSFORM_BOX_DECIMALS); + const bool unchanged = std::abs(value - _transformEdit.startValue) < 0.5 / scale; + if (unchanged && !alignAll) return; + + auto angleOf = [axis](angles& a) -> float& { return (axis == 0) ? a.h : (axis == 1) ? a.p : a.b; }; + + float current; if (rotateMode) { - if (isMulti && localMode) { - // Local delta: compute angle delta from curObj, apply to every marked object. - angles oldAng{}; - vm_extract_angles_matrix(&oldAng, &Objects[curObj].orient); - const float dh = fl_radians(static_cast(_transformA->value())) - oldAng.h; - const float dp = fl_radians(static_cast(_transformB->value())) - oldAng.p; - const float db = fl_radians(static_cast(_transformC->value())) - oldAng.b; - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - angles a{}; - vm_extract_angles_matrix(&a, &p->orient); - a.h += dh; a.p += dp; a.b += db; - vm_angles_2_matrix(&p->orient, &a); - } - } else if (isMulti) { - // Global multi: align every marked object to the same absolute orientation. - angles ang{}; - ang.h = fl_radians(static_cast(_transformA->value())); - ang.p = fl_radians(static_cast(_transformB->value())); - ang.b = fl_radians(static_cast(_transformC->value())); - matrix m{}; - vm_angles_2_matrix(&m, &ang); - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - p->orient = m; - } + angles ang{}; + vm_extract_angles_matrix(&ang, &Objects[curObj].orient); + current = fl_degrees(angleOf(ang)); + } else { + current = Objects[curObj].pos.a1d[axis]; + } + + const auto newValue = static_cast(value); + const float delta = newValue - current; + + auto apply = [&](object* p) { + if (rotateMode) { + angles a{}; + vm_extract_angles_matrix(&a, &p->orient); + angleOf(a) = relative ? angleOf(a) + fl_radians(delta) : fl_radians(newValue); + vm_angles_2_matrix(&p->orient, &a); } else { - // Single object. - angles ang{}; - ang.h = fl_radians(static_cast(_transformA->value())); - ang.p = fl_radians(static_cast(_transformB->value())); - ang.b = fl_radians(static_cast(_transformC->value())); - vm_angles_2_matrix(&Objects[curObj].orient, &ang); + p->pos.a1d[axis] = relative ? p->pos.a1d[axis] + delta : newValue; } - } else { - if (isMulti && localMode) { - // Local delta: shift every marked object by the same offset relative to curObj. - const float dx = static_cast(_transformA->value()) - Objects[curObj].pos.xyz.x; - const float dy = static_cast(_transformB->value()) - Objects[curObj].pos.xyz.y; - const float dz = static_cast(_transformC->value()) - Objects[curObj].pos.xyz.z; - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - p->pos.xyz.x += dx; - p->pos.xyz.y += dy; - p->pos.xyz.z += dz; - } - } else if (isMulti) { - // Global multi: move every marked object to the same absolute position. - const auto nx = static_cast(_transformA->value()); - const auto ny = static_cast(_transformB->value()); - const auto nz = static_cast(_transformC->value()); + }; + + auto forEachTarget = [&](auto&& fn) { + if (isMulti) { for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - p->pos.xyz.x = nx; - p->pos.xyz.y = ny; - p->pos.xyz.z = nz; + if (p->flags[Object::Object_Flags::Marked]) + fn(p); } } else { - // Single object. - Objects[curObj].pos.xyz.x = static_cast(_transformA->value()); - Objects[curObj].pos.xyz.y = static_cast(_transformB->value()); - Objects[curObj].pos.xyz.z = static_cast(_transformC->value()); + fn(&Objects[curObj]); } - } + }; - // Move the docked partners with the changed objects - if (isMulti) { - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (p->flags[Object::Object_Flags::Marked]) - object_moved(p); - } - } else { - object_moved(&Objects[curObj]); - } + forEachTarget(apply); + // Move the docked partners after all the objects changed + forEachTarget(object_moved); fred->missionChanged(); } @@ -1682,6 +1725,12 @@ void FredView::ensureViewportFocus() { return; } + // Do not take the focus from a transform bar box while the user types a value. The toolbar is not a + // dock widget. Enter, Escape, or a click in the viewport gives the focus back. + if (transformBoxAxis(focusedWidget) >= 0) { + return; + } + // Don't steal focus from dock widget children — the user is intentionally // interacting with a panel (search bar, buttons, checkboxes, etc.). QWidget* w = focusedWidget; diff --git a/qtfred/src/ui/FredView.h b/qtfred/src/ui/FredView.h index ab15c28d02b..daff6a34efa 100644 --- a/qtfred/src/ui/FredView.h +++ b/qtfred/src/ui/FredView.h @@ -322,7 +322,11 @@ class FredView: public QMainWindow, public IDialogProvider { void initializeTransformBar(); void onUpdateTransformBar(); - void onTransformEditingFinished(); + bool eventFilter(QObject* watched, QEvent* event) override; + // Returns 0, 1, or 2 for the X/Y/Z (or H/P/B) box of the transform toolbar, and -1 for all other objects + int transformBoxAxis(const QObject* object) const; + bool transformEditIsCurrent() const; + void applyTransformValue(int axis, double value); QToolBar* _contextToolBar = nullptr; QLabel* _contextLabel = nullptr; @@ -343,6 +347,14 @@ class FredView: public QMainWindow, public IDialogProvider { QDoubleSpinBox* _transformA = nullptr; QDoubleSpinBox* _transformB = nullptr; QDoubleSpinBox* _transformC = nullptr; + // The state when a transform box got the focus. Enter applies the typed value only if the current object + // and the mode did not change, and only if the value is different from the start value. + struct TransformEdit { + int object = -1; + CursorMode mode = CursorMode::Selecting; + double startValue = 0.0; + }; + TransformEdit _transformEdit; QComboBox* _transformMoveSpeedCombo = nullptr; QComboBox* _transformRotSpeedCombo = nullptr; // Last camera speeds written to QSettings; lets us persist on change instead of only on close. diff --git a/qtfred/src/ui/widgets/renderwidget.cpp b/qtfred/src/ui/widgets/renderwidget.cpp index c327e82d8df..e45a45cf004 100644 --- a/qtfred/src/ui/widgets/renderwidget.cpp +++ b/qtfred/src/ui/widgets/renderwidget.cpp @@ -189,6 +189,10 @@ void RenderWidget::mouseDoubleClickEvent(QMouseEvent* event) { event->ignore(); } void RenderWidget::mousePressEvent(QMouseEvent* event) { + // RenderWindow forwards the mouse events with sendEvent(). Qt does not move the focus for such an event, so a + // click in the viewport did not take the focus from a toolbar box. + setFocus(Qt::MouseFocusReason); + // Orbit camera: middle button if (event->button() == Qt::MiddleButton) { if (_viewport != nullptr && _viewport->camera.getViewpoint() == 0 && _viewport->camera.getControlMode() == 0) { From 4ae8c241729ee8211c79da76eae4fae8463c7ba6 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 13:50:16 +0100 Subject: [PATCH 05/12] QtFRED: make the text readable on colored annotation rows applyVisuals() set only the background color of an annotated SEXP tree row. The text kept the color of the theme, so light text on a light annotation color (or dark text on a dark one) was unreadable. Now the text is black or white, with the rule that FRED2 uses. The rule moves to the new shared function annotation_text_is_dark(), so that the two editors show the same text color. The Events editor also gave a color to annotations that have only a note. For "no color", the model uses white (255, 255, 255). But the dialog stored white as a valid color, so these rows were white, also in dark mode. Now such a row has no color. applyVisuals() removes the background and text colors, so the row uses the theme colors. Co-Authored-By: Claude Opus 5.5 --- code/missioneditor/sexp_annotation_model.cpp | 7 +++++++ code/missioneditor/sexp_annotation_model.h | 4 ++++ fred2/eventeditor.cpp | 4 +--- qtfred/src/ui/dialogs/MissionEventsDialog.cpp | 8 +++----- qtfred/src/ui/widgets/sexp_tree_view.cpp | 8 +++++++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/code/missioneditor/sexp_annotation_model.cpp b/code/missioneditor/sexp_annotation_model.cpp index 3478642a817..5382abf9c81 100644 --- a/code/missioneditor/sexp_annotation_model.cpp +++ b/code/missioneditor/sexp_annotation_model.cpp @@ -1,6 +1,13 @@ #include "missioneditor/sexp_annotation_model.h" #include "missioneditor/sexp_tree_model.h" +bool annotation_text_is_dark(int r, int g, int b) +{ + // The source of this contrast rule: + // https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color + return (r * 0.299 + g * 0.587 + b * 0.114) > 149; +} + SCP_vector Event_annotations; // ----------------------------------------------------------------------- diff --git a/code/missioneditor/sexp_annotation_model.h b/code/missioneditor/sexp_annotation_model.h index fef784dee48..a20228021a3 100644 --- a/code/missioneditor/sexp_annotation_model.h +++ b/code/missioneditor/sexp_annotation_model.h @@ -14,6 +14,10 @@ struct event_annotation { }; extern SCP_vector Event_annotations; +// Returns true if black text is readable on the annotation background color, and false if white text is better. +// Both editors use this rule, so that an annotation has the same text color in FRED2 and in QtFRED. +bool annotation_text_is_dark(int r, int g, int b); + struct sexp_tree_item; class SexpAnnotationModel { diff --git a/fred2/eventeditor.cpp b/fred2/eventeditor.cpp index 51aa4234bca..7d43766903a 100644 --- a/fred2/eventeditor.cpp +++ b/fred2/eventeditor.cpp @@ -1991,9 +1991,7 @@ void event_sexp_tree::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) { if (ea->r != 255 || ea->g != 255 || ea->b != 255) { - // contrast color calculation taken from here: - // https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color - if ((ea->r*0.299 + ea->g*0.587 + ea->b*0.114) > 149) + if (annotation_text_is_dark(ea->r, ea->g, ea->b)) pcd->clrText = RGB(0, 0, 0); else pcd->clrText = RGB(255, 255, 255); diff --git a/qtfred/src/ui/dialogs/MissionEventsDialog.cpp b/qtfred/src/ui/dialogs/MissionEventsDialog.cpp index 267ad74b739..942cfc8ba5e 100644 --- a/qtfred/src/ui/dialogs/MissionEventsDialog.cpp +++ b/qtfred/src/ui/dialogs/MissionEventsDialog.cpp @@ -180,11 +180,9 @@ MissionEventsDialog::MissionEventsDialog(QWidget* parent, EditorViewport* viewpo if (!it) return; - const QString q = QString::fromStdString(note); - it->setData(0, sexp_tree_view::NoteRole, q); - it->setToolTip(0, q); - it->setData(0, sexp_tree_view::BgColorRole, QColor(r, g, b)); - it->setBackground(0, has_color ? QBrush(QColor(r, g, b)) : QBrush()); + it->setData(0, sexp_tree_view::NoteRole, QString::fromStdString(note)); + // An annotation with only a note has no color. An invalid color keeps the theme colors. + it->setData(0, sexp_tree_view::BgColorRole, has_color ? QColor(r, g, b) : QColor()); sexp_tree_view::applyVisuals(it); }); diff --git a/qtfred/src/ui/widgets/sexp_tree_view.cpp b/qtfred/src/ui/widgets/sexp_tree_view.cpp index caeb403b9a7..ab987d10526 100644 --- a/qtfred/src/ui/widgets/sexp_tree_view.cpp +++ b/qtfred/src/ui/widgets/sexp_tree_view.cpp @@ -3,6 +3,7 @@ #include "mission/Editor.h" #include "mission/EditorViewport.h" #include "mission/object.h" +#include "missioneditor/sexp_annotation_model.h" #include #include @@ -2048,9 +2049,14 @@ void sexp_tree_view::applyVisuals(QTreeWidgetItem* it) const auto color = it->data(0, BgColorRole).value(); it->setToolTip(0, note); - // Background color for the entire row if (color.isValid()) { it->setBackground(0, QBrush(color)); + // The text color of the theme can be unreadable on the annotation color + it->setForeground(0, QBrush(annotation_text_is_dark(color.red(), color.green(), color.blue()) ? Qt::black : Qt::white)); + } else { + // Give the row the colors of the theme again + it->setData(0, Qt::BackgroundRole, QVariant()); + it->setData(0, Qt::ForegroundRole, QVariant()); } } From 2ab626fd7171df71358b205beab26324873ec804 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:00:00 +0100 Subject: [PATCH 06/12] QtFRED: share the loop that moves the docked partners of marked objects Seven places had a copy of the loop that calls object_moved() for each marked object. The new function marked_objects_moved() holds this loop. It returns the number of marked objects, so Level Object and Align Object do not need their own counters. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/mission/EditorViewport.cpp | 58 +++---------------- .../dialogs/ObjectOrientEditorDialogModel.cpp | 6 +- qtfred/src/mission/object.cpp | 12 ++++ qtfred/src/mission/object.h | 3 + 4 files changed, 24 insertions(+), 55 deletions(-) diff --git a/qtfred/src/mission/EditorViewport.cpp b/qtfred/src/mission/EditorViewport.cpp index f5b9f731dda..5dedbbded67 100644 --- a/qtfred/src/mission/EditorViewport.cpp +++ b/qtfred/src/mission/EditorViewport.cpp @@ -485,14 +485,7 @@ void EditorViewport::game_do_frame(const int cur_object_index) { objp = GET_NEXT(objp); } - objp = GET_FIRST(&obj_used_list); - while (objp != END_OF_LIST(&obj_used_list)) { - if (objp->flags[Object::Object_Flags::Marked]) { - object_moved(objp); - } - - objp = GET_NEXT(objp); - } + marked_objects_moved(); editor->missionChanged(); } @@ -545,7 +538,7 @@ void EditorViewport::game_do_frame(const int cur_object_index) { } void EditorViewport::level_controlled() { - int cmode, count = 0; + int cmode; object* objp; cmode = camera.getControlMode(); @@ -577,18 +570,8 @@ void EditorViewport::level_controlled() { objp = GET_NEXT(objp); } - objp = GET_FIRST(&obj_used_list); - while (objp != END_OF_LIST(&obj_used_list)) { - if (objp->flags[Object::Object_Flags::Marked]) { - object_moved(objp); - count++; - } - - objp = GET_NEXT(objp); - } - ///! \todo Notify. - if (count) { + if (marked_objects_moved() > 0) { editor->missionChanged(); } @@ -599,7 +582,7 @@ void EditorViewport::level_controlled() { } void EditorViewport::verticalize_controlled() { - int cmode, count = 0; + int cmode; object* objp; cmode = camera.getControlMode(); @@ -631,18 +614,8 @@ void EditorViewport::verticalize_controlled() { objp = GET_NEXT(objp); } - objp = GET_FIRST(&obj_used_list); - while (objp != END_OF_LIST(&obj_used_list)) { - if (objp->flags[Object::Object_Flags::Marked]) { - object_moved(objp); - count++; - } - - objp = GET_NEXT(objp); - } - ///! \todo Notify. - if (count) { + if (marked_objects_moved() > 0) { editor->missionChanged(); } @@ -1612,13 +1585,7 @@ int EditorViewport::drag_objects(int x, int y) objp = GET_NEXT(objp); } - objp = GET_FIRST(&obj_used_list); - while (objp != END_OF_LIST(&obj_used_list)) { - if (objp->flags[Object::Object_Flags::Marked]) - object_moved(objp); - - objp = GET_NEXT(objp); - } + marked_objects_moved(); } editor->missionChanged(); @@ -1725,13 +1692,7 @@ int EditorViewport::drag_rotate_objects(int mouse_dx, int mouse_dy) { objp = GET_NEXT(objp); } - objp = GET_FIRST(&obj_used_list); - while (objp != END_OF_LIST(&obj_used_list)) { - if (objp->flags[Object::Object_Flags::Marked]) - object_moved(objp); - - objp = GET_NEXT(objp); - } + marked_objects_moved(); editor->missionChanged(); return rval; @@ -1757,10 +1718,7 @@ void EditorViewport::cancel_drag() { } // Put the docked partners back with the restored objects - for (objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp)) { - if (objp->flags[Object::Object_Flags::Marked]) - object_moved(objp); - } + marked_objects_moved(); button_down = false; moved = false; diff --git a/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp b/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp index a1490c1b8b9..66e2c098462 100644 --- a/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/ObjectOrientEditorDialogModel.cpp @@ -239,11 +239,7 @@ bool ObjectOrientEditorDialogModel::apply() // Notify the engine about moved objects. A rotation also moves the docked partners. if (change_pos || change_orient || _pointTo || relativeToOrigin) { - for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) { - if (ptr->flags[Object::Object_Flags::Marked]) { - object_moved(ptr); - } - } + marked_objects_moved(); } return true; diff --git a/qtfred/src/mission/object.cpp b/qtfred/src/mission/object.cpp index c4a6781e8cf..5512bce80f5 100644 --- a/qtfred/src/mission/object.cpp +++ b/qtfred/src/mission/object.cpp @@ -27,6 +27,18 @@ void object_moved(object *objp) } } +int marked_objects_moved() +{ + int count = 0; + for (auto objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp)) { + if (objp->flags[Object::Object_Flags::Marked]) { + object_moved(objp); + count++; + } + } + return count; +} + bool query_valid_object(int index) { bool obj_found = false; diff --git a/qtfred/src/mission/object.h b/qtfred/src/mission/object.h index 488197e0b81..a8c1cde651e 100644 --- a/qtfred/src/mission/object.h +++ b/qtfred/src/mission/object.h @@ -5,6 +5,9 @@ class object; void object_moved(object *ptr); +// Calls object_moved() for each marked object, so that their docked partners move with them. +// Returns the number of marked objects. +int marked_objects_moved(); bool query_valid_object(int index); const char *object_name(int obj); From 96ccca25ccc92a6e944d636f3dc2cca97ef23023 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:02:06 +0100 Subject: [PATCH 07/12] QtFRED: use one function for next and previous object select_next_object() and select_previous_object() were near copies. The previous version copied the object list into an array to step back, and each version had its own loop to skip locked objects. Now both call select_adjacent_object(), which steps through the circular object list in either direction. The behavior does not change. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/mission/Editor.cpp | 109 +++++++++++----------------------- qtfred/src/mission/Editor.h | 2 + 2 files changed, 36 insertions(+), 75 deletions(-) diff --git a/qtfred/src/mission/Editor.cpp b/qtfred/src/mission/Editor.cpp index bc2dbac7065..e41e9ddc5ed 100644 --- a/qtfred/src/mission/Editor.cpp +++ b/qtfred/src/mission/Editor.cpp @@ -1705,42 +1705,50 @@ void Editor::cancel_select_subsystem() { } void Editor::select_next_object() { - object* ptr; - + select_adjacent_object(true); +} +void Editor::select_previous_object() +{ + select_adjacent_object(false); +} +void Editor::select_adjacent_object(bool forward) +{ if (EMPTY(&obj_used_list)) return; - if (query_valid_object(currentObject)) { - ptr = Objects[currentObject].next; + // Step through the circular object list, and jump over the list head + auto step = [forward](object* ptr) { + ptr = forward ? GET_NEXT(ptr) : GET_PREV(ptr); if (ptr == END_OF_LIST(&obj_used_list)) - ptr = GET_NEXT(ptr); - } else { - ptr = GET_FIRST(&obj_used_list); - } + ptr = forward ? GET_NEXT(ptr) : GET_PREV(ptr); + return ptr; + }; + + object* ptr; + if (query_valid_object(currentObject)) + ptr = step(&Objects[currentObject]); + else + ptr = forward ? GET_FIRST(&obj_used_list) : GET_LAST(&obj_used_list); if (getNumMarked() > 1) { // Cycle current object through marked objects only - while (!Objects[OBJ_INDEX(ptr)].flags[Object::Object_Flags::Marked]) { - ptr = GET_NEXT(ptr); - if (ptr == END_OF_LIST(&obj_used_list)) - ptr = GET_NEXT(ptr); - } + while (!ptr->flags[Object::Object_Flags::Marked]) + ptr = step(ptr); setupCurrentObjectIndices(OBJ_INDEX(ptr)); - } else { - // Skip the locked objects. Like a click in the viewport, this command does not select them. - const object* start = ptr; - while (ptr->flags[Object::Object_Flags::Locked_from_editing]) { - ptr = GET_NEXT(ptr); - if (ptr == END_OF_LIST(&obj_used_list)) - ptr = GET_NEXT(ptr); - if (ptr == start) - return; // all objects are locked - } + return; + } - if (getNumMarked()) - unmarkObject(currentObject); - markObject(OBJ_INDEX(ptr)); + // Skip the locked objects. Like a click in the viewport, this command does not select them. + const object* start = ptr; + while (ptr->flags[Object::Object_Flags::Locked_from_editing]) { + ptr = step(ptr); + if (ptr == start) + return; // all objects are locked } + + if (getNumMarked()) + unmarkObject(currentObject); + markObject(OBJ_INDEX(ptr)); } void Editor::unmarkLockedObjects() { @@ -1749,55 +1757,6 @@ void Editor::unmarkLockedObjects() unmarkObject(OBJ_INDEX(ptr)); } } -void Editor::select_previous_object() -{ - int arr[MAX_OBJECTS], i = 0, n = 0; - object* ptr; - - if (EMPTY(&obj_used_list)) - return; - - ptr = GET_FIRST(&obj_used_list); - while (ptr != END_OF_LIST(&obj_used_list)) { - if (currentObject == OBJ_INDEX(ptr)) - i = n; - arr[n++] = OBJ_INDEX(ptr); - ptr = GET_NEXT(ptr); - } - - Assert(n); - if (query_valid_object(currentObject)) { - i--; - if (i < 0) - i = n - 1; - } else { - i = n - 1; - } - - if (getNumMarked() > 1) { - // Cycle current object through marked objects only - while (!Objects[arr[i]].flags[Object::Object_Flags::Marked]) { - i--; - if (i < 0) - i = n - 1; - } - setupCurrentObjectIndices(arr[i]); - } else { - // Skip the locked objects. Like a click in the viewport, this command does not select them. - const int start = i; - while (Objects[arr[i]].flags[Object::Object_Flags::Locked_from_editing]) { - i--; - if (i < 0) - i = n - 1; - if (i == start) - return; // all objects are locked - } - - if (getNumMarked()) - unmarkObject(currentObject); - markObject(arr[i]); - } -} int Editor::get_visible_sub_system_count(ship * shipp) { int count = 0; ship_subsys* cur_subsys; diff --git a/qtfred/src/mission/Editor.h b/qtfred/src/mission/Editor.h index 5566c799bc1..cb7e8a419cf 100644 --- a/qtfred/src/mission/Editor.h +++ b/qtfred/src/mission/Editor.h @@ -278,6 +278,8 @@ class Editor : public QObject { void initialSetup(); void setupCurrentObjectIndices(int obj); + // Next or previous object: moves the selection one object forward or back in obj_used_list + void select_adjacent_object(bool forward); SCP_vector> _viewports; EditorViewport* _lastActiveViewport = nullptr; From 63adf93cab900f1d097125923d55b91dcfc35187 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:04:43 +0100 Subject: [PATCH 08/12] QtFRED: skip locked ships in mark_wing() instead of unmarking them The Select Wing commands called mark_wing() and then unmarkLockedObjects(). Thus each locked ship was marked and then unmarked, and the marking signals went out two times. Also the FredView::selectWing() wrapper had no other purpose. Now mark_wing() has a LockedObjects argument with no default value. The Select Wing commands pass Skip. A selection by name, for example in the Wing Editor, and the new wing after Form Wing pass Include. unmarkLockedObjects() and selectWing() are removed. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/mission/Editor.cpp | 7 ------- qtfred/src/mission/Editor.h | 9 +++++---- qtfred/src/mission/EditorWing.cpp | 20 +++++++++++++------ .../mission/dialogs/WingEditorDialogModel.cpp | 4 ++-- qtfred/src/ui/FredView.cpp | 12 +++++------ qtfred/src/ui/FredView.h | 1 - qtfred/src/ui/dialogs/WingEditorDialog.cpp | 2 +- 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/qtfred/src/mission/Editor.cpp b/qtfred/src/mission/Editor.cpp index e41e9ddc5ed..028767a0516 100644 --- a/qtfred/src/mission/Editor.cpp +++ b/qtfred/src/mission/Editor.cpp @@ -1750,13 +1750,6 @@ void Editor::select_adjacent_object(bool forward) unmarkObject(currentObject); markObject(OBJ_INDEX(ptr)); } -void Editor::unmarkLockedObjects() -{ - for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) { - if (ptr->flags[Object::Object_Flags::Marked] && ptr->flags[Object::Object_Flags::Locked_from_editing]) - unmarkObject(OBJ_INDEX(ptr)); - } -} int Editor::get_visible_sub_system_count(ship * shipp) { int count = 0; ship_subsys* cur_subsys; diff --git a/qtfred/src/mission/Editor.h b/qtfred/src/mission/Editor.h index cb7e8a419cf..ae6859ec31a 100644 --- a/qtfred/src/mission/Editor.h +++ b/qtfred/src/mission/Editor.h @@ -203,15 +203,16 @@ class Editor : public QObject { */ int create_wing(); + enum class LockedObjects { Include, Skip }; + /** * @brief Mark all ships within this wing * * @param[in] wing Index of the wing to mark + * @param[in] locked Skip for the Select Wing commands: like a click in the viewport, they do not select + * a locked ship. Include for a selection of the wing by name, for example in the Wing Editor. */ - void mark_wing(int wing); - // Unmark the marked objects that are locked. The Select Wing commands call this function after mark_wing(), - // because like a click in the viewport, they do not select a locked object. - void unmarkLockedObjects(); + void mark_wing(int wing, LockedObjects locked); bool query_single_wing_marked(); diff --git a/qtfred/src/mission/EditorWing.cpp b/qtfred/src/mission/EditorWing.cpp index 6b311b81a1d..5bede3174ab 100644 --- a/qtfred/src/mission/EditorWing.cpp +++ b/qtfred/src/mission/EditorWing.cpp @@ -315,7 +315,7 @@ int Editor::create_wing() } } - mark_wing(wing); + mark_wing(wing, LockedObjects::Include); update_custom_wing_indexes(); @@ -391,15 +391,23 @@ int Editor::find_free_wing() return -1; } -void Editor::mark_wing(int wing) +void Editor::mark_wing(int wing, LockedObjects locked) { - int i; + auto skip = [locked](int obj) { + return locked == LockedObjects::Skip && Objects[obj].flags[Object::Object_Flags::Locked_from_editing]; + }; unmark_all(); Assert(Wings[wing].special_ship >= 0 && Wings[wing].special_ship < Wings[wing].wave_count); - setupCurrentObjectIndices(wing_objects[wing][Wings[wing].special_ship]); - for (i = 0; i < Wings[wing].wave_count; i++) { - markObject(wing_objects[wing][i]); + // The leader is the current object. If the leader is skipped, markObject() makes the first marked ship current. + const int leader = wing_objects[wing][Wings[wing].special_ship]; + if (!skip(leader)) { + setupCurrentObjectIndices(leader); + } + for (int i = 0; i < Wings[wing].wave_count; i++) { + if (!skip(wing_objects[wing][i])) { + markObject(wing_objects[wing][i]); + } } } diff --git a/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp b/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp index 38ab3537d58..bfd9cc0f0b5 100644 --- a/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp @@ -707,7 +707,7 @@ void WingEditorDialogModel::selectPreviousWing() } _editor->unmark_all(); - _editor->mark_wing(prv); + _editor->mark_wing(prv, Editor::LockedObjects::Include); reloadFromCurWing(); } @@ -730,7 +730,7 @@ void WingEditorDialogModel::selectNextWing() } _editor->unmark_all(); - _editor->mark_wing(nxt); + _editor->mark_wing(nxt, Editor::LockedObjects::Include); reloadFromCurWing(); } diff --git a/qtfred/src/ui/FredView.cpp b/qtfred/src/ui/FredView.cpp index bfff16bd00f..37750cfc65b 100644 --- a/qtfred/src/ui/FredView.cpp +++ b/qtfred/src/ui/FredView.cpp @@ -1191,7 +1191,9 @@ void FredView::onUpdateContextToolbar() { addBtn(tr("Edit Wing"), &FredView::on_actionWings_triggered); auto* selWingAct = new QAction(tr("Select Wing"), _contextToolBar); int capturedWing = effectiveWingNum; - connect(selWingAct, &QAction::triggered, this, [this, capturedWing]() { selectWing(capturedWing); }); + connect(selWingAct, &QAction::triggered, this, [this, capturedWing]() { + fred->mark_wing(capturedWing, Editor::LockedObjects::Skip); + }); _contextToolBar->addAction(selWingAct); } } else if (effectiveType == OBJ_WAYPOINT) { @@ -2107,7 +2109,7 @@ void FredView::initializePopupMenus() { if (query_valid_object(obj) && (Objects[obj].type == OBJ_SHIP || Objects[obj].type == OBJ_START)) { int wing = Ships[Objects[obj].instance].wingnum; if (wing >= 0) { - selectWing(wing); + fred->mark_wing(wing, Editor::LockedObjects::Skip); } } }); @@ -3002,10 +3004,6 @@ void FredView::onGroupSelected(int group) { objp = GET_NEXT(objp); } } -void FredView::selectWing(int wing) { - fred->mark_wing(wing); - fred->unmarkLockedObjects(); -} void FredView::onSetGroup(int group) { bool err = false; @@ -3060,7 +3058,7 @@ void FredView::on_actionPrev_Object_triggered(bool) { } void FredView::on_actionMark_Wing_triggered(bool) { if (fred->cur_wing != -1) { - selectWing(fred->cur_wing); + fred->mark_wing(fred->cur_wing, Editor::LockedObjects::Skip); } } void FredView::on_actionError_Checker_triggered(bool) { diff --git a/qtfred/src/ui/FredView.h b/qtfred/src/ui/FredView.h index daff6a34efa..981f4b84047 100644 --- a/qtfred/src/ui/FredView.h +++ b/qtfred/src/ui/FredView.h @@ -247,7 +247,6 @@ class FredView: public QMainWindow, public IDialogProvider { void populateCreatePropSubmenu(); void openLayerManagerDialog(); void ensureViewportFocus(); - void selectWing(int wing); void enforceSideDockAreas(); void onGroupSelected(int group); diff --git a/qtfred/src/ui/dialogs/WingEditorDialog.cpp b/qtfred/src/ui/dialogs/WingEditorDialog.cpp index 5bb423f4bf5..cc4298ad956 100644 --- a/qtfred/src/ui/dialogs/WingEditorDialog.cpp +++ b/qtfred/src/ui/dialogs/WingEditorDialog.cpp @@ -78,7 +78,7 @@ WingEditorDialog::WingEditorDialog(FredView* parent, EditorViewport* viewport) return entries; }, [editor]() { return editor->cur_wing; }, - [editor](int wing) { editor->mark_wing(wing); }, + [editor](int wing) { editor->mark_wing(wing, Editor::LockedObjects::Include); }, tr("&Select Wing")); initializeUi(); From 986b7f35f5ad759b940883451a9d9d9a9b37a139 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:05:46 +0100 Subject: [PATCH 09/12] Editors: take the projection field of view from the g3 view The projection matrix computed its field of view from the editor constant, and the g3 view matrix used the same constant. Thus two calculations had to stay equal. In FRED2, both also had to check Briefing_dialog. g3_set_view_matrix() already sets Proj_fov from its zoom, and the game gives Proj_fov to gr_set_proj_matrix(). The editors now do the same. Each editor frame sets the g3 view matrix before it enables the projection, so the result does not change. Co-Authored-By: Claude Opus 5.5 --- fred2/fredrender.cpp | 3 ++- qtfred/src/mission/FredRenderer.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/fred2/fredrender.cpp b/fred2/fredrender.cpp index 24832273d21..b8648f6b5f1 100644 --- a/fred2/fredrender.cpp +++ b/fred2/fredrender.cpp @@ -843,7 +843,8 @@ void fred_disable_htl() { } void fred_enable_htl() { - gr_set_proj_matrix((4.0f / 9.0f) * PI * (Briefing_dialog ? Briefing_window_FOV : EDITOR_DEFAULT_FOV), gr_screen.aspect*(float) gr_screen.clip_width / (float) gr_screen.clip_height, 1.0f, FRED_DEAFULT_HTL_DRAW_DIST); + // g3_set_view_matrix() sets Proj_fov from its zoom, so the projection always uses the field of view of the g3 view + gr_set_proj_matrix(Proj_fov, gr_screen.aspect*(float) gr_screen.clip_width / (float) gr_screen.clip_height, 1.0f, FRED_DEAFULT_HTL_DRAW_DIST); gr_set_view_matrix(&Eye_position, &Eye_matrix); } diff --git a/qtfred/src/mission/FredRenderer.cpp b/qtfred/src/mission/FredRenderer.cpp index 67b904b8eb2..495f8e1ac7e 100644 --- a/qtfred/src/mission/FredRenderer.cpp +++ b/qtfred/src/mission/FredRenderer.cpp @@ -43,7 +43,8 @@ const int FRED_COLOUR_WHITE = 0xffffff; const int FRED_COLOUR_YELLOW_GREEN = 0xc8ff00; void enable_htl() { - gr_set_proj_matrix((4.0f / 9.0f) * PI * EDITOR_DEFAULT_FOV, + // g3_set_view_matrix() sets Proj_fov from its zoom, so the projection always uses the field of view of the g3 view + gr_set_proj_matrix(Proj_fov, gr_screen.aspect * static_cast(gr_screen.clip_width) / static_cast(gr_screen.clip_height), 1.0f, From c149c166d5df7a853d0e71d380569f4863c4ff05 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:09:47 +0100 Subject: [PATCH 10/12] QtFRED: move the transform toolbar into its own class The transform toolbar code was about 450 lines of FredView.cpp, with its own state: the edit record of the boxes, the Local preference of each mode, and the dirty flags of the IFF and layer lists. Now the new class TransformToolBar owns the widgets, this state, the event filter of the boxes, and the Local settings. FredView creates the class, calls update() on each idle tick, and tells it when the layers change. FredView.cpp goes from 3194 to 2719 lines. Other changes: - The three boxes and their labels are arrays, so the per-axis code is a loop. - The sync of the camera speed combo boxes moves from onUpdateViewSpeeds() into the class. - update() calls one function for each widget group. The behavior does not change. Co-Authored-By: Claude Opus 5.5 --- qtfred/source_groups.cmake | 2 + qtfred/src/ui/FredView.cpp | 493 +------------------ qtfred/src/ui/FredView.h | 40 +- qtfred/src/ui/widgets/TransformToolBar.cpp | 529 +++++++++++++++++++++ qtfred/src/ui/widgets/TransformToolBar.h | 96 ++++ 5 files changed, 639 insertions(+), 521 deletions(-) create mode 100644 qtfred/src/ui/widgets/TransformToolBar.cpp create mode 100644 qtfred/src/ui/widgets/TransformToolBar.h diff --git a/qtfred/source_groups.cmake b/qtfred/source_groups.cmake index 6718b68a21a..c43acfadc61 100644 --- a/qtfred/source_groups.cmake +++ b/qtfred/source_groups.cmake @@ -339,6 +339,8 @@ add_file_folder("Source/UI/Widgets" src/ui/widgets/ShipFlagCheckbox.cpp src/ui/widgets/SimpleListSelectDialog.cpp src/ui/widgets/SimpleListSelectDialog.h + src/ui/widgets/TransformToolBar.cpp + src/ui/widgets/TransformToolBar.h src/ui/widgets/MusicComboWidget.cpp src/ui/widgets/MusicComboWidget.h ) diff --git a/qtfred/src/ui/FredView.cpp b/qtfred/src/ui/FredView.cpp index 37750cfc65b..04b12f74bb9 100644 --- a/qtfred/src/ui/FredView.cpp +++ b/qtfred/src/ui/FredView.cpp @@ -77,6 +77,7 @@ #include "missioneditor/missionsave.h" #include "widgets/ObjectComboBox.h" +#include "widgets/TransformToolBar.h" #include "widgets/data_list_menu.h" #include "util.h" @@ -87,9 +88,6 @@ SCP_string cmdline_build_string(); namespace { -// Number of decimals in the X/Y/Z boxes of the transform toolbar -constexpr int TRANSFORM_BOX_DECIMALS = 2; - template void copyActionSettings(QAction* action, T* target) { Q_ASSERT(action->isCheckable()); @@ -267,13 +265,11 @@ void FredView::setEditor(Editor* editor, EditorViewport* viewport) { connect(_otherClassBox, &ObjectComboBox::classSelected, this, &FredView::onOtherKindSelected); initializeContextToolbar(); - initializeTransformBar(); + _transformBar = new TransformToolBar(ui->transformToolBar, fred, _viewport, ui->centralWidget); - // Restore per-mode Local preferences and camera speeds from last session. + // Restore the camera speeds from the last session. { QSettings settings; - _tbLocalMove = settings.value("FredView/transformLocalMove", false).toBool(); - _tbLocalRotate = settings.value("FredView/transformLocalRotate", false).toBool(); _viewport->camera.setPhysicsSpeed(settings.value("FredView/cameraSpeedMove", 1).toInt()); _viewport->camera.setPhysicsRot(settings.value("FredView/cameraSpeedRot", 25).toInt()); _lastSavedCameraSpeedMove = _viewport->camera.getPhysicsSpeed(); @@ -292,7 +288,7 @@ void FredView::setEditor(Editor* editor, EditorViewport* viewport) { save.set_fred_callsigns(Fred_callsigns); save.save_autosave_file(savePath.toUtf8().constData()); }); - connect(fred, &Editor::layerListChanged, this, [this]() { _tbLayerComboDirty = true; }); + connect(fred, &Editor::layerListChanged, _transformBar, &TransformToolBar::markLayersChanged); // Sets the initial window title on_mission_loaded(""); @@ -310,7 +306,7 @@ void FredView::setEditor(Editor* editor, EditorViewport* viewport) { connect(this, &FredView::viewIdle, this, &FredView::onUpdateEditorActions); connect(this, &FredView::viewIdle, this, &FredView::onUpdateWingActionStatus); connect(this, &FredView::viewIdle, this, &FredView::onUpdateContextToolbar); - connect(this, &FredView::viewIdle, this, &FredView::onUpdateTransformBar); + connect(this, &FredView::viewIdle, _transformBar, &TransformToolBar::update); connect(this, &FredView::viewIdle, this, @@ -825,7 +821,8 @@ void FredView::on_mission_loaded(const std::string& filepath) { if (_viewport != nullptr) { _viewport->reloadLayersFromMission(); - _tbLayerComboDirty = true; + if (_transformBar != nullptr) + _transformBar->markLayersChanged(); } QString filename = "Untitled"; @@ -1230,456 +1227,6 @@ void FredView::quickRenameCurrentObject() { } -// --------------------------------------------------------------------------- -// Transform bar (bottom, above the status bar) -// --------------------------------------------------------------------------- - -void FredView::initializeTransformBar() { - _transformToolBar = ui->transformToolBar; - _transformToolBar->setContextMenuPolicy(Qt::PreventContextMenu); - _transformToolBar->setVisible(true); - - // Helper: add a fixed-width spacer widget to the toolbar. - auto addFixedSpacer = [this](int w) { - auto* sp = new QWidget(_transformToolBar); - sp->setFixedWidth(w); - _transformToolBar->addWidget(sp); - }; - - // ---- Left section: camera move and rotation speed selectors --------------- - addFixedSpacer(8); - - auto* moveSpeedLabel = new QLabel(tr("Camera Move:"), _transformToolBar); - moveSpeedLabel->setContentsMargins(0, 0, 4, 0); - _transformToolBar->addWidget(moveSpeedLabel); - - _transformMoveSpeedCombo = new QComboBox(_transformToolBar); - _transformMoveSpeedCombo->setFixedWidth(72); - _transformMoveSpeedCombo->setToolTip(tr("Camera movement speed (mirrors the Speed > Movement menu)")); - for (int v : {1, 2, 3, 5, 8, 10, 50, 100}) - _transformMoveSpeedCombo->addItem(tr("x%1").arg(v), v); - _transformToolBar->addWidget(_transformMoveSpeedCombo); - connect(_transformMoveSpeedCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { - if (!_viewport) return; - _viewport->camera.setPhysicsSpeed(_transformMoveSpeedCombo->itemData(idx).toInt()); - }); - - addFixedSpacer(8); - - auto* rotSpeedLabel = new QLabel(tr("Camera Rot:"), _transformToolBar); - rotSpeedLabel->setContentsMargins(0, 0, 4, 0); - _transformToolBar->addWidget(rotSpeedLabel); - - _transformRotSpeedCombo = new QComboBox(_transformToolBar); - _transformRotSpeedCombo->setFixedWidth(72); - _transformRotSpeedCombo->setToolTip(tr("Camera rotation speed (mirrors the Speed > Rotation menu)")); - // Labels match the existing menu (physics_rot / ~2 ≈ displayed multiplier) - for (auto [label, val] : std::initializer_list>{ - {"x1", 2}, {"x5", 10}, {"x12", 25}, {"x25", 50}, {"x50", 100}}) - _transformRotSpeedCombo->addItem(tr(label), val); - _transformToolBar->addWidget(_transformRotSpeedCombo); - connect(_transformRotSpeedCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { - if (!_viewport) return; - _viewport->camera.setPhysicsRot(_transformRotSpeedCombo->itemData(idx).toInt()); - }); - - addFixedSpacer(8); - - // ---- Single expanding spacer pushes everything to the right side ------- - auto* leftSpacer = new QWidget(_transformToolBar); - leftSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); - _transformToolBar->addWidget(leftSpacer); - - // ---- IFF / Team selector ----------------------------------------------- - // IFF items are populated lazily in onUpdateTransformBar() once Iff_info is loaded. - auto* iffLabel = new QLabel(tr("IFF:"), _transformToolBar); - iffLabel->setContentsMargins(0, 0, 4, 0); - _transformToolBar->addWidget(iffLabel); - - _transformIffCombo = new QComboBox(_transformToolBar); - _transformIffCombo->setFixedWidth(130); - _transformIffCombo->setToolTip(tr("IFF of the selected ship(s). Changes apply to all marked ships.")); - _transformToolBar->addWidget(_transformIffCombo); - connect(_transformIffCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { - if (idx < 0 || !_viewport) return; - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - if (p->type == OBJ_SHIP || p->type == OBJ_START) - Ships[p->instance].team = idx; - } - fred->missionChanged(); - }); - - addFixedSpacer(8); - - // ---- Local-axes toggle ---- - _transformLocalBtn = new QToolButton(_transformToolBar); - _transformLocalBtn->setCheckable(true); - _transformLocalBtn->setToolButtonStyle(Qt::ToolButtonIconOnly); - _transformLocalBtn->setFixedSize(28, 24); - _transformLocalBtn->setToolTip(tr("Local mode: in multi-selection, apply position/orientation as a delta to each object rather than setting all to the same absolute value. (X)")); - // FRED2 bound "Rotate Locally" to the X key; route it through the button so the per-mode local memory stays in sync. - _transformLocalBtn->setShortcut(QKeySequence(Qt::Key_X)); - bindThemeIcon(_transformLocalBtn, QStringLiteral("rotlocal")); - _transformToolBar->addWidget(_transformLocalBtn); - connect(_transformLocalBtn, &QToolButton::toggled, this, [this](bool checked) { - if (!_viewport) return; - // Local ON = each object rotates/moves individually -> Group_rotate = false - // Local OFF = formation (group orbits leader) -> Group_rotate = true - _viewport->Group_rotate = !checked; - if (_viewport->Editing_mode == CursorMode::Moving) - _tbLocalMove = checked; - else if (_viewport->Editing_mode == CursorMode::Rotating) - _tbLocalRotate = checked; - }); - - addFixedSpacer(8); - - // ---- Spin boxes (position or orientation) ------------------------------ - _transformLabel = new QLabel(tr("Pos"), _transformToolBar); - _transformLabel->setContentsMargins(0, 0, 4, 0); - _transformLabel->setMinimumWidth(24); - _transformToolBar->addWidget(_transformLabel); - - auto makeSpinBox = [this](QLabel*& lbl, const QString& axisName, QDoubleSpinBox*& sb) { - lbl = new QLabel(axisName, _transformToolBar); - lbl->setContentsMargins(4, 0, 2, 0); - _transformToolBar->addWidget(lbl); - sb = new QDoubleSpinBox(_transformToolBar); - sb->setDecimals(TRANSFORM_BOX_DECIMALS); - sb->setRange(-99999.99, 99999.99); - sb->setFixedWidth(90); - sb->setKeyboardTracking(false); - _transformToolBar->addWidget(sb); - sb->installEventFilter(this); - }; - - makeSpinBox(_transformLabelA, tr("X"), _transformA); - makeSpinBox(_transformLabelB, tr("Y"), _transformB); - makeSpinBox(_transformLabelC, tr("Z"), _transformC); - - // ---- Object radius (read-only) ----------------------------------------- - addFixedSpacer(8); - - auto* radiusStaticLabel = new QLabel(tr("Radius:"), _transformToolBar); - radiusStaticLabel->setContentsMargins(0, 0, 4, 0); - _transformToolBar->addWidget(radiusStaticLabel); - - _transformRadiusLabel = new QLabel(tr("--"), _transformToolBar); - _transformRadiusLabel->setFixedWidth(64); - _transformRadiusLabel->setToolTip(tr("Bounding radius of the selected object")); - _transformToolBar->addWidget(_transformRadiusLabel); - - addFixedSpacer(8); - - // ---- Layer selector ---------------------------------------------------- - auto* layerLabel = new QLabel(tr("Layer:"), _transformToolBar); - layerLabel->setContentsMargins(0, 0, 4, 0); - _transformToolBar->addWidget(layerLabel); - - _transformLayerCombo = new QComboBox(_transformToolBar); - _transformLayerCombo->setFixedWidth(130); - _transformLayerCombo->setToolTip(tr("Layer of the selected object(s). Choosing a layer moves all marked objects to it.")); - _transformToolBar->addWidget(_transformLayerCombo); - connect(_transformLayerCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { - if (idx < 0 || !_viewport) return; - SCP_string layerName = _transformLayerCombo->itemText(idx).toUtf8().constData(); - _viewport->moveMarkedObjectsToLayer(layerName, nullptr); - fred->missionChanged(); - }); - - addFixedSpacer(12); - - // addToolBar is intentionally NOT called here... added after restoreState() in setEditor(). -} - -void FredView::onUpdateTransformBar() { - const int curObj = fred->currentObject; - const int numMarked = fred->getNumMarked(); - const bool valid = query_valid_object(curObj); - const bool rotateMode = _viewport->Editing_mode == CursorMode::Rotating; - const bool selectMode = _viewport->Editing_mode == CursorMode::Selecting; - const int rawType = valid ? Objects[curObj].type : -1; - const bool isShip = valid && (rawType == OBJ_SHIP || rawType == OBJ_START); - - // A change of the current object or of the mode makes the typed value incorrect. Give the focus back to - // the viewport before the code below changes the ranges, so that the box shows the new value. - if (transformBoxAxis(QApplication::focusWidget()) >= 0 && !transformEditIsCurrent()) { - ui->centralWidget->setFocus(Qt::OtherFocusReason); - } - - // ---- Spin box labels and ranges ---------------------------------------- - if (rotateMode) { - _transformLabel->setText(tr("Ori")); - _transformLabelA->setText(tr("H")); - _transformLabelB->setText(tr("P")); - _transformLabelC->setText(tr("B")); - _transformA->setRange(-360.0, 360.0); - _transformB->setRange(-360.0, 360.0); - _transformC->setRange(-360.0, 360.0); - } else { - _transformLabel->setText(tr("Pos")); - _transformLabelA->setText(tr("X")); - _transformLabelB->setText(tr("Y")); - _transformLabelC->setText(tr("Z")); - _transformA->setRange(-99999.99, 99999.99); - _transformB->setRange(-99999.99, 99999.99); - _transformC->setRange(-99999.99, 99999.99); - } - - const bool editable = valid && !selectMode; - _transformA->setEnabled(editable); - _transformB->setEnabled(editable); - _transformC->setEnabled(editable); - - // ---- Local-axes toggle: per-mode preference + inverted Group_rotate mapping ---- - // When the cursor mode changes, restore the last Local setting for that mode. - const int curModeInt = static_cast(_viewport->Editing_mode); - if (curModeInt != _tbCachedCursorMode) { - _tbCachedCursorMode = curModeInt; - if (_viewport->Editing_mode == CursorMode::Moving) - _viewport->Group_rotate = !_tbLocalMove; - else if (_viewport->Editing_mode == CursorMode::Rotating) - _viewport->Group_rotate = !_tbLocalRotate; - // Select mode: leave Group_rotate unchanged - } - // Local ON = individual axes = Group_rotate false, so button reflects !Group_rotate - { - QSignalBlocker bl(_transformLocalBtn); - _transformLocalBtn->setChecked(!_viewport->Group_rotate); - } - _transformLocalBtn->setEnabled(editable); - - // ---- IFF combo: populate lazily once Iff_info is loaded by the game tables -- - if (!_tbIffPopulated && !Iff_info.empty()) { - _tbIffPopulated = true; - QSignalBlocker bl(_transformIffCombo); - for (const auto& iff : Iff_info) - _transformIffCombo->addItem(QString::fromUtf8(iff.iff_name)); - } - - // Find common IFF among marked ships; -1 means mixed or no ship selected. - int iffIndex = -1; - bool anyShip = isShip; - if (numMarked > 1) { - anyShip = false; - int firstIff = -2; - bool allSame = true; - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - if (p->type != OBJ_SHIP && p->type != OBJ_START) continue; - anyShip = true; - int team = Ships[p->instance].team; - if (firstIff == -2) { firstIff = team; } - else if (team != firstIff) { allSame = false; } - } - if (anyShip && allSame && firstIff >= 0) iffIndex = firstIff; - } else if (isShip) { - iffIndex = Ships[Objects[curObj].instance].team; - } - _transformIffCombo->setEnabled(anyShip); - { - QSignalBlocker bl(_transformIffCombo); - _transformIffCombo->setCurrentIndex(iffIndex); // -1 = blank for mixed - } - - // ---- Radius display: object radius for single, selection bounding radius for multi -- - if (valid && numMarked <= 1) { - _transformRadiusLabel->setText(QString::number(static_cast(Objects[curObj].radius), 'f', 1)); - } else if (numMarked > 1 && obj_used_list.next != nullptr) { - vec3d center; - float radius; - if (get_object_bounds(true, ¢er, &radius)) { - _transformRadiusLabel->setText(QString::number(static_cast(radius), 'f', 1)); - } else { - _transformRadiusLabel->setText(tr("--")); - } - } else { - _transformRadiusLabel->setText(tr("--")); - } - - // ---- Layer combo ------------------------------------------------------- - // Rebuild contents only when layer structure has changed. - if (_tbLayerComboDirty) { - const auto layerNames = _viewport->getLayerNames(); - QSignalBlocker bl(_transformLayerCombo); - _transformLayerCombo->clear(); - for (const auto& n : layerNames) - _transformLayerCombo->addItem(QString::fromUtf8(n.c_str())); - _tbLayerComboDirty = false; - } - - const bool anySelected = valid || numMarked > 0; - _transformLayerCombo->setEnabled(anySelected); - - // Find common layer index; -1 = blank for mixed layers. - int layerIdx = -1; - if (anySelected) { - if (numMarked <= 1 && valid) { - QString ln = QString::fromUtf8(_viewport->getObjectLayerName(curObj).c_str()); - layerIdx = _transformLayerCombo->findText(ln); - } else if (numMarked > 1) { - int firstLyr = -2; - bool allSame = true; - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (!p->flags[Object::Object_Flags::Marked]) continue; - int objIdx = static_cast(p - Objects); - int idx = _transformLayerCombo->findText( - QString::fromUtf8(_viewport->getObjectLayerName(objIdx).c_str())); - if (firstLyr == -2) { firstLyr = idx; } - else if (idx != firstLyr) { allSame = false; break; } - } - if (allSame && firstLyr >= 0) layerIdx = firstLyr; - } - } - { - QSignalBlocker bl(_transformLayerCombo); - _transformLayerCombo->setCurrentIndex(layerIdx); - } - - // ---- Spin box values (single selection only) --------------------------- - if (!valid) return; - - auto setIfUnfocused = [](QDoubleSpinBox* sb, double val) { - if (!sb->hasFocus()) sb->setValue(val); - }; - - if (rotateMode) { - angles ang{}; - vm_extract_angles_matrix(&ang, &Objects[curObj].orient); - setIfUnfocused(_transformA, fl_degrees(ang.h)); - setIfUnfocused(_transformB, fl_degrees(ang.p)); - setIfUnfocused(_transformC, fl_degrees(ang.b)); - } else { - const vec3d& pos = Objects[curObj].pos; - setIfUnfocused(_transformA, pos.xyz.x); - setIfUnfocused(_transformB, pos.xyz.y); - setIfUnfocused(_transformC, pos.xyz.z); - } -} - -bool FredView::eventFilter(QObject* watched, QEvent* event) { - const int axis = transformBoxAxis(watched); - if (axis < 0) { - return QMainWindow::eventFilter(watched, event); - } - auto* box = static_cast(watched); - - switch (event->type()) { - case QEvent::FocusIn: { - // The focus also comes back after a popup or a window change. The edit then continues, so keep the record. - const auto reason = static_cast(event)->reason(); - if (reason != Qt::PopupFocusReason && reason != Qt::ActiveWindowFocusReason) { - _transformEdit = {fred->currentObject, _viewport->Editing_mode, box->value()}; - } - break; - } - case QEvent::ShortcutOverride: { - // Do not let a menu shortcut or a control binding take Enter or Escape from the box - const int key = static_cast(event)->key(); - if (key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Escape) { - event->accept(); - return true; - } - break; - } - case QEvent::KeyPress: { - // Enter applies the value. Escape discards it. Both give the focus back to the viewport, so that the - // keyboard controls work again. All other focus changes discard the value. - const int key = static_cast(event)->key(); - if (key == Qt::Key_Return || key == Qt::Key_Enter) { - box->interpretText(); - applyTransformValue(axis, box->value()); - } else if (key != Qt::Key_Escape) { - break; - } - ui->centralWidget->setFocus(Qt::OtherFocusReason); - return true; - } - default: - break; - } - return QMainWindow::eventFilter(watched, event); -} - -int FredView::transformBoxAxis(const QObject* object) const { - if (object == nullptr) return -1; - if (object == _transformA) return 0; - if (object == _transformB) return 1; - if (object == _transformC) return 2; - return -1; -} - -bool FredView::transformEditIsCurrent() const { - return fred->currentObject == _transformEdit.object && _viewport->Editing_mode == _transformEdit.mode; -} - -void FredView::applyTransformValue(int axis, double value) { - // The selection or the mode can change while the box has the focus. Then the value is not for this object. - if (!transformEditIsCurrent()) return; - - const int curObj = fred->currentObject; - if (!query_valid_object(curObj)) return; - - // Change only the axis of the edited box. The other boxes show rounded values. If the code also wrote them, - // the object would move on the axes that the user did not change. - const bool rotateMode = _viewport->Editing_mode == CursorMode::Rotating; - const bool isMulti = fred->getNumMarked() > 1; - // Local ON (= !Group_rotate) with more than one object: add the change of the current object to - // each marked object. Otherwise, set the value on each marked object. - const bool relative = isMulti && !_viewport->Group_rotate; - - // More than one object with Local OFF: Enter sets the value on all the marked objects, also if it did not change - const bool alignAll = isMulti && !relative; - - // If the user did not change the value, do not write the rounded value back to the object - const double scale = std::pow(10.0, TRANSFORM_BOX_DECIMALS); - const bool unchanged = std::abs(value - _transformEdit.startValue) < 0.5 / scale; - if (unchanged && !alignAll) return; - - auto angleOf = [axis](angles& a) -> float& { return (axis == 0) ? a.h : (axis == 1) ? a.p : a.b; }; - - float current; - if (rotateMode) { - angles ang{}; - vm_extract_angles_matrix(&ang, &Objects[curObj].orient); - current = fl_degrees(angleOf(ang)); - } else { - current = Objects[curObj].pos.a1d[axis]; - } - - const auto newValue = static_cast(value); - const float delta = newValue - current; - - auto apply = [&](object* p) { - if (rotateMode) { - angles a{}; - vm_extract_angles_matrix(&a, &p->orient); - angleOf(a) = relative ? angleOf(a) + fl_radians(delta) : fl_radians(newValue); - vm_angles_2_matrix(&p->orient, &a); - } else { - p->pos.a1d[axis] = relative ? p->pos.a1d[axis] + delta : newValue; - } - }; - - auto forEachTarget = [&](auto&& fn) { - if (isMulti) { - for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { - if (p->flags[Object::Object_Flags::Marked]) - fn(p); - } - } else { - fn(&Objects[curObj]); - } - }; - - forEachTarget(apply); - // Move the docked partners after all the objects changed - forEachTarget(object_moved); - - fred->missionChanged(); -} - void FredView::updateUI() { if (!_viewport) { // The following code requires a valid viewport @@ -1729,7 +1276,7 @@ void FredView::ensureViewportFocus() { // Do not take the focus from a transform bar box while the user types a value. The toolbar is not a // dock widget. Enter, Escape, or a click in the viewport gives the focus back. - if (transformBoxAxis(focusedWidget) >= 0) { + if (_transformBar != nullptr && _transformBar->isTransformBox(focusedWidget)) { return; } @@ -2323,8 +1870,8 @@ void FredView::closeEvent(QCloseEvent* event) { QSettings settings; settings.setValue("FredView/mainWindowState", saveState()); settings.setValue("FredView/geometry", saveGeometry()); - settings.setValue("FredView/transformLocalMove", _tbLocalMove); - settings.setValue("FredView/transformLocalRotate", _tbLocalRotate); + if (_transformBar != nullptr) + _transformBar->saveSettings(); // Camera speeds are persisted on change in onUpdateViewSpeeds(), so no need to save them here. if (!maybePromptToSaveMissionChanges(tr("closing QtFRED"))) { @@ -2371,26 +1918,6 @@ void FredView::onUpdateViewSpeeds() { ui->actionRotx25->setChecked(_viewport->camera.getPhysicsRot() == 50); ui->actionRotx50->setChecked(_viewport->camera.getPhysicsRot() == 100); - // Keep the bottom-bar combos in sync (covers changes made via keyboard shortcuts or menu). - if (_transformMoveSpeedCombo) { - QSignalBlocker bl(_transformMoveSpeedCombo); - for (int i = 0; i < _transformMoveSpeedCombo->count(); ++i) { - if (_transformMoveSpeedCombo->itemData(i).toInt() == _viewport->camera.getPhysicsSpeed()) { - _transformMoveSpeedCombo->setCurrentIndex(i); - break; - } - } - } - if (_transformRotSpeedCombo) { - QSignalBlocker bl(_transformRotSpeedCombo); - for (int i = 0; i < _transformRotSpeedCombo->count(); ++i) { - if (_transformRotSpeedCombo->itemData(i).toInt() == _viewport->camera.getPhysicsRot()) { - _transformRotSpeedCombo->setCurrentIndex(i); - break; - } - } - } - // Persist immediately when a speed changes (via toolbar, menu, or keyboard) so the choice // survives even an unclean exit. Only writes on an actual change to avoid per-idle churn. const int moveSpeed = _viewport->camera.getPhysicsSpeed(); diff --git a/qtfred/src/ui/FredView.h b/qtfred/src/ui/FredView.h index 981f4b84047..1f2c4cfdeee 100644 --- a/qtfred/src/ui/FredView.h +++ b/qtfred/src/ui/FredView.h @@ -5,10 +5,7 @@ #include #include #include -#include -#include #include -#include #include #include @@ -37,6 +34,7 @@ class PropEditorDialog; } class SceneBrowserPanel; +class TransformToolBar; namespace Ui { class FredView; @@ -319,14 +317,6 @@ class FredView: public QMainWindow, public IDialogProvider { void onUpdateContextToolbar(); void quickRenameCurrentObject(); - void initializeTransformBar(); - void onUpdateTransformBar(); - bool eventFilter(QObject* watched, QEvent* event) override; - // Returns 0, 1, or 2 for the X/Y/Z (or H/P/B) box of the transform toolbar, and -1 for all other objects - int transformBoxAxis(const QObject* object) const; - bool transformEditIsCurrent() const; - void applyTransformValue(int axis, double value); - QToolBar* _contextToolBar = nullptr; QLabel* _contextLabel = nullptr; @@ -338,36 +328,10 @@ class FredView: public QMainWindow, public IDialogProvider { int _ctxCachedSharedWing = -2; // multi-select: shared wing index (-1=none, -2=N/A) waypoint_list* _ctxCachedSharedWaypointList = nullptr; - QToolBar* _transformToolBar = nullptr; - QLabel* _transformLabel = nullptr; - QLabel* _transformLabelA = nullptr; - QLabel* _transformLabelB = nullptr; - QLabel* _transformLabelC = nullptr; - QDoubleSpinBox* _transformA = nullptr; - QDoubleSpinBox* _transformB = nullptr; - QDoubleSpinBox* _transformC = nullptr; - // The state when a transform box got the focus. Enter applies the typed value only if the current object - // and the mode did not change, and only if the value is different from the start value. - struct TransformEdit { - int object = -1; - CursorMode mode = CursorMode::Selecting; - double startValue = 0.0; - }; - TransformEdit _transformEdit; - QComboBox* _transformMoveSpeedCombo = nullptr; - QComboBox* _transformRotSpeedCombo = nullptr; + TransformToolBar* _transformBar = nullptr; // Last camera speeds written to QSettings; lets us persist on change instead of only on close. int _lastSavedCameraSpeedMove = -1; int _lastSavedCameraSpeedRot = -1; - QComboBox* _transformIffCombo = nullptr; - QLabel* _transformRadiusLabel = nullptr; - QToolButton* _transformLocalBtn = nullptr; - QComboBox* _transformLayerCombo = nullptr; - bool _tbLayerComboDirty = true; // rebuild layer combo only when layer structure changes - bool _tbIffPopulated = false; // IFF items are populated lazily (tables load after init) - bool _tbLocalMove = false; // remembered Local preference while in move mode - bool _tbLocalRotate = false; // remembered Local preference while in rotate mode - int _tbCachedCursorMode = -1; // -1 forces per-mode restore on first update void onShipClassSelected(int ship_class); void onPropClassSelected(int prop_class); diff --git a/qtfred/src/ui/widgets/TransformToolBar.cpp b/qtfred/src/ui/widgets/TransformToolBar.cpp new file mode 100644 index 00000000000..f5e0daec02e --- /dev/null +++ b/qtfred/src/ui/widgets/TransformToolBar.cpp @@ -0,0 +1,529 @@ +#include "TransformToolBar.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "mission/Editor.h" +#include "mission/EditorViewport.h" +#include "mission/object.h" +#include "ui/Theme.h" + +#include + +namespace fso::fred { + +namespace { + +// Number of decimals in the boxes +constexpr int BOX_DECIMALS = 2; + +constexpr double POSITION_LIMIT = 99999.99; +constexpr double ANGLE_LIMIT = 360.0; + +const char* const SETTING_LOCAL_MOVE = "FredView/transformLocalMove"; +const char* const SETTING_LOCAL_ROTATE = "FredView/transformLocalRotate"; + +bool isShipType(int type) +{ + return type == OBJ_SHIP || type == OBJ_START; +} + +// Selects the item with the given data value, without a signal +void selectByData(QComboBox* combo, int value) +{ + QSignalBlocker blocker(combo); + const int index = combo->findData(value); + if (index >= 0) { + combo->setCurrentIndex(index); + } +} + +} // namespace + +TransformToolBar::TransformToolBar(QToolBar* toolbar, Editor* editor, EditorViewport* viewport, QWidget* viewportWidget) + : QObject(toolbar), _toolbar(toolbar), _editor(editor), _viewport(viewport), _viewportWidget(viewportWidget) +{ + QSettings settings; + _localMove = settings.value(SETTING_LOCAL_MOVE, false).toBool(); + _localRotate = settings.value(SETTING_LOCAL_ROTATE, false).toBool(); + + addWidgets(); +} + +void TransformToolBar::saveSettings() const +{ + QSettings settings; + settings.setValue(SETTING_LOCAL_MOVE, _localMove); + settings.setValue(SETTING_LOCAL_ROTATE, _localRotate); +} + +void TransformToolBar::markLayersChanged() +{ + _layersChanged = true; +} + +bool TransformToolBar::isTransformBox(const QObject* object) const +{ + return boxAxis(object) >= 0; +} + +int TransformToolBar::boxAxis(const QObject* object) const +{ + if (object == nullptr) { + return -1; + } + for (int axis = 0; axis < 3; ++axis) { + if (object == _boxes[axis]) { + return axis; + } + } + return -1; +} + +void TransformToolBar::addFixedSpacer(int width) +{ + auto* spacer = new QWidget(_toolbar); + spacer->setFixedWidth(width); + _toolbar->addWidget(spacer); +} + +void TransformToolBar::addWidgets() +{ + _toolbar->setContextMenuPolicy(Qt::PreventContextMenu); + _toolbar->setVisible(true); + + auto addLabel = [this](const QString& text) { + auto* label = new QLabel(text, _toolbar); + label->setContentsMargins(0, 0, 4, 0); + _toolbar->addWidget(label); + return label; + }; + + // ---- Left section: camera move and rotation speed selectors --------------- + addFixedSpacer(8); + + addLabel(tr("Camera Move:")); + _moveSpeedCombo = new QComboBox(_toolbar); + _moveSpeedCombo->setFixedWidth(72); + _moveSpeedCombo->setToolTip(tr("Camera movement speed (mirrors the Speed > Movement menu)")); + for (int v : {1, 2, 3, 5, 8, 10, 50, 100}) + _moveSpeedCombo->addItem(tr("x%1").arg(v), v); + _toolbar->addWidget(_moveSpeedCombo); + connect(_moveSpeedCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { + _viewport->camera.setPhysicsSpeed(_moveSpeedCombo->itemData(idx).toInt()); + }); + + addFixedSpacer(8); + + addLabel(tr("Camera Rot:")); + _rotSpeedCombo = new QComboBox(_toolbar); + _rotSpeedCombo->setFixedWidth(72); + _rotSpeedCombo->setToolTip(tr("Camera rotation speed (mirrors the Speed > Rotation menu)")); + // Labels match the existing menu (physics_rot / ~2 ≈ displayed multiplier) + for (auto [label, val] : std::initializer_list>{ + {"x1", 2}, {"x5", 10}, {"x12", 25}, {"x25", 50}, {"x50", 100}}) + _rotSpeedCombo->addItem(tr(label), val); + _toolbar->addWidget(_rotSpeedCombo); + connect(_rotSpeedCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { + _viewport->camera.setPhysicsRot(_rotSpeedCombo->itemData(idx).toInt()); + }); + + addFixedSpacer(8); + + // ---- Single expanding spacer pushes everything to the right side ------- + auto* leftSpacer = new QWidget(_toolbar); + leftSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + _toolbar->addWidget(leftSpacer); + + // ---- IFF / Team selector ----------------------------------------------- + // The IFF items are added in update(), after the tables fill Iff_info. + addLabel(tr("IFF:")); + _iffCombo = new QComboBox(_toolbar); + _iffCombo->setFixedWidth(130); + _iffCombo->setToolTip(tr("IFF of the selected ship(s). Changes apply to all marked ships.")); + _toolbar->addWidget(_iffCombo); + connect(_iffCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { + if (idx < 0) return; + for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { + if (p->flags[Object::Object_Flags::Marked] && isShipType(p->type)) + Ships[p->instance].team = idx; + } + _editor->missionChanged(); + }); + + addFixedSpacer(8); + + // ---- Local-axes toggle ---- + _localButton = new QToolButton(_toolbar); + _localButton->setCheckable(true); + _localButton->setToolButtonStyle(Qt::ToolButtonIconOnly); + _localButton->setFixedSize(28, 24); + _localButton->setToolTip(tr("Local mode: in multi-selection, apply position/orientation as a delta to each object rather than setting all to the same absolute value. (X)")); + // FRED2 bound "Rotate Locally" to the X key; route it through the button so the per-mode local memory stays in sync. + _localButton->setShortcut(QKeySequence(Qt::Key_X)); + bindThemeIcon(_localButton, QStringLiteral("rotlocal")); + _toolbar->addWidget(_localButton); + connect(_localButton, &QToolButton::toggled, this, [this](bool checked) { + // Local ON = each object rotates/moves individually -> Group_rotate = false + // Local OFF = formation (group orbits leader) -> Group_rotate = true + _viewport->Group_rotate = !checked; + if (_viewport->Editing_mode == CursorMode::Moving) + _localMove = checked; + else if (_viewport->Editing_mode == CursorMode::Rotating) + _localRotate = checked; + }); + + addFixedSpacer(8); + + // ---- Spin boxes (position or orientation) ------------------------------ + _modeLabel = addLabel(tr("Pos")); + _modeLabel->setMinimumWidth(24); + + for (auto& box : _boxes) { + const int axis = static_cast(&box - _boxes.data()); + auto*& label = _axisLabels[axis]; + label = new QLabel(_toolbar); + label->setContentsMargins(4, 0, 2, 0); + _toolbar->addWidget(label); + + box = new QDoubleSpinBox(_toolbar); + box->setDecimals(BOX_DECIMALS); + box->setRange(-POSITION_LIMIT, POSITION_LIMIT); + box->setFixedWidth(90); + box->setKeyboardTracking(false); + _toolbar->addWidget(box); + box->installEventFilter(this); + } + _axisLabels[0]->setText(tr("X")); + _axisLabels[1]->setText(tr("Y")); + _axisLabels[2]->setText(tr("Z")); + + // ---- Object radius (read-only) ----------------------------------------- + addFixedSpacer(8); + + addLabel(tr("Radius:")); + _radiusLabel = new QLabel(tr("--"), _toolbar); + _radiusLabel->setFixedWidth(64); + _radiusLabel->setToolTip(tr("Bounding radius of the selected object")); + _toolbar->addWidget(_radiusLabel); + + addFixedSpacer(8); + + // ---- Layer selector ---------------------------------------------------- + addLabel(tr("Layer:")); + _layerCombo = new QComboBox(_toolbar); + _layerCombo->setFixedWidth(130); + _layerCombo->setToolTip(tr("Layer of the selected object(s). Choosing a layer moves all marked objects to it.")); + _toolbar->addWidget(_layerCombo); + connect(_layerCombo, QOverload::of(&QComboBox::activated), this, [this](int idx) { + if (idx < 0) return; + SCP_string layerName = _layerCombo->itemText(idx).toUtf8().constData(); + _viewport->moveMarkedObjectsToLayer(layerName, nullptr); + _editor->missionChanged(); + }); + + addFixedSpacer(12); +} + +void TransformToolBar::update() +{ + const int curObj = _editor->currentObject; + const int numMarked = _editor->getNumMarked(); + const bool valid = query_valid_object(curObj); + const bool rotateMode = _viewport->Editing_mode == CursorMode::Rotating; + const bool selectMode = _viewport->Editing_mode == CursorMode::Selecting; + const bool isShip = valid && isShipType(Objects[curObj].type); + const bool editable = valid && !selectMode; + + updateBoxes(valid, rotateMode, editable); + updateLocalButton(editable); + updateCameraSpeeds(); + updateIff(valid, isShip, numMarked); + updateRadius(valid, numMarked); + updateLayer(valid, numMarked); +} + +void TransformToolBar::updateBoxes(bool valid, bool rotateMode, bool editable) +{ + // A change of the current object or of the mode makes the typed value incorrect. Give the focus back to the + // viewport before the code below changes the ranges, so that the box shows the new value. + if (isTransformBox(QApplication::focusWidget()) && !editIsCurrent()) { + _viewportWidget->setFocus(Qt::OtherFocusReason); + } + + _modeLabel->setText(rotateMode ? tr("Ori") : tr("Pos")); + const QString axisNames[3] = {rotateMode ? tr("H") : tr("X"), rotateMode ? tr("P") : tr("Y"), + rotateMode ? tr("B") : tr("Z")}; + const double limit = rotateMode ? ANGLE_LIMIT : POSITION_LIMIT; + + float values[3] = {}; + if (valid) { + if (rotateMode) { + angles ang{}; + vm_extract_angles_matrix(&ang, &Objects[_editor->currentObject].orient); + values[0] = fl_degrees(ang.h); + values[1] = fl_degrees(ang.p); + values[2] = fl_degrees(ang.b); + } else { + const vec3d& pos = Objects[_editor->currentObject].pos; + values[0] = pos.xyz.x; + values[1] = pos.xyz.y; + values[2] = pos.xyz.z; + } + } + + for (int axis = 0; axis < 3; ++axis) { + auto* box = _boxes[axis]; + _axisLabels[axis]->setText(axisNames[axis]); + box->setRange(-limit, limit); + box->setEnabled(editable); + // Do not change the value while the user types in the box + if (valid && !box->hasFocus()) { + box->setValue(values[axis]); + } + } +} + +void TransformToolBar::updateLocalButton(bool editable) +{ + // When the cursor mode changes, restore the last Local setting for that mode + const int mode = static_cast(_viewport->Editing_mode); + if (mode != _cachedMode) { + _cachedMode = mode; + if (_viewport->Editing_mode == CursorMode::Moving) + _viewport->Group_rotate = !_localMove; + else if (_viewport->Editing_mode == CursorMode::Rotating) + _viewport->Group_rotate = !_localRotate; + // Select mode: leave Group_rotate unchanged + } + + // Local ON = individual axes = Group_rotate false, so the button shows !Group_rotate + { + QSignalBlocker blocker(_localButton); + _localButton->setChecked(!_viewport->Group_rotate); + } + _localButton->setEnabled(editable); +} + +void TransformToolBar::updateCameraSpeeds() +{ + // Keyboard shortcuts and the Speed menu also change the speeds + selectByData(_moveSpeedCombo, _viewport->camera.getPhysicsSpeed()); + selectByData(_rotSpeedCombo, _viewport->camera.getPhysicsRot()); +} + +void TransformToolBar::updateIff(bool valid, bool isShip, int numMarked) +{ + if (!_iffPopulated && !Iff_info.empty()) { + _iffPopulated = true; + QSignalBlocker blocker(_iffCombo); + for (const auto& iff : Iff_info) + _iffCombo->addItem(QString::fromUtf8(iff.iff_name)); + } + + // Find the common IFF of the marked ships. -1 means mixed or no ship. + int iffIndex = -1; + bool anyShip = isShip; + if (numMarked > 1) { + anyShip = false; + int firstIff = -2; + bool allSame = true; + for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { + if (!p->flags[Object::Object_Flags::Marked] || !isShipType(p->type)) continue; + anyShip = true; + const int team = Ships[p->instance].team; + if (firstIff == -2) { firstIff = team; } + else if (team != firstIff) { allSame = false; } + } + if (anyShip && allSame && firstIff >= 0) iffIndex = firstIff; + } else if (valid && isShip) { + iffIndex = Ships[Objects[_editor->currentObject].instance].team; + } + + _iffCombo->setEnabled(anyShip); + QSignalBlocker blocker(_iffCombo); + _iffCombo->setCurrentIndex(iffIndex); // -1 = blank for mixed +} + +void TransformToolBar::updateRadius(bool valid, int numMarked) +{ + // The radius of the object for one object, and the bounding radius of the selection for more + float radius = 0.0f; + bool known = false; + if (valid && numMarked <= 1) { + radius = Objects[_editor->currentObject].radius; + known = true; + } else if (numMarked > 1 && obj_used_list.next != nullptr) { + vec3d center; + known = get_object_bounds(true, ¢er, &radius); + } + _radiusLabel->setText(known ? QString::number(static_cast(radius), 'f', 1) : tr("--")); +} + +void TransformToolBar::updateLayer(bool valid, int numMarked) +{ + if (_layersChanged) { + QSignalBlocker blocker(_layerCombo); + _layerCombo->clear(); + for (const auto& name : _viewport->getLayerNames()) + _layerCombo->addItem(QString::fromUtf8(name.c_str())); + _layersChanged = false; + } + + auto layerIndexOf = [this](int objnum) { + return _layerCombo->findText(QString::fromUtf8(_viewport->getObjectLayerName(objnum).c_str())); + }; + + const bool anySelected = valid || numMarked > 0; + _layerCombo->setEnabled(anySelected); + + // Find the common layer. -1 = blank for mixed layers. + int layerIdx = -1; + if (numMarked <= 1 && valid) { + layerIdx = layerIndexOf(_editor->currentObject); + } else if (numMarked > 1) { + int firstLayer = -2; + bool allSame = true; + for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { + if (!p->flags[Object::Object_Flags::Marked]) continue; + const int idx = layerIndexOf(OBJ_INDEX(p)); + if (firstLayer == -2) { firstLayer = idx; } + else if (idx != firstLayer) { allSame = false; break; } + } + if (allSame && firstLayer >= 0) layerIdx = firstLayer; + } + + QSignalBlocker blocker(_layerCombo); + _layerCombo->setCurrentIndex(layerIdx); +} + +bool TransformToolBar::eventFilter(QObject* watched, QEvent* event) +{ + const int axis = boxAxis(watched); + if (axis < 0) { + return QObject::eventFilter(watched, event); + } + auto* box = _boxes[axis]; + + switch (event->type()) { + case QEvent::FocusIn: { + // The focus also comes back after a popup or a window change. The edit then continues, so keep the record. + const auto reason = static_cast(event)->reason(); + if (reason != Qt::PopupFocusReason && reason != Qt::ActiveWindowFocusReason) { + _edit = {_editor->currentObject, _viewport->Editing_mode, box->value()}; + } + break; + } + case QEvent::ShortcutOverride: { + // Do not let a menu shortcut or a control binding take Enter or Escape from the box + const int key = static_cast(event)->key(); + if (key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Escape) { + event->accept(); + return true; + } + break; + } + case QEvent::KeyPress: { + // Enter applies the value. Escape discards it. Both give the focus back to the viewport, so that the + // keyboard controls work again. All other focus changes discard the value. + const int key = static_cast(event)->key(); + if (key == Qt::Key_Return || key == Qt::Key_Enter) { + box->interpretText(); + applyValue(axis, box->value()); + } else if (key != Qt::Key_Escape) { + break; + } + _viewportWidget->setFocus(Qt::OtherFocusReason); + return true; + } + default: + break; + } + return QObject::eventFilter(watched, event); +} + +bool TransformToolBar::editIsCurrent() const +{ + return _editor->currentObject == _edit.object && _viewport->Editing_mode == _edit.mode; +} + +void TransformToolBar::applyValue(int axis, double value) +{ + // The selection or the mode can change while the box has the focus. Then the value is not for this object. + if (!editIsCurrent()) return; + + const int curObj = _editor->currentObject; + if (!query_valid_object(curObj)) return; + + // Change only the axis of the edited box. The other boxes show rounded values. If the code also wrote them, + // the object would move on the axes that the user did not change. + const bool rotateMode = _viewport->Editing_mode == CursorMode::Rotating; + const bool isMulti = _editor->getNumMarked() > 1; + // Local ON (= !Group_rotate) with more than one object: add the change of the current object to + // each marked object. Otherwise, set the value on each marked object. + const bool relative = isMulti && !_viewport->Group_rotate; + + // More than one object with Local OFF: Enter sets the value on all the marked objects, also if it did not change + const bool alignAll = isMulti && !relative; + + // If the user did not change the value, do not write the rounded value back to the object + const double scale = std::pow(10.0, BOX_DECIMALS); + const bool unchanged = std::abs(value - _edit.startValue) < 0.5 / scale; + if (unchanged && !alignAll) return; + + auto angleOf = [axis](angles& a) -> float& { return (axis == 0) ? a.h : (axis == 1) ? a.p : a.b; }; + + float current; + if (rotateMode) { + angles ang{}; + vm_extract_angles_matrix(&ang, &Objects[curObj].orient); + current = fl_degrees(angleOf(ang)); + } else { + current = Objects[curObj].pos.a1d[axis]; + } + + const auto newValue = static_cast(value); + const float delta = newValue - current; + + auto apply = [&](object* p) { + if (rotateMode) { + angles a{}; + vm_extract_angles_matrix(&a, &p->orient); + angleOf(a) = relative ? angleOf(a) + fl_radians(delta) : fl_radians(newValue); + vm_angles_2_matrix(&p->orient, &a); + } else { + p->pos.a1d[axis] = relative ? p->pos.a1d[axis] + delta : newValue; + } + }; + + // Change all the objects first, then move the docked partners + if (isMulti) { + for (object* p = GET_FIRST(&obj_used_list); p != END_OF_LIST(&obj_used_list); p = GET_NEXT(p)) { + if (p->flags[Object::Object_Flags::Marked]) + apply(p); + } + marked_objects_moved(); + } else { + apply(&Objects[curObj]); + object_moved(&Objects[curObj]); + } + + _editor->missionChanged(); +} + +} // namespace fso::fred diff --git a/qtfred/src/ui/widgets/TransformToolBar.h b/qtfred/src/ui/widgets/TransformToolBar.h new file mode 100644 index 00000000000..eb28a41f945 --- /dev/null +++ b/qtfred/src/ui/widgets/TransformToolBar.h @@ -0,0 +1,96 @@ +#pragma once + +#include + +#include + +#include + +class QComboBox; +class QDoubleSpinBox; +class QLabel; +class QToolBar; +class QToolButton; +class QWidget; + +namespace fso::fred { + +class Editor; +class EditorViewport; + +/** + * The bottom toolbar of the main window: the camera speeds, the IFF and the Local mode of the marked objects, the + * X/Y/Z (or H/P/B) boxes of the current object, its radius, and its layer. + * + * A box keeps the focus while the user types. Enter applies the typed value to the edited axis only. Escape and all + * other focus changes discard it. + */ +class TransformToolBar : public QObject { + Q_OBJECT + + public: + // Adds the widgets to toolbar. viewportWidget gets the focus back after an edit in a box. + TransformToolBar(QToolBar* toolbar, Editor* editor, EditorViewport* viewport, QWidget* viewportWidget); + + // Updates the widgets from the editor state. Call this function on each idle tick. + void update(); + + // The layer list changed. The layer combo box rebuilds its list at the next update. + void markLayersChanged(); + + // Returns true if the object is one of the three boxes. The viewport must not take the focus from such a box. + bool isTransformBox(const QObject* object) const; + + // Writes the Local preference of each mode to the settings + void saveSettings() const; + + protected: + bool eventFilter(QObject* watched, QEvent* event) override; + + private: + // The state when a box got the focus. Enter applies the typed value only if the current object and the mode + // did not change, and only if the value is different from the start value. + struct Edit { + int object = -1; + CursorMode mode = CursorMode::Selecting; + double startValue = 0.0; + }; + + void addWidgets(); + void addFixedSpacer(int width); + void updateBoxes(bool valid, bool rotateMode, bool editable); + void updateLocalButton(bool editable); + void updateCameraSpeeds(); + void updateIff(bool valid, bool isShip, int numMarked); + void updateRadius(bool valid, int numMarked); + void updateLayer(bool valid, int numMarked); + + // Returns 0, 1, or 2 for a box, and -1 for all other objects + int boxAxis(const QObject* object) const; + bool editIsCurrent() const; + void applyValue(int axis, double value); + + QToolBar* _toolbar; + Editor* _editor; + EditorViewport* _viewport; + QWidget* _viewportWidget; + + QComboBox* _moveSpeedCombo = nullptr; + QComboBox* _rotSpeedCombo = nullptr; + QComboBox* _iffCombo = nullptr; + QToolButton* _localButton = nullptr; + QLabel* _modeLabel = nullptr; + std::array _axisLabels{}; + std::array _boxes{}; + QLabel* _radiusLabel = nullptr; + QComboBox* _layerCombo = nullptr; + + Edit _edit; + bool _layersChanged = true; // rebuild the layer list only when the layers change + bool _iffPopulated = false; // the IFF list fills later, because the tables load after the toolbar + bool _localMove = false; // the Local preference in move mode + bool _localRotate = false; // the Local preference in rotate mode + int _cachedMode = -1; // -1 forces the restore of the Local preference at the first update +}; + +} // namespace fso::fred From ea69ac219a37cc6a7efbf4040ba9946c53e9b2d4 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:20:50 +0100 Subject: [PATCH 11/12] QtFRED: align the heights of the transform toolbar widgets QToolBar puts the tops of its widgets on one line. With the Fusion style, a spin box is taller than a combo box, so the X/Y/Z boxes went lower than the other widgets. The Local button was 1 pixel shorter. Now the boxes and the Local button have the height of the combo boxes. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/ui/widgets/TransformToolBar.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/qtfred/src/ui/widgets/TransformToolBar.cpp b/qtfred/src/ui/widgets/TransformToolBar.cpp index f5e0daec02e..09161f6093e 100644 --- a/qtfred/src/ui/widgets/TransformToolBar.cpp +++ b/qtfred/src/ui/widgets/TransformToolBar.cpp @@ -166,13 +166,17 @@ void TransformToolBar::addWidgets() _editor->missionChanged(); }); + // QToolBar aligns the tops of its widgets, and the widget types have different heights. Give the Local button + // and the boxes the height of the combo boxes, so that the bottom edges also align. + const int rowHeight = _iffCombo->sizeHint().height(); + addFixedSpacer(8); // ---- Local-axes toggle ---- _localButton = new QToolButton(_toolbar); _localButton->setCheckable(true); _localButton->setToolButtonStyle(Qt::ToolButtonIconOnly); - _localButton->setFixedSize(28, 24); + _localButton->setFixedSize(28, rowHeight); _localButton->setToolTip(tr("Local mode: in multi-selection, apply position/orientation as a delta to each object rather than setting all to the same absolute value. (X)")); // FRED2 bound "Rotate Locally" to the X key; route it through the button so the per-mode local memory stays in sync. _localButton->setShortcut(QKeySequence(Qt::Key_X)); @@ -205,6 +209,7 @@ void TransformToolBar::addWidgets() box->setDecimals(BOX_DECIMALS); box->setRange(-POSITION_LIMIT, POSITION_LIMIT); box->setFixedWidth(90); + box->setFixedHeight(rowHeight); box->setKeyboardTracking(false); _toolbar->addWidget(box); box->installEventFilter(this); From 1abd8a12f87e1146a5e82027413ba5864b689c78 Mon Sep 17 00:00:00 2001 From: the-e Date: Thu, 12 Nov 2026 17:25:21 +0100 Subject: [PATCH 12/12] QtFRED: apply a transform toolbar step at once A step with the mouse wheel, the arrow buttons, or the Up, Down, Page Up, and Page Down keys changed only the box. The user had to push Enter to apply it. Now each step that changes the value applies it to the object at once. Typed text still needs Enter. A small QDoubleSpinBox subclass reports each step from stepBy(). The step applies to the current object, also if the box did not have the focus: the box always shows the value of the current object. Co-Authored-By: Claude Opus 5.5 --- qtfred/src/ui/widgets/TransformToolBar.cpp | 36 +++++++++++++++++++++- qtfred/src/ui/widgets/TransformToolBar.h | 5 ++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/qtfred/src/ui/widgets/TransformToolBar.cpp b/qtfred/src/ui/widgets/TransformToolBar.cpp index 09161f6093e..dde633126a7 100644 --- a/qtfred/src/ui/widgets/TransformToolBar.cpp +++ b/qtfred/src/ui/widgets/TransformToolBar.cpp @@ -24,6 +24,7 @@ #include "ui/Theme.h" #include +#include namespace fso::fred { @@ -53,6 +54,24 @@ void selectByData(QComboBox* combo, int value) } } +// A spin box that reports each step that changes its value: the mouse wheel, the arrow buttons, and the Up, Down, +// Page Up, and Page Down keys. A step does not interpret typed text, so the toolbar can apply it at once. +class StepSpinBox : public QDoubleSpinBox { + public: + using QDoubleSpinBox::QDoubleSpinBox; + + std::function onStep; + + void stepBy(int steps) override + { + const double before = value(); + QDoubleSpinBox::stepBy(steps); + if (value() != before && onStep) { + onStep(); + } + } +}; + } // namespace TransformToolBar::TransformToolBar(QToolBar* toolbar, Editor* editor, EditorViewport* viewport, QWidget* viewportWidget) @@ -205,7 +224,9 @@ void TransformToolBar::addWidgets() label->setContentsMargins(4, 0, 2, 0); _toolbar->addWidget(label); - box = new QDoubleSpinBox(_toolbar); + auto* stepBox = new StepSpinBox(_toolbar); + stepBox->onStep = [this, axis]() { applyStep(axis); }; + box = stepBox; box->setDecimals(BOX_DECIMALS); box->setRange(-POSITION_LIMIT, POSITION_LIMIT); box->setFixedWidth(90); @@ -467,6 +488,19 @@ bool TransformToolBar::editIsCurrent() const return _editor->currentObject == _edit.object && _viewport->Editing_mode == _edit.mode; } +void TransformToolBar::applyStep(int axis) +{ + // The box shows the value of the current object: the update gives a box with the focus back to the viewport + // when the object or the mode changes. Thus the step is for the current object, also if the box did not have + // the focus before the step. + _edit.object = _editor->currentObject; + _edit.mode = _viewport->Editing_mode; + applyValue(axis, _boxes[axis]->value()); + + // A later Enter must not apply the same value again + _edit.startValue = _boxes[axis]->value(); +} + void TransformToolBar::applyValue(int axis, double value) { // The selection or the mode can change while the box has the focus. Then the value is not for this object. diff --git a/qtfred/src/ui/widgets/TransformToolBar.h b/qtfred/src/ui/widgets/TransformToolBar.h index eb28a41f945..3548846fd58 100644 --- a/qtfred/src/ui/widgets/TransformToolBar.h +++ b/qtfred/src/ui/widgets/TransformToolBar.h @@ -23,7 +23,8 @@ class EditorViewport; * X/Y/Z (or H/P/B) boxes of the current object, its radius, and its layer. * * A box keeps the focus while the user types. Enter applies the typed value to the edited axis only. Escape and all - * other focus changes discard it. + * other focus changes discard it. A step with the mouse wheel, the arrow buttons, or the Up and Down keys applies at + * once. */ class TransformToolBar : public QObject { Q_OBJECT @@ -68,6 +69,8 @@ class TransformToolBar : public QObject { // Returns 0, 1, or 2 for a box, and -1 for all other objects int boxAxis(const QObject* object) const; bool editIsCurrent() const; + // A step changed the value of a box. Apply it at once. + void applyStep(int axis); void applyValue(int axis, double value); QToolBar* _toolbar;