Python/UI: add more convenient API for drawing based on DPI.

system.ui_scale: size multiplier to use when drawing custom UI elements.
system.ui_line_width: suggested line thickness and point size in pixels.
This commit is contained in:
Brecht Van Lommel 2018-04-28 20:39:18 +02:00
parent e87dd9aa00
commit b5d3fcc9f5
4 changed files with 25 additions and 15 deletions

View File

@ -230,7 +230,7 @@ enum {
/* scale fixed button widths by this to account for DPI */
#define UI_DPI_FAC ((U.pixelsize * (float)U.dpi) / 72.0f)
#define UI_DPI_FAC (U.dpi_fac)
/* 16 to copy ICON_DEFAULT_HEIGHT */
#define UI_DPI_ICON_SIZE ((float)16 * UI_DPI_FAC)

View File

@ -487,12 +487,16 @@ typedef struct UserDef {
int audioformat;
int audiochannels;
float ui_scale; /* setting for UI scale */
int ui_line_width; /* setting for UI line width */
int dpi; /* runtime, full DPI divided by pixelsize */
float dpi_fac; /* runtime, multiplier to scale UI elements based on DPI */
float pixelsize; /* runtime, line width and point size based on DPI */
int virtual_pixel; /* deprecated, for forward compatibility */
int scrollback; /* console scrollback limit */
int dpi; /* range 48-128? */
float ui_scale; /* interface scale */
int ui_line_width; /* interface line width */
char node_margin; /* node insert offset (aka auto-offset) margin, but might be useful for later stuff as well */
char pad2;
char pad2[5];
short transopts; /* eUserpref_Translation_Flags */
short menuthreshold1, menuthreshold2;
@ -583,8 +587,6 @@ typedef struct UserDef {
int compute_device_id;
float fcu_inactive_alpha; /* opacity of inactive F-Curves in F-Curve Editor */
float pixelsize; /* private, set by GHOST, to multiply DPI with */
int virtual_pixel; /* virtual pixelsize mode */
short pie_interaction_type; /* if keeping a pie menu spawn button pressed after this time, it turns into
* a drag/release pie menu */

View File

@ -3957,20 +3957,27 @@ static void rna_def_userdef_system(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "International Fonts", "Use international fonts");
RNA_def_property_update(prop, NC_WINDOW, "rna_userdef_language_update");
prop = RNA_def_property(srna, "ui_scale", PROP_FLOAT, PROP_NONE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_float_sdna(prop, NULL, "dpi_fac");
RNA_def_property_ui_text(prop, "UI Scale",
"Size multiplier to use when drawing custom user interface elements, so that "
"they are scaled correctly on screens with different DPI. This value is based "
"on operating system DPI settings and Blender display scale");
prop = RNA_def_property(srna, "ui_line_width", PROP_FLOAT, PROP_NONE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_float_sdna(prop, NULL, "pixelsize");
RNA_def_property_ui_text(prop, "UI Line Width",
"Suggested line thickness and point size in pixels, for add-ons drawing custom "
"user interface elements, based on operating system settings and Blender UI scale");
prop = RNA_def_property(srna, "dpi", PROP_INT, PROP_NONE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "DPI",
"DPI for add-ons to use when drawing custom user interface elements, controlled by "
"operating system settings and Blender UI scale, with a reference value of 72 DPI "
"(note that since this value includes a user defined scale, it is not always the "
"actual monitor DPI)");
prop = RNA_def_property(srna, "pixel_size", PROP_FLOAT, PROP_NONE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_float_sdna(prop, NULL, "pixelsize");
RNA_def_property_ui_text(prop, "Pixel Size",
"Suggested line thickness and point size in pixels, for add-ons drawing custom user "
"interface elements, controlled by operating system settings and Blender UI scale");
prop = RNA_def_property(srna, "font_path_ui", PROP_STRING, PROP_FILEPATH);
RNA_def_property_string_sdna(prop, NULL, "font_path_ui");

View File

@ -602,6 +602,7 @@ void WM_window_set_dpi(wmWindow *win)
U.dpi = dpi / pixelsize;
U.virtual_pixel = (pixelsize == 1) ? VIRTUAL_PIXEL_NATIVE : VIRTUAL_PIXEL_DOUBLE;
U.widget_unit = (U.pixelsize * U.dpi * 20 + 36) / 72;
U.dpi_fac = ((U.pixelsize * (float)U.dpi) / 72.0f);
/* update font drawing */
BLF_default_dpi(U.pixelsize * U.dpi);