1 module gui_root; 2 3 import re; 4 import re.gfx; 5 import re.math; 6 import re.ecs; 7 import re.ng.diag; 8 import re.util.interop; 9 10 import raylib; 11 import raylib_nuklear; 12 13 import std.array; 14 import std.conv; 15 import std.string; 16 17 // font size 18 enum UI_FS = 16; 19 enum UI_PAD = 4; 20 21 class GuiRoot : Component, Renderable2D, Updatable { 22 mixin Reflect; 23 24 @property public Rectangle bounds() { 25 return Rectangle( 26 transform.position2.x, transform.position2.y, entity.scene.resolution.x, entity 27 .scene.resolution.y); 28 } 29 30 nk_context* ctx; 31 nk_colorf bg; 32 33 override void setup() { 34 bg = ColorToNuklearF(Colors.SKYBLUE); 35 auto ui_font = raylib.LoadFontEx("./res/SourceSansPro-Regular.ttf", UI_FS, null, 0); 36 ctx = InitNuklearEx(ui_font, UI_FS); 37 ctx.backend_render_scale = cast(int) (Core.window.scale_dpi); 38 39 // nk_color[nk_style_colors.NK_COLOR_COUNT] table; 40 // table[nk_style_colors.NK_COLOR_TEXT] = nk_rgba(190, 190, 190, 255); 41 // table[nk_style_colors.NK_COLOR_WINDOW] = nk_rgba(30, 33, 40, 215); 42 // table[nk_style_colors.NK_COLOR_HEADER] = nk_rgba(181, 45, 69, 220); 43 // table[nk_style_colors.NK_COLOR_BORDER] = nk_rgba(51, 55, 67, 255); 44 // table[nk_style_colors.NK_COLOR_BUTTON] = nk_rgba(181, 45, 69, 255); 45 // table[nk_style_colors.NK_COLOR_BUTTON_HOVER] = nk_rgba(190, 50, 70, 255); 46 // table[nk_style_colors.NK_COLOR_BUTTON_ACTIVE] = nk_rgba(195, 55, 75, 255); 47 // table[nk_style_colors.NK_COLOR_TOGGLE] = nk_rgba(51, 55, 67, 255); 48 // table[nk_style_colors.NK_COLOR_TOGGLE_HOVER] = nk_rgba(45, 60, 60, 255); 49 // table[nk_style_colors.NK_COLOR_TOGGLE_CURSOR] = nk_rgba(181, 45, 69, 255); 50 // table[nk_style_colors.NK_COLOR_SELECT] = nk_rgba(51, 55, 67, 255); 51 // table[nk_style_colors.NK_COLOR_SELECT_ACTIVE] = nk_rgba(181, 45, 69, 255); 52 // table[nk_style_colors.NK_COLOR_SLIDER] = nk_rgba(51, 55, 67, 255); 53 // table[nk_style_colors.NK_COLOR_SLIDER_CURSOR] = nk_rgba(181, 45, 69, 255); 54 // table[nk_style_colors.NK_COLOR_SLIDER_CURSOR_HOVER] = nk_rgba(186, 50, 74, 255); 55 // table[nk_style_colors.NK_COLOR_SLIDER_CURSOR_ACTIVE] = nk_rgba(191, 55, 79, 255); 56 // table[nk_style_colors.NK_COLOR_PROPERTY] = nk_rgba(51, 55, 67, 255); 57 // table[nk_style_colors.NK_COLOR_EDIT] = nk_rgba(51, 55, 67, 225); 58 // table[nk_style_colors.NK_COLOR_EDIT_CURSOR] = nk_rgba(190, 190, 190, 255); 59 // table[nk_style_colors.NK_COLOR_COMBO] = nk_rgba(51, 55, 67, 255); 60 // table[nk_style_colors.NK_COLOR_CHART] = nk_rgba(51, 55, 67, 255); 61 // table[nk_style_colors.NK_COLOR_CHART_COLOR] = nk_rgba(170, 40, 60, 255); 62 // table[nk_style_colors.NK_COLOR_CHART_COLOR_HIGHLIGHT] = nk_rgba(255, 0, 0, 255); 63 // table[nk_style_colors.NK_COLOR_SCROLLBAR] = nk_rgba(30, 33, 40, 255); 64 // table[nk_style_colors.NK_COLOR_SCROLLBAR_CURSOR] = nk_rgba(64, 84, 95, 255); 65 // table[nk_style_colors.NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(70, 90, 100, 255); 66 // table[nk_style_colors.NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(75, 95, 105, 255); 67 // table[nk_style_colors.NK_COLOR_TAB_HEADER] = nk_rgba(181, 45, 69, 220); 68 // nk_style_from_table(ctx, cast(nk_color*) table); 69 70 status("ready."); 71 } 72 73 @property string status(string val) { 74 // log status 75 Core.log.info(format("status: %s", val)); 76 return status_text = val; 77 } 78 79 private string status_text = ""; 80 int active_tab = 0; 81 bool tab_picker_open = false; 82 83 void update() { 84 // keyboard shortcuts 85 if (Input.is_key_down(Keys.KEY_LEFT_CONTROL) && Input.is_key_pressed(Keys.KEY_TAB)) { 86 // advance tab 87 // active_tab = cast(int)((active_tab + 1) % tab_mds.length); 88 } 89 } 90 91 void render() { 92 auto ui_bounds = bounds; 93 94 UpdateNuklear(ctx); 95 96 // GUI 97 if (nk_begin(ctx, "Demo", RectangleToNuklearScaled(ctx, ui_bounds), 98 nk_panel_flags.NK_WINDOW_BORDER | nk_panel_flags.NK_WINDOW_TITLE)) { 99 enum EASY = 0; 100 enum HARD = 1; 101 static int op = EASY; 102 static int property = 20; 103 nk_layout_row_static(ctx, 30, 80, 1); 104 if (nk_button_label(ctx, "button")) 105 TraceLog(TraceLogLevel.LOG_INFO, "button pressed"); 106 107 nk_layout_row_dynamic(ctx, 30, 2); 108 if (nk_option_label(ctx, "easy", op == EASY)) 109 op = EASY; 110 if (nk_option_label(ctx, "hard", op == HARD)) 111 op = HARD; 112 113 nk_layout_row_dynamic(ctx, 25, 1); 114 nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); 115 116 nk_layout_row_dynamic(ctx, 20, 1); 117 nk_label(ctx, "background:", nk_text_alignment.NK_TEXT_LEFT); 118 nk_layout_row_dynamic(ctx, 25, 1); 119 if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx), 400))) { 120 nk_layout_row_dynamic(ctx, 120, 1); 121 bg = nk_color_picker(ctx, bg, nk_color_format.NK_RGBA); 122 nk_layout_row_dynamic(ctx, 25, 1); 123 bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f, 0.005f); 124 bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f, 0.005f); 125 bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f, 0.005f); 126 bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f, 0.005f); 127 nk_combo_end(ctx); 128 } 129 } 130 131 nk_end(ctx); 132 133 DrawNuklear(ctx); 134 } 135 136 void debug_render() { 137 raylib.DrawRectangleLinesEx(bounds, 1, Colors.RED); 138 } 139 }