存储F1:来自回调的按钮值(FLTK)

时间:2015-12-10 20:32:50

标签: c++ fltk

我正在尝试用FLTK库编写一个模拟国际象棋游戏的程序。

我的问题是我想在二维数组按钮上做两个回调,我想点击一个按钮,然后点击另一个按钮,当点击第二个按钮时,第一个按钮的标签切换到标签第二个按钮,然后删除第一个按钮的标签。

我觉得我需要存储单击的第一个按钮的值,例如,如果我单击FBoard [1] [2],我有一个等于FBoard的变量[1] [2]并打开第二次点击将FBoard [1] [2]的标签替换为空白(假设FBoard [1] [2]是板上有一块的单元格。但我不确定如何,甚至是否这是正确的方法。

这是cpp文件:

  #include"Window.h"
  #include"ChessBoard.h"

  const char *  DisplayWindow::LastLabel;
  bool   DisplayWindow::flag;
  DisplayWindow::DisplayWindow(int width, int height, const char*)
  :Fl_Window(800, 650, "Chess"){   


                for (int X = 0; X <= 7; ++X){
                    for (int Y = 0; Y <= 7; ++Y){
                    // Leaves all positions that arent occupied by
                    // a figure at the start of the game blank
                        FBoard[X][Y] = new Fl_Button(10 + 50*Y, 100 + 50*X, 50, 50,"");
                        FBoard[X][Y]->callback((Fl_Callback*)DisplayWindow::ChangeButton);
                    }
                }

         flag = false;
         MakeLabel(); 
         LabelButton(); 
         show();
}


 DisplayWindow::~DisplayWindow(){}

 void DisplayWindow::MakeLabel(){

  for (int X = 0; X <= 7; ++X){
    for (int Y = 0; Y <= 7; ++Y){

            LBoard[X][Y] = ("  ");
    }
  }

  for (int X = 0; X <= 7; ++X){ 
        LBoard[1][X] = ("WP");
    // Occupies second row with white pawns

  }
    LBoard[0][0] = ("WR");
    LBoard[0][1] = ("WH");
    LBoard[0][2] = ("WB");
    LBoard[0][3] = ("WQ");
    LBoard[0][4] = ("WK");
    LBoard[0][5] = ("WB");
    LBoard[0][6] = ("WH");
    LBoard[0][7] = ("WR");


  for (int X = 0; X <= 7; ++X){
        LBoard[6][X] = ("BP");
  }

    LBoard[7][0] = ("BR");
    LBoard[7][1] = ("BH");
    LBoard[7][2] = ("BB");
    LBoard[7][3] = ("BQ");
    LBoard[7][4] = ("BK");
    LBoard[7][5] = ("BB");
    LBoard[7][6] = ("BH");
    LBoard[7][7] = ("BR");
}


void DisplayWindow::LabelButton(){

    for (int X = 0; X <= 7; ++X){ 
        FBoard[1][X]->label(LBoard[1][X]);
    // Occupies second row with white pawns

    }
    FBoard[0][0]->label(LBoard[0][0]);
    FBoard[0][1]->label(LBoard[0][1]);
    FBoard[0][2]->label(LBoard[0][2]);
    FBoard[0][3]->label(LBoard[0][3]);
    FBoard[0][4]->label(LBoard[0][4]);
    FBoard[0][5]->label(LBoard[0][5]);
    FBoard[0][6]->label(LBoard[0][6]);
    FBoard[0][7]->label(LBoard[0][7]);


    for (int X = 0; X <= 7; ++X){
        FBoard[6][X]->label(LBoard[6][X]);
    }

    FBoard[7][0] ->label(LBoard[7][0]);
    FBoard[7][1] ->label(LBoard[7][1]);
    FBoard[7][2] ->label(LBoard[7][2]);
    FBoard[7][3] ->label(LBoard[7][3]);
    FBoard[7][4] ->label(LBoard[7][4]);
    FBoard[7][5] ->label(LBoard[7][5]);
    FBoard[7][6] ->label(LBoard[7][6]);
    FBoard[7][7] ->label(LBoard[7][7]);

    for(int i=0 ; i<=7 ; i++)
    {
        for(int j=0 ; j<=7 ; j++)
        {
            int k=i+j;

                    if(k % 2 != 0 ){
                            FBoard[i][j]->color(FL_WHITE);
                    }

                    else if (k % 2 ==0 ){ 
                            FBoard[i][j]->color(FL_YELLOW); 
                    }
        }
    }
 }
 void DisplayWindow::ChangeButton(Fl_Widget * o, void * v){
    DisplayWindow* Win = (DisplayWindow *) v;
    Fl_Button * NewBoard = (Fl_Button*) o;

      if (Win->flag == false){
        DisplayWindow::LastLabel =  NewBoard->label();
        NewBoard->label("  ");
        Win->flag = true;
      }
      else{
         NewBoard->label(DisplayWindow::LastLabel);
         Win->flag = false;
      }         
 }

Header文件:

class DisplayWindow: public Fl_Window {
public:

DisplayWindow(int width, int height, const char* title=0);

virtual ~DisplayWindow();

void MakeLabel();
void LabelButton();
static void ChangeButton(Fl_Button * o, void * );
static bool flag;
bool flag1;
static const char * LastLabel;

private:
Fl_Button *  FBoard[8][8];
char * LBoard[8][8];
};

#endif

1 个答案:

答案 0 :(得分:0)

CPP:

'*** Check Exist Files ***'
If Not objFSO.FileExists(Server.MapPath(sFileName)) Then
    Response.write("File not found.")
Else
    '*** Open Files ***'
    Set oInStream = objFSO.OpenTextFile(Server.MapPath(sFileName),1,False)
End If

H:

#include"Window.h"
#include"ChessBoard.h"

const char *  DisplayWindow::LastLabel;
bool   DisplayWindow::flag;
DisplayWindow::DisplayWindow(int width, int height, const char*)
:Fl_Window(800, 650, "Chess"){   


                             for (int X = 0; X <= 7; ++X){
                                    for (int Y = 0; Y <= 7; ++Y){
         // Leaves all positions that arent occupied by
         // a figure at the start of the game blank
                                        FBoard[X][Y] = new Fl_Button(10 + 50*Y, 100 + 50*X, 50, 50,"");
                                        FBoard[X][Y]->callback((Fl_Callback*)DisplayWindow::ChangeButton);


            //FBoard[X][Y]->callback(ChangeButton);
                                    }
                             }
                             flag = false;
                             MakeLabel(); 
                             LabelButton(); 
                             show();
                          }

DisplayWindow::~DisplayWindow(){}

void DisplayWindow::MakeLabel(){

  for (int X = 0; X <= 7; ++X){
    for (int Y = 0; Y <= 7; ++Y){

            LBoard[X][Y] = ("  ");
    }
  }

  for (int X = 0; X <= 7; ++X){ 
        LBoard[1][X] = ("WP");
    // Occupies second row with white pawns

  }
    LBoard[0][0] = ("WR");
    LBoard[0][1] = ("WH");
    LBoard[0][2] = ("WB");
    LBoard[0][3] = ("WQ");
    LBoard[0][4] = ("WK");
    LBoard[0][5] = ("WB");
    LBoard[0][6] = ("WH");
    LBoard[0][7] = ("WR");


  for (int X = 0; X <= 7; ++X){
        LBoard[6][X] = ("BP");
  }

    LBoard[7][0] = ("BR");
    LBoard[7][1] = ("BH");
    LBoard[7][2] = ("BB");
    LBoard[7][3] = ("BQ");
    LBoard[7][4] = ("BK");
    LBoard[7][5] = ("BB");
    LBoard[7][6] = ("BH");
    LBoard[7][7] = ("BR");
}

void DisplayWindow::LabelButton(){

    for (int X = 0; X <= 7; ++X){ 
        FBoard[1][X]->label(LBoard[1][X]);
    // Occupies second row with white pawns

    }
    FBoard[0][0]->label(LBoard[0][0]);
    FBoard[0][1]->label(LBoard[0][1]);
    FBoard[0][2]->label(LBoard[0][2]);
    FBoard[0][3]->label(LBoard[0][3]);
    FBoard[0][4]->label(LBoard[0][4]);
    FBoard[0][5]->label(LBoard[0][5]);
    FBoard[0][6]->label(LBoard[0][6]);
    FBoard[0][7]->label(LBoard[0][7]);


    for (int X = 0; X <= 7; ++X){
        FBoard[6][X]->label(LBoard[6][X]);
    }

    FBoard[7][0] ->label(LBoard[7][0]);
    FBoard[7][1] ->label(LBoard[7][1]);
    FBoard[7][2] ->label(LBoard[7][2]);
    FBoard[7][3] ->label(LBoard[7][3]);
    FBoard[7][4] ->label(LBoard[7][4]);
    FBoard[7][5] ->label(LBoard[7][5]);
    FBoard[7][6] ->label(LBoard[7][6]);
    FBoard[7][7] ->label(LBoard[7][7]);

    for(int i=0 ; i<=7 ; i++)
    {
        for(int j=0 ; j<=7 ; j++)
        {
            int k=i+j;

                    if(k % 2 != 0 ){
                            FBoard[i][j]->color(FL_WHITE);
                    }

                    else if (k % 2 ==0 ){ 
                            FBoard[i][j]->color(FL_YELLOW); 
                    }
        }
    }
}

void DisplayWindow::ChangeButton(Fl_Widget * o, void * v){
DisplayWindow* Win = (DisplayWindow *) v;
Fl_Button * NewBoard = (Fl_Button*) o;

if (Win->flag == false){
    DisplayWindow::LastLabel =  NewBoard->label();
    NewBoard->label("  ");
    Win->flag = true;
}
else{
    NewBoard->label(DisplayWindow::LastLabel);
    Win->flag = false;
    }           
}