Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "std_lib_facilities.h"
- #include "Simple_window.h"
- #include "Graph.h"
- const int button_size = 30;
- const int xoff = 0;
- const int yoff = 0;
- const int gametiles = 10;
- Vector_ref<CoolButton> buttons;
- Text *t;
- Rectangle *r;
- string stringForInt(int i)
- {
- stringstream ss;
- ss << i;
- return ss.str();
- }
- struct CoolButton : Button
- {
- int col;
- int row;
- string hidden;
- CoolButton(Point xy, int ww, int hh, const string& s, Callback cb,int r,int c) :Button(xy,ww,hh,s,cb)
- {
- col = c;
- row = r;
- }
- void attach(Window& win)//this makes a button save itself in the address of its callback
- {
- pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str());
- pw->callback(reinterpret_cast<Fl_Callback*>(do_it), this);
- own = &win;
- }
- };
- void try_move_to(int x, int y)
- {
- r->move(x*button_size - r->point(0).x + button_size/2, y*button_size - r->point(0).y + button_size/2);
- }
- int num_clicks=0;
- void cool_callback(Address,Address addr)
- {
- num_clicks+=1;
- CoolButton *b = static_cast<CoolButton*>(addr);
- cout << "row " << b->row << endl;
- cout << "col " << b->col << endl;
- try_move_to(b->row, b->col);
- t->set_label(b->hidden+" steps away");
- if(b->hidden == "win")
- {
- for(int i=0; i<buttons.size(); i++)
- {
- buttons[i].hide();
- }
- t->set_label(b->hidden+" in "+ stringForInt(num_clicks) + " steps");
- }
- b->hide();
- Fl::redraw();
- }
- int main()
- {
- Window win(Point(100,200),button_size*gametiles,button_size*gametiles,"Buttons Demo");
- r = new Rectangle(Point(button_size/2,button_size/2),button_size/2,button_size/2);
- //r.set_color(Color::green);
- win.attach(*r);
- t = new Text(Point(50,50),"Click around");
- srand(time(0));//makes random really random
- int goal_x = rand()%gametiles;
- int goal_y = rand()%gametiles;
- for(int x=0;x<gametiles;x++)
- {
- for(int y=0;y<gametiles;y++)
- {
- CoolButton *b = new CoolButton(Point(xoff+button_size*x,yoff+button_size*y), button_size, button_size, "", cool_callback,x,y);
- if(x == goal_x && y == goal_y)
- b->hidden = "win";
- else
- b->hidden = stringForInt(abs(goal_x-x)+abs(goal_y-y));
- win.attach(*b);
- buttons.push_back(*b);
- }
- }
- win.attach(*t);
- t->set_font_size(30);
- return gui_main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement