/// <summary> /// Handles the DrawItem event of the shapesList control. /// Draws item in ListBox. Special drawing (colored items). /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param> private void shapesList_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index == -1) { return; } ShapeState shape = ((ListBox)sender).Items[e.Index] as ShapeState; Debug.Assert(shape != null, "Item is not an instance of TextureScreen.ShapeState"); // draw background if (e.Index == shapesList.SelectedIndex) { brush.Color = Color.White; e.Graphics.FillRectangle(brush, e.Bounds); brush.Color = Color.Black; } else { brush.Color = shape.Color; e.Graphics.FillRectangle(brush, e.Bounds); brush.Color = Color.White; } // draw text e.Graphics.DrawString(shape.ShapeText(), ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y); }
/// <summary> /// Called when <see cref="ShapesEditingScreen"/> state changes. /// Changes visibility of controls for the current shape. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void ShapesScreen_StateChanged(object sender, EventArgs e) { ShapeState shapeState = ShapesScreen.State as ShapeState; if (shapeState == null) { polygonSettings.Visible = false; } else { switch (shapeState.Shape.Type) { case Shape.ShapeType.Polygon: polygonSettings.Visible = true; break; case Shape.ShapeType.Circle: polygonSettings.Visible = false; break; case Shape.ShapeType.Edge: polygonSettings.Visible = false; break; } } }
/// <summary> /// Handles the FormClosing event of the TextureForm control. /// Checks if collision shapes are valid. If not form is not closed until all shapes are valid. /// If yes, saves data to the texture and invokes its <see cref="DrawableAsset.DrawableAssetChanged"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param> private void TextureForm_FormClosing(object sender, FormClosingEventArgs e) { // check if every shape is valid for (int i = 0; i < shapesController.ShapesList.Items.Count; ++i) { ShapeState shapeState = shapesController.ShapesList.Items[i] as ShapeState; if (shapeState != null) { // shape is not valid if (!shapeState.IsShapeValid()) { // select invalid shape shapesController.ShapesList.SelectedIndex = i; // show message shapeState.OnInvalidShape(); // form will not be closed until all shapes are valid e.Cancel = true; return; } } } // texture is valid => update texture UpdateTexture(); // texture changed texture.InvokeDrawableAssetChanged(); }
/// <summary> /// Removes the specified shape from the screen. /// </summary> /// <param name="shape">Shape to remove.</param> public void RemoveShape(ShapeState shape) { if (State == shape) { State = DefaultState; } Shapes.Remove(shape); }
/// <summary> /// Handles the SelectedIndexChanged event of the shapesList control. /// Changes the active shape of the <see cref="ShapesEditingScreen"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void shapesList_SelectedIndexChanged(object sender, EventArgs e) { if (shapesList.SelectedIndex != -1) { ShapeState shapeState = shapesList.SelectedItem as ShapeState; Debug.Assert(shapeState != null, "Item is not an instance of TextureScreen.ShapeState"); ShapesScreen.State = shapeState; shapesList.Invalidate(); } }
/// <summary> /// Creates polygon from the texture. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void createPolygonFromTextureButton_Click(object sender, EventArgs e) { ShapeState newItem = CreatePolygonFromTexture(); if (newItem != null) { ShapesList.Items.Add(newItem); ShapesList.SelectedIndex = ShapesList.Items.Count - 1; } Invalidate(); }
/// <summary> /// Handles the KeyDown event of the shapesList control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param> private void shapesList_KeyDown(object sender, KeyEventArgs e) { // delete selected shape if (e.KeyCode == Keys.Delete && shapesList.SelectedIndex != -1) { ShapeState shapeToRemove = shapesList.SelectedItem as ShapeState; if (shapeToRemove != null) { // remove shape ShapesScreen.RemoveShape(shapeToRemove); shapesList.Items.RemoveAt(shapesList.SelectedIndex); // select last shape if any if (shapesList.SelectedIndex == -1 && shapesList.Items.Count != 0) { shapesList.SelectedIndex = shapesList.Items.Count - 1; } } } }
/// <summary> /// Creates new shape based on chosen type from checkbox. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void addShapeButton_Click(object sender, EventArgs e) { // find chosen shape foreach (Shape.ShapeType shape in Enum.GetValues(typeof(Shape.ShapeType))) { if (selectShapeBox.Text == shape.ToString()) { // add new shape ShapeState newItem = ShapesScreen.AddShape(shape); if (newItem != null) { shapesList.Items.Add(newItem); shapesList.SelectedIndex = shapesList.Items.Count - 1; } break; } } }
/// <summary> /// Adds the specified shape to the screen. /// </summary> /// <param name="shape">Shape to add</param> /// <returns>Returns <see cref="ShapeState"/> of the added shape.</returns> public ShapeState AddShape(Shape shape) { ShapeState newState = null; switch (shape.Type) { case Shape.ShapeType.Polygon: newState = new PolygonEditState((Polygon)shape, this) { Color = ColorSettings.GoodColors[goodColorsIndex] }; State = newState; break; case Shape.ShapeType.Circle: newState = new CircleEditState((Circle)shape, this) { Color = ColorSettings.GoodColors[goodColorsIndex] }; State = newState; break; case Shape.ShapeType.Edge: newState = new EdgeEditState((Edge)shape, this) { Color = ColorSettings.GoodColors[goodColorsIndex] }; State = newState; break; } if (newState != null) { if (++goodColorsIndex >= ColorSettings.GoodColors.Length) { goodColorsIndex = 0; } Shapes.Add(newState); } return(newState); }
/// <summary> /// Initializes a new instance of the <see cref="TextureForm"/> class. /// </summary> /// <param name="texture">The texture to edit.</param> public TextureForm(Texture texture) { InitializeComponent(); Icon = Properties.Resources._2DPGC_Logo; // messages manager messagesManager = new DefaultMessagesManager(statusLabel); Messages.MessagesManager = messagesManager; // texture this.texture = texture; // init controls settings nameTextBox.Text = texture.Name; // texture screen textureScreen = new TextureScreen(); textureScreen.Texture = texture; textureScreen.Dock = DockStyle.Fill; tableLayoutPanel.Controls.Add(textureScreen, 0, 0); textureScreen.Zoom = 100; textureScreen.Position = new PointF(-textureScreen.Width / 2f, -textureScreen.Height / 2f); // shapes controller shapesController = new TextureControllerForShapesEditing(textureScreen); shapesController.Location = new Point(3, 81); textureSettingsPanel.Controls.Add(shapesController); // title (window text) UpdateTitle(); // init shapes from texture foreach (Shape shape in texture.Shapes) { ShapeState newItem = textureScreen.AddShape(shape); shapesController.ShapesList.Items.Add(newItem); } shapesController.ShapesList.SelectedIndex = shapesController.ShapesList.Items.Count - 1; }
/// <summary> /// Removes the specified shape. /// </summary> /// <param name="shape">Shape as <see cref="ShapeState"/> to remove.</param> private void RemoveShape(ShapeState shape) { shapesList.Items.Remove(shape); ShapesScreen.RemoveShape(shape); }