The Battle for Wesnoth  1.17.12+dev
palette_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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(),
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  const SDL_Rect& rect = gui_.palette_area();
52  set_location(rect);
53  palette_start_ = rect.y;
55  set_dirty();
56 }
57 
59 {
60  bool scrolled = active_palette().scroll_down();
61 
62  if (scrolled) {
63  set_dirty();
64  }
65 }
66 
68 {
69  return active_palette().can_scroll_up();
70 }
71 
73 {
75 }
76 
78 {
79  bool scrolled_up = active_palette().scroll_up();
80 
81  if(scrolled_up) {
82  set_dirty();
83  }
84 }
85 
87 {
89  set_dirty();
90 }
91 
93 {
94  return toolkit_.get_palette();
95 }
96 
98 {
99  unsigned int old_start = active_palette().num_items();
100  unsigned int items_start = active_palette().start_num();
101  while (old_start != items_start) {
102  old_start = items_start;
103  scroll_down();
104  }
105 }
106 
108 {
109  if (!dirty()) {
110  return;
111  }
112 
113  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
114  if (upscroll_button)
115  upscroll_button->hide(false);
116  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
117  if (downscroll_button)
118  downscroll_button->hide(false);
119  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
120  if (palette_menu_button)
121  palette_menu_button->hide(false);
122 
123  active_palette().set_dirty(true);
124  active_palette().hide(false);
125 
126  set_dirty(false);
127 }
128 
130 {
131  // This is unnecessary as every GUI1 widget is a TLD.
132  //active_palette().draw();
133 }
134 
136 {
137  //handler_vector h;
138 // for (gui::widget& b : active_palette().get_widgets()) {
139 // h.push_back(&b);
140 // }
141  //return h;
142  return active_palette().handler_members();
143 }
144 
145 void palette_manager::handle_event(const SDL_Event& event) {
146 
148 
149  if (event.type == SDL_MOUSEMOTION) {
150  // If the mouse is inside the palette, give it focus.
151  if (location().contains(event.button.x, event.button.y)) {
152  if (!focus(&event)) {
153  set_focus(true);
154  }
155  }
156  // If the mouse is outside, remove focus.
157  else if (focus(&event)) {
158  set_focus(false);
159  }
160  }
161  if (!focus(&event)) {
162  return;
163  }
164 
165  const SDL_MouseButtonEvent &mouse_button_event = event.button;
166 
167 
168  if (event.type == SDL_MOUSEWHEEL) {
169  if (event.wheel.y > 0) {
170  scroll_up();
171  } else if (event.wheel.y < 0) {
172  scroll_down();
173  }
174 
175  if (event.wheel.x < 0) {
177  scroll_top();
178  } else if (event.wheel.x > 0) {
180  scroll_top();
181  }
182  }
183 
184  if (mouse_button_event.type == SDL_MOUSEBUTTONUP) {
185  //set_dirty(true);
186 // draw(true);
187 // set_dirty(active_palette().mouse_click());
188 // gui_.invalidate_game_status();
189  }
190 }
191 
192 
193 } //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:825
const std::unique_ptr< unit_palette > unit_palette_
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:187
virtual void prev_group()=0
common_palette & active_palette()
void set_group(std::size_t index)
virtual bool can_scroll_down()=0
virtual std::size_t num_items()=0
Palette where the terrain to be drawn can be selected.
General purpose widgets.
virtual void next_group()=0
void set_focus(bool focus)
Definition: widget.cpp:129
List of starting locations and location ids.
bool dirty() const
Definition: widget.cpp:194
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:84
editor_toolkit & toolkit_
virtual bool scroll_up()=0
Scroll the editor-palette up one step if possible.
void set_dirty(bool dirty=true)
Definition: widget.cpp:181
virtual bool can_scroll_up()=0
bool focus(const SDL_Event *event)
Definition: widget.cpp:137
common_palette & get_palette()
Get the current palette.
virtual void hide(bool value=true)
Definition: widget.cpp:142
virtual void set_group(std::size_t index)=0
virtual std::size_t start_num()=0
const rect & location() const
Definition: widget.cpp:124
Manage the empty-palette in the editor.
Definition: action.cpp:30
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:70
const std::unique_ptr< item_palette > item_palette_
void scroll_down()
Scroll the editor-palette down one step if possible.
virtual sdl_handler_vector handler_members()
virtual void handle_event(const SDL_Event &event) override
virtual void adjust_size(const SDL_Rect &target)=0
virtual void layout() override
Called by draw_manager to validate layout before drawing.
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 handle_event(const SDL_Event &) override
Definition: widget.hpp:93
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
const rect & palette_area() const
Definition: display.cpp:511
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
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
palette_manager(editor_display &gui, const game_config_view &cfg, editor_toolkit &toolkit)
void draw_contents() override
Draw the palette.
std::shared_ptr< gui::button > find_menu_button(const std::string &id)
Definition: display.cpp:835
sdl_handler_vector handler_members() override