///scr_Book_CreateEvent
//Place this script within the Create Event of the obj_Book or equivalent.
//This script only takes one argument and that is the location of the book ini file.
//Usage example:
//    scr_Book_CreateEvent(working_directory+"Books/Example.ini");

//The following IF-THEN statement allows you to also specify an INI book
//via the command-line.
//Usage example:
//    Book.bin "/path/to/Book.ini"

Book = argument0;
if ( file_exists(parameter_string(1)) ){ Book = parameter_string(1); }

Page[0,0] = ''; //Leave this alone
Sketch[0] = ''; //Leave this alone
TotalPages = 0; //Leave this alone
MaxParagraphsPerPage = 0; //Leave this alone
HeardIt = false; //Leave this alone

//Get graphics settings...
ini_open(working_directory+"Settings/Graphics.ini");
    TransparentImages = ini_read_string("Books","TransparentImages",string(false));
    SmoothImages = ini_read_string("Books","SmoothImages",string(false));
    PreloadImages = ini_read_string("Books","PreloadImages",string(true));
ini_close();

//Get audio settings...
ini_open(working_directory+"Settings/Audio.ini");
    PreloadAudio = ini_read_string("Books","PreloadAudio",string(true));
    BackgroundAudioOn = ini_read_string("Books","BackgroundAudioOn","true");
    BackgroundAudioVolume = ini_read_real("Books","BackgroundAudioVolume",1);
    NarrationOn = ini_read_string("Books","NarrationOn","true");
    NarrationVolume = ini_read_real("Books","NarrationVolume",1);
ini_close();

//Control settings...
//See included KeyboardCodes.txt file for help with keyboard codes.
ini_open(working_directory+"Settings/Controls.ini");
    LeftOrRight = ini_read_string("DominantHand","LeftOrRight","RightHanded");
    Keyboard_PageForward1 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageForward_1",vk_right);
    Keyboard_PageForward2 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageForward_2",vk_pagedown);
    Keyboard_PageForward3 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageForward_3",vk_space);
    Keyboard_PageForward4 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageForward_4",ord("D"));
    Keyboard_PageBackward1 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageBackward_1",vk_left);
    Keyboard_PageBackward2 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageBackward_2",vk_pageup);
    Keyboard_PageBackward3 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageBackward_3",vk_backspace);
    Keyboard_PageBackward4 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"PageBackward_4",ord("A"));
    Keyboard_FirstPage1 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"FirstPage_1",vk_home);
    Keyboard_FirstPage2 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"FirstPage_2",vk_up);
    Keyboard_FirstPage3 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"FirstPage_3",ord("W"));
    Keyboard_LastPage1 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"LastPage_1",vk_end);
    Keyboard_LastPage2 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"LastPage_2",vk_down);
    Keyboard_LastPage3 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"LastPage_3",ord("S"));
    Keyboard_Bookmark1 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"Bookmark_1",ord("B"));
    Keyboard_Bookmark2 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"Bookmark_2",vk_insert);
    Keyboard_ToggleMusic = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"Toggle_BackgroundMusic",ord("M"));
    Keyboard_ToggleNarration = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"Toggle_Narration",ord("N"));
    Keyboard_Exit1 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"Exit_1",vk_escape);
    Keyboard_Exit2 = ini_read_real("Books_Keyboard_"+string(LeftOrRight),"Exit_2",ord("Q"));
ini_close();

