|
| 1 | +#include "RelativeCoordinatesDialogModel.h" |
| 2 | + |
| 3 | +#include "math/vecmat.h" |
| 4 | +#include <mission/object.h> |
| 5 | + |
| 6 | + |
| 7 | +namespace fso::fred::dialogs { |
| 8 | + |
| 9 | +RelativeCoordinatesDialogModel::RelativeCoordinatesDialogModel(QObject* parent, EditorViewport* viewport) |
| 10 | + : AbstractDialogModel(parent, viewport) |
| 11 | +{ |
| 12 | + _objects.clear(); |
| 13 | + |
| 14 | + for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) { |
| 15 | + bool added = false; |
| 16 | + |
| 17 | + int objnum = OBJ_INDEX(ptr); |
| 18 | + |
| 19 | + if (ptr->type == OBJ_START || ptr->type == OBJ_SHIP) { |
| 20 | + _objects.emplace_back(Ships[ptr->instance].ship_name, objnum); |
| 21 | + |
| 22 | + added = true; |
| 23 | + } else if (ptr->type == OBJ_WAYPOINT) { |
| 24 | + SCP_string text; |
| 25 | + int waypoint_num; |
| 26 | + |
| 27 | + auto wp_list = find_waypoint_list_with_instance(ptr->instance, &waypoint_num); |
| 28 | + Assert(wp_list != nullptr); |
| 29 | + text = wp_list->get_name(); |
| 30 | + text += ":"; |
| 31 | + text += std::to_string(waypoint_num + 1); |
| 32 | + _objects.emplace_back(text, objnum); |
| 33 | + |
| 34 | + added = true; |
| 35 | + } |
| 36 | + |
| 37 | + bool marked = (ptr->flags[Object::Object_Flags::Marked]); |
| 38 | + |
| 39 | + if (added && marked && _originIndex == -1) { |
| 40 | + _originIndex = objnum; // TODO: select first marked object as origin.. not sure QtFRED has cur_object_index available yet |
| 41 | + } else if (added && marked && _satelliteIndex == -1 && objnum != _originIndex) { |
| 42 | + _satelliteIndex = objnum; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + computeCoordinates(); |
| 47 | +} |
| 48 | + |
| 49 | +bool RelativeCoordinatesDialogModel::apply() |
| 50 | +{ |
| 51 | + // Read only dialog |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +void RelativeCoordinatesDialogModel::reject() |
| 56 | +{ |
| 57 | + // Read only dialog |
| 58 | +} |
| 59 | + |
| 60 | +float RelativeCoordinatesDialogModel::getDistance() const |
| 61 | +{ |
| 62 | + return _distance; |
| 63 | +} |
| 64 | +float RelativeCoordinatesDialogModel::getPitch() const |
| 65 | +{ |
| 66 | + return _orientation_p; |
| 67 | +} |
| 68 | +float RelativeCoordinatesDialogModel::getBank() const |
| 69 | +{ |
| 70 | + return _orientation_b; |
| 71 | +} |
| 72 | +float RelativeCoordinatesDialogModel::getHeading() const |
| 73 | +{ |
| 74 | + return _orientation_h; |
| 75 | +} |
| 76 | + |
| 77 | +int RelativeCoordinatesDialogModel::getOrigin() const |
| 78 | +{ |
| 79 | + return _originIndex; |
| 80 | +} |
| 81 | + |
| 82 | +void RelativeCoordinatesDialogModel::setOrigin(int index) |
| 83 | +{ |
| 84 | + if (_originIndex == index) |
| 85 | + return; |
| 86 | + _originIndex = index; |
| 87 | + computeCoordinates(); |
| 88 | +} |
| 89 | + |
| 90 | +int RelativeCoordinatesDialogModel::getSatellite() const |
| 91 | +{ |
| 92 | + return _satelliteIndex; |
| 93 | +} |
| 94 | + |
| 95 | +void RelativeCoordinatesDialogModel::setSatellite(int index) |
| 96 | +{ |
| 97 | + if (_satelliteIndex == index) |
| 98 | + return; |
| 99 | + _satelliteIndex = index; |
| 100 | + computeCoordinates(); |
| 101 | +} |
| 102 | + |
| 103 | +SCP_vector<std::pair<SCP_string, int>> RelativeCoordinatesDialogModel::getObjectsList() const |
| 104 | +{ |
| 105 | + return _objects; |
| 106 | +} |
| 107 | + |
| 108 | +void RelativeCoordinatesDialogModel::computeCoordinates() |
| 109 | +{ |
| 110 | + if (_originIndex < 0 || _satelliteIndex < 0 || (_originIndex == _satelliteIndex)) { |
| 111 | + _distance = 0.0f; |
| 112 | + _orientation_p = 0.0f; |
| 113 | + _orientation_b = 0.0f; |
| 114 | + _orientation_h = 0.0f; |
| 115 | + |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + auto origin_pos = Objects[_originIndex].pos; |
| 120 | + auto satellite_pos = Objects[_satelliteIndex].pos; |
| 121 | + |
| 122 | + // distance |
| 123 | + _distance = vm_vec_dist(&origin_pos, &satellite_pos); |
| 124 | + |
| 125 | + // transform the coordinate frame |
| 126 | + vec3d delta_vec, local_vec; |
| 127 | + vm_vec_sub(&delta_vec, &satellite_pos, &origin_pos); |
| 128 | + if (Objects[_originIndex].type != OBJ_WAYPOINT) |
| 129 | + vm_vec_rotate(&local_vec, &delta_vec, &Objects[_originIndex].orient); |
| 130 | + |
| 131 | + // find the orientation |
| 132 | + matrix m; |
| 133 | + vm_vector_2_matrix(&m, &local_vec); |
| 134 | + |
| 135 | + // find the angles |
| 136 | + angles ang; |
| 137 | + vm_extract_angles_matrix(&ang, &m); |
| 138 | + _orientation_p = to_degrees(ang.p); |
| 139 | + _orientation_b = to_degrees(ang.b); |
| 140 | + _orientation_h = to_degrees(ang.h); |
| 141 | +} |
| 142 | + |
| 143 | +float RelativeCoordinatesDialogModel::to_degrees(float rad) |
| 144 | +{ |
| 145 | + float deg = fl_degrees(rad); |
| 146 | + return normalize_degrees(deg); |
| 147 | +} |
| 148 | + |
| 149 | +float RelativeCoordinatesDialogModel::normalize_degrees(float deg) |
| 150 | +{ |
| 151 | + while (deg < -180.0f) |
| 152 | + deg += 180.0f; |
| 153 | + while (deg > 180.0f) |
| 154 | + deg -= 180.0f; |
| 155 | + // check for negative zero... |
| 156 | + if (deg == -0.0f) |
| 157 | + return 0.0f; |
| 158 | + return deg; |
| 159 | +} |
| 160 | + |
| 161 | +} // namespace fso::fred::dialogs |
0 commit comments