Exemplo n.º 1
0
class ToggleButtonGroupButton(Container):
    OPACITY_SELECTED = 1.0
    OPACITY_HOVER = 0.125
    TEXT_TOPMARGIN = 4
    default_padRight = 1
    default_align = uiconst.TOLEFT_PROP
    default_state = uiconst.UI_NORMAL
    default_iconSize = 32
    default_colorSelected = None
    default_iconOpacity = 1.0
    default_showBg = True

    def ApplyAttributes(self, attributes):
        Container.ApplyAttributes(self, attributes)
        self.controller = attributes.controller
        self.btnID = attributes.Get('btnID', None)
        self.panel = attributes.Get('panel', None)
        self.colorSelected = attributes.Get('colorSelected',
                                            self.default_colorSelected)
        label = attributes.Get('label', None)
        iconPath = attributes.Get('iconPath', None)
        iconSize = attributes.Get('iconSize', None)
        iconSize = iconSize or self.default_iconSize
        iconOpacity = attributes.get('iconOpacity', self.default_iconOpacity)
        self.hint = attributes.Get('hint', None)
        self.isSelected = False
        self.isDisabled = attributes.Get('isDisabled', False)
        self.showBg = attributes.Get('showBg', self.default_showBg)
        self.ConstructLayout(iconOpacity, iconPath, iconSize, label)
        self.selectedBG = RaisedUnderlay(bgParent=self,
                                         color=self.colorSelected,
                                         isGlowEdgeRotated=True)
        if not self.showBg:
            self.selectedBG.display = False
        if self.isDisabled:
            self.SetDisabled()

    def ConstructLayout(self, iconOpacity, iconPath, iconSize, label):
        if iconPath:
            self.AddIcon(self, iconOpacity, iconPath, iconSize)
            self.label = None
        else:
            clipper = Container(parent=self, clipChildren=True)
            self.AddLabel(clipper, label)
            self.icon = None

    def AddIcon(self, parent, iconOpacity, iconPath, iconSize):
        self.icon = GlowSprite(parent=parent,
                               align=uiconst.CENTER,
                               state=uiconst.UI_DISABLED,
                               width=iconSize,
                               height=iconSize,
                               texturePath=iconPath,
                               iconOpacity=iconOpacity,
                               color=Color.GRAY6)

    def AddLabel(self, parent, label):
        self.label = LabelThemeColored(text=label,
                                       parent=parent,
                                       align=uiconst.CENTER,
                                       fontsize=EVE_SMALL_FONTSIZE)

    def GetAutoHeight(self):
        if self.label:
            return self.label.textheight + self.TEXT_TOPMARGIN * 2
        if self.icon:
            return self.icon.height
        return 0

    def SetDisabled(self):
        self.isDisabled = True
        if self.icon:
            self.icon.opacity = 0.1
        if self.label:
            self.label.opacity = 0.1
        if self.showBg:
            self.selectedBG.SetDisabled()

    def SetEnabled(self):
        self.isDisabled = False
        if self.showBg:
            self.selectedBG.SetEnabled()

    def OnMouseEnter(self, *args):
        if not self.isSelected and not self.isDisabled:
            self.selectedBG.OnMouseEnter()
            if self.icon:
                self.icon.OnMouseEnter()
            else:
                uicore.animations.FadeTo(self.label,
                                         self.label.opacity,
                                         OPACITY_LABEL_HOVER,
                                         duration=uiconst.TIME_ENTRY)

    def OnMouseExit(self, *args):
        if self.isDisabled:
            return
        if not self.isSelected:
            self.selectedBG.OnMouseExit()
        if self.icon:
            self.icon.OnMouseExit()
        elif not self.isSelected:
            uicore.animations.FadeTo(self.label,
                                     self.label.opacity,
                                     OPACITY_LABEL_IDLE,
                                     duration=uiconst.TIME_EXIT)

    def OnMouseDown(self, *args):
        if self.isDisabled:
            return
        if self.icon:
            self.icon.OnMouseDown()
        self.selectedBG.OnMouseDown()

    def OnMouseUp(self, *args):
        if self.isDisabled:
            return
        if self.icon:
            self.icon.OnMouseUp()
        self.selectedBG.OnMouseUp()

    def SetSelected(self, animate=True):
        self.isSelected = True
        if not self.showBg:
            self.selectedBG.display = True
        self.selectedBG.Select()
        if self.label:
            self.label.opacity = OPACITY_LABEL_HOVER
        if self.icon:
            self.icon.OnMouseExit()

    def SetDeselected(self, animate=True):
        self.isSelected = False
        if self.label:
            self.label.opacity = 1.0
        if self.isDisabled:
            return
        if not self.showBg:
            self.selectedBG.display = False
        self.selectedBG.Deselect()

    def IsSelected(self):
        return self.isSelected

    def OnClick(self, *args):
        if not self.isDisabled:
            self.controller.Select(self)