ini_open(Book); //Opens the Book-realted INI file for reading
    Title = ini_read_string("TitlePage","Title","??? Missing Book;#Or, Does Not Exists ???");
    Author = ini_read_string("TitlePage","Author",'??? Author ???');
    Date = ini_read_string("TitlePage","Date",'??? Date ???');
    TSketch = ini_read_string("TitlePage","Sketch",string(bkg_Empty));
    TAudio = ini_read_string("TitlePage","Narration",string(snd_Empty));
    TopMargin = ini_read_real("Settings","TopMargin",room_width/20);
    LeftOuterMargins = ini_read_real("Settings","LeftOuterMargins",100);
    RightInnerMargins = ini_read_real("Settings","RightInnerMargins",50);
    ParagraphSpacing = ini_read_real("Settings","ParagraphSpacing",30);
    WordWrapWidth = ini_read_real("Settings","WordWrapWidth",500);
    TextColor = ini_read_string("Settings","TextColor",string(c_black));
    BackgroundImage = ini_read_string("Settings","Background",string(bkg_Book_Default));
    
    PageForwardImage = ini_read_string("Settings","PageForward",string(spr_PageForward));
    PageBackwardImage = ini_read_string("Settings","PageBackward",string(spr_PageBackward));
    CloseBookImage = ini_read_string("Settings","CloseBookImage",string(spr_CloseBook));
    PageForwardImage_Highlighted = ini_read_string("Settings","PageForward",string(spr_PageForward));
    PageBackwardImage_Highlighted = ini_read_string("Settings","PageBackward",string(spr_PageBackward));
    CloseBookImage_Highlighted = ini_read_string("Settings","CloseBookImage_Highlighted",string(spr_CloseBook));
    
    Font = ini_read_string("Settings","Font",string(fnt_Default));

    PAudio1 = ini_read_string("Settings","PageTurnAudio1",string(snd_PageTurn1));
    PAudio2 = ini_read_string("Settings","PageTurnAudio2",string(snd_PageTurn2));
    PAudio3 = ini_read_string("Settings","PageTurnAudio3",string(snd_PageTurn3));
    OCAudio = ini_read_string("Settings","BookOpenCloseAudio",string(snd_BookClose));
    
    BgAudioImageOn = ini_read_string("Settings","BgAudioImageOn",string(spr_BgAudioOn));
    BgAudioImageOff = ini_read_string("Settings","BgAudioImageOff",string(spr_BgAudioOff));
    
    NarrationImageOn = ini_read_string("Settings","NarrationImageOn",string(spr_NarrationOn));
    NarrationImageOff = ini_read_string("Settings","NarrationImageOff",string(spr_NarrationOff));
    
    //So you can have a book open to a certain page, aka a "Bookmark"
    // -1 is the title page
    Bookmark = ini_read_real("Settings","Bookmark",-1);
    BookmarkImage = ini_read_string("Settings","BookmarkImage",string(spr_Bookmark));
    BookmarkImage_Highlighted = ini_read_string("Settings","BookmarkImage_Highlighted",string(spr_Bookmark_Highlighted));
    BMarkAudio = ini_read_string("Settings","BookmarkAudio",string(snd_Bookmark));
    
    //Custom cursor...
    CursorImage = ini_read_string("Settings","CursorImage",string(spr_Cursor));
    CursorImage_Clicked =  ini_read_string("Settings","CursorImage_Clicked",string(spr_CursorClicked));
    CursorOrigin_X = ini_read_real("Settings","CursorOrigin_X",9);
    CursorOrigin_Y = ini_read_real("Settings","CursorOrigin_Y",9);
    
    //Background audio (loops)...
    BAudio = ini_read_string("Settings","BackgroundAudio",string(snd_Empty));

    //WHILE-LOOP used so as to know how many Page# there are in the Book-related
    //INI file. This is later divided by two because two pages are shown at once.
    //And while we are at it, go ahead and figure-out the maximum amount of used
    //paragraphs used while looking at all of the pages.
    i = 1; t = 1;
    while( ini_section_exists("Page"+string(i)) ){
        TotalPages += 1; i += 1;
        while( ini_key_exists("Page"+string(i),"Paragraph"+string(t)) ){
            MaxParagraphsPerPage += 1; t += 1;
        }
    }
    
    if ( frac(TotalPages) != 0 ){ TotalPages += 1; }//So as to always have 'p' divisable by two
    PagesDivededByTwo = (TotalPages/2); //This is because the book shows two pages at a time
    //FOR-LOOP that goes through every Page# and every Paragraph# within the Page#s
    //and assigns the results as a two-dimensional array: Page[page number, paragraph number]
    for (p=1; p <= (PagesDivededByTwo * 2); p+=1){
        for (t=1; t <= MaxParagraphsPerPage; t+=1){
            if ( ini_key_exists("Page"+string(p),"Paragraph"+string(t)) ){
                Page[p,t]=ini_read_string("Page"+string(p),"Paragraph"+string(t),'???');
            }
        }
        if ( ini_key_exists("Page"+string(p),"Sketch") ) {
            temp=ini_read_string("Page"+string(p),"Sketch",'???');
            if ( file_exists(working_directory+temp) ){
               Sketch[p]=background_add(working_directory+temp,TransparentImages,SmoothImages,PreloadImages,0);
            }
            else{ Sketch[p] = bkg_Empty; }
        }
        else{ Sketch[p] = bkg_Empty; }
        if ( ini_key_exists("Page"+string(p),"Narration") ) {
            temp=ini_read_string("Page"+string(p),"Narration",'???');
            if ( file_exists(working_directory+temp) ) {
                Narration[p]=sound_add(working_directory+temp,1,PreloadAudio);
            }
            else{ Narration[p] = snd_Empty; }
        }
        else{ Narration[p] = snd_Empty; }
    }
ini_close(); //Closes the Book-realted INI file

p = Bookmark; //Keep this at -1 if you want the book to start on the title page
PreviousPage = p; //Do not touch this

//If background audio file exists...
if ( file_exists(working_directory+BAudio) ){
    BackgroundAudio = sound_add(working_directory+BAudio,1,PreloadAudio);
}
else{ BackgroundAudio = snd_Empty; }

//If cursor image files exists...
if ( file_exists(working_directory+CursorImage) ) and ( file_exists(working_directory+CursorImage_Clicked) ){ 
    CursorSprite = sprite_add(working_directory+CursorImage,1,TransparentImages,SmoothImages,CursorOrigin_X,CursorOrigin_Y);
    CursorSprite_Clicked = sprite_add(working_directory+CursorImage_Clicked,1,TransparentImages,SmoothImages,CursorOrigin_X,CursorOrigin_Y);
    }
else { CursorSprite = spr_Cursor; CursorSprite_Clicked = spr_CursorClicked; }

//If background audio on/off images files exists...
if ( file_exists(working_directory+BgAudioImageOn) ) and ( file_exists(working_directory+BgAudioImageOff) ){ 
    BgAudioOnSprite = sprite_add(working_directory+BgAudioImageOn,1,TransparentImages,SmoothImages,0,0);
    BgAudioOffSprite = sprite_add(working_directory+BgAudioImageOff,1,TransparentImages,SmoothImages,0,0);
    }
else { BgAudioOnSprite = spr_BgAudioOn; BgAudioOffSprite = spr_BgAudioOff; }

//If narration on/off images files exists...
if ( file_exists(working_directory+NarrationImageOn) ) and ( file_exists(working_directory+NarrationImageOff) ){ 
    NarrationOnSprite = sprite_add(working_directory+NarrationImageOn,1,TransparentImages,SmoothImages,0,0);
    NarrationOffSprite = sprite_add(working_directory+NarrationImageOff,1,TransparentImages,SmoothImages,0,0);
    }
else { NarrationOnSprite = spr_NarrationOn; NarrationOffSprite = spr_NarrationOff; }

//If BackgroundImage file exists, use that instead of the default
if ( file_exists(working_directory+BackgroundImage) ){ 
    Background = background_add(working_directory+BackgroundImage,TransparentImages,SmoothImages,PreloadImages,0);
    }
else { Background = bkg_Book_Default; }

