aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-04 14:50:30 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-04 14:50:30 +0100
commit59e14944328f237089213410acc90de3b278eb23 (patch)
tree1e9986247f4ac71507c203e6b6e5d79cf53b90dd
parenta182157c7cbae8e9b3275f58418c84a5648bde2f (diff)
gui: Remove teams
-rw-r--r--gui.cpp59
-rw-r--r--world.cpp23
-rw-r--r--world.h4
3 files changed, 73 insertions, 13 deletions
diff --git a/gui.cpp b/gui.cpp
index 4629dbb..353b6fb 100644
--- a/gui.cpp
+++ b/gui.cpp
@@ -1,7 +1,10 @@
#include <iostream>
#include <fstream>
#include <vector>
+#include <unordered_map>
+#include <functional>
#include <cstdlib>
+#include <cassert>
#include <sys/time.h>
#include <FL/Fl.H>
@@ -19,14 +22,14 @@ using namespace std;
struct ColourTeam{
- Team *team;
+ const Team *team;
Fl_Color col;
};
class BotList{
struct Item{
ColourTeam *cteam;
- Fl_Box *box;
+ Fl_Group *grp;
};
Fl_Scroll *listScr;
@@ -36,17 +39,34 @@ 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_Box *box=new Fl_Box(listScr->x(),listScr->y()+20*teams.size(),listScr->w(),20);
- box->color(cteam->col);
- box->label(cteam->team->name.data());
- box->box(FL_FLAT_BOX);
- box->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
- listScr->add(box);
+ 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,box});
+ teams.push_back({cteam,grp});
}
void remove(ColourTeam *cteam){
@@ -54,11 +74,13 @@ public:
for(i=0;i<teams.size();i++){
if(teams[i].cteam==cteam)break;
}
- delete teams[i].box;
+ assert(i<teams.size());
+ delete teams[i].grp;
for(size_t j=i+1;j<teams.size();j++){
- teams[j].box->position(teams[j].box->x(),teams[j].box->y()-20);
+ teams[j].grp->position(teams[j].grp->x(),teams[j].grp->y()-20);
}
teams.erase(teams.begin()+i);
+ listScr->redraw();
}
};
@@ -95,7 +117,7 @@ public:
Simulation(World *world,BotList *botList)
:world(world),botList(botList){}
- void add(Team *team){
+ void add(const Team *team){
(void)world;
teams.push_back({{team,genColour()}});
botList->add(&teams.back().cteam);
@@ -110,6 +132,17 @@ public:
}
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);
+ }
};
@@ -150,7 +183,7 @@ int main(int argc,char **argv){
window->hide();
});*/
- Fl_Group *botlistGroup=new Fl_Group(10,10,150,120);
+ Fl_Group *botlistGroup=new Fl_Group(10,10,150,300);
botlistGroup->box(FL_BORDER_BOX);
BotList *botList=makeBotlist(botlistGroup);
botlistGroup->end();
diff --git a/world.cpp b/world.cpp
index bc08f9d..63ba3c1 100644
--- a/world.cpp
+++ b/world.cpp
@@ -270,6 +270,20 @@ Robot& World::createInFront(const Robot *caller,const Team *team,int iset,int nb
return *r;
}
+void World::removeRobot(const Robot *r){
+ *botptr(r)=nullptr;
+}
+
+void World::removeTeam(const Team *team){
+ for(int y=0;y<SIZE;y++){
+ for(int x=0;x<SIZE;x++){
+ if(board[y][x]&&board[y][x]->team==team){
+ board[y][x]=nullptr;
+ }
+ }
+ }
+}
+
void World::tick(){
for(int y=0;y<SIZE;y++){
for(int x=0;x<SIZE;x++){
@@ -299,6 +313,15 @@ void World::tick(){
}
}
+Robot** World::botptr(const Robot *r){
+ for(int y=0;y<SIZE;y++){
+ for(int x=0;x<SIZE;x++){
+ if(board[y][x]==r)return &board[y][x];
+ }
+ }
+ assert(false);
+}
+
Robot** World::targetbotptr(const Robot *r){
for(int y=0;y<SIZE;y++){
for(int x=0;x<SIZE;x++){
diff --git a/world.h b/world.h
index 9d89c98..fde3907 100644
--- a/world.h
+++ b/world.h
@@ -86,6 +86,7 @@ public:
};
class World{
+ Robot** botptr(const Robot *r);
Robot** targetbotptr(const Robot *r);
public:
@@ -98,6 +99,9 @@ public:
Robot& create(const Team *team,int iset,int nbanks,bool mobile,int x,int y,int heading);
Robot& createInFront(const Robot *caller,const Team *team,int iset,int nbanks,bool mobile);
+ void removeRobot(const Robot *r);
+ void removeTeam(const Team *team);
+
void tick();
Robot* targetbot(const Robot *r);