dxx-rebirth/common/ui/message.cpp

226 lines
5 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 <stdio.h>
#include <stdarg.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"
2006-03-20 17:12:09 +00:00
#include "ui.h"
#include "mouse.h"
2006-03-20 17:12:09 +00:00
#include "key.h"
#include "u_mem.h"
2006-03-20 17:12:09 +00:00
#include "dxxsconf.h"
#include "dsx-ns.h"
#include <array>
#include <memory>
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
// ts = total span
// w = width of each item
// n = number of items
// i = item number (0 based)
#define EVEN_DIVIDE(ts,w,n,i) ((((ts)-((w)*(n)))*((i)+1))/((n)+1))+((w)*(i))
#define BUTTON_HORZ_SPACING 20
#define TEXT_EXTRA_HEIGHT 5
namespace {
2020-10-12 03:28:25 +00:00
struct messagebox : UI_DIALOG
{
2020-10-12 03:28:25 +00:00
explicit messagebox(short x, short y, short w, short h, enum dialog_flags flags) :
UI_DIALOG(x, y, w, h, flags, nullptr, nullptr)
{
}
const ui_messagebox_tie *button = nullptr;
2020-05-02 21:18:42 +00:00
std::array<std::unique_ptr<UI_GADGET_BUTTON>, 10> button_g;
2020-10-12 03:28:25 +00:00
const char *text = nullptr;
int *choice = nullptr;
int width;
int text_y;
int line_y;
2020-10-12 03:28:25 +00:00
virtual window_event_result callback_handler(const d_event &) override;
};
2020-10-12 03:28:25 +00:00
window_event_result messagebox::callback_handler(const d_event &event)
{
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_UI_DIALOG_DRAW)
{
2014-07-20 03:48:27 +00:00
const grs_font * temp_font;
gr_set_current_canvas(grd_curscreen->sc_canvas);
2017-03-10 01:22:25 +00:00
auto &canvas = *grd_curcanv;
temp_font = grd_curscreen->sc_canvas.cv_font;
2015-03-22 18:49:21 +00:00
if (grd_curscreen->get_screen_width() < 640) {
2014-07-20 03:48:27 +00:00
grd_curscreen->sc_canvas.cv_font = ui_small_font.get();
}
ui_dialog_set_current_canvas(*this);
2020-10-12 03:28:25 +00:00
ui_string_centered(canvas, width / 2, text_y, text);
2020-10-12 03:28:25 +00:00
Hline(canvas, 1, width - 2, line_y + 1, CGREY);
Hline(canvas, 2, width - 2, line_y + 2, CBRIGHT);
grd_curscreen->sc_canvas.cv_font = temp_font;
return window_event_result::handled;
}
2020-10-12 03:28:25 +00:00
for (uint_fast32_t i=0; i < button->count(); i++ )
{
2020-10-12 03:28:25 +00:00
if (GADGET_PRESSED(button_g[i].get()))
{
2020-10-12 03:28:25 +00:00
*(choice) = i+1;
return window_event_result::handled;
}
}
return window_event_result::ignored;
}
2020-10-12 03:28:25 +00:00
}
2013-12-04 23:31:29 +00:00
int (ui_messagebox)( short xc, short yc, const char * text, const ui_messagebox_tie &Button )
2006-03-20 17:12:09 +00:00
{
2017-02-11 21:42:34 +00:00
int avg, x, y;
2006-03-20 17:12:09 +00:00
int button_width, button_height, text_height, text_width;
int w, h;
int choice;
button_width = button_height = 0;
gr_set_current_canvas(grd_curscreen->sc_canvas);
2017-03-10 01:22:25 +00:00
auto &canvas = *grd_curcanv;
auto &cv_font = *canvas.cv_font;
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
2014-09-20 23:47:27 +00:00
for (uint_fast32_t i=0; i < Button.count(); i++ )
2006-03-20 17:12:09 +00:00
{
2017-02-11 21:42:34 +00:00
int width, height;
2017-03-10 01:22:25 +00:00
ui_get_button_size(cv_font, Button.string(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-03-10 01:22:25 +00:00
gr_get_string_size(cv_font, text, &text_width, &text_height, &avg);
2006-03-20 17:12:09 +00:00
2017-02-11 21:42:34 +00:00
unsigned width, height;
2013-12-04 23:31:29 +00:00
width = button_width*Button.count();
width += BUTTON_HORZ_SPACING*(Button.count()+1);
2006-03-20 17:12:09 +00:00
width ++;
text_width += avg*6;
text_width += 10;
if (text_width > width )
width = text_width;
height = text_height;
height += button_height;
height += 4*TEXT_EXTRA_HEIGHT;
height += 2; // For line in middle
{
int mx, my, mz;
mouse_get_pos(&mx, &my, &mz);
2006-03-20 17:12:09 +00:00
if ( xc == -1 )
xc = mx;
if ( yc == -1 )
yc = my - button_height/2;
}
2006-03-20 17:12:09 +00:00
if ( xc == -2 )
xc = w/2;
if ( yc == -2 )
yc = h/2;
x = xc - width/2;
y = yc - height/2;
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;
}
const auto dlg = ui_create_dialog<messagebox>(x, y, width, height, static_cast<dialog_flags>(DF_DIALOG | DF_MODAL));
2020-10-12 03:28:25 +00:00
dlg->button = &Button;
dlg->text = text;
dlg->choice = &choice;
2006-03-20 17:12:09 +00:00
y = TEXT_EXTRA_HEIGHT + text_height/2 - 1;
2020-10-12 03:28:25 +00:00
dlg->width = width;
dlg->text_y = y;
2006-03-20 17:12:09 +00:00
y = 2*TEXT_EXTRA_HEIGHT + text_height;
2020-10-12 03:28:25 +00:00
dlg->line_y = y;
2006-03-20 17:12:09 +00:00
y = height - TEXT_EXTRA_HEIGHT - button_height;
2014-09-20 23:47:27 +00:00
for (uint_fast32_t i=0; i < Button.count(); i++ )
2006-03-20 17:12:09 +00:00
{
2013-12-04 23:31:29 +00:00
x = EVEN_DIVIDE(width,button_width,Button.count(),i);
2006-03-20 17:12:09 +00:00
dlg->button_g[i] = ui_add_gadget_button(*dlg, x, y, button_width, button_height, Button.string(i), nullptr);
2006-03-20 17:12:09 +00:00
}
2020-10-12 03:28:26 +00:00
ui_gadget_calc_keys(*dlg);
2006-03-20 17:12:09 +00:00
//key_flush();
2020-10-12 03:28:25 +00:00
dlg->keyboard_focus_gadget = dlg->button_g[0].get();
2006-03-20 17:12:09 +00:00
choice = 0;
while(choice==0)
event_process();
2006-03-20 17:12:09 +00:00
2020-10-12 03:28:26 +00:00
ui_close_dialog(*dlg);
2006-03-20 17:12:09 +00:00
return choice;
}
2015-12-05 22:57:24 +00:00
}