//Just in case the specified Sketch and/or Audio-related file do not exists...
if ( file_exists(working_directory+TAudio) ){
    TitleAudio = sound_add(working_directory+TAudio,0,PreloadAudio);
}
else { TitleAudio = snd_Empty; }
if ( file_exists(working_directory+TSketch) ){
    TitleSketch = background_add(working_directory+TSketch,TransparentImages,SmoothImages,PreloadImages,0);
}
else{ TitleSketch = bkg_Empty; }

//Just in case the font specified does not exist...
if !font_exists(Font){ Font = fnt_Default; }

//Just in case the page turn audio specified does not exists...
//There are three for randomness; otherwise, page turning will sound unnatural
if !file_exists(working_directory+PAudio1){ PageTurnAudio1 = snd_PageTurn1; }
else{ PageTurnAudio1 = sound_add(working_directory+PAudio1,0,PreloadAudio); }
if !file_exists(working_directory+PAudio2){ PageTurnAudio2 = snd_PageTurn2; }
else{ PageTurnAudio2 = sound_add(working_directory+PAudio2,0,PreloadAudio); }
if !file_exists(working_directory+PAudio3){ PageTurnAudio3 = snd_PageTurn3; }
else{ PageTurnAudio3 = sound_add(working_directory+PAudio3,0,PreloadAudio); }

//Just in case the book close audio specified does not exists...
if !file_exists(working_directory+OCAudio){ BookOpenCloseAudio = snd_BookClose; }
else{ BookOpenCloseAudio = sound_add(working_directory+OCAudio,0,PreloadAudio); }

//Just in case the bookmark audio specified does not exists...
if !file_exists(working_directory+BMarkAudio){ BookmarkAudio = snd_Bookmark; }
else{ BookmarkAudio = sound_add(working_directory+BMarkAudio,0,PreloadAudio); }

