ObjectElement

这个博客主要探讨了一个抽象类`ObjElement`,它用于表示图形用户界面中的对象元素。类中包含了数据实体、选择状态管理以及绘制操纵柄的方法。操纵柄的绘制根据对象是否被选中或当前高亮显示,使用不同的颜色来区分。此外,还提供了选择和取消选择对象的接口,方便用户交互。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanvasDemo.Canvas
{
///
/// 对象元素
///
public abstract class ObjElement : ObjElement where T : IElementData
{
///
/// 对象实体
///
public T Data { get; set; }

    public ObjElement(Layer layer, T data) : base(layer, data.ID)
    {
        Data = data;
    }
}

public abstract class ObjElement : Element
{

    public Layer Layer;

    public ObjElement(Layer layer, string id) : base(layer.Canvas, id)
    {
        Layer = layer;
        Layer.Elements.Add(this);
    }

    /// <summary>
    /// 是否非选中
    /// </summary>
    public bool IsSelected { get; private set; } = false;

    /// <summary>
    /// 是否是当前对象
    /// </summary>
    public bool IsCurrent { get; private set; } = false;

    /// <summary>
    /// 选择对象
    /// </summary>
    public void Selected()
    {
        IsSelected = true;
        SelectedEvent();
    }
    protected virtual void SelectedEvent() { }

    /// <summary>
    /// 清除对象选择
    /// </summary>
    public void UnSelected()
    {
        IsSelected = false;
        UnSelectedEvent();
    }
    protected virtual void UnSelectedEvent() { }

    public void Current()
    {
        IsCurrent = true;
        CurrentEvent();
    }
    protected virtual void CurrentEvent() { }

    public void UnCurrent()
    {
        IsCurrent = false;
        UnCurrentEvent();
    }
    protected virtual void UnCurrentEvent() { }


    public int JoystickSize { get { return (Rect.Width + Rect.Height) / 20 + 1; } }

    static Brush JoystickCurrent = new SolidBrush(Color.FromArgb(230, 255, 255, 255));
    static Brush JoystickSelect = new SolidBrush(Color.FromArgb(230, 50, 50, 50));

    /// <summary>
    /// 绘制八个操纵柄
    /// </summary>
    /// <param name="painter"></param>
    public void DrawingJoystick(Graphics g)
    {
        if (IsSelected == false) return;

        var cX = Rect.Width / 2;
        var cY = Rect.Height / 2;
        var s = JoystickSize;
        if (IsCurrent == false)
        {
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X, Rect.Y + (0), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (cX - s / 2), Rect.Y + (0), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (0), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (cY - s / 2), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (Rect.Height - s), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (cX - s / 2), Rect.Y + (Rect.Height - s), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (0), Rect.Y + (Rect.Height - s), s, s));
            g.FillRectangle(JoystickSelect, Viewer.LocalToShow(Rect.X + (0), Rect.Y + (cY - s / 2), s, s));
        }
        else if (IsCurrent == true)
        {
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X, Rect.Y + (0), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (cX - s / 2), Rect.Y + (0), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (0), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (cY - s / 2), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (Rect.Height - s), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (cX - s / 2), Rect.Y + (Rect.Height - s), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (0), Rect.Y + (Rect.Height - s), s, s));
            g.FillRectangle(JoystickCurrent, Viewer.LocalToShow(Rect.X + (0), Rect.Y + (cY - s / 2), s, s));

            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X, Rect.Y + (0), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (cX - s / 2), Rect.Y + (0), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (0), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (cY - s / 2), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (Rect.Width - s), Rect.Y + (Rect.Height - s), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (cX - s / 2), Rect.Y + (Rect.Height - s), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (0), Rect.Y + (Rect.Height - s), s, s));
            g.DrawRectangle(Pens.Black, Viewer.LocalToShow(Rect.X + (0), Rect.Y + (cY - s / 2), s, s));

        }
    }



}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值