/*****************************************************************************/
/*         FM-Towns 用 MS-DOS画面テキスト文字色変更プログラム                */
/*                         Copyright(C) June 19th,1991 by T.J.SHIN           */
/*                         Special Thanks for ま〜くん師匠                   */
/*****************************************************************************/
/*
  履歴
------------------------------------------------------------------------------
  1991.06.19 T.J.SHIN 作成
------------------------------------------------------------------------------
*******************************************************************************
  編集画面レイアウト
*******************************************************************************

                   ******************************************
                   *      MS-DOS文字色変更プログラム V1.0   *
                   *             Copyrigt(C) June 19,1991   *
                   *             Made       by T.J.SHIN     *
                   *             Given Idia by ありおん     *
                   *             Assist     by ま〜くん師匠 *
                   ******************************************

                              文字色用色識別番号

                        低輝度用               高輝度用
                      1 2 3 4 5 6 7          9101112131415
                     ■■■■■■■         ■■■■■■■

                   変更する色識別番号      赤色  緑色  青色
                           __    ■         __    __    __


*******************************************************************************
*/
#include <stdio.h>
#include <fif.h>

/* ﾊﾟﾚｯﾄ色変更ﾙｰﾁﾝ */
extern void chg_pal( ) ;

#define ESC    0x1b
#define CUR_R  0x1c
#define CUR_L  0x1d
#define CUR_U  0x1e
#define CUR_D  0x1f
#define M_get  0x0ff00
#define M_code 0x0ff