//Get the close book image stuff working...
if ( file_exists(working_directory+CloseBookImage) ) and ( file_exists(working_directory+CloseBookImage_Highlighted) ){ 
    CloseBookSprite = sprite_add(working_directory+CloseBookImage,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
    CloseBookSprite_Highlighted = sprite_add(working_directory+CloseBookImage_Highlighted,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
}
else{CloseBookSprite = spr_CloseBook; CloseBookSprite_Highlighted = spr_CloseBook_Highlighted; }

//Get the page forward image stuf working...
if ( file_exists(working_directory+PageForwardImage) ) and ( file_exists(working_directory+PageForwardImage_Highlighted) ){ 
    PageForwardSprite = sprite_add(working_directory+PageForwardImage,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
    PageForwardSprite_Highlighted = sprite_add(working_directory+PageForwardImage_Highlighted,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
}
else{PageForwardSprite = spr_PageForward; PageForwardSprite_Highlighted = spr_PageForward_Highlighted; }

//Get the page backward image stuff working...
if ( file_exists(working_directory+PageBackwardImage) ) and ( file_exists(working_directory+PageBackwardImage_Highlighted) ){ 
    PageBackwardSprite = sprite_add(working_directory+PageBackwardImage,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
    PageBackwardSprite_Highlighted = sprite_add(working_directory+PageBackwardImage_Highlighted,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
}
else{PageBackwardSprite = spr_PageBackward; PageBackwardSprite_Highlighted = spr_PageBackward_Highlighted; }

//Get the bookmark image stuff working...
if ( file_exists(working_directory+BookmarkImage) ) and ( file_exists(working_directory+BookmarkImage_Highlighted) ){ 
    BookmarkSprite = sprite_add(working_directory+BookmarkImage,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
    BookmarkSprite_Highlighted = sprite_add(working_directory+BookmarkImage_Highlighted,1,false,TransparentImages,SmoothImages,PreloadImages,0,0,0);
}
else{BookmarkSprite = spr_Bookmark; BookmarkSprite_Highlighted = spr_Bookmark_Highlighted; }

//Play Open/Close book audio at start of book if SoundOn is true...
if (BackgroundAudioOn = "true"){ sound_play(BookOpenCloseAudio); }

//Play background audio...
sound_loop(BackgroundAudio);

//By doing this in the Create Event, both the Draw Event and the Step Event
//do not have to do the same calculations rapidly over and over...
//Do not move these out of order.

Bookmark_Width = sprite_get_width(BookmarkSprite);
Bookmark_Height = sprite_get_height(BookmarkSprite);

BgAudioOn_Width = sprite_get_width(BgAudioOnSprite);
BgAudioOn_Height = sprite_get_height(BgAudioOnSprite);

BgAudioOff_Width = sprite_get_width(BgAudioOffSprite);
BgAudioOff_Height = sprite_get_height(BgAudioOffSprite);

NarrationOn_Width = sprite_get_width(NarrationOnSprite);
NarrationOn_Height = sprite_get_height(NarrationOnSprite);

NarrationOff_Width = sprite_get_width(NarrationOffSprite);
NarrationOff_Height = sprite_get_height(NarrationOffSprite);

CloseBook_Width = sprite_get_width(CloseBookSprite);
CloseBook_Height = sprite_get_height(CloseBookSprite);

PageForward_Width = sprite_get_width(PageForwardSprite);
PageForward_Height = sprite_get_height(PageForwardSprite);

PageBackward_Width = sprite_get_width(PageBackwardSprite);
PageBackward_Height = sprite_get_height(PageBackwardSprite);

CloseBook_X = 10;
CloseBook_Y = 10;
CloseBook_XX = CloseBook_X+CloseBook_Width;
CloseBook_YY = CloseBook_Y+CloseBook_Height;

CloseBook_LastPageX = room_width-(CloseBook_Width*1.2);
CloseBook_LastPageY = room_height-(CloseBook_Height*1.2);
CloseBook_LastPageXX = CloseBook_LastPageX+CloseBook_Width;
CloseBook_LastPageYY = CloseBook_LastPageY+CloseBook_Height;

Bookmark_X = room_width-(Bookmark_Width*2);
Bookmark_Y = 10;
Bookmark_XX = Bookmark_X+Bookmark_Width;
Bookmark_YY = Bookmark_Y+Bookmark_Height;

BgAudioOn_X = (room_width/2)-((BgAudioOn_Width+NarrationOn_Width)*0.5)-ParagraphSpacing;
BgAudioOn_Y = room_height-(BgAudioOn_Height*1.2);
BgAudioOn_XX = BgAudioOn_X+BgAudioOn_Width;
BgAudioOn_YY = BgAudioOn_Y+BgAudioOn_Height;

BgAudioOff_X = (room_width/2)-((BgAudioOff_Width+NarrationOff_Width)*0.5)-ParagraphSpacing;
BgAudioOff_Y = room_height-(BgAudioOff_Height*1.2);
BgAudioOff_XX = BgAudioOff_X+BgAudioOff_Width;
BgAudioOff_YY = BgAudioOff_Y+BgAudioOff_Height;

NarrationOff_X = BgAudioOff_XX+ParagraphSpacing;
NarrationOff_Y = room_height-(NarrationOff_Height*1.2);
NarrationOff_XX = NarrationOff_X+NarrationOff_Width;
NarrationOff_YY = NarrationOff_Y+NarrationOff_Height;

NarrationOn_X = BgAudioOn_XX+ParagraphSpacing;
NarrationOn_Y = room_height-(NarrationOn_Height*1.2);
NarrationOn_XX = NarrationOn_X+NarrationOn_Width;
NarrationOn_YY = NarrationOn_Y+NarrationOn_Height;

PageForward_X = room_width-(PageForward_Width*1.2);
PageForward_Y = room_height-(PageForward_Height*1.6);
PageForward_XX = PageForward_X+PageForward_Width;
PageForward_YY = PageForward_Y+PageForward_Height;

PageBackward_X = 10;
PageBackward_Y = room_height-(PageBackward_Height*1.6);
PageBackward_XX = PageBackward_X+PageBackward_Width;
PageBackward_YY = PageBackward_Y+PageBackward_Height;
