libwtk-sdl2  0.0.1
draw_context.hpp
1 #ifndef LIBWTK_SDL2_DRAW_CONTEXT_HPP
2 #define LIBWTK_SDL2_DRAW_CONTEXT_HPP
3 
4 #include <exception>
5 #include <SDL2/SDL_rect.h>
6 #include <SDL2/SDL_surface.h>
7 #include <SDL2/SDL_render.h>
8 
9 #include "font_manager.hpp"
10 #include "copy_command.hpp"
11 
12 // TODO replace rendering with a virtual interface that might as well blit images
14 {
15  color_theme();
16 
17  SDL_Color button_bg_color;
18  SDL_Color button_fg_color;
19  SDL_Color button_frame_color;
20  SDL_Color button_pressed_bg_color;
21  SDL_Color button_selected_bg_color;
22 
23  SDL_Color entry_selected_bg_color;
24  SDL_Color entry_highlight_bg_color;
25 
26  SDL_Color entry_box_bg_color;
27  SDL_Color entry_box_frame_color;
28  SDL_Color entry_box_selected_frame_color;
29 
30  SDL_Color bg_color;
31 
32  // displays the status of something, but not related to direct user interaction
33  SDL_Color active_color;
34 
35  SDL_Color hightlight_color;
36 };
37 
38 /*
39 struct theme
40 {
41  enum class box_type
42  {
43  BUTTON,
44  LIST,
45  TEXT_ENTRY
46  };
47 
48  enum class state
49  {
50  IDLE = 0
51  PRESSED = 1,
52  SELECTED = 2,
53  };
54 
55  static state operator|(state l, state r)
56  {
57  return static_cast<state>(static_cast<int>(l) | static_cast<int>(r));
58  }
59 
60  static bool operator&(state l, state r)
61  {
62  return static_cast<int>(l) & static_cast<int>(r);
63  }
64 
65  virtual int box_border_width(box_type bt, state s) = 0;
66  virtual void draw_box(box_type bt, state s, SDL_Rect box) = 0;
67 };
68 
69 struct default_theme : theme
70 {
71  int box_border_width(box_type bt, state s) override
72  {
73  }
74  void draw_box(box_type bt, state s, SDL_Rect box) override
75  {
76  }
77 
78  struct box_colors
79  {
80  SDL_Color bg;
81  SDL_Color fg;
82  SDL_Color frame;
83  };
84 
85  default_theme()
86  : button_bg_color{25, 25, 25}
87  , button_fg_color{235, 235, 235}
88  , button_frame_color{105, 105, 105}
89  , button_pressed_bg_color{105, 55, 55}
90  , button_selected_bg_color{55, 55, 105}
91  , entry_bg_color{255, 255, 255}
92  , entry_frame_color{100, 100, 100}
93  , entry_selected_bg_color{250, 200, 200}
94  , bg_color{0, 0, 0}
95  , active_color{230, 230, 255}
96  {
97  box_colors base { {25, 25, 25}, {235, 235, 235}, {105, 105, 105} };
98 
99  // 0 - theme::box_type::BUTTON
100  _colors[(int)box_type::BUTTON][(int)state::IDLE] = base;
101  base.bg = {105, 55, 55};
102  _colors[(int)box_type::BUTTON][(int)state::PRESSED] = base;
103  base.frame = {55, 55, 105} // TODO brighter
104  _colors[(int)box_type::BUTTON][(int)state::PRESSED | state::SELECTED] = base;
105  base.bg = {25, 25, 25};
106  _colors[(int)box_type::BUTTON][(int)state::SELECTED] = base;
107 
108  // 1 - theme::box_type::LIST
109  base = {{255, 255, 255}, {0, 0, 0}, {100, 100, 100}};
110  _colors[(int)box_type::LIST][(int)state::IDLE] = base;
111  _colors[(int)box_type::LIST][(int)state::PRESSED] = base;
112  base.frame = {55, 55, 105};
113  _colors[(int)box_type::LIST][(int)state::PRESSED | state::SELECTED] = base;
114  _colors[(int)box_type::LIST][(int)state::SELECTED] = base;
115 
116  // 2 - theme::box_type::TEXT_ENTRY
117  }
118 
119  private:
120 
121  // for each state includes colors for: background, foreground (e.g., font), frame
122  box_colors[2][4] _colors;
123 };
124 */
125 
127 {
128  // draw to a window exclusively
129  draw_context(SDL_Renderer * renderer, font_manager & fm);
130 
131  void present();
132 
133  // button (heightened box)
134  void draw_button_box(SDL_Rect box, bool activated, bool selected);
135  void draw_button_text(std::string const & text, SDL_Rect abs_rect);
136 
137  // entry (lowered box)
138  void draw_entry_box(SDL_Rect box, bool selected);
139  void draw_entry_text(std::string text, SDL_Rect abs_rect, int texture_x_offset = 0, int texture_y_offset = 0);
140  void draw_entry_pressed_background(SDL_Rect box);
141  void draw_entry_active_background(SDL_Rect box);
142  void draw_entry_hightlighted_background(SDL_Rect box);
143  void draw_entry_position_indicator(SDL_Rect box);
144 
145  // Draws a string in the box and returns the actually used height within the
146  // box.
147  int draw_label_text(SDL_Rect box, std::string text, bool wrap, int font_idx = 0);
148 
149  void draw_background(SDL_Rect box);
150 
151  // low-level drawing TODO move or refactor
152  void set_color(SDL_Color c);
153  void set_color_alpha(SDL_Color c);
154  void draw_rect_filled(SDL_Rect r);
155  void draw_rect(SDL_Rect r);
156  void blit(SDL_Surface * s, const SDL_Rect * srcrect, const SDL_Rect * dstrect);
157  void run_copy_commands(std::vector<copy_command> const & commands, point origin, SDL_Color color);
158  void copy_texture(SDL_Texture * t, SDL_Rect src, SDL_Rect dst);
159  void copy_texture(SDL_Texture * t, SDL_Rect dst);
160 
161  private:
162 
163  SDL_Renderer * _renderer;
164 
165  font_manager & _fm;
166 
167  //SDL_Rect _clip_box;
168 
169  color_theme _theme;
170 };
171 
172 #endif
173 
Definition: draw_context.hpp:13
Definition: draw_context.hpp:126
Definition: geometry.hpp:6
Definition: font_manager.hpp:13
Definition: box.hpp:11