Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include "std_lib_facilities.h"
  2. #include "Simple_window.h"
  3. #include "Graph.h"
  4.  
  5.  
  6. const int button_size = 30;
  7. const int xoff = 0;
  8. const int yoff = 0;
  9. const int gametiles = 10;
  10.  
  11. Vector_ref<CoolButton> buttons;
  12. Text *t;
  13. Rectangle *r;
  14.  
  15. string stringForInt(int i)
  16. {
  17.   stringstream ss;
  18.   ss << i;
  19.   return ss.str();
  20. }
  21.  
  22. struct CoolButton : Button
  23. {
  24.    int col;
  25.    int row;
  26.    string hidden;
  27.    CoolButton(Point xy, int ww, int hh, const string& s, Callback cb,int r,int c) :Button(xy,ww,hh,s,cb)
  28.    {
  29.        col = c;
  30.        row = r;
  31.    }
  32.  
  33.   void attach(Window& win)//this makes a button save itself in the address of its callback
  34.   {
  35.        pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str());
  36.        pw->callback(reinterpret_cast<Fl_Callback*>(do_it), this);
  37.        own = &win;
  38.   }
  39. };
  40.  
  41. void try_move_to(int x, int y)
  42. {
  43.    r->move(x*button_size - r->point(0).x + button_size/2, y*button_size - r->point(0).y + button_size/2);
  44. }
  45.  
  46. int num_clicks=0;
  47. void cool_callback(Address,Address addr)
  48. {
  49.    num_clicks+=1;
  50.    CoolButton *b = static_cast<CoolButton*>(addr);
  51.    cout << "row " << b->row << endl;
  52.    cout << "col " << b->col << endl;
  53.    try_move_to(b->row, b->col);
  54.    t->set_label(b->hidden+" steps away");
  55.    if(b->hidden == "win")
  56.    {
  57.       for(int i=0; i<buttons.size(); i++)
  58.       {
  59.          buttons[i].hide();
  60.       }
  61.       t->set_label(b->hidden+" in "+ stringForInt(num_clicks) + " steps");
  62.    }
  63.    b->hide();
  64.    Fl::redraw();
  65. }
  66.  
  67.  
  68. int main()
  69. {
  70.     Window win(Point(100,200),button_size*gametiles,button_size*gametiles,"Buttons Demo");
  71.     r = new Rectangle(Point(button_size/2,button_size/2),button_size/2,button_size/2);
  72.     //r.set_color(Color::green);
  73.     win.attach(*r);
  74.     t = new Text(Point(50,50),"Click around");
  75.  
  76.     srand(time(0));//makes random really random
  77.     int goal_x = rand()%gametiles;
  78.     int goal_y = rand()%gametiles;
  79.     for(int x=0;x<gametiles;x++)
  80.     {
  81.         for(int y=0;y<gametiles;y++)
  82.         {
  83.           CoolButton *b = new CoolButton(Point(xoff+button_size*x,yoff+button_size*y), button_size, button_size, "", cool_callback,x,y);
  84.           if(x == goal_x && y == goal_y)
  85.               b->hidden = "win";
  86.           else
  87.               b->hidden = stringForInt(abs(goal_x-x)+abs(goal_y-y));
  88.           win.attach(*b);
  89.           buttons.push_back(*b);
  90.         }
  91.     }
  92.     win.attach(*t);
  93.     t->set_font_size(30);
  94.     return gui_main();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement