/** * @brief display value of a node property from the command line */ static void UI_NodeGetProperty_f (void) { uiNode_t* node; const value_t* property; const char* sValue; float fValue; if (Cmd_Argc() != 2) { Com_Printf("Usage: %s <nodepath@prop>\n", Cmd_Argv(0)); return; } UI_ReadNodePath(Cmd_Argv(1), nullptr, &node, &property); if (node == nullptr) { Com_Printf("UI_NodeGetProperty_f: Node from path '%s' doesn't exist\n", Cmd_Argv(1)); return; } if (property == nullptr) { Com_Printf("UI_NodeGetProperty_f: Property from path '%s' doesn't exist\n", Cmd_Argv(1)); return; } /* check string value */ sValue = UI_GetStringFromNodeProperty(node, property); if (sValue) { Com_Printf("\"%s\" is \"%s\"\n", Cmd_Argv(1), sValue); return; } /* check float value */ fValue = UI_GetFloatFromNodeProperty(node, property); Com_Printf("\"%s\" is \"%f\"\n", Cmd_Argv(1), fValue); }
/** * @return A float value, else 0 */ float UI_GetFloatFromExpression (uiAction_t *expression, const uiCallContext_t *context) { switch (expression->type & EA_HIGHT_MASK) { case EA_VALUE: switch (expression->type) { case EA_VALUE_VAR: { uiValue_t *variable = UI_GetVariable(context, expression->d.terminal.d1.integer); switch (variable->type) { case EA_VALUE_STRING: if (variable->value.string == nullptr) { Com_Printf("UI_GetFloatFromExpression: String variable not initialized. '0' returned"); return 0; } return atof(variable->value.string); case EA_VALUE_FLOAT: return variable->value.number; case EA_VALUE_CVAR: { cvar_t *cvar = variable->value.cvar; if (cvar == nullptr) { Com_Printf("UI_GetFloatFromExpression: Cvar variable not initialized. '0' returned"); return 0; } return cvar->value; } default: Com_Printf("UI_GetFloatFromExpression: Unsupported variable type: %i. '0' returned", variable->type); return 0; } } case EA_VALUE_STRING: case EA_VALUE_STRING_WITHINJECTION: { const char* string = expression->d.terminal.d1.constString; if (expression->type == EA_VALUE_STRING_WITHINJECTION) string = UI_GenInjectedString(string, false, context); return atof(string); } case EA_VALUE_FLOAT: return expression->d.terminal.d1.number; case EA_VALUE_CVARNAME: case EA_VALUE_CVARNAME_WITHINJECTION: { cvar_t *cvar = nullptr; const char *cvarName = expression->d.terminal.d1.constString; if (expression->type == EA_VALUE_CVARNAME_WITHINJECTION) cvarName = UI_GenInjectedString(cvarName, false, context); cvar = Cvar_Get(cvarName, "", 0, "Cvar from UI script expression"); return cvar->value; } case EA_VALUE_PATHPROPERTY: case EA_VALUE_PATHPROPERTY_WITHINJECTION: { uiNode_t *node; const value_t *property; node = UI_GetNodeFromExpression(expression, context, &property); if (!node) { Com_Printf("UI_GetFloatFromParam: Node wasn't found; '0'\n"); return 0; } if (!property) { Com_Printf("UI_GetFloatFromParam: Property wasn't found; '0' returned\n"); return 0; } return UI_GetFloatFromNodeProperty(node, property); } case EA_VALUE_PARAM: { const int paramId = expression->d.terminal.d1.integer; const char *string = UI_GetParam(context, paramId); if (string[0] == '\0') { Com_Printf("UI_GetFloatFromParam: Param '%i' is out of range, or empty; '0' returned\n", paramId); return 0; } return atof(string); } case EA_VALUE_PARAMCOUNT: return UI_GetParamNumber(context); } break; case EA_OPERATOR_FLOAT2FLOAT: { const float value1 = UI_GetFloatFromExpression(expression->d.nonTerminal.left, context); const float value2 = UI_GetFloatFromExpression(expression->d.nonTerminal.right, context); switch (expression->type) { case EA_OPERATOR_ADD: return value1 + value2; case EA_OPERATOR_SUB: return value1 - value2; case EA_OPERATOR_MUL: return value1 * value2; case EA_OPERATOR_DIV: if (value2 == 0) { Com_Printf("UI_GetFloatFromExpression: Div by 0. '0' returned"); return 0; } else return value1 / value2; case EA_OPERATOR_MOD: { const int v1 = value1; const int v2 = value2; /** @todo do we have some check to do? */ return v1 % v2; } } } break; case EA_OPERATOR_UNARY: switch (expression->type) { case EA_OPERATOR_PATHPROPERTYFROM: { uiNode_t *node; const value_t *property; node = UI_GetNodeFromExpression(expression, context, &property); return UI_GetFloatFromNodeProperty(node, property); } default: Com_Error(ERR_FATAL, "UI_GetFloatFromExpression: (EA_OPERATOR_UNARY) Invalid expression type %i", expression->type); } } Com_Printf("UI_GetFloatFromExpression: Unsupported expression type: %i. '0' returned", expression->type); return 0; }