[Bf-blender-cvs] [fafd21b14c2] master: Animation: Generic Slider implementation
Christoph Lendenfeld
noreply at git.blender.org
Fri Jul 23 00:45:07 CEST 2021
Commit: fafd21b14c2337c2a5ca580d444d05cb1bb92566
Author: Christoph Lendenfeld
Date: Thu Jul 22 23:44:53 2021 +0100
Branches: master
https://developer.blender.org/rBfafd21b14c2337c2a5ca580d444d05cb1bb92566
Animation: Generic Slider implementation
Extract the slider gui implemented for the pose slide tools.
Generalise it so it can be used by other tools as well.
Reviewed by: Sybren A. Stüvel
Differential Revision: https://developer.blender.org/D9314
Ref: D9314
===================================================================
M source/blender/editors/armature/pose_slide.c
M source/blender/editors/include/ED_util.h
M source/blender/editors/util/ed_draw.c
===================================================================
diff --git a/source/blender/editors/armature/pose_slide.c b/source/blender/editors/armature/pose_slide.c
index 675af6eada9..e87d221058c 100644
--- a/source/blender/editors/armature/pose_slide.c
+++ b/source/blender/editors/armature/pose_slide.c
@@ -82,6 +82,7 @@
#include "ED_numinput.h"
#include "ED_screen.h"
#include "ED_space_api.h"
+#include "ED_util.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
@@ -137,8 +138,8 @@ typedef struct tPoseSlideOp {
Scene *scene;
/** area that we're operating in (needed for modal()) */
ScrArea *area;
- /** Header of the region used for drawing the slider. */
- ARegion *region_header;
+ /** Region we're operating in (needed for modal()). */
+ ARegion *region;
/** len of the PoseSlideObject array. */
uint objects_len;
@@ -168,26 +169,7 @@ typedef struct tPoseSlideOp {
/** Axis-limits for transforms. */
ePoseSlide_AxisLock axislock;
- /** Allow overshoot or clamp between 0% and 100%. */
- bool overshoot;
-
- /** Reduces factor delta from mouse movement. */
- bool precision;
-
- /** Move factor in 10% steps. */
- bool increments;
-
- /** Draw callback handler. */
- void *draw_handle;
-
- /** Accumulative, unclamped and unrounded factor. */
- float raw_factor;
-
- /** 0-1 value for determining the influence of whatever is relevant. */
- float factor;
-
- /** Last cursor position in screen space used for mouse movement delta calculation. */
- int last_cursor_x;
+ struct tSlider *slider;
/** Numeric input. */
NumInput num;
@@ -232,256 +214,6 @@ static const EnumPropertyItem prop_axis_lock_types[] = {
/* ------------------------------------ */
-static void draw_overshoot_triangle(const uint8_t color[4],
- const bool facing_right,
- const float x,
- const float y)
-{
- const uint shdr_pos_2d = GPU_vertformat_attr_add(
- immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
- immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
- GPU_blend(GPU_BLEND_ALPHA);
- GPU_polygon_smooth(true);
- immUniformColor3ubvAlpha(color, 225);
- const float triangle_side_length = facing_right ? 6 * U.pixelsize : -6 * U.pixelsize;
- const float triangle_offset = facing_right ? 2 * U.pixelsize : -2 * U.pixelsize;
-
- immBegin(GPU_PRIM_TRIS, 3);
- immVertex2f(shdr_pos_2d, x + triangle_offset + triangle_side_length, y);
- immVertex2f(shdr_pos_2d, x + triangle_offset, y + triangle_side_length / 2);
- immVertex2f(shdr_pos_2d, x + triangle_offset, y - triangle_side_length / 2);
- immEnd();
-
- GPU_polygon_smooth(false);
- GPU_blend(GPU_BLEND_NONE);
- immUnbindProgram();
-}
-
-static void draw_ticks(const float start_factor,
- const float end_factor,
- const float line_start[2],
- const float base_tick_height,
- const float line_width,
- const uint8_t color_overshoot[4],
- const uint8_t color_line[4])
-{
- /* Use factor represented as 0-100 int to avoid floating point precision problems. */
- const int tick_increment = 10;
-
- /* Round initial_tick_factor up to the next tick_increment. */
- int tick_percentage = ceil((start_factor * 100) / tick_increment) * tick_increment;
-
- while (tick_percentage <= (int)(end_factor * 100)) {
- float tick_height;
- /* Different ticks have different heights. Multiples of 100% are the tallest, 50% is a bit
- * smaller and the rest is the minimum size. */
- if (tick_percentage % 100 == 0) {
- tick_height = base_tick_height;
- }
- else if (tick_percentage % 50 == 0) {
- tick_height = base_tick_height * 0.8;
- }
- else {
- tick_height = base_tick_height * 0.5;
- }
-
- const float x = line_start[0] +
- (((float)tick_percentage / 100) - start_factor) * SLIDE_PIXEL_DISTANCE;
- const rctf tick_rect = {
- .xmin = x - (line_width / 2),
- .xmax = x + (line_width / 2),
- .ymin = line_start[1] - (tick_height / 2),
- .ymax = line_start[1] + (tick_height / 2),
- };
-
- if (tick_percentage < 0 || tick_percentage > 100) {
- UI_draw_roundbox_3ub_alpha(&tick_rect, true, 1, color_overshoot, 255);
- }
- else {
- UI_draw_roundbox_3ub_alpha(&tick_rect, true, 1, color_line, 255);
- }
- tick_percentage += tick_increment;
- }
-}
-
-static void draw_main_line(const rctf *main_line_rect,
- const float factor,
- const bool overshoot,
- const uint8_t color_overshoot[4],
- const uint8_t color_line[4])
-{
- if (overshoot) {
- /* In overshoot mode, draw the 0-100% range differently to provide a visual reference. */
- const float line_zero_percent = main_line_rect->xmin -
- ((factor - 0.5f - OVERSHOOT_RANGE_DELTA) *
- SLIDE_PIXEL_DISTANCE);
-
- const float clamped_line_zero_percent = clamp_f(
- line_zero_percent, main_line_rect->xmin, main_line_rect->xmax);
- const float clamped_line_hundred_percent = clamp_f(
- line_zero_percent + SLIDE_PIXEL_DISTANCE, main_line_rect->xmin, main_line_rect->xmax);
-
- const rctf left_overshoot_line_rect = {
- .xmin = main_line_rect->xmin,
- .xmax = clamped_line_zero_percent,
- .ymin = main_line_rect->ymin,
- .ymax = main_line_rect->ymax,
- };
- const rctf right_overshoot_line_rect = {
- .xmin = clamped_line_hundred_percent,
- .xmax = main_line_rect->xmax,
- .ymin = main_line_rect->ymin,
- .ymax = main_line_rect->ymax,
- };
- UI_draw_roundbox_3ub_alpha(&left_overshoot_line_rect, true, 0, color_overshoot, 255);
- UI_draw_roundbox_3ub_alpha(&right_overshoot_line_rect, true, 0, color_overshoot, 255);
-
- const rctf non_overshoot_line_rect = {
- .xmin = clamped_line_zero_percent,
- .xmax = clamped_line_hundred_percent,
- .ymin = main_line_rect->ymin,
- .ymax = main_line_rect->ymax,
- };
- UI_draw_roundbox_3ub_alpha(&non_overshoot_line_rect, true, 0, color_line, 255);
- }
- else {
- UI_draw_roundbox_3ub_alpha(main_line_rect, true, 0, color_line, 255);
- }
-}
-
-static void draw_backdrop(const int fontid,
- const rctf *main_line_rect,
- const float color_bg[4],
- const short region_y_size,
- const float base_tick_height)
-{
- float string_pixel_size[2];
- const char *percentage_string_placeholder = "000%%";
- BLF_width_and_height(fontid,
- percentage_string_placeholder,
- sizeof(percentage_string_placeholder),
- &string_pixel_size[0],
- &string_pixel_size[1]);
- const float pad[2] = {(region_y_size - base_tick_height) / 2, 2.0f * U.pixelsize};
- const rctf backdrop_rect = {
- .xmin = main_line_rect->xmin - string_pixel_size[0] - pad[0],
- .xmax = main_line_rect->xmax + pad[0],
- .ymin = pad[1],
- .ymax = region_y_size - pad[1],
- };
- UI_draw_roundbox_aa(&backdrop_rect, true, 4.0f, color_bg);
-}
-
-/**
- * Draw an on screen Slider for a Pose Slide Operator.
- */
-static void pose_slide_draw_2d_slider(const struct bContext *UNUSED(C), ARegion *region, void *arg)
-{
- tPoseSlideOp *pso = arg;
-
- /* Only draw in region from which the Operator was started. */
- if (region != pso->region_header) {
- return;
- }
-
- uint8_t color_text[4];
- uint8_t color_line[4];
- uint8_t color_handle[4];
- uint8_t color_overshoot[4];
- float color_bg[4];
-
- /* Get theme colors. */
- UI_GetThemeColor4ubv(TH_TEXT, color_text);
- UI_GetThemeColor4ubv(TH_TEXT, color_line);
- UI_GetThemeColor4ubv(TH_TEXT, color_overshoot);
- UI_GetThemeColor4ubv(TH_ACTIVE, color_handle);
- UI_GetThemeColor3fv(TH_BACK, color_bg);
-
- color_bg[3] = 0.5f;
- color_overshoot[0] = color_overshoot[0] * 0.7;
- color_overshoot[1] = color_overshoot[1] * 0.7;
- color_overshoot[2] = color_overshoot[2] * 0.7;
-
- /* Get the default font. */
- const uiStyle *style = UI_style_get();
- const uiFontStyle *fstyle = &style->widget;
- const int fontid = fstyle->uifont_id;
- BLF_color3ubv(fontid, color_text);
- BLF_rotation(fontid, 0.0f);
-
- const float line_width = 1.5 * U.pixelsize;
- const float base_tick_height = 12.0 * U.pixelsize;
- const float line_y = region->winy / 2;
-
- rctf main_line_rect = {
- .xmin = (region->winx / 2) - (SLIDE_PIXEL_DISTANCE / 2),
- .xmax = (region->winx / 2) + (SLIDE_PIXEL_DISTANCE / 2),
- .ymin = line_y - line_width / 2,
- .ymax = line_y + line_width / 2,
- };
- float line_start_factor = 0;
- int handle_pos_x = main_line_rect.xmin + SLIDE_PIXEL_DISTANCE * pso->factor;
-
- if (pso->overshoot) {
- main_line_rect.xmin = main_line_rect.xmin - SLIDE_PIXEL_DISTANCE * OVERSHOOT_RANGE_DELTA;
- main_line_rect.xmax = main_line_rect.xmax + SLIDE_PIXEL_DISTANCE * OVERSHOOT_RANGE_DELTA;
- line_start_factor = pso->factor - 0.5f - OVERSHOOT_RANGE_DELTA;
- handle_pos_x = region->winx / 2;
- }
-
- draw_backdrop(fontid, &main_line_rect, color_bg, pso->region_header->winy, base_tick_height);
-
- draw_main_line(&main_line_rect, pso->factor, pso->overshoot, color_overshoot, color_line);
-
- const float factor_range = pso->overshoot ? 1 + OVERSHOOT_RANGE_DELTA * 2 : 1;
- const float line_start_position[2] = {main_line_rect.xmin, line_y};
- draw_ticks(line_start_factor,
- line_start_factor + factor_range,
- line_start_position,
- base_tick_height,
- line_width,
- color_overshoot,
- color_line);
-
- /* Draw triangles at the ends of the line in overshoot mode to indicate direction of 0-100%
- * range. */
- if (pso->overshoot) {
- if (pso->factor > 1 + OVERSHOOT_RANGE_DELTA + 0.5) {
- draw_overshoot_triangle(color_line, false, main_line_rect.xmin, line_y);
- }
- if (pso->factor < 0 - OVERSHOOT_RANGE_DELTA - 0.5) {
- draw
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list