2011-02-23 10:52:22 +00:00
|
|
|
/*
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
*/
|
2011-02-27 20:40:57 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
2011-02-27 20:40:57 +00:00
|
|
|
*/
|
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2010-12-10 04:10:21 +00:00
|
|
|
#include <assert.h>
|
2010-09-16 04:19:22 +00:00
|
|
|
|
2014-01-04 17:16:19 +11:00
|
|
|
#include "BLI_sys_types.h"
|
2010-10-21 07:29:15 +00:00
|
|
|
#include "BLI_math.h"
|
2011-08-30 10:07:50 +00:00
|
|
|
#include "BLI_string.h"
|
2012-04-10 02:51:24 +00:00
|
|
|
#include "BLI_string_utf8.h"
|
2012-04-15 07:54:07 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
2014-01-04 17:16:19 +11:00
|
|
|
#include "BKE_unit.h" /* own include */
|
|
|
|
|
2012-04-15 07:54:07 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# include "BLI_winstuff.h"
|
|
|
|
#endif
|
2009-08-13 18:26:15 +00:00
|
|
|
|
2014-08-26 20:52:07 +10:00
|
|
|
/* no BKE or DNA includes! */
|
|
|
|
|
2019-04-16 16:53:50 +02:00
|
|
|
/* Keep alignment. */
|
|
|
|
/* clang-format off */
|
|
|
|
|
2009-08-13 17:05:27 +00:00
|
|
|
#define TEMP_STR_SIZE 256
|
|
|
|
|
2009-08-17 12:48:56 +00:00
|
|
|
#define SEP_CHR '#'
|
|
|
|
#define SEP_STR "#"
|
|
|
|
|
2015-07-16 17:45:51 +02:00
|
|
|
#define EPS 0.001
|
2009-08-17 12:48:56 +00:00
|
|
|
|
2010-09-22 02:36:14 +00:00
|
|
|
#define UN_SC_KM 1000.0f
|
|
|
|
#define UN_SC_HM 100.0f
|
|
|
|
#define UN_SC_DAM 10.0f
|
|
|
|
#define UN_SC_M 1.0f
|
|
|
|
#define UN_SC_DM 0.1f
|
|
|
|
#define UN_SC_CM 0.01f
|
|
|
|
#define UN_SC_MM 0.001f
|
|
|
|
#define UN_SC_UM 0.000001f
|
|
|
|
|
|
|
|
#define UN_SC_MI 1609.344f
|
|
|
|
#define UN_SC_FUR 201.168f
|
|
|
|
#define UN_SC_CH 20.1168f
|
|
|
|
#define UN_SC_YD 0.9144f
|
|
|
|
#define UN_SC_FT 0.3048f
|
|
|
|
#define UN_SC_IN 0.0254f
|
|
|
|
#define UN_SC_MIL 0.0000254f
|
|
|
|
|
|
|
|
#define UN_SC_MTON 1000.0f /* metric ton */
|
|
|
|
#define UN_SC_QL 100.0f
|
|
|
|
#define UN_SC_KG 1.0f
|
|
|
|
#define UN_SC_HG 0.1f
|
|
|
|
#define UN_SC_DAG 0.01f
|
|
|
|
#define UN_SC_G 0.001f
|
2014-01-04 04:10:55 +01:00
|
|
|
#define UN_SC_MG 0.000001f
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
#define UN_SC_ITON 907.18474f /* imperial ton */
|
|
|
|
#define UN_SC_CWT 45.359237f
|
|
|
|
#define UN_SC_ST 6.35029318f
|
|
|
|
#define UN_SC_LB 0.45359237f
|
|
|
|
#define UN_SC_OZ 0.028349523125f
|
2009-08-17 12:48:56 +00:00
|
|
|
|
2019-04-16 16:53:50 +02:00
|
|
|
/* clang-format on */
|
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* define a single unit */
|
|
|
|
typedef struct bUnitDef {
|
2010-11-23 14:14:06 +00:00
|
|
|
const char *name;
|
2019-01-14 16:30:43 +11:00
|
|
|
/** abused a bit for the display name */
|
|
|
|
const char *name_plural;
|
|
|
|
/** this is used for display*/
|
|
|
|
const char *name_short;
|
|
|
|
/** keyboard-friendly ASCII-only version of name_short, can be NULL */
|
|
|
|
const char *name_alt;
|
2012-03-10 14:20:55 +00:00
|
|
|
/* if name_short has non-ASCII chars, name_alt should be present */
|
|
|
|
|
2019-01-14 16:30:43 +11:00
|
|
|
/** can be NULL */
|
|
|
|
const char *name_display;
|
|
|
|
/** when NULL, a transformed version of the name will be taken */
|
|
|
|
const char *identifier;
|
2009-08-17 12:48:56 +00:00
|
|
|
|
2009-08-12 14:11:53 +00:00
|
|
|
double scalar;
|
2019-01-14 16:30:43 +11:00
|
|
|
/** not used yet, needed for converting temperature */
|
|
|
|
double bias;
|
2009-08-12 14:11:53 +00:00
|
|
|
int flag;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
} bUnitDef;
|
|
|
|
|
2019-01-14 16:30:43 +11:00
|
|
|
enum {
|
|
|
|
B_UNIT_DEF_NONE = 0,
|
|
|
|
/** Use for units that are not used enough to be translated into for common use */
|
|
|
|
B_UNIT_DEF_SUPPRESS = 1,
|
|
|
|
/** Display a unit even if its value is 0.1, eg 0.1mm instead of 100um */
|
|
|
|
B_UNIT_DEF_TENTH = 2,
|
2019-02-17 16:39:28 +01:00
|
|
|
/** Short unit name is case sensitive, for example to distinguish mW and MW */
|
|
|
|
B_UNIT_DEF_CASE_SENSITIVE = 4,
|
2019-01-14 16:30:43 +11:00
|
|
|
};
|
2009-08-12 14:11:53 +00:00
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* define a single unit */
|
|
|
|
typedef struct bUnitCollection {
|
2016-05-03 13:52:07 +10:00
|
|
|
const struct bUnitDef *units;
|
2019-01-14 16:30:43 +11:00
|
|
|
/** basic unit index (when user doesn't specify unit explicitly) */
|
|
|
|
int base_unit;
|
|
|
|
/** options for this system */
|
|
|
|
int flag;
|
|
|
|
/** to quickly find the last item */
|
|
|
|
int length;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
} bUnitCollection;
|
|
|
|
|
2019-04-16 17:02:52 +02:00
|
|
|
/* Keep table lignment. */
|
|
|
|
/* clang-format off */
|
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
#define UNIT_COLLECTION_LENGTH(def) (sizeof(def) / sizeof(bUnitDef) - 1)
|
2018-11-15 14:39:52 +01:00
|
|
|
#define NULL_UNIT {NULL, NULL, NULL, NULL, NULL, NULL, 0.0, 0.0}
|
2018-10-03 10:20:16 +02:00
|
|
|
|
added time units, currently only used when metric or imperial are enabled.
long/short units...
day,d, hour,hr,h, minute,min,m, second,sec,s, millisecond,ms, microsecond,us
Also may fix some bugs that were reported.
Note, to convert fps to time evil_C needs to be used to get the scene.
2009-08-12 05:20:16 +00:00
|
|
|
/* Dummy */
|
2018-11-15 14:39:52 +01:00
|
|
|
static struct bUnitDef buDummyDef[] = { {"", NULL, "", NULL, NULL, NULL, 1.0, 0.0}, NULL_UNIT};
|
2013-03-13 17:16:49 +00:00
|
|
|
static struct bUnitCollection buDummyCollection = {buDummyDef, 0, 0, sizeof(buDummyDef)};
|
added time units, currently only used when metric or imperial are enabled.
long/short units...
day,d, hour,hr,h, minute,min,m, second,sec,s, millisecond,ms, microsecond,us
Also may fix some bugs that were reported.
Note, to convert fps to time evil_C needs to be used to get the scene.
2009-08-12 05:20:16 +00:00
|
|
|
|
|
|
|
/* Lengths */
|
2018-11-15 14:39:52 +01:00
|
|
|
static struct bUnitDef buMetricLenDef[] = {
|
|
|
|
{"kilometer", "kilometers", "km", NULL, "Kilometers", "KILOMETERS", UN_SC_KM, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"hectometer", "hectometers", "hm", NULL, "100 Meters", "HECTOMETERS", UN_SC_HM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"dekameter", "dekameters", "dam", NULL, "10 Meters", "DEKAMETERS", UN_SC_DAM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"meter", "meters", "m", NULL, "Meters", "METERS", UN_SC_M, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"decimeter", "decimeters", "dm", NULL, "10 Centimeters", "DECIMETERS", UN_SC_DM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"centimeter", "centimeters", "cm", NULL, "Centimeters", "CENTIMETERS", UN_SC_CM, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"millimeter", "millimeters", "mm", NULL, "Millimeters", "MILLIMETERS", UN_SC_MM, 0.0, B_UNIT_DEF_NONE | B_UNIT_DEF_TENTH},
|
|
|
|
{"micrometer", "micrometers", "µm", "um", "Micrometers", "MICROMETERS", UN_SC_UM, 0.0, B_UNIT_DEF_NONE},
|
2009-08-12 17:02:03 +00:00
|
|
|
|
|
|
|
/* These get displayed because of float precision problems in the transform header,
|
|
|
|
* could work around, but for now probably people wont use these */
|
2012-03-03 20:19:11 +00:00
|
|
|
#if 0
|
2012-07-04 12:56:58 +00:00
|
|
|
{"nanometer", "Nanometers", "nm", NULL, 0.000000001, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"picometer", "Picometers", "pm", NULL, 0.000000000001, 0.0, B_UNIT_DEF_NONE},
|
2012-03-03 20:19:11 +00:00
|
|
|
#endif
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static const struct bUnitCollection buMetricLenCollection = {buMetricLenDef, 3, 0, UNIT_COLLECTION_LENGTH(buMetricLenDef)};
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buImperialLenDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"mile", "miles", "mi", "m", "Miles", "MILES", UN_SC_MI, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"furlong", "furlongs", "fur", NULL, "Furlongs", "FURLONGS", UN_SC_FUR, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"chain", "chains", "ch", NULL, "Chains", "CHAINS", UN_SC_CH, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"yard", "yards", "yd", NULL, "Yards", "YARDS", UN_SC_YD, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"foot", "feet", "'", "ft", "Feet", "FEET", UN_SC_FT, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"inch", "inches", "\"", "in", "Inches", "INCHES", UN_SC_IN, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"thou", "thou", "thou", "mil", "Thou", "THOU", UN_SC_MIL, 0.0, B_UNIT_DEF_NONE}, /* plural for thou has no 's' */
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buImperialLenCollection = {buImperialLenDef, 4, 0, UNIT_COLLECTION_LENGTH(buImperialLenDef)};
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2010-09-22 02:36:14 +00:00
|
|
|
/* Areas */
|
|
|
|
static struct bUnitDef buMetricAreaDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"square kilometer", "square kilometers", "km²", "km2", "Square Kilometers", NULL, UN_SC_KM * UN_SC_KM, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"square hectometer", "square hectometers", "hm²", "hm2", "Square Hectometers", NULL, UN_SC_HM * UN_SC_HM, 0.0, B_UNIT_DEF_SUPPRESS}, /* hectare */
|
|
|
|
{"square dekameter", "square dekameters", "dam²", "dam2", "Square Dekameters", NULL, UN_SC_DAM * UN_SC_DAM, 0.0, B_UNIT_DEF_SUPPRESS}, /* are */
|
|
|
|
{"square meter", "square meters", "m²", "m2", "Square Meters", NULL, UN_SC_M * UN_SC_M, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"square decimeter", "square decimetees", "dm²", "dm2", "Square Decimeters", NULL, UN_SC_DM * UN_SC_DM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"square centimeter", "square centimeters", "cm²", "cm2", "Square Centimeters", NULL, UN_SC_CM * UN_SC_CM, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"square millimeter", "square millimeters", "mm²", "mm2", "Square Millimeters", NULL, UN_SC_MM * UN_SC_MM, 0.0, B_UNIT_DEF_NONE | B_UNIT_DEF_TENTH},
|
|
|
|
{"square micrometer", "square micrometers", "µm²", "um2", "Square Micrometers", NULL, UN_SC_UM * UN_SC_UM, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buMetricAreaCollection = {buMetricAreaDef, 3, 0, UNIT_COLLECTION_LENGTH(buMetricAreaDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buImperialAreaDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"square mile", "square miles", "sq mi", "sq m", "Square Miles", NULL, UN_SC_MI * UN_SC_MI, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"square furlong", "square furlongs", "sq fur", NULL, "Square Furlongs", NULL, UN_SC_FUR * UN_SC_FUR, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"square chain", "square chains", "sq ch", NULL, "Square Chains", NULL, UN_SC_CH * UN_SC_CH, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"square yard", "square yards", "sq yd", NULL, "Square Yards", NULL, UN_SC_YD * UN_SC_YD, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"square foot", "square feet", "sq ft", NULL, "Square Feet", NULL, UN_SC_FT * UN_SC_FT, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"square inch", "square inches", "sq in", NULL, "Square Inches", NULL, UN_SC_IN * UN_SC_IN, 0.0, B_UNIT_DEF_NONE},
|
2019-02-25 11:42:02 +01:00
|
|
|
{"square thou", "square thou", "sq mil", NULL, "Square Thou", NULL, UN_SC_MIL * UN_SC_MIL, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buImperialAreaCollection = {buImperialAreaDef, 4, 0, UNIT_COLLECTION_LENGTH(buImperialAreaDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
/* Volumes */
|
|
|
|
static struct bUnitDef buMetricVolDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"cubic kilometer", "cubic kilometers", "km³", "km3", "Cubic Kilometers", NULL, UN_SC_KM * UN_SC_KM * UN_SC_KM, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"cubic hectometer", "cubic hectometers", "hm³", "hm3", "Cubic Hectometers", NULL, UN_SC_HM * UN_SC_HM * UN_SC_HM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"cubic dekameter", "cubic dekameters", "dam³", "dam3", "Cubic Dekameters", NULL, UN_SC_DAM * UN_SC_DAM * UN_SC_DAM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"cubic meter", "cubic meters", "m³", "m3", "Cubic Meters", NULL, UN_SC_M * UN_SC_M * UN_SC_M, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"cubic decimeter", "cubic decimeters", "dm³", "dm3", "Cubic Decimeters", NULL, UN_SC_DM * UN_SC_DM * UN_SC_DM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"cubic centimeter", "cubic centimeters", "cm³", "cm3", "Cubic Centimeters", NULL, UN_SC_CM * UN_SC_CM * UN_SC_CM, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"cubic millimeter", "cubic millimeters", "mm³", "mm3", "Cubic Millimeters", NULL, UN_SC_MM * UN_SC_MM * UN_SC_MM, 0.0, B_UNIT_DEF_NONE | B_UNIT_DEF_TENTH},
|
|
|
|
{"cubic micrometer", "cubic micrometers", "µm³", "um3", "Cubic Micrometers", NULL, UN_SC_UM * UN_SC_UM * UN_SC_UM, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buMetricVolCollection = {buMetricVolDef, 3, 0, UNIT_COLLECTION_LENGTH(buMetricVolDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buImperialVolDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"cubic mile", "cubic miles", "cu mi", "cu m", "Cubic Miles", NULL, UN_SC_MI * UN_SC_MI * UN_SC_MI, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"cubic furlong", "cubic furlongs", "cu fur", NULL, "Cubic Furlongs", NULL, UN_SC_FUR * UN_SC_FUR * UN_SC_FUR, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"cubic chain", "cubic chains", "cu ch", NULL, "Cubic Chains", NULL, UN_SC_CH * UN_SC_CH * UN_SC_CH, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"cubic yard", "cubic yards", "cu yd", NULL, "Cubic Yards", NULL, UN_SC_YD * UN_SC_YD * UN_SC_YD, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"cubic foot", "cubic feet", "cu ft", NULL, "Cubic Feet", NULL, UN_SC_FT * UN_SC_FT * UN_SC_FT, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"cubic inch", "cubic inches", "cu in", NULL, "Cubic Inches", NULL, UN_SC_IN * UN_SC_IN * UN_SC_IN, 0.0, B_UNIT_DEF_NONE},
|
2019-02-25 11:42:02 +01:00
|
|
|
{"cubic thou", "cubic thou", "cu mil", NULL, "Cubic Thou", NULL, UN_SC_MIL * UN_SC_MIL * UN_SC_MIL, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buImperialVolCollection = {buImperialVolDef, 4, 0, UNIT_COLLECTION_LENGTH(buImperialVolDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
/* Mass */
|
|
|
|
static struct bUnitDef buMetricMassDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"ton", "tonnes", "ton", "t", "Tonnes", "TONNES", UN_SC_MTON, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"quintal", "quintals", "ql", "q", "100 Kilograms", "QUINTALS", UN_SC_QL, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"kilogram", "kilograms", "kg", NULL, "Kilograms", "KILOGRAMS", UN_SC_KG, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"hectogram", "hectograms", "hg", NULL, "Hectograms", "HECTOGRAMS", UN_SC_HG, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"dekagram", "dekagrams", "dag", NULL, "10 Grams", "DEKAGRAMS", UN_SC_DAG, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"gram", "grams", "g", NULL, "Grams", "GRAMS", UN_SC_G, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"milligram", "milligrams", "mg", NULL, "Milligrams", "MILLIGRAMS", UN_SC_MG, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buMetricMassCollection = {buMetricMassDef, 2, 0, UNIT_COLLECTION_LENGTH(buMetricMassDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buImperialMassDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"ton", "tonnes", "ton", "t", "Tonnes", "TONNES", UN_SC_ITON, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"centum weight", "centum weights", "cwt", NULL, "Centum weights", "CENTUM_WEIGHTS", UN_SC_CWT, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"stone", "stones", "st", NULL, "Stones", "STONES", UN_SC_ST, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"pound", "pounds", "lb", NULL, "Pounds", "POUNDS", UN_SC_LB, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"ounce", "ounces", "oz", NULL, "Ounces", "OUNCES", UN_SC_OZ, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buImperialMassCollection = {buImperialMassDef, 3, 0, UNIT_COLLECTION_LENGTH(buImperialMassDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
/* Even if user scales the system to a point where km^3 is used, velocity and
|
|
|
|
* acceleration aren't scaled: that's why we have so few units for them */
|
|
|
|
|
|
|
|
/* Velocity */
|
|
|
|
static struct bUnitDef buMetricVelDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"meter per second", "meters per second", "m/s", NULL, "Meters per second", NULL, UN_SC_M, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"kilometer per hour", "kilometers per hour", "km/h", NULL, "Kilometers per hour", NULL, UN_SC_KM / 3600.0f, 0.0, B_UNIT_DEF_SUPPRESS},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buMetricVelCollection = {buMetricVelDef, 0, 0, UNIT_COLLECTION_LENGTH(buMetricVelDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buImperialVelDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"foot per second", "feet per second", "ft/s", "fps", "Feet per second", NULL, UN_SC_FT, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"mile per hour", "miles per hour", "mph", NULL, "Miles per hour", NULL, UN_SC_MI / 3600.0f, 0.0, B_UNIT_DEF_SUPPRESS},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buImperialVelCollection = {buImperialVelDef, 0, 0, UNIT_COLLECTION_LENGTH(buImperialVelDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
/* Acceleration */
|
|
|
|
static struct bUnitDef buMetricAclDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"meter per second squared", "meters per second squared", "m/s²", "m/s2", "Meters per second squared", NULL, UN_SC_M, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buMetricAclCollection = {buMetricAclDef, 0, 0, UNIT_COLLECTION_LENGTH(buMetricAclDef)};
|
2010-09-22 02:36:14 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buImperialAclDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"foot per second squared", "feet per second squared", "ft/s²", "ft/s2", "Feet per second squared", NULL, UN_SC_FT, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-09-22 02:36:14 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buImperialAclCollection = {buImperialAclDef, 0, 0, UNIT_COLLECTION_LENGTH(buImperialAclDef)};
|
added time units, currently only used when metric or imperial are enabled.
long/short units...
day,d, hour,hr,h, minute,min,m, second,sec,s, millisecond,ms, microsecond,us
Also may fix some bugs that were reported.
Note, to convert fps to time evil_C needs to be used to get the scene.
2009-08-12 05:20:16 +00:00
|
|
|
|
|
|
|
/* Time */
|
|
|
|
static struct bUnitDef buNaturalTimeDef[] = {
|
|
|
|
/* weeks? - probably not needed for blender */
|
2018-11-15 14:39:52 +01:00
|
|
|
{"day", "days", "d", NULL, "Days", "DAYS", 90000.0, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"hour", "hours", "hr", "h", "Hours", "HOURS", 3600.0, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"minute", "minutes", "min", "m", "Minutes", "MINUTES", 60.0, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"second", "seconds", "sec", "s", "Seconds", "SECONDS", 1.0, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"millisecond", "milliseconds", "ms", NULL, "Milliseconds", "MILLISECONDS", 0.001, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"microsecond", "microseconds", "µs", "us", "Microseconds", "MICROSECONDS", 0.000001, 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
added time units, currently only used when metric or imperial are enabled.
long/short units...
day,d, hour,hr,h, minute,min,m, second,sec,s, millisecond,ms, microsecond,us
Also may fix some bugs that were reported.
Note, to convert fps to time evil_C needs to be used to get the scene.
2009-08-12 05:20:16 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buNaturalTimeCollection = {buNaturalTimeDef, 3, 0, UNIT_COLLECTION_LENGTH(buNaturalTimeDef)};
|
added time units, currently only used when metric or imperial are enabled.
long/short units...
day,d, hour,hr,h, minute,min,m, second,sec,s, millisecond,ms, microsecond,us
Also may fix some bugs that were reported.
Note, to convert fps to time evil_C needs to be used to get the scene.
2009-08-12 05:20:16 +00:00
|
|
|
|
2010-01-25 06:24:05 +00:00
|
|
|
|
|
|
|
static struct bUnitDef buNaturalRotDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"degree", "degrees", "°", "d", "Degrees", "DEGREES", M_PI / 180.0, 0.0, B_UNIT_DEF_NONE},
|
Support units in modal numinput
Summary:
This completly changes the way modal numinput is handled. Now, edited expression is a string, which then gets unit- and py-evaluated to get a float value.
We gain many power and flexibility, but lose a few "shortcuts" like '-' to negate, or '/' to inverse (if they are really needed, we still can add them with modifiers, like e.g. ctrl-/ or so).
Features:
- units (cm, ", deg, etc.).
- basic operations from python/BKE_unit (+, *, **, etc.), and math constants and functions (pi, sin, etc.).
- you can navigate in edited value (left/right key, ctrl to move by block) and insert/delete chars, e.g. to fix a typo without having to rewrite everything.
- you can go to next/previous value with (ctrl-)TAB key.
- As before, hitting backspace after having deleted all leading chars will first reset the edited value to init state, and on second press, the whole "modal numinput" editing will be cancelled, going back to usual transform with mouse.
Notes:
- Did not touch to how values are shown in header when modal numinput is not enabled (would do that in another commit), so this is still quite inconsistent.
- Added back radian support in BKE_unit.
- Added arcminute/arcsecond to BKE_unit.
(those unit changes affect all angle UI controls, btw, so you can now enter radians or longitude/latitude values when in degrees units).
Related to T37600.
Reviewers: brecht, campbellbarton, carter2422
Reviewed By: brecht, campbellbarton, carter2422
Thanks everybody!
Differential Revision: http://developer.blender.org/D61
2013-12-21 17:11:43 +01:00
|
|
|
/* arcminutes/arcseconds are used in Astronomy/Navigation areas... */
|
2018-11-15 14:39:52 +01:00
|
|
|
{"arcminute", "arcminutes", "'", NULL, "Arcminutes", "ARCMINUTES", (M_PI / 180.0) / 60.0, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"arcsecond", "arcseconds", "\"", NULL, "Arcseconds", "ARCSECONDS", (M_PI / 180.0) / 3600.0, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"radian", "radians", "r", NULL, "Radians", "RADIANS", 1.0, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
// {"turn", "turns", "t", NULL, "Turns", NULL, 1.0 / (M_PI * 2.0), 0.0, B_UNIT_DEF_NONE},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2010-01-25 06:24:05 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buNaturalRotCollection = {buNaturalRotDef, 0, 0, UNIT_COLLECTION_LENGTH(buNaturalRotDef)};
|
2010-01-25 06:24:05 +00:00
|
|
|
|
2013-03-13 17:16:49 +00:00
|
|
|
/* Camera Lengths */
|
|
|
|
static struct bUnitDef buCameraLenDef[] = {
|
2018-11-15 14:39:52 +01:00
|
|
|
{"meter", "meters", "m", NULL, "Meters", NULL, UN_SC_KM, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
|
|
|
{"decimeter", "decimeters", "dm", NULL, "10 Centimeters", NULL, UN_SC_HM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"centimeter", "centimeters", "cm", NULL, "Centimeters", NULL, UN_SC_DAM, 0.0, B_UNIT_DEF_SUPPRESS},
|
|
|
|
{"millimeter", "millimeters", "mm", NULL, "Millimeters", NULL, UN_SC_M, 0.0, B_UNIT_DEF_NONE},
|
|
|
|
{"micrometer", "micrometers", "µm", "um", "Micrometers", NULL, UN_SC_MM, 0.0, B_UNIT_DEF_SUPPRESS},
|
2019-02-03 14:01:45 +11:00
|
|
|
NULL_UNIT,
|
2013-03-13 17:16:49 +00:00
|
|
|
};
|
2018-10-03 10:20:16 +02:00
|
|
|
static struct bUnitCollection buCameraLenCollection = {buCameraLenDef, 3, 0, UNIT_COLLECTION_LENGTH(buCameraLenDef)};
|
2013-03-13 17:16:49 +00:00
|
|
|
|
2019-02-08 12:31:28 +01:00
|
|
|
/* (Light) Power */
|
|
|
|
static struct bUnitDef buPowerDef[] = {
|
|
|
|
{"gigawatt", "gigawatts", "GW", NULL, "Gigawatts", NULL, 1e9f, 0.0, B_UNIT_DEF_NONE},
|
2019-02-17 16:39:28 +01:00
|
|
|
{"megawatt", "megawatts", "MW", NULL, "Megawatts", NULL, 1e6f, 0.0, B_UNIT_DEF_CASE_SENSITIVE},
|
2019-02-08 12:31:28 +01:00
|
|
|
{"kilowatt", "kilowatts", "kW", NULL, "Kilowatts", NULL, 1e3f, 0.0, B_UNIT_DEF_SUPPRESS},
|
2019-02-18 14:20:55 +01:00
|
|
|
{"watt", "watts", "W", NULL, "Watts", NULL, 1.0f, 0.0, B_UNIT_DEF_NONE}, /* base unit */
|
2019-02-17 16:39:28 +01:00
|
|
|
{"milliwatt", "milliwatts", "mW", NULL, "Milliwatts", NULL, 1e-3f, 0.0, B_UNIT_DEF_CASE_SENSITIVE},
|
2019-02-08 12:31:28 +01:00
|
|
|
{"microwatt", "microwatts", "µW", "uW", "Microwatts", NULL, 1e-6f, 0.0, B_UNIT_DEF_NONE},
|
2019-02-27 13:22:55 +01:00
|
|
|
{"nanowatt", "nanowatts", "nW", NULL, "Nanowatts", NULL, 1e-9f, 0.0, B_UNIT_DEF_NONE},
|
2019-02-18 14:20:55 +01:00
|
|
|
NULL_UNIT,
|
2019-02-08 12:31:28 +01:00
|
|
|
};
|
|
|
|
static struct bUnitCollection buPowerCollection = {buPowerDef, 3, 0, UNIT_COLLECTION_LENGTH(buPowerDef)};
|
|
|
|
|
2013-03-13 17:16:49 +00:00
|
|
|
|
|
|
|
#define UNIT_SYSTEM_TOT (((sizeof(bUnitSystems) / B_UNIT_TYPE_TOT) / sizeof(void *)) - 1)
|
2016-05-03 13:52:07 +10:00
|
|
|
static const struct bUnitCollection *bUnitSystems[][B_UNIT_TYPE_TOT] = {
|
2019-02-08 12:31:28 +01:00
|
|
|
{NULL, NULL, NULL, NULL, NULL, &buNaturalRotCollection, &buNaturalTimeCollection, NULL, NULL, NULL, NULL},
|
|
|
|
{NULL, &buMetricLenCollection, &buMetricAreaCollection, &buMetricVolCollection, &buMetricMassCollection, &buNaturalRotCollection, &buNaturalTimeCollection, &buMetricVelCollection, &buMetricAclCollection, &buCameraLenCollection, &buPowerCollection}, /* metric */
|
|
|
|
{NULL, &buImperialLenCollection, &buImperialAreaCollection, &buImperialVolCollection, &buImperialMassCollection, &buNaturalRotCollection, &buNaturalTimeCollection, &buImperialVelCollection, &buImperialAclCollection, &buCameraLenCollection, &buPowerCollection}, /* imperial */
|
|
|
|
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
};
|
|
|
|
|
2019-04-16 17:02:52 +02:00
|
|
|
/* clang-format on*/
|
2010-09-16 04:19:22 +00:00
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* internal, has some option not exposed */
|
2016-05-03 13:52:07 +10:00
|
|
|
static const bUnitCollection *unit_get_system(int system, int type)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
2010-12-10 04:10:21 +00:00
|
|
|
assert((system > -1) && (system < UNIT_SYSTEM_TOT) && (type > -1) && (type < B_UNIT_TYPE_TOT));
|
added time units, currently only used when metric or imperial are enabled.
long/short units...
day,d, hour,hr,h, minute,min,m, second,sec,s, millisecond,ms, microsecond,us
Also may fix some bugs that were reported.
Note, to convert fps to time evil_C needs to be used to get the scene.
2009-08-12 05:20:16 +00:00
|
|
|
return bUnitSystems[system][type]; /* select system to use, metric/imperial/other? */
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static const bUnitDef *unit_default(const bUnitCollection *usys)
|
2009-08-12 07:23:10 +00:00
|
|
|
{
|
|
|
|
return &usys->units[usys->base_unit];
|
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static const bUnitDef *unit_best_fit(
|
|
|
|
double value, const bUnitCollection *usys, const bUnitDef *unit_start, int suppress)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitDef *unit;
|
2012-03-10 14:20:55 +00:00
|
|
|
double value_abs = value > 0.0 ? value : -value;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
for (unit = unit_start ? unit_start : usys->units; unit->name; unit++) {
|
2009-08-12 14:11:53 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (suppress && (unit->flag & B_UNIT_DEF_SUPPRESS))
|
2009-08-12 14:11:53 +00:00
|
|
|
continue;
|
|
|
|
|
2018-09-24 17:27:41 +02:00
|
|
|
/* scale down scalar so 1cm doesn't convert to 10mm because of float error */
|
2013-05-30 21:17:50 +00:00
|
|
|
if (UNLIKELY(unit->flag & B_UNIT_DEF_TENTH)) {
|
|
|
|
if (value_abs >= unit->scalar * (0.1 - EPS)) {
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (value_abs >= unit->scalar * (1.0 - EPS)) {
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
}
|
2009-08-12 14:11:53 +00:00
|
|
|
}
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2009-08-12 07:23:10 +00:00
|
|
|
return unit_default(usys);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* convert into 2 units and 2 values for "2ft, 3inch" syntax */
|
2016-05-03 13:52:07 +10:00
|
|
|
static void unit_dual_convert(
|
|
|
|
double value, const bUnitCollection *usys,
|
|
|
|
bUnitDef const **r_unit_a, bUnitDef const **r_unit_b,
|
2018-10-03 10:20:16 +02:00
|
|
|
double *r_value_a, double *r_value_b,
|
2018-10-04 09:23:58 +10:00
|
|
|
const bUnitDef *main_unit)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
2018-10-03 10:20:16 +02:00
|
|
|
const bUnitDef *unit;
|
|
|
|
if (main_unit) unit = main_unit;
|
|
|
|
else unit = unit_best_fit(value, usys, NULL, 1);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
*r_value_a = (value < 0.0 ? ceil : floor)(value / unit->scalar) * unit->scalar;
|
|
|
|
*r_value_b = value - (*r_value_a);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
*r_unit_a = unit;
|
|
|
|
*r_unit_b = unit_best_fit(*r_value_b, usys, *r_unit_a, 1);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static size_t unit_as_string(char *str, int len_max, double value, int prec, const bUnitCollection *usys,
|
Support units in modal numinput
Summary:
This completly changes the way modal numinput is handled. Now, edited expression is a string, which then gets unit- and py-evaluated to get a float value.
We gain many power and flexibility, but lose a few "shortcuts" like '-' to negate, or '/' to inverse (if they are really needed, we still can add them with modifiers, like e.g. ctrl-/ or so).
Features:
- units (cm, ", deg, etc.).
- basic operations from python/BKE_unit (+, *, **, etc.), and math constants and functions (pi, sin, etc.).
- you can navigate in edited value (left/right key, ctrl to move by block) and insert/delete chars, e.g. to fix a typo without having to rewrite everything.
- you can go to next/previous value with (ctrl-)TAB key.
- As before, hitting backspace after having deleted all leading chars will first reset the edited value to init state, and on second press, the whole "modal numinput" editing will be cancelled, going back to usual transform with mouse.
Notes:
- Did not touch to how values are shown in header when modal numinput is not enabled (would do that in another commit), so this is still quite inconsistent.
- Added back radian support in BKE_unit.
- Added arcminute/arcsecond to BKE_unit.
(those unit changes affect all angle UI controls, btw, so you can now enter radians or longitude/latitude values when in degrees units).
Related to T37600.
Reviewers: brecht, campbellbarton, carter2422
Reviewed By: brecht, campbellbarton, carter2422
Thanks everybody!
Differential Revision: http://developer.blender.org/D61
2013-12-21 17:11:43 +01:00
|
|
|
/* non exposed options */
|
2014-07-12 09:07:51 +10:00
|
|
|
const bUnitDef *unit, char pad)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
|
|
|
double value_conv;
|
Support units in modal numinput
Summary:
This completly changes the way modal numinput is handled. Now, edited expression is a string, which then gets unit- and py-evaluated to get a float value.
We gain many power and flexibility, but lose a few "shortcuts" like '-' to negate, or '/' to inverse (if they are really needed, we still can add them with modifiers, like e.g. ctrl-/ or so).
Features:
- units (cm, ", deg, etc.).
- basic operations from python/BKE_unit (+, *, **, etc.), and math constants and functions (pi, sin, etc.).
- you can navigate in edited value (left/right key, ctrl to move by block) and insert/delete chars, e.g. to fix a typo without having to rewrite everything.
- you can go to next/previous value with (ctrl-)TAB key.
- As before, hitting backspace after having deleted all leading chars will first reset the edited value to init state, and on second press, the whole "modal numinput" editing will be cancelled, going back to usual transform with mouse.
Notes:
- Did not touch to how values are shown in header when modal numinput is not enabled (would do that in another commit), so this is still quite inconsistent.
- Added back radian support in BKE_unit.
- Added arcminute/arcsecond to BKE_unit.
(those unit changes affect all angle UI controls, btw, so you can now enter radians or longitude/latitude values when in degrees units).
Related to T37600.
Reviewers: brecht, campbellbarton, carter2422
Reviewed By: brecht, campbellbarton, carter2422
Thanks everybody!
Differential Revision: http://developer.blender.org/D61
2013-12-21 17:11:43 +01:00
|
|
|
size_t len, i;
|
2012-03-10 14:20:55 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (unit) {
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* use unit without finding the best one */
|
|
|
|
}
|
2012-02-23 02:17:50 +00:00
|
|
|
else if (value == 0.0) {
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* use the default units since there is no way to convert */
|
2012-03-10 14:20:55 +00:00
|
|
|
unit = unit_default(usys);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-10 14:20:55 +00:00
|
|
|
unit = unit_best_fit(value, usys, NULL, 1);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
value_conv = value / unit->scalar;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2017-07-31 15:40:26 +02:00
|
|
|
/* Adjust precision to expected number of significant digits.
|
|
|
|
* Note that here, we shall not have to worry about very big/small numbers, units are expected to replace
|
|
|
|
* 'scientific notation' in those cases. */
|
2017-08-01 16:35:07 +02:00
|
|
|
prec -= integer_digits_d(value_conv);
|
2017-07-31 15:40:26 +02:00
|
|
|
CLAMP(prec, 0, 6);
|
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* Convert to a string */
|
2015-04-22 05:37:22 +10:00
|
|
|
len = BLI_snprintf_rlen(str, len_max, "%.*f", prec, value_conv);
|
2012-03-10 14:20:55 +00:00
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* Add unit prefix and strip zeros */
|
|
|
|
|
2009-08-13 17:05:27 +00:00
|
|
|
/* replace trailing zero's with spaces
|
2012-09-26 20:05:38 +00:00
|
|
|
* so the number is less complicated but alignment in a button wont
|
2009-08-13 17:05:27 +00:00
|
|
|
* jump about while dragging */
|
2012-03-10 14:20:55 +00:00
|
|
|
i = len - 1;
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2013-04-08 18:55:08 +00:00
|
|
|
if (prec > 0) {
|
|
|
|
while (i > 0 && str[i] == '0') { /* 4.300 -> 4.3 */
|
|
|
|
str[i--] = pad;
|
|
|
|
}
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2013-04-08 18:55:08 +00:00
|
|
|
if (i > 0 && str[i] == '.') { /* 10. -> 10 */
|
|
|
|
str[i--] = pad;
|
|
|
|
}
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
2012-03-10 14:20:55 +00:00
|
|
|
|
2009-08-13 17:05:27 +00:00
|
|
|
/* Now add the suffix */
|
2012-03-10 14:20:55 +00:00
|
|
|
if (i < len_max) {
|
|
|
|
int j = 0;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
i++;
|
2012-03-10 14:20:55 +00:00
|
|
|
while (unit->name_short[j] && (i < len_max)) {
|
|
|
|
str[i++] = unit->name_short[j++];
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:27:41 +02:00
|
|
|
/* terminate no matter what's done with padding above */
|
2012-02-23 02:17:50 +00:00
|
|
|
if (i >= len_max)
|
2012-03-10 14:20:55 +00:00
|
|
|
i = len_max - 1;
|
2009-08-13 17:05:27 +00:00
|
|
|
|
|
|
|
str[i] = '\0';
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
static bool unit_should_be_split(int type)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
2018-10-03 10:20:16 +02:00
|
|
|
return ELEM(type, B_UNIT_LENGTH, B_UNIT_MASS, B_UNIT_TIME, B_UNIT_CAMERA);
|
|
|
|
}
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
typedef struct {
|
|
|
|
int system;
|
|
|
|
int rotation;
|
|
|
|
/* USER_UNIT_ADAPTIVE means none, otherwise the value is the index in the collection */
|
|
|
|
int length;
|
|
|
|
int mass;
|
|
|
|
int time;
|
|
|
|
} PreferredUnits;
|
2012-03-10 14:20:55 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
static PreferredUnits preferred_units_from_UnitSettings(const UnitSettings *settings)
|
|
|
|
{
|
|
|
|
PreferredUnits units = { 0 };
|
|
|
|
units.system = settings->system;
|
|
|
|
units.rotation = settings->system_rotation;
|
|
|
|
units.length = settings->length_unit;
|
|
|
|
units.mass = settings->mass_unit;
|
|
|
|
units.time = settings->time_unit;
|
|
|
|
return units;
|
|
|
|
}
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
static size_t unit_as_string_splitted(
|
|
|
|
char *str, int len_max, double value, int prec,
|
|
|
|
const bUnitCollection *usys, const bUnitDef *main_unit)
|
|
|
|
{
|
|
|
|
const bUnitDef *unit_a, *unit_b;
|
|
|
|
double value_a, value_b;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
unit_dual_convert(value, usys, &unit_a, &unit_b, &value_a, &value_b, main_unit);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
/* check the 2 is a smaller unit */
|
|
|
|
if (unit_b > unit_a) {
|
|
|
|
size_t i;
|
|
|
|
i = unit_as_string(str, len_max, value_a, prec, usys, unit_a, '\0');
|
2017-08-01 16:35:07 +02:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
prec -= integer_digits_d(value_a / unit_b->scalar) - integer_digits_d(value_b / unit_b->scalar);
|
|
|
|
prec = max_ii(prec, 0);
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
/* is there enough space for at least 1 char of the next unit? */
|
|
|
|
if (i + 2 < len_max) {
|
|
|
|
str[i++] = ' ';
|
|
|
|
|
|
|
|
/* use low precision since this is a smaller unit */
|
|
|
|
i += unit_as_string(str + i, len_max - i, value_b, prec, usys, unit_b, '\0');
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
2018-10-03 10:20:16 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_valid_unit_collection(const bUnitCollection *usys)
|
|
|
|
{
|
|
|
|
return usys != NULL && usys->units[0].name != NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:20:22 +01:00
|
|
|
static const bUnitDef *get_preferred_display_unit_if_used(int type, PreferredUnits units)
|
2018-10-03 10:20:16 +02:00
|
|
|
{
|
|
|
|
const bUnitCollection *usys = unit_get_system(units.system, type);
|
|
|
|
if (!is_valid_unit_collection(usys)) return NULL;
|
|
|
|
|
|
|
|
int max_offset = usys->length - 1;
|
|
|
|
|
2018-10-04 09:23:58 +10:00
|
|
|
switch (type) {
|
|
|
|
case B_UNIT_LENGTH:
|
|
|
|
case B_UNIT_AREA:
|
|
|
|
case B_UNIT_VOLUME:
|
|
|
|
if (units.length == USER_UNIT_ADAPTIVE) return NULL;
|
|
|
|
return usys->units + MIN2(units.length, max_offset);
|
|
|
|
case B_UNIT_MASS:
|
|
|
|
if (units.mass == USER_UNIT_ADAPTIVE) return NULL;
|
|
|
|
return usys->units + MIN2(units.mass, max_offset);
|
|
|
|
case B_UNIT_TIME:
|
|
|
|
if (units.time == USER_UNIT_ADAPTIVE) return NULL;
|
|
|
|
return usys->units + MIN2(units.time, max_offset);
|
|
|
|
case B_UNIT_ROTATION:
|
|
|
|
if (units.rotation == 0) return usys->units + 0;
|
|
|
|
else if (units.rotation == USER_UNIT_ROT_RADIANS) return usys->units + 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-03 10:20:16 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the length of the generated string. */
|
|
|
|
static size_t unit_as_string_main(
|
|
|
|
char *str, int len_max, double value, int prec,
|
|
|
|
int type, bool split, bool pad, PreferredUnits units)
|
|
|
|
{
|
|
|
|
const bUnitCollection *usys = unit_get_system(units.system, type);
|
|
|
|
const bUnitDef *main_unit = NULL;
|
|
|
|
|
|
|
|
if (!is_valid_unit_collection(usys)) {
|
|
|
|
usys = &buDummyCollection;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
2018-10-03 10:20:16 +02:00
|
|
|
else {
|
2019-01-08 19:20:22 +01:00
|
|
|
main_unit = get_preferred_display_unit_if_used(type, units);
|
2018-10-03 10:20:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (split && unit_should_be_split(type)) {
|
|
|
|
int length = unit_as_string_splitted(str, len_max, value, prec, usys, main_unit);
|
|
|
|
/* failed when length is negative, fallback to no split */
|
|
|
|
if (length >= 0) return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return unit_as_string(str, len_max, value, prec, usys, main_unit, pad ? ' ' : '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bUnit_AsString(char *str, int len_max, double value, int prec, int system, int type, bool split, bool pad)
|
|
|
|
{
|
|
|
|
PreferredUnits units;
|
|
|
|
units.system = system;
|
|
|
|
units.rotation = 0;
|
|
|
|
units.length = USER_UNIT_ADAPTIVE;
|
|
|
|
units.mass = USER_UNIT_ADAPTIVE;
|
|
|
|
units.time = USER_UNIT_ADAPTIVE;
|
|
|
|
return unit_as_string_main(str, len_max, value, prec, type, split, pad, units);
|
|
|
|
}
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
size_t bUnit_AsString2(char *str, int len_max, double value, int prec, int type, const UnitSettings *settings, bool pad)
|
|
|
|
{
|
|
|
|
bool do_split = (settings->flag & USER_UNIT_OPT_SPLIT) != 0;
|
|
|
|
PreferredUnits units = preferred_units_from_UnitSettings(settings);
|
|
|
|
return unit_as_string_main(str, len_max, value, prec, type, do_split, pad, units);
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 12:31:15 +02:00
|
|
|
BLI_INLINE bool isalpha_or_utf8(const int ch)
|
2012-04-10 02:51:24 +00:00
|
|
|
{
|
|
|
|
return (ch >= 128 || isalpha(ch));
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:39:28 +01:00
|
|
|
static const char *unit_find_str(const char *str, const char *substr, bool case_sensitive)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
2012-02-23 02:17:50 +00:00
|
|
|
if (substr && substr[0] != '\0') {
|
Fix T47661: cm (centimeter) unit breaks m (meter) unit in Metric.
`m` unit when used after `cm`/`mm`/etc. ones would get ignored, and the alt version of miles
would be used instead.
The root of the issue is that, in `unit_find_str`, once we get a 'hit' for a unit, we check
it's actual unit (since 'm' would also hit on 'cm', 'mm', etc.). In case that hit is not a
valid unit one, we would just return NULL, breaking the cycle of checks over that unit, and
hence missing all later usages of it.
So now, in case we have an 'invalid unit hit', we immediately retry to find it within remaining string.
2016-03-02 17:57:03 +01:00
|
|
|
while (true) {
|
2019-02-08 12:31:28 +01:00
|
|
|
/* Unit detection is case insensitive. */
|
2019-02-17 16:39:28 +01:00
|
|
|
const char *str_found;
|
|
|
|
if (case_sensitive)
|
|
|
|
str_found = strstr(str, substr);
|
|
|
|
else
|
|
|
|
str_found = BLI_strcasestr(str, substr);
|
Fix T47661: cm (centimeter) unit breaks m (meter) unit in Metric.
`m` unit when used after `cm`/`mm`/etc. ones would get ignored, and the alt version of miles
would be used instead.
The root of the issue is that, in `unit_find_str`, once we get a 'hit' for a unit, we check
it's actual unit (since 'm' would also hit on 'cm', 'mm', etc.). In case that hit is not a
valid unit one, we would just return NULL, breaking the cycle of checks over that unit, and
hence missing all later usages of it.
So now, in case we have an 'invalid unit hit', we immediately retry to find it within remaining string.
2016-03-02 17:57:03 +01:00
|
|
|
|
|
|
|
if (str_found) {
|
|
|
|
/* Previous char cannot be a letter. */
|
|
|
|
if (str_found == str ||
|
2016-03-09 19:31:44 +11:00
|
|
|
/* weak unicode support!, so "µm" won't match up be replaced by "m"
|
|
|
|
* since non ascii utf8 values will NEVER return true */
|
|
|
|
isalpha_or_utf8(*BLI_str_prev_char_utf8(str_found)) == 0)
|
Fix T47661: cm (centimeter) unit breaks m (meter) unit in Metric.
`m` unit when used after `cm`/`mm`/etc. ones would get ignored, and the alt version of miles
would be used instead.
The root of the issue is that, in `unit_find_str`, once we get a 'hit' for a unit, we check
it's actual unit (since 'm' would also hit on 'cm', 'mm', etc.). In case that hit is not a
valid unit one, we would just return NULL, breaking the cycle of checks over that unit, and
hence missing all later usages of it.
So now, in case we have an 'invalid unit hit', we immediately retry to find it within remaining string.
2016-03-02 17:57:03 +01:00
|
|
|
{
|
|
|
|
/* next char cannot be alphanum */
|
|
|
|
int len_name = strlen(substr);
|
|
|
|
|
|
|
|
if (!isalpha_or_utf8(*(str_found + len_name))) {
|
|
|
|
return str_found;
|
|
|
|
}
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
Fix T47661: cm (centimeter) unit breaks m (meter) unit in Metric.
`m` unit when used after `cm`/`mm`/etc. ones would get ignored, and the alt version of miles
would be used instead.
The root of the issue is that, in `unit_find_str`, once we get a 'hit' for a unit, we check
it's actual unit (since 'm' would also hit on 'cm', 'mm', etc.). In case that hit is not a
valid unit one, we would just return NULL, breaking the cycle of checks over that unit, and
hence missing all later usages of it.
So now, in case we have an 'invalid unit hit', we immediately retry to find it within remaining string.
2016-03-02 17:57:03 +01:00
|
|
|
/* If str_found is not a valid unit, we have to check further in the string... */
|
2016-03-02 18:09:32 +01:00
|
|
|
for (str_found++; isalpha_or_utf8(*str_found); str_found++);
|
|
|
|
str = str_found;
|
Fix T47661: cm (centimeter) unit breaks m (meter) unit in Metric.
`m` unit when used after `cm`/`mm`/etc. ones would get ignored, and the alt version of miles
would be used instead.
The root of the issue is that, in `unit_find_str`, once we get a 'hit' for a unit, we check
it's actual unit (since 'm' would also hit on 'cm', 'mm', etc.). In case that hit is not a
valid unit one, we would just return NULL, breaking the cycle of checks over that unit, and
hence missing all later usages of it.
So now, in case we have an 'invalid unit hit', we immediately retry to find it within remaining string.
2016-03-02 17:57:03 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-13 17:05:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-17 12:48:56 +00:00
|
|
|
/* Note that numbers are added within brackets
|
|
|
|
* ") " - is used to detect numbers we added so we can detect if commas need to be added
|
|
|
|
*
|
2019-01-11 10:50:21 +11:00
|
|
|
* "1m1cm+2mm" - Original value
|
|
|
|
* "1*1#1*0.01#+2*0.001#" - Replace numbers
|
|
|
|
* "1*1+1*0.01 +2*0.001 " - Add add signs if ( + - * / | & ~ < > ^ ! = % ) not found in between
|
2009-08-17 12:48:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-08-20 12:12:03 +02:00
|
|
|
/* not too strict, (+ - * /) are most common */
|
2014-02-05 22:36:15 +11:00
|
|
|
static bool ch_is_op(char op)
|
2009-08-17 12:48:56 +00:00
|
|
|
{
|
2012-03-10 14:20:55 +00:00
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '*':
|
|
|
|
case '/':
|
|
|
|
case '|':
|
|
|
|
case '&':
|
|
|
|
case '~':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '^':
|
|
|
|
case '!':
|
|
|
|
case '=':
|
|
|
|
case '%':
|
2014-08-20 12:31:15 +02:00
|
|
|
return true;
|
2012-03-10 14:20:55 +00:00
|
|
|
default:
|
2014-08-20 12:31:15 +02:00
|
|
|
return false;
|
2009-08-17 12:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static int unit_scale_str(char *str, int len_max, char *str_tmp, double scale_pref, const bUnitDef *unit,
|
2019-02-17 16:39:28 +01:00
|
|
|
const char *replace_str, bool case_sensitive)
|
2009-08-13 17:05:27 +00:00
|
|
|
{
|
2009-08-14 12:29:55 +00:00
|
|
|
char *str_found;
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2019-02-17 16:39:28 +01:00
|
|
|
if ((len_max > 0) && (str_found = (char *)unit_find_str(str, replace_str, case_sensitive))) {
|
2011-12-21 22:56:06 +00:00
|
|
|
/* XXX - investigate, does not respect len_max properly */
|
|
|
|
|
2009-08-13 17:05:27 +00:00
|
|
|
int len, len_num, len_name, len_move, found_ofs;
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
found_ofs = (int)(str_found - str);
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
len = strlen(str);
|
2009-08-13 17:05:27 +00:00
|
|
|
|
|
|
|
len_name = strlen(replace_str);
|
2012-03-10 14:20:55 +00:00
|
|
|
len_move = (len - (found_ofs + len_name)) + 1; /* 1+ to copy the string terminator */
|
2014-06-17 16:06:12 +02:00
|
|
|
len_num = BLI_snprintf(str_tmp, TEMP_STR_SIZE, "*%.9g"SEP_STR, unit->scalar / scale_pref); /* # removed later */
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (len_num > len_max)
|
2012-03-10 14:20:55 +00:00
|
|
|
len_num = len_max;
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
if (found_ofs + len_num + len_move > len_max) {
|
2009-08-13 17:05:27 +00:00
|
|
|
/* can't move the whole string, move just as much as will fit */
|
2012-03-10 14:20:55 +00:00
|
|
|
len_move -= (found_ofs + len_num + len_move) - len_max;
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
if (len_move > 0) {
|
2009-08-13 17:05:27 +00:00
|
|
|
/* resize the last part of the string */
|
2012-03-10 14:20:55 +00:00
|
|
|
memmove(str_found + len_num, str_found + len_name, len_move); /* may grow or shrink the string */
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
if (found_ofs + len_num > len_max) {
|
2009-08-13 17:05:27 +00:00
|
|
|
/* not even the number will fit into the string, only copy part of it */
|
2012-03-10 14:20:55 +00:00
|
|
|
len_num -= (found_ofs + len_num) - len_max;
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (len_num > 0) {
|
2009-08-13 17:05:27 +00:00
|
|
|
/* its possible none of the number could be copied in */
|
|
|
|
memcpy(str_found, str_tmp, len_num); /* without the string terminator */
|
|
|
|
}
|
|
|
|
|
2009-08-14 12:29:55 +00:00
|
|
|
/* since the null terminator wont be moved if the stringlen_max
|
|
|
|
* was not long enough to fit everything in it */
|
2012-03-10 14:20:55 +00:00
|
|
|
str[len_max - 1] = '\0';
|
2009-08-14 12:29:55 +00:00
|
|
|
return found_ofs + len_num;
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
|
|
|
return 0;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static int unit_replace(char *str, int len_max, char *str_tmp, double scale_pref, const bUnitDef *unit)
|
2012-03-10 14:20:55 +00:00
|
|
|
{
|
2019-02-17 16:39:28 +01:00
|
|
|
const bool case_sensitive = (unit->flag & B_UNIT_DEF_CASE_SENSITIVE) != 0;
|
2012-03-10 14:20:55 +00:00
|
|
|
int ofs = 0;
|
2019-02-17 16:39:28 +01:00
|
|
|
ofs += unit_scale_str(str + ofs, len_max - ofs, str_tmp, scale_pref, unit, unit->name_short, case_sensitive);
|
|
|
|
ofs += unit_scale_str(str + ofs, len_max - ofs, str_tmp, scale_pref, unit, unit->name_plural, false);
|
|
|
|
ofs += unit_scale_str(str + ofs, len_max - ofs, str_tmp, scale_pref, unit, unit->name_alt, case_sensitive);
|
|
|
|
ofs += unit_scale_str(str + ofs, len_max - ofs, str_tmp, scale_pref, unit, unit->name, false);
|
2009-08-14 12:29:55 +00:00
|
|
|
return ofs;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static bool unit_find(const char *str, const bUnitDef *unit)
|
2009-08-13 17:05:27 +00:00
|
|
|
{
|
2019-02-17 16:39:28 +01:00
|
|
|
const bool case_sensitive = (unit->flag & B_UNIT_DEF_CASE_SENSITIVE) != 0;
|
|
|
|
if (unit_find_str(str, unit->name_short, case_sensitive)) return true;
|
|
|
|
if (unit_find_str(str, unit->name_plural, false)) return true;
|
|
|
|
if (unit_find_str(str, unit->name_alt, case_sensitive)) return true;
|
|
|
|
if (unit_find_str(str, unit->name, false)) return true;
|
2009-08-13 17:05:27 +00:00
|
|
|
|
2014-08-20 12:31:15 +02:00
|
|
|
return false;
|
2009-08-13 17:05:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
static const bUnitDef *unit_detect_from_str(const bUnitCollection *usys, const char *str, const char *str_prev)
|
2014-08-20 12:12:03 +02:00
|
|
|
{
|
|
|
|
/* Try to find a default unit from current or previous string.
|
|
|
|
* This allows us to handle cases like 2 + 2mm, people would expect to get 4mm, not 2.002m!
|
|
|
|
* Note this does not handle corner cases like 2 + 2cm + 1 + 2.5mm... We can't support everything. */
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitDef *unit = NULL;
|
2014-08-20 12:12:03 +02:00
|
|
|
|
|
|
|
/* see which units the new value has */
|
|
|
|
for (unit = usys->units; unit->name; unit++) {
|
|
|
|
if (unit_find(str, unit))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Else, try to infer the default unit from the previous string. */
|
|
|
|
if (str_prev && (unit == NULL || unit->name == NULL)) {
|
|
|
|
/* see which units the original value had */
|
|
|
|
for (unit = usys->units; unit->name; unit++) {
|
|
|
|
if (unit_find(str_prev, unit))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Else, fall back to default unit. */
|
|
|
|
if (unit == NULL || unit->name == NULL) {
|
|
|
|
unit = unit_default(usys);
|
|
|
|
}
|
|
|
|
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
2019-01-09 10:39:31 +01:00
|
|
|
bool bUnit_ContainsUnit(const char *str, int type)
|
2018-10-03 10:20:16 +02:00
|
|
|
{
|
2019-01-09 10:39:31 +01:00
|
|
|
for (int system = 0; system < UNIT_SYSTEM_TOT; system++) {
|
|
|
|
const bUnitCollection *usys = unit_get_system(system, type);
|
|
|
|
if (!is_valid_unit_collection(usys)) continue;
|
2018-10-03 10:20:16 +02:00
|
|
|
|
2019-01-09 10:39:31 +01:00
|
|
|
for (int i = 0; i < usys->length; i++) {
|
|
|
|
if (unit_find(str, usys->units + i)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-03 10:20:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:20:22 +01:00
|
|
|
double bUnit_PreferredInputUnitScalar(const struct UnitSettings *settings, int type)
|
2018-10-03 10:20:16 +02:00
|
|
|
{
|
|
|
|
PreferredUnits units = preferred_units_from_UnitSettings(settings);
|
2019-01-08 19:20:22 +01:00
|
|
|
const bUnitDef *unit = get_preferred_display_unit_if_used(type, units);
|
|
|
|
if (unit) return unit->scalar;
|
|
|
|
else return bUnit_BaseScalar(units.system, type);
|
2018-10-03 10:20:16 +02:00
|
|
|
}
|
|
|
|
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
/* make a copy of the string that replaces the units with numbers
|
|
|
|
* this is used before parsing
|
|
|
|
* This is only used when evaluating user input and can afford to be a bit slower
|
2012-03-10 14:20:55 +00:00
|
|
|
*
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
* This is to be used before python evaluation so..
|
|
|
|
* 10.1km -> 10.1*1000.0
|
|
|
|
* ...will be resolved by python.
|
2012-03-10 14:20:55 +00:00
|
|
|
*
|
2014-08-20 12:12:03 +02:00
|
|
|
* values will be split by an add sign
|
|
|
|
* 5'2" -> 5*0.3048 + 2*0.0254
|
2009-08-17 12:48:56 +00:00
|
|
|
*
|
2009-08-12 07:23:10 +00:00
|
|
|
* str_prev is optional, when valid it is used to get a base unit when none is set.
|
|
|
|
*
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
* return true of a change was made.
|
|
|
|
*/
|
2014-08-20 12:31:15 +02:00
|
|
|
bool bUnit_ReplaceString(char *str, int len_max, const char *str_prev, double scale_pref, int system, int type)
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
{
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitCollection *usys = unit_get_system(system, type);
|
2018-10-03 10:20:16 +02:00
|
|
|
if (!is_valid_unit_collection(usys)) return false;
|
2009-08-12 07:23:10 +00:00
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitDef *unit = NULL, *default_unit;
|
2014-08-20 12:12:03 +02:00
|
|
|
double scale_pref_base = scale_pref;
|
2009-08-13 17:05:27 +00:00
|
|
|
char str_tmp[TEMP_STR_SIZE];
|
2014-08-20 12:31:15 +02:00
|
|
|
bool changed = false;
|
2009-08-12 05:53:12 +00:00
|
|
|
|
2014-08-20 12:12:03 +02:00
|
|
|
/* Try to find a default unit from current or previous string. */
|
|
|
|
default_unit = unit_detect_from_str(usys, str, str_prev);
|
|
|
|
|
|
|
|
/* We apply the default unit to the whole expression (default unit is now the reference '1.0' one). */
|
|
|
|
scale_pref_base *= default_unit->scalar;
|
|
|
|
|
|
|
|
/* Apply the default unit on the whole expression, this allows to handle nasty cases like '2+2in'. */
|
|
|
|
if (BLI_snprintf(str_tmp, sizeof(str_tmp), "(%s)*%.9g", str, default_unit->scalar) < sizeof(str_tmp)) {
|
|
|
|
strncpy(str, str_tmp, len_max);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* BLI_snprintf would not fit into str_tmp, cant do much in this case
|
|
|
|
* check for this because otherwise bUnit_ReplaceString could call its self forever */
|
2014-08-20 12:31:15 +02:00
|
|
|
return changed;
|
2014-08-20 12:12:03 +02:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
for (unit = usys->units; unit->name; unit++) {
|
2012-03-09 00:41:09 +00:00
|
|
|
/* in case there are multiple instances */
|
2014-08-20 12:12:03 +02:00
|
|
|
while (unit_replace(str, len_max, str_tmp, scale_pref_base, unit))
|
2013-11-26 06:39:14 +11:00
|
|
|
changed = true;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
2012-03-10 14:20:55 +00:00
|
|
|
unit = NULL;
|
2009-08-12 07:23:10 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
/* try other unit systems now, so we can evaluate imperial when metric is set for eg. */
|
2014-08-20 12:12:03 +02:00
|
|
|
/* Note that checking other systems at that point means we do not support their units as 'default' one.
|
|
|
|
* In other words, when in metrics, typing '2+2in' will give 2 meters 2 inches, not 4 inches.
|
|
|
|
* I do think this is the desired behavior!
|
|
|
|
*/
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitCollection *usys_iter;
|
2009-08-12 07:23:10 +00:00
|
|
|
int system_iter;
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
for (system_iter = 0; system_iter < UNIT_SYSTEM_TOT; system_iter++) {
|
2009-08-12 07:23:10 +00:00
|
|
|
if (system_iter != system) {
|
2012-03-10 14:20:55 +00:00
|
|
|
usys_iter = unit_get_system(system_iter, type);
|
2010-01-25 06:24:05 +00:00
|
|
|
if (usys_iter) {
|
2012-03-10 14:20:55 +00:00
|
|
|
for (unit = usys_iter->units; unit->name; unit++) {
|
2010-09-22 02:47:08 +00:00
|
|
|
int ofs = 0;
|
2012-03-09 00:41:09 +00:00
|
|
|
/* in case there are multiple instances */
|
2014-08-20 12:12:03 +02:00
|
|
|
while ((ofs = unit_replace(str + ofs, len_max - ofs, str_tmp, scale_pref_base, unit)))
|
2013-11-26 06:39:14 +11:00
|
|
|
changed = true;
|
2009-08-14 12:29:55 +00:00
|
|
|
}
|
2009-08-12 07:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 14:20:55 +00:00
|
|
|
unit = NULL;
|
|
|
|
|
2014-08-20 12:12:03 +02:00
|
|
|
/* replace # with add sign when there is no operator between it and the next number
|
2009-08-17 12:48:56 +00:00
|
|
|
*
|
2014-08-20 12:12:03 +02:00
|
|
|
* "1*1# 3*100# * 3" -> "1*1+ 3*100 * 3"
|
2009-08-17 12:48:56 +00:00
|
|
|
*
|
|
|
|
* */
|
|
|
|
{
|
2012-03-10 14:20:55 +00:00
|
|
|
char *str_found = str;
|
2014-04-27 00:20:13 +10:00
|
|
|
const char *ch = str;
|
2009-08-17 12:48:56 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
while ((str_found = strchr(str_found, SEP_CHR))) {
|
2014-08-20 12:12:03 +02:00
|
|
|
bool op_found = false;
|
2009-08-12 07:23:10 +00:00
|
|
|
|
2014-08-20 12:12:03 +02:00
|
|
|
/* any operators after this? */
|
2012-03-10 14:20:55 +00:00
|
|
|
for (ch = str_found + 1; *ch != '\0'; ch++) {
|
|
|
|
if (*ch == ' ' || *ch == '\t') {
|
2014-08-20 12:12:03 +02:00
|
|
|
continue;
|
2009-08-17 12:48:56 +00:00
|
|
|
}
|
2014-08-20 12:12:03 +02:00
|
|
|
op_found = (ch_is_op(*ch) || ELEM(*ch, ',', ')'));
|
|
|
|
break;
|
2009-08-17 12:48:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 12:12:03 +02:00
|
|
|
/* If found an op, comma or closing parenthesis, no need to insert a '+', else we need it. */
|
|
|
|
*str_found++ = op_found ? ' ' : '+';
|
2009-08-17 12:48:56 +00:00
|
|
|
}
|
2009-08-12 07:23:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-26 06:39:14 +11:00
|
|
|
return changed;
|
user interface units, off by default.
- currently only distances work.
- user preferences, edit section to set the units and scale.
- option to display pairs (nicer for imperial display?)
- support for evaluating multiple comma separated values eg: 2',11" ..or.. 5ft, 4mil
- comma separated expressions/values accumulate 1+1,2**3,4cm/3
- attempted fast conversion from a value to a string so button drawing isn't too slow.
* imperial long/short *
- mile, mi
- yard, yd
- foot, '
- inch, "
- thou, mil
* metric long/short *
kilometer, km
meter, m
centimeter, cm
millimeter, mm
micrometer, um
nanometer, nm
picometer, pm
2009-08-11 18:53:01 +00:00
|
|
|
}
|
2009-08-12 08:16:10 +00:00
|
|
|
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
/* 45µm --> 45um */
|
2011-11-05 11:04:28 +00:00
|
|
|
void bUnit_ToUnitAltName(char *str, int len_max, const char *orig_str, int system, int type)
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
{
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitCollection *usys = unit_get_system(system, type);
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitDef *unit;
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
|
|
|
|
/* find and substitute all units */
|
2012-03-10 14:20:55 +00:00
|
|
|
for (unit = usys->units; unit->name; unit++) {
|
2015-10-05 11:55:52 +02:00
|
|
|
if (len_max > 0 && unit->name_alt) {
|
2019-02-17 16:39:28 +01:00
|
|
|
const bool case_sensitive = (unit->flag & B_UNIT_DEF_CASE_SENSITIVE) != 0;
|
|
|
|
const char *found = unit_find_str(orig_str, unit->name_short, case_sensitive);
|
2012-02-23 02:17:50 +00:00
|
|
|
if (found) {
|
2012-03-10 14:20:55 +00:00
|
|
|
int offset = (int)(found - orig_str);
|
|
|
|
int len_name = 0;
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
|
|
|
|
/* copy everything before the unit */
|
2012-03-10 14:20:55 +00:00
|
|
|
offset = (offset < len_max ? offset : len_max);
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
strncpy(str, orig_str, offset);
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
str += offset;
|
|
|
|
orig_str += offset + strlen(unit->name_short);
|
|
|
|
len_max -= offset;
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
|
|
|
|
/* print the alt_name */
|
2012-02-23 02:17:50 +00:00
|
|
|
if (unit->name_alt)
|
2013-03-14 10:39:18 +00:00
|
|
|
len_name = BLI_strncpy_rlen(str, unit->name_alt, len_max);
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
else
|
2012-03-10 14:20:55 +00:00
|
|
|
len_name = 0;
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
len_name = (len_name < len_max ? len_name : len_max);
|
|
|
|
str += len_name;
|
|
|
|
len_max -= len_name;
|
patch [#23758] Better handling of UTF chars in UNITS fields (lengths, angles, etc.)
from Lorenzo Tozzi (oni_niubbo) with minor edits.
--- from the tracker
The present situation is this: due to bug#22274, during editing, UTF chars are stripped from buttons with a unit associated
(length, angles, etc.).
Example: if the button displays '90°' and you click on it with LMB, the editing string will become '90'.
The problem arises if you use microns: '34µm' becomes '34' that blender interprets as 34 meters. So clicking on a button
and hitting enter won't confirm the previous value, but will change it (very badly also).
Of course nobody is using microns in blender, but the problem will arise when we will implement areas and option 'Separate
Units' will be enabled. The value '2m² 3cm²' will become '2m' during editing.
This patch solves the problem rewriting the string in a smarter way than just stripping the UTF chars: the unit is translated
from unit->name_short ('µm') to unit->name_alt ('um'). So clicking on '34µm' the editing string will become
'34um'.
--- end
note: rather then allowing empty strings in name_alt field I made it so if the unit system was the default one a NULL name_alt will just strip the string, since its the default its not needed.
2010-09-15 17:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finally copy the rest of the string */
|
|
|
|
strncpy(str, orig_str, len_max);
|
|
|
|
}
|
2009-08-12 08:16:10 +00:00
|
|
|
|
2009-08-12 14:11:53 +00:00
|
|
|
double bUnit_ClosestScalar(double value, int system, int type)
|
2009-08-12 08:16:10 +00:00
|
|
|
{
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitCollection *usys = unit_get_system(system, type);
|
|
|
|
const bUnitDef *unit;
|
2009-08-12 08:16:10 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
if (usys == NULL)
|
2009-08-12 08:16:10 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
unit = unit_best_fit(value, usys, NULL, 1);
|
|
|
|
if (unit == NULL)
|
2009-08-12 08:16:10 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-08-12 14:11:53 +00:00
|
|
|
return unit->scalar;
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:02:03 +00:00
|
|
|
double bUnit_BaseScalar(int system, int type)
|
|
|
|
{
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitCollection *usys = unit_get_system(system, type);
|
2019-01-08 19:20:22 +01:00
|
|
|
if (usys) return unit_default(usys)->scalar;
|
|
|
|
else return 1.0;
|
2009-08-12 17:02:03 +00:00
|
|
|
}
|
|
|
|
|
2009-08-12 14:11:53 +00:00
|
|
|
/* external access */
|
2014-08-20 12:31:15 +02:00
|
|
|
bool bUnit_IsValid(int system, int type)
|
2010-09-16 04:19:22 +00:00
|
|
|
{
|
2010-12-10 04:10:21 +00:00
|
|
|
return !(system < 0 || system > UNIT_SYSTEM_TOT || type < 0 || type > B_UNIT_TYPE_TOT);
|
2010-09-16 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
void bUnit_GetSystem(int system, int type, void const **r_usys_pt, int *r_len)
|
2009-08-12 14:11:53 +00:00
|
|
|
{
|
2016-05-03 13:52:07 +10:00
|
|
|
const bUnitCollection *usys = unit_get_system(system, type);
|
|
|
|
*r_usys_pt = usys;
|
2009-08-12 14:11:53 +00:00
|
|
|
|
2012-03-10 14:20:55 +00:00
|
|
|
if (usys == NULL) {
|
2016-05-03 13:52:07 +10:00
|
|
|
*r_len = 0;
|
2009-08-12 14:11:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
*r_len = usys->length;
|
2009-08-12 14:11:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
int bUnit_GetBaseUnit(const void *usys_pt)
|
2010-11-23 17:14:03 +00:00
|
|
|
{
|
|
|
|
return ((bUnitCollection *)usys_pt)->base_unit;
|
|
|
|
}
|
|
|
|
|
2018-10-03 10:20:16 +02:00
|
|
|
int bUnit_GetBaseUnitOfType(int system, int type)
|
|
|
|
{
|
|
|
|
return unit_get_system(system, type)->base_unit;
|
|
|
|
}
|
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
const char *bUnit_GetName(const void *usys_pt, int index)
|
2009-08-12 14:11:53 +00:00
|
|
|
{
|
|
|
|
return ((bUnitCollection *)usys_pt)->units[index].name;
|
|
|
|
}
|
2016-05-03 13:52:07 +10:00
|
|
|
const char *bUnit_GetNameDisplay(const void *usys_pt, int index)
|
2009-08-12 14:11:53 +00:00
|
|
|
{
|
2009-08-17 12:48:56 +00:00
|
|
|
return ((bUnitCollection *)usys_pt)->units[index].name_display;
|
2009-08-12 14:11:53 +00:00
|
|
|
}
|
2018-11-15 14:39:52 +01:00
|
|
|
const char *bUnit_GetIdentifier(const void *usys_pt, int index)
|
|
|
|
{
|
2018-11-15 16:31:26 +01:00
|
|
|
const bUnitDef *unit = ((const bUnitCollection *)usys_pt)->units + index;
|
2018-11-15 14:39:52 +01:00
|
|
|
if (unit->identifier == NULL) {
|
|
|
|
BLI_assert(false && "identifier for this unit is not specified yet");
|
|
|
|
}
|
2018-11-15 16:31:26 +01:00
|
|
|
return unit->identifier;
|
2018-11-15 14:39:52 +01:00
|
|
|
}
|
2009-08-12 14:11:53 +00:00
|
|
|
|
2016-05-03 13:52:07 +10:00
|
|
|
double bUnit_GetScaler(const void *usys_pt, int index)
|
2009-08-12 14:11:53 +00:00
|
|
|
{
|
|
|
|
return ((bUnitCollection *)usys_pt)->units[index].scalar;
|
2009-08-12 08:16:10 +00:00
|
|
|
}
|
2018-10-03 10:20:16 +02:00
|
|
|
|
|
|
|
bool bUnit_IsSuppressed(const void *usys_pt, int index)
|
|
|
|
{
|
|
|
|
return (((bUnitCollection *)usys_pt)->units[index].flag & B_UNIT_DEF_SUPPRESS) != 0;
|
2018-10-04 09:23:58 +10:00
|
|
|
}
|