Implemented a context menu for the unit tool.

Features:
Renaming, Deleting, Toogle canrecruit and toogle renamable.
This commit is contained in:
fendrin 2013-05-05 01:55:11 +02:00
parent 0f54c16415
commit 83f5f4799d
8 changed files with 175 additions and 91 deletions

View file

@ -44,7 +44,7 @@
[menu]
id=menu-editor-paste-context
is_context_menu=true
items=editor-cut,editor-copy,editor-paste,editor-tool-select,editor-select-all,editor-select-inverse,editor-select-none,editor-clipboard-rotate-cw,editor-clipboard-rotate-ccw,editor-clipboard-flip-horizontal,editor-clipboard-flip-vertical,editor-selection-name,editor-selection-fill,editor-selection-rotate,editor-selection-flip, editor-selection-generate,editor-selection-randomize
items=renameunit,describeunit,editor-deleteunit,editor-toggle-renameable,editor-toggle-canrecruit,editor-cut,editor-copy,editor-paste,editor-tool-select,editor-select-all,editor-select-inverse,editor-select-none,editor-clipboard-rotate-cw,editor-clipboard-rotate-ccw,editor-clipboard-flip-horizontal,editor-clipboard-flip-vertical,editor-selection-name,editor-selection-fill,editor-selection-rotate,editor-selection-flip, editor-selection-generate,editor-selection-randomize
[/menu]
####### Menu Bar

View file

@ -114,7 +114,7 @@ editor_action* mouse_action_unit::drag_end_left(editor_display& disp, int x, int
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);
@ -131,65 +131,66 @@ editor_action* mouse_action_unit::click_right(editor_display& disp, int x, int y
click_ = true;
return NULL;
}
*/
editor_action* mouse_action_unit::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
{
map_location hex = disp.hex_clicked_on(x, y);
if (previous_move_hex_ == hex)
return NULL;
//editor_action* mouse_action_unit::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
//{
// map_location hex = disp.hex_clicked_on(x, y);
// if (previous_move_hex_ == hex)
// return NULL;
//
// click_ = (start_hex_ == hex);
// previous_move_hex_ = hex;
//
// const unit_map& units = disp.get_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) {
// return new editor_action_unit_facing(start_hex_, new_direction, old_direction_);
// }
// }
// }
//
// return NULL;
//}
click_ = (start_hex_ == hex);
previous_move_hex_ = hex;
//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_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;
//}
const unit_map& units = disp.get_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) {
return new editor_action_unit_facing(start_hex_, new_direction, old_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_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_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;
}
//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_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)

View file

@ -38,6 +38,10 @@ public:
{
}
bool has_context_menu() const {
return true;
}
void move(editor_display& disp, const map_location& hex);
/**
@ -58,27 +62,9 @@ public:
*/
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);
editor_action* click_right(editor_display& /*disp*/, int /*x*/, int /*y*/) {
return NULL;
}
virtual void set_mouse_overlay(editor_display& disp);
void set_unit_mouse_overlay(editor_display& disp, const unit_type& u);

View file

