NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx, struct nk_vec2 item_padding) { /* update the bounds to stand without padding */ struct nk_window *win; struct nk_style *style; struct nk_panel *layout; enum nk_widget_layout_states state; struct nk_vec2 panel_padding; NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(ctx->current->layout); if (!ctx || !ctx->current || !ctx->current->layout) return NK_WIDGET_INVALID; win = ctx->current; style = &ctx->style; layout = win->layout; state = nk_widget(bounds, ctx); panel_padding = nk_panel_get_padding(style, layout->type); if (layout->row.index == 1) { bounds->w += panel_padding.x; bounds->x -= panel_padding.x; } else bounds->x -= item_padding.x; if (layout->row.index == layout->row.columns) bounds->w += panel_padding.x; else bounds->w += item_padding.x; return state; }
NK_API int nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size) { struct nk_window *win; struct nk_rect header; const struct nk_input *in; int is_clicked = nk_false; nk_flags state; NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(ctx->current->layout); if (!ctx || !ctx->current || !ctx->current->layout) return 0; win = ctx->current; state = nk_widget(&header, ctx); if (!state) return 0; in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input; if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, ctx->style.font, in)) is_clicked = nk_true; return nk_menu_begin(ctx, win, title, is_clicked, header, size); }
static void canvas_begin(struct nk_context *ctx, struct nk_canvas *canvas, nk_flags flags, uint32_t x, uint32_t y, uint32_t width, uint32_t height, struct nk_color background_color){ /* save style properties which will be overwritten */ canvas->panel_padding = ctx->style.window.padding; canvas->item_spacing = ctx->style.window.spacing; canvas->window_background = ctx->style.window.fixed_background; /* use the complete window space and set background */ ctx->style.window.spacing = nk_vec2(0,0); ctx->style.window.padding = nk_vec2(0,0); ctx->style.window.fixed_background = nk_style_item_color(background_color); /* create/update window and set position + size */ flags = flags & ~NK_WINDOW_DYNAMIC; nk_begin(ctx, "Window", nk_rect(x, y, width, height), NK_WINDOW_NO_SCROLLBAR|flags); //nk_window_set_bounds(ctx, nk_rect(x, y, width, height)); /* allocate the complete window space for drawing */ {struct nk_rect total_space; total_space = nk_window_get_content_region(ctx); nk_layout_row_dynamic(ctx, total_space.h, 1); nk_widget(&total_space, ctx); canvas->painter = nk_window_get_canvas(ctx);} }
NK_API int nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img, struct nk_vec2 size) { struct nk_window *win; struct nk_rect header; const struct nk_input *in; int is_clicked = nk_false; nk_flags state; NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(ctx->current->layout); if (!ctx || !ctx->current || !ctx->current->layout) return 0; win = ctx->current; state = nk_widget(&header, ctx); if (!state) return 0; in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input; if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header, img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in)) is_clicked = nk_true; return nk_menu_begin(ctx, win, id, is_clicked, header, size); }
static void hat_widget(struct nk_context* nk, unsigned char state) { float radius; struct nk_rect area; struct nk_vec2 center; if (nk_widget(&area, nk) != NK_WIDGET_VALID) return; center = nk_vec2(area.x + area.w / 2.f, area.y + area.h / 2.f); radius = NK_MIN(area.w, area.h) / 2.f; nk_stroke_circle(nk_window_get_canvas(nk), nk_rect(center.x - radius, center.y - radius, radius * 2.f, radius * 2.f), 1.f, nk_rgb(175, 175, 175)); if (state) { const float angles[] = { 0.f, 0.f, NK_PI * 1.5f, NK_PI * 1.75f, NK_PI, 0.f, NK_PI * 1.25f, 0.f, NK_PI * 0.5f, NK_PI * 0.25f, 0.f, 0.f, NK_PI * 0.75f, 0.f, }; const float cosa = nk_cos(angles[state]); const float sina = nk_sin(angles[state]); const struct nk_vec2 p0 = nk_vec2(0.f, -radius); const struct nk_vec2 p1 = nk_vec2( radius / 2.f, -radius / 3.f); const struct nk_vec2 p2 = nk_vec2(-radius / 2.f, -radius / 3.f); nk_fill_triangle(nk_window_get_canvas(nk), center.x + cosa * p0.x + sina * p0.y, center.y + cosa * p0.y - sina * p0.x, center.x + cosa * p1.x + sina * p1.y, center.y + cosa * p1.y - sina * p1.x, center.x + cosa * p2.x + sina * p2.y, center.y + cosa * p2.y - sina * p2.x, nk_rgb(175, 175, 175)); } }
/* =============================================================== * * CUSTOM WIDGET * * ===============================================================*/ static int ui_piemenu(struct nk_context *ctx, struct nk_vec2 pos, float radius, struct nk_image *icons, int item_count) { int ret = -1; struct nk_rect total_space; struct nk_rect bounds; int active_item = 0; /* pie menu popup */ struct nk_color border = ctx->style.window.border_color; struct nk_style_item background = ctx->style.window.fixed_background; ctx->style.window.fixed_background = nk_style_item_hide(); ctx->style.window.border_color = nk_rgba(0,0,0,0); total_space = nk_window_get_content_region(ctx); ctx->style.window.spacing = nk_vec2(0,0); ctx->style.window.padding = nk_vec2(0,0); if (nk_popup_begin(ctx, NK_POPUP_STATIC, "piemenu", NK_WINDOW_NO_SCROLLBAR, nk_rect(pos.x - total_space.x - radius, pos.y - radius - total_space.y, 2*radius,2*radius))) { int i = 0; struct nk_command_buffer* out = nk_window_get_canvas(ctx); const struct nk_input *in = &ctx->input; total_space = nk_window_get_content_region(ctx); ctx->style.window.spacing = nk_vec2(4,4); ctx->style.window.padding = nk_vec2(8,8); nk_layout_row_dynamic(ctx, total_space.h, 1); nk_widget(&bounds, ctx); /* outer circle */ nk_fill_circle(out, bounds, nk_rgb(50,50,50)); { /* circle buttons */ float step = (2 * 3.141592654f) / (float)(MAX(1,item_count)); float a_min = 0; float a_max = step; struct nk_vec2 center = nk_vec2(bounds.x + bounds.w / 2.0f, bounds.y + bounds.h / 2.0f); struct nk_vec2 drag = nk_vec2(in->mouse.pos.x - center.x, in->mouse.pos.y - center.y); float angle = (float)atan2(drag.y, drag.x); if (angle < -0.0f) angle += 2.0f * 3.141592654f; active_item = (int)(angle/step); for (i = 0; i < item_count; ++i) { struct nk_rect content; float rx, ry, dx, dy, a; nk_fill_arc(out, center.x, center.y, (bounds.w/2.0f), a_min, a_max, (active_item == i) ? nk_rgb(45,100,255): nk_rgb(60,60,60)); /* separator line */ rx = bounds.w/2.0f; ry = 0; dx = rx * (float)cos(a_min) - ry * (float)sin(a_min); dy = rx * (float)sin(a_min) + ry * (float)cos(a_min); nk_stroke_line(out, center.x, center.y, center.x + dx, center.y + dy, 1.0f, nk_rgb(50,50,50)); /* button content */ a = a_min + (a_max - a_min)/2.0f; rx = bounds.w/2.5f; ry = 0; content.w = 30; content.h = 30; content.x = center.x + ((rx * (float)cos(a) - ry * (float)sin(a)) - content.w/2.0f); content.y = center.y + (rx * (float)sin(a) + ry * (float)cos(a) - content.h/2.0f); nk_draw_image(out, content, &icons[i], nk_rgb(255,255,255)); a_min = a_max; a_max += step; } } { /* inner circle */ struct nk_rect inner; inner.x = bounds.x + bounds.w/2 - bounds.w/4; inner.y = bounds.y + bounds.h/2 - bounds.h/4; inner.w = bounds.w/2; inner.h = bounds.h/2; nk_fill_circle(out, inner, nk_rgb(45,45,45)); /* active icon content */ bounds.w = inner.w / 2.0f; bounds.h = inner.h / 2.0f; bounds.x = inner.x + inner.w/2 - bounds.w/2; bounds.y = inner.y + inner.h/2 - bounds.h/2; nk_draw_image(out, bounds, &icons[active_item], nk_rgb(255,255,255)); } nk_layout_space_end(ctx); if (!nk_input_is_mouse_down(&ctx->input, NK_BUTTON_RIGHT)) { nk_popup_close(ctx); ret = active_item; } } else ret = -2; ctx->style.window.spacing = nk_vec2(4,4); ctx->style.window.padding = nk_vec2(8,8); nk_popup_end(ctx); ctx->style.window.fixed_background = background; ctx->style.window.border_color = border; return ret; }
int main(int argc, char** argv) { if (argc > 2) message_and_abort_fmt("Usage: %s [filename]", argv[0]); Render::RenderSettings renderSettings; renderSettings.windowWidth = 800; renderSettings.windowHeight = 600; renderSettings.fullscreen = false; NuklearMisc::StandaloneGuiHandler guiHandler("Cel Viewer", renderSettings); nk_context* ctx = guiHandler.getNuklearContext(); Settings::Settings settings; settings.loadFromFile("resources/celview.ini"); bool faioInitDone = false; std::string listFile = settings.get<std::string>("celview", "listFile", "Diablo I.txt"); std::string mpqFile = settings.get<std::string>("celview", "mpqFile", "DIABDAT.MPQ"); std::vector<std::string> celFiles; std::string selectedImage = ""; std::unique_ptr<NuklearMisc::GuiSprite> image; std::unique_ptr<NuklearMisc::GuiSprite> nextImage; int animate = false; int32_t frame = 0; float rowHeight = 30; auto lastFrame = std::chrono::high_resolution_clock::now(); bool quit = false; while (!quit) { auto now = std::chrono::high_resolution_clock::now(); if (nextImage) image = std::unique_ptr<NuklearMisc::GuiSprite>(nextImage.release()); renderSettings = Render::getWindowSize(); if (nk_begin(ctx, "main_window", nk_rect(0, 0, renderSettings.windowWidth, renderSettings.windowHeight), NK_WINDOW_NO_SCROLLBAR)) { struct nk_rect bounds = nk_window_get_content_region(ctx); nk_layout_row_dynamic(ctx, bounds.h, 2); if (nk_group_begin(ctx, "image", 0)) { nk_layout_row_dynamic(ctx, rowHeight, 1); std::string label = selectedImage; if (selectedImage == "") label = "No image selected"; nk_label(ctx, label.c_str(), NK_TEXT_CENTERED); nk_checkbox_label(ctx, "Animate", &animate); if (image) { nk_label(ctx, (boost::format("Number of Frames: %1%") % image.get()->getSprite()->size()).str().c_str(), NK_TEXT_LEFT); nk_label(ctx, (boost::format("Width: %1%") % image->getSprite()->getWidth()).str().c_str(), NK_TEXT_LEFT); nk_label(ctx, (boost::format("Height: %1%") % image->getSprite()->getHeight()).str().c_str(), NK_TEXT_LEFT); frame = nk_propertyi(ctx, "Frame", 0, frame, image->getSprite()->size(), 1, 0.2f); if (nk_button_label(ctx, "save as png")) { nfdchar_t* outPath = NULL; nfdresult_t result = NFD_SaveDialog("png", NULL, &outPath); if (result == NFD_OKAY) { Render::SpriteGroup::toPng(selectedImage, outPath); free(outPath); } } if (nk_button_label(ctx, "save as gif")) { nfdchar_t* outPath = NULL; nfdresult_t result = NFD_SaveDialog("gif", NULL, &outPath); if (result == NFD_OKAY) { Render::SpriteGroup::toGif(selectedImage, outPath); free(outPath); } } auto msSinceLastFrame = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastFrame).count(); if (animate && msSinceLastFrame > 100) { lastFrame = now; frame++; } if (frame >= (int32_t)image.get()->getSprite()->size()) frame = 0; Render::Sprite sprite = image.get()->getSprite()->operator[](frame); int32_t w, h; Render::spriteSize(sprite, w, h); nk_layout_space_begin(ctx, NK_STATIC, h, 1); { nk_layout_space_push(ctx, nk_rect(0, 0, w, h)); auto canvas = nk_window_get_canvas(ctx); struct nk_rect imageRect; nk_widget(&imageRect, ctx); nk_fill_rect(canvas, imageRect, 0.0, nk_rgb(0, 255, 0)); auto img = image.get()->getNkImage(frame); nk_draw_image(canvas, imageRect, &img, nk_rgb(255, 255, 255)); } nk_layout_space_end(ctx); } nk_group_end(ctx); } if (nk_group_begin(ctx, "file list", 0)) { if (!faioInitDone) { nk_layout_row_dynamic(ctx, rowHeight * 2, 1); NuklearMisc::nk_file_pick(ctx, "DIABDAT.MPQ", mpqFile, "mpq,MPQ", rowHeight); NuklearMisc::nk_file_pick(ctx, "Diablo listfile", listFile, "txt", rowHeight); if (nk_button_label(ctx, "load")) { FAIO::init(mpqFile, listFile); celFiles = FAIO::listMpqFiles("*.cel"); auto tmp = FAIO::listMpqFiles("*.cl2"); celFiles.insert(celFiles.end(), tmp.begin(), tmp.end()); std::sort(celFiles.begin(), celFiles.end()); settings.set<std::string>("celview", "listFile", listFile); settings.set<std::string>("celview", "mpqFile", mpqFile); settings.save(); faioInitDone = true; if (argc > 1) { selectedImage = argv[1]; frame = 0; nextImage = std::unique_ptr<NuklearMisc::GuiSprite>(guiHandler.getSprite(new Render::SpriteGroup(selectedImage))); } } } nk_layout_row_dynamic(ctx, rowHeight, 1); for (size_t i = 0; i < celFiles.size(); i++) { auto buttonStyle = ctx->style.button; if (selectedImage == celFiles[i]) buttonStyle.normal = buttonStyle.hover; if (nk_button_label_styled(ctx, &buttonStyle, celFiles[i].c_str())) { selectedImage = celFiles[i]; frame = 0; nextImage = std::unique_ptr<NuklearMisc::GuiSprite>(guiHandler.getSprite(new Render::SpriteGroup(selectedImage))); } } nk_group_end(ctx); } } nk_end(ctx); quit = guiHandler.update(); } FAIO::quit(); return 0; }
NK_API int nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len, struct nk_image img, struct nk_vec2 size) { struct nk_window *win; struct nk_style *style; struct nk_input *in; struct nk_rect header; int is_clicked = nk_false; enum nk_widget_layout_states s; const struct nk_style_item *background; struct nk_text text; NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(ctx->current->layout); if (!ctx || !ctx->current || !ctx->current->layout) return 0; win = ctx->current; style = &ctx->style; s = nk_widget(&header, ctx); if (!s) return 0; in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input; if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT)) is_clicked = nk_true; /* draw combo box header background and border */ if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) { background = &style->combo.active; text.text = style->combo.label_active; } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) { background = &style->combo.hover; text.text = style->combo.label_hover; } else { background = &style->combo.normal; text.text = style->combo.label_normal; } if (background->type == NK_STYLE_ITEM_IMAGE) { text.background = nk_rgba(0,0,0,0); nk_draw_image(&win->buffer, header, &background->data.image, nk_white); } else { text.background = background->data.color; nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color); nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color); } { struct nk_rect content; struct nk_rect button; struct nk_rect label; struct nk_rect image; enum nk_symbol_type sym; if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) sym = style->combo.sym_hover; else if (is_clicked) sym = style->combo.sym_active; else sym = style->combo.sym_normal; /* calculate button */ button.w = header.h - 2 * style->combo.button_padding.y; button.x = (header.x + header.w - header.h) - style->combo.button_padding.x; button.y = header.y + style->combo.button_padding.y; button.h = button.w; content.x = button.x + style->combo.button.padding.x; content.y = button.y + style->combo.button.padding.y; content.w = button.w - 2 * style->combo.button.padding.x; content.h = button.h - 2 * style->combo.button.padding.y; nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state, &ctx->style.combo.button, sym, style->font); /* draw image */ image.x = header.x + style->combo.content_padding.x; image.y = header.y + style->combo.content_padding.y; image.h = header.h - 2 * style->combo.content_padding.y; image.w = image.h; nk_draw_image(&win->buffer, image, &img, nk_white); /* draw label */ text.padding = nk_vec2(0,0); label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x; label.y = header.y + style->combo.content_padding.y; label.w = (button.x - style->combo.content_padding.x) - label.x; label.h = header.h - 2 * style->combo.content_padding.y; nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font); } return nk_combo_begin(ctx, win, size, is_clicked, header); }
NK_API int nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size) { struct nk_window *win; struct nk_style *style; const struct nk_input *in; struct nk_rect header; int is_clicked = nk_false; enum nk_widget_layout_states s; const struct nk_style_item *background; NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(ctx->current->layout); if (!ctx || !ctx->current || !ctx->current->layout) return 0; win = ctx->current; style = &ctx->style; s = nk_widget(&header, ctx); if (s == NK_WIDGET_INVALID) return 0; in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input; if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT)) is_clicked = nk_true; /* draw combo box header background and border */ if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) background = &style->combo.active; else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) background = &style->combo.hover; else background = &style->combo.normal; if (background->type == NK_STYLE_ITEM_IMAGE) { nk_draw_image(&win->buffer, header, &background->data.image, nk_white); } else { nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color); nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color); } { struct nk_rect bounds = {0,0,0,0}; struct nk_rect content; struct nk_rect button; enum nk_symbol_type sym; if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) sym = style->combo.sym_hover; else if (is_clicked) sym = style->combo.sym_active; else sym = style->combo.sym_normal; /* calculate button */ button.w = header.h - 2 * style->combo.button_padding.y; button.x = (header.x + header.w - header.h) - style->combo.button_padding.y; button.y = header.y + style->combo.button_padding.y; button.h = button.w; content.x = button.x + style->combo.button.padding.x; content.y = button.y + style->combo.button.padding.y; content.w = button.w - 2 * style->combo.button.padding.x; content.h = button.h - 2 * style->combo.button.padding.y; /* draw image */ bounds.h = header.h - 2 * style->combo.content_padding.y; bounds.y = header.y + style->combo.content_padding.y; bounds.x = header.x + style->combo.content_padding.x; bounds.w = (button.x - style->combo.content_padding.y) - bounds.x; nk_draw_image(&win->buffer, bounds, &img, nk_white); /* draw open/close button */ nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state, &ctx->style.combo.button, sym, style->font); } return nk_combo_begin(ctx, win, size, is_clicked, header); }