The Battle for Wesnoth  1.19.0-dev
palette_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 
22 
23 namespace editor {
24 
26  , editor_toolkit& toolkit)
27  : gui::widget(),
28  gui_(gui),
29  palette_start_(0),
30  toolkit_(toolkit),
31  terrain_palette_(new terrain_palette(gui, cfg, toolkit)),
32  unit_palette_(new unit_palette(gui, cfg, toolkit)),
33  empty_palette_(new empty_palette(gui)),
34  item_palette_(new item_palette(gui, cfg, toolkit))
35  , location_palette_(new location_palette(gui, cfg, toolkit))
36 {
37  unit_palette_->setup(cfg);
38  terrain_palette_->setup(cfg);
39  item_palette_->setup(cfg);
40 }
41 
43 {
45  scroll_top();
46 }
47 
49 {
50  const SDL_Rect& rect = gui_.palette_area();
52  palette_start_ = rect.y;
54  set_dirty();
55 }
56 
58 {
59  bool scrolled = active_palette().scroll_down();
60 
61  if (scrolled) {
62  set_dirty();
63  }
64 }
65 
67 {
68  return active_palette().can_scroll_up();
69 }
70 
72 {
74 }
75 
77 {
78  bool scrolled_up = active_palette().scroll_up();
79 
80  if(scrolled_up) {
81  set_dirty();
82  }
83 }
84 
86 {
88  set_dirty();
89 }
90 
92 {
93  return toolkit_.get_palette();
94 }
95 
97 {
98  if (!dirty()) {
99  return;
100  }
101 
102  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
103  if (upscroll_button)
104  upscroll_button->hide(false);
105  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
106  if (downscroll_button)
107  downscroll_button->hide(false);
108  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
109  if (palette_menu_button)
110  palette_menu_button->hide(false);
111 
112  active_palette().set_dirty(true);
113  active_palette().hide(false);
114 
115  set_dirty(false);
116 }
117 
119 {
120  // This is unnecessary as every GUI1 widget is a TLD.
121  //active_palette().draw();
122 }
123 
125 {
126  //handler_vector h;
127 // for (gui::widget& b : active_palette().get_widgets()) {
128 // h.push_back(&b);
129 // }
130  //return h;
131  return active_palette().handler_members();
132 }
133 
134 void palette_manager::handle_event(const SDL_Event& event) {
135 
137 
138  if (event.type == SDL_MOUSEMOTION) {
139  // If the mouse is inside the palette, give it focus.
140  if (location().contains(event.button.x, event.button.y)) {
141  if (!focus(&event)) {
142  set_focus(true);
143  }
144  }
145  // If the mouse is outside, remove focus.
146  else if (focus(&event)) {
147  set_focus(false);
148  }
149  }
150  if (!focus(&event)) {
151  return;
152  }
153 
154  const SDL_MouseButtonEvent &mouse_button_event = event.button;
155 
156 
157  if (event.type == SDL_MOUSEWHEEL) {
158  if (event.wheel.y > 0) {
159  scroll_up();
160  } else if (event.wheel.y < 0) {
161  scroll_down();
162  }
163 
164  if (event.wheel.x < 0) {
166  scroll_top();
167  } else if (event.wheel.x > 0) {
169  scroll_top();
170  }
171  }
172 
173  if (mouse_button_event.type == SDL_MOUSEBUTTONUP) {
174  //set_dirty(true);
175 // draw(true);
176 // set_dirty(active_palette().mouse_click());
177 // gui_.invalidate_game_status();
178  }
179 }
180 
181 
182 } //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:805
std::shared_ptr< gui::button > find_menu_button(const std::string &id)
Definition: display.cpp:815
const rect & palette_area() const
Definition: display.cpp:487
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:90
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:70
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:83
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47