Created
June 28, 2024 23:30
-
-
Save Ryder17z/402376fa1a509269363182724c1ef922 to your computer and use it in GitHub Desktop.
Nuklear.h - unwanted horizontal scrollbar issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fixes to prevent 'std' class members from being overwritten by windows.h | |
#define WIN32_LEAN_AND_MEAN | |
#define NOSERVICE | |
#define NOMCX | |
#define NOIME | |
#define NOMINMAX | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#include <assert.h> | |
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <cstring> | |
// redirection on windows | |
#if defined(_WIN32) | |
int wmain(int argc, char* argv[]) | |
{ | |
main(argc, argv); | |
} | |
#endif | |
/* macros */ | |
#define MAX_VERTEX_MEMORY 512 * 1024 | |
#define MAX_ELEMENT_MEMORY 128 * 1024 | |
#define UNUSED(a) (void)a | |
#define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
#define MAX(a,b) ((a) < (b) ? (b) : (a)) | |
#define LEN(a) (sizeof(a)/sizeof(a)[0]) | |
#ifdef __APPLE__ | |
#define NK_SHADER_VERSION "#version 150\n" | |
#else | |
#define NK_SHADER_VERSION "#version 300 es\n" | |
#endif | |
#define NK_INCLUDE_FIXED_TYPES | |
#define NK_INCLUDE_STANDARD_IO | |
#define NK_INCLUDE_STANDARD_VARARGS | |
#define NK_INCLUDE_DEFAULT_ALLOCATOR | |
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT | |
#define NK_INCLUDE_FONT_BAKING | |
#define NK_INCLUDE_DEFAULT_FONT | |
#define NK_IMPLEMENTATION | |
#define NK_SDL_RENDERER_IMPLEMENTATION | |
#include <SDL.h> | |
#include "nuklear.h" | |
#include "nuklear_sdl_renderer.h" | |
#define WINDOW_WIDTH 1200 | |
#define WINDOW_HEIGHT 700 | |
#define WINDOW_MIN_WIDTH 960 | |
#define WINDOW_MIN_HEIGHT 700 | |
constexpr float margin_left = 0.015f; | |
constexpr float margin_top = 0.015f; | |
constexpr float margin_right = 0.015f; | |
constexpr float margin_bottom = 0.015f; | |
float side_panel_width = 0.280f; | |
float side_panel_margin = 0.01f; | |
float side_panel_nomargin = 0.0f; | |
float middle_panel_pos; | |
float middle_panel_width; | |
float right_panel_pos; | |
double borders[4]; | |
int prev_window_w = WINDOW_MIN_WIDTH, prev_window_h = WINDOW_MIN_HEIGHT; | |
int ui_panel_height; | |
bool runOnce=true; | |
int maingui(struct nk_context* ctx, SDL_Window* win, SDL_Renderer* renderer) { | |
if(runOnce) | |
{ | |
runOnce=false; | |
} | |
SDL_SetRenderDrawColor(renderer, 55, 55, 55, 255); | |
int winflags = NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR; | |
float ratio_three[] = { 0.25f, 0.50f, 0.25f }; | |
int window_w, window_h; | |
SDL_GetWindowSize(win, &window_w, &window_h); | |
if (window_w != prev_window_w || window_h != prev_window_h) { | |
// Window size has changed | |
prev_window_w = window_w; | |
prev_window_h = window_h; | |
borders[0] = window_w * margin_left; | |
borders[1] = window_h * margin_top; | |
borders[2] = window_w * ((1.0f - margin_right) - margin_left); | |
borders[3] = window_h * ((1.0f - margin_bottom) - margin_top); | |
ui_panel_height = borders[3] - (borders[1] * 6); | |
middle_panel_pos = side_panel_width; // + margin_left; | |
middle_panel_width = 1.0f - (side_panel_width * 2.0f); | |
right_panel_pos = (1.0f - side_panel_width); | |
// Perform the calculations based on the new window size | |
// printf("Borders: %.2f %.2f %.2f %.2f\n", borders[0], borders[1], borders[2], borders[3]); | |
} | |
if (nk_begin(ctx, "MainGUI", nk_rect(borders[0], borders[1], borders[2], borders[3]), winflags)) | |
{ | |
//nk_style_from_table(ctx, theme); | |
nk_layout_space_begin(ctx, NK_DYNAMIC, ui_panel_height, 3); | |
nk_layout_space_push(ctx, nk_rect(side_panel_nomargin, side_panel_nomargin, side_panel_width, 1.0)); | |
// left panel here | |
nk_layout_space_push(ctx, nk_rect(middle_panel_pos, side_panel_nomargin, middle_panel_width, 1.0)); | |
if (nk_group_begin(ctx, "Group_Middle", NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR)){ | |
int fixed_item_width = 60; | |
int cols = (int)(((middle_panel_width*window_w) - fixed_item_width) / (fixed_item_width + (fixed_item_width / 10))); | |
auto cg_height = nk_widget_height(ctx); | |
nk_layout_row_dynamic(ctx, ui_panel_height - (cg_height * 1.35f), 1); | |
if (nk_group_begin(ctx, "Grid", NK_WINDOW_BORDER)) { | |
for (int i = 0; i < 14; i++) | |
{ | |
if (i % cols == 0) // dynamically change number of columns based on window width | |
nk_layout_row_dynamic(ctx, fixed_item_width, cols); | |
char id[8]; // Allocate memory for the string | |
snprintf(id, sizeof(id), "%d", i); | |
nk_button_label(ctx, id); | |
} | |
nk_group_end(ctx); | |
} | |
nk_group_end(ctx); | |
} | |
nk_layout_space_push(ctx, nk_rect(right_panel_pos, side_panel_nomargin, side_panel_width, 1.0)); | |
// right panel here | |
nk_layout_space_end(ctx); | |
} | |
nk_end(ctx); | |
return !nk_window_is_closed(ctx, "MainGUI"); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
/* Platform */ | |
SDL_Window* win; | |
SDL_Renderer* renderer; | |
int running = 1; | |
int flags = 0; | |
float font_scale = 1; | |
/* GUI */ | |
struct nk_context* ctx; | |
// SDL setup | |
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0"); | |
SDL_Init(SDL_INIT_VIDEO); | |
win = SDL_CreateWindow("Demo", | |
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE); | |
if (win == NULL) { | |
SDL_Log("Error SDL_CreateWindow %s", SDL_GetError()); | |
exit(-1); | |
} | |
flags |= SDL_RENDERER_ACCELERATED; | |
flags |= SDL_RENDERER_PRESENTVSYNC; | |
#if 0 | |
SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); | |
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software"); | |
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); | |
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software"); | |
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2"); | |
#endif | |
renderer = SDL_CreateRenderer(win, -1, flags); | |
SDL_SetWindowMinimumSize(win, WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT); | |
if (renderer == NULL) { | |
SDL_Log("Error SDL_CreateRenderer %s", SDL_GetError()); | |
exit(-1); | |
} | |
/* scale the renderer output for High-DPI displays */ | |
{ | |
int render_w, render_h; | |
int window_w, window_h; | |
float scale_x, scale_y; | |
SDL_GetRendererOutputSize(renderer, &render_w, &render_h); | |
SDL_GetWindowSize(win, &window_w, &window_h); | |
scale_x = (float)(render_w) / (float)(window_w); | |
scale_y = (float)(render_h) / (float)(window_h); | |
SDL_RenderSetScale(renderer, scale_x, scale_y); | |
font_scale = scale_y; | |
} | |
/* GUI */ | |
ctx = nk_sdl_init(win, renderer); | |
/* Load Fonts: if none of these are loaded a default font will be used */ | |
/* Load Cursor: if you uncomment cursor loading please hide the cursor */ | |
{ | |
struct nk_font_atlas* atlas; | |
struct nk_font_config config = nk_font_config(0); | |
struct nk_font* font; | |
/* set up the font atlas and add desired font; note that font sizes are | |
* multiplied by font_scale to produce better results at higher DPIs */ | |
nk_sdl_font_stash_begin(&atlas); | |
font = nk_font_atlas_add_default(atlas, 13 * font_scale, &config); | |
nk_sdl_font_stash_end(); | |
/* this hack makes the font appear to be scaled down to the desired | |
* size and is only necessary when font_scale > 1 */ | |
font->handle.height /= font_scale; | |
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/ | |
nk_style_set_font(ctx, &font->handle); | |
} | |
// Fix initial rendering bug regarding hue slider | |
SDL_SetWindowSize(win, WINDOW_WIDTH, WINDOW_HEIGHT - 10); | |
while (running) | |
{ | |
/* Input */ | |
SDL_Event evt; | |
nk_input_begin(ctx); | |
while (SDL_PollEvent(&evt)) { | |
if (evt.type == SDL_QUIT) goto cleanup; | |
nk_sdl_handle_event(&evt); | |
} | |
nk_input_end(ctx); | |
maingui(ctx, win, renderer); | |
SDL_RenderClear(renderer); | |
nk_sdl_render(NK_ANTI_ALIASING_ON); | |
SDL_RenderPresent(renderer); | |
} | |
cleanup: | |
nk_sdl_shutdown(); | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(win); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment