The Battle for Wesnoth  1.17.21+dev
palette_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
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 {
46  scroll_top();
47 }
48 
50 {
51  const SDL_Rect& rect = gui_.palette_area();
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  if (!dirty()) {
100  return;
101  }
102 
103  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
104  if (upscroll_button)
105  upscroll_button->hide(false);
106  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
107  if (downscroll_button)
108  downscroll_button->hide(false);
109  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
110  if (palette_menu_button)
111  palette_menu_button->hide(false);
112 
113  active_palette().set_dirty(true);
114  active_palette().hide(false);
115 
116  set_dirty(false);
117 }
118 
120 {
121  // This is unnecessary as every GUI1 widget is a TLD.
122  //active_palette().draw();
123 }
124 
126 {
127  //handler_vector h;
128 // for (gui::widget& b : active_palette().get_widgets()) {
129 // h.push_back(&b);
130 // }
131  //return h;
132  return active_palette().handler_members();
133 }
134 
135 void palette_manager::handle_event(const SDL_Event& event) {
136 
138 
139  if (event.type == SDL_MOUSEMOTION) {
140  // If the mouse is inside the palette, give it focus.
141  if (location().contains(event.button.x, event.button.y)) {
142  if (!focus(&event)) {
143  set_focus(true);
144  }
145  }
146  // If the mouse is outside, remove focus.
147  else if (focus(&event)) {
148  set_focus(false);
149  }
150  }
151  if (!focus(&event)) {
152  return;
153  }
154 
155  const SDL_MouseButtonEvent &mouse_button_event = event.button;
156 
157 
158  if (event.type == SDL_MOUSEWHEEL) {
159  if (event.wheel.y > 0) {
160  scroll_up();
161  } else if (event.wheel.y < 0) {
162  scroll_down();
163  }
164 
165  if (event.wheel.x < 0) {
167  scroll_top();
168  } else if (event.wheel.x > 0) {
170  scroll_top();
171  }
172  }
173 
174  if (mouse_button_event.type == SDL_MOUSEBUTTONUP) {
175  //set_dirty(true);
176 // draw(true);
177 // set_dirty(active_palette().mouse_click());
178 // gui_.invalidate_game_status();
179  }
180 }
181 
182 
183 } //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:806
std::shared_ptr< gui::button > find_menu_button(const std::string &id)
Definition: display.cpp:816
const rect & palette_area() const
Definition: display.cpp:492
virtual void set_start_item(std::size_t index)=0
virtual bool scroll_down()=0
Scroll the editor-palette down one step if possible.
virtual void set_group(std::size_t index)=0
virtual void next_group()=0
virtual bool scroll_up()=0
Scroll the editor-palette up one step if possible.
virtual bool can_scroll_down()=0
virtual bool can_scroll_up()=0
virtual void prev_group()=0
virtual void adjust_size(const SDL_Rect &target)=0
virtual sdl_handler_vector handler_members()
common_palette & get_palette()
Get the current palette.
Palette where the terrain to be drawn can be selected.
List of starting locations and location ids.
void scroll_up()
Scroll the editor-palette up one step if possible.
const std::unique_ptr< item_palette > item_palette_
sdl_handler_vector handler_members() override
void scroll_down()
Scroll the editor-palette down one step if possible.
void draw_contents() override
Draw the palette.
void set_group(std::size_t index)
const std::unique_ptr< terrain_palette > terrain_palette_
palette_manager(editor_display &gui, const game_config_view &cfg, editor_toolkit &toolkit)
const std::unique_ptr< unit_palette > unit_palette_
editor_toolkit & toolkit_
virtual void layout() override
Called by draw_manager to validate layout before drawing.
common_palette & active_palette()
virtual void handle_event(const SDL_Event &event) override
Palette where the terrain to be drawn can be selected.
Palette where the terrain to be drawn can be selected.
A class grating read only view to a vector of config objects, viewed as one config with all children ...
void set_dirty(bool dirty=true)
Definition: widget.cpp:180
bool dirty() const
Definition: widget.cpp:193
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:69
virtual void handle_event(const SDL_Event &) override
Definition: widget.hpp:91
bool focus(const SDL_Event *event)
Definition: widget.cpp:136
const rect & location() const
Definition: widget.cpp:123
virtual void hide(bool value=true)
Definition: widget.cpp:141
void set_focus(bool focus)
Definition: widget.cpp:128
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:190
Manage the empty-palette in the editor.
Definition: action.cpp:31
General purpose widgets.
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
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:84
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47