dxx-rebirth/common/ui/menu.cpp

152 lines
3.4 KiB
C++
Raw Normal View History

2006-03-20 17:12:09 +00:00
/*
2014-06-01 17:55:23 +00:00
* Portions of this file are copyright Rebirth contributors and licensed as
* described in COPYING.txt.
* Portions of this file are copyright Parallax Software and licensed
* according to the Parallax license below.
* See COPYING.txt for license details.
2006-03-20 17:12:09 +00:00
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
#include <stdlib.h>
#include "u_mem.h"
2012-07-01 02:54:33 +00:00
#include "maths.h"
2006-03-20 17:12:09 +00:00
#include "pstypes.h"
#include "gr.h"
#include "event.h"
#include "mouse.h"
2006-03-20 17:12:09 +00:00
#include "ui.h"
2014-08-16 04:15:16 +00:00
#include "dxxsconf.h"
#include "dsx-ns.h"
#include <memory>
2006-03-20 17:12:09 +00:00
2015-12-13 18:00:49 +00:00
namespace dcx {
2015-12-05 22:57:24 +00:00
2006-03-20 17:12:09 +00:00
#define MENU_BORDER 2
#define MENU_VERT_SPACING 2
namespace {
struct menu
{
std::unique_ptr<std::unique_ptr<UI_GADGET_BUTTON>[]> button_g;
2014-08-07 02:58:00 +00:00
std::unique_ptr<const char *[]> button;
int *choice;
int num_buttons;
};
}
static window_event_result menu_handler(UI_DIALOG *,const d_event &event, menu *m)
{
for (int i=0; i<m->num_buttons; i++ )
{
if (GADGET_PRESSED(m->button_g[i].get()))
{
*(m->choice) = i+1;
return window_event_result::handled;
}
}
if ( (*(m->choice)==0) && B1_JUST_RELEASED )
{
*(m->choice) = -1;
return window_event_result::handled;
}
return window_event_result::ignored;
}
2013-09-02 23:21:13 +00:00
int MenuX( int x, int y, int NumButtons, const char *const text[] )
2006-03-20 17:12:09 +00:00
{
UI_DIALOG * dlg;
2017-02-11 21:42:34 +00:00
int button_width, button_height;
2006-03-20 17:12:09 +00:00
int w, h;
int choice;
2020-05-02 21:18:42 +00:00
auto m = std::make_unique<menu>();
m->num_buttons = NumButtons;
2020-05-02 21:18:42 +00:00
m->button_g = std::make_unique<std::unique_ptr<UI_GADGET_BUTTON>[]>(NumButtons);
m->button = std::make_unique<const char *[]>(NumButtons);
m->choice = &choice;
2006-03-20 17:12:09 +00:00
button_width = button_height = 0;
for (int i=0; i<NumButtons; i++ )
2006-03-20 17:12:09 +00:00
{
m->button[i] = text[i];
2006-03-20 17:12:09 +00:00
2017-02-11 21:42:34 +00:00
int width, height;
ui_get_button_size(*grd_curcanv->cv_font, m->button[i], width, height);
2006-03-20 17:12:09 +00:00
if ( width > button_width ) button_width = width;
if ( height > button_height ) button_height = height;
}
2017-02-11 21:42:34 +00:00
unsigned width, height;
2006-03-20 17:12:09 +00:00
width = button_width + 2*(MENU_BORDER+3);
height = (button_height*NumButtons) + (MENU_VERT_SPACING*(NumButtons-1)) ;
height += (MENU_BORDER+3) * 2;
2015-03-22 18:49:21 +00:00
w = grd_curscreen->get_screen_width();
h = grd_curscreen->get_screen_height();
2006-03-20 17:12:09 +00:00
{
int mx, my, mz;
mouse_get_pos(&mx, &my, &mz);
if ( x == -1 ) x = mx - width/2;
if ( y == -1 ) y = my;
}
2006-03-20 17:12:09 +00:00
if (x < 0 ) {
x = 0;
}
if ( (x+width-1) >= w ) {
x = w - width;
}
if (y < 0 ) {
y = 0;
}
if ( (y+height-1) >= h ) {
y = h - height;
}
2014-08-07 02:54:22 +00:00
dlg = ui_create_dialog(x, y, width, height, static_cast<dialog_flags>(DF_FILLED | DF_SAVE_BG | DF_MODAL), menu_handler, m.get());
2006-03-20 17:12:09 +00:00
x = MENU_BORDER+3;
y = MENU_BORDER+3;
for (int i=0; i<NumButtons; i++ )
2006-03-20 17:12:09 +00:00
{
m->button_g[i] = ui_add_gadget_button( dlg, x, y, button_width, button_height, m->button[i], NULL );
2006-03-20 17:12:09 +00:00
y += button_height+MENU_VERT_SPACING;
}
choice = 0;
while(choice==0)
event_process();
2006-03-20 17:12:09 +00:00
ui_close_dialog(dlg);
2006-03-20 17:12:09 +00:00
return choice;
}
2015-12-05 22:57:24 +00:00
}