NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

assetsSearch.py (6412B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\shared\assetsSearch.py
      2 import carbonui.const as uiconst
      3 import localization
      4 from carbon.common.script.util import timerstuff
      5 from carbonui.control.singlelineedits.singleLineEditHistory import HistoryData
      6 from carbonui.control.singlelineedits.singleLineEditText import SingleLineEditText
      7 from carbonui.primitives.sprite import Sprite
      8 from eveAssets.assetSearchUtil import IsPartOfText, IsTextMatch, ParseString
      9 
     10 class SearchBox(SingleLineEditText):
     11     __guid__ = 'assets.SearchBox'
     12     default_dynamicHistoryWidth = True
     13 
     14     def ApplyAttributes(self, attributes):
     15         super(SearchBox, self).ApplyAttributes(attributes)
     16         self.blockSetValue = True
     17         self.textRightMargin = 1
     18         self.searchKeywords = attributes.get('keywords', [])
     19         self.OnClearFilter = attributes.OnClearFilter
     20         self.CreateLayout()
     21 
     22     def SetValue(self, text, *args, **kwargs):
     23         oldText = self.GetValue()
     24         super(SearchBox, self).SetValue(text, *args, **kwargs)
     25         self.caretIndex = self.GetCursorFromIndex(self.GetSmartCaretIndex(oldText, text))
     26         self.RefreshCaretPosition()
     27 
     28     def GetSmartCaretIndex(self, oldText, newText):
     29         oldText = oldText[::-1]
     30         newText = newText[::-1]
     31         for i in xrange(len(oldText)):
     32             if oldText[i] != newText[i]:
     33                 return len(newText) - i
     34 
     35         return len(newText)
     36 
     37     def CreateLayout(self):
     38         self.optionIcon = Sprite(parent=self.sr.maincontainer, name='options', texturePath='res:/UI/Texture/Icons/38_16_229.png', pos=(0, 0, 16, 16), align=uiconst.TORIGHT, idx=0, hint=localization.GetByLabel('UI/Inventory/AssetSearch/KeywordOptionsHint'))
     39         self.optionIcon.SetAlpha(0.8)
     40         self.optionIcon.OnClick = self.OnOptionClick
     41 
     42     def OnOptionClick(self):
     43         self.ShowHistoryMenu(self.GetStaticHints())
     44 
     45     def GetStaticHints(self):
     46         currentText = self.GetValue(registerHistory=0)
     47         currentText = currentText.rstrip()
     48         if currentText:
     49             currentText += ' '
     50         hints = []
     51         for kw in self.searchKeywords:
     52             hints.append((localization.GetByLabel('UI/Inventory/AssetSearch/KeywordHint', keyword=kw.keyword, description=kw.optionDescription), '%s%s: ' % (currentText, kw.keyword)))
     53 
     54         return hints
     55 
     56     def GetDynamicHints(self):
     57         hints = []
     58         caretIndex = self.caretIndex[0]
     59         currentText = self.GetValue(registerHistory=0)
     60         headText, tailText = currentText[:caretIndex], currentText[caretIndex:]
     61         tailText = tailText.lstrip()
     62         trimmedText = headText.rstrip()
     63         if trimmedText.endswith(':'):
     64             strippedText, lastWord = self.SplitText(trimmedText, removeSeprator=True)
     65             if lastWord:
     66                 for kw in self.IterMatchingKeywords(lastWord):
     67                     if kw.specialOptions:
     68                         for option in kw.specialOptions:
     69                             historyData = HistoryData(localization.GetByLabel('UI/Inventory/AssetSearch/OptionHint', keyword=kw.keyword, option=option), '%s%s: %s %s' % (strippedText,
     70                              kw.keyword,
     71                              option,
     72                              tailText), False)
     73                             hints.append(historyData)
     74 
     75         else:
     76             strippedText, lastWord = self.SplitText(trimmedText, removeSeprator=False)
     77             freeText, matches = ParseString(trimmedText)
     78             if lastWord:
     79                 if matches and IsTextMatch(lastWord, matches[-1][1]):
     80                     keyword, value = matches[-1]
     81                     for kw in self.IterMatchingKeywords(keyword):
     82                         if kw.specialOptions:
     83                             for option in kw.specialOptions:
     84                                 if IsPartOfText(option, value):
     85                                     historyData = HistoryData(localization.GetByLabel('UI/Inventory/AssetSearch/OptionHint', keyword=kw.keyword, option=option), '%s%s %s' % (strippedText, option, tailText), False)
     86                                     hints.append(historyData)
     87 
     88                             break
     89 
     90                 else:
     91                     for kw in self.IterMatchingKeywords(lastWord):
     92                         historyData = HistoryData(localization.GetByLabel('UI/Inventory/AssetSearch/KeywordHint', keyword=kw.keyword, description=kw.optionDescription), '%s%s: %s' % (strippedText, kw.keyword, tailText), False)
     93                         hints.append(historyData)
     94 
     95         return hints
     96 
     97     def IterMatchingKeywords(self, keyword):
     98         for kw in self.searchKeywords:
     99             if IsPartOfText(kw.keyword, keyword):
    100                 yield kw
    101 
    102     def SplitText(self, baseText, removeSeprator = False):
    103         strippedText, lastWord = (None, None)
    104         parts = baseText.split()
    105         if parts:
    106             lastWord = parts[-1]
    107             strippedText = baseText[:-len(lastWord)]
    108             if removeSeprator:
    109                 lastWord = lastWord[:-1]
    110             if strippedText:
    111                 strippedText = strippedText.rstrip() + ' '
    112         return (strippedText, '' if lastWord is None else lastWord.lower())
    113 
    114     def TryRefreshHistory(self, currentString):
    115         self.refreshHistoryTimer = timerstuff.AutoTimer(200, self.TryRefreshHistory_Thread, currentString)
    116 
    117     def TryRefreshHistory_Thread(self, currentString):
    118         if currentString.rstrip().endswith(':'):
    119             self.CheckHistory()
    120         self.refreshHistoryTimer = None
    121 
    122     def OnHistoryClick(self, clickedString, *args):
    123         self.TryRefreshHistory(clickedString)
    124 
    125     def OnComboChange(self, combo, label, value, *args):
    126         self.SetValue(label, updateIndex=0)
    127         self.TryRefreshHistory(value)
    128 
    129     def GetValid(self):
    130         validHistory = super(SearchBox, self).GetValid()
    131         hints = self.GetDynamicHints()
    132         return hints + validHistory
    133 
    134     def Confirm(self, *args):
    135         active = getattr(self, 'active', None)
    136         text = ''
    137         if active:
    138             text = active.string
    139             self.SetValue(text)
    140         super(SearchBox, self).Confirm(*args)
    141         if active:
    142             self.TryRefreshHistory(text)
    143 
    144     def OnClearButtonClick(self, *args, **kwds):
    145         super(SearchBox, self).OnClearButtonClick()
    146         if self.OnClearFilter:
    147             self.OnClearFilter()