1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
// This is the minimum required to get the RenderDoc demo UI to work
// The keyboard handling for delete is not perfect
/*
* ==============================================================
*
* API
*
* ===============================================================
*/
#ifndef NK_APPKIT_H_
#define NK_APPKIT_H_
struct AppkitFont;
struct nk_appkit_window;
NK_API struct nk_context *nk_appkit_create(nk_appkit_window *win);
NK_API void nk_appkit_init(AppkitFont *font);
NK_API void nk_appkit_shutdown(void);
NK_API void nk_appkit_new_frame(void);
NK_API void nk_appkit_render(struct nk_color clear);
NK_API AppkitFont *nk_appkit_create_font(const char *name, int size);
NK_API void nk_appkit_delete_font(AppkitFont *font);
#endif // #ifndef NK_APPKIT_H_
#if defined(NK_APPKIT_IMPLEMENTATION) || defined(NK_APPKIT_OBJC_IMPLEMENTATION)
#define nk_appkit_release 0
#define nk_appkit_press 1
#define nk_appkit_key_unknown -1
#define nk_appkit_key_0 48
#define nk_appkit_key_1 49
#define nk_appkit_key_2 50
#define nk_appkit_key_3 51
#define nk_appkit_key_4 52
#define nk_appkit_key_5 53
#define nk_appkit_key_6 54
#define nk_appkit_key_7 55
#define nk_appkit_key_8 56
#define nk_appkit_key_9 57
#define nk_appkit_key_A 65
#define nk_appkit_key_B 66
#define nk_appkit_key_C 67
#define nk_appkit_key_D 68
#define nk_appkit_key_E 69
#define nk_appkit_key_F 70
#define nk_appkit_key_G 71
#define nk_appkit_key_H 72
#define nk_appkit_key_I 73
#define nk_appkit_key_J 74
#define nk_appkit_key_K 75
#define nk_appkit_key_L 76
#define nk_appkit_key_M 77
#define nk_appkit_key_N 78
#define nk_appkit_key_O 79
#define nk_appkit_key_P 80
#define nk_appkit_key_Q 81
#define nk_appkit_key_R 82
#define nk_appkit_key_S 83
#define nk_appkit_key_T 84
#define nk_appkit_key_U 85
#define nk_appkit_key_V 86
#define nk_appkit_key_W 87
#define nk_appkit_key_X 88
#define nk_appkit_key_Y 89
#define nk_appkit_key_Z 90
#define nk_appkit_key_enter 301
#define nk_appkit_key_tab 302
#define nk_appkit_key_backspace 303
#define nk_appkit_key_delete 305
#define nk_appkit_key_right 306
#define nk_appkit_key_left 307
#define nk_appkit_key_down 308
#define nk_appkit_key_up 309
#define nk_appkit_key_page_up 310
#define nk_appkit_key_page_down 311
#define nk_appkit_key_home 312
#define nk_appkit_key_end 313
#define nk_appkit_key_left_shift 400
#define nk_appkit_key_left_control 401
#define nk_appkit_key_right_shift 402
#define nk_appkit_key_right_control 403
#define nk_appkit_key_first nk_appkit_key_0
#define nk_appkit_key_last nk_appkit_key_right_control
#define nk_appkit_mouse_button_left 0
#define nk_appkit_mouse_button_right 1
#define nk_appkit_mouse_button_middle 2
struct nk_appkit_window;
typedef void (*nk_appkit_mouse_button_cb)(nk_appkit_window *, int, int, int);
typedef void (*nk_appkit_character_cb)(nk_appkit_window *, unsigned int);
typedef void (*nk_appkit_key_cb)(nk_appkit_window *, int, int);
typedef void (*nk_appkit_scroll_cb)(nk_appkit_window *, double, double);
#endif // #if defined(NK_APPKIT_IMPLEMENTATION) || defined(NK_APPKIT_OBJC_IMPLEMENTATION)
/*
* ==============================================================
*
* IMPLEMENTATION
*
* ===============================================================
*/
#ifdef NK_APPKIT_IMPLEMENTATION
#include <mach/mach_time.h>
#ifndef NK_APPKIT_TEXT_MAX
#define NK_APPKIT_TEXT_MAX 256
#endif
#ifndef NK_APPKIT_DOUBLE_CLICK_LO
#define NK_APPKIT_DOUBLE_CLICK_LO 0.02
#endif
#ifndef NK_APPKIT_DOUBLE_CLICK_HI
#define NK_APPKIT_DOUBLE_CLICK_HI 0.2
#endif
struct AppkitFont
{
struct nk_user_font nk;
float height;
};
static struct nk_appkit
{
nk_appkit_window *win;
struct nk_context ctx;
unsigned int text[NK_APPKIT_TEXT_MAX];
int text_len;
struct nk_vec2 scroll;
double last_button_click;
int is_double_click_down;
struct nk_vec2 double_click_pos;
uint64_t timerFrequency;
} nk_appkit;
bool nk_appkit_core_initialize(void);
void nk_appkit_core_shutdown(void);
nk_appkit_window *nk_appkit_window_create(int width, int height, const char *title);
void nk_appkit_window_delete(nk_appkit_window *window);
bool nk_appkit_window_is_closed(nk_appkit_window *window);
void nk_appkit_window_get_mouse_position(nk_appkit_window *window, double *xpos, double *ypos);
int nk_appkit_window_get_mouse_button_state(nk_appkit_window *window, int button);
int nk_appkit_window_get_key_state(nk_appkit_window *window, int key);
void nk_appkit_window_set_mouse_button_callback(nk_appkit_window *window,
nk_appkit_mouse_button_cb callback);
void nk_appkit_window_set_scroll_callback(nk_appkit_window *window, nk_appkit_scroll_cb callback);
void nk_appkit_window_set_character_callback(nk_appkit_window *window,
nk_appkit_character_cb callback);
void nk_appkit_window_set_key_callback(nk_appkit_window *window, nk_appkit_key_cb callback);
void nk_appkit_drawing_begin(nk_appkit_window *window, u_int8_t r, u_int8_t g, u_int8_t b,
u_int8_t a);
void nk_appkit_drawing_end(nk_appkit_window *window);
void nk_appkit_drawing_filled_rect(nk_appkit_window *window, short x, short y, u_int16_t w,
u_int16_t h, u_int8_t r, u_int8_t g, u_int8_t b, u_int8_t a,
int rounding);
void nk_appkit_drawing_rect(nk_appkit_window *window, short x, short y, u_int16_t w, u_int16_t h,
u_int8_t r, u_int8_t g, u_int8_t b, u_int8_t a, int rounding,
int lineThickness);
void nk_appkit_drawing_scissor(nk_appkit_window *window, short x, short y, u_int16_t w, u_int16_t h);
void nk_appkit_drawing_text(nk_appkit_window *window, short x, short y, u_int16_t w, u_int16_t h,
const char *text, int len, void *font, u_int8_t bgR, u_int8_t bgG,
u_int8_t bgB, u_int8_t bgA, u_int8_t fgR, u_int8_t fgG, u_int8_t fgB,
u_int8_t fgA);
float nk_appkit_drawing_set_font(nk_appkit_window *window, const char *name, float size);
float nk_appkit_drawing_get_text_width(nk_appkit_window *window, const char *text, int len);
static float nk_appkit_font_get_text_width(nk_handle handle, float height, const char *text, int len)
{
AppkitFont *font = (AppkitFont *)handle.ptr;
if(!font || !text)
return 0.0f;
return nk_appkit_drawing_get_text_width(nk_appkit.win, text, len);
}
static void nk_appkit_char_callback(nk_appkit_window *window, unsigned int codepoint)
{
if(nk_appkit.text_len < NK_APPKIT_TEXT_MAX)
nk_appkit.text[nk_appkit.text_len++] = codepoint;
}
static void nk_appkit_scroll_callback(nk_appkit_window *window, double xoff, double yoff)
{
nk_appkit.scroll.x += (float)xoff;
nk_appkit.scroll.y += (float)yoff;
}
static void nk_appkit_mouse_button_callback(nk_appkit_window *window, int button, int action, int mods)
{
double x, y;
if(button != nk_appkit_mouse_button_left)
return;
nk_appkit_window_get_mouse_position(window, &x, &y);
if(action == nk_appkit_press)
{
double now = (double)mach_absolute_time() / nk_appkit.timerFrequency;
double dt = now - nk_appkit.last_button_click;
if(dt > NK_APPKIT_DOUBLE_CLICK_LO && dt < NK_APPKIT_DOUBLE_CLICK_HI)
{
nk_appkit.is_double_click_down = nk_true;
nk_appkit.double_click_pos = nk_vec2((float)x, (float)y);
}
nk_appkit.last_button_click = now;
}
else
nk_appkit.is_double_click_down = nk_false;
}
NK_API struct nk_context *nk_appkit_create(nk_appkit_window *win)
{
nk_appkit.win = win;
nk_appkit_window_set_character_callback(win, nk_appkit_char_callback);
nk_appkit_window_set_scroll_callback(win, nk_appkit_scroll_callback);
nk_appkit_window_set_mouse_button_callback(win, nk_appkit_mouse_button_callback);
mach_timebase_info_data_t info;
mach_timebase_info(&info);
nk_appkit.timerFrequency = (info.denom * 1e9) / info.numer;
nk_appkit.is_double_click_down = nk_false;
nk_appkit.double_click_pos = nk_vec2(0, 0);
nk_appkit.last_button_click = 0;
return &nk_appkit.ctx;
}
NK_API void nk_appkit_init(AppkitFont *font)
{
struct nk_user_font *nkFont = &font->nk;
nkFont->userdata = nk_handle_ptr(font);
nkFont->height = font->height;
nkFont->width = nk_appkit_font_get_text_width;
nk_init_default(&nk_appkit.ctx, nkFont);
nk_appkit.ctx.clip.userdata = nk_handle_ptr(0);
}
NK_API void nk_appkit_shutdown(void)
{
nk_free(&nk_appkit.ctx);
memset(&nk_appkit, 0, sizeof(nk_appkit));
}
NK_API AppkitFont *nk_appkit_create_font(const char *name, int size)
{
AppkitFont *font = (AppkitFont *)calloc(1, sizeof(AppkitFont));
if(!font)
return NULL;
font->height = nk_appkit_drawing_set_font(nk_appkit.win, name, size);
return font;
}
NK_API void nk_appkit_delete_font(AppkitFont *font)
{
if(!font)
return;
free(font);
}
NK_API void nk_appkit_render(struct nk_color clear)
{
const struct nk_command *cmd;
struct nk_context *ctx = &nk_appkit.ctx;
nk_appkit_drawing_begin(nk_appkit.win, clear.r, clear.g, clear.b, clear.a);
nk_foreach(cmd, &nk_appkit.ctx)
{
switch(cmd->type)
{
case NK_COMMAND_NOP: break;
case NK_COMMAND_SCISSOR:
{
const struct nk_command_scissor *s = (const struct nk_command_scissor *)cmd;
nk_appkit_drawing_scissor(nk_appkit.win, s->x, s->y, s->w, s->h);
break;
}
case NK_COMMAND_LINE: assert(false); break;
case NK_COMMAND_CURVE: assert(false); break;
case NK_COMMAND_RECT:
{
const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
nk_appkit_drawing_rect(nk_appkit.win, r->x, r->y, r->w, r->h, r->color.r, r->color.g,
r->color.b, r->color.a, r->rounding, r->line_thickness);
break;
}
case NK_COMMAND_RECT_FILLED:
{
const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
nk_appkit_drawing_filled_rect(nk_appkit.win, r->x, r->y, r->w, r->h, r->color.r, r->color.g,
r->color.b, r->color.a, r->rounding);
break;
}
case NK_COMMAND_RECT_MULTI_COLOR: assert(false); break;
case NK_COMMAND_CIRCLE: assert(false); break;
case NK_COMMAND_CIRCLE_FILLED: assert(false); break;
case NK_COMMAND_ARC: assert(false); break;
case NK_COMMAND_ARC_FILLED: assert(false); break;
case NK_COMMAND_TRIANGLE: assert(false); break;
case NK_COMMAND_TRIANGLE_FILLED: assert(false); break;
case NK_COMMAND_POLYGON: assert(false); break;
case NK_COMMAND_POLYGON_FILLED: assert(false); break;
case NK_COMMAND_POLYLINE: assert(false); break;
case NK_COMMAND_TEXT:
{
const struct nk_command_text *t = (const struct nk_command_text *)cmd;
nk_appkit_drawing_text(nk_appkit.win, t->x, t->y, t->w, t->h, (const char *)t->string,
t->length, (AppkitFont *)t->font->userdata.ptr, t->background.r,
t->background.g, t->background.b, t->background.a, t->foreground.r,
t->foreground.g, t->foreground.b, t->foreground.a);
break;
}
case NK_COMMAND_IMAGE: assert(false); break;
case NK_COMMAND_CUSTOM: assert(false); break;
}
}
nk_appkit_drawing_end(nk_appkit.win);
nk_clear(ctx);
}
NK_API void nk_appkit_new_frame(void)
{
int i;
double x, y;
struct nk_context *ctx = &nk_appkit.ctx;
nk_appkit_window *win = nk_appkit.win;
nk_input_begin(ctx);
for(i = 0; i < nk_appkit.text_len; ++i)
nk_input_unicode(ctx, nk_appkit.text[i]);
nk_input_key(ctx, NK_KEY_DEL,
nk_appkit_window_get_key_state(win, nk_appkit_key_delete) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_ENTER,
nk_appkit_window_get_key_state(win, nk_appkit_key_enter) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TAB,
nk_appkit_window_get_key_state(win, nk_appkit_key_tab) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_BACKSPACE,
nk_appkit_window_get_key_state(win, nk_appkit_key_backspace) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_UP,
nk_appkit_window_get_key_state(win, nk_appkit_key_up) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_DOWN,
nk_appkit_window_get_key_state(win, nk_appkit_key_down) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_START,
nk_appkit_window_get_key_state(win, nk_appkit_key_home) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_END,
nk_appkit_window_get_key_state(win, nk_appkit_key_end) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_SCROLL_START,
nk_appkit_window_get_key_state(win, nk_appkit_key_home) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_SCROLL_END,
nk_appkit_window_get_key_state(win, nk_appkit_key_end) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_SCROLL_DOWN,
nk_appkit_window_get_key_state(win, nk_appkit_key_page_down) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_SCROLL_UP,
nk_appkit_window_get_key_state(win, nk_appkit_key_page_up) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_SHIFT,
nk_appkit_window_get_key_state(win, nk_appkit_key_left_shift) == nk_appkit_press ||
nk_appkit_window_get_key_state(win, nk_appkit_key_right_shift) == nk_appkit_press);
if(nk_appkit_window_get_key_state(win, nk_appkit_key_left_control) == nk_appkit_press ||
nk_appkit_window_get_key_state(win, nk_appkit_key_right_control) == nk_appkit_press)
{
nk_input_key(ctx, NK_KEY_COPY,
nk_appkit_window_get_key_state(win, nk_appkit_key_C) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_PASTE,
nk_appkit_window_get_key_state(win, nk_appkit_key_V) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_CUT,
nk_appkit_window_get_key_state(win, nk_appkit_key_X) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_UNDO,
nk_appkit_window_get_key_state(win, nk_appkit_key_Z) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_REDO,
nk_appkit_window_get_key_state(win, nk_appkit_key_R) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT,
nk_appkit_window_get_key_state(win, nk_appkit_key_left) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT,
nk_appkit_window_get_key_state(win, nk_appkit_key_right) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_LINE_START,
nk_appkit_window_get_key_state(win, nk_appkit_key_B) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_TEXT_LINE_END,
nk_appkit_window_get_key_state(win, nk_appkit_key_E) == nk_appkit_press);
}
else
{
nk_input_key(ctx, NK_KEY_LEFT,
nk_appkit_window_get_key_state(win, nk_appkit_key_left) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_RIGHT,
nk_appkit_window_get_key_state(win, nk_appkit_key_right) == nk_appkit_press);
nk_input_key(ctx, NK_KEY_COPY, 0);
nk_input_key(ctx, NK_KEY_PASTE, 0);
nk_input_key(ctx, NK_KEY_CUT, 0);
nk_input_key(ctx, NK_KEY_SHIFT, 0);
}
nk_appkit_window_get_mouse_position(win, &x, &y);
nk_input_motion(ctx, (int)x, (int)y);
nk_input_button(
ctx, NK_BUTTON_LEFT, (int)x, (int)y,
nk_appkit_window_get_mouse_button_state(win, nk_appkit_mouse_button_left) == nk_appkit_press);
nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y,
nk_appkit_window_get_mouse_button_state(win, nk_appkit_mouse_button_middle) ==
nk_appkit_press);
nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y,
nk_appkit_window_get_mouse_button_state(win, nk_appkit_mouse_button_right) ==
nk_appkit_press);
nk_input_button(ctx, NK_BUTTON_DOUBLE, (int)nk_appkit.double_click_pos.x,
(int)nk_appkit.double_click_pos.y, nk_appkit.is_double_click_down);
nk_input_scroll(ctx, nk_appkit.scroll);
nk_input_end(&nk_appkit.ctx);
nk_appkit.text_len = 0;
nk_appkit.scroll = nk_vec2(0, 0);
}
#endif // #ifdef NK_APPKIT_IMPLEMENTATION
#ifdef NK_APPKIT_OBJC_IMPLEMENTATION
#import <AppKit/AppKit.h>
typedef struct nk_appkit_window
{
NSWindow *nsWindow;
id delegate;
NSView *nsView;
NSImage *nsImage;
bool shouldClose;
char mouseButtons[3];
char keys[nk_appkit_key_last + 1];
nk_appkit_character_cb characterCallback;
nk_appkit_key_cb keyCallback;
nk_appkit_mouse_button_cb mouseButtonCallback;
nk_appkit_scroll_cb scrollCallback;
} nk_appkit_window;
typedef struct nk_appkit_state
{
short int keycodes[256];
nk_appkit_window *window;
NSFont *nsFont;
float fontHeight;
CGEventSourceRef eventSource;
id nsAppDelegate;
id keyUpMonitor;
} nk_appkit_state;
static nk_appkit_state s_state = {0};
static void CreateKeyTables(void)
{
memset(s_state.keycodes, -1, sizeof(s_state.keycodes));
s_state.keycodes[0x1D] = nk_appkit_key_0;
s_state.keycodes[0x12] = nk_appkit_key_1;
s_state.keycodes[0x13] = nk_appkit_key_2;
s_state.keycodes[0x14] = nk_appkit_key_3;
s_state.keycodes[0x15] = nk_appkit_key_4;
s_state.keycodes[0x17] = nk_appkit_key_5;
s_state.keycodes[0x16] = nk_appkit_key_6;
s_state.keycodes[0x1A] = nk_appkit_key_7;
s_state.keycodes[0x1C] = nk_appkit_key_8;
s_state.keycodes[0x19] = nk_appkit_key_9;
s_state.keycodes[0x00] = nk_appkit_key_A;
s_state.keycodes[0x0B] = nk_appkit_key_B;
s_state.keycodes[0x08] = nk_appkit_key_C;
s_state.keycodes[0x02] = nk_appkit_key_D;
s_state.keycodes[0x0E] = nk_appkit_key_E;
s_state.keycodes[0x03] = nk_appkit_key_F;
s_state.keycodes[0x05] = nk_appkit_key_G;
s_state.keycodes[0x04] = nk_appkit_key_H;
s_state.keycodes[0x22] = nk_appkit_key_I;
s_state.keycodes[0x26] = nk_appkit_key_J;
s_state.keycodes[0x28] = nk_appkit_key_K;
s_state.keycodes[0x25] = nk_appkit_key_L;
s_state.keycodes[0x2E] = nk_appkit_key_M;
s_state.keycodes[0x2D] = nk_appkit_key_N;
s_state.keycodes[0x1F] = nk_appkit_key_O;
s_state.keycodes[0x23] = nk_appkit_key_P;
s_state.keycodes[0x0C] = nk_appkit_key_Q;
s_state.keycodes[0x0F] = nk_appkit_key_R;
s_state.keycodes[0x01] = nk_appkit_key_S;
s_state.keycodes[0x11] = nk_appkit_key_T;
s_state.keycodes[0x20] = nk_appkit_key_U;
s_state.keycodes[0x09] = nk_appkit_key_V;
s_state.keycodes[0x0D] = nk_appkit_key_W;
s_state.keycodes[0x07] = nk_appkit_key_X;
s_state.keycodes[0x10] = nk_appkit_key_Y;
s_state.keycodes[0x06] = nk_appkit_key_Z;
s_state.keycodes[0x33] = nk_appkit_key_backspace;
s_state.keycodes[0x75] = nk_appkit_key_delete;
s_state.keycodes[0x7D] = nk_appkit_key_down;
s_state.keycodes[0x77] = nk_appkit_key_end;
s_state.keycodes[0x24] = nk_appkit_key_enter;
s_state.keycodes[0x73] = nk_appkit_key_home;
s_state.keycodes[0x7B] = nk_appkit_key_left;
s_state.keycodes[0x79] = nk_appkit_key_page_down;
s_state.keycodes[0x74] = nk_appkit_key_page_up;
s_state.keycodes[0x7C] = nk_appkit_key_right;
s_state.keycodes[0x30] = nk_appkit_key_tab;
s_state.keycodes[0x7E] = nk_appkit_key_up;
s_state.keycodes[0x33] = nk_appkit_key_backspace;
s_state.keycodes[0x75] = nk_appkit_key_delete;
s_state.keycodes[0x7D] = nk_appkit_key_down;
s_state.keycodes[0x77] = nk_appkit_key_end;
s_state.keycodes[0x24] = nk_appkit_key_enter;
s_state.keycodes[0x73] = nk_appkit_key_home;
s_state.keycodes[0x7B] = nk_appkit_key_left;
s_state.keycodes[0x3B] = nk_appkit_key_left_control;
s_state.keycodes[0x38] = nk_appkit_key_left_shift;
s_state.keycodes[0x79] = nk_appkit_key_page_down;
s_state.keycodes[0x74] = nk_appkit_key_page_up;
s_state.keycodes[0x7C] = nk_appkit_key_right;
s_state.keycodes[0x3E] = nk_appkit_key_right_control;
s_state.keycodes[0x3C] = nk_appkit_key_right_shift;
s_state.keycodes[0x30] = nk_appkit_key_tab;
s_state.keycodes[0x7E] = nk_appkit_key_up;
}
static int TranslateKey(unsigned int key)
{
if(key >= sizeof(s_state.keycodes) / sizeof(s_state.keycodes[0]))
return nk_appkit_key_unknown;
return s_state.keycodes[key];
}
static void InputMouseClick(nk_appkit_window *window, int button, int action)
{
if(button < 0 || button > 2)
return;
window->mouseButtons[button] = (char)action;
if(window->mouseButtonCallback)
window->mouseButtonCallback(window, button, action, 0);
}
static void InputKey(nk_appkit_window *window, int key, int action)
{
if(key >= 0 && key <= nk_appkit_key_last)
{
if(action == nk_appkit_release && window->keys[key] == nk_appkit_release)
return;
window->keys[key] = (char)action;
}
}
@interface nk_appkit_windowDelegate : NSObject
{
nk_appkit_window *window;
}
- (instancetype)init_with_window:(nk_appkit_window *)initWindow;
@end
@implementation nk_appkit_windowDelegate
- (instancetype)init_with_window:(nk_appkit_window *)initWindow
{
self = [super init];
window = initWindow;
return self;
}
- (BOOL)windowShouldClose:(id)sender
{
window->shouldClose = true;
return NO;
}
@end
@interface nk_appkit_windowView : NSView<NSTextInputClient>
{
nk_appkit_window *window;
}
- (instancetype)init_with_window:(nk_appkit_window *)initWindow;
@end
@implementation nk_appkit_windowView
- (instancetype)init_with_window:(nk_appkit_window *)initWindow
{
self = [super init];
window = initWindow;
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[window->nsImage drawInRect:dirtyRect];
}
- (void)mouseDown:(NSEvent *)event
{
InputMouseClick(window, nk_appkit_mouse_button_left, nk_appkit_press);
}
- (void)mouseUp:(NSEvent *)event
{
InputMouseClick(window, nk_appkit_mouse_button_left, nk_appkit_release);
}
- (void)rightMouseDown:(NSEvent *)event
{
InputMouseClick(window, nk_appkit_mouse_button_right, nk_appkit_press);
}
- (void)rightMouseUp:(NSEvent *)event
{
InputMouseClick(window, nk_appkit_mouse_button_right, nk_appkit_release);
}
- (void)otherMouseDown:(NSEvent *)event
{
InputMouseClick(window, (int)[event buttonNumber], nk_appkit_press);
}
- (void)otherMouseUp:(NSEvent *)event
{
InputMouseClick(window, (int)[event buttonNumber], nk_appkit_release);
}
- (void)keyDown:(NSEvent *)event
{
const int key = TranslateKey([event keyCode]);
InputKey(window, key, nk_appkit_press);
[self interpretKeyEvents:@[ event ]];
if(window->keyCallback)
window->keyCallback(window, key, nk_appkit_press);
}
- (void)flagsChanged:(NSEvent *)event
{
int action;
const unsigned int modifierFlags =
[event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
const int key = TranslateKey([event keyCode]);
NSUInteger keyFlag = 0;
switch(key)
{
case nk_appkit_key_left_shift:
case nk_appkit_key_right_shift: keyFlag = NSEventModifierFlagShift; break;
case nk_appkit_key_left_control:
case nk_appkit_key_right_control: keyFlag = NSEventModifierFlagControl; break;
}
if(keyFlag & modifierFlags)
{
if(window->keys[key] == nk_appkit_press)
action = nk_appkit_release;
else
action = nk_appkit_press;
}
else
action = nk_appkit_release;
InputKey(window, key, action);
}
- (void)keyUp:(NSEvent *)event
{
const int key = TranslateKey([event keyCode]);
InputKey(window, key, nk_appkit_release);
if(window->keyCallback)
window->keyCallback(window, key, nk_appkit_release);
}
- (void)scrollWheel:(NSEvent *)event
{
double deltaX = [event scrollingDeltaX];
double deltaY = [event scrollingDeltaY];
if([event hasPreciseScrollingDeltas])
{
deltaX *= 0.1;
deltaY *= 0.1;
}
if(fabs(deltaX) > 0.0 || fabs(deltaY) > 0.0)
{
if(window->scrollCallback)
window->scrollCallback(window, deltaX, deltaY);
}
}
static const NSRange s_NSRange_Empty = {NSNotFound, 0};
- (BOOL)hasMarkedText
{
return NO;
}
- (NSRange)markedRange
{
return s_NSRange_Empty;
}
- (NSRange)selectedRange
{
return s_NSRange_Empty;
}
- (void)setMarkedText:(id)string
selectedRange:(NSRange)selectedRange
replacementRange:(NSRange)replacementRange
{
}
- (void)unmarkText
{
}
- (NSArray *)validAttributesForMarkedText
{
return [NSArray array];
}
- (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)range
actualRange:(NSRangePointer)actualRange
{
return nil;
}
- (NSUInteger)characterIndexForPoint:(NSPoint)point
{
return 0;
}
- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange
{
const NSRect frame = [window->nsView frame];
return NSMakeRect(frame.origin.x, frame.origin.y, 0.0, 0.0);
}
- (void)insertText:(id)string replacementRange:(NSRange)replacementRange
{
if(!window->characterCallback)
return;
NSString *characters;
NSEvent *event = [NSApp currentEvent];
if([string isKindOfClass:[NSAttributedString class]])
characters = [string string];
else
characters = (NSString *)string;
NSRange range = NSMakeRange(0, [characters length]);
while(range.length)
{
uint32_t codepoint = 0;
if([characters getBytes:&codepoint
maxLength:sizeof(codepoint)
usedLength:nil
encoding:NSUTF32StringEncoding
options:0
range:range
remainingRange:&range])
{
if(codepoint >= 0xf700 && codepoint <= 0xf7ff)
continue;
if(window->characterCallback)
window->characterCallback(window, codepoint);
}
}
}
@end
void nk_appkit_window_get_mouse_position(nk_appkit_window *window, double *xpos, double *ypos)
{
@autoreleasepool
{
const NSRect contentRect = [window->nsView frame];
const NSPoint pos = [window->nsWindow mouseLocationOutsideOfEventStream];
if(xpos)
*xpos = pos.x;
if(ypos)
*ypos = contentRect.size.height - pos.y;
}
}
@interface nk_appkit_ApplicationDelegate : NSObject<NSApplicationDelegate>
@end
@implementation nk_appkit_ApplicationDelegate
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
if(s_state.window)
s_state.window->shouldClose = true;
return NSTerminateCancel;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
@autoreleasepool
{
NSEvent *event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
location:NSMakePoint(0, 0)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
subtype:0
data1:0
data2:0];
[NSApp postEvent:event atStart:YES];
[NSApp stop:nil];
}
}
@end // nk_appkit_ApplicationDelegate
bool nk_appkit_window_is_closed(nk_appkit_window *window)
{
return window->shouldClose;
}
void nk_appkit_window_set_character_callback(nk_appkit_window *window, nk_appkit_character_cb callback)
{
window->characterCallback = callback;
}
void nk_appkit_window_set_key_callback(nk_appkit_window *window, nk_appkit_key_cb callback)
{
window->keyCallback = callback;
}
void nk_appkit_window_set_scroll_callback(nk_appkit_window *window, nk_appkit_scroll_cb callback)
{
window->scrollCallback = callback;
}
void nk_appkit_window_set_mouse_button_callback(nk_appkit_window *window,
nk_appkit_mouse_button_cb callback)
{
window->mouseButtonCallback = callback;
}
int nk_appkit_window_get_key_state(nk_appkit_window *window, int key)
{
assert(key >= nk_appkit_key_first && key <= nk_appkit_key_last);
return (int)window->keys[key];
}
int nk_appkit_window_get_mouse_button_state(nk_appkit_window *window, int button)
{
assert(button >= 0 && button <= 2);
return (int)window->mouseButtons[button];
}
void nk_appkit_window_delete(nk_appkit_window *window)
{
assert(window == s_state.window);
window->characterCallback = nil;
window->keyCallback = nil;
window->mouseButtonCallback = nil;
window->scrollCallback = nil;
@autoreleasepool
{
[window->nsWindow orderOut:nil];
[window->nsWindow setDelegate:nil];
[window->delegate release];
window->delegate = nil;
[window->nsImage release];
window->nsImage = nil;
[window->nsView release];
window->nsView = nil;
[window->nsWindow close];
window->nsWindow = nil;
}
free(window);
s_state.window = NULL;
}
void nk_appkit_core_shutdown(void)
{
if(s_state.window)
nk_appkit_window_delete(s_state.window);
@autoreleasepool
{
if(s_state.eventSource)
{
CFRelease(s_state.eventSource);
s_state.eventSource = nil;
}
if(s_state.nsAppDelegate)
{
[NSApp setDelegate:nil];
[s_state.nsAppDelegate release];
s_state.nsAppDelegate = nil;
}
if(s_state.keyUpMonitor)
[NSEvent removeMonitor:s_state.keyUpMonitor];
if(s_state.nsFont)
{
[s_state.nsFont release];
s_state.nsFont = nil;
}
}
memset(&s_state, 0, sizeof(s_state));
}
bool nk_appkit_core_initialize(void)
{
memset(&s_state, 0, sizeof(s_state));
@autoreleasepool
{
[NSApplication sharedApplication];
s_state.nsAppDelegate = [[nk_appkit_ApplicationDelegate alloc] init];
[NSApp setDelegate:s_state.nsAppDelegate];
NSEvent * (^block)(NSEvent *) = ^NSEvent *(NSEvent *event)
{
if([event modifierFlags] & NSEventModifierFlagCommand)
[[NSApp keyWindow] sendEvent:event];
return event;
};
s_state.keyUpMonitor =
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyUp handler:block];
CreateKeyTables();
s_state.eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
if(!s_state.eventSource)
{
nk_appkit_core_shutdown();
return false;
}
CGEventSourceSetLocalEventsSuppressionInterval(s_state.eventSource, 0.0);
if(![[NSRunningApplication currentApplication] isFinishedLaunching])
[NSApp run];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}
return true;
}
nk_appkit_window *nk_appkit_window_create(int width, int height, const char *title)
{
nk_appkit_window *window = (nk_appkit_window *)calloc(1, sizeof(nk_appkit_window));
s_state.window = window;
window->delegate = [[nk_appkit_windowDelegate alloc] init_with_window:window];
window->nsWindow = [[NSWindow alloc]
initWithContentRect:NSMakeRect(0, 0, width, height)
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered
defer:NO];
[(NSWindow *)window->nsWindow center];
window->nsView = [[nk_appkit_windowView alloc] init_with_window:window];
[window->nsWindow setContentView:window->nsView];
[window->nsWindow makeFirstResponder:window->nsView];
[window->nsWindow setTitle:@(title)];
[window->nsWindow setDelegate:window->delegate];
[window->nsWindow setAcceptsMouseMovedEvents:YES];
[window->nsWindow setRestorable:NO];
[window->nsWindow orderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[window->nsWindow makeKeyAndOrderFront:nil];
window->nsImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
return (nk_appkit_window *)window;
}
void nk_appkit_drawing_begin(nk_appkit_window *window, u_int8_t r, u_int8_t g, u_int8_t b, u_int8_t a)
{
[NSGraphicsContext saveGraphicsState];
[window->nsImage lockFocus];
[[NSColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:a / 255.0f] setFill];
NSRectFill(NSMakeRect(0, 0, window->nsImage.size.width, window->nsImage.size.height));
}
void nk_appkit_drawing_end(nk_appkit_window *window)
{
[window->nsImage unlockFocus];
[window->nsView setNeedsDisplay:TRUE];
[NSGraphicsContext restoreGraphicsState];
}
float nk_appkit_drawing_set_font(nk_appkit_window *window, const char *name, float size)
{
@autoreleasepool
{
s_state.nsFont = [NSFont fontWithName:[NSString stringWithUTF8String:name] size:size];
[s_state.nsFont retain];
float height = [[[NSLayoutManager alloc] init] defaultLineHeightForFont:s_state.nsFont];
s_state.fontHeight = height + 1;
return s_state.fontHeight;
}
}
float nk_appkit_drawing_get_text_width(nk_appkit_window *window, const char *text, int len)
{
@autoreleasepool
{
NSString *nsString =
[[NSString alloc] initWithBytes:(void *)text length:len encoding:NSASCIIStringEncoding];
NSMutableDictionary *atts = [[NSMutableDictionary alloc] init];
[atts setObject:s_state.nsFont forKey:NSFontAttributeName];
NSSize bounds = [nsString sizeWithAttributes:atts];
float width = floor(bounds.width);
return width;
}
}
void nk_appkit_drawing_filled_rect(nk_appkit_window *window, short x, short y, u_int16_t w,
u_int16_t h, u_int8_t r, u_int8_t g, u_int8_t b, u_int8_t a,
int rounding)
{
@autoreleasepool
{
[[NSColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:a / 255.0f] setFill];
float H = window->nsImage.size.height;
float y0 = H - y - h;
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(x, y0, w, h)
xRadius:rounding
yRadius:rounding];
[path fill];
}
}
void nk_appkit_drawing_rect(nk_appkit_window *window, short x, short y, u_int16_t w, u_int16_t h,
u_int8_t r, u_int8_t g, u_int8_t b, u_int8_t a, int rounding,
int lineThickness)
{
@autoreleasepool
{
[[NSColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:a / 255.0f] setStroke];
float H = window->nsImage.size.height;
float y0 = H - y - h;
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(x, y0, w, h)
xRadius:rounding
yRadius:rounding];
path.lineWidth = lineThickness;
[path stroke];
}
}
void nk_appkit_drawing_scissor(nk_appkit_window *window, short x, short y, u_int16_t w, u_int16_t h)
{
@autoreleasepool
{
[NSGraphicsContext restoreGraphicsState];
[NSGraphicsContext saveGraphicsState];
float H = window->nsImage.size.height;
float y0 = H - y - h;
NSRectClip(NSMakeRect(x, y0, w, h));
}
}
void nk_appkit_drawing_text(nk_appkit_window *window, short x, short y, u_int16_t w, u_int16_t h,
const char *text, int len, void *font, u_int8_t bgR, u_int8_t bgG,
u_int8_t bgB, u_int8_t bgA, u_int8_t fgR, u_int8_t fgG, u_int8_t fgB,
u_int8_t fgA)
{
@autoreleasepool
{
float H = window->nsImage.size.height;
float y0 = H - y - s_state.fontHeight;
NSString *nsString =
[[NSString alloc] initWithBytes:(void *)text length:len encoding:NSASCIIStringEncoding];
NSMutableDictionary *atts = [[NSMutableDictionary alloc] init];
[atts setObject:s_state.nsFont forKey:NSFontAttributeName];
[atts setObject:[NSColor colorWithRed:bgR / 255.0f
green:bgG / 255.0f
blue:bgB / 255.0f
alpha:bgA / 255.0f]
forKey:NSBackgroundColorAttributeName];
[atts setObject:[NSColor colorWithRed:fgR / 255.0f
green:fgG / 255.0f
blue:fgB / 255.0f
alpha:fgA / 255.0f]
forKey:NSForegroundColorAttributeName];
[nsString drawAtPoint:NSMakePoint(x, y0) withAttributes:atts];
}
}
#endif // #ifdef NK_APPKIT_OBJC_IMPLEMENTATION
|