carousel.py (5252B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\carousel.py 2 import logging 3 from carbon.common.script.util.timerstuff import AutoTimer 4 from carbonui import uiconst 5 from carbonui.control.buttonIcon import ButtonIcon 6 from carbonui.control.scrollContainer import ScrollContainer 7 from carbonui.primitives.containerAutoSize import ContainerAutoSize 8 from carbonui.uianimations import animations 9 from collections import OrderedDict 10 CIRCLE_FULL_PNG = 'res:/UI/Texture/Shared/Carousel/circleFull.png' 11 CIRCLE_EMPTY_PNG = 'res:/UI/Texture/Shared/Carousel/circleEmpty.png' 12 logger = logging.getLogger(__name__) 13 14 class Carousel(ScrollContainer): 15 default_name = 'Carousel' 16 default_alignMode = uiconst.TOLEFT 17 default_scrollBarDisabled = True 18 default_interval = 5 19 default_scrollSpeed = 1.2 20 21 def ApplyAttributes(self, attributes): 22 super(Carousel, self).ApplyAttributes(attributes) 23 self.buttons = OrderedDict() 24 self.interval = attributes.get('interval', self.default_interval) 25 self.scrollSpeed = attributes.get('scrollSpeed', self.default_scrollSpeed) 26 self.autoScrollTimer = None 27 self.previousButton = None 28 self.ConstructButtonContainer() 29 self.ConstructControlButtons() 30 self.StartAutoScroll() 31 32 def InitializeButtons(self): 33 self.ClearButtons() 34 try: 35 firstButton = self.buttons.values()[0] 36 except IndexError: 37 logger.error('cannot initialize carousel without children') 38 else: 39 self.SetButtonFull(firstButton) 40 41 def ConstructButtonContainer(self): 42 self.buttonContainer = ContainerAutoSize(name='carouselButtons', parent=self.clipCont, align=uiconst.CENTERBOTTOM, state=uiconst.UI_NORMAL, height=24, top=8, idx=0) 43 44 def ConstructControlButtons(self): 45 self.leftButton = ButtonIcon(name='leftButton', parent=self.buttonContainer, align=uiconst.TOLEFT, width=8, height=8, iconSize=8, texturePath='res:/UI/Texture/Shared/Carousel/leftArrow.png', func=self.SelectPreviousChild) 46 self.centerButtonContainer = ContainerAutoSize(parent=self.buttonContainer, align=uiconst.TOLEFT) 47 self.rightButton = ButtonIcon(name='rightButton', parent=self.buttonContainer, align=uiconst.TOLEFT, width=8, height=8, iconSize=8, left=4, texturePath='res:/UI/Texture/Shared/Carousel/rightArrow.png', func=self.SelectNextChild) 48 49 def show_control_buttons(self): 50 self.leftButton.display = True 51 self.rightButton.display = True 52 53 def hide_control_buttons(self): 54 self.leftButton.display = False 55 self.rightButton.display = False 56 57 def AddControlButtonForChild(self, child): 58 if child in self.buttons: 59 return 60 self.buttons[child] = ButtonIcon(name='%s_carouselCircle' % child.name, parent=self.centerButtonContainer, align=uiconst.TOLEFT, width=8, height=8, iconSize=6, left=4, texturePath=CIRCLE_EMPTY_PNG) 61 62 def Close(self, *args): 63 self.StopAutoScroll() 64 super(Carousel, self).Close(*args) 65 66 def _InsertChild(self, idx, obj): 67 super(Carousel, self)._InsertChild(idx, obj) 68 self.AddControlButtonForChild(obj) 69 70 def _AppendChild(self, obj): 71 super(Carousel, self)._AppendChild(obj) 72 self.AddControlButtonForChild(obj) 73 74 def StartAutoScroll(self): 75 self.autoScrollTimer = AutoTimer(self.interval * 1000, self.ScrollToNext) 76 77 def StopAutoScroll(self): 78 if not self.autoScrollTimer: 79 return 80 self.autoScrollTimer.KillTimer() 81 self.autoScrollTimer = None 82 83 def ScrollToNext(self): 84 if self.mainCont.HasAnimation('left'): 85 return 86 childToMove = self.mainCont.children[0] 87 self.ClearButtons() 88 self.SetButtonFull(self.buttons[self.mainCont.children[1]]) 89 animations.MorphScalar(self.mainCont, 'left', startVal=self.mainCont.left, endVal=self.mainCont.left - childToMove.width, duration=self.scrollSpeed, callback=lambda : self.MoveChildToBack(childToMove), curveType=uiconst.ANIM_SMOOTH) 90 91 def ScrollToPrevious(self): 92 if self.mainCont.HasAnimation('left'): 93 return 94 self.MoveChildToFront(self.mainCont.children[-1]) 95 animations.MorphScalar(self.mainCont, 'left', startVal=self.mainCont.left, endVal=0, duration=self.scrollSpeed, curveType=uiconst.ANIM_SMOOTH) 96 97 def MoveChildToFront(self, child): 98 self._RemoveChild(child) 99 self._InsertChild(0, child) 100 self.ClearButtons() 101 self.SetButtonFull(self.buttons[child]) 102 self.mainCont.left = -child.width 103 104 def ClearButtons(self): 105 for button in self.buttons.values(): 106 button.SetTexturePath(CIRCLE_EMPTY_PNG) 107 108 @staticmethod 109 def SetButtonFull(button): 110 button.SetTexturePath(CIRCLE_FULL_PNG) 111 112 def MoveChildToBack(self, child): 113 self._RemoveChild(child) 114 self._AppendChild(child) 115 self.mainCont.left = 0 116 117 def SelectPreviousChild(self, *args): 118 self.StopAutoScroll() 119 self.ScrollToPrevious() 120 self.StartAutoScroll() 121 122 def SelectNextChild(self, *args): 123 self.StopAutoScroll() 124 self.ScrollToNext() 125 self.StartAutoScroll()