Skip to content

Commit

Permalink
UI callbacks (#1133)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Dec 31, 2020
1 parent f820cd4 commit e504e2e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 55 deletions.
56 changes: 49 additions & 7 deletions apps/ytrace/ytrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,61 @@ int run_view(const view_params& params) {
},
[viewer](const image<vec4f>& render, int current, int total) {
set_image(viewer, "render", render);
// if (current > 0) return;
// app->render = render;
// app->display = tonemap_image(app->render, app->exposure);
},
[](const image<vec4f>& render, int current, int total, const vec2i& ij) {
// app->render[ij] = render[ij];
// app->display[ij] = tonemap(app->render[ij], app->exposure);
});

// show rendering params
set_params(
viewer, "render", to_json(params), to_schema(params, "Render params"));

// set callback
set_callback(viewer,
[state, scene, camera, bvh, lights, viewer, &params](const string& name,
const json_value& uiparams, const gui_input& input) {
if (name != "render") return;
if (!uiparams.is_null()) {
trace_stop(state);
(view_params&)params = from_json<view_params>(uiparams);
// show rendering params
set_params(viewer, "render", to_json(params),
to_schema(params, "Render params"));
trace_start(
state, scene, camera, bvh, lights, params,
[viewer](const string& message, int sample, int nsamples) {
set_param(viewer, "render", "sample", to_json(sample),
to_schema(sample, "Current sample"));
print_progress(message, sample, nsamples);
},
[viewer](const image<vec4f>& render, int current, int total) {
set_image(viewer, "render", render);
});
} else if ((input.mouse_left || input.mouse_right) &&
input.mouse_pos != input.mouse_last) {
trace_stop(state);
auto dolly = 0.0f;
auto pan = zero2f;
auto rotate = zero2f;
if (input.mouse_left && !input.modifier_shift)
rotate = (input.mouse_pos - input.mouse_last) / 100.0f;
if (input.mouse_right)
dolly = (input.mouse_pos.x - input.mouse_last.x) / 100.0f;
if (input.mouse_left && input.modifier_shift)
pan = (input.mouse_pos - input.mouse_last) * camera->focus / 200.0f;
pan.x = -pan.x;
std::tie(camera->frame, camera->focus) = camera_turntable(
camera->frame, camera->focus, rotate, dolly, pan);
trace_start(
state, scene, camera, bvh, lights, params,
[viewer](const string& message, int sample, int nsamples) {
set_param(viewer, "render", "sample", to_json(sample),
to_schema(sample, "Current sample"));
print_progress(message, sample, nsamples);
},
[viewer](const image<vec4f>& render, int current, int total) {
set_image(viewer, "render", render);
});
}
});

// run view
run_view(viewer);

Expand Down
75 changes: 27 additions & 48 deletions libs/yocto_gui/yocto_imageviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ void set_params(imageview_state* state, const string& name,
state->queue.push(command);
}

// Callback
void set_callback(imageview_state* state, const imageview_callback& callback) {
state->callback = callback;
}

} // namespace yocto

// -----------------------------------------------------------------------------
Expand All @@ -152,11 +157,6 @@ static void update_display(imageview_image* img) {
} else {
// TODO(fabio): decide about empty images
}
// if (app->colorgrade) {
// colorgrade_image_mt(app->display, app->source, true, app->params);
// } else {
// tonemap_image_mt(app->display, app->source, app->exposure, app->filmic);
// }
}

