/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2010 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstdgraphics
 * Version: 1.05
 * First release date:   2010-11-08
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstdgraphics library
 * 
 * mzstdgraphics library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstdgraphics library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#include "mzAnimatedBitmap.h"
#include "mzstdgraphicsGlobals.h"
#include "mzFileUtils.h"
#include "mzMessageBox.h"

CAnimatedBitmap::CAnimatedBitmap(HINSTANCE hInst, CString base_fname)
    : CBitmap()
{
    this->setRealClassName(L"CAnimatedBitmap");
    this->fname = base_fname;
    this->_step = 0;
    this->_looping = FALSE;
    this->_rewind = FALSE;

    // sprawdzamy ile jest plików klatek animacji
    LONG i;
    CString num;
    for (i = 1; i <= 100; i++) {
        num = i;
        if (i < 10) {
            num.prepend('0');
        }
        if (!FileExists(base_fname + '_' + num + ".bmp")) {
            break;
        }
    }

    this->_framesCount = i - 1;
    this->_currentFrame = 0;
    CBitmap *bmp = new CBitmap(hInst, base_fname + "_01.bmp");
    this->_frameWidth = bmp->width();
    this->_createBlankBitmap(NULL, bmp->width() * this->_framesCount, bmp->height(), RGB(255, 255, 255));
    this->beginPaint();
    this->drawBitmap(bmp);
    delete bmp;

    for (i = 2; i <= this->_framesCount; i++) {
        num = i;
        if (i < 10) {
            num.prepend('0');
        }
        bmp = new CBitmap(hInst, base_fname + '_' + num + ".bmp");
        this->drawBitmap(bmp, (i-1)*this->_frameWidth, 0);
        delete bmp;
    }
    this->endPaint();
}

VOID CAnimatedBitmap::forward() {
    if (this->_currentFrame < this->_framesCount - 1) {
        this->_currentFrame++;
    } else {
        if (this->_looping) {
            this->_currentFrame = 0;
        } else {
            this->_step = 0;
            if (this->_rewind) {
                this->_currentFrame = 0;
            }
        }
    }
    this->_offsetX = this->_currentFrame * this->_frameWidth;
}

VOID CAnimatedBitmap::backward() {
    if (this->_currentFrame > 0) {
        this->_currentFrame--;
    } else {
        if (this->_looping) {
            this->_currentFrame = this->_framesCount - 1;
        } else {
            this->_step = 0;
        }
    }
    this->_offsetX = this->_currentFrame * this->_frameWidth;
}

VOID CAnimatedBitmap::play(BOOL forward) {
    if (forward) {
        this->_step = 1;
    } else {
        this->_step = -1;
    }
}

VOID CAnimatedBitmap::update() {
    if (this->_step == 1) {
        this->forward();
    } else if (this->_step == -1) {
        this->backward();
    }
}
