kivyblocks/kivyblocks/bgcolorbehavior.py

86 lines
2.1 KiB
Python
Raw Normal View History

2020-04-02 13:18:10 +08:00
from kivy.logger import Logger
from kivy.graphics import Color, Rectangle
from kivy.properties import ListProperty
2020-04-24 18:35:30 +08:00
from .color_definitions import getColors
2020-04-02 13:18:10 +08:00
_logcnt = 0
class BGColorBehavior(object):
2020-04-24 18:35:30 +08:00
def __init__(self, bgcolor=[],fgcolor=[], color_level=-1,**kwargs):
self.color_level = color_level
2020-04-02 13:18:10 +08:00
self.bgcolor = bgcolor
2020-04-24 18:35:30 +08:00
self.fgcolor = fgcolor
self.normal_bgcolor = bgcolor
self.normal_fgcolor = fgcolor
self.selected_bgcolor = bgcolor
self.selected_fgcolor = fgcolor
self.useOwnColor = False
self.useOwnBG = False
self.useOwnFG = False
if color_level != -1:
fg,bg= getColors(color_level)
self.fgcolor = fg
self.bgcolor = bg
self.normal_bgcolor = bg
self.normal_fgcolor = fg
fg,bg= getColors(color_level,selected=True)
self.selected_bgcolor = bg
self.selected_fgcolor = fg
self.useOwnColor = True
else:
if bgcolor != [] and fgcolor != []:
self.useOwnColor = True
elif bgcolor != []:
self.useOwnBG = True
elif fgcolor != []:
self.useOwnFG = True
if self.fgcolor!=[]:
self.color = self.fgcolor
2020-04-18 11:49:13 +08:00
self.bind(size=self.onSize_bgcolor_behavior,
pos=self.onSize_bgcolor_behavior)
2020-04-24 18:35:30 +08:00
self.bind(parent=self.useAcestorColor)
2020-04-18 11:49:13 +08:00
self.on_bgcolor()
2020-04-24 18:35:30 +08:00
def useAcestorColor(self,o,v=None):
if self.useOwnColor:
return
p = self.parent
cnt = 0
while p:
if isinstance(p,BGColorBehavior):
break
p = p.parent
cnt += 1
if cnt > 100:
return
if not self.useOwnBG and p:
self.bgcolor = p.bgcolor
self.on_bgcolor()
if not self.useOwnFG and p:
self.fgcolor = p.bgcolor
self.color = self.fgcolor
2020-04-18 11:49:13 +08:00
def onSize_bgcolor_behavior(self,o,v=None):
if not hasattr(self,'rect'):
self.on_bgcolor()
else:
2020-04-24 18:35:30 +08:00
self.rect.pos = self.pos
self.rect.size = self.size
2020-04-02 13:18:10 +08:00
def on_bgcolor(self,o=None,v=None):
2020-04-24 18:35:30 +08:00
if self.bgcolor == []:
return
2020-04-02 13:18:10 +08:00
if self.canvas:
with self.canvas.before:
Color(*self.bgcolor)
2020-04-18 11:49:13 +08:00
self.rect = Rectangle(pos=self.pos,
size=self.size)
2020-04-02 13:18:10 +08:00
2020-04-24 18:35:30 +08:00
def selected(self):
self.bgcolor = self.selected_bgcolor
self.on_bgcolor()
def unselected(self):
self.bgcolor = self.normal_bgcolor
self.on_bgcolor()