| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Routines for menus. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include "fwd-event.h" |
| 29 |
|
| 30 |
#include <cstdint> |
| 31 |
#include <algorithm> |
| 32 |
#include <memory> |
| 33 |
#include <span> |
| 34 |
#include <stdexcept> |
| 35 |
#include <tuple> |
| 36 |
#include <type_traits> |
| 37 |
#include <ranges> |
| 38 |
#include <utility> |
| 39 |
#include "varutil.h" |
| 40 |
#include "dxxsconf.h" |
| 41 |
#include "dsx-ns.h" |
| 42 |
#include "fmtcheck.h" |
| 43 |
#include "ntstring.h" |
| 44 |
#include "partial_range.h" |
| 45 |
#ifdef DXX_BUILD_DESCENT |
| 46 |
#include "gamefont.h" |
| 47 |
#include "window.h" |
| 48 |
|
| 49 |
namespace dcx { |
| 50 |
|
| 51 |
struct listbox; |
| 52 |
|
| 53 |
enum class nm_type : uint8_t |
| 54 |
{ |
| 55 |
menu = 0, // A menu item... when enter is hit on this, newmenu_do returns this item number |
| 56 |
input = 1, // An input box... fills the text field in, and you need to fill in text_len field. |
| 57 |
check = 2, // A check box. Set and get its status by looking at flags field (1=on, 0=off) |
| 58 |
radio = 3, // Same as check box, but only 1 in a group can be set at a time. Set group fields. |
| 59 |
text = 4, // A line of text that does nothing. |
| 60 |
number = 5, // A numeric entry counter. Changes value from min_value to max_value; |
| 61 |
input_menu = 6, // A inputbox that you hit Enter to edit, when done, hit enter and menu leaves. |
| 62 |
slider = 7, // A slider from min_value to max_value. Draws with text_len chars. |
| 63 |
}; |
| 64 |
|
| 65 |
#define NM_MAX_TEXT_LEN 255 |
| 66 |
|
| 67 |
class newmenu_item |
| 68 |
{ |
| 69 |
struct input_common_type |
| 70 |
{ |
| 71 |
const char *allowed_chars; |
| 72 |
unsigned text_len; |
| 73 |
/* Only used by imenu, but placing it in imenu_specific_type |
| 74 |
* makes newmenu_item non-POD. Some users expect newmenu_item |
| 75 |
* to be POD. Placing group here does not increase overall size |
| 76 |
* since number_slider_common_type also has two int members. |
| 77 |
*/ |
| 78 |
int group; |
| 79 |
}; |
| 80 |
struct number_slider_common_type |
| 81 |
{ |
| 82 |
int min_value; |
| 83 |
int max_value; |
| 84 |
}; |
| 85 |
public: |
| 86 |
struct input_specific_type : input_common_type |
| 87 |
{ |
| 88 |
static constexpr std::integral_constant<nm_type, nm_type::input> static_type{}; |
| 89 |
}; |
| 90 |
struct radio_specific_type |
| 91 |
{ |
| 92 |
static constexpr std::integral_constant<nm_type, nm_type::radio> static_type{}; |
| 93 |
int group; // What group this belongs to for radio buttons. |
| 94 |
}; |
| 95 |
struct number_specific_type : number_slider_common_type |
| 96 |
{ |
| 97 |
static constexpr std::integral_constant<nm_type, nm_type::number> static_type{}; |
| 98 |
}; |
| 99 |
struct imenu_specific_type : input_common_type |
| 100 |
{ |
| 101 |
static constexpr std::integral_constant<nm_type, nm_type::input_menu> static_type{}; |
| 102 |
imenu_specific_type(input_common_type n, ntstring<NM_MAX_TEXT_LEN> &t) : |
| 103 |
input_common_type{n}, |
| 104 |
saved_text{t} |
| 105 |
{ |
| 106 |
} |
| 107 |
ntstring<NM_MAX_TEXT_LEN> &saved_text; |
| 108 |
}; |
| 109 |
struct slider_specific_type : number_slider_common_type |
| 110 |
{ |
| 111 |
static constexpr std::integral_constant<nm_type, nm_type::slider> static_type{}; |
| 112 |
slider_specific_type(number_slider_common_type n, ntstring<NM_MAX_TEXT_LEN> &t) : |
| 113 |
number_slider_common_type{n}, |
| 114 |
saved_text{t} |
| 115 |
{ |
| 116 |
} |
| 117 |
ntstring<NM_MAX_TEXT_LEN> &saved_text; |
| 118 |
}; |
| 119 |
private: |
| 120 |
static void check_union_type(const nm_type current_type, const nm_type static_type) |
| 121 |
{ |
| 122 |
#ifdef DXX_CONSTANT_TRUE |
| 123 |
if (DXX_CONSTANT_TRUE(current_type != static_type)) |
| 124 |
DXX_ALWAYS_ERROR_FUNCTION("invalid type access"); |
| 125 |
#endif |
| 126 |
if (current_type != static_type) |
| 127 |
throw std::runtime_error("invalid type access"); |
| 128 |
} |
| 129 |
template <typename T, nm_type static_type = T::static_type> |
| 130 |
T &get_union_member(T &v) const |
| 131 |
{ |
| 132 |
check_union_type(type, static_type); |
| 133 |
return v; |
| 134 |
} |
| 135 |
void initialize_imenu(char *const text, const uint16_t len, ntstring<NM_MAX_TEXT_LEN> &saved_text) |
| 136 |
{ |
| 137 |
this->text = text; |
| 138 |
auto &im = imenu(); |
| 139 |
new(&im) newmenu_item::imenu_specific_type({nullptr, len, 0}, saved_text); |
| 140 |
} |
| 141 |
public: |
| 142 |
struct nm_item_text |
| 143 |
{ |
| 144 |
const char *text; |
| 145 |
}; |
| 146 |
struct nm_item_menu |
| 147 |
{ |
| 148 |
const char *text; |
| 149 |
}; |
| 150 |
struct nm_item_input |
| 151 |
{ |
| 152 |
const char *allowed_chars; |
| 153 |
char *const text; |
| 154 |
const uint16_t size; |
| 155 |
template <std::size_t len> |
| 156 |
requires(len > 1 && std::in_range<uint16_t>(len)) |
| 157 |
nm_item_input(std::array<char, len> &text, const char *const allowed_chars = nullptr) : |
| 158 |
allowed_chars{allowed_chars}, text{text.data()}, size{len} |
| 159 |
{ |
| 160 |
} |
| 161 |
template <std::size_t len> |
| 162 |
requires(len != std::dynamic_extent && std::in_range<uint16_t>(len)) |
| 163 |
nm_item_input(const std::span<char, len> text) : |
| 164 |
allowed_chars{nullptr}, text{text.data()}, size{len} |
| 165 |
{ |
| 166 |
} |
| 167 |
}; |
| 168 |
struct nm_item_slider |
| 169 |
{ |
| 170 |
int min_value; |
| 171 |
int max_value; |
| 172 |
ntstring<NM_MAX_TEXT_LEN> &saved_text; |
| 173 |
}; |
| 174 |
newmenu_item() = default; |
| 175 |
newmenu_item(nm_item_text text) : |
| 176 |
text(const_cast<char *>(text.text)), |
| 177 |
type(nm_type::text) |
| 178 |
{ |
| 179 |
} |
| 180 |
newmenu_item(nm_item_menu menu) : |
| 181 |
text(const_cast<char *>(menu.text)), |
| 182 |
type(nm_type::menu) |
| 183 |
{ |
| 184 |
} |
| 185 |
newmenu_item(nm_item_input input) : |
| 186 |
text{input.text}, |
| 187 |
type(nm_type::input), |
| 188 |
nm_private(input) |
| 189 |
{ |
| 190 |
} |
| 191 |
input_specific_type &input() { |
| 192 |
return get_union_member(nm_private.input); |
| 193 |
} |
| 194 |
const radio_specific_type &radio() const |
| 195 |
{ |
| 196 |
return get_union_member(nm_private.radio); |
| 197 |
} |
| 198 |
radio_specific_type &radio() { |
| 199 |
return get_union_member(nm_private.radio); |
| 200 |
} |
| 201 |
number_specific_type &number() { |
| 202 |
return get_union_member(nm_private.number); |
| 203 |
} |
| 204 |
imenu_specific_type &imenu() { |
| 205 |
return get_union_member(nm_private.imenu); |
| 206 |
} |
| 207 |
slider_specific_type &slider() { |
| 208 |
return get_union_member(nm_private.slider); |
| 209 |
} |
| 210 |
number_slider_common_type *number_or_slider() { |
| 211 |
return (type == nm_private.number.static_type || type == nm_private.slider.static_type) |
| 212 |
? &nm_private.number |
| 213 |
: nullptr; |
| 214 |
} |
| 215 |
input_common_type *input_or_menu() { |
| 216 |
return (type == nm_private.input.static_type || type == nm_private.imenu.static_type) |
| 217 |
? &nm_private.input |
| 218 |
: nullptr; |
| 219 |
} |
| 220 |
char *text |
| 221 |
#ifndef NDEBUG |
| 222 |
= reinterpret_cast<char *>(UINTPTR_MAX); |
| 223 |
#endif |
| 224 |
; // The text associated with this item. |
| 225 |
int value; // For checkboxes and radio buttons, this is 1 if marked initially, else 0 |
| 226 |
// The rest of these are used internally by by the menu system, so don't set 'em!! |
| 227 |
short x, y; |
| 228 |
short w, h; |
| 229 |
uint8_t right_offset; |
| 230 |
nm_type type |
| 231 |
#ifndef NDEBUG |
| 232 |
{UINT8_MAX}; |
| 233 |
#endif |
| 234 |
; // What kind of item this is, see NM_TYPE_????? defines |
| 235 |
union nm_type_specific_data { |
| 236 |
nm_type_specific_data() : |
| 237 |
input{{nullptr, 0, 0}} |
| 238 |
{ |
| 239 |
} |
| 240 |
nm_type_specific_data(const nm_item_input &input) : |
| 241 |
input{{input.allowed_chars, input.size, 0}} |
| 242 |
{ |
| 243 |
} |
| 244 |
nm_type_specific_data(const nm_item_slider &slider) : |
| 245 |
slider{{slider.min_value, slider.max_value}, slider.saved_text} |
| 246 |
{ |
| 247 |
} |
| 248 |
input_specific_type input; |
| 249 |
radio_specific_type radio; |
| 250 |
number_specific_type number; |
| 251 |
imenu_specific_type imenu; |
| 252 |
slider_specific_type slider; |
| 253 |
}; |
| 254 |
nm_type_specific_data nm_private; |
| 255 |
template <std::size_t len> |
| 256 |
requires(len > 1 && std::in_range<uint16_t>(len)) |
| 257 |
void initialize_imenu(std::array<char, len> &text, ntstring<NM_MAX_TEXT_LEN> &saved_text) |
| 258 |
{ |
| 259 |
initialize_imenu(text.data(), len - 1, saved_text); |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
enum class tab_processing_flag : bool |
| 264 |
{ |
| 265 |
ignore, |
| 266 |
process, |
| 267 |
}; |
| 268 |
|
| 269 |
enum class tiny_mode_flag : bool |
| 270 |
{ |
| 271 |
normal, |
| 272 |
tiny, |
| 273 |
}; |
| 274 |
|
| 275 |
enum class draw_box_flag : bool |
| 276 |
{ |
| 277 |
none, |
| 278 |
menu_background, |
| 279 |
}; |
| 280 |
|
| 281 |
template <typename> |
| 282 |
struct menu_tagged_string |
| 283 |
{ |
| 284 |
const char *const p; |
| 285 |
operator const char *() const |
| 286 |
{ |
| 287 |
return p; |
| 288 |
} |
| 289 |
}; |
| 290 |
|
| 291 |
struct menu_title_tag; |
| 292 |
struct menu_subtitle_tag; |
| 293 |
struct menu_filename_tag; |
| 294 |
using menu_title = menu_tagged_string<menu_title_tag>; |
| 295 |
using menu_subtitle = menu_tagged_string<menu_subtitle_tag>; |
| 296 |
using menu_filename = menu_tagged_string<menu_filename_tag>; |
| 297 |
|
| 298 |
struct newmenu_layout |
| 299 |
{ |
| 300 |
struct adjusted_citem |
| 301 |
{ |
| 302 |
const std::ranges::subrange<newmenu_item *> items; |
| 303 |
const int citem; |
| 304 |
const uint8_t all_text; |
| 305 |
static adjusted_citem create(std::ranges::subrange<newmenu_item *> items, int citem); |
| 306 |
}; |
| 307 |
int x,y,w,h; |
| 308 |
short swidth, sheight; |
| 309 |
// with these we check if resolution or fonts have changed so menu structure can be recreated |
| 310 |
font_x_scale_proportion fntscalex; |
| 311 |
font_y_scale_proportion fntscaley; |
| 312 |
int citem; |
| 313 |
const menu_title title; |
| 314 |
const menu_subtitle subtitle; |
| 315 |
const menu_filename filename; |
| 316 |
grs_canvas &parent_canvas; |
| 317 |
const tiny_mode_flag tiny_mode; |
| 318 |
const tab_processing_flag tabs_flag; |
| 319 |
const uint8_t max_on_menu; |
| 320 |
const uint8_t all_text; //set true if all text items |
| 321 |
const uint8_t is_scroll_box; // Is this a scrolling box? Set to false at init |
| 322 |
const uint8_t max_displayable; |
| 323 |
const draw_box_flag draw_box; |
| 324 |
uint8_t mouse_state; |
| 325 |
const std::ranges::subrange<newmenu_item *> items; |
| 326 |
int scroll_offset{0}; |
| 327 |
newmenu_layout(const menu_title title, const menu_subtitle subtitle, const menu_filename filename, grs_canvas &parent_canvas, const tiny_mode_flag tiny_mode, const tab_processing_flag tabs_flag, const adjusted_citem citem_init, const draw_box_flag draw_box) : |
| 328 |
citem(citem_init.citem), |
| 329 |
title(title), subtitle(subtitle), filename(filename), |
| 330 |
parent_canvas(parent_canvas), |
| 331 |
tiny_mode(tiny_mode), tabs_flag(tabs_flag), |
| 332 |
max_on_menu(std::min<uint8_t>(citem_init.items.size(), tiny_mode != tiny_mode_flag::normal ? 21u : 14u)), |
| 333 |
all_text(citem_init.all_text), |
| 334 |
is_scroll_box(max_on_menu < citem_init.items.size()), |
| 335 |
max_displayable(std::min<uint8_t>(max_on_menu, citem_init.items.size())), |
| 336 |
draw_box(draw_box), |
| 337 |
items(citem_init.items) |
| 338 |
{ |
| 339 |
create_structure(); |
| 340 |
} |
| 341 |
newmenu_layout(newmenu_layout &&) = default; |
| 342 |
/* gcc can implement this as requested. clang implicitly deletes |
| 343 |
* the move-assignment operator= due to the presence of |
| 344 |
* const-qualified member variables. |
| 345 |
*/ |
| 346 |
newmenu_layout &operator=(newmenu_layout &&) = delete; |
| 347 |
void create_structure(); |
| 348 |
}; |
| 349 |
|
| 350 |
struct newmenu : newmenu_layout, window, mixin_trackable_window |
| 351 |
{ |
| 352 |
using subfunction_type = int(*)(newmenu *menu, const d_event &event, void *userdata); |
| 353 |
newmenu(const menu_title title, const menu_subtitle subtitle, const menu_filename filename, const tiny_mode_flag tiny_mode, const tab_processing_flag tabs_flag, const adjusted_citem citem_init, grs_canvas &src, const draw_box_flag draw_box = draw_box_flag::menu_background) : |
| 354 |
newmenu_layout(title, subtitle, filename, src, tiny_mode, tabs_flag, citem_init, draw_box), window(src, x, y, w, h) |
| 355 |
{ |
| 356 |
} |
| 357 |
std::shared_ptr<int> rval; // Pointer to return value (for polling newmenus) |
| 358 |
virtual window_event_result event_handler(const d_event &) override; |
| 359 |
static int process_until_closed(newmenu *); |
| 360 |
}; |
| 361 |
|
| 362 |
template <typename T1, typename... ConstructionArgs> |
| 363 |
int run_blocking_newmenu(ConstructionArgs &&... args) |
| 364 |
{ |
| 365 |
return T1::process_until_closed(window_create<T1>(std::forward<ConstructionArgs>(args)...)); |
| 366 |
} |
| 367 |
|
| 368 |
struct passive_newmenu : newmenu |
| 369 |
{ |
| 370 |
using newmenu::newmenu; |
| 371 |
}; |
| 372 |
|
| 373 |
struct reorder_newmenu : newmenu |
| 374 |
{ |
| 375 |
using newmenu::newmenu; |
| 376 |
void event_key_command(const d_event &event); |
| 377 |
/* event_handler is not overridden here, because users of |
| 378 |
* reorder_newmenu will need to handle more than just the key event, |
| 379 |
* so requiring them to have a general handler produces better code. |
| 380 |
*/ |
| 381 |
}; |
| 382 |
|
| 383 |
struct listbox_layout |
| 384 |
{ |
| 385 |
struct marquee |
| 386 |
{ |
| 387 |
using ptr = std::unique_ptr<marquee>; |
| 388 |
static ptr allocate(const unsigned maxchars) |
| 389 |
{ |
| 390 |
return std::unique_ptr<marquee>(new(maxchars) marquee{maxchars}); |
| 391 |
} |
| 392 |
void *operator new(std::size_t /* sizeof(marquee) */) = delete; |
| 393 |
static void *operator new(std::size_t /* sizeof(marquee) */, const unsigned maxchars) |
| 394 |
{ |
| 395 |
return ::operator new(sizeof(marquee) + maxchars); |
| 396 |
} |
| 397 |
/* std::default_delete uses regular `delete` */ |
| 398 |
static void operator delete(void *p) |
| 399 |
{ |
| 400 |
::operator delete(p); |
| 401 |
} |
| 402 |
/* recovery from a constructor exception calls placement `delete` */ |
| 403 |
static void operator delete(void *p, unsigned /* maxchars */) |
| 404 |
{ |
| 405 |
::operator delete(p); |
| 406 |
} |
| 407 |
marquee(const unsigned mc) : |
| 408 |
maxchars{mc} |
| 409 |
{ |
| 410 |
} |
| 411 |
fix64 lasttime; // to scroll text if string does not fit in box |
| 412 |
const unsigned maxchars; |
| 413 |
int pos{0}, scrollback = 0; |
| 414 |
char text[]; /* flexible array must be last */ |
| 415 |
}; |
| 416 |
listbox_layout(int citem, unsigned nitems, const char **item, menu_title title, grs_canvas &parent_canvas) : |
| 417 |
citem(citem), nitems(nitems), item(item), title(title), |
| 418 |
parent_canvas(parent_canvas) |
| 419 |
{ |
| 420 |
create_structure(); |
| 421 |
} |
| 422 |
void create_structure(); |
| 423 |
unsigned items_on_screen; |
| 424 |
uint16_t box_x, box_y; |
| 425 |
int box_w, height, title_height; |
| 426 |
int citem, first_item; |
| 427 |
unsigned nitems; |
| 428 |
const char **const item; |
| 429 |
const menu_title title; |
| 430 |
marquee::ptr marquee; |
| 431 |
grs_canvas &parent_canvas; |
| 432 |
short swidth, sheight; |
| 433 |
// with these we check if resolution or fonts have changed so listbox structure can be recreated |
| 434 |
font_x_scale_proportion fntscalex; |
| 435 |
font_y_scale_proportion fntscaley; |
| 436 |
}; |
| 437 |
|
| 438 |
template <typename T> |
| 439 |
using newmenu_subfunction_t = int(*)(newmenu *menu,const d_event &event, T *userdata); |
| 440 |
using newmenu_subfunction = newmenu_subfunction_t<void>; |
| 441 |
|
| 442 |
class unused_newmenu_userdata_t; |
| 443 |
constexpr newmenu_subfunction_t<const unused_newmenu_userdata_t> unused_newmenu_subfunction = nullptr; |
| 444 |
constexpr const unused_newmenu_userdata_t *unused_newmenu_userdata = nullptr; |
| 445 |
|
| 446 |
//should be called whenever the palette changes |
| 447 |
void newmenu_free_background(); |
| 448 |
|
| 449 |
int newmenu_do2(menu_title title, menu_subtitle subtitle, std::ranges::subrange<newmenu_item *> items, newmenu_subfunction subfunction, void *userdata, int citem, menu_filename filename); |
| 450 |
|
| 451 |
// Pass an array of newmenu_items and it processes the menu. It will |
| 452 |
// return a -1 if Esc is pressed, otherwise, it returns the index of |
| 453 |
// the item that was current when Enter was was selected. |
| 454 |
// The subfunction function accepts standard events, plus additional |
| 455 |
// NEWMENU events in future. Just pass NULL if you don't want this, |
| 456 |
// or return 0 where you don't want to override the default behaviour. |
| 457 |
// Title draws big, Subtitle draw medium sized. You can pass NULL for |
| 458 |
// either/both of these if you don't want them. |
| 459 |
// Same as above, only you can pass through what background bitmap to use. |
| 460 |
template <typename T> |
| 461 |
int newmenu_do2(const menu_title title, const menu_subtitle subtitle, std::ranges::subrange<newmenu_item *> items, const newmenu_subfunction_t<T> subfunction, T *const userdata, const int citem = 0, const menu_filename filename = {}) |
| 462 |
{ |
| 463 |
return newmenu_do2(title, subtitle, std::move(items), reinterpret_cast<newmenu_subfunction>(subfunction), static_cast<void *>(userdata), citem, filename); |
| 464 |
} |
| 465 |
|
| 466 |
template <typename T> |
| 467 |
int newmenu_do2(const menu_title title, const menu_subtitle subtitle, std::ranges::subrange<newmenu_item *> items, const newmenu_subfunction_t<const T> subfunction, const T *const userdata, const int citem = 0, const menu_filename filename = {}) |
| 468 |
{ |
| 469 |
return newmenu_do2(title, subtitle, std::move(items), reinterpret_cast<newmenu_subfunction>(subfunction), static_cast<void *>(const_cast<T *>(userdata)), citem, filename); |
| 470 |
} |
| 471 |
|
| 472 |
enum class mission_filter_mode : bool |
| 473 |
{ |
| 474 |
exclude_anarchy, |
| 475 |
include_anarchy, |
| 476 |
}; |
| 477 |
|
| 478 |
unsigned get_border_x(const grs_canvas &canvas); |
| 479 |
unsigned get_border_y(const grs_canvas &canvas); |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
namespace dsx { |
| 484 |
|
| 485 |
//Handles creating and selecting from the mission list. |
| 486 |
//Returns 1 if a mission was loaded. |
| 487 |
void select_mission (mission_filter_mode anarchy_mode, menu_title message, window_event_result (*when_selected)(void)); |
| 488 |
|
| 489 |
} |
| 490 |
|
| 491 |
newmenu_item *newmenu_get_items(newmenu *menu); |
| 492 |
int newmenu_get_citem(newmenu *menu); |
| 493 |
|
| 494 |
// Sample Code: |
| 495 |
/* |
| 496 |
{ |
| 497 |
int mmn; |
| 498 |
newmenu_item mm[8]; |
| 499 |
char xtext[21]; |
| 500 |
|
| 501 |
strcpy( xtext, "John" ); |
| 502 |
|
| 503 |
mm[0].type=NM_TYPE_MENU; mm[0].text="Play game"; |
| 504 |
mm[1].type=NM_TYPE_INPUT; mm[1].text=xtext; mm[1].text_len=20; |
| 505 |
mm[2].type=NM_TYPE_CHECK; mm[2].value=0; mm[2].text="check box"; |
| 506 |
mm[3].type=NM_TYPE_TEXT; mm[3].text="-pickone-"; |
| 507 |
mm[4].type=NM_TYPE_RADIO; mm[4].value=1; mm[4].group=0; mm[4].text="Radio #1"; |
| 508 |
mm[5].type=NM_TYPE_RADIO; mm[5].value=1; mm[5].group=0; mm[5].text="Radio #2"; |
| 509 |
mm[6].type=NM_TYPE_RADIO; mm[6].value=1; mm[6].group=0; mm[6].text="Radio #3"; |
| 510 |
mm[7].type=NM_TYPE_PERCENT; mm[7].value=50; mm[7].text="Volume"; |
| 511 |
|
| 512 |
mmn = newmenu_do("Descent", "Sample Menu", 8, mm, NULL ); |
| 513 |
} |
| 514 |
|
| 515 |
*/ |
| 516 |
|
| 517 |
// This function pops up a messagebox and returns which choice was selected... |
| 518 |
// Example: |
| 519 |
// nm_messagebox( "Title", "Subtitle", 2, "Ok", "Cancel", "There are %d objects", nobjects ); |
| 520 |
// Returns 0 through nchoices-1. |
| 521 |
//int nm_messagebox(const char *title, int nchoices, ...); |
| 522 |
using nm_messagebox_tie = cstring_tie<4>; |
| 523 |
|
| 524 |
int nm_messagebox(menu_title title, const nm_messagebox_tie &tie, const char *format, ...) dxx_compiler_attribute_format_printf(3, 4); |
| 525 |
|
| 526 |
void nm_draw_background(grs_canvas &, int x1, int y1, int x2, int y2); |
| 527 |
|
| 528 |
// Example listbox callback function... |
| 529 |
// int lb_callback( int * citem, int *nitems, char * items[], int *keypress ) |
| 530 |
// { |
| 531 |
// int i; |
| 532 |
// |
| 533 |
// if ( *keypress = KEY_CTRLED+KEY_D ) { |
| 534 |
// if ( *nitems > 1 ) { |
| 535 |
// unlink( items[*citem] ); // Delete the file |
| 536 |
// for (i=*citem; i<*nitems-1; i++ ) { |
| 537 |
// items[i] = items[i+1]; |
| 538 |
// } |
| 539 |
// *nitems = *nitems - 1; |
| 540 |
// free( items[*nitems] ); |
| 541 |
// items[*nitems] = NULL; |
| 542 |
// return 1; // redraw; |
| 543 |
// } |
| 544 |
// *keypress = 0; |
| 545 |
// } |
| 546 |
// return 0; |
| 547 |
// } |
| 548 |
|
| 549 |
namespace dcx { |
| 550 |
|
| 551 |
int nm_messagebox_str(menu_title title, const nm_messagebox_tie &tie, menu_subtitle str); |
| 552 |
|
| 553 |
struct messagebox_newmenu final : |
| 554 |
std::array<newmenu_item, nm_messagebox_tie::maximum_arity>, |
| 555 |
newmenu |
| 556 |
{ |
| 557 |
messagebox_newmenu(const menu_title title, const menu_subtitle subtitle, const nm_messagebox_tie &tie, grs_canvas &canvas); |
| 558 |
static adjusted_citem create_adjusted_citem(std::array<newmenu_item, nm_messagebox_tie::maximum_arity> &items, const nm_messagebox_tie &tie); |
| 559 |
}; |
| 560 |
|
| 561 |
struct listbox : listbox_layout, window |
| 562 |
{ |
| 563 |
listbox(int citem, unsigned nitems, const char **item, menu_title title, grs_canvas &canvas, uint8_t allow_abort_flag); |
| 564 |
const uint8_t allow_abort_flag; |
| 565 |
uint8_t mouse_state{0}; |
| 566 |
marquee::ptr marquee; |
| 567 |
virtual window_event_result event_handler(const d_event &) override; |
| 568 |
virtual window_event_result callback_handler(const d_event &, window_event_result default_return_value) = 0; |
| 569 |
}; |
| 570 |
|
| 571 |
const char **listbox_get_items(listbox &lb); |
| 572 |
int listbox_get_citem(listbox &lb); |
| 573 |
void listbox_delete_item(listbox &lb, int item); |
| 574 |
} |
| 575 |
|
| 576 |
static inline void nm_set_item_menu(newmenu_item &ni, const char *text) |
| 577 |
{ |
| 578 |
ni.type = nm_type::menu; |
| 579 |
ni.text = const_cast<char *>(text); |
| 580 |
} |
| 581 |
|
| 582 |
dxx_compiler_attribute_nonnull() |
| 583 |
static inline void nm_set_item_input(newmenu_item &ni, unsigned len, char *text, const char *const allowed_chars) |
| 584 |
{ |
| 585 |
ni.type = nm_type::input; |
| 586 |
ni.text = text; |
| 587 |
auto &i = ni.input(); |
| 588 |
i.text_len = len - 1; |
| 589 |
i.allowed_chars = allowed_chars; |
| 590 |
} |
| 591 |
|
| 592 |
template <std::size_t len> |
| 593 |
static inline void nm_set_item_input(newmenu_item &ni, std::array<char, len> &text, const char *const allowed_chars = nullptr) |
| 594 |
{ |
| 595 |
nm_set_item_input(ni, len, text.data(), allowed_chars); |
| 596 |
} |
| 597 |
|
| 598 |
dxx_compiler_attribute_nonnull() |
| 599 |
static inline void nm_set_item_checkbox(newmenu_item &ni, const char *text, unsigned checked) |
| 600 |
{ |
| 601 |
ni.type = nm_type::check; |
| 602 |
ni.text = const_cast<char *>(text); |
| 603 |
ni.value = checked; |
| 604 |
} |
| 605 |
|
| 606 |
dxx_compiler_attribute_nonnull() |
| 607 |
static inline void nm_set_item_text(newmenu_item &ni, const char *text) |
| 608 |
{ |
| 609 |
ni.type = nm_type::text; |
| 610 |
ni.text = const_cast<char *>(text); |
| 611 |
} |
| 612 |
|
| 613 |
dxx_compiler_attribute_nonnull() |
| 614 |
static inline void nm_set_item_radio(newmenu_item &ni, const char *text, unsigned checked, unsigned grp) |
| 615 |
{ |
| 616 |
ni.type = nm_type::radio; |
| 617 |
ni.text = const_cast<char *>(text); |
| 618 |
ni.value = checked; |
| 619 |
auto &radio = ni.radio(); |
| 620 |
radio.group = grp; |
| 621 |
} |
| 622 |
|
| 623 |
dxx_compiler_attribute_nonnull() |
| 624 |
static inline void nm_set_item_number(newmenu_item &ni, const char *text, unsigned now, unsigned low, unsigned high) |
| 625 |
{ |
| 626 |
ni.type = nm_type::number; |
| 627 |
ni.text = const_cast<char *>(text); |
| 628 |
ni.value = now; |
| 629 |
auto &number = ni.number(); |
| 630 |
number.min_value = low; |
| 631 |
number.max_value = high; |
| 632 |
} |
| 633 |
|
| 634 |
dxx_compiler_attribute_nonnull() |
| 635 |
static inline void nm_set_item_slider(newmenu_item &ni, const char *text, unsigned now, const int low, const int high, ntstring<NM_MAX_TEXT_LEN> &saved_text) |
| 636 |
{ |
| 637 |
ni.type = nm_type::slider; |
| 638 |
ni.text = const_cast<char *>(text); |
| 639 |
ni.value = now; |
| 640 |
new(&ni.nm_private.slider) newmenu_item::slider_specific_type({low, high}, saved_text); |
| 641 |
} |
| 642 |
|
| 643 |
struct passive_messagebox : std::array<newmenu_item, 1>, newmenu |
| 644 |
{ |
| 645 |
passive_messagebox(const menu_title title, const menu_subtitle subtitle, const char *const item, grs_canvas &src) : |
| 646 |
std::array<newmenu_item, 1>{{newmenu_item::nm_item_menu{item}}}, |
| 647 |
newmenu(title, subtitle, menu_filename{nullptr}, tiny_mode_flag::normal, tab_processing_flag::ignore, adjusted_citem::create(*this, 0), src) |
| 648 |
{ |
| 649 |
} |
| 650 |
}; |
| 651 |
|
| 652 |
#define NORMAL_CHECK_BOX "\201" |
| 653 |
#define CHECKED_CHECK_BOX "\202" |
| 654 |
|
| 655 |
#define NORMAL_RADIO_BOX "\177" |
| 656 |
#define CHECKED_RADIO_BOX "\200" |
| 657 |
#define CURSOR_STRING "_" |
| 658 |
#define SLIDER_LEFT "\203" // 131 |
| 659 |
#define SLIDER_RIGHT "\204" // 132 |
| 660 |
#define SLIDER_MIDDLE "\205" // 133 |
| 661 |
#define SLIDER_MARKER "\206" // 134 |
| 662 |
|
| 663 |
#define BORDERX get_border_x(grd_curscreen->sc_canvas) |
| 664 |
#define BORDERY get_border_y(grd_curscreen->sc_canvas) |
| 665 |
|
| 666 |
#define DXX_NEWMENU_VARIABLE m |
| 667 |
#define DXX_MENUITEM(VERB, TYPE, ...) DXX_MENUITEM_V_##VERB(TYPE, ## __VA_ARGS__) |
| 668 |
#define DXX_MENUITEM_V_ENUM(TYPE,S,OPT,...) OPT, |
| 669 |
#define DXX_MENUITEM_V_COUNT(TYPE,...) +1 |
| 670 |
#define DXX_MENUITEM_V_DECL(TYPE,S,OPT,...) DXX_MENUITEM_V_DECL_T_##TYPE(S, OPT, ## __VA_ARGS__) |
| 671 |
#define DXX_MENUITEM_V_ADD(TYPE,S,OPT,...) DXX_MENUITEM_V_ADD_T_##TYPE(S, OPT, ## __VA_ARGS__) |
| 672 |
#define DXX_MENUITEM_V_READ(TYPE,S,OPT,...) DXX_MENUITEM_V_READ_T_##TYPE(S, OPT, ## __VA_ARGS__) |
| 673 |
#define DXX_MENUITEM_V_DECL_T_CHECK(S,OPT,V) |
| 674 |
|
| 675 |
#define DXX_MENUITEM_V_DECL_T_FCHECK(S,OPT,V,F) |
| 676 |
#define DXX_MENUITEM_V_DECL_T_RADIO(S,OPT,C,G) |
| 677 |
#define DXX_MENUITEM_V_DECL_T_NUMBER(S,OPT,V,MIN,MAX) |
| 678 |
#define DXX_MENUITEM_V_DECL_T_SLIDER(S,OPT,V,MIN,MAX) \ |
| 679 |
ntstring<NM_MAX_TEXT_LEN> DXX_NEWMENU_VARIABLE ## _ ## OPT ## _saved_text; |
| 680 |
|
| 681 |
#define DXX_MENUITEM_V_DECL_T_SCALE_SLIDER(S,OPT,V,MIN,MAX,SCALE) \ |
| 682 |
DXX_MENUITEM_V_DECL_T_SLIDER(,OPT,,,) |
| 683 |
#define DXX_MENUITEM_V_DECL_T_MENU(S,OPT) \ |
| 684 |
|
| 685 |
#define DXX_MENUITEM_V_DECL_T_TEXT(S,OPT) |
| 686 |
#define DXX_MENUITEM_V_DECL_T_INPUT(S,OPT) \ |
| 687 |
|
| 688 |
#define DXX_MENUITEM_V_ADD_T_CHECK(S,OPT,V) \ |
| 689 |
nm_set_item_checkbox(((DXX_NEWMENU_VARIABLE)[(OPT)]), (S), (V)); |
| 690 |
#define DXX_MENUITEM_V_ADD_T_FCHECK(S,OPT,V,F) DXX_MENUITEM_V_ADD_T_CHECK(S,OPT,(V) & (F)) |
| 691 |
#define DXX_MENUITEM_V_ADD_T_RADIO(S,OPT,C,G) \ |
| 692 |
nm_set_item_radio(((DXX_NEWMENU_VARIABLE)[(OPT)]), (S), (C), (G)); |
| 693 |
#define DXX_MENUITEM_V_ADD_T_NUMBER(S,OPT,V,MIN,MAX) \ |
| 694 |
nm_set_item_number(((DXX_NEWMENU_VARIABLE)[(OPT)]), (S), (V), (MIN), (MAX)); |
| 695 |
#define DXX_MENUITEM_V_ADD_T_SLIDER(S,OPT,V,MIN,MAX) \ |
| 696 |
nm_set_item_slider(((DXX_NEWMENU_VARIABLE)[(OPT)]), (S), (V), (MIN), (MAX), DXX_NEWMENU_VARIABLE ## _ ## OPT ## _saved_text); |
| 697 |
#define DXX_MENUITEM_V_ADD_T_SCALE_SLIDER(S,OPT,V,MIN,MAX,SCALE) \ |
| 698 |
DXX_MENUITEM_V_ADD_T_SLIDER((S),OPT,(V) / (SCALE),(MIN),(MAX)) |
| 699 |
#define DXX_MENUITEM_V_ADD_T_MENU(S,OPT) \ |
| 700 |
nm_set_item_menu(((DXX_NEWMENU_VARIABLE)[(OPT)]), (S)); |
| 701 |
#define DXX_MENUITEM_V_ADD_T_TEXT(S,OPT) \ |
| 702 |
nm_set_item_text(((DXX_NEWMENU_VARIABLE)[(OPT)]), (S)); |
| 703 |
#define DXX_MENUITEM_V_ADD_T_INPUT(S,OPT) \ |
| 704 |
nm_set_item_input(((DXX_NEWMENU_VARIABLE)[(OPT)]),(S)); |
| 705 |
#define DXX_MENUITEM_V_READ_T_CHECK(S,OPT,V) \ |
| 706 |
(V) = (DXX_NEWMENU_VARIABLE)[(OPT)].value; |
| 707 |
#define DXX_MENUITEM_V_READ_T_FCHECK(S,OPT,V,F) \ |
| 708 |
( DXX_BEGIN_COMPOUND_STATEMENT { \ |
| 709 |
auto &dxx_menuitem_read_fcheck_v = (V); \ |
| 710 |
const auto dxx_menuitem_read_fcheck_f = (F); \ |
| 711 |
if ((DXX_NEWMENU_VARIABLE)[(OPT)].value) \ |
| 712 |
dxx_menuitem_read_fcheck_v |= dxx_menuitem_read_fcheck_f; \ |
| 713 |
else \ |
| 714 |
dxx_menuitem_read_fcheck_v &= ~dxx_menuitem_read_fcheck_f; \ |
| 715 |
} DXX_END_COMPOUND_STATEMENT ); |
| 716 |
#define DXX_MENUITEM_V_READ_T_RADIO(S,OPT,C,G) /* handled specially */ |
| 717 |
#define DXX_MENUITEM_V_READ_T_NUMBER(S,OPT,V,MIN,MAX) \ |
| 718 |
(V) = (DXX_NEWMENU_VARIABLE)[(OPT)].value; |
| 719 |
#define DXX_MENUITEM_V_READ_T_SLIDER(S,OPT,V,MIN,MAX) \ |
| 720 |
(V) = (DXX_NEWMENU_VARIABLE)[(OPT)].value; |
| 721 |
#define DXX_MENUITEM_V_READ_T_SCALE_SLIDER(S,OPT,V,MIN,MAX,SCALE) \ |
| 722 |
(V) = (DXX_NEWMENU_VARIABLE)[(OPT)].value * (SCALE); |
| 723 |
#define DXX_MENUITEM_V_READ_T_MENU(S,OPT) /* handled specially */ |
| 724 |
#define DXX_MENUITEM_V_READ_T_TEXT(S,OPT) /* handled specially */ |
| 725 |
#define DXX_MENUITEM_V_READ_T_INPUT(S,OPT) /* handled specially */ |
| 726 |
|
| 727 |
template <typename T, typename B> |
| 728 |
class menu_bit_wrapper_t |
| 729 |
{ |
| 730 |
using M = decltype(std::declval<const T &>() & std::declval<B>()); |
| 731 |
std::tuple<T &, B> m_data; |
| 732 |
enum |
| 733 |
{ |
| 734 |
m_mask = 0, |
| 735 |
m_bit = 1, |
| 736 |
}; |
| 737 |
T &get_mask() |
| 738 |
{ |
| 739 |
return std::get<m_mask>(m_data); |
| 740 |
} |
| 741 |
const T &get_mask() const |
| 742 |
{ |
| 743 |
return std::get<m_mask>(m_data); |
| 744 |
} |
| 745 |
B get_bit() const |
| 746 |
{ |
| 747 |
return std::get<m_bit>(m_data); |
| 748 |
} |
| 749 |
public: |
| 750 |
constexpr menu_bit_wrapper_t(T &t, B bit) : |
| 751 |
m_data(t, bit) |
| 752 |
{ |
| 753 |
} |
| 754 |
constexpr operator M() const |
| 755 |
{ |
| 756 |
return get_mask() & get_bit(); |
| 757 |
} |
| 758 |
menu_bit_wrapper_t &operator=(const bool n) |
| 759 |
{ |
| 760 |
auto &m = get_mask(); |
| 761 |
const auto b = get_bit(); |
| 762 |
if (n) |
| 763 |
m |= b; |
| 764 |
else |
| 765 |
m &= ~b; |
| 766 |
return *this; |
| 767 |
} |
| 768 |
}; |
| 769 |
|
| 770 |
template <typename T, typename B> |
| 771 |
static constexpr menu_bit_wrapper_t<T, B> menu_bit_wrapper(T &t, B b) |
| 772 |
{ |
| 773 |
return {t, b}; |
| 774 |
} |
| 775 |
|
| 776 |
template <unsigned B, typename T> |
| 777 |
class menu_number_bias_wrapper_t |
| 778 |
{ |
| 779 |
std::tuple<T &, std::integral_constant<unsigned, B>> m_data; |
| 780 |
#define m_value std::get<0>(m_data) |
| 781 |
#define m_bias std::get<1>(m_data) |
| 782 |
public: |
| 783 |
constexpr menu_number_bias_wrapper_t(T &t) : |
| 784 |
m_data(t, {}) |
| 785 |
{ |
| 786 |
} |
| 787 |
constexpr operator T() const |
| 788 |
{ |
| 789 |
return m_value + m_bias; |
| 790 |
} |
| 791 |
menu_number_bias_wrapper_t &operator=(const T n) |
| 792 |
{ |
| 793 |
m_value = n - m_bias; |
| 794 |
return *this; |
| 795 |
} |
| 796 |
#undef m_bias |
| 797 |
#undef m_value |
| 798 |
}; |
| 799 |
|
| 800 |
template <unsigned B, typename T> |
| 801 |
static constexpr menu_number_bias_wrapper_t<B, T> menu_number_bias_wrapper(T &t) |
| 802 |
{ |
| 803 |
return t; |
| 804 |
} |
| 805 |
#endif |