From 522c520fe2f5ac9f4c7f7bfdca84d2e8b0be96ef Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 19 Sep 2012 01:06:08 +0200 Subject: [PATCH 1/5] graphics/widget: introduce opacity attribute on the canvas and widget. You can use the opacity attribute to control the opacity of the widget and its children. The attribute is cumulative, and multiplied to the current context color. By default, the opacity is 1.0, and doesn't act at all. --- kivy/data/glsl/default.vs | 2 +- kivy/data/glsl/header.vs | 1 + kivy/graphics/instructions.pxd | 2 ++ kivy/graphics/instructions.pyx | 25 ++++++++++++++++++++++++- kivy/uix/widget.py | 24 ++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/kivy/data/glsl/default.vs b/kivy/data/glsl/default.vs index 62f2f1690..ac9ac4d6d 100644 --- a/kivy/data/glsl/default.vs +++ b/kivy/data/glsl/default.vs @@ -1,6 +1,6 @@ $HEADER$ void main (void) { - frag_color = color; + frag_color = color * vec4(1.0, 1.0, 1.0, opacity); tex_coord0 = vTexCoords0; gl_Position = projection_mat * modelview_mat * vec4(vPosition.xy, 0.0, 1.0); } diff --git a/kivy/data/glsl/header.vs b/kivy/data/glsl/header.vs index ef00fcf35..a2638bffc 100644 --- a/kivy/data/glsl/header.vs +++ b/kivy/data/glsl/header.vs @@ -14,3 +14,4 @@ attribute vec2 vTexCoords0; uniform mat4 modelview_mat; uniform mat4 projection_mat; uniform vec4 color; +uniform float opacity; diff --git a/kivy/graphics/instructions.pxd b/kivy/graphics/instructions.pxd index 1bda49dfe..fe90ae929 100644 --- a/kivy/graphics/instructions.pxd +++ b/kivy/graphics/instructions.pxd @@ -82,6 +82,7 @@ cdef class CanvasBase(InstructionGroup): cdef class Canvas(CanvasBase): cdef object __weakref__ + cdef float _opacity cdef CanvasBase _before cdef CanvasBase _after cdef void reload(self) @@ -89,6 +90,7 @@ cdef class Canvas(CanvasBase): cpdef add(self, Instruction c) cpdef remove(self, Instruction c) cpdef draw(self) + cdef void apply(self) cdef class RenderContext(Canvas): diff --git a/kivy/graphics/instructions.pyx b/kivy/graphics/instructions.pyx index acaacc0e6..090505ac7 100644 --- a/kivy/graphics/instructions.pyx +++ b/kivy/graphics/instructions.pyx @@ -499,6 +499,7 @@ cdef class Canvas(CanvasBase): def __init__(self, **kwargs): get_context().register_canvas(self) CanvasBase.__init__(self, **kwargs) + self._opacity = kwargs.get('opacity', 1.0) self._before = None self._after = None @@ -530,6 +531,19 @@ cdef class Canvas(CanvasBase): ''' self.apply() + cdef void apply(self): + cdef float opacity = self._opacity + cdef float rc_opacity + cdef RenderContext rc + if opacity != 1.0: + rc = getActiveContext() + rc_opacity = rc['opacity'] + rc.push_state('opacity') + rc['opacity'] = rc_opacity * opacity + InstructionGroup.apply(self) + if opacity != 1.0: + rc.pop_state('opacity') + cpdef add(self, Instruction c): # the after group must remain the last one. if self._after is None: @@ -569,6 +583,15 @@ cdef class Canvas(CanvasBase): self._after = c return self._after + property opacity: + '''Property for getting the opacity value + ''' + def __get__(self): + return self._opacity + def __set__(self, value): + self._opacity = value + self.flag_update() + # Active Canvas and getActiveCanvas function is used # by instructions, so they know which canvas to add # tehmselves to @@ -629,8 +652,8 @@ cdef class RenderContext(Canvas): self.default_texture = tex self.state_stacks = { + 'opacity': [1.0], 'texture0' : [0], - 'linewidth': [1.0], 'color' : [[1.0,1.0,1.0,1.0]], 'projection_mat': [Matrix()], 'modelview_mat' : [Matrix()], diff --git a/kivy/uix/widget.py b/kivy/uix/widget.py index bb4a582ea..43755a65f 100644 --- a/kivy/uix/widget.py +++ b/kivy/uix/widget.py @@ -532,6 +532,30 @@ class Widget(EventDispatcher): dict. ''' + opacity = NumericProperty(1.0) + '''Opacity of the widget and all the children. + + .. versionadded:: 1.4.1 + + The opacity attribute control the opacity of the widget and the children. + Take care, it's a cumulative attribute: the value is multiplied to the + current global opacity, and the result is applied to the current context + color. + + For example: if your parent have an opacity of 0.5, and one children have an + opacity of 0.2, the real opacity of the children will be 0.5 * 0.2 = 0.1. + + Then, the opacity is applied on the shader as:: + + frag_color = color * vec4(1.0, 1.0, 1.0, opacity); + + :data:`opacity` is a :class:`~kivy.properties.NumericProperty`, default to + 1.0. + ''' + + def on_opacity(self, instance, value): + self.canvas.opacity = value + canvas = None '''Canvas of the widget. From c69b2ff20010e90754a3e66db53d7384bfc0aa2e Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 19 Sep 2012 01:23:19 +0200 Subject: [PATCH 2/5] doc fixes --- kivy/graphics/instructions.pyx | 17 ++++++++++++++++- kivy/uix/widget.py | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/kivy/graphics/instructions.pyx b/kivy/graphics/instructions.pyx index 090505ac7..10e8ca3ba 100644 --- a/kivy/graphics/instructions.pyx +++ b/kivy/graphics/instructions.pyx @@ -584,7 +584,22 @@ cdef class Canvas(CanvasBase): return self._after property opacity: - '''Property for getting the opacity value + '''Property for get/set the opacity value of the canvas. + + .. versionadded:: 1.4.1 + + The opacity attribute controls the opacity of the canvas and its + children. Be careful, it's a cumulative attribute: the value is + multiplied to the current global opacity, and the result is applied to + the current context color. + + For example: if your parent have an opacity of 0.5, and one children have an + opacity of 0.2, the real opacity of the children will be 0.5 * 0.2 = 0.1. + + Then, the opacity is applied on the shader as:: + + frag_color = color * vec4(1.0, 1.0, 1.0, opacity); + ''' def __get__(self): return self._opacity diff --git a/kivy/uix/widget.py b/kivy/uix/widget.py index 43755a65f..f06dd326f 100644 --- a/kivy/uix/widget.py +++ b/kivy/uix/widget.py @@ -537,8 +537,8 @@ class Widget(EventDispatcher): .. versionadded:: 1.4.1 - The opacity attribute control the opacity of the widget and the children. - Take care, it's a cumulative attribute: the value is multiplied to the + The opacity attribute controls the opacity of the widget and its children. + Be careful, it's a cumulative attribute: the value is multiplied to the current global opacity, and the result is applied to the current context color. From 5631a3efb5bd198f3bda7a7cc04ea30826186985 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 19 Sep 2012 04:45:22 +0200 Subject: [PATCH 3/5] window/pygame: fix key translations. closes #680 --- kivy/core/window/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kivy/core/window/__init__.py b/kivy/core/window/__init__.py index 0ad21777d..0fa45b4a2 100755 --- a/kivy/core/window/__init__.py +++ b/kivy/core/window/__init__.py @@ -59,10 +59,10 @@ class Keyboard(EventDispatcher): 'screenlock': 145, 'pause': 19, # a-z keys - 'q': 97, 'b': 98, 'c': 99, 'q': 100, 'e': 101, 'f': 102, 'g': 103, + 'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104, 'i': 105, 'j': 106, 'k': 107, 'l': 108, 'm': 109, 'n': 110, - 'o': 111, 'p': 112, 'a': 113, 'r': 114, 's': 115, 't': 116, 'u': 117, - 'v': 118, 'z': 119, 'x': 120, 'y': 121, 'w': 122, + 'o': 111, 'p': 112, 'q': 113, 'r': 114, 's': 115, 't': 116, 'u': 117, + 'v': 118, 'w': 119, 'x': 120, 'y': 121, 'z': 122, # 0-9 keys '0': 48, '1': 49, '2': 50, '3': 51, '4': 52, From b0b172aeaa937acdff05e720eb78f1dafd10181b Mon Sep 17 00:00:00 2001 From: Qua-non Date: Wed, 19 Sep 2012 15:10:19 +0530 Subject: [PATCH 4/5] TextInput: introduce background_normal and background_active properties used to reffer to the background images depending on state --- kivy/data/style.kv | 4 ++-- kivy/uix/textinput.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/kivy/data/style.kv b/kivy/data/style.kv index 86af49c1f..2d4d773b4 100644 --- a/kivy/data/style.kv +++ b/kivy/data/style.kv @@ -119,10 +119,10 @@ Color: rgba: self.background_color BorderImage: - border: (16, 16, 16, 16) + border: self.border pos: self.pos size: self.size - source: 'atlas://data/images/defaulttheme/textinput%s' % ('_active' if self.focus else '') + source: self.background_active if self.focus else self.background_normal Color: rgba: (1, 0, 0, 1 if self.focus and not self.cursor_blink else 0) Rectangle: diff --git a/kivy/uix/textinput.py b/kivy/uix/textinput.py index 3839076c3..49cb41794 100644 --- a/kivy/uix/textinput.py +++ b/kivy/uix/textinput.py @@ -1472,6 +1472,38 @@ class TextInput(Widget): to [0.1843, 0.6549, 0.8313, .5] ''' + border = ListProperty([16, 16, 16, 16]) + '''Border used for :class:`~kivy.graphics.vertex_instructions.BorderImage` + graphics instruction. Used with :data:`background_normal` and + :data:`background_active`. Can be used for a custom background. + + It must be a list of four values: (top, right, bottom, left). Read the + BorderImage instruction for more information about how to use it. + + :data:`border` is a :class:`~kivy.properties.ListProperty`, default to (16, + 16, 16, 16) + ''' + + background_normal = StringProperty( + 'atlas://data/images/defaulttheme/textinput') + '''Background image of the TextInput when it's not in focus'. + + .. versionadded:: 1.4.1 + + :data:`background_normal` is a :class:`~kivy.properties.StringProperty`, + default to 'atlas://data/images/defaulttheme/textinput' + ''' + + background_active = StringProperty( + 'atlas://data/images/defaulttheme/textinput_active') + '''Background image of the TextInput when it's in focus'. + + .. versionadded:: 1.4.1 + + :data:`background_active` is a :class:`~kivy.properties.StringProperty`, + default to 'atlas://data/images/defaulttheme/textinput_active' + ''' + background_color = ListProperty([1, 1, 1, 1]) '''Current color of the background, in (r, g, b, a) format. From ae70d9d10ecfc4be6527a61e2a7f54d7be8bd82a Mon Sep 17 00:00:00 2001 From: Qua-non Date: Wed, 19 Sep 2012 15:21:55 +0530 Subject: [PATCH 5/5] Textinput: add versionadded info for `border` instruction --- kivy/uix/textinput.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kivy/uix/textinput.py b/kivy/uix/textinput.py index 49cb41794..31e634e9d 100644 --- a/kivy/uix/textinput.py +++ b/kivy/uix/textinput.py @@ -1477,6 +1477,8 @@ class TextInput(Widget): graphics instruction. Used with :data:`background_normal` and :data:`background_active`. Can be used for a custom background. + .. versionadded:: 1.4.1 + It must be a list of four values: (top, right, bottom, left). Read the BorderImage instruction for more information about how to use it.