Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. struct State
  2. {
  3.     State()
  4.         : counter(0)
  5.     {
  6.     }
  7.  
  8.     State with_counter(int counter) const
  9.     {
  10.         State copy(*this);
  11.         copy.counter = counter;
  12.  
  13.         return copy;
  14.     }
  15.  
  16.     const char *get_button_text() const
  17.     {
  18.         static char string[100];
  19.  
  20.         sprintf(string, "Value: %d", counter);
  21.  
  22.         return string;
  23.     }
  24.  
  25.     int counter;
  26. };
  27.  
  28. struct MyApplication : public Application<MyApplication, State, DefaultStyle>
  29. {
  30.     static State increment_counter(const State &state)
  31.     {
  32.         return state.with_counter(state.counter + 1);
  33.     }
  34.  
  35.     static State decrement_counter(const State &state)
  36.     {
  37.         return state.with_counter(state.counter - 1);
  38.     }
  39.  
  40.     static auto layout(const State &state)
  41.     {
  42.         return Rectangle
  43.         {
  44.             Button
  45.             {
  46.                 position = SDL_Point { 120, 100 - state.counter * 5 },
  47.                 size = SDL_Point { 100, 30 },
  48.                 on_clicked = &decrement_counter,
  49.                 text = state.get_button_text()
  50.             },
  51.  
  52.             Button
  53.             {
  54.                 position = SDL_Point { 10, 100 + state.counter * 5 },
  55.                 size = SDL_Point { 100, 30 },
  56.                 on_clicked = &increment_counter,
  57.                 text = state.get_button_text()
  58.             },
  59.  
  60.             position = SDL_Point { 100, 10 },
  61.             size = SDL_Point { 50, 50 },
  62.             color = SDL_Color { 0xFF, 0x00, 0x00, 0xFF },
  63.         };
  64.     }
  65. };
  66.  
  67. int main(int argc, char **argv)
  68. {
  69.     QApplication application(argc, argv);
  70.  
  71.     MyApplication app;
  72.     app.run();
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement