private static List <BasicShape> ExtractShapesFromCanvas(Templates.Canvas canvas)
        {
            List <BasicShape> basicShapeList = new List <BasicShape>();
            dynamic           shapes         = canvas.shapes;

            for (int i = 0; i < shapes.Length; i++)
            {
                // If there are 8 attributes, it's a class, else, it's a basicShape
                if (shapes[i].Count == 8)
                {
                    string        id         = shapes[i]["id"];
                    int           type       = shapes[i]["type"];
                    string        name       = shapes[i]["name"];
                    var           shapeStyle = GetShapeStyle(shapes[i]["shapeStyle"]);
                    List <string> linksTo    = GetStringList(shapes[i]["linksTo"]);
                    List <string> linksFrom  = GetStringList(shapes[i]["linksFrom"]);
                    List <string> attributes = GetStringList(shapes[i]["attributes"]);
                    List <string> methods    = GetStringList(shapes[i]["methods"]);
                    basicShapeList.Add(new ClassShape(id, type, name, shapeStyle, linksTo, linksFrom, attributes, methods));
                }
                else
                {
                    string        id         = shapes[i]["id"];
                    int           type       = shapes[i]["type"];
                    string        name       = shapes[i]["name"];
                    var           shapeStyle = GetShapeStyle(shapes[i]["shapeStyle"]);
                    List <string> linksTo    = GetStringList(shapes[i]["linksTo"]);
                    List <string> linksFrom  = GetStringList(shapes[i]["linksFrom"]);
                    basicShapeList.Add(new BasicShape(id, type, name, shapeStyle, linksTo, linksFrom));
                }
            }

            return(basicShapeList);
        }
        public static void CreateCanvas(Templates.Canvas canvas)
        {
            EditCanevasData editCanevasData = new EditCanevasData(username, canvas);

            editCanevasData.canevas.dimensions.x *= 2.1;
            editCanevasData.canevas.dimensions.y *= 2.1;
            currentCanvas = canvas;
            socket.Emit("createCanvas", serializer.Serialize(editCanevasData));
        }
        public static void SendCanvas(string thumbnail)
        {
            Templates.Canvas canvas = new Templates.Canvas();
            canvas.name      = canvasName;
            canvas.thumbnail = thumbnail;
            EditCanevasData editCanevasData = new EditCanevasData(username, canvas);

            // Commente pour pas buster le cloud (à uncomment avant la remise)
            socket.Emit("saveCanvas", serializer.Serialize(editCanevasData));
        }
예제 #4
0
        private void CreateCanvas(object o)
        {
            var passwordBox = o as PasswordBox;

            if (CanvasProtection == "Unprotected")
            {
                passwordBox.Password = "";
            }
            var password = passwordBox.Password;

            int accessibility = CanvasPrivacy == "Public" ? 1 : 0;
            // Max: W:1520, H1200
            Coordinates dimensions = new Coordinates(500, 380);

            Templates.Canvas canvas = new Templates.Canvas(Guid.NewGuid().ToString(), CanvasName, username, username,
                                                           accessibility, password, new List <BasicShape>(), new List <Link>(), dimensions, EMPTY_CANVAS);

            DrawingService.CreateCanvas(canvas);

            passwordBox.Password = "";
            CanvasPrivacy        = "Public";
            CanvasProtection     = "Unprotected";
            CanvasName           = "";
        }
        public static void Initialize(object o)
        {
            socket.On(Socket.EVENT_RECONNECT, () =>
            {
                if (canvasName != null)
                {
                    // Stocker le mot de passe? ADD
                    JoinCanvas(canvasName, "");
                }
            });

            #region canvas .On
            socket.On("createCanvasResponse", (data) =>
            {
                CreateCanvasResponse response = serializer.Deserialize <CreateCanvasResponse>((string)data);
                if (response.isCreated)
                {
                    canvasName = response.canvasName;
                    RefreshCanvases();
                }
                else
                {
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { CanvasCreationFailed(); }), DispatcherPriority.Render);
                }
            });

            socket.On("canvasCreated", (data) =>
            {
                RefreshCanvases();
            });

            socket.On("updateCanvasPasswordResponse", (data) =>
            {
                UpdateCanvasPasswordResponse response = serializer.Deserialize <UpdateCanvasPasswordResponse>((string)data);
                if (response.isPasswordUpdated)
                {
                    RefreshCanvases();
                }
            });

            socket.On("canvasPasswordUpdated", (data) =>
            {
                LeaveCanvas();
                Application.Current?.Dispatcher?.Invoke(new Action(() => { BackToGallery(); }), DispatcherPriority.Render);
            });

            socket.On("accessCanvasResponse", (data) =>
            {
                AccessCanvasResponse response = serializer.Deserialize <AccessCanvasResponse>((string)data);
                if (response.isPasswordValid)
                {
                    canvasName = response.canvasName;
                }
                Application.Current?.Dispatcher?.Invoke(new Action(() => { JoinCanvasRoom(response); }), DispatcherPriority.Render);
            });

            socket.On("canvasSelected", (data) =>
            {
                EditGalleryData response = serializer.Deserialize <EditGalleryData>((string)data);
                if (!username.Equals((string)response.username) && response.canevasName.Equals(canvasName))
                {
                    isCanvasSizeRemotelyEditing = true;
                }
            });

            socket.On("canvasDeselected", (data) =>
            {
                EditGalleryData response = serializer.Deserialize <EditGalleryData>((string)data);
                if (!username.Equals((string)response.username) && response.canevasName.Equals(canvasName))
                {
                    isCanvasSizeRemotelyEditing = false;
                }
            });

            socket.On("canvasResized", (data) =>
            {
                ResizeCanevasData response = serializer.Deserialize <ResizeCanevasData>((string)data);
                if (username != null && !username.Equals((string)response.username))
                {
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { OnResizeCanvas(response.dimensions); }), DispatcherPriority.Render);
                }
            });

            socket.On("getPublicCanvasResponse", (data) =>
            {
                PublicCanvases canvases = serializer.Deserialize <PublicCanvases>((string)data);

                ExtractCanvasesShapes(canvases.publicCanvas);

                try
                {
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdatePublicCanvases(canvases); }), DispatcherPriority.Render);
                }
                catch { }
            });

            socket.On("getPrivateCanvasResponse", (data) =>
            {
                PrivateCanvases canvases = serializer.Deserialize <PrivateCanvases>((string)data);

                ExtractCanvasesShapes(canvases.privateCanvas);
                Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdatePrivateCanvases(canvases); }), DispatcherPriority.Render);
            });
            #endregion

            #region links .On
            socket.On("linkCreated", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    LinkStroke linkStroke = createLinkStroke(response.links[0]);
                    linkStroke.owner      = response.username;
                    InkCanvasStrokeCollectedEventArgs eventArgs = new InkCanvasStrokeCollectedEventArgs(linkStroke);
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { AddStroke(eventArgs); }), DispatcherPriority.ContextIdle);
                }
            });

            socket.On("linksDeleted", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    StrokeCollection strokes = new StrokeCollection();
                    foreach (dynamic link in response.links)
                    {
                        strokes.Add(createLinkStroke(link));
                    }
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { RemoveStrokes(strokes); }), DispatcherPriority.ContextIdle);
                }
            });

            socket.On("linksUpdated", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    foreach (dynamic link in response.links)
                    {
                        InkCanvasStrokeCollectedEventArgs eventArgs = new InkCanvasStrokeCollectedEventArgs(createLinkStroke(link));
                        Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateStroke(eventArgs); }), DispatcherPriority.Render);
                    }
                }
            });

            socket.On("linksSelected", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    StrokeCollection strokes = new StrokeCollection();
                    foreach (dynamic link in response.links)
                    {
                        LinkStroke linkStroke = createLinkStroke(link);
                        strokes.Add(linkStroke);
                        if (!remoteSelectedStrokes.Contains(linkStroke.guid.ToString()))
                        {
                            remoteSelectedStrokes.Add(linkStroke.guid.ToString());
                        }
                        Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateSelection(); }), DispatcherPriority.Render);
                    }
                }
            });

            socket.On("linksDeselected", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    StrokeCollection strokes = new StrokeCollection();
                    foreach (dynamic link in response.links)
                    {
                        LinkStroke linkStroke = createLinkStroke(link);
                        strokes.Add(linkStroke);
                        if (remoteSelectedStrokes.Contains(linkStroke.guid.ToString()))
                        {
                            remoteSelectedStrokes.Remove(linkStroke.guid.ToString());
                        }
                        Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateDeselection(); }), DispatcherPriority.Render);
                    }
                }
            });
            #endregion

            #region forms .On
            socket.On("formCreated", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                //Console.WriteLine("FORM CREATED: \n" + "position: " + response.forms[0].shapeStyle.coordinates.x + ", "
                //    + response.forms[0].shapeStyle.coordinates.y + "/n" + "size: " + response.forms[0].shapeStyle.width + ", "
                //    + response.forms[0].shapeStyle.height);
                if (!username.Equals((string)response.username))
                {
                    CustomStroke customStroke = createShapeStroke(response.forms[0]);
                    InkCanvasStrokeCollectedEventArgs eventArgs = new InkCanvasStrokeCollectedEventArgs(customStroke);
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { AddStroke(eventArgs); }), DispatcherPriority.ContextIdle);
                }
            });

            socket.On("formsDeleted", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    StrokeCollection strokes = new StrokeCollection();
                    foreach (dynamic shape in response.forms)
                    {
                        ShapeStroke shapeStroke = createShapeStroke(shape);
                        if (remoteSelectedStrokes.Contains(shapeStroke.guid.ToString()))
                        {
                            remoteSelectedStrokes.Remove(shapeStroke.guid.ToString());
                        }
                        strokes.Add(shapeStroke);
                    }
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { RemoveStrokes(strokes); }), DispatcherPriority.ContextIdle);
                }
            });

            socket.On("formsUpdated", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    foreach (dynamic shape in response.forms)
                    {
                        InkCanvasStrokeCollectedEventArgs eventArgs = new InkCanvasStrokeCollectedEventArgs(createShapeStroke(shape));
                        Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateStroke(eventArgs); }), DispatcherPriority.Render);
                    }
                }
            });

            socket.On("formsSelected", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    StrokeCollection strokes = new StrokeCollection();
                    foreach (dynamic shape in response.forms)
                    {
                        ShapeStroke stroke = createShapeStroke(shape);
                        strokes.Add(stroke);
                        if (!remoteSelectedStrokes.Contains(stroke.guid.ToString()))
                        {
                            remoteSelectedStrokes.Add(stroke.guid.ToString());
                        }
                        Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateSelection(); }), DispatcherPriority.Render);
                    }
                }
            });

            socket.On("formsDeselected", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                if (!username.Equals((string)response.username))
                {
                    StrokeCollection strokes = new StrokeCollection();
                    foreach (dynamic shape in response.forms)
                    {
                        ShapeStroke stroke = createShapeStroke(shape);
                        strokes.Add(stroke);
                        if (remoteSelectedStrokes.Contains(stroke.guid.ToString()))
                        {
                            remoteSelectedStrokes.Remove(stroke.guid.ToString());
                        }
                        Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateDeselection(); }), DispatcherPriority.Render);
                    }
                }
            });
            #endregion

            socket.On("selectedForms", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                foreach (string id in response.selectedForms)
                {
                    if (!remoteSelectedStrokes.Contains(id))
                    {
                        remoteSelectedStrokes.Add(id);
                    }
                }
                Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateDeselection(); }), DispatcherPriority.Render);
            });

            socket.On("selectedLinks", (data) =>
            {
                dynamic response = JObject.Parse((string)data);
                foreach (string id in response.selectedLinks)
                {
                    if (!remoteSelectedStrokes.Contains(id))
                    {
                        remoteSelectedStrokes.Add(id);
                    }
                }
                Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateDeselection(); }), DispatcherPriority.Render);
            });

            socket.On("canvasSaved", (data) =>
            {
                EditCanevasData response = serializer.Deserialize <EditCanevasData>((string)data);
                RefreshCanvases();
                saving = false;
            });

            socket.On("getCanvasResponse", (data) =>
            {
                Templates.Canvas canvas = serializer.Deserialize <Templates.Canvas>((string)data);
                canvasName    = canvas.name;
                currentCanvas = canvas;
                Application.Current?.Dispatcher?.Invoke(new Action(() => { OnResizeCanvas(canvas.dimensions); }), DispatcherPriority.ContextIdle);
                localSelectedStrokes             = new List <string>();
                localAddedStrokes                = new List <string>();
                EditGalleryData editGallerysData = new EditGalleryData(username, canvasName, "");

                foreach (Link link in canvas.links)
                {
                    LinkStroke linkStroke = LinkStrokeFromLink(link);
                    InkCanvasStrokeCollectedEventArgs eventArgs = new InkCanvasStrokeCollectedEventArgs(linkStroke);
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { AddStroke(eventArgs); }), DispatcherPriority.ContextIdle);
                }

                List <BasicShape> basicShapes = ExtractShapesFromCanvas(canvas);
                foreach (BasicShape shape in basicShapes)
                {
                    ShapeStroke shapeStroke = ShapeStrokeFromShape(shape);
                    InkCanvasStrokeCollectedEventArgs eventArgs = new InkCanvasStrokeCollectedEventArgs(shapeStroke);
                    Application.Current?.Dispatcher?.Invoke(new Action(() => { AddStroke(eventArgs); }), DispatcherPriority.ContextIdle);
                }

                socket.Emit("getSelectedForms", serializer.Serialize(editGallerysData));
                socket.Emit("getSelectedLinks", serializer.Serialize(editGallerysData));

                Application.Current?.Dispatcher?.Invoke(new Action(() => { RefreshChildren(); }), DispatcherPriority.ContextIdle);
            });

            socket.On("canvasReinitialized", (data) =>
            {
                localAddedStrokes     = new List <string>();
                localSelectedStrokes  = new List <string>();
                remoteSelectedStrokes = new List <string>();
                Application.Current?.Dispatcher?.Invoke(new Action(() => { RemoteReset(); }), DispatcherPriority.Render);
            });

            socket.On("getCanvasLogHistoryResponse", (data) =>
            {
                HistoryData[] historyData = serializer.Deserialize <HistoryData[]>((string)data);
                History history           = new History(historyData);

                Application.Current?.Dispatcher?.Invoke(new Action(() => { UpdateHistory(history); }), DispatcherPriority.Render);
            });

            socket.On("disconnect", (data) =>
            {
                // Application.Current?.Dispatcher?.Invoke(new Action(() => { BackToGallery(); }), DispatcherPriority.ContextIdle);
            });

            RefreshCanvases();
        }
        public static void DrawCanvas(Templates.Canvas canvas)
        {
            EditGalleryData editGalleryData = new EditGalleryData(username, canvasName);

            socket.Emit("getCanvas", serializer.Serialize(editGalleryData));
        }