Beispiel #1
0
/* str must be NUM_STR_REP_LEN * (idx_max + 1) length. */
void outputNumInput(NumInput *n, char *str)
{
	short i, j;
	const int ln = NUM_STR_REP_LEN;
	int prec = 2; /* draw-only, and avoids too much issues with radian->degrees conversion. */

	for (j = 0; j <= n->idx_max; j++) {
		/* if AFFECTALL and no number typed and cursor not on number, use first number */
		i = (n->flag & NUM_AFFECT_ALL && n->idx != j && !(n->val_flag[j] & NUM_EDITED)) ? 0 : j;

		if (n->val_flag[i] & NUM_EDITED) {
			/* Get the best precision, allows us to draw '10.0001' as '10' instead! */
			prec = uiFloatPrecisionCalc(prec, (double)n->val[i]);
			if (i == n->idx) {
				const char *heading_exp = "", *trailing_exp = "";
				char before_cursor[NUM_STR_REP_LEN];
				char val[16];

				if (n->val_flag[i] & NUM_NEGATE) {
					heading_exp = (n->val_flag[i] & NUM_INVERSE) ? "-1/(" : "-(";
					trailing_exp = ")";
				}
				else if (n->val_flag[i] & NUM_INVERSE) {
					heading_exp = "1/(";
					trailing_exp = ")";
				}

				if (n->val_flag[i] & NUM_INVALID) {
					BLI_strncpy(val, "Invalid", sizeof(val));
				}
				else {
					bUnit_AsString(val, sizeof(val), (double)n->val[i], prec,
					               n->unit_sys, n->unit_type[i], true, false);
				}

				BLI_strncpy(before_cursor, n->str, n->str_cur + 1);  /* +1 because of trailing '\0' */
				BLI_snprintf(&str[j * ln], ln, "[%s%s|%s%s] = %s",
				             heading_exp, before_cursor, &n->str[n->str_cur], trailing_exp, val);
			}
			else {
				const char *cur = (i == n->idx) ? "|" : "";
				if (n->unit_use_radians && n->unit_type[i] == B_UNIT_ROTATION) {
					/* Radian exception... */
					BLI_snprintf(&str[j * ln], ln, "%s%.6gr%s", cur, n->val[i], cur);
				}
				else {
					char tstr[NUM_STR_REP_LEN];
					bUnit_AsString(tstr, ln, (double)n->val[i], prec, n->unit_sys, n->unit_type[i], true, false);
					BLI_snprintf(&str[j * ln], ln, "%s%s%s", cur, tstr, cur);
				}
			}
		}
		else {
			const char *cur = (i == n->idx) ? "|" : "";
			BLI_snprintf(&str[j * ln], ln, "%sNONE%s", cur, cur);
		}
		/* We might have cut some multi-bytes utf8 chars (e.g. trailing '°' of degrees values can become only 'A')... */
		BLI_utf8_invalid_strip(&str[j * ln], strlen(&str[j * ln]));
	}
}
Beispiel #2
0
int new_id(ListBase *lb, ID *id, const char *tname)
{
    int result;
    char name[MAX_ID_NAME - 2];

    /* if library, don't rename */
    if (id->lib) return 0;

    /* if no libdata given, look up based on ID */
    if (lb == NULL) lb = which_libbase(G.main, GS(id->name));

    /* if no name given, use name of current ID
     * else make a copy (tname args can be const) */
    if (tname == NULL)
        tname = id->name + 2;

    strncpy(name, tname, sizeof(name) - 1);

    /* if result > MAX_ID_NAME-3, strncpy don't put the final '\0' to name.
     * easier to assign each time then to check if its needed */
    name[sizeof(name) - 1] = 0;

    if (name[0] == '\0') {
        /* disallow empty names */
        strcpy(name, ID_FALLBACK_NAME);
    }
    else {
        /* disallow non utf8 chars,
         * the interface checks for this but new ID's based on file names don't */
        BLI_utf8_invalid_strip(name, strlen(name));
    }

    result = check_for_dupid(lb, id, name);
    strcpy(id->name + 2, name);

    /* This was in 2.43 and previous releases
     * however all data in blender should be sorted, not just duplicate names
     * sorting should not hurt, but noting just incause it alters the way other
     * functions work, so sort every time */
#if 0
    if (result)
        id_sort_by_name(lb, id);
#endif

    id_sort_by_name(lb, id);

    return result;
}