void main()
{
   int f_end=0 ;               /* 処理終了フラグ */
   int ret,lpc ;               /* 戻り値,loopカウント用 */
   int mode ;                  /* 処理モード(0:読み取り,1:書き込み) */
   int n_c[4] ;                /* パレット番号及び各色(赤,緑,青)の割当数 */
   int lc_set ;                /* 設定する項目の位置 */
   unsigned int  moji,encode ;
   static char *tit01="******************************************" ;
   static char *tit02="*      MS-DOS文字色変更プログラム V1.0   *" ;
   static char *tit03="*             Copyrigt(C) June 20,1991   *" ;
   static char *tit04="*             Made       by T.J.SHIN     *" ;
   static char *tit05="*             Given Idia by ありおん     *" ;
   static char *tit06="*             Assist     by ま〜くん師匠 *" ;
   static char *tit07="******************************************" ;
   static char *tit1[16]={ "[0;30m■[0m", /* 低輝度色識別番号０:変更不可 */
                           "[0;34m■[0m", /* 低輝度色識別番号１          */
                           "[0;31m■[0m", /* 低輝度色識別番号２          */
                           "[0;35m■[0m", /* 低輝度色識別番号３          */
                           "[0;32m■[0m", /* 低輝度色識別番号４          */
                           "[0;36m■[0m", /* 低輝度色識別番号５          */
                           "[0;33m■[0m", /* 低輝度色識別番号６          */
                           "[0;37m■[0m", /* 低輝度色識別番号７          */
                           "[1;30m■[0m", /* 高輝度色識別番号０:変更不可 */
                           "[1;34m■[0m", /* 高輝度色識別番号１          */
                           "[1;31m■[0m", /* 高輝度色識別番号２          */
                           "[1;35m■[0m", /* 高輝度色識別番号３          */
                           "[1;32m■[0m", /* 高輝度色識別番号４          */
                           "[1;36m■[0m", /* 高輝度色識別番号５          */
                           "[1;33m■[0m", /* 高輝度色識別番号６          */
                           "[1;37m■[0m"  /* 高輝度色識別番号７          */
                     } ;
   static char *tit2="文字色用",*tit3="色識別番号" ;
   static char *tit4="低輝度用               高輝度用" ;
   static char *tit6=" 1 2 3 4 5 6 7          9101112131415";
   static char *tit8="変更する",*tit9="赤色　緑色　青色" ;
   static int co_x[4] = { 28 , 45 , 51 , 57 } ;

   /* 初期画面出力 */

   VDB_init() ; /* 画面の初期化     */

   VDB_setposcus(20, 1) ;  VDB_outstr(42,tit01) ;
   VDB_setposcus(20, 2) ;  VDB_outstr(42,tit02) ;
   VDB_setposcus(20, 3) ;  VDB_outstr(42,tit03) ;
   VDB_setposcus(20, 4) ;  VDB_outstr(42,tit04) ;
   VDB_setposcus(20, 5) ;  VDB_outstr(42,tit05) ;
   VDB_setposcus(20, 6) ;  VDB_outstr(42,tit06) ;
   VDB_setposcus(20, 7) ;  VDB_outstr(42,tit07) ;

   VDB_setposcus(31, 9) ;  VDB_outstr(8,tit2) ;
   VDB_setposcus(39, 9) ;  VDB_outstr(10,tit3) ;
   VDB_setposcus(25,11) ;  VDB_outstr(31,tit4) ;
   VDB_setposcus(22,12) ;  VDB_outstr(37,tit6) ;
   VDB_setposcus(22,13) ;  for (lpc=1;lpc< 8;lpc++) VDB_outstr(13,tit1[lpc]) ;
   VDB_setposcus(45,13) ;  for (lpc=9;lpc<16;lpc++) VDB_outstr(13,tit1[lpc]) ;

   VDB_setposcus(20,16) ;  VDB_outstr(8,tit8) ;
   VDB_setposcus(28,16) ;  VDB_outstr(10,tit3) ;
   VDB_setposcus(44,16) ;  VDB_outstr(16,tit9) ;

   lc_set = 0 ; n_c[0]=1 ;
   VDB_setposcus(co_x[0],17) ;
   printf("%2d",n_c[0]) ;
   VDB_setposcus(34,17) ; VDB_outstr(13,tit1[(n_c[lc_set])]) ;
   chg_pal(0 , n_c[0], &n_c[1], &n_c[2], &n_c[3]) ;
   VDB_setposcus(co_x[1],17) ;
   printf("%2d    %2d    %2d", n_c[1],n_c[2],n_c[3]);

   VDB_setposcus(co_x[lc_set],17) ;

   /* 色識別番号の色変更メイン */
   while ( f_end == 0 )
   {
       moji = KYB_read(1,&encode) ;
       if ( (moji & M_get) != 0 ) continue ; /* キー入力無し */
       moji = moji & M_code ;
       switch(moji)
       {
           case ESC :     /* Program terminate */
              f_end = 1 ;
              break ;
           case CUR_R :   /* Selecting changing part */
              lc_set++;
              if ( lc_set >= 3 ) lc_set = 3;
              VDB_setposcus(co_x[lc_set],17) ;
              break ;
           case CUR_L :   /* Selecting changing part */
              lc_set--;
              if ( lc_set <= 0 ) lc_set = 0;
              VDB_setposcus(co_x[lc_set],17) ;
              break ;
           case CUR_U :   /* Countupping  palette no. or color no. */
              n_c[lc_set]++;
              if ( lc_set == 0 )
              {
                  if ( n_c[lc_set] >= 15 ) n_c[lc_set] = 15;
                  if ( n_c[lc_set] ==  8 ) n_c[lc_set] =  9;
                  printf("%2d",n_c[lc_set]) ;
                  VDB_setposcus(34,17) ; VDB_outstr(13,tit1[(n_c[lc_set])]) ;
                  VDB_setposcus(co_x[lc_set],17) ;
                  chg_pal(0 , n_c[0], &n_c[1], &n_c[2], &n_c[3]) ;
                  VDB_setposcus(co_x[1],17) ;
                  printf("%2d    %2d    %2d", n_c[1],n_c[2],n_c[3]);
                  VDB_setposcus(co_x[lc_set],17) ;
              }
              else
              {
                  if ( n_c[lc_set] >= 15 ) n_c[lc_set] = 15;
                  printf("%2d",n_c[lc_set]) ;
                  VDB_setposcus(co_x[lc_set],17) ;
                  chg_pal(1 , n_c[0], n_c[1], n_c[2], n_c[3]) ;
              }
              break ;
           case CUR_D :   /* Countdowning palette no. or color no. */
              n_c[lc_set]--;
              if ( lc_set == 0 )
              {
                  if ( n_c[lc_set] <= 1 ) n_c[lc_set] = 1;
                  if ( n_c[lc_set] == 8 ) n_c[lc_set] = 7;
                  printf("%2d",n_c[lc_set]) ;
                  VDB_setposcus(34,17) ; VDB_outstr(13,tit1[(n_c[lc_set])]) ;
                  VDB_setposcus(co_x[lc_set],17) ;
                  chg_pal(0 , n_c[0], &n_c[1], &n_c[2], &n_c[3]) ;
                  VDB_setposcus(co_x[1],17) ;
                  printf("%2d    %2d    %2d", n_c[1],n_c[2],n_c[3]);
                  VDB_setposcus(co_x[lc_set],17) ;
              }
              else
              {
                  if ( n_c[lc_set] <= 0 ) n_c[lc_set] = 0;
                  printf("%2d",n_c[lc_set]) ;
                  VDB_setposcus(co_x[lc_set],17) ;
                  chg_pal(1 , n_c[0], n_c[1], n_c[2], n_c[3]) ;
              }
              break ;
       }

   }

   VDB_cls() ; /* 画面のクリア */
}
