The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 
17 #include "palette_manager.hpp"
18 #include "widgets/widget.hpp"
19 
20 #include "tooltips.hpp"
22 
23 namespace editor {
24 
26  , mouse_action** active_mouse_action)
27  : gui::widget(gui.video()),
28  gui_(gui),
29  palette_start_(0),
30  mouse_action_(active_mouse_action),
31  terrain_palette_(new terrain_palette(gui, cfg, active_mouse_action)),
32  unit_palette_(new unit_palette(gui, cfg, active_mouse_action)),
33  empty_palette_(new empty_palette(gui)),
34  item_palette_(new item_palette(gui, cfg, active_mouse_action))
35  , location_palette_(new location_palette(gui, cfg, active_mouse_action))
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  resrote_palete_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  resrote_palete_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 (*mouse_action_)->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_action_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().draw();
146  //active_palette().hide(false);
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:826
void set_group(size_t index)
virtual void draw()=0
boost::scoped_ptr< unit_palette > unit_palette_
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:163
virtual void prev_group()=0
common_palette & active_palette()
virtual bool can_scroll_down()=0
void resrote_palete_bg(bool scroll_top)
Palette where the terrain to be drawn can be selected.
void bg_register(SDL_Rect const &rect)
Definition: widget.cpp:109
virtual void handle_event(const SDL_Event &event)
General purpose widgets.
virtual void next_group()=0
boost::scoped_ptr< item_palette > item_palette_
void set_focus(bool focus)
Definition: widget.cpp:149
void clear_tooltips()
Definition: tooltips.cpp:125
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:217
virtual bool can_scroll_up()=0
palette_manager(editor_display &gui, const config &cfg, mouse_action **active_mouse_action)
bool focus(const SDL_Event *event)
Definition: widget.cpp:157
virtual int num_items()=0
sdl_handler_vector handler_members()
Manage the empty-palette in the editor.
Definition: action.cpp:28
const SDL_Rect & palette_area() const
Definition: display.hpp:224
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:47
Encapsulates the map of the game.
Definition: location.hpp:38
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.
GLuint index
Definition: glew.h:1782
virtual void draw()
Definition: widget.cpp:283
virtual bool scroll_down()=0
Scroll the editor-palette down one step if possible.
void bg_restore() const
Definition: widget.cpp:251
cl_event event
Definition: glew.h:3070
virtual void set_start_item(size_t index)=0
virtual void set_group(size_t index)=0
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
boost::scoped_ptr< terrain_palette > terrain_palette_
virtual void handle_event(SDL_Event const &)
Definition: widget.cpp:345
A mouse action receives events from the controller, and responds to them by creating an appropriate e...
SDL_Rect const & location() const
Definition: widget.cpp:144
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual size_t start_num()=0