Sign in to follow this  
djnevs

Sprite Dragging

Recommended Posts

djnevs    100
The same sprite as this topic's about. now, i want to drag the sprite.. we (fire2burn and me) have bin working for 2 houres now to get this fixed. we got the following code:
public void OnPaint(Device device, string title, string text)
    {
        // if mouse down and drag window, x and y pos changes
        cMouse muis = new cMouse();
        muis.Update();
        if (muis.LeftButtonDown)
        {
            int curx = cTools.FormHandle.PointToClient(Cursor.Position).X;
            int cury = cTools.FormHandle.PointToClient(Cursor.Position).Y;

            if (cTools.FormHandle.PointToClient(Cursor.Position).X > xx &&
                cTools.FormHandle.PointToClient(Cursor.Position).X <= 350 + xx &&
                cTools.FormHandle.PointToClient(Cursor.Position).Y >= yy &&
                cTools.FormHandle.PointToClient(Cursor.Position).Y <= 150 + yy)
            {

                if (!Dragger)
                {
                    tempx = cTools.FormHandle.PointToClient(Cursor.Position).X;
                    tempy = cTools.FormHandle.PointToClient(Cursor.Position).Y;
                    Dragger = true;
                }
                else
                {
                    xx = curx - tempx; //curx = tempx
                    yy = cury - tempy; // cury = tempy

                }
            }
        }
        else
        {
            Dragger = false;
        }

        frmSprite.OnPaint(device,xx,yy,350,150); // 350, 150
        frmButton.OnPaint(cTools.FormHandle, new System.Drawing.PointF((xx + 350) - 110, yy + 97), new System.Drawing.SizeF(87, 29), "OK");
        frmCaption.OnPaint(title, xx + 40, yy + 13, System.Drawing.Color.White);
        frmText.OnPaint(xx.ToString() + "," + tempy.ToString(), xx + 25, yy + 40, System.Drawing.Color.Black);
        //frmText.OnPaint(text, xx + 25, yy + 40, System.Drawing.Color.Black);

    }
we have problems with these 2 lines
xx = curx - tempx; //curx = tempx
yy = cury - tempy; // cury = tempy
cuz, cury and tempy have the same value.. same with curx and tempx.. Kind regards djnevs and Fire2Burn

Share this post


Link to post
Share on other sites
jollyjeffers    1570
You might be rendering via D3D, but I don't see anything DX specific to your question. Looks like a simple case of debugging some variables and logic - moved to 'For Beginners'.

There are various bits of your code that don't make much sense:
  • You've creating a cMouse object but then appear to get mouse coordinates from Cursor.Position ??
  • Where are xx,yy,tempx,tempy declared - what other code reads/writes them?
  • If the values in your suspected statement are wrong, use your IDE to set up on-change breakpoints and see where they're getting changed. Then work out why they're getting changed.

    hth
    Jack

    Share this post


    Link to post
    Share on other sites
    djnevs    100
    full file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;

    public class cDialog
    {
    private cSprite frmSprite;
    private cText frmText;
    private cText frmCaption;
    private cButton frmButton;
    private int xx;
    private int yy;
    public void Create(Device device, int x, int y)
    {
    xx = x;
    yy = y;
    frmText = new cText();
    frmCaption = new cText();
    frmSprite = new cSprite();
    frmButton = new cButton();

    frmText.Create(device, "Arial", FontWeight.Normal,8);
    frmCaption.Create(device, "Tahoma", FontWeight.Bold,7);

    frmSprite.Create(device, Application.StartupPath + @"\Media\Images\Form\msgbox.png");

    frmButton.Create(device, Application.StartupPath + @"\Media\Images\Button\btnup.png", Application.StartupPath + @"\Media\Images\Button\btndown.png", Application.StartupPath + @"\Media\Images\Button\btnover.png");
    frmButton.OnClick += new cButton.EventDelegate(frmButton_OnClick);
    }

    public event EventDelegate OnClick;
    public delegate void EventDelegate(object sender, EventArgs args);
    public void issueEvent(EventArgs args)
    {
    OnClick(this, args);
    }

    void frmButton_OnClick(object sender, EventArgs args)
    {
    this.issueEvent(new EventArgs());
    }
    bool Dragger = false;
    int tempx, tempy;
    public void OnPaint(Device device, string title, string text, bool Draggable)
    {
    // if mouse down and drag window, x and y pos changes
    if (Draggable)
    {
    cMouse muis = new cMouse();
    muis.Update();
    if (muis.LeftButtonDown)
    {
    int curx = cTools.FormHandle.PointToClient(Cursor.Position).X;
    int cury = cTools.FormHandle.PointToClient(Cursor.Position).Y;

    if (cTools.FormHandle.PointToClient(Cursor.Position).X > xx &&
    cTools.FormHandle.PointToClient(Cursor.Position).X <= 350 + xx &&
    cTools.FormHandle.PointToClient(Cursor.Position).Y >= yy &&
    cTools.FormHandle.PointToClient(Cursor.Position).Y <= 150 + yy)
    {

    if (!Dragger)
    {
    tempx = cTools.FormHandle.PointToClient(Cursor.Position).X;
    tempy = cTools.FormHandle.PointToClient(Cursor.Position).Y;
    Dragger = true;
    }
    else
    {
    xx = curx - tempx; //curx = tempx
    yy = cury - tempy; // cury = tempy

    }
    }
    }
    else
    {
    Dragger = false;
    }
    }
    frmSprite.OnPaint(device,xx,yy,350,150); // 350, 150
    frmButton.OnPaint(cTools.FormHandle, new System.Drawing.PointF((xx + 350) - 110, yy + 97), new System.Drawing.SizeF(87, 29), "OK");
    frmCaption.OnPaint(title, xx + 40, yy + 13, System.Drawing.Color.White);
    frmText.OnPaint(xx.ToString() + "," + tempy.ToString(), xx + 25, yy + 40, System.Drawing.Color.Black);
    //frmText.OnPaint(text, xx + 25, yy + 40, System.Drawing.Color.Black);

    }

    public void Dispose()
    {

    }
    }

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this