[code]// main global script file
#define SAVELOAD_SLOTS_MAX 20 // the max number of saved slots (*must* be 20 or less!);
// Slot name if user doesn't specify one:
#define SLD_DESCRIPTION_DEFAULT "Unnamed slot"
// Default warning messages:
#define SLD_MESSAGE_TooManyGameSlots "There are too many game slots. Please, choose an already existing item to overwrite!"
#define SLD_MESSAGE_NoItemSelectedToLoad "Please, choose an item to load!"
#define SLD_MESSAGE_NoItemSelectedToDelete "Please, choose a slot to delete!"
//--------
// Flags:
//--------
// Close the dialog GUI after saving the game (1 - yes, 0 - no):
int sld_bCloseGUIAfterSave = 0;
// Save the game on 'Enter' pressed:
int sld_bDoSaveOnEnterPressed = 1;
// Save/Load dialog GUI:
int sldGUI = -1; // saveload dialog GUI (-1 until opened);
// Save/Load dialog GUI objects:
int sldTextbox = -1; // saveload textbox GUI object number;
int sldListbox = -1; // saveload listbox GUI object number;
int sldSaveButton = -1; // saveload save button GUI object number;
int sldLoadButton = -1; // saveload load button GUI object number;
int sldDeleteButton = -1; // saveload delete button GUI object number;
int sldCancelButton = -1; // saveload cancel button GUI object number;
//-----------------------
// Functions:
//-----------------------
function PrepareSaveLoadDialog(
int GUI,
int textbox,
int listbox,
int saveButton,
int loadButton,
int deleteButton,
int cancelButton) {
//=---------------------------------------------------------------------=
// Prepares a save/load dialog GUI (setting necessary GUI objects),
//=---------------------------------------------------------------------=
// if GUI wasn't specified:
if (GUI == -1)
{
// display error message and abort the game:
Display("Error: Save/Load dialog GUI hasn't been set up!");
QuitGame(0);
}
sldGUI = GUI; // set saveload GUI;
sldTextbox = textbox; // set saveload textbox GUI object number;
sldListbox = listbox; // set saveload listbox GUI object number;
sldSaveButton = saveButton; // set saveload save button GUI object number;
sldLoadButton = loadButton; // set saveload load button GUI object number;
sldDeleteButton = deleteButton; // set saveload delete button GUI object number;
sldCancelButton = cancelButton; // set saveload cancel button GUI object number;
}
function on_event_SaveLoadDialogGUI(int event, int data) {
//=---------------------------------------------------------------------=
// Processes events catched through the on_event function.
// Note: it must be called from within the on_event function!
//=---------------------------------------------------------------------=
// We have to catch a click over a textbox here as
// the interface_click function isn't triggered
// if such an event happens!
// if there is click over a textbox:
if (event == GUI_MDOWN)
{
if (data == sldGUI)
{
if (GetGUIObjectAt(mouse.x ,mouse.y) == sldTextbox)
{
// remove listbox highlight (textbox has a focus now!):
ListBoxSetSelected(sldGUI, sldListbox, -1);
}
}
}
}
function UpdateSaveLoadDialogGUI() {
//=---------------------------------------------------------------------=
// Updates the saveload dialog GUI to reflect the changes being made
// and returns the total number of saved slots.
//=---------------------------------------------------------------------=
// store an old value (to restore it later):
int selectedItem = ListBoxGetSelected(sldGUI, sldListbox);
// update the savegame list (seleted item is reset here ot 0!):
ListBoxSaveGameList(sldGUI, sldListbox);
// get the number of slots (it's == number of the items in a listbox):
int totalSlots = ListBoxGetNumItems(sldGUI, sldListbox);
// if we have exceeded the limits display a warning message:
//if (totalSlots > SAVELOAD_SLOTS_MAX) Display("too many slots!");
// restore an old value (stored in selectedItem);
ListBoxSetSelected(sldGUI, sldListbox, selectedItem);
// return the number of saved slots:
return totalSlots;
}
function OpenDialogGUI() {
//=---------------------------------------------------------------------=
// Initializes and then finally opens the save/load dialog:
//=---------------------------------------------------------------------=
CentreGUI(sldGUI); // centre the save/load GUI;
GUIOn(sldGUI); // turn the related GUI on;
UpdateSaveLoadDialogGUI(); // refresh GUI's content (show saved games);
ListBoxSetSelected(sldGUI, sldListbox, -1); // remove the highlight:
// if the textbox is specified:
if (sldTextbox != -1)
{
SetTextBoxText(sldGUI, sldTextbox, ""); // clear textbox;
}
Wait(1); // update the screen to show the GUI;
SetMouseCursor(6); // override cursor to look like an arrow;
}
function CloseSaveLoadDialog() {
//=---------------------------------------------------------------------=
// Closes the save/load dialog:
//=---------------------------------------------------------------------=
SetDefaultCursor(); // restore cursor appearence;
GUIOff(sldGUI); // turn the related GUI off;
sldGUI = -1; // free save/load dialog GUI;
}
function interface_click_SaveLoadDialogGUI(int button) {
//=---------------------------------------------------------------------=
// Processes clicks on a save/load dialog GUI.
// Note: it must be called from within the interface_click function!
//=---------------------------------------------------------------------=
//-------------------------------------------------------------------
// if click on a *listbox item*:
if (button == sldListbox)
{
// Show currently selected saved slot's description in a textbox:
// give us a temp buffer to transfer string:
string buffer;
// get selected item:
int selectedItem = ListBoxGetSelected(sldGUI, button);
// copy saved game description into 'buffer':
ListBoxGetItemText(sldGUI, button, selectedItem, buffer);
// and finally, if the textbox exists:
if (sldTextbox != -1)
{
// fill the textbox with what is stored in 'buffer':
SetTextBoxText(sldGUI, sldTextbox, buffer);
}
}
//-------------------------------------------------------------------
// if click on a *save button* or *enter key* is pressed:
else if ( (button == sldSaveButton) || ((button == sldTextbox) && (sld_bDoSaveOnEnterPressed == 1)) )
{
// Save a game to slot:
// get total the number of slots:
int totalSlots = UpdateSaveLoadDialogGUI();
// get selected item:
int selectedItem = ListBoxGetSelected(sldGUI, sldListbox);
// allocate string to transfer slot description:
string strDescription;
// fill 'strDescription' with the message in a listbox:
GetTextBoxText(sldGUI, sldTextbox, strDescription);
// if there is no text description then set default SLD_DESCRIPTION_DEFAULT:
if (StrLen(strDescription) == 0) StrCopy(strDescription, SLD_DESCRIPTION_DEFAULT);
// if it's not out of bounds...
if ( ( selectedItem >= 0 ) && ( selectedItem < SAVELOAD_SLOTS_MAX ) )
{
// overwrite save game slot (with a GUI temporarily disabled):
GUIOff(sldGUI);
SetDefaultCursor();
SaveGameSlot(savegameindex[selectedItem], strDescription);
SetMouseCursor(6);
GUIOn(sldGUI);
}
// if there is no slot selected...
else if ( (selectedItem == -1) && (totalSlots < SAVELOAD_SLOTS_MAX) )
{
// save to a new slot (with a GUI temporarily disabled):
GUIOff(sldGUI);
SetDefaultCursor();
SaveGameSlot(totalSlots + 1, strDescription);
SetMouseCursor(6);
GUIOn(sldGUI);
}
else
{
// display a warning message (slots limit was exceeded):
Display(SLD_MESSAGE_TooManyGameSlots);
}
// update the save/load GUI
UpdateSaveLoadDialogGUI();
// remove the highlight:
ListBoxSetSelected(sldGUI, sldListbox, -1);
// clear a textbox;
SetTextBoxText(sldGUI, sldTextbox, "");
// if the sld_bCloseGUIAfterSave flag is true then close the GUI:
if (sld_bCloseGUIAfterSave == 1)
{
CloseSaveLoadDialog(); // close the save/load GUI;
}
}
//-------------------------------------------------------------------
// if click on a *load button*:
else if (button == sldLoadButton)
{
// Try to delete a saved slot:
// get selected item:
int selectedItem = ListBoxGetSelected(sldGUI, sldListbox);
// if it's not out of bounds...
if ( ( selectedItem >= 0 ) && ( selectedItem < SAVELOAD_SLOTS_MAX ) )
{
// restore game from the slot:
RestoreGameSlot(savegameindex[selectedItem]);
}
else // otherwise...
{
// display a message (as slot isn't selected):
Display(SLD_MESSAGE_NoItemSelectedToLoad);
}
// update the save/load GUI
UpdateSaveLoadDialogGUI();
}
//-------------------------------------------------------------------
// if click on a *delete button*:
else if (button == sldDeleteButton)
{
// Try to delete a saved slot:
// get selected item:
int selectedItem = ListBoxGetSelected(sldGUI, sldListbox);
// if it's not out of bounds...
if ( ( selectedItem >= 0 ) && ( selectedItem < SAVELOAD_SLOTS_MAX ) )
{
// delete the slot:
DeleteSaveSlot(savegameindex[selectedItem]);
// update the save/load GUI and get the number of items:
int totalSlots = UpdateSaveLoadDialogGUI();
// When we delete *the last* slot the highlight is
// removed, so we should get it back by highlighting
// a new last slot:
if (ListBoxGetSelected(sldGUI, sldListbox) == -1)
{
// highlight the last item if required:
ListBoxSetSelected(sldGUI, sldListbox, totalSlots - 1);
}
}
else // otherwise...
{
// display a message (as slot isn't selected):
Display(SLD_MESSAGE_NoItemSelectedToDelete);
}
}
//-------------------------------------------------------------------
// if click on a *cancel button*:
else if (button == sldCancelButton)
{
CloseSaveLoadDialog(); // close the save/load GUI;
}
}
function OpenSaveLoadDialog() {
//=---------------------------------------------------------------------=
// Opens the *save/load* combo dialog:
//=---------------------------------------------------------------------=
// Setting up the GUI (pass -1 if the GUI object is unused!):
// ------------------------GUI---tbox--lbox--save--load--del--cancel
PrepareSaveLoadDialog( 10, 0, 1, 4, 3, 5, 6 );
// Open GUI:
OpenDialogGUI();
}
function OpenSaveDialog() {
//=---------------------------------------------------------------------=
// Opens the *save* dialog:
//=---------------------------------------------------------------------=
// Setting up the GUI (pass -1 if the GUI object is unused!):
// ------------------------GUI---tbox--lbox--save--load--del--cancel
PrepareSaveLoadDialog( 8, 3, 2, 0, -1, 5, 1 );
// Open GUI:
OpenDialogGUI();
}
function OpenLoadDialog() {
//=---------------------------------------------------------------------=
// Opens the *load* dialog:
//=---------------------------------------------------------------------=
// Setting up the GUI (pass -1 if the GUI object is unused!):
// ------------------------GUI---tbox--lbox--save--load--del--cancel
PrepareSaveLoadDialog( 9, -1, 0, -1, 1, -1, 2 );
// Open GUI:
OpenDialogGUI();
}
function on_event(int event, int data) {
// called when an event happens (see AGS manual for details)
// inform our save/load GUI of the event;
on_event_SaveLoadDialogGUI(event, data);
}
[/code]