pagemanage.c 12.6 KB

#include "viewer.h"
#include "status.h"
#include "uilogic.h"
#include "font.h"
#include "gzip.h"
#include "texrect.h"
#include "page.h"
#include "pagetop.h"
#include "pagemanage.h"
#include "pagegpak.h"
#include "pagecpak.h"
#include "pagecontrol.h"
#include "texture.h"
#include "texture/cyes_gz.c"
#include "texture/cyesh_gz.c"
#include "texture/cno_gz.c"
#include "texture/cnoh_gz.c"
#include "texture/cancel_gz.c"
#include "texture/cancelh_gz.c"
#include "texture/pcpwdin_gz.c"
#include "texture/uparrow_gz.c"
#include "texture/downarrow_gz.c"
#include "audrenderer.h"

extern PCTimer gTimer;

#define  PAGE_MANAGE_TOTAL_ROWS      3

int      gPageManageInit = 0;
UiTable  gPageManageTable;
UiRow    gPageManageRow[ PAGE_MANAGE_TOTAL_ROWS ];
UiImage  gButtonImg[ PAGE_MANAGE_TOTAL_ROWS ];

UiTable gConfirmTable;
UiImage gCYesImg;
UiImage gCYeshImg;
UiImage gCNoImg;
UiImage gCNohImg;
UiImage gCancelImg;
UiImage gCancelhImg;
UiRow   gConfirmRow[2];

u8 contVal[MAX_CONT_FIELDS];
u8 lower[MAX_CONT_FIELDS] = {0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 3, 1, 1};
u8 upper[MAX_CONT_FIELDS] = {9, 9, 9, 9, 99, 12, 31, 23, 59, 80, 99, 12, 31};
int gWhich;
int gPcFound = 0;
char gTmpStrBuf[256];
int  pwd_x[]={ (SCREEN_WD-TEX_BOARD_W)/2+(TEX_BOARD_W-64)/2, 
               (SCREEN_WD-TEX_BOARD_W)/2+(TEX_BOARD_W-64)/2+16, 
               (SCREEN_WD-TEX_BOARD_W)/2+(TEX_BOARD_W-64)/2+32, 
               (SCREEN_WD-TEX_BOARD_W)/2+(TEX_BOARD_W-64)/2+48};
int  pwd_y[]={(SCREEN_HT-TEX_BOARD_H)/2 + 70, 
              (SCREEN_HT-TEX_BOARD_H)/2 + 70,
              (SCREEN_HT-TEX_BOARD_H)/2 + 70,
              (SCREEN_HT-TEX_BOARD_H)/2 + 70};

void onManageRowHilite(int idx);
void onManageRowSelect(int idx, u32 button);
void drawPageManageBg();
void initPageManageAssets();
void initConfirmTable(int num);
void updatePwdFields();
void drawPwdFields();

static int gPrevManageIdx=0;

void initPageManage(int row)
{
    int i;

    gPageNo = PAGE_MANAGE;
    osBbRtcGet(gpDate+0, gpDate+1, gpDate+2, gpDate+3,
               gpDate+4, gpDate+5, gpDate+6);
    gPcFound = uiInitContVal(contVal, lower, upper); 

    initPageManageAssets();

    for ( i=0; i<PAGE_MANAGE_TOTAL_ROWS; i++) {
        gButtonImg[i].w = TOP_BUTTON_W; 
        gButtonImg[i].h = TOP_BUTTON_H; 
        gButtonImg[i].img = (i==0) ? (u16*)texB1 : 
                            (i==1) ? (u16*)texB2 : (u16*)texB3;

        gPageManageRow[i].col_cnt = 1;
        gPageManageRow[i].cell_h = TOP_BUTTON_H + 10;

        gPageManageRow[i].cell_w[0] = TOP_BUTTON_W;
        gPageManageRow[i].cell_x[0] = 0;
        gPageManageRow[i].cell_y[0] = 0;
        gPageManageRow[i].type[0] = CELL_TYPE_TEXTURE;
        gPageManageRow[i].data[0] = (void*)(&gButtonImg[i]);
        gPageManageRow[i].txt_color = GPACK_RGBA5551(200, 200, 200, 1); 
        gPageManageRow[i].bg_color  = 0; 
        gPageManageRow[i].bg = NULL; 
    }

    gPageManageTable.x           = TOP_BUTTON_X;
    gPageManageTable.y           = B1_Y;
    gPageManageTable.total_rows  = PAGE_MANAGE_TOTAL_ROWS;
    gPageManageTable.screen_rows = PAGE_MANAGE_TOTAL_ROWS;
    gPageManageTable.hilite_row  = row;
    gPageManageTable.top_row     = 0;
    gPageManageTable.rows        = gPageManageRow;
    gPageManageTable.row_layout  = ROW_LAYOUT_VERTICAL;

    gPrevManageIdx = row;
    gWhich = 0;
}

