traits.py (6971B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\shared\traits.py 2 import eveicon 3 import evetypes 4 import infobubbles 5 import localization 6 import carbonui.const as uiconst 7 from carbon.client.script.environment.AudioUtil import PlaySound 8 from carbon.common.script.util.commonutils import StripTags 9 from carbonui import fontconst, TextColor 10 from carbonui.primitives.container import Container 11 from carbonui.primitives.containerAutoSize import ContainerAutoSize 12 from carbonui.primitives.flowcontainer import FlowContainer 13 from carbonui.primitives.sprite import Sprite 14 from carbonui.util.color import Color 15 from dogma import units 16 from eve.client.script.ui.control.eveLabel import Label 17 from carbonui.uicore import uicore 18 ROLE_BONUS_TYPE = -1 19 MISC_BONUS_TYPE = -2 20 COLOR_CAPTION = (0.298, 0.549, 0.69, 1.0) 21 COLOR_TEXT_HILITE = (0.765, 0.914, 1.0, 1.0) 22 COLOR_TEXT = (0.5, 0.5, 0.5, 1.0) 23 COLOR_TEXT_HILITE_HEX = Color.RGBtoHex(*COLOR_TEXT_HILITE) 24 TAB_MARGIN = 3 25 26 def HasTraits(typeID): 27 return infobubbles.has_traits(typeID) 28 29 30 class TraitsContainer(ContainerAutoSize): 31 default_name = 'TraitsContainer' 32 default_align = uiconst.TOTOP 33 34 def ApplyAttributes(self, attributes): 35 super(TraitsContainer, self).ApplyAttributes(attributes) 36 self.typeID = attributes.typeID 37 displayTraitAttributeIcons = attributes.get('traitAttributeIcons', False) 38 padLeft = self.GetLeftPad() 39 if displayTraitAttributeIcons and self.typeID in cfg.infoBubbleTypeElements: 40 self.AddCaption(localization.GetByLabel('UI/InfoWindow/ShipCharacteristics')) 41 self.AddAttributeIcons(cfg.infoBubbleTypeElements[self.typeID], padLeft) 42 if not infobubbles.has_traits(self.typeID): 43 return 44 bonusLabels = [] 45 for skillTypeID, bonuses in infobubbles.iter_ship_skill_bonuses(self.typeID): 46 self.AddCaption(localization.GetByLabel('UI/ShipTree/SkillNameCaption', skillName=evetypes.GetName(skillTypeID))) 47 bonusLabels += self.AddBonusLabel(bonuses, padLeft) 48 49 role_bonus = infobubbles.get_role_bonus(self.typeID) 50 if role_bonus: 51 self.AddCaption(localization.GetByLabel('UI/ShipTree/RoleBonus')) 52 bonusLabels += self.AddBonusLabel(role_bonus, padLeft) 53 misc_bonus = infobubbles.get_misc_bonus(self.typeID) 54 if misc_bonus: 55 self.AddCaption(text=localization.GetByLabel('UI/ShipTree/MiscBonus')) 56 bonusLabels += self.AddBonusLabel(misc_bonus, padLeft) 57 self.AdjustBonusLabels(bonusLabels) 58 59 def AdjustBonusLabels(self, bonusLabels): 60 defaultPadding = self.GetLeftPad() 61 maxWidth = defaultPadding 62 for b in bonusLabels: 63 parts = b.text.split('<t>') 64 if parts: 65 col0Text = StripTags(parts[0], ignoredTags=['b']) 66 w, h = Label.MeasureTextSize(col0Text) 67 maxWidth = max(maxWidth, w + TAB_MARGIN) 68 69 if maxWidth > defaultPadding: 70 for b in bonusLabels: 71 b.tabs = (maxWidth,) 72 73 def AddCaption(self, text): 74 Label(parent=self, text=text, align=uiconst.TOTOP, padTop=8 if self.children else 0, bold=True, color=COLOR_CAPTION) 75 76 def AddBonusLabel(self, bonuses, padLeft): 77 bonusLines = [] 78 for bonus in sorted(bonuses, key=lambda x: x['importance']): 79 b = self._FormatBonusLine(bonus, padLeft) 80 bonusLines.append(b) 81 82 return bonusLines 83 84 def AddAttributeIcons(self, data, padLeft): 85 rowCont = Container(parent=self, align=uiconst.TOTOP, padTop=3) 86 labelCont = Container(parent=rowCont, align=uiconst.TOPLEFT, height=TraitAttributeIcon.default_height, width=0) 87 text = localization.GetByLabel('UI/InfoWindow/TraitWithoutNumber', color=Color.RGBtoHex(*COLOR_TEXT_HILITE), bonusText='') 88 Label(parent=labelCont, text=text, align=uiconst.CENTERLEFT, state=uiconst.UI_NORMAL, color=COLOR_TEXT, tabs=(padLeft,), tabMargin=3) 89 iconCont = FlowContainer(parent=rowCont, align=uiconst.TOTOP, padLeft=3, contentSpacing=(1, 1), autoHeight=True) 90 old_OnSizeChange_NoBlock = iconCont._OnSizeChange_NoBlock 91 92 def OnSizeChange_NoBlock_Hook(newWidth, newHeight): 93 old_OnSizeChange_NoBlock(newWidth, newHeight) 94 rowCont.height = newHeight 95 96 iconCont._OnSizeChange_NoBlock = OnSizeChange_NoBlock_Hook 97 attributeIDs = [ x[1] for x in sorted(data.items(), key=lambda data: int(data[0])) ] 98 for attributeID in attributeIDs: 99 TraitAttributeIcon(parent=iconCont, align=uiconst.NOALIGN, attributeID=attributeID) 100 101 def GetLeftPad(self): 102 return 40 * fontconst.fontSizeFactor 103 104 def _FormatBonusLine(self, data, padLeft): 105 text = self._GetBonusText(data) 106 label = Label(parent=self, text=text, align=uiconst.TOTOP, state=uiconst.UI_NORMAL, padTop=3, color=COLOR_TEXT, tabs=(padLeft,), tabMargin=TAB_MARGIN) 107 return label 108 109 def _GetBonusText(self, data): 110 if 'bonus' in data: 111 bonus = float(data['bonus']) 112 value = round(bonus, 1) 113 if int(bonus) == bonus: 114 value = int(bonus) 115 text = localization.GetByLabel('UI/InfoWindow/TraitWithNumber', color=COLOR_TEXT_HILITE_HEX, value=value, unit=units.get_display_name(int(data['unitID'])), bonusText=localization.GetByMessageID(int(data['nameID']))) 116 else: 117 text = localization.GetByLabel('UI/InfoWindow/TraitWithoutNumber', color=COLOR_TEXT_HILITE_HEX, bonusText=localization.GetByMessageID(int(data['nameID']))) 118 return text 119 120 121 class TraitAttributeIcon(Container): 122 default_name = 'TraitAttributeIcon' 123 default_width = 30 124 default_height = 30 125 default_state = uiconst.UI_NORMAL 126 127 def ApplyAttributes(self, attributes): 128 super(TraitAttributeIcon, self).ApplyAttributes(attributes) 129 self._iconOpacity = attributes.get('iconOpacity', TextColor.SECONDARY.opacity) 130 self._iconHoverOpacity = attributes.get('iconHoverOpacity', TextColor.NORMAL.opacity) 131 self.attributeID = attributes.attributeID 132 iconID = cfg.infoBubbleElements[self.attributeID]['icon'] 133 self.icon = Sprite(parent=self, align=uiconst.TOALL, state=uiconst.UI_DISABLED, texturePath=eveicon.get(iconID, default=iconID), opacity=self._iconOpacity) 134 135 def GetHint(self): 136 nameID = cfg.infoBubbleElements[self.attributeID]['nameID'] 137 descriptionID = cfg.infoBubbleElements[self.attributeID]['descriptionID'] 138 return '<b>' + localization.GetByMessageID(nameID) + '</b><br>' + localization.GetByMessageID(descriptionID) 139 140 def OnMouseEnter(self): 141 PlaySound(uiconst.SOUND_ENTRY_HOVER) 142 uicore.animations.FadeTo(self.icon, self.icon.opacity, self._iconHoverOpacity, duration=0.2) 143 144 def OnMouseExit(self): 145 uicore.animations.FadeTo(self.icon, self.icon.opacity, self._iconOpacity, duration=0.2)