Exemplo n.º 2
0
class ToggleButtonGroupButton(Container):
    OPACITY_SELECTED = 1.0
    OPACITY_HOVER = 0.125
    default_padRight = 1
    default_align = uiconst.TOLEFT_PROP
    default_state = uiconst.UI_NORMAL
    default_iconSize = 32
    default_colorSelected = None

    def ApplyAttributes(self, attributes):
        Container.ApplyAttributes(self, attributes)
        self.controller = attributes.controller
        self.btnID = attributes.Get('btnID', None)
        self.panel = attributes.Get('panel', None)
        self.colorSelected = attributes.Get('colorSelected',
                                            self.default_colorSelected)
        label = attributes.Get('label', None)
        iconPath = attributes.Get('iconPath', None)
        iconSize = attributes.Get('iconSize', None)
        iconSize = iconSize or self.default_iconSize
        self.hint = attributes.Get('hint', None)
        self.isSelected = False
        self.isDisabled = attributes.Get('isDisabled', False)
        if iconPath:
            self.icon = GlowSprite(parent=self,
                                   align=uiconst.CENTER,
                                   state=uiconst.UI_DISABLED,
                                   width=iconSize,
                                   height=iconSize,
                                   texturePath=iconPath,
                                   iconOpacity=0.75,
                                   color=Color.WHITE)
            self.label = None
        else:
            self.label = LabelUnderlay(text=label,
                                       parent=self,
                                       align=uiconst.CENTER,
                                       fontsize=10)
            self.icon = None
        self.selectedBG = RaisedUnderlay(bgParent=self,
                                         color=self.colorSelected,
                                         isGlowEdgeRotated=True)
        if self.isDisabled:
            self.SetDisabled()

    def SetDisabled(self):
        self.isDisabled = True
        if self.icon:
            self.icon.opacity = 0.1
        if self.label:
            self.label.opacity = 0.1
        self.selectedBG.SetDisabled()

    def SetEnabled(self):
        self.isDisabled = False
        self.selectedBG.SetEnabled()

    def OnMouseEnter(self, *args):
        if not self.isSelected and not self.isDisabled:
            self.selectedBG.OnMouseEnter()
            if self.icon:
                self.icon.OnMouseEnter()
            else:
                uicore.animations.FadeTo(self.label,
                                         self.label.opacity,
                                         1.5,
                                         duration=uiconst.TIME_ENTRY)

    def OnMouseExit(self, *args):
        if self.isDisabled:
            return
        if not self.isSelected:
            self.selectedBG.OnMouseExit()
        if self.icon:
            self.icon.OnMouseExit()
        elif not self.isSelected:
            uicore.animations.FadeTo(self.label,
                                     self.label.opacity,
                                     1.0,
                                     duration=uiconst.TIME_EXIT)

    def OnMouseDown(self, *args):
        if self.isDisabled:
            return
        if self.icon:
            self.icon.OnMouseDown()
        self.selectedBG.OnMouseDown()

    def OnMouseUp(self, *args):
        if self.isDisabled:
            return
        if self.icon:
            self.icon.OnMouseUp()
        self.selectedBG.OnMouseUp()

    def SetSelected(self, animate=True):
        self.isSelected = True
        self.selectedBG.Select()
        if self.label:
            self.label.opacity = 1.5
        if self.icon:
            self.icon.OnMouseExit()

    def SetDeselected(self, animate=True):
        self.isSelected = False
        if self.label:
            self.label.opacity = 1.0
        if self.isDisabled:
            return
        self.selectedBG.Deselect()

    def IsSelected(self):
        return self.isSelected

    def OnClick(self, *args):
        if not self.isDisabled:
            self.controller.Select(self)