[[Add search and fix continue]]

* Added a "search" action, which searches labels and unit names
(unit::description()) for the string you type. Case sensitive. Triggered by the
"/" key. Hitting enter at the enter-search-term dialog repeats the
previous search. Searches labels first, then units.

* Fixed a problem with the conditions for displaying the "continue move" action
in the context menu (it was checking things properly in
turn_info::continue_move(), but not in can_execute_command(...)). This caused
"Continue Move" to appear to be a valid option, but clicking it would have no
affect.
This commit is contained in:
John B. Messerly 2004-05-05 03:37:14 +00:00
parent cb036c7cdc
commit 0be7482cba
7 changed files with 71 additions and 4 deletions

View file

@ -142,6 +142,10 @@ language="English"
command=continue
key=t
[/hotkey]
[hotkey]
command=search
key=/
[/hotkey]
game_title="The Battle for Wesnoth"
version="Version"
@ -257,6 +261,11 @@ total_kills="Kills"
place_label="Place Label"
label="Label"
#search for a unit name or lable
search_title="Find Label or Unit"
search_prompt="Search"
search_string_not_found="Couldn't find label or unit containing the string '%s'."
#buttons
main_menu="Menu"
game_menu="Game"
@ -470,6 +479,7 @@ action_bestenemymoves="Best Possible Enemy Moves"
action_delayshroud="Delay Shroud Updates"
action_updateshroud="Update Shroud Now"
action_continue="Continue Move"
action_search="Find Label or Unit"
action_editnewmap="New Map"
action_editloadmap="Load Map"
action_editsavemap="Save Map"

View file

@ -82,6 +82,7 @@ HOTKEY_COMMAND string_to_command(const std::string& str)
m.insert(val("delayshroud",HOTKEY_DELAY_SHROUD));
m.insert(val("updateshroud",HOTKEY_UPDATE_SHROUD));
m.insert(val("continue",HOTKEY_CONTINUE_MOVE));
m.insert(val("search",HOTKEY_SEARCH));
}
const std::map<std::string,HOTKEY_COMMAND>::const_iterator i = m.find(str);
@ -393,13 +394,16 @@ void execute_command(display& disp, HOTKEY_COMMAND command, command_executor* ex
case HOTKEY_CONTINUE_MOVE:
if(executor)
executor->continue_move();
break;
break;
case HOTKEY_SEARCH:
if(executor)
executor->search();
break;
case HOTKEY_QUIT_GAME: {
const int res = gui::show_dialog(disp,NULL,"",string_table["quit_message"],gui::YES_NO);
if(res == 0) {
throw end_level_exception(QUIT);
}
break;
}
@ -457,6 +461,7 @@ void execute_command(display& disp, HOTKEY_COMMAND command, command_executor* ex
executor->edit_revert();
break;
default:
std::cerr << "command_executor: unknown command number " << command << ", ignoring.\n";
break;
}
}

View file

@ -35,6 +35,7 @@ enum HOTKEY_COMMAND { HOTKEY_CYCLE_UNITS, HOTKEY_END_UNIT_TURN, HOTKEY_LEADER,
HOTKEY_OBJECTIVES, HOTKEY_UNIT_LIST, HOTKEY_STATISTICS, HOTKEY_QUIT_GAME,
HOTKEY_LABEL_TERRAIN, HOTKEY_SHOW_ENEMY_MOVES, HOTKEY_BEST_ENEMY_MOVES,
HOTKEY_DELAY_SHROUD, HOTKEY_UPDATE_SHROUD, HOTKEY_CONTINUE_MOVE,
HOTKEY_SEARCH,
//editing specific commands
HOTKEY_EDIT_SET_TERRAIN,
@ -115,6 +116,7 @@ public:
virtual void toggle_shroud_updates() {};
virtual void update_shroud_now() {};
virtual void continue_move() {};
virtual void search() {};
// Map editor stuff.
virtual void edit_set_terrain() {}

View file

@ -37,6 +37,10 @@ void map_labels::read(const config& cfg)
}
}
const std::string& map_labels::get_label(int index) const {
return font::get_floating_label_text(index);
}
const std::string& map_labels::get_label(const gamemap::location& loc) const
{
const label_map::const_iterator itor = labels_.find(loc);

View file

@ -30,6 +30,10 @@ public:
void recalculate_shroud();
typedef std::map<gamemap::location,int> label_map;
const label_map& labels() { return labels_; }
const std::string& get_label(int index) const;
private:
map_labels(const map_labels&);
void operator=(const map_labels&);
@ -38,7 +42,6 @@ private:
const team* team_;
const gamemap& map_;
typedef std::map<gamemap::location,int> label_map;
label_map labels_;
};

