[longomatch/newui: 14/157] Start with players tagging widget rededign



commit 85dcdfc74cdef6d007528acd0a05598d68cf47d2
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Mon Aug 18 19:19:04 2014 +0200

    Start with players tagging widget rededign

 LongoMatch.Drawing.Cairo/CairoBackend.cs           |    7 +-
 LongoMatch.Drawing/CanvasObject/BenchObject.cs     |  126 ++++++++++
 LongoMatch.Drawing/CanvasObject/FieldObject.cs     |  174 +++++++++++++
 LongoMatch.Drawing/CanvasObject/PlayerObject.cs    |  129 +++++++----
 .../CanvasObject/PlayersTaggerObject.cs            |  202 +++++++++++++++
 LongoMatch.Drawing/LongoMatch.Drawing.mdp          |    3 +
 LongoMatch.Drawing/Makefile.am                     |    3 +
 LongoMatch.Drawing/Utils.cs                        |    7 +
 LongoMatch.Drawing/Widgets/TeamTagger.cs           |  256 +-------------------
 9 files changed, 610 insertions(+), 297 deletions(-)
---
diff --git a/LongoMatch.Drawing.Cairo/CairoBackend.cs b/LongoMatch.Drawing.Cairo/CairoBackend.cs
index 5bcccfb..e28bde2 100644
--- a/LongoMatch.Drawing.Cairo/CairoBackend.cs
+++ b/LongoMatch.Drawing.Cairo/CairoBackend.cs
@@ -235,8 +235,7 @@ namespace LongoMatch.Drawing.Cairo
 
                public void DrawRectangle (Point start, double width, double height)
                {
-                       CContext.Rectangle (start.X, start.Y, width, height);
-                       StrokeAndFill ();
+                       DrawRoundedRectangle (start, width, height, 0);
                }
 
                static public double ByteToDouble (byte val)
@@ -280,8 +279,8 @@ namespace LongoMatch.Drawing.Cairo
                        
                        x = start.X + LineWidth / 2;
                        y = start.Y + LineWidth / 2;
-                       height -= LineWidth / 2;
-                       width -= LineWidth / 2;
+                       height -= LineWidth;
+                       width -= LineWidth;
 
                        if ((radius > height / 2) || (radius > width / 2))
                                radius = Math.Min (height / 2, width / 2);
