Olá a todos:
Gostaria de definir o limite para o valor de entrada no widget realfield. O código abaixo criará um widget de campo real e o limite do valor de entrada está entre 0 e 0,05.
Se você inserir 0,06 dentro do widget e clicar em algum lugar fora da GUI, ele avisará que não é um valor correto e redefinirá o valor para o último valor.
Mas se você inserir 0,05, ele não irá avisá-lo. E então, se você inserir qualquer outro valor, será exibido "Arquivo de configuração incorreto".
Verifiquei novamente a lógica e não encontrei nenhum código errado. Alguém poderia me ajudar a depurar? Muito obrigado.
Cumprimentos
Chen ZX
Number get_tag_as_number(TagGroup tg, String tag_path, String tag_type, Number low_limit, Number high_limit)
{
Number tag_value, tag_is_valid
if (tag_type == "Boolean") tag_is_valid = tg.TagGroupGetTagAsBoolean(tag_path, tag_value)
if (tag_type == "Number") tag_is_valid = tg.TagGroupGetTagAsNumber(tag_path, tag_value)
if (!tag_is_valid)
{
Result("ERROR: Can not get tag value of " + tag_path + "." + "\n")
ShowAlert("Can not get tag value of " + tag_path + ".", 0)
Exit(0)
}
if (tag_value < low_limit || tag_value > high_limit)
{
Result("ERROR: Wrong config file, " + tag_path + "." + "\n")
ShowAlert("Wrong config file, " + tag_path + ".", 0)
Exit(0)
}
return tag_value
}
Class testUI : UIFrame
{
TagGroup ui_config_tg
Void tg_init(Object self)
{
ui_config_tg = NewTagGroup()
ui_config_tg.TagGroupSetTagAsFloat("display_font:init_value", 0.015)
}
TagGroup create_realfield_widget(Object self)
{
Number low_limit = 0
Number high_limit = 0.05
Number init_value = get_tag_as_number(ui_config_tg, "display_font:init_value", "Number", low_limit, high_limit)
TagGroup realfield_widget
realfield_widget = DLGCreateRealField(init_value, 16, 3, "action")
realfield_widget.DLGIdentifier("#realfield_widget")
return realfield_widget
}
Void action(Object self, TagGroup item_tag)
{
Number low_limit = 0
Number high_limit = 0.05
Number curr_value = item_tag.DLGGetValue()
Number init_value
init_value = get_tag_as_number(ui_config_tg, "display_font:init_value", "Number", low_limit, high_limit)
if (curr_value < low_limit || curr_value > high_limit)
{
OKDialog("Font size must between " + low_limit + " to " + high_limit)
self.LookUpElement("#realfield_widget").DLGValue(init_value)
item_tag.DLGValue(init_value)
Exit(0)
}
ui_config_tg.TagGroupSetTagAsFloat("display_font:init_value", curr_value)
Result( curr_value)
}
TagGroup create_dialog(Object self)
{
self.tg_init()
TagGroup realfield_widget = self.create_realfield_widget()
TagGroup tabs = DLGCreateTabList()
tabs.DLGAddElement(realfield_widget)
TagGroup dialog = DLGCreateDialog("test")
dialog.DLGAddElement(tabs)
return dialog
}
testUI(Object self)
{
self.init(self.create_dialog())
self.Display("test")
}
}
Alloc(testUI)
A lógica é boa, mas você está enfrentando problemas de arredondamento que são particularmente difíceis de detectar em linguagens de script com conversões automáticas de números e strings.
number
é tratado internamente como duplo (pelo menos no GMS 3)É mais seguro seguir em frente
...asNumber
para evitar esses problemas. Você precisa ser específico do tipo apenas nos casos em que for importante, como streaming para um fluxo binário.Veja o exemplo a seguir
Como alternativa, você pode corrigir seu script não verificando exatamente "0", mas sim um pequeno valor épsilon que explica o problema de precisão: