PDA

View Full Version : moving a rectangle with a mouse



edexter
04-10-2008, 05:09 PM
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.

AthenaOfDelphi
04-10-2008, 07:01 PM
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.


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;


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.

edexter
05-10-2008, 01:25 AM
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..

AthenaOfDelphi
05-10-2008, 08:01 AM
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.

edexter
05-10-2008, 03:44 PM
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

Andreaz
05-10-2008, 08:10 PM
I'm not really shure what you're after.

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



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;

edexter
06-10-2008, 04:13 PM
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]

User137
07-10-2008, 12:34 PM
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.