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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//
// CoverManager.cs
//
// Author:
//       Mathijs Dumon <mathijsken@hotmail.com>
//
// Copyright (c) 2010 Mathijs Dumon
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


using System;
using System.Collections.Generic;

using ClutterFlow.Alphabet;

using Clutter;
using Gtk;
using GLib;

namespace ClutterFlow
{
    public delegate void ActorEventHandler<T>(T actor, EventArgs e) where T : ClutterFlowBaseActor;

    public class CoverManager : Clutter.Group {

        #region Events
        public event ActorEventHandler<ClutterFlowBaseActor> ActorActivated;
        internal void InvokeActorActivated (ClutterFlowBaseActor cover)
        {
            var handler = ActorActivated;
            if (handler != null) {
                handler (cover, EventArgs.Empty);
            }
        }

        public event ActorEventHandler<ClutterFlowBaseActor> NewCurrentCover;
        protected void InvokeNewCurrentCover (ClutterFlowBaseActor cover)
        {
            var handler = NewCurrentCover;
            if (handler != null) {
                handler (cover, EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> CoversChanged;
        protected void InvokeCoversChanged ()
        {
            var handler = CoversChanged;
            if (handler != null) {
                handler (this, EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> TargetIndexChanged;
        protected void InvokeTargetIndexChanged ()
        {
            var handler = TargetIndexChanged;
            if (handler != null) {
                handler (this, EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> VisibleCoversChanged;
        protected void InvokeVisibleCoversChanged ()
        {
            var handler = VisibleCoversChanged;
            if (handler != null) {
                handler (this, EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> LetterLookupChanged;
        protected void InvokeLetterLookupChanged ()
        {
            var handler = LetterLookupChanged;
            if (handler != null) {
                handler (this, EventArgs.Empty);
            }
        }

        #endregion

        #region Fields
        private static TextureHolder texture_holder;
        public static TextureHolder TextureHolder {
            get { return texture_holder; }
        }

        protected ClutterFlowTimeline timeline;
        public ClutterFlowTimeline Timeline {
            get {
                if (timeline == null) {
                    timeline = new ClutterFlowTimeline(this);
                    timeline.TargetMarkerReached += HandleTargetMarkerReached;
                }
                return timeline;
            }
        }

        protected IActorLoader actor_loader;    //Loads the actors (detaches ClutterFlow from Banshee related dependencies)
        public IActorLoader ActorLoader {
            get { return actor_loader; }
        }

        public Dictionary<AlphabetChars, int> letter_lookup;
        public Dictionary<AlphabetChars, int> LetterLookup {
            get { return letter_lookup; }
        }

        public void ResetLetterLookup () {
            letter_lookup = new Dictionary<AlphabetChars, int>();
            foreach (AlphabetChars key in Enum.GetValues(typeof(AlphabetChars)))
                letter_lookup.Add(key, -1);
        }
        public void UpdateLetterLookup (ClutterFlowBaseActor actor) {
            string label = actor.SortLabel.ToUpper ().Normalize (System.Text.NormalizationForm.FormKD);
            char letter = label.Length>0 ? char.Parse(label.Substring (0,1)) : '?';
            AlphabetChars key;
            if (char.IsLetter(letter))
                key = (AlphabetChars) letter;
            else
                key = AlphabetChars.unknown;
            if (letter_lookup.ContainsKey (key) && letter_lookup[key] == -1)
                letter_lookup[key] = actor.Index;
        }

        protected FlowBehaviour behaviour;
        public FlowBehaviour Behaviour {
            get { return behaviour; }
        }
        #endregion

        #region Cover-related fields

        protected int texture_size;
        public int TextureSize {
            get { return texture_size; }
            set { texture_size = value; }
        }

        protected int visibleCovers = 17;
        public int VisibleCovers {
            get { return visibleCovers; }
            set {
                if (value != visibleCovers) {
                    visibleCovers = value;
                    InvokeVisibleCoversChanged ();
                }
            }
        }
        public int HalfVisCovers {
            get { return (int) ((visibleCovers-1) * 0.5); }
        }
        #endregion

        #region Animation duration limits
        protected static uint maxAnimationSpan = 250; //maximal length of a single step in the animations in ms
        public static uint MaxAnimationSpan {
            get { return maxAnimationSpan; }
        }

        protected static uint minAnimationSpan = 10; //minimal length of a single step in the animations in ms
        public static uint MinAnimationSpan {
            get { return minAnimationSpan; }
        }

        protected static uint doubleClickTime = 200;
        public static uint DoubleClickTime {
            get { return doubleClickTime; }
            set { doubleClickTime = value; }
        }
        #endregion

        #region Target/Current index handling
        private int target_index = 0;            // curent targetted cover index
        public int TargetIndex {
            get { return target_index; }
            set {
                if (value >= TotalCovers) {
                    value = TotalCovers - 1;
                }
                if (value < 0) {
                    value = 0;
                }
                if (value != target_index) {
                    //Console.WriteLine ("TargetIndex_set to " + value);
                    target_index = value;
                    // To prevent clicks to load the old centered cover!
                    current_cover = null;
                    InvokeTargetIndexChanged();
                }
            }
        }

        public ClutterFlowBaseActor TargetActor {
            get {
                if (covers.Count > TargetIndex)
                    return covers[TargetIndex];
                else
                    return null;
            }
        }

        protected ClutterFlowFixedActor empty_actor;
        public ClutterFlowFixedActor EmptyActor {
            get {
                if (empty_actor == null)
                    empty_actor = new ClutterFlowFixedActor ((uint)behaviour.CoverWidth);
                return empty_actor;
            }
        }

        // list with cover actors
        private List<ClutterFlowBaseActor> covers;
        public List<ClutterFlowBaseActor> Covers {
            get { return covers; }
        }

        public int TotalCovers  {
            get { return (covers != null) ? covers.Count : 0; }
        }

        // currently centered cover
        private ClutterFlowBaseActor current_cover = null;
        public ClutterFlowBaseActor CurrentCover {
            get { return current_cover; }
            set {
                if (value != current_cover) {
                    current_cover = value;
                    InvokeNewCurrentCover (current_cover);
                }
            }
        }
        #endregion

        protected bool needs_reloading = false;
        public bool NeedsReloading {
            get { return needs_reloading; }
            internal set { needs_reloading = value; }
        }

        #region Initialisation
        public CoverManager (IActorLoader actor_loader, GetDefaultSurface get_default_surface, int texture_size) : base ()
        {
            this.actor_loader = actor_loader;
            this.texture_size = texture_size;
            behaviour = new FlowBehaviour (this);
            texture_holder = new TextureHolder (texture_size, get_default_surface);
        }

        public override void Dispose ()
        {
            if (reload_timeout > 0) {
                GLib.Source.Remove (reload_timeout);
            }

            behaviour.Dispose ();
            timeline.Dispose ();

            covers.Clear ();
            covers = null;
            current_cover = null;

            base.Dispose ();
        }
        #endregion

        /*new public ClutterFlowChildMeta GetChildMeta (Clutter.Actor actor)
        {
            return base.GetChildMeta(actor) as ClutterFlowChildMeta;
        }*/


        public void UpdateBehaviour ()
        {
            if (behaviour != null && Stage != null) {
                behaviour.Height = Stage.Height;
                behaviour.Width = Stage.Width;
                //Console.WriteLine ("behaviour.CoverWidth = " + behaviour.CoverWidth + "behaviour.Height = " + behaviour.Height + " behaviour.Width = " + behaviour.Width);
            }
        }

        void HandleTargetMarkerReached(object sender, TargetReachedEventArgs args)
        {
            if (args.Target==TargetIndex) {
                if (covers.Count > args.Target) {
                    CurrentCover = covers[(int) args.Target];
                } else {
                    CurrentCover = null;
                }
            }
        }

        private uint reload_timeout = 0;
        public void ReloadCovers ()
        {
            if (reload_timeout > 0) {
                GLib.Source.Remove(reload_timeout);
            }
            reload_timeout = GLib.Timeout.Add (MaxAnimationSpan, new GLib.TimeoutHandler (reload_covers));
        }

         private bool reload_covers ()
         {
            if (Timeline != null) {
                Timeline.Pause ();
            }
            HideAll ();
            Show ();
            if (covers != null && covers.Count != 0) {
                Console.WriteLine("ClutterFlow - Reloading Covers");

                // the old current index
                int old_target_index = CurrentCover!=null ? covers.IndexOf (CurrentCover) : 0;
                // the newly calculated index
                int new_target_index = 0;
                // whether or not to keep the current cover centered
                bool keep_current = false;

                List<ClutterFlowBaseActor> old_covers = new List<ClutterFlowBaseActor> (
                    SafeGetRange (covers, old_target_index - HalfVisCovers - 1, visibleCovers + 2));
                foreach (ClutterFlowBaseActor actor in covers) {
                    if (actor.Data.ContainsKey ("isOldCover")) {
                        actor.Data.Remove ("isOldCover");
                    }
                    actor.Index = -1;
                    if (old_covers.Contains (actor)) {
                        actor.Data.Add ("isOldCover", true);
                    }
                }

                ResetLetterLookup ();
                List<ClutterFlowBaseActor> persistent_covers = new List<ClutterFlowBaseActor>();

                covers = actor_loader.GetActors (this);
                covers.ForEach (delegate (ClutterFlowBaseActor actor) {
                    if (actor.Data.ContainsKey ("isOldCover")) {
                        persistent_covers.Add (actor);
                    }
                    if (CurrentCover==actor) {
                        keep_current = true;
                    }

                    UpdateLetterLookup (actor);
                });
                InvokeLetterLookupChanged ();

                if (covers.Count == 0) {
                    InstallEmptyActor ();
                    if (old_covers.Contains (EmptyActor)) {
                        EmptyActor.Show ();
                        return false;
                    }
                    keep_current = true;
                }

                //recalculate timeline progression and the target index
                if (covers.Count > 1) {
                    if (keep_current) {
                        new_target_index = CurrentCover.Index;
                    } else {
                        if (persistent_covers.Count==0) {
                            new_target_index = (int) Math.Round(Timeline.Progress * (covers.Count-1));
                        } else if (persistent_covers.Count==1) {
                            new_target_index = persistent_covers[0].Index;
                        } else {
                            new_target_index = persistent_covers[(int) (((float) persistent_covers.Count * 0.5f) - 1.0f)].Index;
                        }
                    }
                }
                TargetIndex = new_target_index;
                Timeline.JumpToTarget ();

                //Console.WriteLine ("Timeline progress set to " + Timeline.Progress + " Timeline.RelativeTarget is " + Timeline.RelativeTarget);

                List<ClutterFlowBaseActor> truly_pers = new List<ClutterFlowBaseActor> ();
                List<ClutterFlowBaseActor> new_covers = new List<ClutterFlowBaseActor>(SafeGetRange(covers, new_target_index - HalfVisCovers - 1, visibleCovers + 2));
                foreach (ClutterFlowBaseActor actor in persistent_covers) {
                    if (actor != null) {
                        if (actor.Data.ContainsKey ("isOldCover")) {
                            actor.Data.Remove ("isOldCover");
                        }
                        if (new_covers.Contains (actor)) {
                            truly_pers.Add (actor);
                            new_covers.Remove (actor);
                            old_covers.Remove (actor);
                            actor.Show ();
                        }
                    }
                }
                foreach (ClutterFlowBaseActor actor in old_covers) {
                    if (actor != null) {
                        actor.Data.Remove ("isOldCover");
                    }
                }

                /*Console.WriteLine ("old_covers          contains " + old_covers.Count + " elements:");
                foreach (ClutterFlowBaseActor cover in old_covers)
                    Console.WriteLine("\t- " + cover.Label.Replace("\n", " - "));
                Console.WriteLine ("persistent_covers   contains " + truly_pers.Count + " elements");
                foreach (ClutterFlowBaseActor cover in truly_pers)
                    Console.WriteLine("\t- " + cover.Label.Replace("\n", " - "));
                Console.WriteLine ("new_covers          contains " + new_covers.Count + " elements");
                foreach (ClutterFlowBaseActor cover in new_covers)
                    Console.WriteLine("\t- " + cover.Label.Replace("\n", " - "));*/

                EventHandler update_target = delegate (object o, EventArgs e) {
                    Timeline.Play ();
                    InvokeCoversChanged();
                };

                Behaviour.FadeCoversInAndOut (old_covers, truly_pers, new_covers, update_target);
            } else {
                Console.WriteLine("ClutterFlow - Loading Covers");
                ResetLetterLookup ();
                covers = actor_loader.GetActors (this);
                covers.ForEach (UpdateLetterLookup);
                InvokeLetterLookupChanged ();
                TargetIndex = 0;
                Timeline.JumpToTarget ();
                if (covers==null || covers.Count==0) {
                    InstallEmptyActor ();
                    Behaviour.UpdateActors ();
                    Behaviour.FadeInActor (EmptyActor);
                }
                Timeline.Play ();
                InvokeCoversChanged ();
            }

            return false;
        }

        private void InstallEmptyActor ()
        {
            covers = new List<ClutterFlowBaseActor> ();
            covers.Add (EmptyActor);
            CurrentCover = EmptyActor;
        }

        private IEnumerable<ClutterFlowBaseActor> SafeGetRange (List<ClutterFlowBaseActor> list, int index, int count) {
            for (int i = index; i < index + count; i++) {
                ClutterFlowBaseActor cover = null;
                if (i >= 0 && i < list.Count) {
                    cover = list[i];
                }
                if (cover == null) {
                    continue;
                }
                yield return cover;
            }
            yield break;
        }

        public void ForSomeCovers (System.Action<ClutterFlowBaseActor> method_call, int lBound, int uBound) {
            if (covers != null && lBound <= uBound && lBound >= 0 && uBound < covers.Count) {
                IEnumerator<ClutterFlowBaseActor> enumerator = covers.GetRange(lBound, uBound-lBound + 1).GetEnumerator();
                while (enumerator.MoveNext()) {
                    method_call(enumerator.Current);
                }
            }
        }
    }
}