Refactor the scenario editor's monolithic handle_action function into several smaller chunks

This commit is contained in:
2015-06-06 00:01:45 -04:00
parent 9f8e6f88c2
commit 2b4ca2d0a5
3 changed files with 1056 additions and 1046 deletions

View File

@@ -138,25 +138,10 @@ void update_mouse_spot(location the_point) {
} else mouse_spot = {-1,-1};
}
bool handle_action(location the_point,sf::Event /*event*/) {
using kb = sf::Keyboard;
short i,j, x;
bool are_done = false;
std::string s2;
static bool handle_lb_action(location the_point) {
fs::path file_to_load;
bool need_redraw = false,option_hit = false,ctrl_hit = false;
location spot_hit;
location cur_point,cur_point2;
long right_top;
eScenMode old_mode;
rectangle temp_rect;
if(kb::isKeyPressed(kb::LAlt) || kb::isKeyPressed(kb::RAlt))
option_hit = true;
if(kb::isKeyPressed(kb::LControl) || kb::isKeyPressed(kb::RControl))
ctrl_hit = true;
for(i = 0; i < NLS; i++)
int x;
for(int i = 0; i < NLS; i++)
if(!mouse_button_held && the_point.in(left_buttons[i][0])
&& (left_button_status[i].action != LB_NO_ACTION)) {
draw_lb_slot(i,1);
@@ -202,11 +187,11 @@ bool handle_action(location the_point,sf::Event /*event*/) {
case LB_NEW_TOWN:
if(change_made) {
giveError("You need to save the changes made to your scenario before you can add a new town.");
return are_done;
return true;
}
if(scenario.towns.size() >= 200) {
giveError("You have reached the limit of 200 towns you can have in one scenario.");
return are_done;
return true;
}
if(new_town(scenario.towns.size()))
set_up_main_screen();
@@ -239,7 +224,6 @@ bool handle_action(location the_point,sf::Event /*event*/) {
case LB_EDIT_OUT:
start_out_edit();
mouse_button_held = false;
return false;
break;
case LB_LOAD_TOWN:
if(change_made) {
@@ -256,7 +240,6 @@ bool handle_action(location the_point,sf::Event /*event*/) {
case LB_EDIT_TOWN:
start_town_edit();
mouse_button_held = false;
return false;
break;
case LB_EDIT_TALK:
start_dialogue_editing(0);
@@ -269,15 +252,18 @@ bool handle_action(location the_point,sf::Event /*event*/) {
set_up_main_screen();
}
mouse_button_held = false;
return true;
}
return false;
}
if(overall_mode == MODE_MAIN_SCREEN) {
right_top = right_sbar->getPosition();
for(i = 0; i < NRSONPAGE; i++)
static bool handle_rb_action(location the_point, bool option_hit) {
long right_top = right_sbar->getPosition();
for(int i = 0; i < NRSONPAGE; i++)
if(!mouse_button_held && (the_point.in(right_buttons[i]) )
&& (right_button_status[i + right_top].action != RB_CLEAR)) {
j = right_button_status[i + right_top].i;
int j = right_button_status[i + right_top].i;
//flash_rect(left_buttons[i][0]);
draw_rb_slot(i + right_top,1);
mainPtr.display();
@@ -361,27 +347,21 @@ bool handle_action(location the_point,sf::Event /*event*/) {
right_sbar->setPosition(pos_before);
break;
case RB_SCEN_STR:
if(option_hit) {
s2 = get_str("scen-default", j + 161);
scenario.spec_strs[j] = s2;
}
if(option_hit)
scenario.spec_strs[j] = get_str("scen-default", j + 161);
else edit_text_str(j,0);
start_string_editing(0,1);
break;
case RB_OUT_STR:
if(option_hit) {
s2 = get_str("outdoor-default", j + 11);
current_terrain->spec_strs[j] = s2;
}
if(option_hit)
current_terrain->spec_strs[j] = get_str("outdoor-default", j + 11);
else edit_text_str(j,1);
start_string_editing(1,1);
break;
case RB_TOWN_STR:
if(option_hit) {
s2 = get_str("town-default", j + 21);
town->spec_strs[j] = s2;
}
if(option_hit)
town->spec_strs[j] = get_str("town-default", j + 21);
else edit_text_str(j,2);
start_string_editing(2,1);
break;
@@ -390,10 +370,8 @@ bool handle_action(location the_point,sf::Event /*event*/) {
start_special_item_editing();
break;
case RB_JOURNAL:
if(option_hit) {
s2 = get_str("scen-default", j + 11);
scenario.journal_strs[j] = s2;
}
if(option_hit)
scenario.journal_strs[j] = get_str("scen-default", j + 11);
else edit_text_str(j,3);
start_string_editing(3,1);
break;
@@ -406,18 +384,14 @@ bool handle_action(location the_point,sf::Event /*event*/) {
start_dialogue_editing(1);
break;
case RB_OUT_SIGN:
if(option_hit) {
s2 = get_str("outdoor-default", j + 101);
current_terrain->spec_strs[j] = s2;
}
if(option_hit)
current_terrain->spec_strs[j] = get_str("outdoor-default", j + 101);
else edit_text_str(j,4);
start_string_editing(4,1);
break;
case RB_TOWN_SIGN:
if(option_hit) {
s2 = get_str("town-default", j + 121);
town->spec_strs[j] = s2;
}
if(option_hit)
town->spec_strs[j] = get_str("town-default", j + 121);
else edit_text_str(j,5);
start_string_editing(5,1);
break;
@@ -442,10 +416,13 @@ bool handle_action(location the_point,sf::Event /*event*/) {
break;
}
mouse_button_held = false;
return true;
}
}
update_mouse_spot(the_point);
if(overall_mode < MODE_MAIN_SCREEN) {
return false;
}
static bool handle_terrain_action(location the_point, bool ctrl_hit) {
int x, i;
if(mouse_spot.x >= 0 && mouse_spot.y >= 0) {
if(cur_viewing_mode == 0) {
spot_hit.x = cen_x + mouse_spot.x - 4;
@@ -470,12 +447,12 @@ bool handle_action(location the_point,sf::Event /*event*/) {
if((mouse_button_held) && (spot_hit.x == last_spot_hit.x) &&
(spot_hit.y == last_spot_hit.y))
return are_done;
return true;
else last_spot_hit = spot_hit;
if(!mouse_button_held)
last_spot_hit = spot_hit;
old_mode = overall_mode;
eScenMode old_mode = overall_mode;
change_made = true;
if((spot_hit.x < 0) || (spot_hit.x > ((editing_town) ? town->max_dim() - 1 : 47)) ||
@@ -931,8 +908,9 @@ bool handle_action(location the_point,sf::Event /*event*/) {
if((overall_mode == MODE_DRAWING) && (old_mode != MODE_DRAWING))
set_string("Drawing mode",(char*)scenario.ter_types[current_terrain_type].name.c_str());
draw_terrain();
return true;
}
bool need_redraw = false;
if((the_point.in(border_rect[0])) & (cen_y > (editing_town ? 4 : 3))) {
cen_y--;
if(ctrl_hit)
@@ -964,18 +942,15 @@ bool handle_action(location the_point,sf::Event /*event*/) {
if(need_redraw) {
draw_terrain();
place_location();
need_redraw = false;
}
return true;
}
return false;
}
if(!mouse_button_held && ((overall_mode < MODE_MAIN_SCREEN) || (overall_mode == MODE_EDIT_TYPES))) {
cur_point = the_point;
cur_point.x -= RIGHT_AREA_UL_X;
cur_point.y -= RIGHT_AREA_UL_Y;
for(i = 0; i < 256; i++)
static bool handle_terpal_action(location cur_point) {
for(int i = 0; i < 256; i++)
if(cur_point.in(terrain_rects[i])) {
temp_rect = terrain_rects[i];
rectangle temp_rect = terrain_rects[i];
temp_rect.offset(RIGHT_AREA_UL_X, RIGHT_AREA_UL_Y );
flash_rect(temp_rect);
if(overall_mode < MODE_MAIN_SCREEN) {
@@ -1004,16 +979,18 @@ bool handle_action(location the_point,sf::Event /*event*/) {
set_up_terrain_buttons(true);
}
place_location();
return true;
}
cur_point2 = cur_point;
cur_point2.x -= 5;
cur_point2.y -= terrain_rects[255].bottom + 5;
for(i = 0; i < 10; i++)
for(j = 0; j < 6; j++) {
return false;
}
static bool handle_toolpal_action(location cur_point2) {
for(int i = 0; i < 10; i++)
for(int j = 0; j < 6; j++) {
auto cur_palette_buttons = editing_town ? town_buttons : out_buttons;
if(cur_palette_buttons[j][i] != PAL_BLANK && !mouse_button_held && cur_point2.in(palette_buttons[i][j])
&& /*((j < 3) || (editing_town)) &&*/ (overall_mode < MODE_MAIN_SCREEN)) {
temp_rect = palette_buttons[i][j];
rectangle temp_rect = palette_buttons[i][j];
temp_rect.offset(RIGHT_AREA_UL_X + 5, RIGHT_AREA_UL_Y + terrain_rects[255].bottom + 5);
flash_rect(temp_rect);
switch(cur_palette_buttons[j][i]) {
@@ -1061,7 +1038,8 @@ bool handle_action(location the_point,sf::Event /*event*/) {
break;
case PAL_ZOOM: // switch view
cur_viewing_mode = (cur_viewing_mode + 1) % 4;
need_redraw = true;
draw_main_screen();
draw_terrain();
break;
case PAL_ERASER:
set_string("Erase space","Select space to clear");
@@ -1087,7 +1065,8 @@ bool handle_action(location the_point,sf::Event /*event*/) {
break;
case PAL_CHANGE: // replace terrain
swap_terrain();
need_redraw = true;
draw_main_screen();
draw_terrain();
mouse_button_held = false;
break;
case PAL_EDIT_TOWN:
@@ -1302,15 +1281,50 @@ bool handle_action(location the_point,sf::Event /*event*/) {
set_up_terrain_buttons(true);
break;
}
return true;
}
}
}
if(need_redraw) {
draw_main_screen();
draw_terrain();
return false;
}
void handle_action(location the_point,sf::Event /*event*/) {
using kb = sf::Keyboard;
std::string s2;
bool option_hit = false,ctrl_hit = false;
location spot_hit;
location cur_point,cur_point2;
rectangle temp_rect;
if(kb::isKeyPressed(kb::LAlt) || kb::isKeyPressed(kb::RAlt))
option_hit = true;
if(kb::isKeyPressed(kb::LControl) || kb::isKeyPressed(kb::RControl))
ctrl_hit = true;
if(handle_lb_action(the_point))
return;
if(overall_mode == MODE_MAIN_SCREEN && handle_rb_action(the_point, option_hit))
return;
update_mouse_spot(the_point);
if(overall_mode < MODE_MAIN_SCREEN && handle_terrain_action(the_point, ctrl_hit))
return;
if(!mouse_button_held && ((overall_mode < MODE_MAIN_SCREEN) || (overall_mode == MODE_EDIT_TYPES))) {
cur_point = the_point;
cur_point.x -= RIGHT_AREA_UL_X;
cur_point.y -= RIGHT_AREA_UL_Y;
if(handle_terpal_action(cur_point))
return;
cur_point2 = cur_point;
cur_point2.x -= 5;
cur_point2.y -= terrain_rects[255].bottom + 5;
if(handle_toolpal_action(cur_point2))
return;
}
return are_done;
return;
}
@@ -1368,7 +1382,6 @@ void handle_keystroke(sf::Event event) {
Key chr2 = event.key.code;
char chr;
short i,j,store_ter;
bool are_done;
obscureCursor();
@@ -1382,7 +1395,7 @@ void handle_keystroke(sf::Event event) {
}
else {
pass_point = terrain_click[i];
are_done = handle_action(pass_point,event);
handle_action(pass_point,event);
draw_terrain();
mouse_button_held = false;
return;
@@ -1515,7 +1528,7 @@ void handle_keystroke(sf::Event event) {
mouse_button_held = false;
}
bool handle_scroll(sf::Event& event) {
void handle_scroll(sf::Event& event) {
rectangle pal_rect = terrain_buttons_rect, right_area_rect = {0,0,RIGHT_AREA_HEIGHT,RIGHT_AREA_WIDTH};
right_area_rect.offset(RIGHT_AREA_UL_X, RIGHT_AREA_UL_Y);
pal_rect.offset(RIGHT_AREA_UL_X,RIGHT_AREA_UL_Y);
@@ -1536,7 +1549,6 @@ bool handle_scroll(sf::Event& event) {
else cen_y = minmax(4, town->max_dim() - 5, cen_y - amount);
redraw_screen();
}
return true;
}
void shy_change_circle_terrain(location center,short radius,ter_num_t terrain_type,short probability) {

View File

@@ -1,11 +1,11 @@
void init_current_terrain();
void init_screen_locs();
bool handle_action(location the_point,sf::Event event);
void handle_action(location the_point,sf::Event event);
void flash_rect(rectangle to_flash);
void swap_terrain();
void set_new_terrain(ter_num_t selected_terrain);
void handle_keystroke(sf::Event event);
bool handle_scroll(sf::Event& event);
void handle_scroll(sf::Event& event);
void get_wandering_monst();
void get_town_info();
void get_sign_resource();

View File

@@ -535,9 +535,7 @@ void Mouse_Pressed() {
updater.join();
redraw_screen(/*REFRESH_RIGHT_BAR*/);
set_up_terrain_buttons(false);
}
else // ordinary click
All_Done = handle_action(loc(event.mouseButton.x,event.mouseButton.y),event);
} else handle_action(loc(event.mouseButton.x,event.mouseButton.y),event);
}
void close_program() {