AutoPlay Media Studio ile Özel Callback Fonksiyonu Oluşturma

guclusat

Tanınmış Üye
Süper Moderatör
Create a Custom Callback Function
If you have an action requiring a status dialog, but the standard status dialog (StatusDlg) is not sufficient, it is possible to create a custom callback function for those actions requiring one.

As an example, we will use a custom callback function to show the progress of a file download:

Kod:
--ShowStatusWindow shows the status window, and sets the dialog's icon
function ShowStatusWindow()
    -- Show the Status Dialog
    StatusDlg.Show(MB_ICONINFORMATION, false);
    -- Show the cancel button
    StatusDlg.ShowCancelButton(true, "Stop Download");
end



--DownloadStatus is the callback function to be used.
function DownloadStatus (BytesRead, FileSize, TransferRate, SecondsLeft, SecondsLeftFormat, Message)
    -- Set the status meter's range
    StatusDlg.SetMeterRange(0, Math.Floor(FileSize/1024));
    -- Set the meter position
    StatusDlg.SetMeterPos(Math.Floor(BytesRead/1024));

    -- Check if the cancel button was pressed
    if StatusDlg.IsCancelled() then
        -- Cancel button was pressed.  Hide status dialog and terminate download
        StatusDlg.Hide();
        return false;
    else
        -- Cancel button was not pressed, determine message to be displayed
        if FileSize == 0 then
            -- Display 'size unknown' message
            StatusDlg.SetStatusText("Size Unknown (" .. Math.Floor(BytesRead/1024) .. "kb downloaded so far)");
        elseif FileSize > 0 then
            -- Display 'downloaded of total' message
            StatusDlg.SetStatusText("Downloaded " .. Math.Floor(BytesRead/1024) .. " kb of " .. Math.Floor(FileSize/1024) .. " kb");
        end
    
        -- Determine if end of download was reached:
        --   Message will = "", the end of the download is a 'busy' state
        --   BytesRead will be > 0, because more than 1 byte will have been transferred
        --   BytesRead will = FileSize, unless FileSize is unknown (0)
        if (Message == "") and (BytesRead > 0) and (BytesRead == FileSize or FileSize == 0) then
           -- The download has completed, hide the status dialog and display complete message
           StatusDlg.Hide();
           Dialog.Message("Download Complete", Math.Floor(BytesRead/1024) .. " kb have been downloaded");
           return false;
        else
            -- The download has not ended, continue to download
            return true;
        end
    end
end




-- Call the ShowStatusWindow() function
ShowStatusWindow();

-- Download the file using a callback function
HTTP.Download("http://www.yourdomain.com/download_folder/file.ext", "c:\\file.ext", MODE_BINARY, 20, 80, nil, nil, DownloadStatus);

Note: ShowStatusWindow() shows the status window and sets the title. DownloadStatus() is the custom callback function, and is called by HTTP.Download.
 

AutoPlay Media Studio ile Özel Callback Fonksiyonu Oluşturma​

AutoPlay Media Studio'da standart StatusDlg penceresinin yeterli olmadığı durumlarda, durum bilgilerini kendiniz yönetmek için özel bir Callback Function (geri çağırma fonksiyonu) oluşturabilirsiniz.

Bu örnekte, HTTP üzerinden indirilen bir dosyanın indirme ilerlemesini göstermek, indirmenin durumunu takip etmek ve kullanıcı isterse indirmeyi iptal etmek için özel bir callback fonksiyonu kullanılmaktadır.

ShowStatusWindow() durum penceresini görüntüler ve iptal butonunu oluşturur. DownloadStatus() ise HTTP.Download tarafından çağrılan özel callback fonksiyonudur.

Aşağıdaki örnekte indirilen veri miktarı, toplam dosya boyutu ve indirme tamamlandığında gösterilecek mesaj kontrol edilmektedir.

Kod:
-- ShowStatusWindow durum penceresini gösterir
-- ve pencerenin simgesini ayarlar
function ShowStatusWindow()
-- Status Dialog'u göster
StatusDlg.Show(MB_ICONINFORMATION, false);

-- İptal butonunu göster
StatusDlg.ShowCancelButton(true, "İndirmeyi Durdur");
end


-- DownloadStatus kullanılacak callback fonksiyonudur
function DownloadStatus (BytesRead, FileSize, TransferRate, SecondsLeft, SecondsLeftFormat, Message)

-- Durum göstergesinin aralığını ayarla
StatusDlg.SetMeterRange(0, Math.Floor(FileSize/1024));

-- İlerleme göstergesinin konumunu ayarla
StatusDlg.SetMeterPos(Math.Floor(BytesRead/1024));

-- İptal butonuna basılıp basılmadığını kontrol et
if StatusDlg.IsCancelled() then

-- İptal edildi, durum penceresini gizle ve indirmeyi sonlandır
StatusDlg.Hide();
return false;

else

-- Gösterilecek mesajı belirle
if FileSize == 0 then

-- Dosya boyutu bilinmiyor
StatusDlg.SetStatusText(
"Boyut bilinmiyor (" ..
Math.Floor(BytesRead/1024) ..
" KB indirildi)"
);

elseif FileSize > 0 then

-- İndirilen / toplam dosya boyutunu göster
StatusDlg.SetStatusText(
Math.Floor(BytesRead/1024) ..
" KB / " ..
Math.Floor(FileSize/1024) ..
" KB indirildi"
);
end

-- İndirmenin tamamlanıp tamamlanmadığını kontrol et
if (Message == "") and
(BytesRead > 0) and
(BytesRead == FileSize or FileSize == 0) then

-- İndirme tamamlandı
StatusDlg.Hide();

Dialog.Message(
"İndirme Tamamlandı",
Math.Floor(BytesRead/1024) ..
" KB veri indirildi"
);

return false;

else

-- İndirme devam ediyor
return true;
end
end
end


-- ShowStatusWindow() fonksiyonunu çağır
ShowStatusWindow();


-- Callback fonksiyonu kullanarak dosyayı indir
HTTP.Download(
"http://www.yourdomain.com/download_folder/file.ext",
"c:\\file.ext",
MODE_BINARY,
20,
80,
nil,
nil,
DownloadStatus
);


Çalışma Mantığı​

ShowStatusWindow() durum penceresini görüntüler ve indirmeyi durdurmak için iptal butonunu ekler.

DownloadStatus() indirme sırasında HTTP.Download tarafından çağrılır. İndirilen veri miktarını kontrol eder, ilerleme çubuğunu günceller ve kullanıcı iptal butonuna bastığında indirmeyi sonlandırır.

İndirme tamamlandığında durum penceresi kapatılır ve kullanıcıya indirme işleminin tamamlandığını belirten bir mesaj gösterilir.

Bu yöntem sayesinde AutoPlay Media Studio projelerinde standart durum penceresinden daha kontrollü ve özelleştirilebilir dosya indirme işlemleri oluşturabilirsiniz.
 
Geri
Yukarı