Author: ilor
Date: Mon Sep 22 22:28:26 2008
New Revision: 29649
URL: http://svn.gna.org/viewcvs/wesnoth?rev=29649&view=rev
Log:
Editor2:
* Move mouse overlay code into the mouse action classes
* Add mouse up_ event handling in mouse_actions to avoid drag issue with set
starting position dialog
* Store several preferences (WIP)
Modified:
trunk/src/editor2/editor_controller.cpp
trunk/src/editor2/editor_controller.hpp
trunk/src/editor2/editor_map.cpp
trunk/src/editor2/editor_map.hpp
trunk/src/editor2/mouse_action.cpp
trunk/src/editor2/mouse_action.hpp
Modified: trunk/src/editor2/editor_controller.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/editor_controller.cpp?rev=29649&r1=29648&r2=29649&view=diff
==============================================================================
--- trunk/src/editor2/editor_controller.cpp (original)
+++ trunk/src/editor2/editor_controller.cpp Mon Sep 22 22:28:26 2008
@@ -40,6 +40,7 @@
#include "../preferences.hpp"
#include "../random.hpp"
#include "../wml_exception.hpp"
+#include "../serialization/string_utils.hpp"
#include "SDL.h"
@@ -48,7 +49,8 @@
#include <boost/bind.hpp>
namespace {
- std::string default_dir = get_dir(get_dir(get_user_data_dir() +
"/editor") + "/maps");
+ const char* prefkey_default_dir = "editor2_default_dir";
+ const char* prefkey_auto_update_transitions =
"editor2_auto_update_transitions";
}
namespace editor2 {
@@ -78,8 +80,12 @@
, foreground_terrain_()
, background_terrain_()
, clipboard_()
- , auto_update_transitions_(true)
-{
+ ,
auto_update_transitions_(utils::string_bool(preferences::get(prefkey_auto_update_transitions),
true))
+ , default_dir_(preferences::get(prefkey_default_dir))
+{
+ if (default_dir_.empty()) {
+ default_dir_ = get_dir(get_dir(get_user_data_dir() + "/editor")
+ "/maps");
+ }
init(video);
rng_ = new rand_rng::rng();
rng_setter_ = new rand_rng::set_random_generator(rng_);
@@ -284,7 +290,7 @@
if (!confirm_discard()) return;
std::string fn = get_map_context().get_filename();
if (fn.empty()) {
- fn = default_dir;
+ fn = default_dir_;
}
int res = dialogs::show_file_chooser_dialog(gui(), fn, _("Choose a Map
to Load"));
if (res == 0) {
@@ -313,7 +319,7 @@
{
std::string input_name = get_map_context().get_filename();
if (input_name.empty()) {
- input_name = default_dir;
+ input_name = default_dir_;
}
const std::string old_input_name = input_name;
@@ -623,7 +629,7 @@
return true;
case HOTKEY_EDITOR_TERRAIN_PALETTE_SWAP:
palette_->swap();
- if (get_mouse_action()->uses_terrains())
set_mouseover_overlay();
+ set_mouseover_overlay();
return true;
case HOTKEY_EDITOR_PARTIAL_UNDO:
if (dynamic_cast<const
editor_action_chain*>(get_map_context().last_undo_action()) != NULL) {
@@ -708,6 +714,7 @@
return true;
case HOTKEY_EDITOR_AUTO_UPDATE_TRANSITIONS:
auto_update_transitions_ = !auto_update_transitions_;
+ preferences::set(prefkey_auto_update_transitions,
lexical_cast<std::string>(auto_update_transitions_));
if (!auto_update_transitions_) {
return true;
} // else intentionally fall through
@@ -829,11 +836,7 @@
std::map<hotkey::HOTKEY_COMMAND, mouse_action*>::iterator i =
mouse_actions_.find(command);
if (i != mouse_actions_.end()) {
mouse_action_ = i->second;
- if (mouse_action_->uses_terrains()) {
- set_mouseover_overlay();
- } else {
- clear_mouseover_overlay();
- }
+ set_mouseover_overlay();
redraw_toolbar();
gui().set_report_content(reports::EDIT_LEFT_BUTTON_FUNCTION,
hotkey::get_hotkey(command).get_description());
@@ -879,6 +882,15 @@
{
return mouse_action_;
}
+
+void editor_controller::perform_delete(editor_action* action)
+{
+ if (action) {
+ std::auto_ptr<editor_action> action_auto(action);
+ get_map_context().perform_action(*action);
+ }
+}
+
void editor_controller::perform_refresh_delete(editor_action* action, bool
drag_part /* =false */)
{
@@ -1021,6 +1033,7 @@
refresh_after_action(true);
}
} else {
+ DBG_ED << "move " << hex_clicked << "\n";
get_mouse_action()->move(*gui_, hex_clicked);
}
gui().highlight_hex(hex_clicked);
@@ -1053,13 +1066,15 @@
void editor_controller::left_drag_end(int x, int y, const bool /*browse*/)
{
editor_action* a = get_mouse_action()->drag_end(*gui_, x, y);
- perform_refresh_delete(a);
-}
-
-void editor_controller::left_mouse_up(int /*x*/, int /*y*/, const bool
/*browse*/)
-{
+ perform_delete(a);
+}
+
+void editor_controller::left_mouse_up(int x, int y, const bool /*browse*/)
+{
+ editor_action* a = get_mouse_action()->up_left(*gui_, x, y);
+ perform_delete(a);
refresh_after_action();
- if (get_mouse_action()->uses_terrains()) set_mouseover_overlay();
+ set_mouseover_overlay();
}
bool editor_controller::right_click(int x, int y, const bool browse)
@@ -1079,13 +1094,15 @@
void editor_controller::right_drag_end(int x, int y, const bool /*browse*/)
{
editor_action* a = get_mouse_action()->drag_end(*gui_, x, y);
- perform_refresh_delete(a);
-}
-
-void editor_controller::right_mouse_up(int /*x*/, int /*y*/, const bool
/*browse*/)
-{
+ perform_delete(a);
+}
+
+void editor_controller::right_mouse_up(int x, int y, const bool /*browse*/)
+{
+ editor_action* a = get_mouse_action()->up_right(*gui_, x, y);
+ perform_delete(a);
refresh_after_action();
- if (get_mouse_action()->uses_terrains()) set_mouseover_overlay();
+ set_mouseover_overlay();
}
void editor_controller::process_keyup_event(const SDL_Event& event)
@@ -1094,55 +1111,9 @@
perform_refresh_delete(a);
}
-//todo make this a virtual in mouse_action
void editor_controller::set_mouseover_overlay()
{
- surface image_fg(image::get_image("terrain/" +
get_map().get_terrain_info(
- foreground_terrain_).editor_image() +
- ".png"));
- surface image_bg(image::get_image("terrain/" +
get_map().get_terrain_info(
- background_terrain_).editor_image() +
- ".png"));
-
- if (image_fg == NULL || image_bg == NULL) {
- ERR_ED << "Missing terrain icon\n";
- gui().set_mouseover_hex_overlay(NULL);
- return;
- }
-
- // Create a transparent surface of the right size.
- surface image = create_compatible_surface(image_fg, image_fg->w,
image_fg->h);
- SDL_FillRect(image,NULL,SDL_MapRGBA(image->format,0,0,0, 0));
-
- // For efficiency the size of the tile is cached.
- // We assume all tiles are of the same size.
- // The zoom factor can change, so it's not cached.
- // NOTE: when zooming and not moving the mouse, there are glitches.
- // Since the optimal alpha factor is unknown, it has to be calculated
- // on the fly, and caching the surfaces makes no sense yet.
- static const Uint8 alpha = 196;
- static const int size = image_fg->w;
- static const int half_size = size / 2;
- static const int quarter_size = size / 4;
- static const int offset = 2;
- static const int new_size = half_size - 2;
- const int zoom = static_cast<int>(size * gui().get_zoom_factor());
-
- // Blit left side
- image_fg = scale_surface(image_fg, new_size, new_size);
- SDL_Rect rcDestLeft = { offset, quarter_size, 0, 0 };
- SDL_BlitSurface ( image_fg, NULL, image, &rcDestLeft );
-
- // Blit left side
- image_bg = scale_surface(image_bg, new_size, new_size);
- SDL_Rect rcDestRight = { half_size, quarter_size, 0, 0 };
- SDL_BlitSurface ( image_bg, NULL, image, &rcDestRight );
-
- // Add the alpha factor and scale the image
- image = scale_surface(adjust_surface_alpha(image, alpha), zoom, zoom);
-
- // Set as mouseover
- gui().set_mouseover_hex_overlay(image);
+ get_mouse_action()->set_mouse_overlay(gui());
}
void editor_controller::clear_mouseover_overlay()
Modified: trunk/src/editor2/editor_controller.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/editor_controller.hpp?rev=29649&r1=29648&r2=29649&view=diff
==============================================================================
--- trunk/src/editor2/editor_controller.hpp (original)
+++ trunk/src/editor2/editor_controller.hpp Mon Sep 22 22:28:26 2008
@@ -225,6 +225,12 @@
mouse_action* get_mouse_action();
/**
+ * Perform an action, then delete the action object.
+ * The pointer can be NULL, in which case nothing will happen.
+ */
+ void perform_delete(editor_action* action);
+
+ /**
* Peform an action on the current map_context, then refresh
the display
* and delete the pointer. The pointer can be NULL, in which
case nothing will happen.
*/
@@ -343,11 +349,20 @@
/** Toolbar-requires-redraw flag */
bool toolbar_dirty_;
+ /** Palette's active fg tereain */
t_translation::t_terrain foreground_terrain_;
+
+ /** Palette's active fg tereain */
t_translation::t_terrain background_terrain_;
+
+ /** Clipboard map_fragment -- used for copy-paste. */
map_fragment clipboard_;
+ /** Flag to rebuild terrain on every terrain change */
bool auto_update_transitions_;
+
+ /** Default directory for map load/save as dialogs */
+ std::string default_dir_;
};
} //end namespace editor2
Modified: trunk/src/editor2/editor_map.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/editor_map.cpp?rev=29649&r1=29648&r2=29649&view=diff
==============================================================================
--- trunk/src/editor2/editor_map.cpp (original)
+++ trunk/src/editor2/editor_map.cpp Mon Sep 22 22:28:26 2008
@@ -41,6 +41,14 @@
{
sanity_check();
}
+
+editor_map::editor_map(const config& terrain_cfg, const gamemap& map)
+ : gamemap(map)
+ , selection_()
+{
+ sanity_check();
+}
+
editor_map::~editor_map()
{
Modified: trunk/src/editor2/editor_map.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/editor_map.hpp?rev=29649&r1=29648&r2=29649&view=diff
==============================================================================
--- trunk/src/editor2/editor_map.hpp (original)
+++ trunk/src/editor2/editor_map.hpp Mon Sep 22 22:28:26 2008
@@ -49,6 +49,8 @@
editor_map(const config& terrain_cfg, const std::string& data);
editor_map(const config& terrain_cfg, size_t width, size_t height,
t_translation::t_terrain filler);
+
+ editor_map(const config& terrain_cfg, const gamemap& map);
~editor_map();
Modified: trunk/src/editor2/mouse_action.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/mouse_action.cpp?rev=29649&r1=29648&r2=29649&view=diff
==============================================================================
--- trunk/src/editor2/mouse_action.cpp (original)
+++ trunk/src/editor2/mouse_action.cpp Mon Sep 22 22:28:26 2008
@@ -46,6 +46,18 @@
return res;
}
+editor_action* mouse_action::click_left(
+ editor_display& /*disp*/, int /*x*/, int /*y*/)
+{
+ return NULL;
+}
+
+editor_action* mouse_action::click_right(
+ editor_display& /*disp*/, int /*x*/, int /*y*/)
+{
+ return NULL;
+}
+
editor_action* mouse_action::drag_left(editor_display& /*disp*/,
int /*x*/, int /*y*/, bool& /*partial*/, editor_action*
/*last_undo*/)
{
@@ -59,6 +71,18 @@
}
editor_action* mouse_action::drag_end(
+ editor_display& /*disp*/, int /*x*/, int /*y*/)
+{
+ return NULL;
+}
+
+editor_action* mouse_action::up_right(
+ editor_display& /*disp*/, int /*x*/, int /*y*/)
+{
+ return NULL;
+}
+
+editor_action* mouse_action::up_left(
editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return NULL;
@@ -95,6 +119,11 @@
return a;
}
+void mouse_action::set_mouse_overlay(editor_display& disp)
+{
+ disp.set_mouseover_hex_overlay(NULL);
+}
+
bool mouse_action::has_alt_modifier() const
{
return key_[SDLK_RALT] || key_[SDLK_LALT];
@@ -103,6 +132,55 @@
bool mouse_action::has_shift_modifier() const
{
return key_[SDLK_RSHIFT] || key_[SDLK_LSHIFT];
+}
+
+void mouse_action::set_terrain_mouse_overlay(editor_display& disp,
t_translation::t_terrain fg,
+ t_translation::t_terrain bg)
+{
+ surface image_fg(image::get_image("terrain/" +
disp.get_map().get_terrain_info(
+ fg).editor_image() + ".png"));
+ surface image_bg(image::get_image("terrain/" +
disp.get_map().get_terrain_info(
+ bg).editor_image() + ".png"));
+
+ if (image_fg == NULL || image_bg == NULL) {
+ ERR_ED << "Missing terrain icon\n";
+ disp.set_mouseover_hex_overlay(NULL);
+ return;
+ }
+
+ // Create a transparent surface of the right size.
+ surface image = create_compatible_surface(image_fg, image_fg->w,
image_fg->h);
+ SDL_FillRect(image,NULL,SDL_MapRGBA(image->format,0,0,0, 0));
+
+ // For efficiency the size of the tile is cached.
+ // We assume all tiles are of the same size.
+ // The zoom factor can change, so it's not cached.
+ // NOTE: when zooming and not moving the mouse, there are glitches.
+ // Since the optimal alpha factor is unknown, it has to be calculated
+ // on the fly, and caching the surfaces makes no sense yet.
+ static const Uint8 alpha = 196;
+ static const int size = image_fg->w;
+ static const int half_size = size / 2;
+ static const int quarter_size = size / 4;
+ static const int offset = 2;
+ static const int new_size = half_size - 2;
+ const int zoom = static_cast<int>(size * disp.get_zoom_factor());
+
+ // Blit left side
+ image_fg = scale_surface(image_fg, new_size, new_size);
+ SDL_Rect rcDestLeft = { offset, quarter_size, 0, 0 };
+ SDL_BlitSurface ( image_fg, NULL, image, &rcDestLeft );
+
+ // Blit left side
+ image_bg = scale_surface(image_bg, new_size, new_size);
+ SDL_Rect rcDestRight = { half_size, quarter_size, 0, 0 };
+ SDL_BlitSurface ( image_bg, NULL, image, &rcDestRight );
+
+ // Add the alpha factor and scale the image
+ image = scale_surface(adjust_surface_alpha(image, alpha), zoom, zoom);
+
+ // Set as mouseover
+ disp.set_mouseover_hex_overlay(image);
}
std::set<gamemap::location> brush_drag_mouse_action::affected_hexes(
@@ -181,6 +259,11 @@
return new editor_action_chain(new editor_action_paint_area(hexes,
terrain_right_, has_alt_modifier()));
}
+void mouse_action_paint::set_mouse_overlay(editor_display& disp)
+{
+ set_terrain_mouse_overlay(disp, terrain_left_, terrain_right_);
+}
+
std::set<gamemap::location> mouse_action_select::affected_hexes(
editor_display& disp, const gamemap::location& hex)
@@ -212,6 +295,11 @@
return new editor_action_chain(new editor_action_deselect(hexes));
}
+void mouse_action_select::set_mouse_overlay(editor_display& disp)
+{
+ disp.set_mouseover_hex_overlay(NULL); //TODO
+}
+
std::set<gamemap::location> mouse_action_paste::affected_hexes(
editor_display& /*disp*/, const gamemap::location& hex)
@@ -229,6 +317,11 @@
editor_action* mouse_action_paste::click_right(editor_display& /*disp*/, int
/*x*/, int /*y*/)
{
return NULL;
+}
+
+void mouse_action_paste::set_mouse_overlay(editor_display& disp)
+{
+ disp.set_mouseover_hex_overlay(NULL); //TODO
}
@@ -256,7 +349,13 @@
return a;
}
-editor_action* mouse_action_starting_position::click_left(editor_display&
disp, int x, int y)
+void mouse_action_fill::set_mouse_overlay(editor_display& disp)
+{
+ set_terrain_mouse_overlay(disp, terrain_left_, terrain_right_);
+}
+
+
+editor_action* mouse_action_starting_position::up_left(editor_display& disp,
int x, int y)
{
gamemap::location hex = disp.hex_clicked_on(x, y);
if (!disp.map().on_board(hex)) {
@@ -282,10 +381,16 @@
} else if (res > 0 && res != player_starting_at_hex) {
a = new editor_action_starting_position(hex, res);
}
+ update_brush_highlights(disp, hex);
return a;
}
-editor_action* mouse_action_starting_position::click_right(editor_display&
disp, int x, int y)
+editor_action* mouse_action_starting_position::click_left(editor_display&
/*disp*/, int /*x*/, int /*y*/)
+{
+ return NULL;
+}
+
+editor_action* mouse_action_starting_position::up_right(editor_display& disp,
int x, int y)
{
gamemap::location hex = disp.hex_clicked_on(x, y);
int player_starting_at_hex = disp.map().is_starting_position(hex) + 1;
@@ -296,4 +401,15 @@
}
}
+editor_action* mouse_action_starting_position::click_right(editor_display&
/*disp*/, int /*x*/, int /*y*/)
+{
+ return NULL;
+}
+
+void mouse_action_starting_position::set_mouse_overlay(editor_display& disp)
+{
+ disp.set_mouseover_hex_overlay(NULL); //TODO
+}
+
+
} //end namespace editor2
Modified: trunk/src/editor2/mouse_action.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/mouse_action.hpp?rev=29649&r1=29648&r2=29649&view=diff
==============================================================================
--- trunk/src/editor2/mouse_action.hpp (original)
+++ trunk/src/editor2/mouse_action.hpp Mon Sep 22 22:28:26 2008
@@ -84,6 +84,11 @@
*/
virtual editor_action* drag_end(editor_display& disp, int x, int y);
+
+ virtual editor_action* up_left(editor_display& disp, int x, int y);
+
+ virtual editor_action* up_right(editor_display& disp, int x, int y);
+
/**
* Function called by the controller on a key event for the current
mouse action.
* Defaults to starting position processing.
@@ -101,11 +106,20 @@
*/
const theme::menu* toolbar_button() const { return toolbar_button_; }
- virtual bool uses_terrains() const { return false; }
-
+ /**
+ * Set the mouse overlay for this action. Defaults to an empty overlay.
+ */
+ virtual void set_mouse_overlay(editor_display& disp);
+
protected:
bool has_alt_modifier() const;
bool has_shift_modifier() const;
+
+ /**
+ * Helper function for derived classes that need a active-terrain mouse
overlay
+ */
+ void set_terrain_mouse_overlay(editor_display& disp,
t_translation::t_terrain fg,
+ t_translation::t_terrain bg);
/**
* The hex previously used in move operations
@@ -201,6 +215,9 @@
/**
* Current brush handle. Currently a pointer-to-pointer with full
constness.
+ * The mouse action does not modify the brush, does not modify the
pointer
+ * to the current brush, and we allow setting this pointr only once,
hence
+ * the three "consts".
*/
const brush* const * const brush_;
};
@@ -230,7 +247,8 @@
*/
editor_action* click_perform_right(editor_display& disp, const
std::set<gamemap::location>& hexes);
- bool uses_terrains() const { return true; }
+ void set_mouse_overlay(editor_display& disp);
+
protected:
const t_translation::t_terrain& terrain_left_;
const t_translation::t_terrain& terrain_right_;
@@ -266,6 +284,8 @@
* Right click/drag deselects
*/
editor_action* click_perform_right(editor_display& disp, const
std::set<gamemap::location>& hexes);
+
+ virtual void set_mouse_overlay(editor_display& disp);
};
/**
@@ -293,6 +313,8 @@
* Right click does nothing for now
*/
editor_action* click_right(editor_display& disp, int x, int y);
+
+ virtual void set_mouse_overlay(editor_display& disp);
protected:
/**
@@ -330,8 +352,8 @@
*/
editor_action* click_right(editor_display& disp, int x, int y);
- bool uses_terrains() const { return true; }
-
+ virtual void set_mouse_overlay(editor_display& disp);
+
protected:
const t_translation::t_terrain& terrain_left_;
const t_translation::t_terrain& terrain_right_;
@@ -351,14 +373,20 @@
/**
* Left click displays a player-number-selector dialog and then creates
an action
* or returns NULL if cancel was pressed or there would be no change.
- */
+ * Do this on mouse up to avoid drag issue.
+ */
+ editor_action* up_left(editor_display& disp, int x, int y);
+
editor_action* click_left(editor_display& disp, int x, int y);
-
- /**
- * Right click only erases the starting position if there is one
- */
+ /**
+ * Right click only erases the starting position if there is one.
+ * Do this on mouse up to avoid drag issue,
+ */
+ editor_action* up_right(editor_display& disp, int x, int y);
+
editor_action* click_right(editor_display& disp, int x, int y);
-
+
+ virtual void set_mouse_overlay(editor_display& disp);
};
} //end namespace editor2
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits