tiny 'arrow' buttons use event loop to allow cancel click

This commit is contained in:
2025-03-15 16:50:17 -05:00
parent 9199502c62
commit b401b3dbc1
3 changed files with 100 additions and 72 deletions

View File

@@ -1612,7 +1612,7 @@ bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter) {
if(point_in_area.in(pc_help_button)) { if(point_in_area.in(pc_help_button)) {
rectangle help_button = pc_help_button; rectangle help_button = pc_help_button;
help_button.offset(pc_win_ul); help_button.offset(pc_win_ul);
arrow_button_click(help_button); if(arrow_button_click(help_button, &fps_limiter))
show_dialog_action("help-party"); show_dialog_action("help-party");
} }
for(int i = 0; i < 6; i++) for(int i = 0; i < 6; i++)
@@ -1623,7 +1623,7 @@ bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter) {
break; break;
rectangle button_rect = pc_buttons[i][j]; rectangle button_rect = pc_buttons[i][j];
button_rect.offset(pc_win_ul); button_rect.offset(pc_win_ul);
arrow_button_click(button_rect); if(arrow_button_click(button_rect, &fps_limiter))
switch(j) { switch(j) {
case PCBTN_NAME: case PCBTN_NAME:
handle_switch_pc(i, need_redraw, need_reprint); handle_switch_pc(i, need_redraw, need_reprint);
@@ -1664,7 +1664,7 @@ bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter) {
if(item_bottom_button_active[i] > 0 && point_in_area.in(item_screen_button_rects[i])) { if(item_bottom_button_active[i] > 0 && point_in_area.in(item_screen_button_rects[i])) {
rectangle button_rect = item_screen_button_rects[i]; rectangle button_rect = item_screen_button_rects[i];
button_rect.offset(item_win_ul); button_rect.offset(item_win_ul);
arrow_button_click(button_rect); if(arrow_button_click(button_rect, &fps_limiter))
switch(i) { switch(i) {
case 6: // special screen case 6: // special screen
set_stat_window(ITEM_WIN_SPECIAL, true); set_stat_window(ITEM_WIN_SPECIAL, true);
@@ -1686,8 +1686,7 @@ bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter) {
if(item_area_button_active[i][j] && point_in_area.in(item_buttons[i][j])) { if(item_area_button_active[i][j] && point_in_area.in(item_buttons[i][j])) {
rectangle button_rect = item_buttons[i][j]; rectangle button_rect = item_buttons[i][j];
button_rect.offset(item_win_ul); button_rect.offset(item_win_ul);
arrow_button_click(button_rect); if(arrow_button_click(button_rect, &fps_limiter)){
item_hit = item_sbar->getPosition() + i; item_hit = item_sbar->getPosition() + i;
switch(j) { switch(j) {
case ITEMBTN_NAME: case ITEMBTN_ICON: // equip case ITEMBTN_NAME: case ITEMBTN_ICON: // equip
@@ -1716,6 +1715,7 @@ bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter) {
} }
} }
} }
}
update_item_stats_area(need_reprint); update_item_stats_area(need_reprint);
} }

View File

@@ -482,21 +482,48 @@ void draw_start_button(eStartButton which_position,short which_button) {
win_draw_string(mainPtr(),to_rect,button_labels[which_position],eTextMode::CENTRE,style); win_draw_string(mainPtr(),to_rect,button_labels[which_position],eTextMode::CENTRE,style);
} }
void arrow_button_click(rectangle button_rect) { bool arrow_button_click(rectangle button_rect, cFramerateLimiter* fps_limiter) {
if(recording){ if(recording){
// This action is purely cosmetic, for playing the animation and sound accompanying a click on a button whose real action // In a replay, this action is purely cosmetic, for playing the animation and sound
// is recorded afterward // accompanying a click on a button whose real action is recorded afterward
record_action("arrow_button_click", boost::lexical_cast<std::string>(button_rect)); record_action("arrow_button_click", boost::lexical_cast<std::string>(button_rect));
} }
// Draw depressed:
mainPtr().setActive(); mainPtr().setActive();
clip_rect(mainPtr(), button_rect); clip_rect(mainPtr(), button_rect);
// TODO: Mini-event loop so that the click doesn't happen until releasing the mouse button
refresh_stat_areas(1); refresh_stat_areas(1);
mainPtr().display(); mainPtr().display();
play_sound(37, time_in_ticks(5));
// Mini-event loop so that the click doesn't happen until releasing the mouse button:
bool done = false, clicked = false, depressed = true;
if(replaying) clicked = true;
else{
sf::Event e;
while(!done){
refresh_stat_areas(depressed ? 1 : 0);
while(pollEvent(mainPtr(), e)){
if(e.type == sf::Event::MouseButtonReleased){
done = true;
location clickPos(e.mouseButton.x, e.mouseButton.y);
clickPos = mainPtr().mapPixelToCoords(clickPos);
clicked = button_rect.contains(clickPos);
depressed = false;
break;
} else if(e.type == sf::Event::MouseMoved){
location toPos(e.mouseMove.x, e.mouseMove.y);
toPos = mainPtr().mapPixelToCoords(toPos);
depressed = button_rect.contains(toPos);
}
}
fps_limiter->frame_finished();
}
}
undo_clip(mainPtr()); undo_clip(mainPtr());
play_sound(37, time_in_ticks(5));
refresh_stat_areas(0); refresh_stat_areas(0);
return clicked;
} }

View File

@@ -4,6 +4,7 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "location.hpp" #include "location.hpp"
#include "tools/framerate_limiter.hpp"
enum { enum {
REFRESH_NONE = 0, REFRESH_NONE = 0,
@@ -33,7 +34,7 @@ void draw_startup(short but_type);
void draw_anim(); void draw_anim();
void place_anim(); void place_anim();
void draw_start_button(eStartButton which_position,short which_button); void draw_start_button(eStartButton which_position,short which_button);
void arrow_button_click(rectangle button_rect); bool arrow_button_click(rectangle button_rect, cFramerateLimiter* fps_limiter = nullptr);
void end_startup(); void end_startup();
void load_main_screen(); void load_main_screen();
void redraw_screen(int refresh); void redraw_screen(int refresh);