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

gradientSprite.py (5257B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\primitives\gradientSprite.py
      2 import math
      3 import trinity
      4 from carbonui.primitives.sprite import Sprite
      5 from carbonui.util import colorblind
      6 
      7 class GradientConst:
      8     INTERP_LINEAR = 0
      9     INTERP_COSINE = 1
     10     INTERP_BEZIER = 2
     11     interpModeToString = {INTERP_LINEAR: 'linear',
     12      INTERP_COSINE: 'cosine',
     13      INTERP_BEZIER: 'bezier'}
     14     interpModeFromString = {'linear': INTERP_LINEAR,
     15      'cosine': INTERP_COSINE,
     16      'bezier': INTERP_BEZIER}
     17 
     18 
     19 class GradientSprite(Sprite):
     20     __guid__ = 'uicontrols.GradientSprite'
     21     default_rgbData = [(0.0, (1.0, 0.0, 0.0)), (0.5, (0.0, 1.0, 0.0)), (1.0, (0.0, 0.0, 1.0))]
     22     default_alphaData = [(0.0, 1.0), (1.0, 1.0)]
     23     default_textureSize = 128
     24     default_colorInterp = GradientConst.INTERP_LINEAR
     25     default_alphaInterp = GradientConst.INTERP_LINEAR
     26     default_toCorners = False
     27     default_radial = False
     28 
     29     def ApplyAttributes(self, attributes):
     30         self.colorData = attributes.get('rgbData', self.default_rgbData)
     31         self.alphaData = attributes.get('alphaData', self.default_alphaData)
     32         self.textureSize = attributes.get('textureSize', self.default_textureSize)
     33         self.colorInterp = attributes.get('colorInterp', self.default_colorInterp)
     34         self.alphaInterp = attributes.get('alphaInterp', self.default_alphaInterp)
     35         self.toCorners = attributes.get('toCorners', self.default_toCorners)
     36         self.radial = attributes.get('radial', self.default_radial)
     37         Sprite.ApplyAttributes(self, attributes)
     38         self.SetGradient()
     39         trinity.device.RegisterResource(self)
     40 
     41     def SetGradient(self, colorData = None, alphaData = None):
     42         if colorData:
     43             self.colorData = colorData
     44         if alphaData:
     45             self.alphaData = alphaData
     46         self._CheckColorBlindReplace()
     47         rgbDivs = [ div for div, color in self.colorData ]
     48         rgbPoints = [ color for div, color in self.colorData ]
     49         alphaDivs = [ div for div, alpha in self.alphaData ]
     50         alphaPoints = [ alpha for div, alpha in self.alphaData ]
     51         gradientData = {'rgbInterp': GradientConst.interpModeToString[self.colorInterp],
     52          'alphaInterp': GradientConst.interpModeToString[self.alphaInterp],
     53          'rgbDivs': rgbDivs,
     54          'rgbPoints': rgbPoints,
     55          'alphaDivs': alphaDivs,
     56          'alphaPoints': alphaPoints,
     57          'textureSize': self.textureSize,
     58          'toCorners': self.toCorners}
     59         if self.radial:
     60             prefixString = 'dynamic:/gradient_radial/'
     61         else:
     62             prefixString = 'dynamic:/gradient/'
     63         self.gradientDataString = prefixString + str(gradientData)
     64         self.SetTexturePath(self.gradientDataString)
     65 
     66     def _CheckColorBlindReplace(self):
     67         if not self.ignoreColorBlindMode:
     68             self.colorData = [ (x, colorblind.CheckReplaceColor(color, maintainBrightness=True)) for x, color in self.colorData ]
     69 
     70     @property
     71     def rotation(self):
     72         return self.texture.rotation
     73 
     74     @rotation.setter
     75     def rotation(self, value):
     76         if not self.texture:
     77             return
     78         self.texture.rotation = value
     79         self.texture.useTransform = bool(value)
     80         if value:
     81             value %= math.pi / 2.0
     82             diff = self.textureSize / 2.0 * (math.sin(value) + math.cos(value) - 1.0)
     83             k_magic = 0.99
     84             self.texture.srcWidth = (self.textureSize - 2.0 * diff) * k_magic
     85             self.texture.srcX = math.ceil(diff)
     86 
     87     def OnInvalidate(self, level):
     88         if self.texture:
     89             self.texture.atlasTexture = None
     90 
     91     def OnCreate(self, dev):
     92         self.SetTexturePath(self.gradientDataString)
     93 
     94 
     95 class Gradient2DSprite(Sprite):
     96     __guid__ = 'uicls.Gradient2DSprite'
     97     default_colors = [(1.0, 1.0, 1.0), (0.0, 0.0, 0.0)]
     98     default_interp = [0.0, 1.0]
     99     default_alpha = [1.0, 0.0]
    100     default_textureSize = 128
    101 
    102     def ApplyAttributes(self, kwargs):
    103         Sprite.ApplyAttributes(self, kwargs)
    104         gradientData = {'rgbDataHorizontal': kwargs.get('rgbDataHorizontal', self.default_colors),
    105          'rgbDataVertical': kwargs.get('rgbDataVertical', self.default_colors),
    106          'alphaDataHorizontal': kwargs.get('alphaDataHorizontal', self.default_alpha),
    107          'alphaDataVertical': kwargs.get('alphaDataVertical', self.default_alpha),
    108          'rgbHorizontal': kwargs.get('rgbHorizontal', self.default_interp),
    109          'rgbVertical': kwargs.get('rgbVertical', self.default_interp),
    110          'alphaHorizontal': kwargs.get('alphaHorizontal', self.default_interp),
    111          'alphaVertical': kwargs.get('alphaVertical', self.default_interp),
    112          'textureSize': kwargs.get('textureSize', self.default_textureSize),
    113          'rgbInterp': kwargs.get('rgbInterp', 'linear'),
    114          'alphaInterp': kwargs.get('alphaInterp', 'linear')}
    115         self.gradientDataString = 'dynamic:/gradient2d/' + str(gradientData)
    116         self.SetTexturePath(self.gradientDataString)
    117         trinity.device.RegisterResource(self)
    118 
    119     def OnInvalidate(self, level):
    120         if self.texture:
    121             self.texture.atlasTexture = None
    122 
    123     def OnCreate(self, dev):
    124         self.SetTexturePath(self.gradientDataString)