Jump to content
Linuxuser1234

How to rotate a RoundRect on click

Recommended Posts

I made a RoundRect to look like a tune Knob and i would like the knob to rotate on click  so how can i write a Onclick procedure to rotate the round rect at different angles 1185464267_Screenshot2022-11-08115618.thumb.png.3357f0f5bdad3f2d3160262f2281da6c.png

Share this post


Link to post

 

procedure TForm7.KnobClick(Sender: TObject);
begin
//Knob
property RotationAngle: Single read GetRotationAngle write SetRotationAngle;

end;

@Rollo62 this causes Expected END but received Property 

Edited by Linuxuser1234

Share this post


Link to post
procedure TForm7.FormCreate(Sender: TObject);
begin
    MyRoundRect.RotationCenter.Point( Pointf( MyRoundRect.Position.X + MyRoundRect.Width / 2.0,  MyRoundRect.Position.Y + MyRoundRect.Height / 2.0 ) ); 
end;

procedure TForm7.KnobLeftClick(Sender: TObject);
begin
    MyRoundRect.RotationAngle := MyRoundRect.RotationAngle - 15.0; 
end;

procedure TForm7.KnobRightClick(Sender: TObject);
begin
    MyRoundRect.RotationAngle := MyRoundRect.RotationAngle + 15.0; 
end;

Something similar like this (Untested) ?

Or even adding some smooth animation by TFloatAnimation, like this.

Edited by Rollo62
  • Like 1

Share this post


Link to post

@Rollo62  also how would i change   a tlabel text based on what angle the knob is set to for example if the angle is at 15deg

the tlabel would  display another text and i do know how to set the text just not sure how to set the text based off the knob angle 

Edited by Linuxuser1234

Share this post


Link to post
function TForm7.GetMyStationNameFromAngleMethod( const AAngle : Single ) : String;
const
    CStations : array[0..24-1] of String = (  'New York, 'Honolulu', 'Tokyo' ... 'Paris' );
var
    LIdx : Integer;
begin
    LIdx := AAngle div 15.0;	// 0 ... 360 to 0 ... 23

    Result := CStations[ LIdx ];
end;

function TForm7.GetMyStationFreqFromAngleMethod( const AAngle : Single ) : Single;
begin
    Result := (AAngle / 360.0) * 200.0;   // 0 ... 360 to 0 ... 200.0 MHz
end;


procedure TForm7.KnobLeftClick(Sender: TObject);
begin
    MyRoundRect.RotationAngle := MyRoundRect.RotationAngle - 15.0; 

    Label1.Text := Format( 'My station is %s at %4.0f MHz, 
                           [ GetMyStationNameFromAngleMethod( MyRoundRect.RotationAngle ), GetMyStationFreqFromAngleMethod( MyRoundRect.RotationAngle ) ] );
end;

procedure TForm7.KnobRightClick(Sender: TObject);
begin
    MyRoundRect.RotationAngle := MyRoundRect.RotationAngle + 15.0; 

    Label1.Text := Format( 'My station is %s at %4.0f MHz, 
                           [ GetMyStationNameFromAngleMethod( MyRoundRect.RotationAngle ), GetMyStationFreqFromAngleMethod( MyRoundRect.RotationAngle ) ] );
end;

I assume you want to display the station name and frequency, right ?

Just a rough idea, its already very late here ...

Since I don't know exactly what you need this is a wild guess 🙂

Edited by Rollo62
  • Like 1

Share this post


Link to post

@Rollo62 sorry if this question sounds basic but i cant figure out how to fix this error TForm7 does not contain a member  named KnobLabelStationAngle  i have put the function line under the procedure and in public/private nothing worked 

Share this post


Link to post

You can simply add your function declaration and definition in the code window, similar like this:

 

unit UMain_Form;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts
  ;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }

    function MakeANewFunction( const AParam : Single ) : String;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{ TForm1 }

function TForm1.MakeANewFunction(const AParam: Single): String;
begin
    if AParam >  100.0 then
        Result := 'Large'
    else
    if AParam >=   0.0 then
        Result := 'Small'    
    else
        Result := 'Negative';
end;

end.

For these kind of questions you better look into a good, basic Delphi book, like this one from Marco Cantu.

Edited by Rollo62

Share this post


Link to post

@Rollo62 i put the function under public still wont work 

  private
    { Private declarations }
  public
    { Public declarations }

   function TForm7.KnobLabelStationAngle(const AAngle : Single) : String; overload;
   function TForm7.KnobLabelFreqAngle(const AAngle : Single ) : Single; overload;
  end;
procedure TForm7.KnobClick(Sender: TObject);
begin
//Knob
Knob.RotationAngle := Knob.RotationAngle - 15.0;
Rectangle4.RotationAngle := Knob.RotationAngle - 15.0;
Label2.Text := Format ('%s',
                      [KnobLabelStationAngle(Knob.RotationAngle), KnobLabelFreqAngle(Knob.RotationAngle) ] );
end;
function TForm7.KnobLabelStationAngle(const AAngle : Single) : String;
const
CStationLabel : array[0..24-1] of String = ('KEC61', 'WXK30');
var
LIdx : Integer;
begin
LIdx := AAngle div 15.0;
Result := CStationLabel[LIdx];
end;
function TForm7.KnobLabelFreqAngle(const AAngle : Single ) : Single;
begin
  Result := (AAngle / 162.550 * 162.525 * 162.500  * 162.400 * 162.475)
end;

900891737_Screenshot2022-11-09091445.thumb.png.3772e33544fadd282ae42e107fb8aeb9.png

Edited by Linuxuser1234

Share this post


Link to post

What I spot without too much looking into details: Your inside TForm7 declaration, so don't write TForm7. here

private
    { Private declarations }
  public
    { Public declarations }

   //function TForm7.KnobLabelStationAngle(const AAngle : Single) : String; overload;
   function KnobLabelStationAngle(const AAngle : Single) : String; overload;
   //function TForm7.KnobLabelFreqAngle(const AAngle : Single ) : Single; overload;
   function KnobLabelFreqAngle(const AAngle : Single ) : Single; overload;
  end;

 

Share this post


Link to post

@Rollo62 

now i get this and also when i click on the design tab i keep getting a error saying container form not set and i changed the name from ignore the first error i got it fixed but im still getting the second error 


//second error line  
LIdx := AAngle div 15.0;

169581054_Screenshot2022-11-09101451.thumb.png.09042df136faeff2b98dfa0ce7ae990b.png

Edited by Linuxuser1234

Share this post


Link to post

Ok, ok, here is a short demo maybe what you are looking for, additionally with some mouse drag-and-drop for the knob..

I would really recommend that you look into some books and the help before continuing, as well as checking out all the samples.

Maybe that small demo helps for a start, but you should begin with the basics first.

 

T465_Rotation_001.zip

Edited by Rollo62

Share this post


Link to post

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

×