void initConfirmTable(int num)
{
    int i;

    for (i=0; i<num; i++) {
        gConfirmRow[i].col_cnt = 1;
        gConfirmRow[i].cell_h  = TEX_CYES_H + 2;
        gConfirmRow[i].txt_color = GPACK_RGBA5551(200, 200, 200, 1);
        gConfirmRow[i].bg_color  = 0;
        gConfirmRow[i].bg = NULL;
        if (num==2) 
            gConfirmRow[i].cell_w[0]  = TEX_CYES_W + 2*TEX_CYES_W;
        else if (num==3)
            gConfirmRow[i].cell_w[0]  = TEX_CYES_W + TEX_CANCEL_W;
        gConfirmRow[i].cell_x[0]  = 0;
        gConfirmRow[i].cell_y[0]  = 0;
        gConfirmRow[i].type[0]  = CELL_TYPE_TEXTURE;
        gConfirmRow[i].data[0]  = (i==0) ? (void*)(&gCYesImg) : 
                                  ((i==1) ? (void*)(&gCNoImg) : (void*)(&gCancelImg)); 
    }

    if (num==3) {
        gConfirmTable.x  = (SCREEN_WD-TEX_BOARD_W)/2 + TEX_BOARD_W/4 - 20; 
        gConfirmTable.y  = (SCREEN_HT-TEX_BOARD_H)/2 + TEX_BOARD_H/2 + 10;
    } else if (num==2) {
        gConfirmTable.x  = (SCREEN_WD-TEX_BOARD_W)/2 + TEX_BOARD_W/4; 
        gConfirmTable.y  = (SCREEN_HT-TEX_BOARD_H)/2 + TEX_BOARD_H/2 + 20;
    }
    gConfirmTable.total_rows  = num;
    gConfirmTable.screen_rows = num;
    if (num==2)
        gConfirmTable.hilite_row  = 1; 
    else if (num==3)
        gConfirmTable.hilite_row  = 0; 
    gConfirmTable.top_row     = 0;
    gConfirmTable.rows        = gConfirmRow;
    gConfirmTable.row_layout  = ROW_LAYOUT_HORIZONTAL;
}

void doPageManage()
{
    drawPageManageBg();

    switch (gStatus) {
    case VW_OK:
        updateRowHilite( &gPageManageTable );
        drawTable( &gPageManageTable, &onManageRowHilite, &onManageRowSelect );
        break;
    case VW_PC_PWD:
        if (gPcFound) {
            updatePwdFields();
            drawPwdFields();

            if (gButton==CONT_A) {
                gButton = 0;
                if (contVal[0]==gTimer.pwd[0] &&
                    contVal[1]==gTimer.pwd[1] &&
                    contVal[2]==gTimer.pwd[2] &&
                    contVal[3]==gTimer.pwd[3]) {
                    gStatus = VW_OK;
                    playSound(SOUND_MENU_DOWN, AUDIO_WAIT);
                    initPageControl();
                } else {
                    gStatus = VW_ERR_WRONG_PWD;
                }
            } else if (gButton==CONT_B) {
                gButton = 0;
                playSound(SOUND_MENU_UP, AUDIO_WAIT);
                gStatus = VW_OK;
            }
        } else {
            gStatus = VW_OK;
            initPageControl();
        }
        break;
    case VW_ERR_FATAL:
        drawError(gsError_2, 
                  (TEX_BOARD_W - 8*(TEX_ZH_GLYPH_W+1))/2,
                  (TEX_BOARD_H - (TEX_ZH_GLYPH_H+2))/2 );
        break;
    case VW_ERR_FS:
        drawError(gsError_3, 
                  (TEX_BOARD_W - 8*(TEX_ZH_GLYPH_W+1))/2,
                  (TEX_BOARD_H - (TEX_ZH_GLYPH_H+2))/2 );
        break;
    case VW_ERR_ID_MISMATCH:
        drawError( gsError_4, 
                   (TEX_BOARD_W - 11*(TEX_ZH_GLYPH_W+1))/2,
                   (TEX_BOARD_H - 2*(TEX_ZH_GLYPH_H+2))/2 );
        break;
    case VW_ERR_NO_CARD:
        drawError( gsError_5, 
                   (TEX_BOARD_W - 11*(TEX_ZH_GLYPH_W+1))/2,
                   (TEX_BOARD_H - 2*(TEX_ZH_GLYPH_H+2))/2 );
        break;
    case VW_ERR_WRONG_PWD:
        if (gButton==CONT_B) {
            gButton = 0;
            playSound(SOUND_MENU_UP, AUDIO_WAIT);
            gStatus = VW_OK;
            return;
        }
        drawError( gsError_11, 
                   (TEX_BOARD_W - 5*(TEX_ZH_GLYPH_W+1))/2,
                   (TEX_BOARD_H - (TEX_ZH_GLYPH_H+2))/2 );
        break;
    }
}

