/* SCEI CONFIDENTIAL
PSML Page Development Tool Release 1.0
Copyright (C) 2002 Sony Computer Entertainment Inc.
All Rights Reserved.
*/

#if defined _POP_SKB_included
  #endinput
#endif
#define _POP_SKB_included

#include <all.inc>
#include "defs.inc"

static const TEXTFIELD[] = "texture/textfield.tm2";

#define FONTSIZE_EDIT 16


// mode
// 0 : デフォルト
// 1 : ユーザID入力
// 2 : パスワード入力

stock bool: pop_skb( const _title_[], _text_[], _mode_, _bufsize_ = sizeof _text_, _max_byte_ = 256 )
{
	new texture: tex_frame = Texture( TEXTFIELD );

	if( _bufsize_ * 4 < _max_byte_ ){ _max_byte_ = _bufsize_ * 4; }

    new obj: defocus_plane = CreateDefocus( Disp2D() );
	SetOptionI( defocus_plane, TAG("ZSRT"), ZSORT_TOP_MOST );
	SetOptionI( defocus_plane, TAG("LEVL"), 3 );
	SetColor( defocus_plane, 1.0, 1.0, 1.0, 0.0 );
	AnimColor( defocus_plane, 0.3, 0.3, 0.3, 1.0, 300.0 );

	new obj: skb_title = CreateLabel( defocus_plane );
	SetPos( skb_title, 95.0, 85.0, 0.0 );
	SetColor( skb_title, 1.0, 1.0, 1.0, 1.0 );
	SetColor( skb_title, 1.0, 1.0, 1.0, 1.0 );
	SetOptionI( skb_title, TAG("HALN"), ALIGN_BEGINNING );
	SetOptionI( skb_title, TAG("VALN"), ALIGN_CENTER );
	SetOptionI( skb_title, TAG("FSIZ"), FONTSIZE_EDIT );
	SetOptionS( skb_title, TAG("UTF8"), _title_ );

    new obj: skb_edit = CreateEdit( defocus_plane );

    if( _mode_ == 2 )
    {
        SetOptionI( skb_edit, TAG("PWMD"), '*' );
    }

	SetPos( skb_edit, 100.0, 110.0, 0.0 );
	SetColor( skb_edit, 1.0, 1.0, 1.0, 1.0 );
    SetOptionF( skb_edit, TAG("ZOFS"), 2.0 );
	SetOptionI( skb_edit, TAG("HALN"), ALIGN_BEGINNING );
	SetOptionI( skb_edit, TAG("VALN"), ALIGN_CENTER );
	SetOptionI( skb_edit, TAG("FSIZ"), FONTSIZE_NORMAL );
	SetOptionS( skb_edit, TAG("UTF8"), _text_ );
	SetOptionI( skb_edit, TAG("W64 "), 396 * 64 );
	SetOptionI( skb_edit, TAG("H64 "), 20 * 64 );
	SetOptionI( skb_edit, TAG("LM64"), 60 * 64 );
	SetOptionI( skb_edit, TAG("RM64"), 60 * 64 );

    {
		new bg_color[4];
		MakeCol4( bg_color, {  0, 0, 0, 0,   0, 0, 0, 0,
			                   0, 0, 0, 0,   0, 0, 0, 0 } );
		SetOptionP( skb_edit, TAG("BGC4"), bg_color );
    }

    // 枠
    new obj: skb_frame;
    {
        skb_frame = CreatePlane3x3( defocus_plane );
		SetPos( skb_frame, 300.0, 120.0, 0.0 );
        SetScale( skb_frame, 420.0, 48.0, 1.0 );
        SetColor( skb_frame, 1.0, 1.0, 1.0, 1.0 );
	   	SetTexture( skb_frame, tex_frame );
        SetOptionI( skb_frame, TAG("ALPH"), ALPHA_BLEND_ADD );
        SetOptionF( skb_frame, TAG("ZOFS"), 1.0 );

        // 格子の形
        {
        	new float: tex_mrgn[4] = { 0.5, 0.5, 0.5, 0.5 };
        	SetOptionP( skb_frame, TAG("MGNT"), tex_mrgn );

        	new float: scale[3];
        	GetScale( skb_frame, scale[0],scale[1],scale[2] );

        	new float: mrgn[4];

        	if( scale[0] > scale[1] )
        	{
        		mrgn[0] = mrgn[1] = 0.5 * scale[1] / scale[0];
        		mrgn[2] = mrgn[3] = 0.5;
        	}
        	else
        	{
        		mrgn[2] = mrgn[3] = 0.5 * scale[0] / scale[1];
        		mrgn[0] = mrgn[1] = 0.5;
        	}
        	SetOptionP( skb_frame, TAG("MGN"), mrgn );
        }
    }

    // ボタンヘルプの状態を退避
    skb_push_btnhelp();

    new result;
    switch( _mode_ )
    {
    case 1:
    	{
			new ucs4_list[] = !"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-=\\`_+|~[]{};:'\",.<>/?";

		    result = skbStart( skb_edit,
							   SKB_MODE_QWERTY,
							   SKB_INPPUT_MODE_FEPOFF,
							   SKB_OPTION_LOCK_FEP | SKB_OPTION_LOCK_SKB | SKB_OPTION_KANALOCK_FIX,
							   SKB_LIMIT_BYTE, _max_byte_,
							   ucs4_list, true
							 );
    	}

    case 2:
    	{
			new ucs4_list[] = !"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-=\\`_+|~[]{};:'\",.<>/?";

		    result = skbStart( skb_edit,
							   SKB_MODE_QWERTY,
							   SKB_INPPUT_MODE_FEPOFF,
							   SKB_OPTION_LOCK_FEP | SKB_OPTION_LOCK_SKB | SKB_OPTION_KANALOCK_FIX,
							   SKB_LIMIT_BYTE, _max_byte_,
							   ucs4_list, true
							 );
    	}

	default:
	    result = skbStart( skb_edit,
						   SKB_MODE_NOCHANGE,
						   SKB_INPPUT_MODE_NOCHANGE,
						   SKB_OPTION_NONE,
						   SKB_LIMIT_BYTE, _max_byte_
						 );
    }



    new bool:ret;
    if( result == -1 )
    {
        printf( "edit:" );
        getstring( _text_, _bufsize_ );
        sleep;

        ret = true;
    }
    else
    {
        if( skbWait() == SKB_OK )
        {
            skbGetString( _text_, _bufsize_ );
	        ret = true;
        }
        else
        {
	        ret = false;
        }
    }

    Destroy( defocus_plane );

    // ボタンヘルプの状態を復帰
    skb_pop_btnhelp();

    return ret;
}