void draw_widgets(
Expand Down Expand Up @@ -185,35 +185,6 @@ void draw_widgets(
}
draw_combobox(win, "image", state->selected, state->images, false);
if (!state->selected) return;
// if (begin_header(win, "colorgrade")) {
// auto& params = app->params;
// auto edited = 0;
// edited += draw_checkbox(win, "apply colorgrade", app->colorgrade);
// edited += draw_slider(win, "exposure", params.exposure, -5, 5);
// edited += draw_coloredit(win, "tint", params.tint);
// edited += draw_slider(win, "lincontrast", params.lincontrast, 0, 1);
// edited += draw_slider(win, "logcontrast", params.logcontrast, 0, 1);
// edited += draw_slider(win, "linsaturation", params.linsaturation, 0, 1);
// edited += draw_checkbox(win, "filmic", params.filmic);
// continue_line(win);
// edited += draw_checkbox(win, "srgb", params.srgb);
// continue_line(win);
// if (draw_button(win, "auto wb")) {
// auto wb = 1 / xyz(app->source_stats.average);
// params.tint = wb / max(wb);
// edited += 1;
// }
// edited += draw_slider(win, "contrast", params.contrast, 0, 1);
// edited += draw_slider(win, "saturation", params.saturation, 0, 1);
// edited += draw_slider(win, "shadows", params.shadows, 0, 1);
// edited += draw_slider(win, "midtones", params.midtones, 0, 1);
// edited += draw_slider(win, "highlights", params.highlights, 0, 1);
// edited += draw_coloredit(win, "shadows color", params.shadows_color);
// edited += draw_coloredit(win, "midtones color", params.midtones_color);
// edited += draw_coloredit(win, "highlights color",
// params.highlights_color); if (edited) update_display(app);
// end_header(win);
// }
if (begin_header(win, "inspect")) {
auto img = state->selected;
draw_label(win, "name", img->name);
Expand Down Expand Up @@ -252,8 +223,11 @@ void draw_widgets(
}
}
if (!state->selected->params.empty()) {
draw_params(
win, "params", state->selected->params, state->selected->schema, true);
if (draw_params(win, "params", state->selected->params,
state->selected->schema, false)) {
if (state->callback)
state->callback(state->selected->name, state->selected->params, {});
}
}
}

Expand All @@ -273,9 +247,9 @@ void draw(gui_window* win, imageview_state* state, const gui_input& input) {
}

void update(gui_window* win, imageview_state* state, const gui_input& input) {
auto command = imageview_command{};
auto has_command = state->queue.try_pop(command);
if (has_command) {
while (!state->queue.empty()) {
auto command = imageview_command{};
if (!state->queue.try_pop(command)) break;
switch (command.type) {
case imageview_command_type::set: {
auto img = (imageview_image*)nullptr;
Expand Down Expand Up @@ -379,15 +353,20 @@ void run_view(imageview_state* state) {
};
callbacks.uiupdate_cb = [state](gui_window* win, const gui_input& input) {
if (!state->selected) return;
// auto app = state->selected;
// // handle mouse
// if (input.mouse_left && !input.widgets_active) {
// app->glparams.center += input.mouse_pos - input.mouse_last;
// }
// if (input.mouse_right && !input.widgets_active) {
// app->glparams.scale *= powf(
// 2, (input.mouse_pos.x - input.mouse_last.x) * 0.001f);
// }
if (input.widgets_active) return;
auto img = state->selected;
// handle mouse
if (input.modifier_alt) {
if (input.mouse_left) {
img->glparams.center += input.mouse_pos - input.mouse_last;
}
if (input.mouse_right) {
img->glparams.scale *= powf(
2, (input.mouse_pos.x - input.mouse_last.x) * 0.001f);
}
} else {
if (state->callback) state->callback(state->selected->name, {}, input);
}
};

// run ui
Expand Down
6 changes: 6 additions & 0 deletions libs/yocto_gui/yocto_imageviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ void set_param(imageview_state* viewer, const string& name, const string& pname,
void set_params(imageview_state* viewer, const string& name,
const json_value& params, const json_value& schema);

// Set ui callback
using imageview_callback =
function<void(const string&, const json_value&, const gui_input&)>;
void set_callback(imageview_state* viewer, const imageview_callback& callback);

// Open and asycn viewer
struct imageview_state;
unique_ptr<imageview_state> open_imageview(const string& title);
Expand Down Expand Up @@ -159,6 +164,7 @@ struct imageview_state {
imageview_queue queue; // command queue
vector<imageview_imageptr> images = {}; // images
imageview_image* selected = nullptr; // selected
imageview_callback callback = {}; // params and ui callback
};

} // namespace yocto
Expand Down

0 comments on commit e504e2e

Please sign in to comment.