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

primaryButton.py (8308B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\primaryButton.py
      2 import abc
      3 import contextlib
      4 import functools
      5 import math
      6 import weakref
      7 import caching
      8 import proper
      9 import signals
     10 import threadutils
     11 import trinity
     12 from carbon.common.script.util.commonutils import StripTags
     13 from carbonui import uiconst
     14 from carbonui.primitives.sprite import Sprite
     15 from carbonui.uianimations import animations
     16 from carbonui.util.color import Color
     17 from carbonui.control.button import Button
     18 from eveui import skip_if_destroyed
     19 
     20 def optional(value, default):
     21     if value is not None:
     22         return value
     23     return default
     24 
     25 
     26 class PrimaryButton(Button):
     27     ARROW_ANIMATION_DURATION = 2.0
     28     LABEL_PAD_HORIZONTAL = 30
     29     LABEL_PAD_VERTICAL = 6
     30     MIN_HEIGHT = 32
     31     MIN_WIDTH = 64
     32     RES_ARROWS = 'res:/UI/Texture/Classes/Industry/CenterBar/arrows.png'
     33     RES_ARROWS_BAR_COUNT = 6
     34     RES_ARROWS_MASK = 'res:/UI/Texture/Classes/Industry/CenterBar/arrowMask.png'
     35     RES_BUTTON_BACKGROUND = 'res:/UI/Texture/Classes/Industry/CenterBar/buttonBg.png'
     36     RES_BUTTON_PATTERN = 'res:/UI/Texture/Classes/Industry/CenterBar/buttonPattern.png'
     37     default_bold = True
     38     default_fontsize = 13
     39     default_uppercase = True
     40 
     41     def __init__(self, controller = None, color = None, colorType = None, label = None, func = None, args = None, **kwargs):
     42         if controller is None:
     43             controller = self._create_default_controller(label=label, func=func, args=args, color=color, color_type=colorType)
     44         self.controller = controller
     45         self._is_updating_label = False
     46         self._original_args = args
     47         super(PrimaryButton, self).__init__(func=self.controller.click, args=(), busy=self.controller.is_arrow_animated, **kwargs)
     48         self._update_enabled()
     49         self._update_label()
     50         self.controller.bind(is_arrow_animated=self._update_arrow, is_enabled=self._update_enabled, label=self._update_label_animated)
     51 
     52     def Close(self):
     53         self.controller.close()
     54         super(PrimaryButton, self).Close()
     55 
     56     def Disable(self):
     57         self.controller.is_enabled = False
     58 
     59     def Enable(self):
     60         self.controller.is_enabled = True
     61 
     62     def SetColor(self, color):
     63         self.controller.style = FixedColorStyle(color)
     64 
     65     def SetFunc(self, func):
     66         self.controller.on_clicked = self._create_default_callback(func, self._original_args)
     67 
     68     def SetLabel(self, text):
     69         super(PrimaryButton, self).SetLabel(text)
     70 
     71     def Update_Size_(self):
     72         super(PrimaryButton, self).Update_Size_()
     73 
     74     def _align_text(self, text):
     75         return StripTags(text, stripOnly=['center'])
     76 
     77     def _create_default_controller(self, label, color, color_type, func, args):
     78         controller = PrimaryButtonController(label=label, style=self._create_default_style(color, color_type))
     79         controller.on_clicked = self._create_default_callback(func, args)
     80         return controller
     81 
     82     def _create_default_callback(self, function, arguments):
     83         if function is None:
     84             return
     85         elif isinstance(arguments, tuple):
     86             return functools.partial(function, *arguments)
     87         elif arguments is not None:
     88             return functools.partial(function, arguments)
     89         else:
     90             return functools.partial(function, weakref.proxy(self))
     91 
     92     def _create_default_style(self, color, color_type):
     93         if color is not None:
     94             return FixedColorStyle(base_color=color)
     95         else:
     96             return ThemeColorStyle(color_type)
     97 
     98     @skip_if_destroyed
     99     def _update_arrow(self, *args):
    100         self.busy = self.controller.is_arrow_animated
    101 
    102     @skip_if_destroyed
    103     def _update_enabled(self, *args):
    104         self.enabled = self.controller.is_enabled
    105 
    106     @skip_if_destroyed
    107     def _update_label(self):
    108         if self._is_updating_label:
    109             return
    110         self.SetLabel(self.controller.label)
    111 
    112     @threadutils.threaded
    113     @skip_if_destroyed
    114     def _update_label_animated(self, *args):
    115         self.label = self.controller.label
    116 
    117 
    118 class PrimaryButtonStyle(object):
    119     __metaclass__ = abc.ABCMeta
    120 
    121     @abc.abstractproperty
    122     def color_arrow(self):
    123         pass
    124 
    125     @abc.abstractproperty
    126     def color_frame(self):
    127         pass
    128 
    129     @abc.abstractproperty
    130     def color_highlight(self):
    131         pass
    132 
    133     @abc.abstractproperty
    134     def color_label(self):
    135         pass
    136 
    137     @abc.abstractproperty
    138     def color_underlay(self):
    139         pass
    140 
    141     @caching.lazy_property
    142     def on_update(self):
    143         return signals.Signal(signalName='on_update')
    144 
    145 
    146 def rgba_color(color):
    147     return Color(*color).GetRGBA()
    148 
    149 
    150 class FixedColorStyle(proper.Observable, PrimaryButtonStyle):
    151     default_base_color = Color.GRAY2
    152 
    153     def __init__(self, **kwargs):
    154         super(FixedColorStyle, self).__init__(**kwargs)
    155 
    156     @proper.ty(coerce=rgba_color)
    157     def base_color(self):
    158         pass
    159 
    160     def on_base_color(self, color):
    161         self.on_update()
    162 
    163     @proper.alias
    164     def color_arrow(self):
    165         return clamp_color_brightness(self.base_color, brightness_min=0.4)
    166 
    167     @proper.alias
    168     def color_frame(self):
    169         return clamp_color_brightness(self.base_color, brightness_min=0.1)
    170 
    171     @proper.alias
    172     def color_highlight(self):
    173         return clamp_color_brightness(self.base_color, 0.1, 0.2)
    174 
    175     @proper.alias
    176     def color_label(self):
    177         return clamp_color_saturation(clamp_color_brightness(self.base_color, brightness_min=1.0), saturation_max=0.1)
    178 
    179     @proper.alias
    180     def color_underlay(self):
    181         return clamp_color_brightness(self.base_color, 0.1, 0.6)
    182 
    183 
    184 def clamp_color_brightness(color, brightness_min = 0.0, brightness_max = 1.0):
    185     color = Color(*color)
    186     color.SetBrightness(max(brightness_min, min(color.GetBrightness(), brightness_max)))
    187     return color.GetRGBA()
    188 
    189 
    190 def clamp_color_saturation(color, saturation_min = 0.0, saturation_max = 1.0):
    191     color = Color(*color)
    192     color.SetSaturation(max(saturation_min, min(color.GetSaturation(), saturation_max)))
    193     return color.GetRGBA()
    194 
    195 
    196 class ThemeColorStyle(FixedColorStyle):
    197 
    198     def __init__(self, color_type = None):
    199         self._color_type = color_type or uiconst.COLORTYPE_UIHILIGHT
    200         super(ThemeColorStyle, self).__init__(base_color=self._get_theme_color())
    201 
    202     @property
    203     def color_type(self):
    204         return self._color_type
    205 
    206     @property
    207     def ui_color_service(self):
    208         return sm.GetService('uiColor')
    209 
    210     def _get_theme_color(self):
    211         return self.ui_color_service.GetUIColor(self.color_type)
    212 
    213     def UpdateColor(self):
    214         self.base_color = self._get_theme_color()
    215 
    216 
    217 class PrimaryButtonController(proper.Observable):
    218     default_is_arrow_animated = False
    219     default_is_enabled = True
    220     default_label = None
    221 
    222     def __init__(self, **kwargs):
    223         super(PrimaryButtonController, self).__init__(**kwargs)
    224         self._is_clicking = False
    225         self.style.on_update.connect(self.on_style_update)
    226 
    227     @proper.ty(coerce=bool)
    228     def is_arrow_animated(self):
    229         pass
    230 
    231     @proper.ty(coerce=bool)
    232     def is_enabled(self):
    233         pass
    234 
    235     @proper.ty(default=False, coerce=bool)
    236     def is_hovered(self):
    237         pass
    238 
    239     @proper.ty
    240     def label(self):
    241         pass
    242 
    243     @proper.ty
    244     def style(self):
    245         pass
    246 
    247     @style.before_change
    248     def _reconnect_style_signal(self, style):
    249         self.style.on_update.disconnect(self.on_style_update)
    250         style.on_update.connect(self.on_style_update)
    251 
    252     @style.default
    253     def style_default(self):
    254         return self.default_style
    255 
    256     @caching.lazy_property
    257     def default_style(self):
    258         return ThemeColorStyle()
    259 
    260     def on_style_update(self):
    261         self.dispatch('style')
    262 
    263     @contextlib.contextmanager
    264     def arrow_animated(self):
    265         was_arrow_animated = self.is_arrow_animated
    266         self.is_arrow_animated = True
    267         try:
    268             yield
    269         finally:
    270             if self.is_arrow_animated:
    271                 self.is_arrow_animated = was_arrow_animated
    272 
    273     def click(self):
    274         if self._is_clicking:
    275             return
    276         self._is_clicking = True
    277         try:
    278             self.on_clicked()
    279         finally:
    280             self._is_clicking = False
    281 
    282     def close(self):
    283         self.unbind_all()
    284 
    285     def on_clicked(self):
    286         pass