AnalogTapeModel

Physical modelling signal processing for analog tape recording
Log | Files | Refs | Submodules | README | LICENSE

TooltipComp.cpp (2494B)


      1 #include "TooltipComp.h"
      2 
      3 TooltipComponent::TooltipComponent()
      4 {
      5     setColour (backgroundColourID, Colours::transparentBlack);
      6     setColour (textColourID, Colours::lightgrey);
      7     setColour (nameColourID, Colours::white);
      8 
      9     showTip.store (false);
     10     startTimer (123);
     11 }
     12 
     13 void TooltipComponent::paint (Graphics& g)
     14 {
     15     g.fillAll (findColour (backgroundColourID));
     16 
     17     if (tip.isEmpty())
     18         return;
     19 
     20     if (showTip.load())
     21     {
     22         auto b = getLocalBounds();
     23 
     24         g.setFont (Font (17.0f).boldened());
     25         if (name.isNotEmpty())
     26         {
     27             g.setColour (findColour (nameColourID));
     28             g.drawFittedText (name + ":", b, Justification::topLeft, 1);
     29         }
     30 
     31         auto whitespace = String();
     32         auto font = g.getCurrentFont();
     33         while (font.getStringWidth (whitespace) < font.getStringWidth (name + ": "))
     34             whitespace += " ";
     35 
     36         g.setColour (findColour (textColourID));
     37         g.drawMultiLineText (whitespace + tip, b.getX(), b.getY() + (int) font.getHeight() - 3, b.getWidth(), Justification::topLeft);
     38     }
     39 }
     40 
     41 void TooltipComponent::getTipFor (Component& c, String& newTip, String& newName)
     42 {
     43     if (Process::isForegroundProcess()
     44         && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
     45     {
     46         if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
     47         {
     48             if (! c.isCurrentlyBlockedByAnotherModalComponent())
     49             {
     50                 newTip = ttc->getTooltip();
     51                 newName = c.getName();
     52             }
     53         }
     54     }
     55 }
     56 
     57 void TooltipComponent::timerCallback()
     58 {
     59     auto& desktop = Desktop::getInstance();
     60     auto mouseSource = desktop.getMainMouseSource();
     61 
     62     auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
     63 
     64     bool needsRepaint = false;
     65     if (newComp != nullptr)
     66     {
     67         String newTip, newName;
     68         getTipFor (*newComp, newTip, newName);
     69         needsRepaint = newTip != tip;
     70 
     71         if (newTip.isNotEmpty() && newName.isEmpty())
     72         {
     73             if (auto parent = newComp->getParentComponent())
     74                 newName = parent->getName();
     75         }
     76 
     77         tip = newTip;
     78         name = newName;
     79 
     80         if (! showTip.load())
     81         {
     82             showTip.store (true);
     83             needsRepaint = true;
     84         }
     85     }
     86     else
     87     {
     88         if (showTip.load())
     89         {
     90             showTip.store (false);
     91             needsRepaint = true;
     92         }
     93     }
     94 
     95     if (needsRepaint)
     96         repaint();
     97 }