Пример #1
0
        void Item_Event(object obj, Gnome.CanvasEventArgs args)
        {
            EventButton ev   = new EventButton(args.Event.Handle);
            CanvasRE    item = (CanvasRE)obj;

            switch (ev.Type)
            {
            case EventType.ButtonPress:
                if (ev.Button == 1)
                {
                    remember_x  = ev.X;
                    remember_y  = ev.Y;
                    args.RetVal = true;
                    return;
                }
                else if (ev.Button == 3)
                {
                    item.Destroy();
                    args.RetVal = true;
                    return;
                }
                break;

            case EventType.TwoButtonPress:
                ChangeItemColor(item);
                args.RetVal = true;
                return;

            case EventType.MotionNotify:
                Gdk.ModifierType state = (Gdk.ModifierType)ev.State;
                if ((state & Gdk.ModifierType.Button1Mask) != 0)
                {
                    double new_x = ev.X, new_y = ev.Y;
                    item.Move(new_x - remember_x, new_y - remember_y);
                    remember_x  = new_x;
                    remember_y  = new_y;
                    args.RetVal = true;
                    return;
                }
                break;

            case EventType.EnterNotify:
                item.WidthUnits = 3.0;
                args.RetVal     = true;
                return;

            case EventType.LeaveNotify:
                item.WidthUnits = 1.0;
                args.RetVal     = true;
                return;
            }

            args.RetVal = false;
            return;
        }
Пример #2
0
    static void Piece_Event(object o, Gnome.CanvasEventArgs args)
    {
        BoardPiece piece = (BoardPiece)o;
        Canvas     canvas = piece.Canvas;
        double     dx = 0.0, dy = 0.0;

        switch (args.Event.Type)
        {
        case EventType.EnterNotify:
            piece.Text.FillColor = "white";
            args.RetVal          = true;
            break;

        case EventType.LeaveNotify:
            piece.Text.FillColor = "black";
            args.RetVal          = true;
            break;

        case EventType.ButtonPress:
            int y = piece.Position / 4;
            int x = piece.Position % 4;
            Print_Position("from", piece.Position, true);

            bool toMove = true;

            if ((y > 0) && (piece.Board [(y - 1) * 4 + x] == null))
            {
                dx = 0.0;
                dy = -1.0;
                y--;
            }
            else if ((y < 3) && (piece.Board [(y + 1) * 4 + x] == null))
            {
                dx = 0.0;
                dy = 1.0;
                y++;
            }
            else if ((x > 0) && (piece.Board [y * 4 + x - 1] == null))
            {
                dx = -1.0;
                dy = 0.0;
                x--;
            }
            else if ((x < 3) && (piece.Board [y * 4 + x + 1] == null))
            {
                dx = 1.0;
                dy = 0.0;
                x++;
            }
            else
            {
                toMove = false;
            }

            if (toMove)
            {
                int new_position = y * 4 + x;
                Print_Position("to", new_position, false);
                piece.Board [piece.Position] = null;
                piece.Position = new_position;
                piece.Move(dx * PIECE_SIZE, dy * PIECE_SIZE);
                canvas.UpdateNow();
            }
            else
            {
                Print_Position("to", piece.Position, false);
            }

            args.RetVal = true;
            break;

        default:
            args.RetVal = false;
            break;
        }
    }