From cf1d08c1b604359f60a10d684078c7ceaed49d62 Mon Sep 17 00:00:00 2001 From: Kp Date: Sat, 20 Dec 2014 04:36:08 +0000 Subject: [PATCH] Use unique_ptr for hostage_dialog --- common/include/ui.h | 8 ++++ d1x-rebirth/editor/ehostage.cpp | 73 ++++++++++++++------------------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/common/include/ui.h b/common/include/ui.h index 066401235..63a993fc3 100644 --- a/common/include/ui.h +++ b/common/include/ui.h @@ -267,6 +267,14 @@ UI_DIALOG * ui_create_dialog( short x, short y, short w, short h, enum dialog_fl return ui_create_dialog(x, y, w, h, flags, (ui_subfunction_t::type) callback, (void *)userdata); } +template +UI_DIALOG *ui_create_dialog(short x, short y, short w, short h, enum dialog_flags flags, typename ui_subfunction_t::type callback, std::unique_ptr userdata) +{ + auto r = ui_create_dialog(x, y, w, h, flags, callback, userdata.get()); + userdata.release(); + return r; +} + template <> UI_DIALOG * ui_create_dialog( short x, short y, short w, short h, enum dialog_flags flags, ui_subfunction_t::type callback, void *userdata ); diff --git a/d1x-rebirth/editor/ehostage.cpp b/d1x-rebirth/editor/ehostage.cpp index b25968100..abb45fc0a 100644 --- a/d1x-rebirth/editor/ehostage.cpp +++ b/d1x-rebirth/editor/ehostage.cpp @@ -52,6 +52,8 @@ COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. #include "piggy.h" #include "u_mem.h" +#include "compiler-make_unique.h" + //------------------------------------------------------------------------- // Variables for this module... //------------------------------------------------------------------------- @@ -247,19 +249,13 @@ static int hostage_dialog_handler(UI_DIALOG *dlg,const d_event &event, hostage_d //------------------------------------------------------------------------- int do_hostage_dialog() { - int i; - hostage_dialog *h; - // Only open 1 instance of this window... if ( MainWindow != NULL ) return 0; // Close other windows close_all_windows(); - MALLOC(h, hostage_dialog, 1); - if (!h) - return 0; - + auto h = make_unique(); h->vclip_animation_time = 0; h->vclip_playback_speed = 0; h->vclip_ptr = NULL; @@ -268,36 +264,26 @@ int do_hostage_dialog() SelectClosestHostage(); // Open a window with a quit button - MainWindow = ui_create_dialog( TMAPBOX_X+10, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, DF_DIALOG, hostage_dialog_handler, h ); - h->quitButton = ui_add_gadget_button( MainWindow, 20, 222, 48, 40, "Done", NULL ); - - h->hostageText = ui_add_gadget_inputbox( MainWindow, 10, 50, HOSTAGE_MESSAGE_LEN, HOSTAGE_MESSAGE_LEN, HostageMessage ); - - // The little box the hostage vclip will play in. - h->hostageViewBox = ui_add_gadget_userbox( MainWindow,10, 90+10, 64, 64 ); - - // A bunch of buttons... - i = 90; -//@@ ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Type", SelectPrevVclip ); -//@@ ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Type >>", SelectNextVclip );i += 29; -//@@ ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Sound", find_prev_hostage_sound ); -//@@ ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Sound >>", find_next_hostage_sound );i += 29; - - ui_add_gadget_button( MainWindow,155,i,140, 26, "Next Hostage", SelectNextHostage ); i += 29; - ui_add_gadget_button( MainWindow,155,i,140, 26, "Prev Hostage", SelectPrevHostage ); i += 29; - ui_add_gadget_button( MainWindow,155,i,140, 26, "Compress All", CompressHostages ); i += 29; - ui_add_gadget_button( MainWindow,155,i,140, 26, "Delete", ObjectDelete ); i += 29; - ui_add_gadget_button( MainWindow,155,i,140, 26, "Create New", PlaceHostage ); i += 29; - - h->time = timer_query(); - - LastHostageIndex = -2; // Set to some dummy value so everything works ok on the first frame. - -// if ( CurrentHostageIndex == -1 ) -// SelectNextHostage(); - + MainWindow = ui_create_dialog(TMAPBOX_X+10, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, DF_DIALOG, hostage_dialog_handler, std::move(h)); return 1; +} +static int hostage_dialog_created(UI_DIALOG *const w, hostage_dialog *const h) +{ + h->quitButton = ui_add_gadget_button(w, 20, 222, 48, 40, "Done", NULL); + h->hostageText = ui_add_gadget_inputbox(w, 10, 50, HOSTAGE_MESSAGE_LEN, HOSTAGE_MESSAGE_LEN, HostageMessage); + // The little box the hostage vclip will play in. + h->hostageViewBox = ui_add_gadget_userbox(w, 10, 90+10, 64, 64); + // A bunch of buttons... + int i = 90; + ui_add_gadget_button(w, 155, i, 140, 26, "Next Hostage", SelectNextHostage); i += 29; + ui_add_gadget_button(w, 155, i, 140, 26, "Prev Hostage", SelectPrevHostage); i += 29; + ui_add_gadget_button(w, 155, i, 140, 26, "Compress All", CompressHostages); i += 29; + ui_add_gadget_button(w, 155, i, 140, 26, "Delete", ObjectDelete); i += 29; + ui_add_gadget_button(w, 155, i, 140, 26, "Create New", PlaceHostage); i += 29; + h->time = timer_query(); + LastHostageIndex = -2; // Set to some dummy value so everything works ok on the first frame. + return 0; } void hostage_close_window() @@ -310,6 +296,16 @@ void hostage_close_window() static int hostage_dialog_handler(UI_DIALOG *dlg,const d_event &event, hostage_dialog *h) { + switch(event.type) + { + case EVENT_WINDOW_CREATED: + return hostage_dialog_created(dlg, h); + case EVENT_WINDOW_CLOSE: + std::default_delete()(h); + return 0; + default: + break; + } fix64 Temp; int keypress = 0; int rval = 0; @@ -387,13 +383,6 @@ static int hostage_dialog_handler(UI_DIALOG *dlg,const d_event &event, hostage_d if (ui_button_any_drawn || (LastHostageIndex != CurrentHostageIndex)) Update_flags |= UF_WORLD_CHANGED; - - if (event.type == EVENT_WINDOW_CLOSE) - { - d_free(h); - return 0; - } - if ( GADGET_PRESSED(h->quitButton) || (keypress==KEY_ESC)) { hostage_close_window();