A square is now drawn around the currently selected function...

...for the left mouse button.

Fixed a problem where the selected terrain and brush size was not
remembered through operations that affected the whole map, such as
flip and resize.
This commit is contained in:
Kristoffer Erlandsson 2004-05-31 20:41:57 +00:00
parent 0a85b60fe1
commit 6cc26aa22c
5 changed files with 148 additions and 44 deletions

View file

@ -225,7 +225,7 @@ height=600
title=edit_operations_menu
image=lite
items=undo,redo,editcut,editcopy,editselectall,editfillselection,editresize,editflip
rect=120,1,107,22
rect=120,1,225,22
[/menu]
# The toolbar.
@ -233,7 +233,7 @@ height=600
title="Draw"
image=lite_small
items=editdraw
rect=900,250,55,22
rect=900,250,955,271
xanchor=right
yanchor=fixed
[/menu]
@ -242,7 +242,7 @@ height=600
title="Flood"
image=lite_small
items=editfloodfill
rect=960,250,55,22
rect=960,250,1015,271
xanchor=right
yanchor=fixed
[/menu]
@ -251,7 +251,7 @@ height=600
title="Start P"
image=lite_small
items=editsetstartpos
rect=900,270,55,22
rect=900,275,955,296
xanchor=right
yanchor=fixed
[/menu]
@ -260,7 +260,7 @@ height=600
title="Paste"
image=lite_small
items=editpaste
rect=960,270,55,22
rect=960,275,1015,296
xanchor=right
yanchor=fixed
[/menu]

View file

@ -51,7 +51,7 @@ namespace {
const unsigned int sdl_delay = 20;
const std::string prefs_filename = get_dir(get_user_data_dir() + "/editor")
+ "/preferences";
// Return the side that has it's starting position at hex, or -1 if
// none has.
int starting_side_at(const gamemap& map, const gamemap::location hex) {
@ -80,12 +80,16 @@ int map_editor::num_operations_since_save_ = 0;
config map_editor::prefs_;
config map_editor::hotkeys_;
map_editor::LEFT_BUTTON_FUNC map_editor::l_button_func_ = DRAW;
gamemap::TERRAIN map_editor::old_fg_terrain_;
gamemap::TERRAIN map_editor::old_bg_terrain_;
int map_editor::old_brush_size_;
map_editor::map_editor(display &gui, gamemap &map, config &theme, config &game_config)
: gui_(gui), map_(map), abort_(DONT_ABORT),
theme_(theme), game_config_(game_config), map_dirty_(false),
theme_(theme), game_config_(game_config), map_dirty_(false), l_button_palette_dirty_(true),
palette_(gui, size_specs_, map), brush_(gui, size_specs_),
l_button_held_func_(NONE), prefs_disp_manager_(&gui_), all_hexes_selected_(false) {
l_button_held_func_(NONE), highlighted_locs_cleared_(false), prefs_disp_manager_(&gui_),
all_hexes_selected_(false) {
// Set size specs.
adjust_sizes(gui_, size_specs_);
@ -107,15 +111,17 @@ map_editor::map_editor(display &gui, gamemap &map, config &theme, config &game_c
hotkey::get_hotkeys().clear();
hotkey::add_hotkeys(theme_, true);
hotkey::add_hotkeys(prefs_, true);
left_button_func_changed(DRAW);
first_time_created_ = false;
}
else {
hotkey::get_hotkeys().clear();
hotkey::add_hotkeys(hotkeys_, true);
palette_.select_fg_terrain(old_fg_terrain_);
palette_.select_bg_terrain(old_bg_terrain_);
brush_.select_brush_size(old_brush_size_);
}
recalculate_starting_pos_labels();
reports::set_report_content(reports::EDIT_LEFT_BUTTON_FUNCTION,
translate_string("action_editdraw"));
gui_.invalidate_game_status();
gui_.begin_game();
gui_.invalidate_all();
@ -126,6 +132,9 @@ map_editor::map_editor(display &gui, gamemap &map, config &theme, config &game_c
map_editor::~map_editor() {
// Save the hotkeys so that they are remembered if the editor is recreated.
hotkey::save_hotkeys(hotkeys_);
old_fg_terrain_ = palette_.selected_fg_terrain();
old_bg_terrain_ = palette_.selected_bg_terrain();
old_brush_size_ = brush_.selected_brush_size();
try {
write_file(prefs_filename, prefs_.write());
}
@ -260,8 +269,7 @@ void map_editor::left_click(const gamemap::location hex_clicked) {
// If hexes are selected, clear them and do not draw
// anything.
selected_hexes_.clear();
gui_.clear_highlighted_locs();
update_mouse_over_hexes(hex_clicked);
clear_highlighted_hexes_in_gui();
}
else if (l_button_func_ == DRAW) {
l_button_held_func_ = DRAW_TERRAIN;
@ -329,8 +337,7 @@ void map_editor::perform_set_starting_pos() {
}
void map_editor::edit_set_start_pos() {
l_button_func_ = SET_STARTING_POSITION;
left_button_func_changed("action_editsetstartpos");
left_button_func_changed(SET_STARTING_POSITION);
}
void map_editor::perform_flood_fill() {
@ -348,8 +355,7 @@ void map_editor::perform_flood_fill() {
}
void map_editor::edit_flood_fill() {
l_button_func_ = FLOOD_FILL;
left_button_func_changed("action_editfloodfill");
left_button_func_changed(FLOOD_FILL);
}
void map_editor::edit_save_map() {
@ -424,7 +430,7 @@ void map_editor::perform_paste() {
map_undo_action undo_action;
std::set<gamemap::location> filled;
gamemap::location start_hex = selected_hex_;
gui_.clear_highlighted_locs();
clear_highlighted_hexes_in_gui();
for (std::vector<clipboard_item>::const_iterator it = clipboard_.begin();
it != clipboard_.end(); it++) {
gamemap::location l(clipboard_offset_loc_.x + (*it).x_offset,
@ -452,8 +458,7 @@ void map_editor::perform_paste() {
}
void map_editor::edit_paste() {
l_button_func_ = PASTE;
left_button_func_changed("action_editpaste");
left_button_func_changed(PASTE);
}
void map_editor::edit_revert() {
@ -518,8 +523,7 @@ void map_editor::edit_select_all() {
}
void map_editor::edit_draw() {
l_button_func_ = DRAW;
left_button_func_changed("action_editdraw");
left_button_func_changed(DRAW);
}
std::string map_editor::load_map(const std::string filename) {
@ -706,7 +710,7 @@ void map_editor::redraw_everything() {
void map_editor::highlight_selected_hexes(const bool clear_old) {
if (clear_old) {
gui_.clear_highlighted_locs();
clear_highlighted_hexes_in_gui();
}
for (std::set<gamemap::location>::const_iterator it = selected_hexes_.begin();
it != selected_hexes_.end(); it++) {
@ -714,6 +718,11 @@ void map_editor::highlight_selected_hexes(const bool clear_old) {
}
}
void map_editor::clear_highlighted_hexes_in_gui() {
gui_.clear_highlighted_locs();
highlighted_locs_cleared_ = true;
}
bool map_editor::changed_since_save() const {
return num_operations_since_save_ != 0;
}
@ -848,7 +857,7 @@ void map_editor::perform_selection_move() {
map_undo_action undo_action;
const int x_diff = selected_hex_.x - selection_move_start_.x;
const int y_diff = selected_hex_.y - selection_move_start_.y;
gui_.clear_highlighted_locs();
clear_highlighted_hexes_in_gui();
std::set<gamemap::location> new_selection;
// Transfer the terrain to the new position.
std::set<gamemap::location>::const_iterator it;
@ -1110,13 +1119,71 @@ void map_editor::update_mouse_over_hexes(const gamemap::location mouse_over_hex)
selected_hex_ = mouse_over_hex;
}
void map_editor::left_button_func_changed(const std::string &new_function) {
reports::set_report_content(reports::EDIT_LEFT_BUTTON_FUNCTION,
translate_string(new_function));
//palette_.set_dirty();
//brush_.set_dirty();
gui_.invalidate_game_status();
void map_editor::left_button_func_changed(const LEFT_BUTTON_FUNC func) {
if (func != l_button_func_) {
l_button_func_ = func;
const std::string string_to_translate = std::string("action_") + get_action_name(func);
reports::set_report_content(reports::EDIT_LEFT_BUTTON_FUNCTION,
translate_string(string_to_translate));
gui_.invalidate_game_status();
l_button_palette_dirty_ = true;
}
}
void map_editor::update_l_button_palette() {
const theme &t = gui_.get_theme();
const std::vector<theme::menu> &menus = t.menus();
std::vector<theme::menu>::const_iterator it;
for (it = menus.begin(); it != menus.end(); it++) {
if (is_left_button_func_menu(*it)) {
const SDL_Rect &r = (*it).location(gui_.screen_area());
const int draw_x = maximum<int>(r.x - 1, gui_.screen_area().x);
const int draw_y = maximum<int>(r.y - 1, gui_.screen_area().y);
const int draw_w = minimum<int>(r.w + 2, gui_.screen_area().w);
const int draw_h = minimum<int>(r.h + 2, gui_.screen_area().h);
const SDL_Rect draw_rect = {draw_x, draw_y, draw_w, draw_h};
Uint16 color;
if ((*it).items().back() == get_action_name(l_button_func_)) {
color = 0xF000;
}
else {
color = 0x0000;
}
gui::draw_rectangle(draw_rect.x, draw_rect.y, draw_rect.w, draw_rect.h,
color, gui_.video().getSurface());
update_rect(draw_rect);
}
}
}
std::string map_editor::get_action_name(const LEFT_BUTTON_FUNC func) const {
switch (func) {
case DRAW:
return "editdraw";
case SELECT_HEXES:
return ""; // Not implemented yet.
case FLOOD_FILL:
return "editfloodfill";
case SET_STARTING_POSITION:
return "editsetstartpos";
case PASTE:
return "editpaste";
default:
return "";
}
}
bool map_editor::is_left_button_func_menu(const theme::menu &menu) const {
const std::vector<std::string> &menu_items = menu.items();
if (menu_items.size() == 1) {
const std::string item_name = menu_items.back();
for (size_t i = 0; i < NUM_L_BUTTON_FUNC; i++) {
if (get_action_name(LEFT_BUTTON_FUNC(i)) == item_name) {
return true;
}
}
}
return false;
}
void map_editor::main_loop() {
@ -1133,9 +1200,11 @@ void map_editor::main_loop() {
if (cur_hex != selected_hex_) {
mouse_moved_ = true;
}
if (mouse_moved_ || last_brush_size != brush_.selected_brush_size()) {
if (mouse_moved_ || last_brush_size != brush_.selected_brush_size()
|| highlighted_locs_cleared_) {
// The mouse have moved or the brush size has changed,
// adjust the hexes the mouse is over.
highlighted_locs_cleared_ = false;
update_mouse_over_hexes(cur_hex);
last_brush_size = brush_.selected_brush_size();
}
@ -1195,6 +1264,10 @@ void map_editor::main_loop() {
gui_.recalculate_minimap();
}
}
if (l_button_palette_dirty_) {
update_l_button_palette();
l_button_palette_dirty_ = false;
}
gui_.update_display();
SDL_Delay(sdl_delay);
events::pump();

View file

@ -20,6 +20,7 @@
#include "../events.hpp"
#include "../hotkeys.hpp"
#include "../preferences.hpp"
#include "../theme.hpp"
#include <map>
#include <queue>
@ -130,6 +131,14 @@ public:
};
private:
/// What to perform while the left button is held down.
enum LEFT_BUTTON_HELD_FUNC {DRAW_TERRAIN, ADD_SELECTION, REMOVE_SELECTION,
MOVE_SELECTION, NONE};
/// What to perform on a left button click.
enum LEFT_BUTTON_FUNC {DRAW, SELECT_HEXES, FLOOD_FILL,
SET_STARTING_POSITION, PASTE, NUM_L_BUTTON_FUNC};
/// Called in every iteration when the left mouse button is held
/// down. Note that this differs from a click.
void left_button_down(const int mousex, const int mousey);
@ -217,6 +226,11 @@ private:
/// highlighted.
void highlight_selected_hexes(const bool clear_old=true);
/// Clear the highlighted hexes in the gui and set a variable to
/// indicate this so that the brush size highlighting may be
/// refreshed.
void clear_highlighted_hexes_in_gui();
/// Terrain has changed at the specified hex through user drawing
/// (not undo/redo or other special things). If the hex was a
/// starting position, remove this position. Save additional
@ -229,7 +243,6 @@ private:
void terrain_changed(const std::set<gamemap::location> &hexes,
map_undo_action &undo_action);
/// Save an action so that it may be undone. Add an operation to the
/// number done since save.
void save_undo_action(const map_undo_action &action);
@ -237,7 +250,20 @@ private:
/// Call when the left mouse button function has changed. Updated
/// the report indicating what will be performed. New_function is
/// the hotkey-string describing the action.
void left_button_func_changed(const std::string &new_function);
void left_button_func_changed(const LEFT_BUTTON_FUNC func);
/// Draw black squares around the buttons that are used to select
/// the left button function and draw a read square around the
/// currently selected function.
void update_l_button_palette();
/// Return the hotkey-string representing the left button
/// function. The "action_" is left out.
std::string get_action_name(const LEFT_BUTTON_FUNC func) const;
/// Return true if the menu is a button used for setting the left
/// mouse button function.
bool is_left_button_func_menu(const theme::menu &menu) const;
/// Draw the terrain on the hexes the mouse is over, taking account
/// for brush size.
@ -256,14 +282,6 @@ private:
int starting_side;
};
/// What to perform while the left button is held down.
enum LEFT_BUTTON_HELD_FUNC {DRAW_TERRAIN, ADD_SELECTION, REMOVE_SELECTION,
MOVE_SELECTION, NONE};
/// What to perform on a left button click.
enum LEFT_BUTTON_FUNC {DRAW, SELECT_HEXES, FLOOD_FILL,
SET_STARTING_POSITION, PASTE};
display &gui_;
gamemap &map_;
std::string filename_;
@ -281,6 +299,7 @@ private:
// perform some updates like recalculating labels of starting
// positions.
bool map_dirty_;
bool l_button_palette_dirty_;
terrain_palette palette_;
brush_bar brush_;
std::vector<gamemap::location> starting_positions_;
@ -293,11 +312,14 @@ private:
// mouse_moved_ will be true if the mouse have moved between two
// cycles.
bool mouse_moved_;
bool highlighted_locs_cleared_;
const preferences::display_manager prefs_disp_manager_;
static config prefs_;
static config hotkeys_;
static bool first_time_created_;
static LEFT_BUTTON_FUNC l_button_func_;
static gamemap::TERRAIN old_fg_terrain_, old_bg_terrain_;
static int old_brush_size_;
bool all_hexes_selected_;
};

View file

@ -22,6 +22,8 @@
#include "../language.hpp"
#include "../util.hpp"
#include <cassert>
namespace map_editor {
@ -121,6 +123,7 @@ gamemap::TERRAIN terrain_palette::selected_bg_terrain() const {
void terrain_palette::select_fg_terrain(gamemap::TERRAIN terrain) {
if (selected_fg_terrain_ != terrain) {
set_dirty();
update_report();
selected_fg_terrain_ = terrain;
}
}
@ -128,6 +131,7 @@ void terrain_palette::select_fg_terrain(gamemap::TERRAIN terrain) {
void terrain_palette::select_bg_terrain(gamemap::TERRAIN terrain) {
if (selected_bg_terrain_ != terrain) {
set_dirty();
update_report();
selected_bg_terrain_ = terrain;
}
}
@ -156,7 +160,6 @@ void terrain_palette::left_mouse_click(const int mousex, const int mousey) {
int tselect = tile_selected(mousex, mousey);
if(tselect >= 0) {
select_fg_terrain(terrains_[tstart_+tselect]);
update_report();
gui_.invalidate_game_status();
}
}
@ -165,7 +168,6 @@ void terrain_palette::right_mouse_click(const int mousex, const int mousey) {
int tselect = tile_selected(mousex, mousey);
if(tselect >= 0) {
select_bg_terrain(terrains_[tstart_+tselect]);
update_report();
gui_.invalidate_game_status();
}
}
@ -316,6 +318,11 @@ void brush_bar::adjust_size() {
unsigned int brush_bar::selected_brush_size() {
return selected_ + 1;
}
void brush_bar::select_brush_size(int new_size) {
assert(new_size > 0 && new_size <= total_brush_);
selected_ = new_size - 1;
}
void brush_bar::left_mouse_click(const int mousex, const int mousey) {
int index = selected_index(mousex, mousey);
@ -327,7 +334,6 @@ void brush_bar::left_mouse_click(const int mousex, const int mousey) {
}
}
void brush_bar::handle_event(const SDL_Event& event) {
if (event.type == SDL_MOUSEMOTION) {
// If the mouse is inside the palette, give it focus.

View file

@ -105,6 +105,9 @@ public:
/// Return the size of currently selected brush.
unsigned int selected_brush_size();
/// Select a brush size.
void select_brush_size(int new_size);
// Draw the palette. If force is true everything will be redrawn
// even though it is not dirty.
void draw(bool force=false);