Forgot some new classes to the last commit.
This commit is contained in:
parent
16a54c3052
commit
c630c6480b
4 changed files with 560 additions and 0 deletions
157
src/editor/action/action_unit.cpp
Normal file
157
src/editor/action/action_unit.cpp
Normal file
|
@ -0,0 +1,157 @@
|
|||
/* $Id: action_unit.cpp 49122 2011-04-09 06:39:56Z fendrin $ */
|
||||
/*
|
||||
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.de>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Editor unit action class
|
||||
*/
|
||||
|
||||
//TODO is a textdomain needed?
|
||||
#define GETTEXT_DOMAIN "wesnoth-editor"
|
||||
|
||||
#include "editor/action/action_unit.hpp"
|
||||
|
||||
#include "editor/map/map_context.hpp"
|
||||
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
|
||||
namespace editor {
|
||||
|
||||
editor_action_unit* editor_action_unit::clone() const
|
||||
{
|
||||
return new editor_action_unit(*this);
|
||||
}
|
||||
|
||||
editor_action* editor_action_unit::perform(map_context& mc) const
|
||||
{
|
||||
std::auto_ptr<editor_action> undo(new editor_action_unit_delete(loc_));
|
||||
// boost::shared_ptr<editor_action> undo(new editor_action_unit_delete(loc_));
|
||||
perform_without_undo(mc);
|
||||
return undo.release();
|
||||
}
|
||||
|
||||
void editor_action_unit::perform_without_undo(map_context& /*mc*/) const
|
||||
{
|
||||
/*
|
||||
mc.get_units().add(loc_,u_);
|
||||
mc.get_units().find(loc_)->set_location(loc_);
|
||||
mc.add_changed_location(loc_);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
editor_action_unit_delete* editor_action_unit_delete::clone() const
|
||||
{
|
||||
return new editor_action_unit_delete(*this);
|
||||
}
|
||||
|
||||
editor_action* editor_action_unit_delete::perform(map_context& /*mc*/) const
|
||||
{
|
||||
std::auto_ptr<editor_action> undo;
|
||||
// boost::scoped_ptr<editor_action> undo;
|
||||
|
||||
/*
|
||||
unit_map& units = mc.get_units();
|
||||
unit_map::const_unit_iterator unit_it = units.find(loc_);
|
||||
|
||||
if (unit_it != units.end()) {
|
||||
undo.reset(new editor_action_unit(loc_, *unit_it));
|
||||
perform_without_undo(mc);
|
||||
return undo.release();
|
||||
} else { return NULL; }
|
||||
*/
|
||||
|
||||
return NULL; //can't be reached
|
||||
}
|
||||
|
||||
void editor_action_unit_delete::perform_without_undo(map_context& /*mc*/) const
|
||||
{
|
||||
/*
|
||||
unit_map& units = mc.get_units();
|
||||
if (!units.erase(loc_)) {
|
||||
ERR_ED << "Could not delete unit on " << loc_.x << "/" << loc_.y << "\n";
|
||||
} else {
|
||||
mc.add_changed_location(loc_);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
editor_action_unit_replace* editor_action_unit_replace::clone() const
|
||||
{
|
||||
return new editor_action_unit_replace(*this);
|
||||
}
|
||||
|
||||
editor_action* editor_action_unit_replace::perform(map_context& mc) const
|
||||
{
|
||||
std::auto_ptr<editor_action> undo(new editor_action_unit_replace(new_loc_, loc_));
|
||||
|
||||
perform_without_undo(mc);
|
||||
return undo.release();
|
||||
}
|
||||
|
||||
void editor_action_unit_replace::perform_without_undo(map_context& /*mc*/) const
|
||||
{
|
||||
//unit_map& units = mc.get_map().get_units();
|
||||
//units.move(loc_, new_loc_);
|
||||
//unit::clear_status_caches();
|
||||
|
||||
//unit& u = *units.find(new_loc_);
|
||||
//TODO do we still need set_standing?
|
||||
//u.set_standing();
|
||||
|
||||
//mc.add_changed_location(loc_);
|
||||
//mc.add_changed_location(new_loc_);
|
||||
|
||||
/* @todo
|
||||
if (mc.get_map().is_village(new_loc_)) {
|
||||
(*(resources::teams))[u.side()].get_village(new_loc_);
|
||||
}
|
||||
*/
|
||||
|
||||
//TODO check if that is useful
|
||||
// resources::screen->invalidate_unit_after_move(loc_, new_loc_);
|
||||
// resources::screen->draw();
|
||||
}
|
||||
|
||||
|
||||
editor_action_unit_facing* editor_action_unit_facing::clone() const
|
||||
{
|
||||
return new editor_action_unit_facing(*this);
|
||||
}
|
||||
|
||||
editor_action* editor_action_unit_facing::perform(map_context& mc) const
|
||||
{
|
||||
std::auto_ptr<editor_action> undo(new editor_action_unit_facing(loc_, old_direction_, new_direction_));
|
||||
//boost::scoped_ptr<editor_action> undo(new editor_action_unit_facing(loc_, old_direction_, new_direction_));
|
||||
perform_without_undo(mc);
|
||||
return undo.release();
|
||||
}
|
||||
|
||||
void editor_action_unit_facing::perform_without_undo(map_context& /*mc*/) const
|
||||
{
|
||||
/*
|
||||
unit_map& units = mc.get_units();
|
||||
unit_map::const_unit_iterator unit_it = units.find(loc_);
|
||||
|
||||
if (unit_it != units.end()) {
|
||||
unit_it->set_facing(new_direction_);
|
||||
unit_it->set_standing();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
} //end namespace editor
|
108
src/editor/action/action_unit.hpp
Normal file
108
src/editor/action/action_unit.hpp
Normal file
|
@ -0,0 +1,108 @@
|
|||
/* $Id: action_unit.hpp 49121 2011-04-07 21:57:57Z fendrin $ */
|
||||
/*
|
||||
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.de>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Editor action classes. Some important points:
|
||||
* - This is a polymorphic hierarchy of classes, so actions are usually passed around
|
||||
* as editor_action pointers
|
||||
* - The pointers can, in general, be null. Always check for null before doing anything.
|
||||
* The helper functions perform_ that take a pointer do that.
|
||||
* - The perform() functions can throw when an error occurs. Use smart pointers if you
|
||||
* need to ensure the pointer is deleted.
|
||||
*/
|
||||
|
||||
#ifndef EDITOR_ACTION_UNIT_HPP
|
||||
#define EDITOR_ACTION_UNIT_HPP
|
||||
|
||||
#include "editor/action/action.hpp"
|
||||
|
||||
#include "../../unit_types.hpp"
|
||||
#include "../../unit.hpp"
|
||||
|
||||
|
||||
namespace editor {
|
||||
|
||||
|
||||
/**
|
||||
* place a new unit on the map
|
||||
*/
|
||||
class editor_action_unit : public editor_action_location
|
||||
{
|
||||
public:
|
||||
editor_action_unit(map_location loc,
|
||||
const unit& u)
|
||||
: editor_action_location(loc), u_(u)
|
||||
{
|
||||
}
|
||||
editor_action_unit* clone() const;
|
||||
editor_action* perform(map_context& mc) const;
|
||||
void perform_without_undo(map_context& mc) const;
|
||||
const char* get_name() const { return "unit"; }
|
||||
protected:
|
||||
unit u_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove a unit from the map.
|
||||
*/
|
||||
class editor_action_unit_delete : public editor_action_location
|
||||
{
|
||||
public:
|
||||
editor_action_unit_delete(map_location loc)
|
||||
: editor_action_location(loc)
|
||||
{
|
||||
}
|
||||
editor_action_unit_delete* clone() const;
|
||||
editor_action* perform(map_context& mc) const;
|
||||
void perform_without_undo(map_context& mc) const;
|
||||
const char* get_name() const { return "unit_delete"; }
|
||||
};
|
||||
|
||||
class editor_action_unit_replace : public editor_action_location
|
||||
{
|
||||
public:
|
||||
editor_action_unit_replace(map_location loc, map_location new_loc)
|
||||
: editor_action_location(loc), new_loc_(new_loc)
|
||||
{
|
||||
}
|
||||
editor_action_unit_replace* clone() const;
|
||||
editor_action* perform(map_context& mc) const;
|
||||
void perform_without_undo(map_context& mc) const;
|
||||
const char* get_name() const { return "unit_replace"; }
|
||||
protected:
|
||||
map_location new_loc_;
|
||||
};
|
||||
|
||||
class editor_action_unit_facing : public editor_action_location
|
||||
{
|
||||
public:
|
||||
editor_action_unit_facing(map_location loc, map_location::DIRECTION new_direction, map_location::DIRECTION old_direction)
|
||||
: editor_action_location(loc), new_direction_(new_direction), old_direction_(old_direction)
|
||||
{
|
||||
}
|
||||
editor_action_unit_facing* clone() const;
|
||||
editor_action* perform(map_context& mc) const;
|
||||
void perform_without_undo(map_context& mc) const;
|
||||
const char* get_name() const { return "unit_facing"; }
|
||||
protected:
|
||||
map_location::DIRECTION new_direction_;
|
||||
map_location::DIRECTION old_direction_;
|
||||
};
|
||||
|
||||
|
||||
} //end namespace editor
|
||||
|
||||
#endif
|
205
src/editor/action/mouse/mouse_action_unit.cpp
Normal file
205
src/editor/action/mouse/mouse_action_unit.cpp
Normal file
|
@ -0,0 +1,205 @@
|
|||
/* $Id: mouse_action_unit.cpp 49182 2011-04-12 02:32:34Z fendrin $ */
|
||||
/*
|
||||
|
||||
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.de>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#define GETTEXT_DOMAIN "wesnoth-editor"
|
||||
|
||||
#include "mouse_action_unit.hpp"
|
||||
#include "../action_unit.hpp"
|
||||
|
||||
#include "../../editor_display.hpp"
|
||||
#include "gui/dialogs/unit_create.hpp"
|
||||
|
||||
|
||||
namespace editor {
|
||||
|
||||
editor_action* mouse_action_unit::click_left(editor_display& disp, int x, int y)
|
||||
{
|
||||
start_hex_ = disp.hex_clicked_on(x, y);
|
||||
if (!disp.get_map().on_board(start_hex_)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//const unit_map& units = disp.get_editor_map().get_const_units();
|
||||
|
||||
/*
|
||||
const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
|
||||
if (unit_it != units.end()) {
|
||||
set_mouse_overlay_surface(disp, unit_it->still_image());
|
||||
//TODO set the mouse pointer to a dragging one.
|
||||
}
|
||||
*/
|
||||
click_ = true;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
editor_action* mouse_action_unit::drag_left(editor_display& /*disp*/, int /*x*/, int /*y*/, bool& /*partial*/, editor_action* /*last_undo*/)
|
||||
{
|
||||
click_ = false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
editor_action* mouse_action_unit::up_left(editor_display& disp, int x, int y)
|
||||
{
|
||||
if (!click_) return NULL;
|
||||
map_location hex = disp.hex_clicked_on(x, y);
|
||||
if (!disp.get_map().on_board(hex)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//const unit_map& units = disp.get_editor_map().get_const_units();
|
||||
// const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
|
||||
|
||||
/*
|
||||
if (unit_it != units.end()) {
|
||||
//TODO open the modify unit dialog
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
|
||||
gui2::tunit_create create_dlg;
|
||||
create_dlg.show(disp.video());
|
||||
|
||||
if(create_dlg.no_choice()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const std::string& type_id = create_dlg.choice();
|
||||
const unit_type *new_unit_type = unit_types.find(type_id);
|
||||
if (!new_unit_type) {
|
||||
ERR_ED << "create unit dialog returned inexistent or unusable unit_type id '" << type_id << "'\n";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const unit_type &ut = *new_unit_type;
|
||||
unit_race::GENDER gender = create_dlg.gender();
|
||||
// Do not try to set bad genders, may mess up l10n
|
||||
// FIXME: is this actually necessary?
|
||||
if(ut.genders().end() == std::find(ut.genders().begin(), ut.genders().end(), gender)) {
|
||||
gender = ut.genders().front();
|
||||
}
|
||||
|
||||
//TODO
|
||||
//bool canrecruit = disp.map().get_teams()[disp.playing_side_index() ].no_leader();
|
||||
//if (canrecruit) disp.map().get_teams()[disp.get_playing_team() ].have_leader(true);
|
||||
// FIXME: This may NOT work as intended, as the unit_map to add the unit to cannot be specified.
|
||||
// Blame silene for removing that argument from unit's constructors
|
||||
//unit u(&disp.map().get_const_units(), utp, disp.playing_side(), false, gender, canrecruit);
|
||||
// unit u(utp, disp.playing_side(), false, gender, canrecruit);
|
||||
//unit new_unit(new_unit_type, disp.playing_side(), true, gender);
|
||||
//editor_action* action = new editor_action_unit(hex, new_unit);
|
||||
//return action;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
editor_action* mouse_action_unit::drag_end_left(editor_display& disp, int x, int y)
|
||||
{
|
||||
if (click_) return NULL;
|
||||
|
||||
editor_action* action = NULL;
|
||||
|
||||
map_location hex = disp.hex_clicked_on(x, y);
|
||||
if (!disp.get_map().on_board(hex))
|
||||
return NULL;
|
||||
|
||||
action = new editor_action_unit_replace(start_hex_, hex);
|
||||
|
||||
return action;
|
||||
}
|
||||
|
||||
|
||||
editor_action* mouse_action_unit::click_right(editor_display& disp, int x, int y)
|
||||
{
|
||||
map_location hex = disp.hex_clicked_on(x, y);
|
||||
start_hex_ = hex;
|
||||
|
||||
//const unit_map& units = disp.get_editor_map().get_const_units();
|
||||
//const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
|
||||
/*
|
||||
if (unit_it != units.end()) {
|
||||
old_direction_ = unit_it->facing();
|
||||
}
|
||||
*/
|
||||
click_ = true;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
editor_action* mouse_action_unit::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
|
||||
{
|
||||
click_ = false;
|
||||
|
||||
map_location hex = disp.hex_clicked_on(x, y);
|
||||
if (previous_move_hex_ != hex) {
|
||||
//const unit_map& units = disp.get_editor_map().get_const_units();
|
||||
|
||||
// const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
|
||||
// if (unit_it != units.end()) {
|
||||
// for (map_location::DIRECTION new_direction = map_location::NORTH;
|
||||
// new_direction <= map_location::NORTH_WEST;
|
||||
// new_direction = map_location::DIRECTION(new_direction +1)){
|
||||
// if (unit_it->get_location().get_direction(new_direction, 1) == hex) {
|
||||
// // unit_it->set_facing(new_direction);
|
||||
// // unit_it->set_standing();
|
||||
// new_direction_ = new_direction;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
editor_action* mouse_action_unit::up_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
|
||||
{
|
||||
if (!click_) return NULL;
|
||||
click_ = false;
|
||||
|
||||
//const unit_map& units = disp.get_editor_map().get_const_units();
|
||||
// const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
|
||||
/* if (unit_it != units.end()) {
|
||||
return new editor_action_unit_delete(start_hex_);
|
||||
}
|
||||
*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
editor_action* mouse_action_unit::drag_end_right(editor_display& disp, int x, int y)
|
||||
{
|
||||
if (click_) return NULL;
|
||||
|
||||
map_location hex = disp.hex_clicked_on(x, y);
|
||||
if (!disp.get_map().on_board(hex))
|
||||
return NULL;
|
||||
|
||||
if(new_direction_ != old_direction_) {
|
||||
|
||||
//const unit_map& units = disp.get_editor_map().get_const_units();
|
||||
// const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
|
||||
// if (unit_it != units.end()) {
|
||||
// return new editor_action_unit_facing(start_hex_, new_direction_, old_direction_);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void mouse_action_unit::set_mouse_overlay(editor_display& /*disp*/)
|
||||
{
|
||||
//set_mouse_overlay_image(disp, "editor/tool-overlay-unit.png");
|
||||
}
|
||||
|
||||
|
||||
} //end namespace editor
|
90
src/editor/action/mouse/mouse_action_unit.hpp
Normal file
90
src/editor/action/mouse/mouse_action_unit.hpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
/* $Id: mouse_action_unit.hpp 49122 2011-04-09 06:39:56Z fendrin $ */
|
||||
/*
|
||||
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.de>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef EDITOR_MOUSE_ACTION_UNIT_HPP
|
||||
#define EDITOR_MOUSE_ACTION_UNIT_HPP
|
||||
|
||||
#include "editor/action/mouse/mouse_action.hpp"
|
||||
#include "editor/palette/unit_palette.hpp"
|
||||
|
||||
class CKey;
|
||||
|
||||
namespace editor {
|
||||
|
||||
/**
|
||||
* Unit placement action class
|
||||
*/
|
||||
class mouse_action_unit : public mouse_action
|
||||
{
|
||||
public:
|
||||
mouse_action_unit(const CKey& key, unit_palette* const palette)
|
||||
: mouse_action(key, palette), click_(false), start_hex_()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
editor_action* click_left(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
editor_action* up_left(editor_display& disp, int x, int y);
|
||||
|
||||
editor_action* drag_left(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
|
||||
|
||||
/**
|
||||
* Drag end replaces the unit when clicked left, or adjusts
|
||||
* the facing when clicked right.
|
||||
*/
|
||||
editor_action* drag_end_left(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
editor_action* click_right(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* Right click only erases the unit 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* drag_right(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
|
||||
|
||||
/**
|
||||
* Drag end replaces the unit when clicked left, or adjusts
|
||||
* the facing when clicked right.
|
||||
*/
|
||||
editor_action* drag_end_right(editor_display& disp, int x, int y);
|
||||
|
||||
virtual void set_mouse_overlay(editor_display& disp);
|
||||
|
||||
private:
|
||||
bool click_;
|
||||
|
||||
map_location start_hex_;
|
||||
map_location::DIRECTION old_direction_;
|
||||
map_location::DIRECTION new_direction_;
|
||||
};
|
||||
|
||||
|
||||
} //end namespace editor
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue