Exemple #1
0
        /// <summary>
        /// Save a new custom puzzle
        /// </summary>
                #if IOS
        public PuzzleData AddPuzzle(string filename, string owner, MonoTouch.UIKit.UIImage image)
        {
            string completeFilePath = System.IO.Path.Combine(mCustomPuzzlePath, filename);

            Logger.I("Adding a new " + owner + " puzzle " + filename);

            // Save the new image
            MonoTouch.Foundation.NSError error;
            image.AsPNG().Save(completeFilePath, true, out error);

            if (error == null)
            {
                Logger.I("Adding puzzle OK");
            }
            else
            {
                Logger.E("Adding puzzle KO: " + error);
            }

            PuzzleData newPuzzle = new PuzzleData()
            {
                Filename = completeFilePath,
                IsCustom = true,
                IsNew    = true,
                OwnerId  = owner
            };

            Savedgame.Puzzles.Add(newPuzzle);
            Save();

            return(newPuzzle);
        }
Exemple #2
0
        // Predictable seed
        /// <summary>
        /// Initializes a new instance of the <see cref="PixPuzzle.Data.Grid"/> class.
        /// </summary>
        /// <param name="imageWidth">Image width.</param>
        /// <param name="imageHeight">Image height.</param>
        /// <param name="cellSize">Cell size.</param>
        public Grid(PuzzleData puzzle, int imageWidth, int imageHeight, int cellSize)
        {
            CellSize = cellSize;
            Width    = imageWidth;
            Height   = imageHeight;

            // Determine maximum number for the path
            // TODO Change that, not very interesting
            MaxPathLength = 9 + ((Math.Max(imageWidth, imageHeight) / 32) * 2);
        }
Exemple #3
0
		// Predictable seed
		/// <summary>
		/// Initializes a new instance of the <see cref="PixPuzzle.Data.Grid"/> class.
		/// </summary>
		/// <param name="imageWidth">Image width.</param>
		/// <param name="imageHeight">Image height.</param>
		/// <param name="cellSize">Cell size.</param>
		public Grid (PuzzleData puzzle, int imageWidth, int imageHeight, int cellSize)
		{
			CellSize = cellSize;
			Width = imageWidth;
			Height = imageHeight;

			// Determine maximum number for the path
			// TODO Change that, not very interesting
			MaxPathLength = 9 + ((Math.Max (imageWidth, imageHeight) / 32) * 2);
		}
Exemple #4
0
        public PathGridView(PuzzleData puzzle, int width, int height)
            : base(puzzle, width, height, AppDelegate.UserInterfaceIdiomIsPhone ? CELL_SIZE_IPHONE : CELL_SIZE_IPAD)
        {
            // Create the view
            PathGridViewInternal theView = new PathGridViewInternal (this, new RectangleF (0, 0, width * CellSize, height * CellSize));

            // Create the grid and cells views
            CreateGrid (0,0, theView);

            GridViewInternal = theView;
        }
        /// <summary>
        /// Update the detail view with a puzzle
        /// </summary>
        /// <param name="puzzle">Puzzle.</param>
        public void SetPuzzle(PuzzleData puzzle)
        {
            ImagePuzzle.Image = UIImage.FromFile (puzzle.Filename);
            LabelTitle.Text = System.IO.Path.GetFileNameWithoutExtension (puzzle.Filename);

            DateTime? bestScore = puzzle.GetBestPlayerScore (GameCenterHelper.LocalPlayer.PlayerID);
            if (bestScore.HasValue) {
                LabelTime.Text = bestScore.Value.ToString ("mm:ss");
            } else {
                LabelTime.Text = "Not completed yet";
            }
        }
        public void UpdatePuzzleView(PuzzleData puzzle)
        {
            // Code specific view properties
            this.Layer.BorderWidth = 1.0f;
            this.Layer.BorderColor = UIColor.Black.CGColor;

            if (this.Selected) {
                SetSelected ();
            } else {
                UnsetSelected ();
            }
        }
        /// <summary>
        /// Defines the puzzle the view should display
        /// </summary>
        /// <param name="puzzle">Puzzle.</param>
        /// <param name="selectedPuzzle">Selected puzzle.</param>
        public void DefinePuzzle(PuzzleData puzzle, UIImage selectedPuzzle)
        {
            this.Puzzle = puzzle;

            // Load the image
            UIImage image = selectedPuzzle;
            Bitmap bitmap = new Bitmap (image);

            mGridUIView = InitializeGrid (puzzle, image, bitmap);

            mIsPaused = false;
            mCurrentTime = new DateTime (0);
        }
        public void SetSelectedPuzzle(PuzzleData puzzle)
        {
            lastSelectedPuzzle = puzzle;

            var detailView = this.ChildViewControllers
                .First (vc => vc is PuzzleListDetailViewController) as PuzzleListDetailViewController;

            if (puzzle == null) {
                detailView.View.Hidden = true;
            } else {
                detailView.View.Hidden = false;

                detailView.SetPuzzle (puzzle);
            }
        }
        public static PuzzlesListCellViewController Create(PuzzleData puzzle)
        {
            PuzzlesListCellViewController cell = Nib.Instantiate (null, null) [0] as PuzzlesListCellViewController;

            cell.UpdatePuzzleView (puzzle);

            // Get components. Check XCode for tags.
            // 1 = image
            // 2 = friends icon
            // 3 = completed icon
            UIImageView imageView = cell.ViewWithTag (1) as UIImageView;
            imageView.Image = UIImage.FromFile (puzzle.Filename);

            UIImageView friendsIcon = cell.ViewWithTag (2) as UIImageView;
            friendsIcon.Hidden = !(puzzle.IsCustom);

            UIImageView completedIcon = cell.ViewWithTag (3) as UIImageView;
            completedIcon.Hidden = (puzzle.GetBestPlayerScore(GameCenterHelper.LocalPlayer.PlayerID).HasValue == false);

            return cell;
        }
Exemple #10
0
        /// <summary>
        /// Update versus match status
        /// </summary>
        public static void UpdateMatchFromPuzzle(PuzzleData puzzle, Action<NSError> callback)
        {
            if (puzzle.Match == null) {
                Logger.E ("This puzzle contains no match data... Can't do anything!");
                return;
            }

            // Match data -> Puzzle + Image
            // -- Image as Base64 string
            UIImage image = UIImage.FromFile (puzzle.Filename);
            Byte[] byteArray = null;
            using (NSData nsImageData = image.AsPNG()) {
                byteArray = new Byte[nsImageData.Length];
                System.Runtime.InteropServices.Marshal.Copy (nsImageData.Bytes, byteArray, 0, Convert.ToInt32 (nsImageData.Length));
            }

            string base64image = Convert.ToBase64String (byteArray);

            // -- Data
            TransferablePuzzleData tp = new TransferablePuzzleData ();
            tp.Puzzle = puzzle;
            tp.Base64Image = base64image;

            // Build an XML text
            string xml = string.Empty;
            XmlSerializer xmlSerializer = new XmlSerializer (tp.GetType ());

            using (StringWriter textWriter = new StringWriter()) {
                xmlSerializer.Serialize (textWriter, tp);
                xml = textWriter.ToString ();
            }

            xmlSerializer = null;

            // Xml to bytes
            NSData puzzleData = NSData.FromString (xml);

            // Next participant
            GKTurnBasedParticipant nextPlayer = null;
            foreach (var participant in puzzle.Match.Participants) {
                if (participant.PlayerID != GKLocalPlayer.LocalPlayer.PlayerID) {
                    nextPlayer = participant;
                    break;
                }
            }

            // Game center match progress
            // -- Already an opponent score? End
            if (puzzle.GetBestPlayerScore (nextPlayer.PlayerID).HasValue) {
                puzzle.Match.EndMatchInTurn (
                    puzzleData,
                    (err) => {
                    if (callback != null) {
                        callback (err);
                    }
                }
                );
            } else {
                // -- Send current score
                puzzle.Match.EndTurn (
                    new GKTurnBasedParticipant[] { nextPlayer },
                    120,
                    puzzleData,
                    callback
                );
            }
        }
        public static FriendsPuzzleListCellViewController Create(PuzzleData puzzle)
        {
            FriendsPuzzleListCellViewController cell = Nib.Instantiate (null, null) [0] as FriendsPuzzleListCellViewController;

            return cell;
        }
 public MenuPlayViewController(IntPtr handle)
     : base(handle)
 {
     lastSelectedPuzzle = null;
 }
        void LaunchPuzzleForCurrentImage(PuzzleData matchPuzzle)
        {
            // Register a new puzzle
            string me = (GKLocalPlayer.LocalPlayer.Authenticated ? GKLocalPlayer.LocalPlayer.PlayerID : "Me");
            PuzzleData puzzle = PuzzleService.Instance.AddPuzzle (Guid.NewGuid () + ".png", me, mImageToUse);

            if (matchPuzzle != null) {
                puzzle.Match = matchPuzzle.Match;
            }

            // Prepare game
            var vc = this.Storyboard.InstantiateViewController ("GameViewController") as GameViewController;
            vc.DefinePuzzle (puzzle, mImageToUse);

            NavigationController.PushViewController (vc, true);
        }
        /// <summary>
        /// Create a UIView containing the game
        /// </summary>
        /// <returns>The grid.</returns>
        /// <param name="puzzle">Puzzle.</param>
        /// <param name="image">Image.</param>
        /// <param name="bitmap">Bitmap.</param>
        private UIView InitializeGrid(PuzzleData puzzle, UIImage image, Bitmap bitmap)
        {
            UIView view = null;

            // Here we create the real game view
            mPathGrid = new PathGridView (puzzle, (int)image.Size.Width, (int)image.Size.Height);
            mPathGrid.GridCompleted += GridCompleted;

            // Store it to prevent garbage collection
            view = mPathGrid.GridViewInternal;

            // Look at each pixel of the image
            CellColor[][] pixels = new CellColor[(int)image.Size.Width][];

            for (int x=0; x<image.Size.Width; x++) {

                pixels [x] = new CellColor[(int)image.Size.Height];

                for (int y=0; y<image.Size.Height; y++) {

                    // Get the pixel color
                    Color c = bitmap.GetPixel (x, y);

                    // Transform to generic color
                    pixels [x] [y] = new CellColor () {
                        A = c.A/255f,
                        R = c.R/255f,
                        G = c.G/255f,
                        B = c.B/255f
                    };

                }
            }

            // Transfer pixel data to the internal grid
            this.mPathGrid.SetupGrid (pixels);

            return view;
        }
