The Battle for Wesnoth  1.17.0-dev
palette_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
19 #include "widgets/widget.hpp"
20 
21 #include "tooltips.hpp"
23 
24 namespace editor {
25 
27  , editor_toolkit& toolkit)
28  : gui::widget(gui.video()),
29  gui_(gui),
30  palette_start_(0),
31  toolkit_(toolkit),
32  terrain_palette_(new terrain_palette(gui, cfg, toolkit)),
33  unit_palette_(new unit_palette(gui, cfg, toolkit)),
34  empty_palette_(new empty_palette(gui)),
35  item_palette_(new item_palette(gui, cfg, toolkit))
36  , location_palette_(new location_palette(gui, cfg, toolkit))
37 {
38  unit_palette_->setup(cfg);
39  terrain_palette_->setup(cfg);
40  item_palette_->setup(cfg);
41 }
42 
44 {
45  active_palette().set_group(index);
46  scroll_top();
47 }
48 
50 {
51  restore_palette_bg(false);
52  const SDL_Rect& rect = gui_.palette_area();
53  set_location(rect);
54  palette_start_ = rect.y;
55  bg_register(rect);
57  set_dirty();
58 }
59 
61 {
62  bool scrolled = active_palette().scroll_down();
63 
64  if (scrolled) {
65 
66 // const SDL_Rect& rect = gui_.palette_area();
67 // bg_restore(rect);
68  set_dirty();
69  draw();
70  }
71 }
72 
74 {
75  return active_palette().can_scroll_up();
76 }
77 
79 {
81 }
82 
84 {
85  bool scrolled_up = active_palette().scroll_up();
86  if(scrolled_up) {
87 // const SDL_Rect rect = gui_.palette_area();
88 // bg_restore(rect);
89  set_dirty();
90  draw();
91  }
92 }
93 
95 {
96  restore_palette_bg(true);
97 }
98 
100 {
101  const SDL_Rect rect = gui_.palette_area();
102  if (scroll_top) {
104  }
105  bg_restore(rect);
106  set_dirty();
107 }
108 
110 {
111  return toolkit_.get_palette();
112 }
113 
115 {
116  unsigned int old_start = active_palette().num_items();
117  unsigned int items_start = active_palette().start_num();
118  while (old_start != items_start) {
119  old_start = items_start;
120  scroll_down();
121  }
122 }
123 
125 {
126  //if (!dirty() && !force) {
127  // return;
128  //}
129 
130  const SDL_Rect &loc = location();
131 
133 
134  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
135  if (upscroll_button)
136  upscroll_button->hide(false);
137  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
138  if (downscroll_button)
139  downscroll_button->hide(false);
140  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
141  if (palette_menu_button)
142  palette_menu_button->hide(false);
143 
144 // bg_restore(loc);
145  active_palette().set_dirty(true);
146  active_palette().hide(false);
147  active_palette().draw();
148 
149 // set_dirty(false);
150 }
151 
153 {
154  //handler_vector h;
155 // for (gui::widget& b : active_palette().get_widgets()) {
156 // h.push_back(&b);
157 // }
158  //return h;
159  return active_palette().handler_members();
160 }
161 
162 void palette_manager::handle_event(const SDL_Event& event) {
163 
165 
166  if (event.type == SDL_MOUSEMOTION) {
167  // If the mouse is inside the palette, give it focus.
168  if (sdl::point_in_rect(event.button.x, event.button.y, location())) {
169  if (!focus(&event)) set_focus(true);
170  }
171  // If the mouse is outside, remove focus.
172  else if (focus(&event)) set_focus(false);
173  }
174  if (!focus(&event)) {
175  return;
176  }
177 
178  const SDL_MouseButtonEvent &mouse_button_event = event.button;
179 
180 
181  if (event.type == SDL_MOUSEWHEEL) {
182  if (event.wheel.y > 0) {
183  scroll_up();
184  } else if (event.wheel.y < 0) {
185  scroll_down();
186  }
187 
188  if (event.wheel.x < 0) {
190  scroll_top();
191  } else if (event.wheel.x > 0) {
193  scroll_top();
194  }
195  }
196 
197  if (mouse_button_event.type == SDL_MOUSEBUTTONUP) {
198  //set_dirty(true);
199 // draw(true);
200 // set_dirty(active_palette().mouse_click());
201 // gui_.invalidate_game_status();
202  }
203 }
204 
205 
206 } //Namespace editor
std::shared_ptr< gui::button > find_action_button(const std::string &id)
Retrieves a pointer to a theme UI button.
Definition: display.cpp:832
const std::unique_ptr< unit_palette > unit_palette_
virtual void draw()=0
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:190
virtual void prev_group()=0
common_palette & active_palette()
void set_group(std::size_t index)
virtual bool can_scroll_down()=0
Palette where the terrain to be drawn can be selected.
virtual void handle_event(const SDL_Event &event)
General purpose widgets.
virtual void next_group()=0
void set_focus(bool focus)
Definition: widget.cpp:139
void clear_tooltips()
Definition: tooltips.cpp:121
editor_toolkit & toolkit_
void draw_contents()
Draw the palette.
virtual bool scroll_up()=0
Scroll the editor-palette up one step if possible.
void set_dirty(bool dirty=true)
Definition: widget.cpp:207
virtual bool can_scroll_up()=0
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:330
bool focus(const SDL_Event *event)
Definition: widget.cpp:147
common_palette & get_palette()
Get the current palette.
virtual void hide(bool value=true)
Definition: widget.cpp:152
virtual int num_items()=0
const SDL_Rect & location() const
Definition: widget.cpp:134
virtual void set_group(std::size_t index)=0
const SDL_Rect & palette_area() const
Definition: display.hpp:221
void bg_register(const SDL_Rect &rect)
Definition: widget.cpp:99
sdl_handler_vector handler_members()
virtual std::size_t start_num()=0
Manage the empty-palette in the editor.
Definition: action.cpp:30
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:75
const std::unique_ptr< item_palette > item_palette_
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:23
void scroll_down()
Scroll the editor-palette down one step if possible.
virtual sdl_handler_vector handler_members()
virtual void adjust_size(const SDL_Rect &target)=0
void scroll_up()
Scroll the editor-palette up one step if possible.
Palette where the terrain to be drawn can be selected.
Palette where the terrain to be drawn can be selected.
virtual void draw()
Definition: widget.cpp:269
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:72
void bg_restore() const
Definition: widget.cpp:241
virtual bool scroll_down()=0
Scroll the editor-palette down one step if possible.
const std::unique_ptr< terrain_palette > terrain_palette_
virtual void set_start_item(std::size_t index)=0
void restore_palette_bg(bool scroll_top)
palette_manager(editor_display &gui, const game_config_view &cfg, editor_toolkit &toolkit)
std::shared_ptr< gui::button > find_menu_button(const std::string &id)
Definition: display.cpp:842