SRL Archive - Help and Support

Another forms question (sorry to bother you guys)


Pure1993

2009-01-18 17:38:12

Hi,
Well, first of all I would like to apologize for all the help threads… But, with a bit of luck this is the last one for a very long time… :)
Anyway, I'll just post a script of what I want it to do (obviously, it doesn't work, but it cuts a long explanation short ^^):


program New;
{include srl/srl.scar}
var
frmDesign : TForm;
Shape1, Shape2 : TShape;

procedure Tryme(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
writeln('1');
repeat
Wait(100);
until Shape2.OnMouseDown; // Need some sort of Boolean here...
Writeln('2');
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 250;
frmDesign.Top := 114;
frmDesign.Width := 696;
frmDesign.Height := 480;
frmDesign.Hint := 'block';
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := True;
frmDesign.PixelsPerInch := 96;
Shape1 := TShape.Create(frmDesign);
Shape1.Parent := frmDesign;
Shape1.Left := 304;
Shape1.Top := 64;
Shape1.Width := 33;
Shape1.Height := 33;
Shape1.Hint := 'block';
Shape1.Brush.Color := clBlack;
Shape1.Shape := stCircle;
Shape1.OnMouseUp := @Tryem;
Shape2 := TShape.Create(frmDesign);
Shape2.Parent := frmDesign;
Shape2.Left := 304;
Shape2.Top := 100;
Shape2.Width := 33;
Shape2.Height := 33;
Shape2.Hint := 'block';
Shape2.Brush.Color := clBlack;
Shape2.Shape := stCircle;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;
end.

So yeah, to clarify it further: I need it to click something, then do some stuff, then wait until another object/button/shape is clicked…

Thanks a lot in advance :D

almost

2009-01-18 17:57:24

First off
I would like to apologize for all the help threads

Don't worry about it, because I enjoy helping people script.

Now with what is wrong…
until Shape2.OnMouseDown;
Event handlers (OnMouseDown is an event handler) aren't used like this.
They are not a true or false statement, which is what "until" is asking for.
Also you have to have the right variables for an OnMouseUp event.
You have
procedure Tryme( Sender: TObject);
But for it to work you will need
Tryme(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
You can go in SCAR and click tools then event handler list for a list of the events and what you need for each.

Pure1993

2009-01-18 18:06:17

Yeah, thanks almost, but I was aware of those things already. I just put something together within a few seconds in order to rougly describe what I am looking for. I also know that an event handler is not a boolean, that's why I wrote the comment, "// Need some sort of Boolean here…" next to it ;)
SO, anyway, is there a boolean which checks if it has been clicked on?
Oh yeah, concerning OnMouseUp and OnMouseDown… why is it so important which one I choose? Usually, when I click on an object, I press down and release the mouse button on the very same object… :)

almost

2009-01-18 18:58:34

Ok lol, I thought you were just learning forms.
Didn't see the //need some sort of boolean here
What are you trying to make the form do though?
I can't tell.

Just noticed you have a loop in the form, which is usually not a good idea.
Forms don't respond while in a loop.
If this is just for the testing purposes it is fine though.
But if your script does need to loop I would use a TTimer.

Oh yeah, concerning OnMouseUp and OnMouseDown… why is it so important which one I choose? Usually, when I click on an object, I press down and release the mouse button on the very same object…

OnMouseUp isn't really used for when you click something.
It is for when you hold your mouse down on something, and when you are done holding your mouse the script needs to do something.
I think you might want OnClick for this script, not sure what you're doing though.

OnMouseDown- Activates once your mouse button is down
OnClick- Activates once your mouse is down then up on the same object
OnMouseUp- Activates once your mouse button is released
(kind of common sense /\ but might help)

Pure1993

2009-01-19 01:52:05

Ok lol, I thought you were just learning forms.
Didn't see the //need some sort of boolean here
What are you trying to make the form do though?
I can't tell.

Okay, I'll try to explain it again (sorry in advance, explaining things is not the thing I do best… ;) ). If you run this "script", without the error blocking it, you would see 2 circle shapes. Now, I what I want to do is make it possible to click on the first shape, which kicks of the the tryme procedure (easy part, learned how to do that an eternity ago ;D). Then it does a writeln (In the real script, there is a hell lot of stuff before it :D) and then it is supposed to wait until the second shape is clicked (hard part), in order to continue the script… The problem is, there is nothing to "check" whether the shape is being clicked or not, which needs returns a boolean, in order to fit my purpose…

My purpose for this script it to creat a game based on a little board game I like to play… I know Scar/Pascal is not really the best language to do it in, but heck, I set the objective/goal and I am going to complete it… :)
After this problem is solved though, there is nothing more to deal with… just a few hundred lines of code which I have all written before in some way or another. :D