void updatePwdFields()
{
    switch (gMove) {
        case VW_MOVE_RIGHT:
             if (gWhich==3) gWhich=0;
             else gWhich++;
             break;
        case VW_MOVE_LEFT:
             if (gWhich==0) gWhich=3;
             else gWhich--;
             break;
        case VW_MOVE_UP:
             if (contVal[gWhich]==upper[gWhich]) 
                 contVal[gWhich] = lower[gWhich];
             else
                 contVal[gWhich] ++;
             break;
        case VW_MOVE_DOWN:
             if (contVal[gWhich]==lower[gWhich]) 
                 contVal[gWhich] = upper[gWhich];
             else
                 contVal[gWhich] --;
             break;
    }
    gMove = VW_MOVE_STILL;
}

void drawPwdFields()
{
    int i;

    drawTable( &gPageManageTable, NULL, NULL );
    drawTexRectCI ( texBoard,
                 G_IM_FMT_CI, G_IM_SIZ_8b,
                 TEX_BOARD_W, TEX_BOARD_H,
                 32, 32, 1<<10, 1<<10,
                 TEX_BOARD_W, TEX_BOARD_H,
                 (SCREEN_WD-TEX_BOARD_W)/2, (SCREEN_HT-TEX_BOARD_H)/2);
    drawTexRect( texPcPwdIn,
                 G_IM_FMT_RGBA, G_IM_SIZ_16b,
                 TEX_PC_PWDIN_W, TEX_PC_PWDIN_H,
                 32, 32, 1<<10, 1<<10,
                 TEX_PC_PWDIN_W, TEX_PC_PWDIN_H,
                 (SCREEN_WD-TEX_BOARD_W)/2 +(TEX_BOARD_W-TEX_PC_PWDIN_W)/2,
                 (SCREEN_HT-TEX_BOARD_H)/2 + 20);

    for (i=0; i<4; i++) {
        sprintf(gTmpStrBuf, "%d", contVal[i]);
        if (gWhich==i) {
            drawColorString(gTmpStrBuf, pwd_x[i], pwd_y[i],
                            GPACK_RGBA5551(240, 50, 0, 0));
            drawTexRect ( texUpArrow,
                          G_IM_FMT_RGBA, G_IM_SIZ_16b,
                          TEX_ARROW_W, TEX_ARROW_H,
                          32, 32, 1<<10, 1<<10,
                          TEX_ARROW_W, TEX_ARROW_H,
                          pwd_x[i]+1, pwd_y[i]-10);
            drawTexRect ( texDownArrow,
                          G_IM_FMT_RGBA, G_IM_SIZ_16b,
                          TEX_ARROW_W, TEX_ARROW_H,
                          32, 32, 1<<10, 1<<10,
                          TEX_ARROW_W, TEX_ARROW_H,
                          pwd_x[i]+1, pwd_y[i]+18);
 
        } else {
            drawColorString(gTmpStrBuf, pwd_x[i], pwd_y[i], 0xFFFF);
        }
    }
}

void onManageRowHilite(int idx)
{
    int j;


    if (idx != gPrevManageIdx) {
        gPrevManageIdx = idx;
        playSound(SOUND_SELECTION_MOVE, AUDIO_NOWAIT);
    }

    for (j=0; j<gPageManageTable.total_rows; j++) {
        if (j!=idx) {
            gButtonImg[j].img = (j==0) ? (u16*)texB1 : (j==1 ? (u16*)texB2 : (u16*)texB3);
            gPageManageRow[j].txt_color = GPACK_RGBA5551(200, 200, 200, 1);
        } else {
            gButtonImg[j].img = (j==0) ? (u16*)texHB1 : (j==1 ? (u16*)texHB2 : (u16*)texHB3);
            gPageManageRow[j].txt_color = GPACK_RGBA5551(200, 200, 0, 1);
        }
    }
}

void onManageRowSelect(int idx, u32 button)
{
    if (button==CONT_A) {
        playSound(SOUND_MENU_DOWN, AUDIO_NOWAIT);
        switch (idx) {
            case 0: initPageGpak(0, 0); break;
            case 1: initPageCpak(0); break;
            case 2: gStatus=VW_PC_PWD; break;
        }
    } else if (button==CONT_B) {
        gButton = 0;
        playSound(SOUND_MENU_UP, AUDIO_NOWAIT);
        switch (idx) {
            case 0: initPageTop(2); break;
            case 1: initPageTop(2); break;
            case 2: 
                if (gStatus!=VW_PC_PWD && gStatus!=VW_ERR_WRONG_PWD) {
                   initPageTop(2);        
                } 
                break;
        }

    }
}

void initPageManageAssets()
{
    int outsize;

    if (!gPageManageInit) {

        bzero(texConfirmYes, TEX_CYES_W*TEX_CYES_H*2);
        outsize = expand_gzip( gzTexConfirmYes, texConfirmYes, gzTexConfirmYesSize, TEX_CYES_W*TEX_CYES_H*2);
        bzero(texConfirmYesH, TEX_CYES_W*TEX_CYES_H*2);
        outsize = expand_gzip( gzTexConfirmYesH, texConfirmYesH, gzTexConfirmYesHSize, TEX_CYES_W*TEX_CYES_H*2);
        bzero(texConfirmNo, TEX_CNO_W*TEX_CNO_H*2);
        outsize = expand_gzip( gzTexConfirmNo, texConfirmNo, gzTexConfirmNoSize, TEX_CNO_W*TEX_CNO_H*2);
        bzero(texConfirmNoH, TEX_CNO_W*TEX_CNO_H*2);
        outsize = expand_gzip( gzTexConfirmNoH, texConfirmNoH, gzTexConfirmNoHSize, TEX_CNO_W*TEX_CNO_H*2);
        bzero(texCancel, TEX_CANCEL_W*TEX_CANCEL_H*2);
        outsize = expand_gzip( gzTexCancel, texCancel, gzTexCancelSize, TEX_CANCEL_W*TEX_CANCEL_H*2);
        bzero(texCancelH, TEX_CANCEL_W*TEX_CANCEL_H*2);
        outsize = expand_gzip( gzTexCancelH, texCancelH, gzTexCancelHSize, TEX_CANCEL_W*TEX_CANCEL_H*2);
        bzero(texPcPwdIn, TEX_PC_PWDIN_W * TEX_PC_PWDIN_H * 2);
        outsize = expand_gzip( gzTexPcPwdIn, texPcPwdIn, gzTexPcPwdInSize, TEX_PC_PWDIN_W * TEX_PC_PWDIN_H * 2);
        bzero(texUpArrow, TEX_ARROW_W * TEX_ARROW_H * 2);
        outsize = expand_gzip( gzTexUpArrow, texUpArrow, gzTexUpArrowSize, TEX_ARROW_W * TEX_ARROW_H * 2);
        bzero(texDownArrow, TEX_ARROW_W * TEX_ARROW_H * 2);
        outsize = expand_gzip( gzTexDownArrow, texDownArrow, gzTexDownArrowSize, TEX_ARROW_W * TEX_ARROW_H * 2);

        gCYesImg.w = TEX_CYES_W;
        gCYesImg.h = TEX_CYES_H;
        gCYesImg.img = (u16*)texConfirmYes;
        gCYeshImg.w = TEX_CYES_W;
        gCYeshImg.h = TEX_CYES_H;
        gCYeshImg.img = (u16*)texConfirmYesH;

        gCNoImg.w = TEX_CNO_W;
        gCNoImg.h = TEX_CNO_H;
        gCNoImg.img = (u16*)texConfirmNo;
        gCNohImg.w = TEX_CNO_W;
        gCNohImg.h = TEX_CNO_H;
        gCNohImg.img = (u16*)texConfirmNoH;

        gCancelImg.w = TEX_CANCEL_W;
        gCancelImg.h = TEX_CANCEL_H;
        gCancelImg.img = (u16*)texCancel;
        gCancelhImg.w = TEX_CANCEL_W;
        gCancelhImg.h = TEX_CANCEL_H;
        gCancelhImg.img = (u16*)texCancelH;

        gPageManageInit = 1;
    }
}

void drawPageManageBg()
{
    drawFrame(PAGE_MANAGE);
}