Recent

Author Topic: TScrollBox and TImage , Pan algorithm  (Read 2695 times)

Tommi

  • Full Member
  • ***
  • Posts: 213
TScrollBox and TImage , Pan algorithm
« on: March 24, 2018, 10:07:55 am »
I have a TImage inserted in  a TScrollBox and I would like to implement a PAN function when I move the mouse on the image.

Here my actual code:
Code: [Select]
procedure TForm6.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
 // form6.Caption:=inttostr(X)+'   '+inttostr(Y);
  if ssLeft in Shift then
  begin
    ScrollBox1.VertScrollBar.Position:=Y-trunc(scrollBox1.ClientHeight/2);
    scrollBox1.HorzScrollBar.Position:=X-trunc(scrollBox1.ClientWidth/2);
  end;
end;

It works but it isn't a real pan function. My intent is to keep the image attached to the mouse while it is moving.

Could someone help me ?

Thank you

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: TScrollBox and TImage , Pan algorithm
« Reply #1 on: March 24, 2018, 12:13:03 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Image1: TImage;
  16.     ScrollBox1: TScrollBox;
  17.     procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
  18.       Shift: TShiftState; X, Y: Integer);
  19.     procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
  20.       );
  21.     procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
  22.       Shift: TShiftState; X, Y: Integer);
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.     Panning:Boolean;
  28.     PanX, PanY:Integer;
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  41.   Shift: TShiftState; X, Y: Integer);
  42. begin
  43.   Panning := True;
  44.   PanX := X;
  45.   PanY := Y;
  46. end;
  47.  
  48. procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  49.   Y: Integer);
  50. begin
  51.   if Panning then begin
  52.     ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position + PanX - X;
  53.     ScrollBox1.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position + PanY - Y;
  54.   end;
  55. end;
  56.  
  57. procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  58.   Shift: TShiftState; X, Y: Integer);
  59. begin
  60.   Panning := False;
  61. end;
  62.  
It's assumed, that TScrollBox has some internal Position value range handling. It works, but only bad thing - it looks like, that if one of scroll bars isn't visible, it's width is still being added to virtual with or height of TScrollBox client area.
« Last Edit: March 24, 2018, 12:58:17 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: TScrollBox and TImage , Pan algorithm
« Reply #2 on: March 24, 2018, 01:38:07 pm »
Lets see..

 Lets assume for the moment you use a mouse click on the image, every time you do this you can record the
Point using a Tpoint. This gives you the starting point and you should record to what the scrollbox.client locations are
not the timage using the MOUSE record, this gives you screen coordinates.

 As you process the mouse moves while the mouse is being held down, you then take another reading from the
MOUSE record, calculate the offset difference in movement from the starting point when you initiated the click.
 
 Reposition the Timage Left,Top using the these offsets + or - and you'll get your panning effects..

 Just remember that when you release the mouse, you need to restore the LEFT,TOP of the timage back to 0,0
or where ever you want it.
The only true wisdom is knowing you know nothing

Tommi

  • Full Member
  • ***
  • Posts: 213
Re: TScrollBox and TImage , Pan algorithm
« Reply #3 on: March 25, 2018, 09:12:35 am »
Ok, thank you. It works.

I just changed:

Code: [Select]
  if Panning then begin
    ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position + PanX - X;
    ScrollBox1.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position + PanY - Y;
  end;

with:

Code: [Select]
  if ssLeft in Shift then
  begin
    ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position + PanX - X;
    ScrollBox1.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position + PanY - Y;
  end;

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: TScrollBox and TImage , Pan algorithm
« Reply #4 on: March 26, 2018, 09:54:56 am »
Ok, thank you. It works.

I just changed:

Code: [Select]
  if Panning then begin
    ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position + PanX - X;
    ScrollBox1.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position + PanY - Y;
  end;

with:

Code: [Select]
  if ssLeft in Shift then
  begin
    ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position + PanX - X;
    ScrollBox1.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position + PanY - Y;
  end;
Bad idea, as X and Y are set inside OnMouseDown and your code will pan image even without processing this event.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

 

TinyPortal © 2005-2018