The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
multimenu.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2016 by Boldizsár Lipka <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "multimenu.hpp"
16 #include "image.hpp"
17 #include "video.hpp"
18 
19 namespace gui {
20  multimenu::multimenu(CVideo &video, const std::vector<std::string> &items, bool click_selects, int max_height,
21  int max_width, const menu::sorter *sorter_obj, menu::style *menu_style, const bool auto_join) :
22  menu(video, items, click_selects, max_height, max_width, sorter_obj, menu_style, auto_join),
23  active_items_(items.size(), false) {}
24 
25  void multimenu::draw_row(const size_t row_index, const SDL_Rect &rect, menu::ROW_TYPE type) {
27  ? "buttons/checkbox-pressed.png"
28  : "buttons/checkbox.png");
29  blit_surface(img, nullptr, video().getSurface(), &rect);
30  SDL_Rect newrect = {
31  Sint16 (rect.x + img->w + 2),
32  rect.y,
33  Uint16 (rect.w - img->w - 2),
34  rect.h
35  };
36  menu::draw_row(row_index, newrect, type);
37  }
38 
39  void multimenu::handle_event(const SDL_Event &event) {
40  if (event.type == SDL_MOUSEBUTTONDOWN) {
41  int hit_box = hit_checkbox(event.button.x, event.button.y);
42  if (hit_box != -1) {
43  active_items_[hit_box] = !active_items_[hit_box];
44  invalidate_row_pos(hit_box);
45  last_changed_ = hit_box;
46  return;
47  }
48  }
49  menu::handle_event(event);
50  }
51 
52  int multimenu::hit_checkbox(int x, int y) const {
53  int cb_width = image::get_image("buttons/checkbox-pressed.png")->w;
54  return x > inner_location().x + cb_width ? -1 : hit(x, y);
55  }
56 
58  active_items_.erase(active_items_.begin() + index);
59  menu::erase_item(index);
60  last_changed_ = -1;
61  }
62 
63  void multimenu::set_items(const std::vector<std::string> &items, bool strip_spaces, bool keep_viewport) {
64  active_items_.resize(items.size());
65  std::fill(active_items_.begin(), active_items_.end(), false);
66  last_changed_ = -1;
67  menu::set_items(items, strip_spaces, keep_viewport);
68  }
69 
70  void multimenu::set_active(size_t index, bool active) {
71  active_items_[index] = active;
72  invalidate_row_pos(index);
73  }
74 
76  int result = last_changed_;
77  last_changed_ = -1;
78  return result;
79  }
80 }
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
virtual void draw_row(const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: multimenu.cpp:25
int hit(int x, int y) const
Definition: menu.cpp:1158
std::vector< bool > active_items_
Definition: multimenu.hpp:78
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
Definition: video.hpp:58
ROW_TYPE
Definition: menu.hpp:32
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
virtual void erase_item(size_t index)
Definition: menu.cpp:406
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
const std::vector< std::string > items
virtual void handle_event(const SDL_Event &event)
Definition: menu.cpp:654
-file util.hpp
virtual void draw_row(const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:939
virtual void erase_item(size_t index)
Definition: multimenu.cpp:57
void blit_surface(const surface &surf, const SDL_Rect *srcrect, surface &dst, const SDL_Rect *dstrect)
Replacement for sdl_blit.
Definition: utils.cpp:2185
GLuint64EXT * result
Definition: glew.h:10727
SDL_Rect inner_location() const
Definition: scrollarea.cpp:138
virtual void set_items(const std::vector< std::string > &items, bool strip_spaces=true, bool keep_viewport=false)
Set new items to show and redraw/recalculate everything.
Definition: multimenu.cpp:63
virtual void set_items(const std::vector< std::string > &items, bool strip_spaces=true, bool keep_viewport=false)
Set new items to show and redraw/recalculate everything.
Definition: menu.cpp:446
GLuint index
Definition: glew.h:1782
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
void invalidate_row_pos(size_t pos)
Definition: menu.cpp:1331
multimenu(CVideo &video, const std::vector< std::string > &items, bool click_selects=false, int max_height=-1, int max_width=-1, const sorter *sorter_obj=nullptr, style *menu_style=nullptr, const bool auto_join=true)
Definition: multimenu.cpp:20
GLsizeiptr size
Definition: glew.h:1649
int hit_checkbox(int x, int y) const
Determine which checkbox was hit by a mouse click.
Definition: multimenu.cpp:52
void set_active(size_t index, bool active=true)
Definition: multimenu.cpp:70
cl_event event
Definition: glew.h:3070
GLint GLvoid * img
Definition: glew.h:1353
virtual void handle_event(const SDL_Event &event)
Definition: multimenu.cpp:39
int last_changed()
Returns the item last activated/deactivated.
Definition: multimenu.cpp:75