Ticket #1365: hero-overlay-alpha-desaturate-3.patch

File hero-overlay-alpha-desaturate-3.patch, 13.3 KB (added by Deiz, 12 years ago)
  • source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp

     
    148148            }
    149149        }
    150150
    151         cmpSelectable->SetSelectionHighlight(colour);
     151        cmpSelectable->SetSelectionHighlight(colour, true);
    152152    }
    153153}
    154154
  • source/simulation2/components/ICmpSelectable.h

     
    5656     * Set the color of the selection highlight (typically a circle/square
    5757     * around the unit). Set a = 0 to disable the highlight.
    5858     */
    59     virtual void SetSelectionHighlight(CColor color) = 0;
     59    virtual void SetSelectionHighlight(CColor color, bool selected) = 0;
    6060
    6161    /**
    6262     * Set the alpha of the selection highlight. Set to 0 to disable the highlight.
  • source/simulation2/components/ICmpSelectable.cpp

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

     
    5959    CCmpSelectable()
    6060        : m_DebugBoundingBoxOverlay(NULL), m_DebugSelectionBoxOverlay(NULL),
    6161          m_BuildingOverlay(NULL), m_UnitOverlay(NULL),
    62           m_FadeBaselineAlpha(0.f), m_FadeDeltaAlpha(0.f), m_FadeProgress(0.f)
     62          m_FadeBaselineAlpha(0.f), m_FadeDeltaAlpha(0.f), m_FadeProgress(0.f),
     63          m_Selected(false), m_Cached(false)
    6364    {
    6465        m_Color = CColor(0, 0, 0, m_FadeBaselineAlpha);
    6566    }
     
    8384                "</element>"
    8485            "</optional>"
    8586            "<element name='Overlay' a:help='Specifies the type of overlay to be displayed when this entity is selected'>"
     87                "<optional>"
     88                    "<element name='AlwaysVisible' a:help='If this element is present, the minimum alpha of the overlay will be 50%'>"
     89                        "<empty/>"
     90                    "</element>"
     91                "</optional>"
    8692                "<choice>"
    8793                    "<element name='Texture' a:help='Displays a texture underneath the entity.'>"
    8894                        "<element name='MainTexture' a:help='Texture to display underneath the entity. Filepath relative to art/textures/selection/.'><text/></element>"
     
    101107    {
    102108        m_EditorOnly = paramNode.GetChild("EditorOnly").IsOk();
    103109
     110        // Certain special units always have their selection overlay shown.
     111        m_AlphaMin = paramNode.GetChild("Overlay").GetChild("AlwaysVisible").IsOk() ? .5 : 0.;
     112        if (m_AlphaMin > 0.)
     113            m_Color.a = m_AlphaMin;
     114
    104115        const CParamNode& textureNode = paramNode.GetChild("Overlay").GetChild("Texture");
    105116        const CParamNode& outlineNode = paramNode.GetChild("Overlay").GetChild("Outline");
    106117
     
    141152
    142153    virtual void HandleMessage(const CMessage& msg, bool UNUSED(global));
    143154
    144     virtual void SetSelectionHighlight(CColor color)
     155    virtual void SetSelectionHighlight(CColor color, bool selected)
    145156    {
     157        m_Selected = selected;
    146158        m_Color.r = color.r;
    147159        m_Color.g = color.g;
    148160        m_Color.b = color.b;
     161
     162        // Always-visible overlays will be desaturated if their parent unit is deselected.
     163        if (m_AlphaMin > 0. && !selected)
     164        {
     165            float max;
     166
     167            // Reduce saturation by one-third, the quick-and-dirty way.
     168            if (m_Color.r > m_Color.b)
     169               max = (m_Color.r > m_Color.g) ? m_Color.r : m_Color.g;
     170            else
     171               max = (m_Color.b > m_Color.g) ? m_Color.b : m_Color.g;
     172
     173            m_Color.r += (max - m_Color.r) / 3.;
     174            m_Color.g += (max - m_Color.g) / 3.;
     175            m_Color.b += (max - m_Color.b) / 3.;
     176        }
     177
    149178        SetSelectionHighlightAlpha(color.a);
    150179    }
    151180
    152181    virtual void SetSelectionHighlightAlpha(float alpha)
    153182    {
     183        alpha = std::max(m_AlphaMin, alpha);
     184
    154185        // set up fading from the current value (as the baseline) to the target value
    155186        m_FadeBaselineAlpha = m_Color.a;
    156187        m_FadeDeltaAlpha = alpha - m_FadeBaselineAlpha;
     
    191222    /// Is this entity selectable only in the editor?
    192223    bool m_EditorOnly;
    193224   
     225
     226    /// Whether the parent entity is selected (caches GUI's selection state).
     227    bool m_Selected;
    194228    /// Current selection overlay color. Alpha component is subject to fading.
    195229    CColor m_Color;
     230    /// Whether the selectable's player colour has been cached for rendering.
     231    bool m_Cached;
     232    /// Minimum value for current selection overlay alpha.
     233    float m_AlphaMin;
    196234    /// Baseline alpha value to start fading from. Constant during a single fade.
    197235    float m_FadeBaselineAlpha;
    198236    /// Delta between target and baseline alpha. Constant during a single fade. Can be positive or negative.
     
    260298            // Update the highlight color, while keeping the current alpha target value intact
    261299            // (i.e. baseline + delta), so that any ongoing fades simply continue with the new color.
    262300            CColor color = cmpPlayer->GetColour();
    263             SetSelectionHighlight(CColor(color.r, color.g, color.b, m_FadeBaselineAlpha + m_FadeDeltaAlpha));
     301            SetSelectionHighlight(CColor(color.r, color.g, color.b, m_FadeBaselineAlpha + m_FadeDeltaAlpha), m_Selected);
    264302        }
    265303        // fall-through
    266304    case MT_PositionChanged:
     
    473511    // don't render selection overlay if it's not gonna be visible
    474512    if (m_Color.a > 0)
    475513    {
     514        if (!m_Cached)
     515        {
     516            // Try to initialize m_Color to the owning player's colour.
     517            CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSimContext(), SYSTEM_ENTITY);
     518            if (!cmpPlayerManager)
     519                return;
     520   
     521            CmpPtr<ICmpOwnership> cmpOwnership(GetSimContext(), GetEntityId());
     522            if (!cmpOwnership)
     523                return;
     524   
     525            player_id_t owner = cmpOwnership->GetOwner();
     526            if (owner == INVALID_PLAYER)
     527                return;
     528   
     529            CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(owner));
     530            if (!cmpPlayer)
     531                return;
     532
     533            CColor color = cmpPlayer->GetColour();
     534            color.a = m_FadeBaselineAlpha + m_FadeDeltaAlpha;
     535
     536            SetSelectionHighlight(color, m_Selected);
     537            m_Cached = true;
     538        }
     539
    476540        switch (m_OverlayDescriptor.m_Type)
    477541        {
    478542            case STATIC_OUTLINE:
  • binaries/data/mods/public/gui/session/selection.js

     
    11const MAX_SELECTION_SIZE = 200; // Limits selection size
    22
    3 function _setHighlight(ents, alpha)
     3function _setHighlight(ents, alpha, selected)
    44{
    55    if (ents.length)
    6         Engine.GuiInterfaceCall("SetSelectionHighlight", { "entities":ents, "alpha":alpha });
     6        Engine.GuiInterfaceCall("SetSelectionHighlight", { "entities":ents, "alpha":alpha, "selected":selected });
    77}
    88
    99function _setStatusBars(ents, enabled)
     
    214214        if (entState.visibility == "hidden")
    215215        {
    216216            // Disable any highlighting of the disappeared unit
    217             _setHighlight([ent], 0);
     217            _setHighlight([ent], 0, false);
    218218            _setStatusBars([ent], false);
    219219            _setMotionOverlay([ent], false);
    220220
     
    280280        }
    281281    }
    282282
    283     _setHighlight(added, 1);
     283    _setHighlight(added, 1, true);
    284284    _setStatusBars(added, true);
    285285    _setMotionOverlay(added, this.motionDebugOverlay);
    286286    if (added.length)
     
    304304        }
    305305    }
    306306
    307     _setHighlight(removed, 0);
     307    _setHighlight(removed, 0, false);
    308308    _setStatusBars(removed, false);
    309309    _setMotionOverlay(removed, false);
    310310
     
    313313
    314314EntitySelection.prototype.reset = function()
    315315{
    316     _setHighlight(this.toList(), 0);
     316    _setHighlight(this.toList(), 0, false);
    317317    _setStatusBars(this.toList(), false);
    318318    _setMotionOverlay(this.toList(), false);
    319319    this.selected = {};
     
    349349        if (!this.highlighted[ent] && !this.selected[ent])
    350350            added.push(+ent);
    351351
    352     _setHighlight(removed, 0);
     352    _setHighlight(removed, 0, false);
    353353    _setStatusBars(removed, false);
    354354
    355     _setHighlight(added, 0.5);
     355    _setHighlight(added, 0.5, true);
    356356    _setStatusBars(added, true);
    357357   
    358358    // Store the new highlight list
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    591591    return false;
    592592};
    593593
    594 GuiInterface.prototype.SetSelectionHighlight = function(player, cmd)
     594GuiInterface.prototype.SetSelectionHighlight = function(player, cmd, selected)
    595595{
    596596    var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    597597    var playerColours = {}; // cache of owner -> colour map
     
    618618            playerColours[owner] = colour;
    619619        }
    620620
    621         cmpSelectable.SetSelectionHighlight({"r":colour.r, "g":colour.g, "b":colour.b, "a":cmd.alpha});
     621        cmpSelectable.SetSelectionHighlight({"r":colour.r, "g":colour.g, "b":colour.b, "a":cmd.alpha}, cmd.selected);
    622622    }
    623623};
    624624
  • binaries/data/mods/public/simulation/templates/template_unit_hero_cavalry_archer.xml

     
    6262  </Minimap>
    6363  <Selectable>
    6464    <Overlay>
     65      <AlwaysVisible/>
    6566      <Texture>
    6667        <MainTexture>star/256x256.png</MainTexture>
    6768        <MainTextureMask>star/256x256_mask.png</MainTextureMask>
  • binaries/data/mods/public/simulation/templates/template_unit_hero_infantry.xml

     
    5151  </Minimap>
    5252  <Selectable>
    5353    <Overlay>
     54      <AlwaysVisible/>
    5455      <Texture>
    5556        <MainTexture>star/256x256.png</MainTexture>
    5657        <MainTextureMask>star/256x256_mask.png</MainTextureMask>
  • binaries/data/mods/public/simulation/templates/template_unit_hero.xml

     
    4949  </Minimap>
    5050  <Selectable>
    5151    <Overlay>
     52      <AlwaysVisible/>
    5253      <Texture>
    5354        <MainTexture>star/256x256.png</MainTexture>
    5455        <MainTextureMask>star/256x256_mask.png</MainTextureMask>
  • binaries/data/mods/public/simulation/templates/template_unit_hero_infantry_archer.xml

     
    5454  </Minimap>
    5555  <Selectable>
    5656    <Overlay>
     57      <AlwaysVisible/>
    5758      <Texture>
    5859        <MainTexture>star/256x256.png</MainTexture>
    5960        <MainTextureMask>star/256x256_mask.png</MainTextureMask>
  • binaries/data/mods/public/simulation/templates/template_unit_hero_infantry_javelinist.xml

     
    5858  </Minimap>
    5959  <Selectable>
    6060    <Overlay>
     61      <AlwaysVisible/>
    6162      <Texture>
    6263        <MainTexture>star/256x256.png</MainTexture>
    6364        <MainTextureMask>star/256x256_mask.png</MainTextureMask>
  • binaries/data/mods/public/simulation/templates/template_unit_hero_cavalry_javelinist.xml

     
    5252  </Minimap>
    5353  <Selectable>
    5454    <Overlay>
     55      <AlwaysVisible/>
    5556      <Texture>
    5657        <MainTexture>star/256x256.png</MainTexture>
    5758        <MainTextureMask>star/256x256_mask.png</MainTextureMask>
  • binaries/data/mods/public/simulation/templates/template_unit_hero_cavalry.xml

     
    5353  </Minimap>
    5454  <Selectable>
    5555    <Overlay>
     56      <AlwaysVisible/>
    5657      <Texture>
    5758        <MainTexture>star/256x256.png</MainTexture>
    5859        <MainTextureMask>star/256x256_mask.png</MainTextureMask>