Exemple #15
0
            public override void FoundMatch(GKTurnBasedMatchmakerViewController viewController, GKTurnBasedMatch match)
            {
                Logger.I ("MatchMakerDelegate.FoundMatch");

                viewController.DismissViewController (true, null);

                PuzzleData puzzleData = new PuzzleData ();
                puzzleData.Match = match;
                puzzleData.MatchId = match.MatchID;

                bool matchError = false;

                if (match.MatchData.Length > 0) {
                    // Match has data
                //					var tp = GameCenterHelper.GetPuzzleFromMatch (match);
                    // TODO ?
                } else {
                    // No data: new match

                    // Set up outcomes
                    // -> Player who sent the picture set a time first
                    match.Participants [0].MatchOutcome = GKTurnBasedMatchOutcome.First;
                    match.Participants [1].MatchOutcome = GKTurnBasedMatchOutcome.Second;
                }

                if (matchError == false) {
                    match.Remove (new GKNotificationHandler ((e) => {}));

                    if (MatchFoundCallback != null) {
                        MatchFoundCallback (puzzleData);
                    }
                } else {
                    if (ErrorCallback != null) {
                        ErrorCallback ();
                    }
                }
            }
Exemple #16
0
        /// <summary>
        /// Save a new custom puzzle
        /// </summary>
        public PuzzleData AddPuzzle(string filename, string owner, MonoTouch.UIKit.UIImage image)
        {
            string completeFilePath = System.IO.Path.Combine (mCustomPuzzlePath, filename);

            Logger.I ("Adding a new " + owner+ " puzzle " + filename);

            // Save the new image
            MonoTouch.Foundation.NSError error;
            image.AsPNG ().Save (completeFilePath, true, out error);

            if (error == null) {
                Logger.I ("Adding puzzle OK");
            }
            else {
                Logger.E ("Adding puzzle KO: " + error);
            }

            PuzzleData newPuzzle = new PuzzleData () {
                Filename = completeFilePath,
                IsCustom = true,
                IsNew = true,
                OwnerId = owner
            };

            Savedgame.Puzzles.Add (newPuzzle);
            Save ();

            return newPuzzle;
        }
        internal void SelectPuzzleManually(PuzzleData selectedPuzzle)
        {
            int index = -1;
            int section = -1;

            if (puzzlesPxn.Contains (selectedPuzzle)) {
                section = 0;
                index = puzzlesPxn.IndexOf (selectedPuzzle);
            } else if (puzzlesCustom.Contains (selectedPuzzle)) {
                section = 1;
                index = puzzlesCustom.IndexOf (selectedPuzzle);
            }

            NSIndexPath path = NSIndexPath.FromItemSection (index, section);
            var cell = CollectionView.CellForItem (path) as PuzzlesListCellViewController;
            cell.SetSelected ();

            lastSelectedCell = cell;
        }