2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
2022-08-18 18:18:36 +03:00
|
|
|
"fmt"
|
2021-09-07 17:36:50 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func stringProperty(properties Properties, tag PropertyName, session Session) (string, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value := properties.getRaw(tag); value != nil {
|
|
|
|
if text, ok := value.(string); ok {
|
|
|
|
return session.resolveConstants(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func imageProperty(properties Properties, tag PropertyName, session Session) (string, bool) {
|
2022-04-23 18:13:35 +03:00
|
|
|
if value := properties.getRaw(tag); value != nil {
|
|
|
|
if text, ok := value.(string); ok {
|
|
|
|
if text != "" && text[0] == '@' {
|
|
|
|
if image, ok := session.ImageConstant(text[1:]); ok {
|
|
|
|
return image, true
|
|
|
|
} else {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func valueToSizeUnit(value any, session Session) (SizeUnit, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case SizeUnit:
|
|
|
|
return value, true
|
|
|
|
|
2022-09-05 14:00:07 +03:00
|
|
|
case SizeFunc:
|
|
|
|
return SizeUnit{Type: SizeFunction, Function: value}, true
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
return StringToSizeUnit(text)
|
|
|
|
}
|
2022-08-20 20:05:56 +03:00
|
|
|
|
|
|
|
case float64:
|
|
|
|
return Px(value), true
|
|
|
|
|
|
|
|
case float32:
|
|
|
|
return Px(float64(value)), true
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := isInt(value); ok {
|
|
|
|
return Px(float64(n)), true
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AutoSize(), false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func sizeProperty(properties Properties, tag PropertyName, session Session) (SizeUnit, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToSizeUnit(properties.getRaw(tag), session)
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func angleProperty(properties Properties, tag PropertyName, session Session) (AngleUnit, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value := properties.getRaw(tag); value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case AngleUnit:
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
return StringToAngleUnit(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AngleUnit{Type: 0, Value: 0}, false
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func valueToColor(value any, session Session) (Color, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case Color:
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if len(value) > 1 && value[0] == '@' {
|
|
|
|
return session.Color(value[1:])
|
|
|
|
}
|
|
|
|
return StringToColor(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Color(0), false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func colorProperty(properties Properties, tag PropertyName, session Session) (Color, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToColor(properties.getRaw(tag), session)
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func valueToEnum(value any, tag PropertyName, session Session, defaultValue int) (int, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
values := enumProperties[tag].values
|
|
|
|
switch value := value.(type) {
|
|
|
|
case int:
|
|
|
|
if value >= 0 && value < len(values) {
|
|
|
|
return value, true
|
|
|
|
}
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
if tag == Orientation {
|
|
|
|
switch strings.ToLower(text) {
|
|
|
|
case "vertical":
|
|
|
|
value = "up-down"
|
|
|
|
|
|
|
|
case "horizontal":
|
|
|
|
value = "left-to-right"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result, ok := enumStringToInt(text, values, true); ok {
|
|
|
|
return result, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func enumStringToInt(value string, enumValues []string, logError bool) (int, bool) {
|
|
|
|
value = strings.Trim(value, " \t\n\r")
|
|
|
|
|
|
|
|
for n, val := range enumValues {
|
|
|
|
if val == value {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := strconv.Atoi(value); err == nil {
|
|
|
|
if n >= 0 && n < len(enumValues) {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if logError {
|
|
|
|
ErrorLogF(`Out of bounds: value index = %d, valid values = [%v]`, n, enumValues)
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
value = strings.ToLower(value)
|
|
|
|
for n, val := range enumValues {
|
|
|
|
if val == value {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if logError {
|
|
|
|
ErrorLogF(`Unknown "%s" value. Valid values = [%v]`, value, enumValues)
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func enumProperty(properties Properties, tag PropertyName, session Session, defaultValue int) (int, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToEnum(properties.getRaw(tag), tag, session, defaultValue)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func valueToBool(value any, session Session) (bool, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case bool:
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
switch strings.ToLower(text) {
|
|
|
|
case "true", "yes", "on", "1":
|
|
|
|
return true, true
|
|
|
|
|
|
|
|
case "false", "no", "off", "0":
|
|
|
|
return false, true
|
|
|
|
|
|
|
|
default:
|
|
|
|
ErrorLog(`The error of converting of "` + text + `" to bool`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func boolProperty(properties Properties, tag PropertyName, session Session) (bool, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToBool(properties.getRaw(tag), session)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func valueToInt(value any, session Session, defaultValue int) (int, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
n, err := strconv.Atoi(strings.Trim(text, " \t"))
|
|
|
|
if err == nil {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
ErrorLog(err.Error())
|
|
|
|
} else {
|
|
|
|
n, err := strconv.Atoi(strings.Trim(value, " \t"))
|
|
|
|
if err == nil {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
ErrorLog(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return isInt(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue, false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func intProperty(properties Properties, tag PropertyName, session Session, defaultValue int) (int, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToInt(properties.getRaw(tag), session, defaultValue)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func valueToFloat(value any, session Session, defaultValue float64) (float64, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case float64:
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
f, err := strconv.ParseFloat(text, 64)
|
|
|
|
if err == nil {
|
|
|
|
return f, true
|
|
|
|
}
|
|
|
|
ErrorLog(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue, false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func floatProperty(properties Properties, tag PropertyName, session Session, defaultValue float64) (float64, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToFloat(properties.getRaw(tag), session, defaultValue)
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:18:36 +03:00
|
|
|
func valueToFloatText(value any, session Session, defaultValue float64) (string, bool) {
|
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case float64:
|
|
|
|
return fmt.Sprintf("%g", value), true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
if _, err := strconv.ParseFloat(text, 64); err != nil {
|
|
|
|
ErrorLog(err.Error())
|
|
|
|
return fmt.Sprintf("%g", defaultValue), false
|
|
|
|
}
|
|
|
|
return text, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%g", defaultValue), false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func floatTextProperty(properties Properties, tag PropertyName, session Session, defaultValue float64) (string, bool) {
|
2022-08-18 18:18:36 +03:00
|
|
|
return valueToFloatText(properties.getRaw(tag), session, defaultValue)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func valueToRange(value any, session Session) (Range, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case Range:
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case int:
|
|
|
|
return Range{First: value, Last: value}, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := session.resolveConstants(value); ok {
|
|
|
|
var result Range
|
|
|
|
if result.setValue(text) {
|
|
|
|
return result, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Range{}, false
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func rangeProperty(properties Properties, tag PropertyName, session Session) (Range, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
return valueToRange(properties.getRaw(tag), session)
|
|
|
|
}
|