Ticket #1532: DropDownEasyAccess.patch

File DropDownEasyAccess.patch, 3.1 KB (added by wraitii, 11 years ago)
  • source/gui/CDropDown.cpp

     
    2525
    2626#include "lib/ogl.h"
    2727#include "lib/external_libraries/libsdl.h"
     28#include "lib/timer.h"
    2829
    2930
    3031//-------------------------------------------------------------------
     
    154155            GUI<int>::GetSetting(this, "selected", m_ElementHighlight);
    155156
    156157            // Start at the position of the selected item, if possible.
    157             GetScrollBar(0).SetPos( m_ItemsYPositions.empty() ? 0 : m_ItemsYPositions[m_ElementHighlight] );
     158            GetScrollBar(0).SetPos( m_ItemsYPositions.empty() ? 0 : m_ItemsYPositions[m_ElementHighlight] - 60 );
    158159            return; // overshadow
    159160        }
    160161        else
     
    227228        break;
    228229
    229230    default:
     231        // If we have imputed a character try to get the closest element to it.
     232        // TODO: not too nice and doesn't deal with dashes.
     233        if (m_Open && ((szChar >= SDLK_a && szChar <= SDLK_z) || szChar == SDLK_SPACE
     234                       || (szChar >= SDLK_0 && szChar <= SDLK_9) || (szChar >= SDLK_KP0 && szChar <= SDLK_KP9)))
     235        {
     236            // arbitrary 1 second limit to add to string or start fresh.
     237            // maximal amount of characters is 100, which imo is far more than enough.
     238            if (timer_Time() - m_TimeOfLastInput > 1.0 || m_InputBuffer.length() >= 100)
     239                m_InputBuffer = szChar;
     240            else
     241                m_InputBuffer += szChar;
     242           
     243            m_TimeOfLastInput = timer_Time();
     244           
     245            CGUIList *pList;
     246            GUI<CGUIList>::GetSettingPointer(this, "list", pList);
     247            // let's look for the closest element
     248            // basically it's alphabetic order and "as many letters as we can get".
     249            int closest = -1;
     250            int bestIndex = -1;
     251            int difference = 1250;
     252            for (int i=0; i<(int)pList->m_Items.size(); ++i)
     253            {
     254                int indexOfDifference = 0;
     255                int diff = 0;
     256                for (size_t j=0; j < m_InputBuffer.length(); ++j)
     257                {
     258                    diff = abs(pList->m_Items[i].GetOriginalString().LowerCase()[j] - (int)m_InputBuffer[j]);
     259                    if (diff == 0)
     260                        indexOfDifference = j+1;
     261                    else
     262                        break;
     263                }
     264                if (indexOfDifference > bestIndex || (indexOfDifference >= bestIndex && diff < difference))
     265                {
     266                    bestIndex = indexOfDifference;
     267                    closest = i;
     268                    difference = diff;
     269                }
     270            }
     271            // let's select the closest element. There should basically always be one.
     272            if (closest != -1)
     273            {
     274                GUI<int>::SetSetting(this, "selected", closest);
     275                update_highlight = true;
     276                GetScrollBar(0).SetPos(m_ItemsYPositions[closest] - 60);
     277            }
     278        }
    230279        break;
    231280    }
    232281
  • source/gui/CDropDown.h

     
    127127    //  it is set to "selected", but then when moving the mouse it will
    128128    //  change.
    129129    int m_ElementHighlight;
     130   
     131    // Stores any text entered by the user for quick access to an element
     132    // (ie if you type "acro" it will take you to acropolis).
     133    std::string m_InputBuffer;
     134   
     135    // used to know if we want to restart anew or add to m_inputbuffer.
     136    double m_TimeOfLastInput;
     137
    130138};
    131139
    132140#endif