Results 1 to 8 of 8

Thread: moving a rectangle with a mouse

  1. #1

    moving a rectangle with a mouse

    I was wanting to have a rectangle that moves with the mouse and has a min/max y pos.. I am having trouble fig that out.. any ideas.. It can be for any graphics library for free pascal.. I am just looking for examples.

    interface

    uses
    Classes, SysUtils,

    phxBase,
    phxScreen,
    phxCanvas,
    phxTypes;

    procedure MainLoop;

    implementation

    var Polygon: Array[0..2] of TVector2i = (
    (X: 200; Y: 200),
    (X: 300; Y: 200),
    (X: 300; Y: 300)
    );

    //------------------------------------------------------------------------------
    procedure MainLoop;
    var Screen: TPHXScreen;
    var Canvas: TPHXCanvas;
    begin
    // Get the window
    Screen:= TPHXScreen.getInstance();

    // Open the window
    Screen.Open('Phoenix Canvas Demo', -1, -1, 800, 600);


    Canvas:= TPHXCanvas.Canvas;
    repeat
    // Clear the window
    Screen.Clear();

    Canvas.Pen.Color:= clrPurple;




    Canvas.Rectangle(10,10, 50,50);




    // Flip the buffers
    Screen.Flip();

    until (Screen.Visible = False) ;

    end;

    end.
    http://dexrow.blogspot.com/2007/09/land-of-cigo-roguelike-w-source.html

  2. #2
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,246
    Blog Entries
    2

    moving a rectangle with a mouse

    Hi edexter,

    This is just some basic maths from what I can see.

    If my understanding is correct, you want to calculate the position of a rectangle (top left = RX1,RY1, bottom right = RX2, RY2) based on some mouse coordinates (mx,my) and some valid range (minX, minY, maxX, maxY) where the rectangle can be. Is that about right?

    If so, I think this will work. Note:- When the entire rectangle is the allowed range, the center will follow the mouse.

    [pascal]
    procedure getRectangleCoords(mx,my:integer;minX,minY,maxX,ma xY:integer;width,height:integer;var rx1,ry1,rx2,ry2:integer);
    begin
    rx1:=mx-(width div 2);
    ry1:=my-(height div 2);
    rx2:=rx1+width;
    ry2:=ry1+height;

    if (rx1<minX>maxX) then rx1:=maxX-width;
    if (ry1<minY>maxY) then ry1:=maxY-height;

    rx2:=rx1+width;
    ry2:=ry1+height;
    end;
    [/pascal]

    So, quick explanation... calculate an initial top left corner position, then the bottom right position. Then check the coordinates fit into the bounding box and adjust the top left corner accordingly if they don't. The final stage, recalculate the bottom right corner based on the revised top left corner coordinates.

    As for graphics libraries relating to freepascal, I can't help I'm afraid.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  3. #3

    moving a rectangle with a mouse

    That sounds like something cool I can throw in my toolbox but not quite what I am after...

    What I want to do is load some text from a disk and use that as my starting values and draw a series of rectangles that can slide verticly on the screen/... I was wanting it for some graphics I am doing for csound but maybe I can do it so it is program independent so that I can find other uses for it..

    move ..........instr .....move

    exc.. exc sorta

    the docs are sorta sparce any package will do..
    http://dexrow.blogspot.com/2007/09/land-of-cigo-roguelike-w-source.html

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,246
    Blog Entries
    2

    moving a rectangle with a mouse

    Quote Originally Posted by edexter
    That sounds like something cool I can throw in my toolbox but not quite what I am after...

    What I want to do is load some text from a disk and use that as my starting values and draw a series of rectangles that can slide verticly on the screen/... I was wanting it for some graphics I am doing for csound but maybe I can do it so it is program independent so that I can find other uses for it..

    move ..........instr .....move

    exc.. exc sorta

    the docs are sorta sparce any package will do..
    If there is an example of what it is you want to achieve on-line, a video, an example in a program, or something in a common game, then point us in the direction of it. At the moment you see, I'm thinking you want to do film style credits with the text rolling up the screen.

    Once there's a clear idea of what you want to do, you should find it easier to get help.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  5. #5

    moving a rectangle with a mouse

    csound blue (java) has something where a csound instruments score can be moved and the start and the stop time of the score is changed according to where the user places it (It is a long skinny rectangle).. A simular device is found amoung many sound editors only I don't realy need to know what a wav file looks like though just a simple retanglar shap..

    hopefully that helps
    http://dexrow.blogspot.com/2007/09/land-of-cigo-roguelike-w-source.html

  6. #6

    moving a rectangle with a mouse

    I'm not really shure what you're after.

    But getting the mouse coordinates in phoenix is done via the input class:

    [pascal]

    uses phxInput;

    var Input: TPHXInput;
    ..

    Input:= TPHXInput.Create;

    Canvas:= TPHXCanvas.Create
    while true do

    Canvas.Rectangle(Input.Mouse.X - 10, Input.Mouse.Y - 10, Mouse.Input.Mouse.X + 10, Input.Mouse.Y + 10);
    end;

    [/pascal]
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  7. #7

    moving a rectangle with a mouse

    Code:
    uses phxInput,
         phxCanvas;
    
    var Input&#58;  TPHXInput; 
    var Canvas&#58; TPHXCanvas;
    
    begin
    
    Input&#58;= TPHXInput.Create; 
    
    Canvas&#58;= TPHXCanvas.Create; 
    while true do begin;
    
      Canvas.Rectangle&#40;Input.Mouse.X - 10, Input.Mouse.Y - 10, Mouse.Input.Mouse.X + 10, Input.Mouse.Y + 10&#41;; 
    end; 
    
    end.
    I am getting mouse the mouse is an unidentified var... will I be able to turn this into a custom slider.[/code]
    http://dexrow.blogspot.com/2007/09/land-of-cigo-roguelike-w-source.html

  8. #8

    moving a rectangle with a mouse

    So, if i understood right you want to make a custom vertical Scrollbar?

    In that case it has few steps:
    1.You need to know cursor position on each frame, and store the location at the end of the frame. At beginning of new frame you calculate delta with MouseY-oldMouseY (this is how much the mouse moved)
    2.When mouse is first pressed down: Calculate if cursor hits the scrollbar rectangle. If it does, set a new variable Scrolling:=true.
    3.When mouse is moved and Scrolling = true, move the rectangle vertically by delta.
    4.When mouse is released: Set variable scrolling:=false again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •