Recent

Author Topic: [SOLVED] Slider TPanel  (Read 2490 times)

Pe3s

  • Jr. Member
  • **
  • Posts: 96
[SOLVED] Slider TPanel
« on: November 14, 2021, 08:51:29 pm »
Hello, I wrote a slider using TPanel components, I'm stuck at the end of the slider does not show 100% how I can do it. How to modify the code so that you can assign a final value, e.g. 100, 200
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ComCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ProgressBar1: TProgressBar;
  17.     SeekBarSlider: TPanel;
  18.     SeekBar: TPanel;
  19.     SeekBarFrame: TPanel;
  20.     procedure SeekBarFrameMouseDown(Sender: TObject; Button: TMouseButton;
  21.       Shift: TShiftState; X, Y: Integer);
  22.     procedure SeekBarMouseDown(Sender: TObject; Button: TMouseButton;
  23.       Shift: TShiftState; X, Y: Integer);
  24.     procedure SeekBarSliderMouseDown(Sender: TObject; Button: TMouseButton;
  25.       Shift: TShiftState; X, Y: Integer);
  26.     procedure SeekBarSliderMouseMove(Sender: TObject; Shift: TShiftState; X,
  27.       Y: Integer);
  28.     procedure SeekBarSliderMouseUp(Sender: TObject; Button: TMouseButton;
  29.       Shift: TShiftState; X, Y: Integer);
  30.   private
  31.  
  32.   public
  33.  
  34.   end;
  35.  
  36. var
  37.   Form1: TForm1;
  38.   Seeking: Boolean;
  39.   SeekMouseX, SeekBaseX, MaxPos: Integer;
  40.  
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45. { TForm1 }
  46.  
  47. procedure TForm1.SeekBarFrameMouseDown(Sender: TObject; Button: TMouseButton;
  48.   Shift: TShiftState; X, Y: Integer);
  49. var MaxPos: Integer;
  50. begin
  51.   if not SeekBarSlider.Visible then Exit;
  52.   dec(X, SeekBarSlider.Width DIV 2);
  53.   MaxPos := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  54. end;
  55.  
  56. procedure TForm1.SeekBarMouseDown(Sender: TObject; Button: TMouseButton;
  57.   Shift: TShiftState; X, Y: Integer);
  58. var P: TPoint;
  59. begin
  60.   P := SeekBarFrame.ScreenToClient(SeekBar.ClientToScreen(Point(X, Y)));
  61.   SeekBarFrameMouseDown(Sender, Button, Shift, P.X, P.Y);
  62. end;
  63.  
  64. procedure TForm1.SeekBarSliderMouseDown(Sender: TObject; Button: TMouseButton;
  65.   Shift: TShiftState; X, Y: Integer);
  66. begin
  67.   if Button <> mbLeft then Exit;
  68.   Seeking := True;
  69.   SeekBarSlider.BevelInner := bvLowered;
  70.   SeekMouseX := X;
  71.   SeekBaseX := SeekBarSlider.Left;
  72. end;
  73.  
  74. procedure TForm1.SeekBarSliderMouseMove(Sender: TObject; Shift: TShiftState; X,
  75.   Y: Integer);
  76. var NewX, MaxX: Integer;
  77. begin
  78.   if not (ssLeft in Shift) OR not (Seeking) then Exit;
  79.   NewX := X - SeekMouseX + SeekBarSlider.Left;
  80.   MaxX := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  81.   if NewX < 0 then NewX := 0;
  82.   if NewX > MaxX then NewX := MaxX;
  83.   SeekBarSlider.Left := NewX;
  84.  
  85.     ProgressBar1.Position:= NewX;
  86.     Form1.Caption := IntToStr(NewX);
  87. end;
  88.  
  89. procedure TForm1.SeekBarSliderMouseUp(Sender: TObject; Button: TMouseButton;
  90.   Shift: TShiftState; X, Y: Integer);
  91. var MaxPos: Integer;
  92. begin
  93.   if Button <> mbLeft then Exit;
  94.   Seeking := False;
  95.   SeekBarSlider.BevelInner := bvRaised;
  96.   MaxPos := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  97. end;
  98.  
  99. end.
  100.  
  101.  
« Last Edit: November 21, 2021, 12:40:21 pm by Pe3s »

Tony Stone

  • Full Member
  • ***
  • Posts: 124
Re: Slider TPanel
« Reply #1 on: November 19, 2021, 06:09:27 am »
I played with your demo and came up with this.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ComCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ProgressBar1: TProgressBar;
  17.     SeekBarSlider: TPanel;
  18.     SeekBar: TPanel;
  19.     SeekBarFrame: TPanel;
  20.     procedure SeekBarFrameMouseDown(Sender: TObject; Button: TMouseButton;
  21.       Shift: TShiftState; X, Y: Integer);
  22.     procedure SeekBarMouseDown(Sender: TObject; Button: TMouseButton;
  23.       Shift: TShiftState; X, Y: Integer);
  24.     procedure SeekBarSliderMouseDown(Sender: TObject; Button: TMouseButton;
  25.       Shift: TShiftState; X, Y: Integer);
  26.     procedure SeekBarSliderMouseMove(Sender: TObject; Shift: TShiftState; X,
  27.       Y: Integer);
  28.     procedure SeekBarSliderMouseUp(Sender: TObject; Button: TMouseButton;
  29.       Shift: TShiftState; X, Y: Integer);
  30.     function CalcProgress(Pos64, Max64: int64): integer;
  31.   private
  32.  
  33.   public
  34.  
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.   Seeking: Boolean;
  40.   SeekMouseX, SeekBaseX, MaxPos: Integer;
  41.   newx, maxx, perc: Int64;
  42.  
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm1 }
  49.  
  50. procedure TForm1.SeekBarFrameMouseDown(Sender: TObject; Button: TMouseButton;
  51.   Shift: TShiftState; X, Y: Integer);
  52.  
  53. begin
  54.   if not SeekBarSlider.Visible then Exit;
  55.   dec(X, SeekBarSlider.Width DIV 2);
  56.   //MaxPos := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  57. end;
  58.  
  59. procedure TForm1.SeekBarMouseDown(Sender: TObject; Button: TMouseButton;
  60.   Shift: TShiftState; X, Y: Integer);
  61. var P: TPoint;
  62. begin
  63.   P := SeekBarFrame.ScreenToClient(SeekBar.ClientToScreen(Point(X, Y)));
  64.   SeekBarFrameMouseDown(Sender, Button, Shift, P.X, P.Y);
  65. end;
  66.  
  67. procedure TForm1.SeekBarSliderMouseDown(Sender: TObject; Button: TMouseButton;
  68.   Shift: TShiftState; X, Y: Integer);
  69. begin
  70.   if Button <> mbLeft then Exit;
  71.   Seeking := True;
  72.   SeekBarSlider.BevelInner := bvLowered;
  73.   SeekMouseX := X;
  74.   SeekBaseX := SeekBarSlider.Left;
  75. end;
  76.  
  77. procedure TForm1.SeekBarSliderMouseMove(Sender: TObject; Shift: TShiftState; X,
  78.   Y: Integer);
  79.  
  80. begin
  81.   if not (ssLeft in Shift) OR not (Seeking) then Exit;
  82.   NewX := X - SeekMouseX + SeekBarSlider.Left;
  83.   MaxX := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  84.   if NewX < 0 then NewX := 0;
  85.   if NewX > MaxX then NewX := MaxX;
  86.  
  87.   SeekBarSlider.Left := NewX;
  88.  
  89.  
  90.   perc:=CalcProgress(newx,maxx);
  91.  
  92.     ProgressBar1.Position:= (perc);
  93.     Form1.Caption := IntToStr(perc);
  94.  
  95.  
  96.  
  97. end;
  98.  
  99. function TForm1.CalcProgress(Pos64, Max64: int64): integer;
  100. begin
  101.  
  102.   Result := integer(Trunc((Pos64 / Max64) * 100));
  103.  
  104. end;
  105.  
  106. procedure TForm1.SeekBarSliderMouseUp(Sender: TObject; Button: TMouseButton;
  107.   Shift: TShiftState; X, Y: Integer);
  108.  
  109. begin
  110.   if Button <> mbLeft then Exit;
  111.   Seeking := False;
  112.   SeekBarSlider.BevelInner := bvRaised;
  113.   //MaxPos := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  114. end;
  115.  
  116. end.
  117.  
  118.  

Pe3s

  • Jr. Member
  • **
  • Posts: 96
Re: Slider TPanel
« Reply #2 on: November 19, 2021, 10:23:05 am »
@Tony Stone Thank you, I still have a question, it can be done so as to assign a final value, for example, the length of the film or music.

Tony Stone

  • Full Member
  • ***
  • Posts: 124
Re: Slider TPanel
« Reply #3 on: November 19, 2021, 04:53:37 pm »
I'm not so sure I understand your question.  Give me a clear example and I'll try to help.

Pe3s

  • Jr. Member
  • **
  • Posts: 96
Re: Slider TPanel
« Reply #4 on: November 19, 2021, 05:44:14 pm »
I wanted to make a custom slider for a PasLIbVLC based program in this component
OnMediaPlayerTimeChanged procedure responsible for moving the bar during playback and here there is a problem with the bar moving too fast.

Tony Stone

  • Full Member
  • ***
  • Posts: 124
Re: Slider TPanel
« Reply #5 on: November 19, 2021, 09:00:30 pm »
I wanted to make a custom slider for a PasLIbVLC based program in this component
OnMediaPlayerTimeChanged procedure responsible for moving the bar during playback and here there is a problem with the bar moving too fast.

Ahh, I think I know what you are trying to do.  Give me a little bit.  I will use your demo and see if i can make something work.  So The progress bar can be representitive of 600 to mean 600 seconds for example?  Is that what you mean? 

Pe3s

  • Jr. Member
  • **
  • Posts: 96
Re: Slider TPanel
« Reply #6 on: November 19, 2021, 09:48:30 pm »
The progress bar should adjust to the length of the movie.
For example, if we use the ProgressBar component and use the properties of ProgressBar.Max: = PasLibVlcPlayer1.GetVideoLenInMs (); // The length of the movie
and
ProgressBar.Position: = PasLibVlcPlayer1.GetVideoPosInMs (); // Movie position

I want to do something like that on my belt.
I have included a second demo from ProgressBar for the visualization

Tony Stone

  • Full Member
  • ***
  • Posts: 124
Re: Slider TPanel
« Reply #7 on: November 20, 2021, 01:04:40 am »
try something like this....


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ComCtrls,
  9.   StdCtrls, Buttons, PasLibVlcPlayerUnit, vlc;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     OpenDialog1: TOpenDialog;
  17.     PasLibVlcPlayer1: TPasLibVlcPlayer;
  18.     SeekBarSlider: TPanel;
  19.     SeekBar: TPanel;
  20.     SeekBarFrame: TPanel;
  21.     SpeedButton1: TSpeedButton;
  22.     SpeedButton2: TSpeedButton;
  23.     SpeedButton3: TSpeedButton;
  24.     SpeedButton4: TSpeedButton;
  25.  
  26.     procedure PasLibVlcPlayer1MediaPlayerLengthChanged(Sender: TObject;
  27.       time: Int64);
  28.     procedure PasLibVlcPlayer1MediaPlayerTimeChanged(Sender: TObject;
  29.       time: Int64);
  30.     procedure SeekBarFrameMouseDown(Sender: TObject; Button: TMouseButton;
  31.       Shift: TShiftState; X, Y: Integer);
  32.     procedure SeekBarMouseDown(Sender: TObject; Button: TMouseButton;
  33.       Shift: TShiftState; X, Y: Integer);
  34.     procedure SeekBarSliderMouseDown(Sender: TObject; Button: TMouseButton;
  35.       Shift: TShiftState; X, Y: Integer);
  36.     procedure SeekBarSliderMouseMove(Sender: TObject; Shift: TShiftState; X,
  37.       Y: Integer);
  38.     procedure SeekBarSliderMouseUp(Sender: TObject; Button: TMouseButton;
  39.       Shift: TShiftState; X, Y: Integer);
  40.     procedure SpeedButton1Click(Sender: TObject);
  41.     procedure SpeedButton2Click(Sender: TObject);
  42.     procedure SpeedButton3Click(Sender: TObject);
  43.     procedure SpeedButton4Click(Sender: TObject);
  44.   private
  45.  
  46.   public
  47.  
  48.   end;
  49.  
  50. var
  51.   Form1: TForm1;
  52.   Seeking: Boolean;
  53.   SeekMouseX, SeekBaseX, MaxPos, TotalVideoPos, seekPerc, perc: Integer;
  54.   vidlenMS, NewX, MaxX, perc64: Int64;
  55.  
  56. implementation
  57.  
  58. {$R *.lfm}
  59.  
  60. { TForm1 }
  61.  
  62. procedure TForm1.SeekBarFrameMouseDown(Sender: TObject; Button: TMouseButton;
  63.   Shift: TShiftState; X, Y: Integer);
  64. begin
  65.   if not SeekBarSlider.Visible then Exit;
  66.   dec(X, SeekBarSlider.Width DIV 2);
  67. end;
  68.  
  69. procedure TForm1.PasLibVlcPlayer1MediaPlayerLengthChanged(Sender: TObject;
  70.   time: Int64);
  71. begin
  72.   TotalVideoPos := PasLibVlcPlayer1.GetVideoLenInMs();
  73. end;
  74.  
  75. procedure TForm1.PasLibVlcPlayer1MediaPlayerTimeChanged(Sender: TObject;
  76.   time: Int64);
  77. begin
  78.   //SeekBarSlider.Left := PasLibVlcPlayer1.GetVideoPosInMs();
  79. end;
  80.  
  81. procedure TForm1.SeekBarMouseDown(Sender: TObject; Button: TMouseButton;
  82.   Shift: TShiftState; X, Y: Integer);
  83. var P: TPoint;
  84. begin
  85.   P := SeekBarFrame.ScreenToClient(SeekBar.ClientToScreen(Point(X, Y)));
  86.   SeekBarFrameMouseDown(Sender, Button, Shift, P.X, P.Y);
  87. end;
  88.  
  89. procedure TForm1.SeekBarSliderMouseDown(Sender: TObject; Button: TMouseButton;
  90.   Shift: TShiftState; X, Y: Integer);
  91. begin
  92.   if Button <> mbLeft then Exit;
  93.   Seeking := True;
  94.   SeekBarSlider.BevelInner := bvLowered;
  95.   SeekMouseX := X;
  96.   SeekBaseX := SeekBarSlider.Left;
  97. end;
  98.  
  99. procedure TForm1.SeekBarSliderMouseMove(Sender: TObject; Shift: TShiftState; X,
  100.   Y: Integer);
  101. begin
  102.   if not (ssLeft in Shift) OR not (Seeking) then Exit;
  103.   NewX := X - SeekMouseX + SeekBarSlider.Left;
  104.   MaxX := SeekBarFrame.ClientWidth - SeekBarSlider.Width;
  105.   if NewX < 0 then NewX := 0;
  106.   if NewX > MaxX then NewX := MaxX;
  107.   SeekBarSlider.Left := NewX;
  108.  
  109.   vidlenMS:=PasLibVlcPlayer1.GetVideoLenInMs;
  110.   if NewX > 0 then begin
  111.     seekPerc:=(vidlenMS div MaxX) * NewX;
  112.   end;
  113.   Form1.Caption:='Length in MS: ' + IntToStr(vidlenMS) + '  Seek To:' + IntToStr(seekPerc)
  114.   + '   Percentage: ' + IntToStr(NewX);
  115. end;
  116.  
  117. procedure TForm1.SeekBarSliderMouseUp(Sender: TObject; Button: TMouseButton;
  118.   Shift: TShiftState; X, Y: Integer);
  119. begin
  120.   if Button <> mbLeft then Exit;
  121.   Seeking := False;
  122.   SeekBarSlider.BevelInner := bvRaised;
  123.   PasLibVlcPlayer1.SetVideoPosInMs(seekPerc);
  124. end;
  125.  
  126. procedure TForm1.SpeedButton1Click(Sender: TObject);
  127. begin
  128.   if OpenDialog1.Execute then
  129.   begin
  130.     PasLibVlcPlayer1.Play(WideString(OpenDialog1.FileName));
  131.  
  132.   end;
  133. end;
  134.  
  135. procedure TForm1.SpeedButton2Click(Sender: TObject);
  136. begin
  137.   PasLibVlcPlayer1.Resume();
  138. end;
  139.  
  140. procedure TForm1.SpeedButton3Click(Sender: TObject);
  141. begin
  142.   PasLibVlcPlayer1.Pause();
  143. end;
  144.  
  145. procedure TForm1.SpeedButton4Click(Sender: TObject);
  146. begin
  147.   PasLibVlcPlayer1.Stop();
  148. end;
  149.  
  150.  
  151.  
  152. end.
  153.  


at around line 110 where i set playback position you need to do the opposite in the actual time changed event to reposition your selector to the current play position.

There is probably much better ways to go about this but I am not the guy to make those recommendations!  :D

Hopefully this is what you were trying to do...?
« Last Edit: November 20, 2021, 01:12:54 am by Tony Stone »

Pe3s

  • Jr. Member
  • **
  • Posts: 96
Re: Slider TPanel
« Reply #8 on: November 20, 2021, 08:34:58 am »
I don't understand how to reverse to get the current play position.
To determine the current playback position is
OnMediaPlayerTimeChanged
SeekbarSlider.Left: = PasLibVlcPlayer1.GetVideoPosInMs ();
But that doesn't work with this slider
« Last Edit: November 20, 2021, 11:55:47 am by Pe3s »

jamie

  • Hero Member
  • *****
  • Posts: 5129
Re: Slider TPanel
« Reply #9 on: November 20, 2021, 03:47:47 pm »
Its basically a ratio you need ..

  SliderMaxPixelWidth * PlayerCurrentTime / PlayerTotalTime;

 
With that you can have a slide of lets say a 100 Pixels wide and the ratio of that will dictate the position.

The only true wisdom is knowing you know nothing

Tony Stone

  • Full Member
  • ***
  • Posts: 124
Re: Slider TPanel
« Reply #10 on: November 20, 2021, 03:48:27 pm »
I don't understand how to reverse to get the current play position.
To determine the current playback position is
OnMediaPlayerTimeChanged
SeekbarSlider.Left: = PasLibVlcPlayer1.GetVideoPosInMs ();
But that doesn't work with this slider

You need to do the same thing I did on line 110.  Except use current video position instead of slider newx....

Like I said there is probably better ways to do this.

But try something like that and you may realize some issues and see a better way to go about it.  :D

I am a total idiot with simple math as I get older so it took me a little bit to figure that out in the first place.  It's pretty simple though really...  Good luck.

Pe3s

  • Jr. Member
  • **
  • Posts: 96
Re: Slider TPanel
« Reply #11 on: November 20, 2021, 04:18:32 pm »
Napisalem cos takiego ale pojawiaja sie bledy
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PasLibVlcPlayer1MediaPlayerTimeChanged(Sender: TObject;
  2.   time: Int64);
  3. begin
  4.   SeekBarSlider.Left := (vidlenMS div MaxX) * PasLibVlcPlayer1.GetVideoPosInMs();
  5. end;

Code: Pascal  [Select][+][-]
  1.   if PasLibVlcPlayer1.GetVideoPosInMs() > 0 then begin
  2.   SeekBarSlider.Left := (vidlenMS div MaxX) * PasLibVlcPlayer1.GetVideoPosInMs();
  3. end;
  4.   end;

Tony Stone

  • Full Member
  • ***
  • Posts: 124
Re: Slider TPanel
« Reply #12 on: November 20, 2021, 04:22:43 pm »
Look what Jamie posted.  His little formula is correct, not what I said.  I'm not in front of a computer right now to test and do it myself. Sorry.

Pe3s

  • Jr. Member
  • **
  • Posts: 96
Re: Slider TPanel
« Reply #13 on: November 20, 2021, 05:08:09 pm »
Code: Pascal  [Select][+][-]
  1.  vidlenPS := PasLibVlcPlayer1.GetVideoPosInMs();
  2.   SeekBarSlider.Left := MaxX * vidlenPS div vidlenMS;

Error division by zero

Pe3s

  • Jr. Member
  • **
  • Posts: 96
[SOLVED] Re: Slider TPanel
« Reply #14 on: November 20, 2021, 06:39:29 pm »
Thanks  :)

 

TinyPortal © 2005-2018