@ -17,6 +17,7 @@
#include "asserts.hpp"
#include "editor/action/action.hpp"
#include "editor/action/action_unit.hpp"
#include "editor_controller.hpp"
#include "editor/palette/terrain_palettes.hpp"
@ -25,12 +26,15 @@
#include "editor_preferences.hpp"
#include "gui/dialogs/rename_unit.hpp"
#include "gui/dialogs/editor_settings.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/window.hpp"
#include "wml_exception.hpp"
#include "dialogs.hpp"
#include "../clipboard.hpp"
#include "../game_preferences.hpp"
#include "../gettext.hpp"
@ -251,6 +255,20 @@ bool editor_controller::can_execute_command(hotkey::HOTKEY_COMMAND command, int
case HOTKEY_HELP:
case HOTKEY_QUIT_GAME:
return true; //general hotkeys we can always do
// unit tool related
case HOTKEY_UNIT_DESCRIPTION:
return toolkit_->is_mouse_action_set(HOTKEY_EDITOR_TOOL_UNIT);
case HOTKEY_DELETE_UNIT:
case HOTKEY_RENAME_UNIT:
case HOTKEY_EDITOR_UNIT_TOGGLE_CANRECRUIT:
case HOTKEY_EDITOR_UNIT_TOGGLE_RENAMEABLE:
{
map_location loc = gui_->mouseover_hex();
const unit_map& units = context_manager_->get_map_context().get_units();
return (toolkit_->is_mouse_action_set(HOTKEY_EDITOR_TOOL_UNIT) &&
units.find(loc) != units.end());
}
case HOTKEY_UNDO:
return context_manager_->get_map_context().can_undo();
case HOTKEY_REDO:
@ -300,12 +318,10 @@ bool editor_controller::can_execute_command(hotkey::HOTKEY_COMMAND command, int
case HOTKEY_EDITOR_TOOL_VILLAGE:
return !context_manager_->get_map_context().get_teams().empty();
//TODO
// case HOTKEY_EDITOR_EXPORT_SELECTION_COORDS:
case HOTKEY_EDITOR_CUT:
case HOTKEY_EDITOR_COPY:
//TODO
// case HOTKEY_EDITOR_EXPORT_SELECTION_COORDS:
case HOTKEY_EDITOR_SELECTION_FILL:
return !context_manager_->get_map().selection().empty()
&& !toolkit_->is_mouse_action_set(HOTKEY_EDITOR_PASTE);
@ -356,6 +372,18 @@ hotkey::ACTION_STATE editor_controller::get_action_state(hotkey::HOTKEY_COMMAND
using namespace hotkey;
switch (command) {
case HOTKEY_EDITOR_UNIT_TOGGLE_CANRECRUIT:
{
unit_map::const_unit_iterator un =
context_manager_->get_map_context().get_units().find(gui_->mouseover_hex());
return un->can_recruit() ? ACTION_ON : ACTION_OFF;
}
case HOTKEY_EDITOR_UNIT_TOGGLE_RENAMEABLE:
{
unit_map::const_unit_iterator un =
context_manager_->get_map_context().get_units().find(gui_->mouseover_hex());
return (!un->unrenamable()) ? ACTION_ON : ACTION_OFF;
}
//TODO remove hardcoded hotkey names
case HOTKEY_EDITOR_AUTO_UPDATE_TRANSITIONS:
return context_manager_->is_active_transitions_hotkey("editor-auto-update-transitions")
@ -375,7 +403,7 @@ hotkey::ACTION_STATE editor_controller::get_action_state(hotkey::HOTKEY_COMMAND
case HOTKEY_EDITOR_BRUSH_NW_SE:
return toolkit_->is_active_brush("brush-nw-se") ? ACTION_ON : ACTION_OFF;
case HOTKEY_EDITOR_BRUSH_SW_NE:
return toolkit_->is_active_brush("brush-se-nw") ? ACTION_ON : ACTION_OFF;
return toolkit_->is_active_brush("brush-sw-ne") ? ACTION_ON : ACTION_OFF;
case HOTKEY_ZOOM_DEFAULT:
return (gui_->get_zoom_factor() == 1.0) ? ACTION_ON : ACTION_OFF;
@ -522,6 +550,29 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
toolkit_->hotkey_set_mouse_action(command);
return true;
case HOTKEY_EDITOR_UNIT_TOGGLE_RENAMEABLE:
{
map_location loc = gui_->mouseover_hex();
const unit_map::unit_iterator un = context_manager_->get_map_context().get_units().find(loc);
bool unrenamable = un->unrenamable();
un->set_unrenamable(!unrenamable);
}
return true;
case HOTKEY_EDITOR_UNIT_TOGGLE_CANRECRUIT:
{
map_location loc = gui_->mouseover_hex();
const unit_map::unit_iterator un = context_manager_->get_map_context().get_units().find(loc);
bool canrecruit = un->can_recruit();
un->set_can_recurit(!canrecruit);
un->set_standing();
}
return true;
case HOTKEY_DELETE_UNIT:
{
map_location loc = gui_->mouseover_hex();
perform_delete(new editor_action_unit_delete(loc));
}
return true;
case HOTKEY_EDITOR_PASTE: //paste is somewhat different as it might be "one action then revert to previous mode"
toolkit_->hotkey_set_mouse_action(command);
return true;
@ -593,6 +644,8 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
context_manager_->perform_refresh(editor_action_shuffle_area(
context_manager_->get_map().selection()));
return true;
// map specific
case HOTKEY_EDITOR_CLOSE_MAP:
context_manager_->close_current_context();
return true;
@ -605,10 +658,6 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
case HOTKEY_EDITOR_MAP_NEW:
context_manager_->new_map_dialog();
return true;
case HOTKEY_EDITOR_SIDE_NEW:
context_manager_->get_map_context().new_side();
gui_->init_flags();
return true;
case HOTKEY_EDITOR_MAP_SAVE:
save_map();
return true;
@ -630,6 +679,13 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
case HOTKEY_EDITOR_MAP_RESIZE:
context_manager_->resize_map_dialog();
return true;
// Side specific ones
case HOTKEY_EDITOR_SIDE_NEW:
context_manager_->get_map_context().new_side();
gui_->init_flags();
return true;
case HOTKEY_EDITOR_AUTO_UPDATE_TRANSITIONS:
if (context_manager_->toggle_update_transitions())
return true;
@ -643,6 +699,7 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
case HOTKEY_EDITOR_REFRESH_IMAGE_CACHE:
refresh_image_cache();
return true;
case HOTKEY_EDITOR_DRAW_COORDINATES:
gui().set_draw_coordinates(!gui().get_draw_coordinates());
preferences::editor::set_draw_hex_coordinates(gui().get_draw_coordinates());
@ -713,6 +770,19 @@ void editor_controller::toggle_grid()
gui_->invalidate_all();
}
void editor_controller::unit_description()
{
map_location loc = gui_->mouseover_hex();
unit_map units = context_manager_->get_map_context().get_units();
const unit_map::const_unit_iterator un = units.find(loc);
if(un != units.end()) {
help::show_unit_help(*gui_, un->type_id(), false);
} else {
help::show_help(*gui_, "..units");
}
}
void editor_controller::copy_selection()
{
if (!context_manager_->get_map().selection().empty()) {
@ -721,6 +791,22 @@ void editor_controller::copy_selection()
}
}
void editor_controller::rename_unit()
{
map_location loc = gui_->mouseover_hex();
unit_map& units = context_manager_->get_map_context().get_units();
const unit_map::unit_iterator& un = units.find(loc);
if(un != units.end()) {
std::string name = un->name();
if(gui2::trename_unit::execute(name, gui_->video())) {
//TODO we may not want a translated name here.
un->set_name(name);
//TODO
//gui_->invalidate_unit();
}
}
}
void editor_controller::cut_selection()
{
copy_selection();

View file

@ -122,6 +122,9 @@ class editor_controller : public controller_base,
/** Grid toggle */
void toggle_grid();
void unit_description();
void rename_unit();
/** Copy the selection on the current map to the clipboard */
void copy_selection();

View file

@ -83,8 +83,10 @@ const hotkey_command hotkey_list_[] = {
{ hotkey::HOTKEY_SCREENSHOT, "screenshot", N_("Screenshot"), false, hotkey::SCOPE_GENERAL, NULL },
{ hotkey::HOTKEY_MAP_SCREENSHOT, "mapscreenshot", N_("Map Screenshot"), false, hotkey::SCOPE_GENERAL, NULL },
{ hotkey::HOTKEY_ACCELERATED, "accelerated", N_("Accelerated"), false, hotkey::SCOPE_GAME, NULL },
{ hotkey::HOTKEY_UNIT_DESCRIPTION, "describeunit", N_("Unit Description"), false, hotkey::SCOPE_GAME, NULL },
{ hotkey::HOTKEY_RENAME_UNIT, "renameunit", N_("Rename Unit"), false, hotkey::SCOPE_GAME, NULL },
{ hotkey::HOTKEY_UNIT_DESCRIPTION, "describeunit", N_("Unit Description"), false, hotkey::SCOPE_GENERAL, NULL },
{ hotkey::HOTKEY_RENAME_UNIT, "renameunit", N_("Rename Unit"), false, hotkey::SCOPE_GENERAL, NULL },
{ hotkey::HOTKEY_DELETE_UNIT, "editor-deleteunit", N_("Delete Unit"), false, hotkey::SCOPE_GENERAL, NULL },
{ hotkey::HOTKEY_SAVE_GAME, "save", N_("Save Game"), false, hotkey::SCOPE_GAME, NULL },
{ hotkey::HOTKEY_SAVE_REPLAY, "savereplay", N_("Save Replay"), false, hotkey::SCOPE_GAME, NULL },
{ hotkey::HOTKEY_SAVE_MAP, "savemap", N_("Save Map"), false, hotkey::SCOPE_GAME, NULL },
@ -171,6 +173,9 @@ const hotkey_command hotkey_list_[] = {
{ hotkey::HOTKEY_EDITOR_TOOL_SOUNDSOURCE, "editor-tool-soundsource", N_("Soundsource Tool"), false, hotkey::SCOPE_EDITOR, N_("Left mouse button sets or drags a sound source, right clears. Not implemented yet.") },
{ hotkey::HOTKEY_EDITOR_TOOL_VILLAGE, "editor-tool-village", N_("Village Tool"), false, hotkey::SCOPE_EDITOR, N_("Left mouse button sets the village ownership to the current side, right clears. Not implemented yet.") },
{ hotkey::HOTKEY_EDITOR_UNIT_TOGGLE_CANRECRUIT, "editor-toggle-canrecruit", N_("Canrecruit"), false, hotkey::SCOPE_EDITOR, N_("Toggle the recruit attribute of a unit.") },
{ hotkey::HOTKEY_EDITOR_UNIT_TOGGLE_RENAMEABLE, "editor-toggle-renameable", N_("Can be renamed"), false, hotkey::SCOPE_EDITOR, N_("Toggle the unit being renameable.") },
{ hotkey::HOTKEY_EDITOR_BRUSH_NEXT, "editor-brush-next", N_("Next Brush"), false, hotkey::SCOPE_EDITOR, NULL },
{ hotkey::HOTKEY_EDITOR_BRUSH_DEFAULT, "editor-brush-default", N_("Default Brush"), false, hotkey::SCOPE_EDITOR, NULL },

View file

@ -53,7 +53,8 @@ enum HOTKEY_COMMAND {
HOTKEY_UNDO, HOTKEY_REDO,
HOTKEY_ZOOM_IN, HOTKEY_ZOOM_OUT, HOTKEY_ZOOM_DEFAULT,
HOTKEY_FULLSCREEN, HOTKEY_SCREENSHOT, HOTKEY_MAP_SCREENSHOT, HOTKEY_ACCELERATED,
HOTKEY_UNIT_DESCRIPTION, HOTKEY_RENAME_UNIT,
HOTKEY_UNIT_DESCRIPTION, HOTKEY_RENAME_UNIT, HOTKEY_DELETE_UNIT,
HOTKEY_EDITOR_UNIT_TOGGLE_CANRECRUIT, HOTKEY_EDITOR_UNIT_TOGGLE_RENAMEABLE,
HOTKEY_SAVE_GAME, HOTKEY_SAVE_REPLAY, HOTKEY_SAVE_MAP, HOTKEY_LOAD_GAME,
HOTKEY_RECRUIT, HOTKEY_REPEAT_RECRUIT, HOTKEY_RECALL, HOTKEY_ENDTURN,
HOTKEY_TOGGLE_ELLIPSES, HOTKEY_TOGGLE_GRID, HOTKEY_STATUS_TABLE, HOTKEY_MUTE, HOTKEY_MOUSE_SCROLL,

View file

@ -157,6 +157,7 @@ public:
SDL_Color xp_color() const;
/** Set to true for some scenario-specific units which should not be renamed */
bool unrenamable() const { return unrenamable_; }
void set_unrenamable(bool unrenamable) { unrenamable_ = unrenamable; }
int side() const { return side_; }
std::string side_id() const;
const std::string& team_color() const { return flag_rgb_; }
@ -165,6 +166,7 @@ public:
fixed_t alpha() const { return alpha_; }
bool can_recruit() const { return canrecruit_; }
void set_can_recurit(bool canrecruit) { canrecruit_ = canrecruit; }
const std::vector<std::string>& recruits() const
{ return recruit_list_; }
void set_recruits(const std::vector<std::string>& recruits);