📄 main.cc
字号:
/* * TITLE: Main.cc * * PURPOSE: This function is the main entry point for the referee GUI * program. It builds the GUI app, connecting the appropriate signals * and than branches to gtk_main() and allows the event handlers to * operate. * * WRITTEN BY: Brett Browning * *//* LICENSE: ========================================================================= RoboCup F180 Referee Box Source Code Release ------------------------------------------------------------------------- Copyright (C) 2003 RoboCup Federation ------------------------------------------------------------------------- This software is distributed under the GNU General Public License, version 2. If you do not have a copy of this licence, visit www.gnu.org, or write: Free Software Foundation, 59 Temple Place, Suite 330 Boston, MA 02111-1307 USA. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ------------------------------------------------------------------------- */ #include <cstdio>#include <getopt.h>#include <gtk/gtk.h>#include <sys/time.h>#include <cmath>#include <cstring>#include <ctype.h>#include <cstdlib>#include "serial.h"#include "commands.h"#include "gamecontrol.h"#include "gameinfo.h"#include "gui_util.h"// controls//#define DEBUG/********************************* TYPES *****************************//* serial device being used */#define SERIAL_DEVICE "/dev/ttyS0"/* clock period between ticks in milliseconds*/#define CLOCK_PERIOD 100/* button ID's */#define START_BUTTON 0#define STOP_BUTTON 1#define KICKOFF_YELLOW_CBOX 10#define PENALTY_YELLOW_CBOX 11#define KICKOFF_BLUE_CBOX 20#define PENALTY_BLUE_CBOX 21#define RESTART_CBOX 30#define CORNER_CBOX 31#define THROWIN_CBOX 32#define CANCEL_CBOX 40/* button id for goals */#define GOAL_YELLOW 0#define GOAL_BLUE 1#define MAX_PATH 256 #define WIDGETACTIVE(b) ((b) ? GTK_STATE_NORMAL : GTK_STATE_INSENSITIVE)struct RefWidgets { GtkWidget *enable; GtkWidget *halt; GtkWidget *ready; GtkWidget *stop; GtkWidget *start; GtkWidget *cancel; GtkWidget *corner; GtkWidget *throwin; GtkWidget *goal[NUM_TEAMS]; GtkWidget *subgoal[NUM_TEAMS]; GtkWidget *kickoff[NUM_TEAMS]; GtkWidget *penalty[NUM_TEAMS]; GtkWidget *edit_box; RefWidgets() { enable = start = halt = ready = NULL; stop = start = cancel = corner = throwin = NULL; for (int t = 0; t < NUM_TEAMS; t++) { goal[t] = subgoal[t] = kickoff[t] = NULL; penalty[t] = NULL; } } void setWidgets(EnableState es) { gtk_widget_set_sensitive(halt, es.halt); gtk_widget_set_sensitive(ready, es.ready); gtk_widget_set_sensitive(stop, es.stop); gtk_widget_set_sensitive(start, es.start); gtk_widget_set_sensitive(cancel, es.cancel); gtk_widget_set_sensitive(corner, es.corner); gtk_widget_set_sensitive(throwin, es.throwin); for (int t = 0; t < NUM_TEAMS; t++) { gtk_widget_set_sensitive(goal[t], es.goal[t]); gtk_widget_set_sensitive(subgoal[t], es.subgoal[t]); gtk_widget_set_sensitive(kickoff[t], es.kickoff[t]); gtk_widget_set_sensitive(penalty[t], es.penalty[t]); } } };guint timer_id = 0;RefWidgets refwidgets;GameControl gamecontrol;/********************************* PROTOTYPES ************************/ gint clockTick(gpointer data);void destroy(GtkWidget *w, gpointer data);void updateGUI();void createGUI();#define BUTTONNAME(name) name ## Button#define BUTTON(name, func) \void name ## Button(GtkWidget *w, gpointer data) \{ \ gamecontrol.func(); \} BUTTON(ready, setReady)BUTTON(start, setStart)BUTTON(stop, setStop)BUTTON(halt, setHalt)BUTTON(cancel, setCancel)BUTTON(enable, toggleEnable)BUTTON(throwin, setThrowin)BUTTON(corner, setCorner)// team buttons#define TEAMBUTTONNAME(name, team) name ## Button ## team#define TEAMBUTTON(name, func, team) \void name##Button##team (GtkWidget *w, gpointer data) \{ \ gamecontrol.func(team); \} TEAMBUTTON(goal, goalScored, Blue)TEAMBUTTON(subgoal, removeGoal, Blue)TEAMBUTTON(kickoff, setKickoff, Blue)TEAMBUTTON(penalty, setPenalty, Blue)TEAMBUTTON(goal, goalScored, Yellow)TEAMBUTTON(subgoal, removeGoal, Yellow)TEAMBUTTON(kickoff, setKickoff, Yellow)TEAMBUTTON(penalty, setPenalty, Yellow)/********************************* CODE *****************************//* * main - * * This funciton is the main entry point into the referee program. It * initialises the serial module and then creates the main window * via the GTK libraries. Finally it calls gtk_main() to run the GUI * message loop which returns when the program exits. * * RETURN VALUE: Error code for the program, 0 on success */int main(int argc, char *argv[]){ char c; char configfile[MAX_PATH] = "referee.conf"; char serial_device[MAX_PATH] = "/dev/ttyS0"; char logfile[MAX_PATH] = "gamelog"; bool restart = false; // let GTK have first crack at command line gtk_init(&argc, &argv); while ((c = getopt(argc, argv, "hC:f:r")) != EOF) { switch (c) { case 'C': strcpy(configfile, optarg); break; case 'f': strcpy(logfile, optarg); break; case 'r': restart = true; break; case 'h': default: fprintf(stderr, "\nMidSize Referee Program v1.02\n"); fprintf(stderr, "(c) RoboCup Federation, 2004\n"); fprintf(stderr, "\nUSAGE\n"); fprintf(stderr, "referee -[hCrf]\n"); fprintf(stderr, "\t-h\t\tthis help message\n"); fprintf(stderr, "\t-C <file>\tUse config file <file>\n"); fprintf(stderr, "\t-f <file>\tlog file (no extension)\n"); fprintf(stderr, "\t-r \t restart from existing game state\n"); exit(0); } } // add the timestamp info to the log filename time_t t = time(NULL); strcat(logfile, "."); strcat(logfile, ctime(&t)); // strip carraige return logfile[strlen(logfile) - 1] = 0; strcat(logfile, ".log"); // test if (!gamecontrol.init(configfile, logfile, restart)) { fprintf(stderr, "ERROR: Cannot intialize game controller\n"); return (1); } // setup the GUI fprintf(stderr, "Initializing GUI...\n"); createGUI(); // update the screen contents updateGUI(); /* create the tick function */ timer_id = gtk_timeout_add(CLOCK_PERIOD, (GtkFunction) clockTick, NULL); // begin teh first half gamecontrol.beginFirstHalf(); /* go to the main message loop */ gtk_main(); gamecontrol.close();}void createGUI(){ GtkWidget *window, *vbox, *table,*w; GtkWidget *hbox, *mainhbox; /* create the window box */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "Mid-size Referee v1.00"); gtk_signal_connect(GTK_OBJECT(window), "destroy", (GtkSignalFunc) gtk_main_quit, NULL); /* create the control panel */ mainhbox = gtk_hbox_new(FALSE, 5); gtk_container_add(GTK_CONTAINER(window), mainhbox); gtk_widget_show(mainhbox); gtk_widget_realize(window); vbox = gtk_vbox_new(FALSE, 10); gtk_box_pack_start(GTK_BOX(mainhbox), vbox, true, true, 0); gtk_widget_show(vbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.enable = NewPackCBox(hbox, "Enable commands", 0, true, (GtkSignalFunc) BUTTONNAME(enable)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(refwidgets.enable), true); gamecontrol.setEnable(true); /* create the status text box */ refwidgets.edit_box = NewTextBox(vbox); /* create the hbox to contain the aspect frame and the game control */ hbox = gtk_hbox_new(FALSE, 5); refwidgets.halt = NewPackButton(hbox, "Halt", 0, true, (GtkSignalFunc) BUTTONNAME(halt)); refwidgets.stop = NewPackButton(hbox, "Stop", 0, true, (GtkSignalFunc) BUTTONNAME(stop)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); NewHSeparator(vbox); /* create the hbox to contain the aspect frame and the game control */ hbox = gtk_hbox_new(FALSE, 5); refwidgets.start = NewPackButton(hbox, "Start", 0, true, (GtkSignalFunc) BUTTONNAME(start)); refwidgets.ready = NewPackButton(hbox, "Ready", 0, true, (GtkSignalFunc) BUTTONNAME(ready)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.cancel = NewPackButton(hbox, "Cancel", 0, true, (GtkSignalFunc) BUTTONNAME(cancel)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); /* add a separator */ // NewHSeparator(vbox); vbox = gtk_vbox_new(FALSE, 10); gtk_box_pack_start(GTK_BOX(mainhbox), vbox, true, true, 0); gtk_widget_show(vbox); /* create the hbox to contain the aspect frame and the game control */ hbox = gtk_hbox_new(FALSE, 5); refwidgets.goal[Blue] = NewPackButton(hbox, "Goal Blue!!!", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(goal, Blue)); refwidgets.goal[Yellow] = NewPackButton(hbox, "Goal Yellow!!!", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(goal, Yellow)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.subgoal[Blue] = NewPackButton(hbox, "subGoal Blue", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(subgoal, Blue)); refwidgets.subgoal[Yellow] = NewPackButton(hbox, "subGoal Yellow", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(subgoal, Yellow)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); NewHSeparator(vbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.kickoff[Blue] = NewPackButton(hbox, "Blue Kickoff", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(kickoff, Blue)); refwidgets.kickoff[Yellow] = NewPackButton(hbox, "Yellow Kickoff", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(kickoff, Yellow)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.penalty[Blue] = NewPackButton(hbox, "Blue Penalty", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(penalty, Blue)); refwidgets.penalty[Yellow] = NewPackButton(hbox, "Yellow Penalty", 0, true, (GtkSignalFunc) TEAMBUTTONNAME(penalty, Yellow)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.corner = NewPackButton(hbox, "Corner", 0, true, (GtkSignalFunc) BUTTONNAME(corner)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); hbox = gtk_hbox_new(FALSE, 5); refwidgets.throwin = NewPackButton(hbox, "Throwin", 0, true, (GtkSignalFunc) BUTTONNAME(throwin)); gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 0); gtk_widget_show(hbox); /* set the size so everything is visible nicely */ gtk_widget_set_usize(window, 1000, 550); /* now show the main window and all its little children */ gtk_widget_show(window);}/* * updateGUI - * * This funciton updates the status box with the latest information */void updateGUI(){ char str[1024]; char s[1024]; GameInfo gi = gamecontrol.getGameInfo(); /* update the display */ sprintf(str, "%s\n", gi.getStateString()); strcpy(s, str); sprintf(str, "%s %2i:%04.1f, %2i:%04.1f\n", gi.getStageString(), DISP_MIN(gi.timeTaken()), DISP_SEC(gi.timeTaken()), DISP_MIN(gi.timeRemaining()), DISP_SEC(gi.timeRemaining())); strcat(s, str); if (gi.data.stage == PENALTY_SHOOTOUT) { sprintf(str, "Score (Y:B) %i - %i, Penalties %i - %i\n", gi.data.goals[Yellow], gi.data.goals[Blue], gi.data.penaltygoals[Yellow], gi.data.penaltygoals[Blue]); } else { sprintf(str, "Score (Y:B) %i - %i\n", gi.data.goals[Yellow], gi.data.goals[Blue]); } strcat(s, str); SetText(refwidgets.edit_box, s);}/* * Destroy - * * Called upon quiting the main window. Function kills of the timer * resource and ends nicely */void destroy(GtkWidget *w, gpointer data){ gtk_timeout_remove(timer_id); gamecontrol.close(); gtk_main_quit();}/* * ClockTick - * * This function is the clock tick that is called periodically. It upsates the system time * and refresshes is the display panel */gint clockTick(gpointer data){ // update the game control gamecontrol.stepTime(); // update teh buttons refwidgets.setWidgets(gamecontrol.getEnableState()); // update the gui updateGUI(); gint x; return x; // prevent warning}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -