PDA

View Full Version : The Concise Forms and Saved Settings Tutorial



Kevin
05-07-2013, 09:02 PM
The Concise Forms and Saved Settings Tutorial

Foreword:

Greetings, everyone! This is not a comprehensive tutorial by any means. If you would like a comprehensive tutorial on forms, visit here. (http://villavu.com/forum/showthread.php?t=91724) If you want a more comprehensive tutorial on saving and loading settings for your forms... Ask me and I'll add detail :)

Table of Contents:


Form Creation
Form Objects
Events
INI Files
Implement Multiple Settings Files
FAQ


Form Creation:

You will need to make a couple of Thread Safe Calls to setup the form, the first of which is designated simply to populate and setup the form, while the second is to actually show the form to the user and grant it complete control until it is closed.

var
settingsForm:TForm;

//Initialize those form variables!
procedure InitialiseForm;
begin
//A lot of junk that will be explained upcoming and must happen before the show call in the next thread safe call
end;

//Show that form to the user!
procedure ShowModal;
begin
settingsForm.ShowModal;
end;

begin
ThreadSafeCall('InitialiseForm', params);
ThreadSafeCall('ShowModal', params);
end.

So far, it looks pretty simple right? Wrong! It really is! So how do we setup a blank form? Well that's fairly simple, we just need to initialize it and tell simba how we want it to look, like this:

procedure InitialiseForm;
begin
settingsForm:= CreateForm;//Actually create the form (necessary call)
settingsForm.Width:= 475;//How many pixels wide will your form be?
settingsForm.Height:= 420;//How many pixels tall will your form be?
settingsForm.Caption:= 'Our Settings Form!';//The caption will be the text displayed at the very top of the form in the title bar
settingsForm.Color := ClWhite;//The default color of the form and all objects that will be added to it
settingsForm.Font.Color := ClBlack;//The default color of all text that will occur on the form
settingsForm.Font.Size := 8;//The default font size of all text that will occur on the form
//A lot more junk for the stuff inside the form
end;

http://villavu.com/forum/attachment.php?attachmentid=21379
Wow, we're keeping those "complicated" forms pretty simple still I hope (and to be fair, pretty empty)! Now we should have a blank form that just appears to be a new empty window. We should fill it up next - and that shall be with the inner objects I explain in the next section.


Form Objects:

Shared properties
.Create(TForm);//pass the form you created to this. This must be called first before setting any values of any object
.Parent:= //Set it equal to the form you want this sobejct to appear on. By defualt it will set its other values to match the form's defaults
.Caption:= The text the object will have
.Left:= The amount of pixels from the left side of the form that the object will be on
.Top:= The amount of pixels from the top side of the form that the object will be on
.Width:= The maximum amount of pixels wide the object will be
.Height:= The maximum amount of pixels tall the object will be

TComboBox: A drop down list of options - one of which may be selected at a time.
TEdit: A text box that the user can enter custom strings into
TCheckBox: A check box that the user can check (good for boolean values)
TLabel: A label box that the user cannot edit in any manner
TButton: A button that can be pressed

An important feature of these objects include events. Simply put, whenever the designated action occurs (the event), the tied delegate (or procedure pointer in our case - feel free to read more about them here: http://villavu.com/forum/showthread.php?t=102800) will be called. Ok, finally this part can sound a little daunting and confusing, but I swear it's not that bad after you get your head around it. Your procedure name MUST be styled as such:

procedure ActionClick(Sender: TObject);//It MUST be a procedure and it MUST have that parameter (which you will never pass manually), the actual name of the procedure is up to you.
So how do we connect the desired events to the resulting action? We connect it through designated events for every object. But Kevin, what kind of situations do we even care about this? We care about this because we can use them to save the form, to load values, or to even make specific objects invisible due to other options making them useless!

var
settingsForm: TForm;
timeToRun: TEdit;
RunForever: TCheckBox;

//Change the visibility of end early settings.
procedure SelectRunForever(Sender: TObject);
begin
timeToRun.Visible:= not RunForever.CHECKED;//When the boolean (checkbox) to run the script forever is true, then don't allow the user to see or edit the box that sets the time to run for.
end;

procedure InitialiseForm;
begin
//Keep previous logic in place and add:
timeToRun:= TEdit.Create(settingsForm);
timeToRun.Parent:= settingsForm;
RunForever:= TCheckBox.Create(settingsForm);
RunForever.Parent:= settingsForm;
timeToRun.Visible:= false;
timeToRun.Left:= 255;
timeToRun.Top:= 115;
RunForever.CAPTION:= 'Run Forever';
RunForever.Left:= 170;
RunForever.Top:= 115;
RunForever.CHECKED:= True;
RunForever.ONCLICK:= @SelectRunForever;//Whenever the box is clicked (checked or unchecked), call SelectRunForever
end;

http://villavu.com/forum/attachment.php?attachmentid=21380
Congratulations! This is what your form should now look like with the checkbox unchecked! (And that editable box should disappear when you check the box). Now let's look into combo boxes. These are the drop down menus that are really helpful for allowing the user to choose a pre-defined list of options. Like the other objects, the combo box has top, left, width, height, font, parent, and create properties. However, the combo box has several unique properties such as:
.Style:= csDropDownList;//there are multiple cs* styles, however I like this one best. This defines the style of the combo box
.Items;//This is the list of options in the combo box
.Items.Add(value: String);//Use this to add a new value to the list of options in the combo box
.ItemIndex:= 0;//Finally after all items have been added, you can default the currently selected item to a given value in the list through a value with an index starting at 0

Now let's look at this wonder in code:
procedure InitialiseForm;
var
comboBox: TComboBox;
label: TLabel;
begin
//Same junk as before.
comboBox:= TComboBox.Create(settingsForm);
comboBox.Parent:= settingsForm;
comboBox.Left:= 255;
comboBox.Top:= 10;
comboBox.Width:= 30;
comboBox.Style:= csDropDownList;
comboBox.Items.Add('1');
comboBox.Items.Add('2');
comboBox.Items.Add('3');
comboBox.ItemIndex:= 0;
label:= TLabel.Create(settingsForm);
label.Parent:= settingsForm;
label.FONT.Size:= 10;
label.Left:= 165;
label.Top:= 10;
label.Caption:= 'XP Bar slot';
end;

http://villavu.com/forum/attachment.php?attachmentid=21378
Finally, we need a button and the ability to close our form to start the script. If you have everything else, then here is a piece of cake.
procedure OnButtonClick(Sender: TObject);
begin
SetVariablesFromForm;//your own method to set all your variables to the .Caption values of the form
SaveForm;//Your own method to save your variables to a form using a technique I'm about to describe in the next section
settingsForm.MODALRESULT:= mrOk;//this command will set the form as ready to be closed and it will close automatically
end;

procedure InitialiseForm;
var
saveButton: TButton
begin
//Everything we had before
saveButton:= TButton.Create(settingsForm);
saveButton.Parent:= settingsForm;
saveButton.Left:= 140;
saveButton.Top:= 375;
saveButton.Caption:= 'Start!';
saveButton.OnClick:= @OnButtonClick;//this way clicking the button does something


INI Files


This is easily the simplest method of saving/writing settings to files (originally pointed to me by Mr. Amazing Ollybest - although I did the reading learning myself :p). There are several built in methods for writing settings either to dedicated ini files or the default simba ini file (unused by anyone I know of, and therefore useless, so I suggest using your own per script).
Saving to files:
WriteINI(sectionName, keyName, valueName, saveFileName: String);
WriteINI('Settings', 'PlayerName', 'bot 123', (AppPath + 'MySettingsFile.ini'));//example The aforementioned example will find the "MySettingsFile.ini" file in the AppPath folder, go to the section labeled "Settings", and set the "PlayerName" variable to "bot 123". And if any of those don't already exist (file, section, variable), it will be automatically created for you. Easy!
Loading from files:
ReadINI(sectionName, keyName, saveFileName: String): String;//returns the valueName set before
textBox.Caption:= ReadINI('Settings', 'PlayerName', (AppPath + 'MySettingsFile.ini'));//example - sets the caption of the text box to the value matching all the previous setup

Implement Multiple Settings Files:


This is a little more complicated than the aforementioned ideas and it assumes you already have created your own custom load and save forms. I will show everything at once with the upcoming code, but I will comment to try and help. Please ask if you have questions!

var
SaveFileName: String;

procedure SaveForm(Sender: TObject);
begin
if(comboBox.ITEMINDEX<0)then
SaveFileName:= AppPath + 'ScriptName' + PlayerName + '.ini';//I will likely not include script name in my personal work so I can start using a single settings file that handles many scripts.
//Save your form to your file
end;

procedure LoadForm(Sender: TObject);
begin
if(comboBox.ITEMINDEX<0)then
Exit;
SaveFileName:= AppPath + comboBox.ITEMS[comboBox.ITEMINDEX];
if(not FileExists(SaveFileName))then
Exit;
//Expected remaining loading using this new file name variable instead of the constant it was before
end;

procedure InitialiseForm;
var
files: TStringArray;
begin
//Which file to load related setup
label.Left:= 5;
label.Top:= 315;
label.Caption:= 'Load file: ';
comboBox.Left:= 70;
comboBox.Top:= 315;
comboBox.Width:= 150;
comboBox.Style:= csDropDownList;
files:= GetFiles(AppPath, 'ini');
for i:=0 to high(files) do
begin
comboBox.Items.Add(files[i]);
WriteLn(files[i]);
end;
comboBox.ONCHANGE:= @LoadForm;//By not giving an index, it's -1 and then you can save a new form settings file
end;


FAQ:

Ask me some questions and I'll toss them up here.

Sin
05-07-2013, 09:05 PM
Nice one, +repped

StickToTheScript
05-07-2013, 09:06 PM
She's amazing! Imma gonna use this!

Chris!
05-07-2013, 09:07 PM
Nice guide Kevin!

Kevin
05-07-2013, 09:37 PM
Thank you all! Honestly, I was so intimidated by forms not that long ago, and then it just turned out to be that I had too many details all being thrown at me at once and I just didn't understand what NEEDED to be around and what was optional or what some of these things actually did (like the .Parent property). I hope this helps someone and we can see more forms around here, they're not all that bad after being broken up like this.

I'm thinking of adding a small section for having multiple settings files to be account specific to support multiple accounts better (or even have basic settings being shared cross script because of that). Any thoughts on whether I should add that?

StickToTheScript
05-07-2013, 09:41 PM
Thank you all! Honestly, I was so intimidated by forms not that long ago, and then it just turned out to be that I had too many details all being thrown at me at once and I just didn't understand what NEEDED to be around and what was optional or what some of these things actually did (like the .Parent property). I hope this helps someone and we can see more forms around here, they're not all that bad after being broken up like this.

I'm thinking of adding a small section for having multiple settings files to be account specific to support multiple accounts better (or even have basic settings being shared cross script because of that). Any thoughts on whether I should add that?

Add as much as possible. IMO, the more things the better. Also, this helps me! :D

Kevin
05-07-2013, 09:56 PM
Add as much as possible. IMO, the more things the better. Also, this helps me! :D

Ok then, note to self for later tonight - add:
1)Pictures for visual help
2)Multiple Settings files support
3)Descriptions of Combo Boxes and related properties (I can't believe I left those at)

StickToTheScript
05-07-2013, 09:58 PM
Ok then, note to self for later tonight - add:
1)Pictures for visual help
2)Multiple Settings files support
3)Descriptions of Combo Boxes and related properties (I can't believe I left those at)

And i will be looking back here to see when it is updates so that I can learn more!! :p

Kevin
05-08-2013, 03:52 AM
And i will be looking back here to see when it is updates so that I can learn more!! :p

All has been added like I said I would, lemme know whatcha think!

StickToTheScript
05-08-2013, 04:06 AM
All has been added like I said I would, lemme know whatcha think!

I will read it tomorrow. I am pretty sure its amazing tho! :p

Turpinator
05-23-2013, 12:16 AM
is there a TL;DR for this?

Kevin
05-23-2013, 12:44 AM
is there a TL;DR for this?

Yes. It's called copy pasta.

Ian
05-23-2013, 04:15 AM
Rep+, great tutorial!

My first form:

http://i.imgur.com/moW6AFf.png

E: Oops, can't rep you again right now.

srlMW
06-30-2015, 07:50 AM
Sorry for the bump, just wondering whether this is outdated or not?

Kevin
07-01-2015, 01:14 AM
Sorry for the bump, just wondering whether this is outdated or not?

I can't answer with certainty, but I'm pretty sure it does still work as the version of SRL this was written for hasn't been deprecated yet.