Skip to content

Commit

Permalink
Fixes/improvements to the system map
Browse files Browse the repository at this point in the history
- Separate grid color and grid leg color, adjust that colors.
- Fix little problem with popup menu (sometimes is started showing the
wrong object)

src/SystemView.cpp:

- Refactor zooming in the SystemView: make zoom part of the camera space
matrix. Now, multiplying by m_zoom is done only once, (there were 17).
- Fix that smooth transitions did not affect the grid
- Add clamping the camera pitch, like in the SectorView
- Add grid legs for every "icon object" in the system map
  • Loading branch information
Gliese852 committed Jun 5, 2020
1 parent 1e2e471 commit 9d8be40
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 67 deletions.
9 changes: 5 additions & 4 deletions data/pigui/modules/system-view-ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ local svColor = {
BUTTON_BACK = colors.buttonBlue,
BUTTON_INK = colors.white,
COMBAT_TARGET = colors.combatTarget,
GRID = Color(25,25,25),
GRID = Color(40,40,40),
GRID_LEG = Color(120,120,120),
LAGRANGE = Color(0,214,226),
NAV_TARGET = colors.navTarget,
OBJECT = colors.frame,
Expand Down Expand Up @@ -366,8 +367,8 @@ local function displayOnScreenObjects()

ui.addIcon(mainCoords, getBodyIcon(mainObject), getColor(mainObject), iconsize, ui.anchor.center, ui.anchor.center)

local label = getLabel(mainObject)
if should_show_label then
local label = getLabel(mainObject)
if group.objects then
label = label .. " (" .. #group.objects .. ")"
end
Expand All @@ -379,11 +380,11 @@ local function displayOnScreenObjects()
-- mouse release handler for right button
if (mp - mainCoords):length() < click_radius then
if not ui.isAnyWindowHovered() and ui.isMouseReleased(1) then
ui.openPopup("target" .. objectCounter)
ui.openPopup("target" .. label)
end
end
-- make popup
ui.popup("target" .. objectCounter, function()
ui.popup("target" .. label, function()
local isObject = mainObject.type == Projectable.OBJECT
local isSystemBody = isObject and mainObject.base == Projectable.SYSTEMBODY
local isShip = isObject and not isSystemBody and mainObject.ref:IsShip()
Expand Down
96 changes: 43 additions & 53 deletions src/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void SystemView::PutOrbit(Projectable::bases base, RefType *ref, const Orbit *or
if (fadingColors == 0 && t >= startTrailPercent * maxT)
fadingColors = i;
const vector3d pos = orbit->EvenSpacedPosTrajectory(t, tMinust0);
m_orbitVts[i] = vector3f(offset + pos * double(m_zoom));
m_orbitVts[i] = vector3f(offset + pos);
++num_vertices;
if (pos.Length() < planetRadius)
break;
Expand Down Expand Up @@ -338,20 +338,20 @@ void SystemView::PutOrbit(Projectable::bases base, RefType *ref, const Orbit *or

Gui::Screen::EnterOrtho();
vector3d pos;
if (Gui::Screen::Project(offset + orbit->Perigeum() * double(m_zoom), pos) && pos.z < 1)
AddProjected<RefType>(Projectable::PERIAPSIS, base, ref, pos);
if (Gui::Screen::Project(offset + orbit->Apogeum() * double(m_zoom), pos) && pos.z < 1)
AddProjected<RefType>(Projectable::APOAPSIS, base, ref, pos);
if (Gui::Screen::Project(offset + orbit->Perigeum(), pos) && pos.z < 1)
AddProjected<RefType>(Projectable::PERIAPSIS, base, ref, pos, offset + orbit->Perigeum());
if (Gui::Screen::Project(offset + orbit->Apogeum(), pos) && pos.z < 1)
AddProjected<RefType>(Projectable::APOAPSIS, base, ref, pos, offset + orbit->Apogeum());

if (showLagrange && m_showL4L5 != LAG_OFF) {
const vector3d posL4 = orbit->EvenSpacedPosTrajectory((1.0 / 360.0) * 60.0, tMinust0);
if (Gui::Screen::Project(offset + posL4 * double(m_zoom), pos) && pos.z < 1) {
AddProjected<RefType>(Projectable::L4, base, ref, pos);
if (Gui::Screen::Project(offset + posL4, pos) && pos.z < 1) {
AddProjected<RefType>(Projectable::L4, base, ref, pos, offset + posL4);
}

const vector3d posL5 = orbit->EvenSpacedPosTrajectory((1.0 / 360.0) * 300.0, tMinust0);
if (Gui::Screen::Project(offset + posL5 * double(m_zoom), pos) && pos.z < 1) {
AddProjected<RefType>(Projectable::L5, base, ref, pos);
if (Gui::Screen::Project(offset + posL5, pos) && pos.z < 1) {
AddProjected<RefType>(Projectable::L5, base, ref, pos, offset + posL5);
}
}
Gui::Screen::LeaveOrtho();
Expand All @@ -369,10 +369,11 @@ void SystemView::PutBody(const SystemBody *b, const vector3d &offset, const matr
m_bodyIcon.reset(new Graphics::Drawables::Disk(m_renderer, solidState, svColor[SYSTEMBODY], 1.0f));
}

const double radius = b->GetRadius() * m_zoom;
const double radius = b->GetRadius();

matrix4x4f invRot = trans;
invRot.ClearToRotOnly();
invRot.Renormalize();
invRot = invRot.Inverse();

matrix4x4f bodyTrans = trans;
Expand All @@ -396,7 +397,7 @@ void SystemView::PutBody(const SystemBody *b, const vector3d &offset, const matr
if (is_zero_general(kid->GetOrbit().GetSemiMajorAxis()))
continue;

const double axisZoom = kid->GetOrbit().GetSemiMajorAxis() * m_zoom;
const double axisZoom = kid->GetOrbit().GetSemiMajorAxis();
//semimajor axis radius should be at least 1% of screen width to show the orbit
if (ProjectedSize(axisZoom, offset) > 0.01) {
const SystemBody::BodySuperType bst = kid->GetSuperType();
Expand All @@ -405,7 +406,7 @@ void SystemView::PutBody(const SystemBody *b, const vector3d &offset, const matr
}

// not using current time yet
const vector3d pos = kid->GetOrbit().OrbitalPosAtTime(m_time) * double(m_zoom);
const vector3d pos = kid->GetOrbit().OrbitalPosAtTime(m_time);
PutBody(kid, offset + pos, trans);
}
}
Expand Down Expand Up @@ -478,7 +479,7 @@ void SystemView::CalculateFramePositionAtTime(FrameId frameId, double t, vector3
void SystemView::Draw3D()
{
PROFILE_SCOPED()
m_renderer->SetPerspectiveProjection(CAMERA_FOV, m_renderer->GetDisplayAspect(), 1.f, 1000.f * m_zoom * float(AU) + DEFAULT_VIEW_DISTANCE * 2);
m_renderer->SetPerspectiveProjection(CAMERA_FOV, m_renderer->GetDisplayAspect(), 1.f, 1000.f * float(AU) + DEFAULT_VIEW_DISTANCE * 2);
m_renderer->ClearScreen();
m_projected.clear();
//TODO add reserve
Expand Down Expand Up @@ -507,6 +508,7 @@ void SystemView::Draw3D()
m_cameraSpace.Translate(0, 0, -DEFAULT_VIEW_DISTANCE);
m_cameraSpace.Rotate(DEG2RAD(m_rot_x), 1, 0, 0);
m_cameraSpace.Rotate(DEG2RAD(m_rot_y), 0, 1, 0);
m_cameraSpace.Scale(m_zoom);
m_renderer->SetTransform(m_cameraSpace);

// smooth transition animation
Expand All @@ -522,7 +524,7 @@ void SystemView::Draw3D()
m_trans = m_transTo;
}

vector3d pos = m_trans * m_zoom;
vector3d pos = m_trans;

// glLineWidth(2);
if (!m_system->GetUnexplored() && m_system->GetRootBody()) {
Expand All @@ -545,11 +547,11 @@ void SystemView::Draw3D()
Frame *playerNonRotFrame = Frame::GetFrame(playerNonRotFrameId);
SystemBody *playerAround = playerNonRotFrame->GetSystemBody();
CalculateShipPositionAtTime(static_cast<Ship *>(Pi::player), playerOrbit, m_time, ppos);
AddNotProjected<Body>(Projectable::OBJECT, Projectable::PLAYER, PlayerBody, ppos * m_zoom + pos);
AddNotProjected<Body>(Projectable::OBJECT, Projectable::PLAYER, PlayerBody, ppos + pos);

vector3d offset(0.0);
CalculateFramePositionAtTime(playerNonRotFrameId, m_time, offset);
offset = offset * m_zoom + pos;
offset = offset + pos;

if (Pi::player->GetFlightState() == Ship::FlightState::FLYING) {
PutOrbit<Body>(Projectable::PLAYER, PlayerBody, &playerOrbit, offset, svColor[PLAYER_ORBIT], playerAround->GetRadius());
Expand All @@ -560,9 +562,9 @@ void SystemView::Draw3D()
playerAround->GetMass());
PutOrbit<Body>(Projectable::PLANNER, PlayerBody, &plannedOrbit, offset, svColor[PLANNER_ORBIT], playerAround->GetRadius());
if (std::fabs(m_time - m_game->GetTime()) > 1. && (m_time - plannerStartTime) > 0.)
AddNotProjected<Body>(Projectable::OBJECT, Projectable::PLANNER, PlayerBody, offset + plannedOrbit.OrbitalPosAtTime(m_time - plannerStartTime) * static_cast<double>(m_zoom));
AddNotProjected<Body>(Projectable::OBJECT, Projectable::PLANNER, PlayerBody, offset + plannedOrbit.OrbitalPosAtTime(m_time - plannerStartTime));
else
AddNotProjected<Body>(Projectable::OBJECT, Projectable::PLANNER, PlayerBody, offset + m_planner->GetPosition() * static_cast<double>(m_zoom));
AddNotProjected<Body>(Projectable::OBJECT, Projectable::PLANNER, PlayerBody, offset + m_planner->GetPosition());
}
}
}
Expand Down Expand Up @@ -598,8 +600,7 @@ void SystemView::Update()
Pi::input->GetMouseMotion(motion);
m_rot_x_to += motion[1] * 20 * ft;
m_rot_y_to += motion[0] * 20 * ft;
}
else if (m_zoomView) {
} else if (m_zoomView) {
Pi::input->SetCapturingMouse(true);
int motion[2];
Pi::input->GetMouseMotion(motion);
Expand Down Expand Up @@ -639,41 +640,29 @@ void SystemView::DrawShips(const double t, const vector3d &offset)
for (auto s = m_contacts.begin(); s != m_contacts.end(); s++) {
vector3d pos(0.0);
CalculateShipPositionAtTime((*s).first, (*s).second, t, pos);
pos = pos * m_zoom + offset;
pos = pos + offset;
//draw highlighted orbit for selected ship
const bool isSelected = m_selectedObject.type == Projectable::OBJECT && m_selectedObject.base != Projectable::SYSTEMBODY && m_selectedObject.ref.body == (*s).first;
AddNotProjected<Body>(Projectable::OBJECT, Projectable::SHIP, static_cast<Body *>((*s).first), pos);
if (m_shipDrawing == ORBITS && (*s).first->GetFlightState() == Ship::FlightState::FLYING) {
vector3d framepos(0.0);
CalculateFramePositionAtTime(Frame::GetFrame((*s).first->GetFrame())->GetNonRotFrame(), m_time, framepos);
PutOrbit<Body>(Projectable::SHIP, static_cast<Body *>((*s).first), &(*s).second, offset + framepos * m_zoom, isSelected ? svColor[SELECTED_SHIP_ORBIT] : svColor[SHIP_ORBIT], 0);
PutOrbit<Body>(Projectable::SHIP, static_cast<Body *>((*s).first), &(*s).second, offset + framepos, isSelected ? svColor[SELECTED_SHIP_ORBIT] : svColor[SHIP_ORBIT], 0);
}
}
}

void SystemView::PrepareGrid()
void SystemView::DrawGrid()
{

// calculate lines for this system:
double diameter = std::floor(m_system->GetRootBody()->GetMaxChildOrbitalDistance() * 1.2 / AU);

m_grid_lines = int(diameter) + 1;

m_displayed_sbody.clear();
if (m_gridDrawing == GridDrawing::GRID_AND_LEGS) {
m_displayed_sbody = m_system->GetRootBody()->CollectAllChildren();
}
}

void SystemView::DrawGrid()
{
PrepareGrid();
m_lineVerts.reset(new Graphics::VertexArray(Graphics::ATTRIB_POSITION, m_grid_lines * 4 + (m_gridDrawing == GridDrawing::GRID_AND_LEGS ? m_projected.size() * 2 : 0)));

m_lineVerts.reset(new Graphics::VertexArray(Graphics::ATTRIB_POSITION, m_grid_lines * 4 + m_displayed_sbody.size() * 2));

float zoom = m_zoom * float(AU);
vector3d pos(0.);
if (m_selectedObject.type != Projectable::NONE) GetTransformTo(m_selectedObject, pos);
pos *= m_zoom;
float zoom = float(AU);
vector3d pos = m_trans;

for (int i = -m_grid_lines; i < m_grid_lines + 1; i++) {
float z = float(i) * zoom;
Expand All @@ -687,39 +676,39 @@ void SystemView::DrawGrid()
m_lineVerts->Add(vector3f(x, 0.0f, +m_grid_lines * zoom) + vector3f(pos), svColor[GRID]);
}

for (SystemBody *sbody : m_displayed_sbody) {
vector3d offset(0.);
GetTransformTo(sbody, offset);
offset *= m_zoom;
m_lineVerts->Add(vector3f(pos - offset), svColor[GRID] * 0.5);
offset.y = 0.0;
m_lineVerts->Add(vector3f(pos - offset), svColor[GRID] * 0.5);
}
if (m_gridDrawing == GridDrawing::GRID_AND_LEGS)
for (Projectable &p : m_projected) {
vector3d offset(p.worldpos);
offset.y = m_trans.y;
m_lineVerts->Add(vector3f(p.worldpos), svColor[GRID_LEG] * 0.5);
m_lineVerts->Add(vector3f(offset), svColor[GRID_LEG] * 0.5);
}

m_lines.SetData(m_lineVerts->GetNumVerts(), &m_lineVerts->position[0], &m_lineVerts->diffuse[0]);
m_lines.Draw(Pi::renderer, m_lineState);
}

template <typename T>
void SystemView::AddNotProjected(Projectable::types type, Projectable::bases base, T *ref, const vector3d &worldscaledpos)
void SystemView::AddNotProjected(Projectable::types type, Projectable::bases base, T *ref, const vector3d &worldpos)
{
//project and add
Gui::Screen::EnterOrtho();
vector3d pos;
if (Gui::Screen::Project(worldscaledpos, pos) && pos.z < 1)
AddProjected<T>(type, base, ref, pos);
vector3d screenpos;
if (Gui::Screen::Project(worldpos, screenpos) && screenpos.z < 1)
AddProjected<T>(type, base, ref, screenpos, worldpos);
Gui::Screen::LeaveOrtho();
}

template <typename T>
void SystemView::AddProjected(Projectable::types type, Projectable::bases base, T *ref, vector3d &pos)
void SystemView::AddProjected(Projectable::types type, Projectable::bases base, T *ref, vector3d &pos, const vector3d &worldpos)
{
float scale[2];
Gui::Screen::GetCoords2Pixels(scale);
Projectable p(type, base, ref);
p.screenpos.x = pos.x / scale[0];
p.screenpos.y = pos.y / scale[1];
p.screenpos.z = pos.z;
p.worldpos = worldpos;
m_projected.push_back(p);
}

Expand Down Expand Up @@ -765,7 +754,8 @@ void SystemView::SetZoomMode(bool enable)
}
}

void SystemView::SetRotateMode(bool enable) {
void SystemView::SetRotateMode(bool enable)
{
if (enable != m_rotateView) {
Pi::input->SetCapturingMouse(enable);
m_rotateView = enable;
Expand Down
21 changes: 11 additions & 10 deletions src/SystemView.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct Projectable
const SystemBody* sbody;
} ref;
vector3d screenpos; // x,y - screen coordinate, z - in NDC
vector3d worldpos;

Projectable(const types t, const bases b, const Body* obj) : type(t), base(b)
{
Expand Down Expand Up @@ -135,15 +136,16 @@ class SystemView : public UIView, public DeleteEmitter {
// all used colors. defined in system-view-ui.lua
enum ColorIndex { // <enum name=SystemViewColorIndex scope='SystemView' public>
GRID = 0,
SYSTEMBODY = 1,
SYSTEMBODY_ORBIT = 2,
PLAYER_ORBIT = 3,
PLANNER_ORBIT = 4,
SELECTED_SHIP_ORBIT = 5,
SHIP_ORBIT = 6
GRID_LEG = 1,
SYSTEMBODY = 2,
SYSTEMBODY_ORBIT = 3,
PLAYER_ORBIT = 4,
PLANNER_ORBIT = 5,
SELECTED_SHIP_ORBIT = 6,
SHIP_ORBIT = 7
};

Color svColor[7];
Color svColor[8];
void SetColor(ColorIndex color_index, Color* color_value) { svColor[color_index] = *color_value; }

private:
Expand All @@ -165,12 +167,11 @@ class SystemView : public UIView, public DeleteEmitter {
void MouseWheel(bool up);
void RefreshShips(void);
void DrawShips(const double t, const vector3d &offset);
void PrepareGrid();
void DrawGrid();
template <typename T>
void AddProjected(Projectable::types type, Projectable::bases base, T *ref, vector3d &pos);
void AddProjected(Projectable::types type, Projectable::bases base, T *ref, vector3d &pos, const vector3d &worldpos);
template <typename T>
void AddNotProjected(Projectable::types type, Projectable::bases base, T *ref, const vector3d &worldscaledpos);
void AddNotProjected(Projectable::types type, Projectable::bases base, T *ref, const vector3d &worldpos);
void CalculateShipPositionAtTime(const Ship *s, Orbit o, double t, vector3d &pos);
void CalculateFramePositionAtTime(FrameId frameId, double t, vector3d &pos);
double GetOrbitTime(double t, const SystemBody* b);
Expand Down
1 change: 1 addition & 0 deletions src/enum_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const struct EnumItem ENUM_ProjectableBases[] = {

const struct EnumItem ENUM_SystemViewColorIndex[] = {
{ "GRID", int(SystemView::GRID) },
{ "GRID_LEG", int(SystemView::GRID_LEG) },
{ "SYSTEMBODY", int(SystemView::SYSTEMBODY) },
{ "SYSTEMBODY_ORBIT", int(SystemView::SYSTEMBODY_ORBIT) },
{ "PLAYER_ORBIT", int(SystemView::PLAYER_ORBIT) },
Expand Down

0 comments on commit 9d8be40

Please sign in to comment.