almost

2009-01-19 12:18:40

So if I understand correct, you want it to repeat certain lines after one shape has been clicked until another shape is clicked?
If so then you will need a TTimer.

For the OnClick of your shape you will set the TTimer.Enabled to true.
That is how I always do any kind of loop in a form.
Your TTimer.OnTimer will hold the lines that will be done once your shape is clicked.
Inside the OnTimer procedure you will first want to check to see if the timer is enabled or not.
If it is, disable it, if it isn't, enable it.

If you need different things to be done with each shape you click, make multiple TTimers or a global variable that you set once a shape is clicked.
Something like
var
CurrentShape: string;

Then inside your timer even it will pick what to do with a case
  case CurrentShape of
'circle': DoCircleStuff;
'triangle': DoTriangleStuff;
end;

Pure1993

2009-01-20 07:54:52

Erm, me no thinky me be understands that…. x)
Seriously, you got a bit confused there… I would really appreciate it if you could apply it to the "example" script I posted above… Thanks a bunch :D

almost

2009-01-20 14:44:39

So if I understand correct, you want it to repeat certain lines after one shape has been clicked until another shape is clicked?

If this is right then I might be able to post an example.

Pure1993

2009-01-20 15:33:18

Yep, that about fits it, and after I grasped the concept, I will probably be able to create any needed adjustments :)

almost

2009-01-20 18:17:43

Still not for sure what you are looking for but this is what I came up with.
program New;

var
frmDesign : TForm;
Shape1, Shape2 : TShape;
Timer: TTimer;
CurrentShape: string;

procedure OnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Timer.Enabled:= false;
if (Sender = Shape1) and (CurrentShape <> 'circle1') then
begin
Timer.Enabled:= True;
CurrentShape:= 'circle1';
end;
if (Sender = Shape2) and (CurrentShape <> 'circle2') then
begin
Timer.Enabled:= True;
CurrentShape:= 'circle2';
end;
end;

procedure OnTimer(Sender: TObject);
begin
//use this to do the lines of code you need for what shape you clicked on last
case CurrentShape of
'circle1': WriteLn('You are on the first circle!');
'circle2': WriteLn('You are on the 2nd circle!');
end;
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 250;
frmDesign.Top := 114;
frmDesign.Width := 696;
frmDesign.Height := 480;
frmDesign.Hint := 'block';
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := false;
frmDesign.PixelsPerInch := 96;
frmDesign.Position:= poDesktopCenter;

Shape1 := TShape.Create(frmDesign);
Shape1.Parent := frmDesign;
Shape1.Left := 304;
Shape1.Top := 64;
Shape1.Width := 33;
Shape1.Height := 33;
Shape1.Hint := 'block';
Shape1.Brush.Color := clBlack;
Shape1.Shape := stCircle;
Shape1.OnMouseDown:= @OnMouseDown;

Shape2 := TShape.Create(frmDesign);
Shape2.Parent := frmDesign;
Shape2.Left := 304;
Shape2.Top := 100;
Shape2.Width := 33;
Shape2.Height := 33;
Shape2.Hint := 'block';
Shape2.Brush.Color := clBlack;
Shape2.Shape := stCircle;
Shape2.OnMouseDown:= @OnMouseDown;

Timer:= TTimer.Create(frmDesign);
with Timer do
begin
Enabled:= false;
Interval:= 500;
OnTimer:= @OnTimer;
end;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;
end.