The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
palette_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
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 #define GETTEXT_DOMAIN "wesnoth-editor"
16 
18 #include "widgets/widget.hpp"
19 
20 #include "tooltips.hpp"
22 
23 namespace editor {
24 
26  , editor_toolkit& toolkit)
27  : gui::widget(gui.video()),
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 {
44  active_palette().set_group(index);
45  scroll_top();
46 }
47 
49 {
50  restore_palette_bg(false);
51  const SDL_Rect& rect = gui_.palette_area();
52  set_location(rect);
53  palette_start_ = rect.y;
54  bg_register(rect);
56  set_dirty();
57 }
58 
60 {
61  bool scrolled = active_palette().scroll_down();
62 
63  if (scrolled) {
64 
65 // const SDL_Rect& rect = gui_.palette_area();
66 // bg_restore(rect);
67  set_dirty();
68  draw();
69  }
70 }
71 
73 {
74  return active_palette().can_scroll_up();
75 }
76 
78 {
80 }
81 
83 {
84  bool scrolled_up = active_palette().scroll_up();
85  if(scrolled_up) {
86 // const SDL_Rect rect = gui_.palette_area();
87 // bg_restore(rect);
88  set_dirty();
89  draw();
90  }
91 }
92 
94 {
95  restore_palette_bg(true);
96 }
97 
99 {
100  const SDL_Rect rect = gui_.palette_area();
101  if (scroll_top) {
103  }
104  bg_restore(rect);
105  set_dirty();
106 }
107 
109 {
110  return toolkit_.get_palette();
111 }
112 
114 {
115  unsigned int old_start = active_palette().num_items();
116  unsigned int items_start = active_palette().start_num();
117  while (old_start != items_start) {
118  old_start = items_start;
119  scroll_down();
120  }
121 }
122 
124 {
125  //if (!dirty() && !force) {
126  // return;
127  //}
128 
129  const SDL_Rect &loc = location();
130 
132 
133  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
134  if (upscroll_button)
135  upscroll_button->hide(false);
136  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
137  if (downscroll_button)
138  downscroll_button->hide(false);
139  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
140  if (palette_menu_button)
141  palette_menu_button->hide(false);
142 
143 // bg_restore(loc);
144  active_palette().set_dirty(true);
145  active_palette().hide(false);
146  active_palette().draw();
147 
148 // set_dirty(false);
149 }
150 
152 {
153  //handler_vector h;
154 // for (gui::widget& b : active_palette().get_widgets()) {
155 // h.push_back(&b);
156 // }
157  //return h;
158  return active_palette().handler_members();
159 }
160 
161 void palette_manager::handle_event(const SDL_Event& event) {
162 
164 
165  if (event.type == SDL_MOUSEMOTION) {
166  // If the mouse is inside the palette, give it focus.
167  if (sdl::point_in_rect(event.button.x, event.button.y, location())) {
168  if (!focus(&event)) set_focus(true);
169  }
170  // If the mouse is outside, remove focus.
171  else if (focus(&event)) set_focus(false);
172  }
173  if (!focus(&event)) {
174  return;
175  }
176 
177  const SDL_MouseButtonEvent &mouse_button_event = event.button;
178 
179 
180  if (event.type == SDL_MOUSEWHEEL) {
181  if (event.wheel.y > 0) {
182  scroll_up();
183  } else if (event.wheel.y < 0) {
184  scroll_down();
185  }
186 
187  if (event.wheel.x < 0) {
189  scroll_top();
190  } else if (event.wheel.x > 0) {
192  scroll_top();
193  }
194  }
195 
196  if (mouse_button_event.type == SDL_MOUSEBUTTONUP) {
197  //set_dirty(true);
198 // draw(true);
199 // set_dirty(active_palette().mouse_click());
200 // gui_.invalidate_game_status();
201  }
202 }
203 
204 
205 } //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:853
const std::unique_ptr< unit_palette > unit_palette_
palette_manager(editor_display &gui, const config &cfg, editor_toolkit &toolkit)
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
void set_group(size_t index)
virtual void draw()=0
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:176
virtual void prev_group()=0
common_palette & active_palette()
virtual bool can_scroll_down()=0
Palette where the terrain to be drawn can be selected.
void bg_register(SDL_Rect const &rect)
Definition: widget.cpp:107
virtual void handle_event(const SDL_Event &event)
General purpose widgets.
virtual void next_group()=0
void set_focus(bool focus)
Definition: widget.cpp:147
void clear_tooltips()
Definition: tooltips.cpp:123
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:215
virtual bool can_scroll_up()=0
bool focus(const SDL_Event *event)
Definition: widget.cpp:155
common_palette & get_palette()
Get the current palette.
virtual void hide(bool value=true)
Definition: widget.cpp:160
virtual int num_items()=0
sdl_handler_vector handler_members()
Manage the empty-palette in the editor.
Definition: action.cpp:29
const std::unique_ptr< item_palette > item_palette_
const SDL_Rect & palette_area() const
Definition: display.hpp:215
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:22
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:277
virtual bool scroll_down()=0
Scroll the editor-palette down one step if possible.
const std::unique_ptr< terrain_palette > terrain_palette_
void bg_restore() const
Definition: widget.cpp:249
virtual void set_start_item(size_t index)=0
virtual void set_group(size_t index)=0
void restore_palette_bg(bool scroll_top)
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:83
virtual void handle_event(SDL_Event const &)
Definition: widget.cpp:338
SDL_Rect const & location() const
Definition: widget.cpp:142
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
std::shared_ptr< gui::button > find_menu_button(const std::string &id)
Definition: display.cpp:863
virtual size_t start_num()=0