存储来自回调 (FLTK) 的 Fl:Button 值
Posted
技术标签:
【中文标题】存储来自回调 (FLTK) 的 Fl:Button 值【英文标题】:Store Fl:Button value from callback (FLTK) 【发布时间】:2015-12-10 20:32:49 【问题描述】:我正在尝试使用 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;
还有头文件:
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
【问题讨论】:
如果回调没有提供 this 作为参数,ChangeButton 怎么知道 v 是 DisplayWin?您是否尝试过运行程序以查看回调是否有效? 在复制和粘贴过程中,我可能弄混了一些东西,我在下面发布了修改后的代码。回调有效,但不是我想要的,它会在单击第一个按钮时立即删除它的标签,而不是在单击第二个按钮时删除。 【参考方案1】: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);
//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;
h:
#ifndef WINDOW_H_
#define WINDOW_H_
#include<vector>
#include<limits>
#include<string>
#include<iostream>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
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_Widget * o, void * );
static bool flag;
bool flag1;
static const char * LastLabel;
private:
Fl_Button * FBoard[8][8];
char * LBoard[8][8];
;
#endif
【讨论】:
以上是关于存储来自回调 (FLTK) 的 Fl:Button 值的主要内容,如果未能解决你的问题,请参考以下文章