~ubuntu-branches/debian/jessie/banshee-community-extensions/jessie

« back to all changes in this revision

Viewing changes to src/ClutterFlow/ClutterFlow/ClutterFlowBaseActor.cs

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-09-20 18:45:46 UTC
  • mfrom: (1.2.9 upstream) (5.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20110920184546-3ahue2qplydc4t0e
Tags: 2.2.0-1
* [4940fab] Imported Upstream version 2.2.0
  + Notable bug fixes:
    - Karaoke: Fix crash when switching to Now Playing
    - Lyrics: Fix crash when switching to Now Playing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ClutterFlowBaseActor.cs
 
3
// 
 
4
// Author:
 
5
//       Mathijs Dumon <mathijsken@hotmail.com>
 
6
//
 
7
// Copyright (c) 2010 Mathijs Dumon
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
 
 
29
using Clutter;
 
30
 
 
31
namespace ClutterFlow
 
32
{
 
33
    public interface IIndexable : IComparable<IIndexable>
 
34
    {
 
35
        int Index { get; }
 
36
        event IndexChangedEventHandler IndexChanged;
 
37
    }
 
38
 
 
39
    public delegate void IndexChangedEventHandler (IIndexable item, int old_index, int new_index);
 
40
 
 
41
    public abstract class ClutterFlowBaseActor : Clutter.Group, IIndexable
 
42
    {
 
43
        #region Fields
 
44
        private CoverManager cover_manager;
 
45
        public CoverManager CoverManager {
 
46
            get { return cover_manager; }
 
47
            set { cover_manager = value ; }
 
48
        }
 
49
 
 
50
        protected string cache_key = "";
 
51
        public virtual string CacheKey {
 
52
            get { return cache_key; }
 
53
        }
 
54
 
 
55
        protected string label = "";
 
56
        public virtual string Label {
 
57
            get { return label; }
 
58
        }
 
59
 
 
60
        protected string sort_label = "";
 
61
        public virtual string SortLabel {
 
62
            get { return sort_label; }
 
63
            set { sort_label = value; }
 
64
        }
 
65
 
 
66
        public virtual event IndexChangedEventHandler IndexChanged;
 
67
 
 
68
        protected int index = -1; //-1 = not visible
 
69
        public virtual int Index {
 
70
            get { return index; }
 
71
            set {
 
72
                if (value != index) {
 
73
                    int old_index = index;
 
74
                    index = value;
 
75
                    IsReactive = !(index < 0);
 
76
                    if (IndexChanged != null) {
 
77
                        IndexChanged (this, old_index, index);
 
78
                    }
 
79
                }
 
80
            }
 
81
        }
 
82
 
 
83
        public virtual int CompareTo (IIndexable obj) {
 
84
            if (obj.Index == -1 && this.Index != -1) {
 
85
                return 1;
 
86
            }
 
87
            return obj.Index - this.Index;
 
88
        }
 
89
        #endregion
 
90
 
 
91
        public ClutterFlowBaseActor () : base ()
 
92
        { }
 
93
 
 
94
        public ClutterFlowBaseActor (CoverManager cover_manager) : base ()
 
95
        {
 
96
            this.cover_manager = cover_manager;
 
97
            this.cover_manager.Add (this);
 
98
        }
 
99
 
 
100
        private bool disposed = false;
 
101
        public override void Dispose ()
 
102
        {
 
103
            if (disposed) {
 
104
                return;
 
105
            }
 
106
            disposed = true;
 
107
 
 
108
            cover_manager.Remove (this);
 
109
 
 
110
            base.Dispose ();
 
111
        }
 
112
 
 
113
        #region Texture Handling
 
114
        public static void MakeReflection (Cairo.ImageSurface source, CairoTexture dest)
 
115
        {
 
116
            int w = source.Width + 4;
 
117
            int h = source.Height * 2 + 4;
 
118
 
 
119
            dest.SetSurfaceSize ((uint) w, (uint) h);
 
120
 
 
121
            Cairo.Context context = dest.Create ();
 
122
 
 
123
            MakeReflection (context, source,  w, h);
 
124
 
 
125
            //((IDisposable) context.Target).Dispose ();
 
126
            ((IDisposable) context).Dispose ();
 
127
        }
 
128
 
 
129
        public static Cairo.ImageSurface MakeReflection (Cairo.ImageSurface source)
 
130
        {
 
131
            int w = source.Width + 4;
 
132
            int h = source.Height * 2 + 4;
 
133
 
 
134
            Cairo.ImageSurface dest = new Cairo.ImageSurface(Cairo.Format.ARGB32, w, h);
 
135
            Cairo.Context context = new Cairo.Context(dest);
 
136
 
 
137
            MakeReflection (context, source, w, h);
 
138
 
 
139
            //((IDisposable) context.Target).Dispose ();
 
140
            ((IDisposable) context).Dispose ();
 
141
 
 
142
            return dest;
 
143
        }
 
144
 
 
145
        private static void MakeReflection (Cairo.Context context, Cairo.ImageSurface source, int w, int h)
 
146
        {
 
147
            context.ResetClip ();
 
148
            context.SetSourceSurface (source, 2, 2);
 
149
            context.Paint ();
 
150
 
 
151
            double alpha = -0.3;
 
152
            double step = 1.0 / (double) source.Height;
 
153
 
 
154
            context.Translate (0, h);
 
155
            context.Scale (1, -1);
 
156
            context.SetSourceSurface (source, 2, 2);
 
157
            for (int i = 0; i < source.Height; i++) {
 
158
                context.Rectangle (0, i+2, w, 1);
 
159
                context.Clip ();
 
160
                alpha += step;
 
161
                context.PaintWithAlpha (Math.Max (Math.Min (alpha, 0.7), 0.0));
 
162
                context.ResetClip ();
 
163
            }
 
164
        }
 
165
        #endregion
 
166
    }
 
167
}