aboutsummaryrefslogtreecommitdiff
path: root/gui.cpp
blob: 847c52aec7215b418e0cd13828a6a45e5f003dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <functional>
#include <cstdlib>
#include <cassert>
#include <sys/time.h>

#include <FL/Fl.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Window.H>

#include "parse.h"
#include "world.h"

using namespace std;


struct ColourTeam{
	const Team *team;
	Fl_Color col;
};

class BotList{
	struct Item{
		ColourTeam *cteam;
		Fl_Group *grp;
	};

	Fl_Scroll *listScr;
	vector<Item> teams;

public:
	BotList(Fl_Group *parent){
		listScr=new Fl_Scroll(parent->x()+1,parent->y()+25,parent->w()-2,parent->h()-26);
		listScr->end();
		listScr->user_data(this);
	}

	~BotList(){
		delete listScr;
	}

	void add(ColourTeam *cteam){
		Fl_Group *grp=new Fl_Group(listScr->x(),listScr->y()+20*teams.size(),listScr->w(),20);
		grp->color(cteam->col);
		grp->label(cteam->team->name.data());
		grp->labelcolor(fl_contrast(FL_WHITE,cteam->col));
		grp->box(FL_FLAT_BOX);
		grp->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);

		Fl_Button *delBtn=new Fl_Button(grp->x()+grp->w()-20,grp->y()+2,grp->h()-4,grp->h()-4,"X");
		delBtn->box(FL_THIN_UP_BOX);
		delBtn->labelcolor(FL_RED);
		delBtn->labelfont(FL_BOLD);
		delBtn->callback([](Fl_Widget *wid,void *cteam){
			BotList *self=(BotList*)wid->parent()->parent()->user_data();
			self->remove((ColourTeam*)cteam);
		},(void*)cteam);

		grp->end();
		listScr->add(grp);
		listScr->redraw();
		teams.push_back({cteam,grp});
	}

	void remove(ColourTeam *cteam){
		size_t i;
		for(i=0;i<teams.size();i++){
			if(teams[i].cteam==cteam)break;
		}
		assert(i<teams.size());
		delete teams[i].grp;
		for(size_t j=i+1;j<teams.size();j++){
			teams[j].grp->position(teams[j].grp->x(),teams[j].grp->y()-20);
		}
		teams.erase(teams.begin()+i);
		listScr->redraw();
	}
};

class Simulation{
	struct Item{
		ColourTeam cteam;
	};

	vector<Item> teams;
	World *world;
	BotList *botList;

	Fl_Color genColour(){
		static const int nBase=7;
		static const Fl_Color baselist[nBase]={
			FL_RED, FL_GREEN, FL_BLUE, FL_YELLOW, FL_MAGENTA, FL_DARK_RED, FL_DARK_CYAN,
		};
		bool taken[nBase];
		memset(taken,0,nBase*sizeof(bool));
		for(const Item &item : teams){
			int i;
			for(i=0;i<nBase;i++){
				if(baselist[i]==item.cteam.col)break;
			}
			if(i<nBase)taken[i]=true;
		}
		for(int i=0;i<nBase;i++){
			if(!taken[i])return baselist[i];
		}
		return baselist[rand()%nBase];
	}

public:
	Simulation(World *world,BotList *botList)
		:world(world),botList(botList){}

	void add(const Team *team){
		(void)world;
		teams.push_back({{team,genColour()}});
		botList->add(&teams.back().cteam);
	}

	void add(const char *fname){
		cout<<"Adding team from file <"<<fname<<">"<<endl;
		ifstream f(fname);
		if(!f){
			fl_alert("Cannot open file '%s'",fname);
			return;
		}
		add(new Team(assemble(preprocess(f))));
	}

	void remove(const Team *team){
		size_t i;
		for(i=0;i<teams.size();i++){
			if(teams[i].cteam.team==team)break;
		}
		assert(i<teams.size());
		botList->remove(&teams[i].cteam);
		world->removeTeam(team);
		teams.erase(teams.begin()+i);
	}
};


Simulation *simulation;


static void openBotFileChooser(){
	Fl_Native_File_Chooser fc(Fl_Native_File_Chooser::BROWSE_MULTI_FILE);
	fc.title("Add bots to sim");
	fc.filter("Robocom Bot Files\t*.rob");

	switch(fc.show()) {
		case -1:
			cout << "Native File Chooser error: " << fc.errmsg() << endl;
			break;
		case 1:
			break;
		default: {
			int count = fc.count();
			for(int i=0; i<count; i++) {
				simulation->add(fc.filename(i));
			}
			break;
		}
	}
}

static BotList* makeBotlist(Fl_Group *parent){
	Fl_Button *openBtn=new Fl_Button(parent->x()+1,parent->y()+1,70,20,"Add bots");
	openBtn->callback([](Fl_Widget*,void*){
		openBotFileChooser();
	});

	return new BotList(parent);
}

int main(int argc,char **argv){
	struct timeval tv;
	gettimeofday(&tv,nullptr);
	srand(tv.tv_sec*1000000+tv.tv_usec);


	Fl_Window *window=new Fl_Window(960,700,"Mocobor");
	/*window->callback([](Fl_Widget*,void*){
		if(Fl::event()==FL_SHORTCUT&&Fl::event_key()==FL_Escape)return;
		window->hide();
	});*/

	Fl_Group *botlistGroup=new Fl_Group(10,10,150,300);
	botlistGroup->box(FL_BORDER_BOX);
	BotList *botList=makeBotlist(botlistGroup);
	botlistGroup->end();

	window->end();


	World world;
	simulation=new Simulation(&world,botList);

	window->show(argc,argv);
	return Fl::run();
}