// ファイルからテクスチャID作るブロッキング関数
stock texture: Texture( const url[], w=0, h=0, format=ORIGINAL )
{
	if( strlen(url)==0 ){ return texture:0; }

	new filename[256];
	sprintf( filename, url );

    // デコード起動
    imgDecStart( filename, w, h, format );

    // デコード待ち
    new result = imgDecWait();
    if( result != IMGDEC_OK ){ return texture:0; }

    return imgTexID();
}

static skb_padhelp_onoff;
static skb_padhelp_str_rd[32];
static skb_padhelp_str_ru[32];
static skb_padhelp_str_rl[32];
static skb_padhelp_str_rr[32];

stock skb_push_btnhelp()
{
    skb_padhelp_onoff = GetSystemParam( SYSTEM_PARAM_PADHELP_ONOFF );
    GetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RD, skb_padhelp_str_rd );
    GetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RU, skb_padhelp_str_ru );
    GetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RL, skb_padhelp_str_rl );
    GetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RR, skb_padhelp_str_rr );
}

stock skb_pop_btnhelp()
{
    SetSystemParam( SYSTEM_PARAM_PADHELP_ONOFF, skb_padhelp_onoff );
    SetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RD, skb_padhelp_str_rd );
    SetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RU, skb_padhelp_str_ru );
    SetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RL, skb_padhelp_str_rl );
    SetSystemParamStr( SYSTEM_PARAM_STR_PADHELP_RR, skb_padhelp_str_rr );
}
