Ticket #3915: visualrange_v0.2.patch

File visualrange_v0.2.patch, 7.2 KB (added by Sandarac, 8 years ago)

Properly handles multiple selected buildings.

  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    867867                color = cmpPlayer.GetColor();
    868868            playerColors[owner] = color;
    869869        }
     870        let cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
     871        let showRange = cmpIdentity.HasClass("Defensive") && owner == player;
    870872
    871         cmpSelectable.SetSelectionHighlight({ "r": color.r, "g": color.g, "b": color.b, "a": cmd.alpha }, cmd.selected);
     873        cmpSelectable.SetSelectionHighlight({ "r": color.r, "g": color.g, "b": color.b, "a": cmd.alpha }, cmd.selected, showRange);
    872874    }
    873875};
    874876
  • source/simulation2/components/CCmpSelectable.cpp

     
    3838#include "simulation2/components/ICmpOwnership.h"
    3939#include "simulation2/components/ICmpPlayer.h"
    4040#include "simulation2/components/ICmpPlayerManager.h"
     41#include "simulation2/components/ICmpVision.h"
    4142#include "simulation2/components/ICmpWaterManager.h"
    4243#include "simulation2/helpers/Render.h"
    4344#include "simulation2/system/Component.h"
     
    6465
    6566    CCmpSelectable()
    6667        : m_DebugBoundingBoxOverlay(NULL), m_DebugSelectionBoxOverlay(NULL),
    67           m_BuildingOverlay(NULL), m_UnitOverlay(NULL),
     68          m_BuildingOverlay(NULL), m_RangeOverlay(NULL), m_UnitOverlay(NULL),
    6869          m_FadeBaselineAlpha(0.f), m_FadeDeltaAlpha(0.f), m_FadeProgress(0.f),
    69           m_Selected(false), m_Cached(false), m_Visible(false)
     70          m_Selected(false), m_Cached(false), m_Visible(false), m_ShowRange(false)
    7071    {
    7172        m_Color = CColor(0, 0, 0, m_FadeBaselineAlpha);
    7273    }
     
    7677        delete m_DebugBoundingBoxOverlay;
    7778        delete m_DebugSelectionBoxOverlay;
    7879        delete m_BuildingOverlay;
     80        delete m_RangeOverlay;
    7981        delete m_UnitOverlay;
    8082    }
    8183
     
    167169
    168170    virtual void HandleMessage(const CMessage& msg, bool UNUSED(global));
    169171
    170     virtual void SetSelectionHighlight(const CColor& color, bool selected)
     172    virtual void SetSelectionHighlight(const CColor& color, bool selected, bool showRange = false)
    171173    {
    172174        m_Selected = selected;
    173175        m_Color.r = color.r;
    174176        m_Color.g = color.g;
    175177        m_Color.b = color.b;
     178        m_ShowRange = showRange;
    176179
    177180        // Always-visible overlays will be desaturated if their parent unit is deselected.
    178181        if (m_AlwaysVisible && !selected)
     
    247250private:
    248251    SOverlayDescriptor m_OverlayDescriptor;
    249252    SOverlayTexturedLine* m_BuildingOverlay;
     253    SOverlayTexturedLine* m_RangeOverlay;
    250254    SOverlayQuad* m_UnitOverlay;
    251255
    252256    SOverlayLine* m_DebugBoundingBoxOverlay;
     
    275279    float m_FadeDeltaAlpha;
    276280    /// Linear time progress of the fade, between 0 and m_FadeDuration.
    277281    float m_FadeProgress;
     282    /// Whether visual range will be shown.
     283    bool m_ShowRange;
    278284
    279285    /// Total duration of a single fade, in seconds. Assumed constant for now; feel free to change this into
    280286    /// a member variable if you need to adjust it per component.
     
    340346        // Update the highlight color, while keeping the current alpha target value intact
    341347        // (i.e. baseline + delta), so that any ongoing fades simply continue with the new color.
    342348        CColor color = cmpPlayer->GetColor();
    343         SetSelectionHighlight(CColor(color.r, color.g, color.b, m_FadeBaselineAlpha + m_FadeDeltaAlpha), m_Selected);
     349        SetSelectionHighlight(CColor(color.r, color.g, color.b, m_FadeBaselineAlpha + m_FadeDeltaAlpha), m_Selected, m_ShowRange);
    344350
    345351        InvalidateStaticOverlay();
    346352        break;
     
    400406void CCmpSelectable::InvalidateStaticOverlay()
    401407{
    402408    SAFE_DELETE(m_BuildingOverlay);
     409    SAFE_DELETE(m_RangeOverlay);
     410
    403411}
    404412
    405413void CCmpSelectable::UpdateStaticOverlay()
     
    449457    m_BuildingOverlay->m_TextureBase = g_Renderer.GetTextureManager().CreateTexture(texturePropsBase);
    450458    m_BuildingOverlay->m_TextureMask = g_Renderer.GetTextureManager().CreateTexture(texturePropsMask);
    451459
     460    m_RangeOverlay = new SOverlayTexturedLine;
     461    m_RangeOverlay->m_AlwaysVisible = false;
     462    m_RangeOverlay->m_Closed = true;
     463    m_RangeOverlay->m_SimContext = &GetSimContext();
     464    m_RangeOverlay->m_Thickness = m_OverlayDescriptor.m_LineThickness;
     465    m_RangeOverlay->m_TextureBase = g_Renderer.GetTextureManager().CreateTexture(texturePropsBase);
     466    m_RangeOverlay->m_TextureMask = g_Renderer.GetTextureManager().CreateTexture(texturePropsMask);
     467
    452468    CVector2D origin(position.X.ToFloat(), position.Y.ToFloat());
    453469
     470    float rangeRadius;
     471
     472    if (m_ShowRange)
     473    {
     474        CmpPtr<ICmpVision> cmpVision(GetSimContext(), GetEntityId());
     475        if (!cmpVision)
     476            return;
     477        rangeRadius = cmpVision->GetRange().ToFloat();
     478
     479        float stepAngle;
     480        unsigned numSteps;
     481        SimRender::AngularStepFromChordLen(TERRAIN_TILE_SIZE / 3.f, rangeRadius, stepAngle, numSteps);
     482
     483        for (unsigned i = 0; i < numSteps; i++)
     484        {
     485            float angle = i * stepAngle;
     486            float px = origin.X + rangeRadius * sinf(angle);
     487            float pz = origin.Y + rangeRadius * cosf(angle);
     488
     489            m_RangeOverlay->PushCoords(px, pz);
     490        }
     491    }
     492
    454493    switch (fpShape)
    455494    {
    456495    case ICmpFootprint::SQUARE:
     
    617656            }
    618657            color.a = m_FadeBaselineAlpha + m_FadeDeltaAlpha;
    619658
    620             SetSelectionHighlight(color, m_Selected);
     659            SetSelectionHighlight(color, m_Selected, m_ShowRange);
    621660            m_Cached = true;
    622661        }
    623662
     
    628667                    UpdateStaticOverlay();
    629668                    m_BuildingOverlay->m_Color = m_Color; // done separately so alpha changes don't require a full update call
    630669                    collector.Submit(m_BuildingOverlay);
     670                    m_RangeOverlay->m_Color = m_Color;
     671                    collector.Submit(m_RangeOverlay);
    631672                }
    632673                break;
    633674            case DYNAMIC_QUAD:
  • source/simulation2/components/ICmpSelectable.cpp

     
    2424#include "ps/Shapes.h"
    2525
    2626BEGIN_INTERFACE_WRAPPER(Selectable)
    27 DEFINE_INTERFACE_METHOD_2("SetSelectionHighlight", void, ICmpSelectable, SetSelectionHighlight, CColor, bool)
     27DEFINE_INTERFACE_METHOD_3("SetSelectionHighlight", void, ICmpSelectable, SetSelectionHighlight, CColor, bool, bool)
    2828END_INTERFACE_WRAPPER(Selectable)
    2929
    3030bool ICmpSelectable::ms_EnableDebugOverlays = false;
  • source/simulation2/components/ICmpSelectable.h

     
    5757     * The highlight is typically a circle/square overlay around the unit.
    5858     * @param color color and alpha of the selection highlight. Set color.a = 0 to hide the highlight.
    5959     * @param selected whether the entity is selected; affects desaturation for always visible highlights.
     60     * @param showRange whether visual range will be shown.
    6061     */
    61     virtual void SetSelectionHighlight(const CColor& color, bool selected) = 0;
     62    virtual void SetSelectionHighlight(const CColor& color, bool selected, bool showRange = false) = 0;
    6263
    6364    /**
    6465     * Enables or disables rendering of an entity's selectable.