View file

@ -896,6 +896,7 @@ bool turn_info::can_execute_command(hotkey::HOTKEY_COMMAND command) const
case hotkey::HOTKEY_UNIT_LIST:
case hotkey::HOTKEY_STATISTICS:
case hotkey::HOTKEY_QUIT_GAME:
case hotkey::HOTKEY_SEARCH:
return true;
case hotkey::HOTKEY_SHOW_ENEMY_MOVES:
@ -912,7 +913,8 @@ bool turn_info::can_execute_command(hotkey::HOTKEY_COMMAND command) const
case hotkey::HOTKEY_CONTINUE_MOVE: {
if(browse_) return false;
if(current_unit()->second.move_interrupted()) return true;
if(current_unit() != units_.end() && current_unit()->second.move_interrupted())
return true;
const unit_map::const_iterator i = units_.find(selected_hex_);
if (i == units_.end()) return false;
return i->second.move_interrupted();
@ -1995,6 +1997,45 @@ void turn_info::show_statistics()
}
}
void turn_info::search() {
std::stringstream prompt;
prompt << string_table["search_prompt"];
if(last_search_.empty() == false) {
prompt << " [" << last_search_ << "]";
}
prompt << ": ";
std::string new_search;
const int res = gui::show_dialog(gui_,NULL,"",string_table["search_title"],gui::OK_CANCEL,
NULL,NULL,prompt.str(),&new_search);
if(new_search.empty() == false)
last_search_ = new_search;
if(res == 0 && last_search_.empty() == false) {
//Search labels
const map_labels::label_map& labels = gui_.labels().labels();
for(map_labels::label_map::const_iterator i = labels.begin(); i != labels.end(); ++i) {
if(gui_.fogged(i->first.x,i->first.y)) continue;
std::string label = gui_.labels().get_label(i->second);
if(std::search(label.begin(), label.end(), last_search_.begin(), last_search_.end()) != label.end()) {
gui_.scroll_to_tile(i->first.x,i->first.y,display::WARP);
return;
}
}
//Search unit names
for(unit_map::const_iterator u = units_.begin(); u != units_.end(); ++u) {
if(gui_.fogged(u->first.x,u->first.y)) continue;
std::string name = u->second.description();
if(std::search(name.begin(), name.end(), last_search_.begin(), last_search_.end()) != name.end()) {
gui_.scroll_to_tile(u->first.x,u->first.y,display::WARP);
return;
}
}
//Not found, inform the player
std::string msg = string_table["search_string_not_found"];
msg.replace(msg.find("%s"), 2, last_search_);
gui::show_dialog(gui_,NULL,"",msg);
}
}
void turn_info::label_terrain()
{
if(map_.on_board(last_hex_) == false) {

View file

@ -124,6 +124,7 @@ private:
virtual void toggle_shroud_updates();
virtual void update_shroud_now();
virtual void continue_move();
virtual void search();
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command) const;
void do_recruit(const std::string& name);
@ -179,6 +180,7 @@ private:
bool end_turn_;
std::string last_recruit_;
std::string last_search_;
};
void play_turn(game_data& gameinfo, game_state& state_of_game,