// helper to set cvar value from element, TODO: refactor out of this class static void setCvar( Element *elem ) { ElementFormControl *target = dynamic_cast<ElementFormControl*>( elem ); if( target != 0 ) { String cvar = target->GetAttribute<String>( "cvar", "" ); String type = target->GetAttribute<String>( "type", "" ); // TODO: add support for <select> widgets if( type == "checkbox" || type == "radio" ) { float fvalue = target->HasAttribute( "checked" ) ? 1.0 : 0.0; trap::Cvar_SetValue( cvar.CString(), fvalue ); //Com_Printf("onChange: Cvar_Set \"%s\" \"%g\"\n", cvar.CString(), fvalue ); } else if( type == "range" ) { float fvalue = atof( target->GetValue().CString() ); trap::Cvar_SetValue( cvar.CString(), fvalue ); //Com_Printf("onChange: Cvar_Set \"%s\" \"%g\"\n", cvar.CString(), fvalue ); } else { String value = target->GetValue(); trap::Cvar_Set( cvar.CString(), value.CString() ); //Com_Printf("onChange: Cvar_Set \"%s\" \"%s\"\n", cvar.CString(), value.CString() ); } } }
//getters int ElementFormControlGetAttrdisabled(lua_State* L) { ElementFormControl* efc = LuaType<ElementFormControl>::check(L,1); LUACHECKOBJ(efc); lua_pushboolean(L,efc->IsDisabled()); return 1; }
//setters int ElementFormControlSetAttrdisabled(lua_State* L) { ElementFormControl* efc = LuaType<ElementFormControl>::check(L,1); LUACHECKOBJ(efc); efc->SetDisabled(CHECK_BOOL(L,2)); return 0; }
int ElementFormControlGetAttrvalue(lua_State* L) { ElementFormControl* efc = LuaType<ElementFormControl>::check(L,1); LUACHECKOBJ(efc); lua_pushstring(L,efc->GetValue().CString()); return 1; }
int ElementFormControlSetAttrvalue(lua_State* L) { ElementFormControl* efc = LuaType<ElementFormControl>::check(L,1); LUACHECKOBJ(efc); const char* value = luaL_checkstring(L,2); efc->SetValue(value); return 0; }
// Submits the form. void ElementForm::Submit(const Rocket::Core::String& name, const Rocket::Core::String& submit_value) { Rocket::Core::Dictionary values; if (name.Empty()) values.Set("submit", submit_value); else values.Set(name, submit_value); Core::ElementList form_controls; Core::ElementUtilities::GetElementsByTagName(form_controls, this, "input"); Core::ElementUtilities::GetElementsByTagName(form_controls, this, "textarea"); Core::ElementUtilities::GetElementsByTagName(form_controls, this, "select"); Core::ElementUtilities::GetElementsByTagName(form_controls, this, "dataselect"); for (size_t i = 0; i < form_controls.size(); i++) { ElementFormControl* control = rocket_dynamic_cast< ElementFormControl* >(form_controls[i]); if (!control) continue; // Skip disabled controls. if (control->IsDisabled()) continue; // Only process controls that should be submitted. if (!control->IsSubmitted()) continue; Rocket::Core::String control_name = control->GetName(); Rocket::Core::String control_value = control->GetValue(); // Skip over unnamed form controls. if (control_name.Empty()) continue; // If the item already exists, append to it. Rocket::Core::Variant* value = values.Get(control_name); if (value != NULL) value->Set(value->Get< Rocket::Core::String >() + ", " + control_value); else values.Set< Rocket::Core::String >(control_name, control_value); } DispatchEvent("submit", values); }