diff --git a/LongoMatch.Drawing/CanvasObject/BenchObject.cs b/LongoMatch.Drawing/CanvasObject/BenchObject.cs
new file mode 100644
index 0000000..6f2ea21
--- /dev/null
+++ b/LongoMatch.Drawing/CanvasObject/BenchObject.cs
@@ -0,0 +1,126 @@
+//
+//  Copyright (C) 2014 Andoni Morales Alastruey
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using LongoMatch.Interfaces.Drawing;
+using LongoMatch.Common;
+using System.Collections.Generic;
+using LongoMatch.Store.Drawables;
+
+namespace LongoMatch.Drawing.CanvasObject
+{
+       public class BenchObject: CanvasObject, ICanvasSelectableObject
+       {
+               public BenchObject ()
+               {
+                       BenchPlayers = new List<PlayerObject> ();
+               }
+
+               public List<PlayerObject> BenchPlayers {
+                       get;
+                       set;
+               }
+
+               public Point Position {
+                       get;
+                       set;
+               }
+
+               public double Width {
+                       get;
+                       set;
+               }
+
+               public double Height {
+                       get;
+                       set;
+               }
+
+               public int PlayersPerRow {
+                       get;
+                       set;
+               }
+
+               public int PlayersSize {
+                       get;
+                       set;
+               }
+               
+               public void Update ()
+               {
+                       if (BenchPlayers == null) {
+                               return;
+                       }
+                       for (int i = 0; i < BenchPlayers.Count; i++) {
+                               PlayerObject po;
+                               double x, y;
+                               double s = Width / PlayersPerRow;
+                               
+                               x = s * (i % PlayersPerRow) + s / 2;
+                               y = s * (i / PlayersPerRow) + s / 2;
+
+                               po = BenchPlayers [i];
+                               po.Position = new Point (x, y);
+                               po.Size = PlayersSize;
+                       }
+               }
+
+               public override void Draw (IDrawingToolkit tk, Area area)
+               {
+                       if (BenchPlayers == null) {
+                               return;
+                       }
+                       tk.Begin ();
+                       tk.TranslateAndScale (Position, new Point (1, 1)); 
+                       tk.LineStyle = LineStyle.Dashed;
+                       tk.LineWidth = Config.Style.BenchLineWidth;
+                       tk.StrokeColor = Config.Style.PaletteActive;
+                       tk.FillColor = null;
+                       tk.DrawRectangle (new Point (0, 0), Width, Height);
+                       tk.LineStyle = LineStyle.Normal;
+
+                       foreach (PlayerObject po in BenchPlayers) {
+                               po.Draw (tk, area);
+                       }
+                       
+                       tk.End ();
+               }
+
+               public Selection GetSelection (Point point, double precision)
+               {
+                       Selection selection = null;
+
+                       if (BenchPlayers == null) {
+                               return selection;
+                       }
+                       
+                       point = Utils.ToUserCoords (point, Position, 1, 1);
+                       
+                       foreach (PlayerObject po in BenchPlayers) {
+                               selection = po.GetSelection (point, precision);
+                               if (selection != null)
+                                       break;
+                       }
+                       return selection;
+               }
+
+               public void Move (Selection s, Point p, Point start)
+               {
+               }
+       }
+}
+
diff --git a/LongoMatch.Drawing/CanvasObject/FieldObject.cs b/LongoMatch.Drawing/CanvasObject/FieldObject.cs
new file mode 100644
index 0000000..bf51bc0
--- /dev/null
+++ b/LongoMatch.Drawing/CanvasObject/FieldObject.cs
@@ -0,0 +1,174 @@
+//
+//  Copyright (C) 2014 Andoni Morales Alastruey
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using System.Linq;
+using LongoMatch.Interfaces.Drawing;
+using System.Collections.Generic;
+using LongoMatch.Common;
+using LongoMatch.Store.Drawables;
+
+namespace LongoMatch.Drawing.CanvasObject
+{
+       public class FieldObject: CanvasObject, ICanvasSelectableObject
+       {
+               int[] homeFormation;
+               int[] awayFormation;
+               List<PlayerObject> homePlayingPlayers;
+               List<PlayerObject> awayPlayingPlayers;
+               int playerSize;
+               Image background;
+
+               public FieldObject ()
+               {
+                       Position = new Point (0, 0);
+               }
+
+               public int Width {
+                       get;
+                       set;
+               }
+
+               public int Height {
+                       get;
+                       set;
+               }
+
+               public Point Position {
+                       get;
+                       set;
+               }
+
+               public void LoadTeams (Image backgroundImg, int[] homeF, int[] awayF,
+                                      List<PlayerObject> homeT, List<PlayerObject> awayT,
+                                      int size, int nteams)
+               {
+                       background = backgroundImg;
+                       homeFormation = homeF;
+                       awayFormation = awayF;
+                       homePlayingPlayers = homeT;
+                       awayPlayingPlayers = awayT;
+                       playerSize = size;
+                       NTeams = nteams;
+                       Update ();
+               }
+
+               public void Update ()
+               {
+                       if (homeFormation != null) {
+                               UpdateTeam (homePlayingPlayers, homeFormation, Team.LOCAL);
+                       }
+                       if (awayFormation != null) {
+                               UpdateTeam (awayPlayingPlayers, awayFormation, Team.VISITOR);
+                       }
+               }
+
+               public int NTeams {
+                       get;
+                       set;
+               }
+
+               void UpdateTeam (List<PlayerObject> players, int[] formation, Team team)
+               {
+                       int index = 0, offsetX;
+                       int width, colWidth;
+                       Color color;
+
+                       width = Width / NTeams;
+                       colWidth = width / formation.Length;
+                       if (team == Team.LOCAL) {
+                               color = Config.Style.HomeTeamColor;
+                               offsetX = 0;
+                       } else {
+                               color = Config.Style.AwayTeamColor; 
+                               offsetX = Width;
+                       }
+
+                       /* Columns */
+                       for (int col=0; col < formation.Length; col ++) {
+                               double colX, rowHeight;
+                               
+                               if (players.Count == index)
+                                       break;
+
+                               if (team == Team.LOCAL) {
+                                       colX = offsetX + colWidth * col + colWidth / 2;
+                               } else {
+                                       colX = offsetX - colWidth * col - colWidth / 2;
+                               }
+                               rowHeight = Height / formation [col];
+
+                               for (int row=0; row < formation[col]; row ++) {
+                                       PlayerObject po = players [index];
+                                       po.Position = new Point (colX, rowHeight * row + rowHeight / 2); 
+                                       po.Size = playerSize;
+                                       index ++;
+                                       if (players.Count == index)
+                                               break;
+                               }
+                       }
+               }
+
+               public override void Draw (IDrawingToolkit tk, Area area)
+               {
+                       tk.Begin ();
+                       tk.TranslateAndScale (Position, new Point (1, 1));
+                       if (background != null) {
+                               tk.DrawImage (background);
+                       }
+                       if (homePlayingPlayers != null) {
+                               foreach (PlayerObject po in homePlayingPlayers) {
+                                       po.Draw (tk, area);
+                               }
+                       }
+                       if (awayPlayingPlayers != null) {
+                               foreach (PlayerObject po in awayPlayingPlayers) {
+                                       po.Draw (tk, area);
+                               }
+                       }
+                       tk.End ();
+               }
+
+               public Selection GetSelection (Point point, double precision)
+               {
+                       Selection selection = null;
+
+                       point = Utils.ToUserCoords (point, Position, 1, 1);
+
+                       if (homePlayingPlayers != null) {
+                               foreach (PlayerObject po in homePlayingPlayers) {
+                                       selection = po.GetSelection (point, precision);
+                                       if (selection != null)
+                                               break;
+                               }
+                       }
+                       if (selection == null && awayPlayingPlayers != null) {
+                               foreach (PlayerObject po in awayPlayingPlayers) {
+                                       selection = po.GetSelection (point, precision);
+                                       if (selection != null)
+                                               break;
+                               }
+                       }
+                       return selection;
+               }
+
+               public void Move (Selection s, Point p, Point start)
+               {
+               }
+       }
+}
+
diff --git a/LongoMatch.Drawing/CanvasObject/PlayerObject.cs b/LongoMatch.Drawing/CanvasObject/PlayerObject.cs
index 6e201c8..9f12bd2 100644
--- a/LongoMatch.Drawing/CanvasObject/PlayerObject.cs
+++ b/LongoMatch.Drawing/CanvasObject/PlayerObject.cs
@@ -27,19 +27,30 @@ namespace LongoMatch.Drawing.CanvasObject
 {
        public class PlayerObject: CanvasObject, ICanvasSelectableObject
        {
-               public PlayerObject (Player player, Point position)
+               public PlayerObject ()
+               {
+                       Init ();
+               }
+               
+               public PlayerObject (Player player, Point position = null)
                {
                        Player = player;
-                       Position = position;
+                       Init (position);
+               }
+               
+               void Init (Point pos = null) {
+                       if (pos == null) {
+                               pos = new Point (0, 0);
+                       }
+                       Position = pos;
                        DrawPhoto = true;
-                       SelectedColor = Constants.PLAYER_SELECTED_COLOR;
-                       UnSelectedColor = Constants.PLAYER_UNSELECTED_COLOR;
-                       IconSize = PlayersIconSize.Medium;
+                       Color = Constants.PLAYER_SELECTED_COLOR;
+                       Size = (int)PlayersIconSize.Medium;
                }
 
                public Player Player {
                        get;
-                       protected set;
+                       set;
                }
 
                public Point Position {
@@ -47,7 +58,7 @@ namespace LongoMatch.Drawing.CanvasObject
                        set;
                }
 
-               public PlayersIconSize IconSize {
+               public int Size {
                        set;
                        get;
                }
@@ -57,25 +68,20 @@ namespace LongoMatch.Drawing.CanvasObject
                        set;
                }
 
-               public Color SelectedColor {
-                       get;
-                       set;
-               }
-
-               public Color UnSelectedColor {
+               public Color Color {
                        get;
                        set;
                }
 
                int Width {
                        get {
-                               return (int)IconSize;
+                               return Size;
                        }
                }
 
                int Height {
                        get {
-                               return (int)IconSize;
+                               return Size;
                        }
                }
 
@@ -97,46 +103,75 @@ namespace LongoMatch.Drawing.CanvasObject
 
                public override void Draw (IDrawingToolkit tk, Area area)
                {
-                       Color background, line;
-                       Point position = new Point (Position.X - Width / 2, Position.Y - Height / 2);
+                       Point zero, p;
+                       double numberWidth, numberHeight;
+                       double size, scale;
+
+                       if (Position == null) {
+                               Console.WriteLine (Player.Name + Player.Number);
+                               return;
+                       }
                        
-                       tk.Begin ();
+                       zero = new Point (0, 0);
+                       size = Config.Style.PlayerSize;
+                       scale = Width / size; 
                        
+                       tk.Begin ();
+                       tk.TranslateAndScale (Position - new Point (Size / 2, Size / 2),
+                                             new Point (scale, scale));
+
                        /* Background */
-                       if (Selected) {
-                               background = SelectedColor;
-                               line = SelectedColor;
+                       tk.LineStyle = LineStyle.Normal;
+                       tk.LineWidth = Config.Style.PlayerBorder;
+                       tk.FillColor = Config.Style.PaletteBackgroundDark;
+                       tk.StrokeColor = Config.Style.PaletteBackgroundDark;
+                       tk.DrawRoundedRectangle (zero, size, size, Config.Style.PlayerRadius);
+                       
+                       if (!DrawPhoto || Player.Photo == null) {
+                               numberHeight = size;
+                               numberWidth = Config.Style.PlayerNumberWidth;
+                               p = new Point (Config.Style.PlayerNumberOffset, 0);
                        } else {
-                               background = UnSelectedColor;
-                               line = UnSelectedColor;
+                               /* Image */
+                               tk.DrawImage (zero, size, size, Player.Photo, true);
+                               numberHeight = Config.Style.PlayerNumberHeight;
+                               numberWidth = Config.Style.PlayerNumberWidth;
+                               p = new Point (Config.Style.PlayerNumberOffset, size - numberHeight);
                        }
-                       tk.StrokeColor = line;
-                       tk.FillColor = background;
-                       tk.LineWidth = 5;
-                       tk.DrawRoundedRectangle (position, Width, Height, 5);
                        
-                       if (!DrawPhoto || Player.Photo == null || IconSize < PlayersIconSize.Medium) {
-                               tk.FillColor = Color.White;
-                               tk.StrokeColor = Color.White;
-                               tk.FontSize = Width / 2;
-                               tk.FontWeight = FontWeight.Bold;
-                               /* Only draw player number for the smaller size */
-                               if (IconSize > PlayersIconSize.Small) {
-                                       tk.DrawText (position, Width, Height - 20, Player.Number.ToString ());
-                                       tk.FontSize = 8;
-                                       tk.DrawText (new Point (position.X, position.Y + Height - 20), Width, 
20, Player.Name);
-                               } else {
-                                       tk.DrawText (position, Width, Height, Player.Number.ToString ());
-                               }
+                       /* Draw background */
+                       tk.FillColor = Color;
+                       tk.StrokeColor = Color;
+                       tk.LineWidth = 0;
+                       tk.DrawRoundedRectangle (p, numberWidth, numberHeight, Config.Style.PlayerRadius);
+                               
+                       /* Draw bottom Line */
+                       tk.StrokeColor = Color;
+                       tk.FillColor = Color;
+                       tk.LineWidth = Config.Style.PlayerBorder;
+                       tk.DrawRoundedRectangle (new Point (0, size - Config.Style.PlayerTeamLineWidth),
+                                                size, Config.Style.PlayerTeamLineWidth + 1, 2);
+                       
+                       /* Draw number */
+                       tk.FillColor = Color.White;
+                       tk.StrokeColor = Color.White;
+                       tk.FontWeight = FontWeight.Normal;
+                       if (Player.Number >= 100) {
+                               tk.FontSize = (int)(size / 4);
                        } else {
-                               tk.FillColor = Color.Black;
-                               tk.StrokeColor = Color.Black;
-                               tk.DrawImage (position, Width, Height, Player.Photo, true);
-                               tk.FontSize = 16;
-                               tk.FontWeight = FontWeight.Bold;
-                               tk.DrawText (new Point (position.X, position.Y + Height - 20), Width, 20, 
Player.Number.ToString ());
+                               tk.FontSize = (int)(size / 3);
                        }
-
+                       tk.DrawText (p, numberWidth, numberHeight, Player.Number.ToString ());
+                       
+                       /* Selection line */
+                       if (Selected) {
+                               tk.LineStyle = LineStyle.Normal;
+                               tk.LineWidth = Config.Style.PlayerBorder;
+                               tk.FillColor = null;
+                               tk.StrokeColor = Config.Style.PaletteActive;
+                               tk.DrawRoundedRectangle (zero, size + 1, size + 1, Config.Style.PlayerRadius);
+                       }
+                       
                        tk.End ();
                }
        }
diff --git a/LongoMatch.Drawing/CanvasObject/PlayersTaggerObject.cs 
b/LongoMatch.Drawing/CanvasObject/PlayersTaggerObject.cs
new file mode 100644
index 0000000..2813332
--- /dev/null
+++ b/LongoMatch.Drawing/CanvasObject/PlayersTaggerObject.cs
@@ -0,0 +1,202 @@
+//
+//  Copyright (C) 2014 Andoni Morales Alastruey
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using System.Linq;
+using LongoMatch.Interfaces.Drawing;
+using LongoMatch.Store.Templates;
+using System.Collections.Generic;
+using LongoMatch.Common;
+using LongoMatch.Store.Drawables;
+using LongoMatch.Store;
+
+namespace LongoMatch.Drawing.CanvasObject
+{
+       public class PlayersTaggerObject: CanvasObject, ICanvasSelectableObject
+       {
+
+               TeamTemplate homeTeam, awayTeam;
+               List<PlayerObject> homePlayingPlayers, awayPlayingPlayers;
+               List<PlayerObject> homeBenchPlayers, awayBenchPlayers;
+               BenchObject homeBench, awayBench;
+               Image background;
+               FieldObject field;
+               int NTeams;
+               Point offset;
+               double scaleX, scaleY;
+
+               public PlayersTaggerObject ()
+               {
+                       Position = new Point (0, 0);
+                       homeBench = new BenchObject ();
+                       awayBench = new BenchObject ();
+                       field = new FieldObject ();
+               }
+               
+               public Point Position {
+                       get;
+                       set;
+               }
+               
+               public double Width {
+                       get;
+                       set;
+               }
+               
+               public double Height {
+                       get;
+                       set;
+               }
+               
+               public void Update ()
+               {
+                       homeBench.Update ();
+                       awayBench.Update ();
+                       field.Update ();
+               }
+
+               int ColumnSize {
+                       get {
+                               int width, optWidth, optHeight, count = 0, max = 0;
+
+                               width = field.Width / NTeams;
+                               if (homeTeam != null && awayTeam != null) {
+                                       count = Math.Max (homeTeam.Formation.Count (),
+                                                         awayTeam.Formation.Count ());
+                                       max = Math.Max (homeTeam.Formation.Max (),
+                                                       awayTeam.Formation.Max ());
+                               } else if (homeTeam != null) {
+                                       count = homeTeam.Formation.Count ();
+                                       max = homeTeam.Formation.Max ();
+                               } else if (awayTeam != null) {
+                                       count = awayTeam.Formation.Count ();
+                                       max = awayTeam.Formation.Max ();
+                               }
+                               optWidth = width / count;
+                               optHeight = field.Height / max;
+                               return Math.Min (optWidth, optHeight);
+                       }
+               }
+
+               List<PlayerObject> GetPlayers (List<Player> players, Team team)
+               {
+                       Color color = null;
+
+                       if (team == Team.LOCAL) {
+                               color = Config.Style.HomeTeamColor;
+                       } else {
+                               color = Config.Style.AwayTeamColor;
+                       }
+
+                       return players.Select (p => new PlayerObject {Player = p, Color = color}).ToList();
+               }
+
+               public void LoadTeams (TeamTemplate homeTeam, TeamTemplate awayTeam, Image background)
+               {
+                       int[] homeF = null, awayF = null;
+                       int playerSize, colSize, widgetSize, border;
+                       double width, height;
+
+                       this.homeTeam = homeTeam;
+                       this.awayTeam = awayTeam;
+                       this.background = background;
+                       NTeams = 0;
+
+                       if (background != null) {
+                               field.Height = background.Height;
+                               field.Width = background.Width;
+                       } else {
+                               field.Width = 300;
+                               field.Height = 250;
+                       }
+                       homePlayingPlayers = awayPlayingPlayers = null;
+
+                       if (homeTeam != null) {
+                               homePlayingPlayers = GetPlayers (homeTeam.StartingPlayersList, Team.LOCAL);
+                               homeBenchPlayers = GetPlayers (homeTeam.BenchPlayersList, Team.LOCAL);
+                               homeF = homeTeam.Formation;
+                               NTeams ++;
+                       }
+                       if (awayTeam != null) {
+                               awayPlayingPlayers = GetPlayers (awayTeam.StartingPlayersList, Team.VISITOR);
+                               awayBenchPlayers = GetPlayers (awayTeam.BenchPlayersList, Team.VISITOR);
+                               awayF = awayTeam.Formation;
+                               NTeams ++;
+                       }
+
+                       colSize = ColumnSize;
+                       playerSize = colSize * 80 / 100;
+
+                       field.LoadTeams (background, homeF, awayF, homePlayingPlayers,
+                                        awayPlayingPlayers, playerSize, NTeams);
+                       homeBench.BenchPlayers = homeBenchPlayers;
+                       awayBench.BenchPlayers = awayBenchPlayers;
+                       homeBench.PlayersSize = awayBench.PlayersSize = playerSize;
+                       homeBench.PlayersPerRow = awayBench.PlayersPerRow = 2;
+                       homeBench.Width = awayBench.Width = colSize * 2;
+                       homeBench.Height = awayBench.Height = field.Height;
+                       
+                       border = Config.Style.TeamTaggerBenchBorder;
+                       homeBench.Position = new Point (border, 0);
+                       field.Position = new Point (awayBench.Width + 2 * border, 0);
+                       awayBench.Position = new Point (awayBench.Width + field.Width + 3 * border, 0);
+
+                       Update ();
+               }
+               
+               public override void Draw (IDrawingToolkit tk, Area area)
+               {
+                       double width, height;
+                       
+                       /* Compute how we should scale and translate to fit the widget
+                        * in the designated area */
+                       width = homeBench.Width * NTeams + field.Width +
+                               2 * NTeams * Config.Style.TeamTaggerBenchBorder; 
+                       height = field.Height;
+                       Image.ScaleFactor ((int)width, (int)height, (int)Width, (int)Height,
+                                          out scaleX, out scaleY, out offset);
+                       tk.Begin ();
+                       tk.TranslateAndScale (Position + offset, new Point (scaleX, scaleY));
+                       homeBench.Draw (tk, area);
+                       awayBench.Draw (tk, area);
+                       field.Draw (tk, area);
+                       tk.End ();
+               }
+
+               public Selection GetSelection (Point point, double precision)
+               {
+                       Selection selection = null;
+
+                       point = Utils.ToUserCoords (point, offset, scaleX, scaleY);
+
+                       selection = homeBench.GetSelection (point, precision);
+                       if (selection == null) {
+                               selection = awayBench.GetSelection (point, precision);
+                               if (selection == null) {
+                                       selection = field.GetSelection (point, precision);
+                               }
+                       }
+                       return selection;
+               }
+
+               public void Move (Selection s, Point p, Point start)
+               {
+               }
+               
+       }
+}
+
diff --git a/LongoMatch.Drawing/LongoMatch.Drawing.mdp b/LongoMatch.Drawing/LongoMatch.Drawing.mdp
index 57d4b38..d8dad1a 100644
--- a/LongoMatch.Drawing/LongoMatch.Drawing.mdp
+++ b/LongoMatch.Drawing/LongoMatch.Drawing.mdp
@@ -49,6 +49,9 @@
     <File subtype="Code" buildaction="Compile" name="CanvasObject/CardObject.cs" />
     <File subtype="Code" buildaction="Compile" name="CanvasObject/TimerObject.cs" />
     <File subtype="Code" buildaction="Compile" name="CanvasObject/TagObject.cs" />
+    <File subtype="Code" buildaction="Compile" name="CanvasObject/BenchObject.cs" />
+    <File subtype="Code" buildaction="Compile" name="CanvasObject/FieldObject.cs" />
+    <File subtype="Code" buildaction="Compile" name="CanvasObject/PlayersTaggerObject.cs" />
   </Contents>
   <References>
     <ProjectReference type="Package" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" />
diff --git a/LongoMatch.Drawing/Makefile.am b/LongoMatch.Drawing/Makefile.am
index 92a8cbb..80dbc64 100644
--- a/LongoMatch.Drawing/Makefile.am
+++ b/LongoMatch.Drawing/Makefile.am
@@ -5,6 +5,7 @@ TARGET = library
 LINK = $(REF_DEP_LONGOMATCH_DRAWING)
 
 SOURCES = Canvas.cs \
+       CanvasObject/BenchObject.cs \
        CanvasObject/CanvasObject.cs \
        CanvasObject/CardObject.cs \
        CanvasObject/CategoryLabel.cs \
@@ -12,9 +13,11 @@ SOURCES = Canvas.cs \
        CanvasObject/CounterObject.cs \
        CanvasObject/CrossObject.cs \
        CanvasObject/EllipseObject.cs \
+       CanvasObject/FieldObject.cs \
        CanvasObject/LineObject.cs \
        CanvasObject/PlayObject.cs \
        CanvasObject/PlayerObject.cs \
+       CanvasObject/PlayersTaggerObject.cs \
        CanvasObject/PositionObject.cs \
        CanvasObject/QuadrilateralObject.cs \
        CanvasObject/RectangleObject.cs \
diff --git a/LongoMatch.Drawing/Utils.cs b/LongoMatch.Drawing/Utils.cs
index 7ceebe5..c0cdf96 100644
--- a/LongoMatch.Drawing/Utils.cs
+++ b/LongoMatch.Drawing/Utils.cs
@@ -94,6 +94,13 @@ namespace LongoMatch.Drawing
                {
                        return RenderFrameDrawing (tk, image.Width, image.Height, fd, image);
                }
+               
+               public static Point ToUserCoords (Point p, Point offset, double scaleX, double scaleY)
+               {
+                       return new Point ((p.X - offset.X) / scaleX,
+                                         (p.Y - offset.Y) / scaleY);
+               
+               }
        }
 }
 
diff --git a/LongoMatch.Drawing/Widgets/TeamTagger.cs b/LongoMatch.Drawing/Widgets/TeamTagger.cs
index 398de12..8eb6686 100644
--- a/LongoMatch.Drawing/Widgets/TeamTagger.cs
+++ b/LongoMatch.Drawing/Widgets/TeamTagger.cs
@@ -34,10 +34,7 @@ namespace LongoMatch.Drawing.Widgets
                public event PlayersPropertiesHandler PlayersSelectionChangedEvent;
                public event PlayersPropertiesHandler ShowMenuEvent;
 
-               TeamTemplate homeTeam, awayTeam;
-               Image background;
-               double currentWidth, currentHeight;
-               double backgroundWidth;
+               PlayersTaggerObject tagger;
                Point offset;
                MultiSelectionMode prevMode;
                bool inSubs;
@@ -46,265 +43,32 @@ namespace LongoMatch.Drawing.Widgets
                {
                        Accuracy = 0;
                        SelectionMode = MultiSelectionMode.MultipleWithModifier;
-                       SubstitutionsMode = false;
-                       HomeColor = Constants.PLAYER_UNSELECTED_COLOR;
-                       AwayColor = Constants.PLAYER_UNSELECTED_COLOR;
-                       PlayersPorRowInBench = 2;
-                       BenchIconSize = PlayersIconSize.Small;
-               }
-
-               public Color HomeColor {
-                       get;
-                       set;
-               }
-
-               public Color AwayColor {
-                       get;
-                       set;
-               }
-
-               public int PlayersPorRowInBench {
-                       get;
-                       set;
-               }
-
-               public PlayersIconSize BenchIconSize {
-                       get;
-                       set;
+                       widget.SizeChangedEvent += HandleSizeChangedEvent;
+                       tagger = new PlayersTaggerObject ();
+                       Objects.Add (tagger);
                }
 
                public void LoadTeams (TeamTemplate homeTeam, TeamTemplate awayTeam, Image background)
                {
-                       this.homeTeam = homeTeam;
-                       this.awayTeam = awayTeam;
-                       this.background = background;
-                       Resize ();
-               }
-
-               public bool SubstitutionsMode {
-                       set {
-                               if (value) {
-                                       prevMode = SelectionMode;
-                                       SelectionMode = MultiSelectionMode.Multiple;
-                                       ClearSelection ();
-                               } else {
-                                       SelectionMode = prevMode;
-                               }
-                               inSubs = value;
-                       }
-                       get {
-                               return inSubs;
-                       }
-               }
-
-               public void Select (List<Player> players)
-               {
-                       ClearSelection ();
-                       if (players != null) {
-                               foreach (Player p in players) {
-                                       SelectPlayer (p, false);
-                               }
-                       }
-                       widget.ReDraw ();
-               }
-
-               public void Select (Player p)
-               {
-                       ClearSelection ();
-                       SelectPlayer (p, false);
+                       tagger.LoadTeams (homeTeam, awayTeam, background);
                        widget.ReDraw ();
                }
 
                public void Reload ()
                {
-                       Objects.Clear ();
-                       if (homeTeam != null) {
-                               LoadTeam (homeTeam, Team.LOCAL);
-                       }
-                       if (awayTeam != null) {
-                               LoadTeam (awayTeam, Team.VISITOR);
-                       }
-                       widget.ReDraw ();
-               }
-
-               int NTeams {
-                       get {
-                               return awayTeam == null ? 1 : 2;
-                       }
-               }
-
-               int BenchWidth {
-                       get {
-                               return PlayersPorRowInBench * (int)BenchIconSize;
-                       }
-               }
-
-               void SelectPlayer (Player p, bool notify=true)
-               {
-                       if (p != null) {
-                               ICanvasObject co = Objects.LastOrDefault (pl => (pl as 
PlayerObject).Player.ID == p.ID);
-                               PlayerObject po = co as PlayerObject;
-                               if (po != null) {
-                                       UpdateSelection (new Selection (po, SelectionPosition.All), notify);
-                               }
-                       }
                }
 
-               PlayersIconSize BestIconSize (int[] formation)
-               {
-                       double width = backgroundWidth / NTeams;
-                       double optWidth = width / formation.Count ();
-                       double optHeight = currentHeight / formation.Max ();
-                       double size = Math.Min (optWidth, optHeight);
-
-                       if (size < (int)PlayersIconSize.Small) {
-                               return PlayersIconSize.Smallest;
-                       } else if (size < (int)PlayersIconSize.Medium) {
-                               return PlayersIconSize.Small;
-                       } else if (size < (int)PlayersIconSize.Large) {
-                               return PlayersIconSize.Medium;
-                       } else if (size < (int)PlayersIconSize.ExtraLarge) {
-                               return PlayersIconSize.Large;
-                       } else {
-                               return PlayersIconSize.ExtraLarge;
-                       }
+               public void Select (Player p) {
                }
-
-               void LoadTeam (TeamTemplate template, Team team)
-               {
-                       int index = 0;
-                       double width, colWidth, offsetX;
-                       Color color;
-                       PlayersIconSize size = BestIconSize (template.Formation);
-
-                       width = backgroundWidth / NTeams;
-                       colWidth = width / template.Formation.Length;
-                       if (team == Team.LOCAL) {
-                               offsetX = BenchWidth;
-                               color = HomeColor;
-                       } else {
-                               offsetX = currentWidth - BenchWidth;
-                               color = AwayColor;
-                       }
-                       
-                       /* Starting players */
-                       for (int col=0; col < template.Formation.Length; col ++) {
-                               double colX, rowHeight;
-                               
-                               if (template.List.Count == index)
-                                       break;
-
-                               if (team == Team.LOCAL) {
-                                       colX = offsetX + colWidth * col + colWidth / 2;
-                               } else {
-                                       colX = offsetX - colWidth * col - colWidth / 2;
-                               }
-                               rowHeight = currentHeight / template.Formation [col];
-
-                               for (int row=0; row < template.Formation[col]; row ++) {
-                                       Point p = new Point (colX, rowHeight * row + rowHeight / 2);
-                                       PlayerObject po = new PlayerObject (template.List [index], p);
-                                       po.IconSize = size;
-                                       po.UnSelectedColor = color;
-                                       Objects.Add (po);
-                                       index ++;
-                                       if (template.List.Count == index)
-                                               break;
-                               }
-                       }
-                       
-                       /* Substitution players */
-                       for (int i = index; i < template.List.Count; i++) {
-                               PlayerObject po;
-                               double x, y;
-                               int reli = i - index;
-                               int s = (int)BenchIconSize;
-                               
-                               x = s * (reli % PlayersPorRowInBench) + s / 2;
-                               y = s * (reli / PlayersPorRowInBench) + s / 2;
-                               if (team == Team.VISITOR) {
-                                       x += BenchWidth + backgroundWidth;
-                               }
-                                                    
-                               po = new PlayerObject (template.List [i], new Point (x, y));
-                               po.IconSize = PlayersIconSize.Small;
-                               po.UnSelectedColor = color;
-                               Objects.Add (po);
-                       }
-               }
-
-               void Resize ()
-               {
-                       currentWidth = widget.Width;
-                       currentHeight = widget.Height;
-                       backgroundWidth = currentWidth - BenchWidth * NTeams;
-                       
-                       if (background != null) {
-                               background.ScaleFactor ((int)backgroundWidth, (int)currentHeight,
-                                                       out scaleX, out scaleY, out offset);
-                       }
-                       Reload ();
-               }
-
-               protected override void SelectionChanged (List<Selection> selections)
-               {
-                       List<Player> players;
-                       
-                       players = selections.Select (s => (s.Drawable as PlayerObject).Player).ToList ();
-
-                       if (SubstitutionsMode) {
-                               bool subsDone = false;
-                               if (homeTeam != null) {
-                                       List<Player> hplayers = players.Where (p => homeTeam.List.Contains 
(p)).ToList ();
-                                       if (hplayers.Count == 2) {
-                                               homeTeam.List.Swap (hplayers [0], hplayers [1]);
-                                               subsDone = true;
-                                       }
-                               }
-                               if (awayTeam != null) {
-                                       List<Player> aplayers = players.Where (p => awayTeam.List.Contains 
(p)).ToList ();
-                                       if (aplayers.Count == 2) {
-                                               awayTeam.List.Swap (aplayers [0], aplayers [1]);
-                                               subsDone = true;
-                                       }
-                               }
-                               if (subsDone) {
-                                       ClearSelection ();
-                                       Reload ();
-                                       widget.ReDraw ();
-                               }
-                       } else {
-                               if (PlayersSelectionChangedEvent != null) {
-                                       PlayersSelectionChangedEvent (players);
-                               }
-                       }
-               }
-
+               
                protected override void ShowMenu (Point coords)
                {
-                       if (ShowMenuEvent != null && Selections.Count > 0) {
-                               ShowMenuEvent (
-                                       Selections.Select (s => (s.Drawable as PlayerObject).Player).ToList 
());
-                       }
                }
 
-               public override void Draw (IContext context, Area area)
+               void HandleSizeChangedEvent ()
                {
-                       if (currentWidth != widget.Width || currentHeight != widget.Height) {
-                               Resize ();
-                       }
-                       
-                       tk.Context = context;
-                       tk.Begin ();
-
-                       /* Background */
-                       if (background != null) {
-                               tk.DrawImage (new Point (BenchWidth, 0), backgroundWidth, currentHeight,
-                                             background, false);
-                       }
-                       
-                       tk.End ();
-                       base.Draw (context, area);
+                       tagger.Width = widget.Width;
+                       tagger.Height = widget.Height;
                }
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]