/// <summary>Sets the option at the given index as the selected one.</summary> /// <param name="index">The index of the option to select.</param> /// <param name="element">The element at the given index.</param> /// <param name="runOnChange">True if the onchange event should run.</param> private void SetSelected(int index, Element element, bool runOnChange) { if (index == SelectedIndex) { return; } SelectedIndex = index; if (index < 0 || element == null) { // Clear the option text: DisplayText.innerHTML = ""; } else { DisplayText.innerHTML = element.innerHTML; } // Call onchange, but only if the dropdown didn't auto select this option because it's starting up. if (runOnChange) { Element.Run("onchange"); } }
/// <summary>Called when the marquee bounces the content.</summary> private void Bounced() { // Trigger: Element.Run("onbounce"); // Consider looping too: Wrapped(); }
/// <summary>Used by boolean inputs (checkbox/radio). Selects this as the active one.</summary> public void Select() { if (Checked) { return; } Checked = true; // Set checked: Element["checked"] = "1"; if (Type == InputType.Radio) { // Firstly, unselect all other radio elements with this same name: string name = Element["name"]; if (Element.form != null) { List <Element> allInputs = Element.form.GetAllInputs(); foreach (Element element in allInputs) { if (element == Element) { // Skip this element continue; } if (element["type"] == "radio") { // Great, got one - same name? if (element["name"] == name) { // Yep; unselect it. ((InputTag)(element.Handler)).Unselect(); } } } } Element.Run("onchange"); Element.innerHTML = "<div style='width:60%;height:60%;background:#ffffff;border-radius:4px;'></div>"; } else if (Type == InputType.Checkbox) { SetValue("1"); Element.innerHTML = "x"; } }
/// <summary>Call this to begin a marquee.</summary> public void Start() { if (Active) { return; } Active = true; // Start our timer: Timer = new UITimer(false, ScrollDelay, OnTick); Element.Run("onstart"); }
/// <summary>Call this to stop a scrolling marquee.</summary> public void Stop() { if (!Active) { return; } Active = false; // Stop and clear the timer: Timer.Stop(); Timer = null; Element.Run("onstop"); }
/// <summary>Called when the marquee wraps.</summary> private void Wrapped() { if (Loop == -1) { return; } Loop--; if (Loop == 0) { // Stop the marquee: Stop(); // Fire the finish event: Element.Run("onfinish"); } }
private void OnFormSent(HttpRequest request) { Element element = (Element)request.ExtraData; // Attempt to run ondone: object result = element.Run("ondone", request); if (result != null && result.GetType() == typeof(bool) && (((bool)result) == false)) { // The ondone function returned false. Don't load into a target at all. return; } // Load the result into target now. Document document = ResolveTarget(element); if (document == null) { // Posting a form to an external target. Log.Add("Warning: Unity cannot post to external targets. The page will be loaded a second time."); // Open the URL outside of Unity: Application.OpenURL(request.Url); } else { if (request.Errored) { if (ErrorHandlers.PageNotFound != null) { ErrorHandlers.PageNotFound(new HttpErrorInfo(request), document); } else { document.innerHTML = "Error: " + request.Error; } } else { document.innerHTML = request.Text; } } }
/// <summary>Applies the image data so it's ready for rendering.</summary> public void ApplyImageData() { // Grab the canvas: Element element = canvas; // Grab its computed style: ComputedStyle computed = element.style.Computed; if (ImageData == null) { ImageData = new DynamicTexture(); } // Resize the texture: ImageData.Resize(computed.PixelWidth, computed.PixelHeight, false); if (ImageData.Width != 0 && ImageData.Height != 0) { if (Package == null) { //We now need a package to actually display it. // Create the package: Package = new ImagePackage(ImageData); // Apply it to the element: if (computed.BGImage == null) { computed.BGImage = new BackgroundImage(element); } computed.BGImage.SetImage(Package); } // Run the change event: element.Run("onchange"); apply(); } }
/// <summary>Sets the value of this textarea, optionally as a html string.</summary> /// <param name="value">The value to set.</param> /// <param name="html">True if the value can safely contain html.</param> public void SetValue(string value, bool html) { if (MaxLength != int.MaxValue) { // Do we need to clip it? if (value.Length > MaxLength) { // Yep! value = value.Substring(0, MaxLength); } } if (CursorIndex > value.Length) { MoveCursor(0); } // Fire onchange: Element.Run("onchange"); Element["value"] = Value = value; if (html) { Element.innerHTML = value; } else { Element.innerHTML = Wrench.Text.Escape(value).Replace("\n", "<br>"); } if (Cursor != null) { Element.AppendNewChild(Cursor); } }
/// <summary>Submits this form.</summary> public void submit() { // Generate a nice dictionary of the form contents. // Step 1: find the unique names of the elements: Dictionary <string, string> uniqueValues = new Dictionary <string, string>(); List <Element> allInputs = GetAllInputs(); foreach (Element element in allInputs) { string type = element["type"]; if (type == "submit" || type == "button") { // Don't want buttons in here. continue; } string name = element["name"]; if (name == null) { name = ""; } // Step 2: For each one, get their value. // We might have a name repeated, in which case we check if they are radio boxes. if (uniqueValues.ContainsKey(name)) { // Ok the element is already added - we have two with the same name. // If they are radio, then only overwrite value if the new addition is checked. // Otherwise, overwrite - furthest down takes priority. if (element.Tag == "input") { InputTag tag = (InputTag)(element.Handler); if (tag.Type == InputType.Radio && !tag.Checked) { // Don't overwrite. continue; } } } string value = element.value; if (value == null) { value = ""; } uniqueValues[name] = value; } FormData formData = new FormData(uniqueValues); // Hook up the form element: formData.form = Element; object result = Element.Run("onsubmit", formData); if (!string.IsNullOrEmpty(Action) && (result == null || !(bool)result)) { // Post to Action. FilePath path = new FilePath(Action, Element.Document.basepath, false); FileProtocol fileProtocol = path.Handler; if (fileProtocol != null) { fileProtocol.OnPostForm(formData, Element, path); } } }
/// <summary>Sets the value of this input box, optionally as a html string.</summary> /// <param name="value">The value to set.</param> /// <param name="html">True if the value can safely contain html.</param> public void SetValue(string value, bool html) { if (IsScrollInput()) { return; } if (MaxLength != int.MaxValue) { // Do we need to clip it? if (value != null && value.Length > MaxLength) { // Yep! value = value.Substring(0, MaxLength); } } if (value == null || CursorIndex > value.Length) { MoveCursor(0); } // Fire onchange: Element.Run("onchange"); Element["value"] = Value = value; if (!IsBoolInput()) { if (Hidden) { // Unfortunately the new string(char,length); constructor isn't reliable. // Build the string manually here. StringBuilder sb = new StringBuilder("", value.Length); for (int i = 0; i < value.Length; i++) { sb.Append('*'); } if (html) { Element.innerHTML = sb.ToString(); } else { Element.textContent = sb.ToString(); } } else { if (html) { Element.innerHTML = value; } else { Element.textContent = value; } } } if (IsTextInput()) { if (Cursor != null) { Element.AppendNewChild(Cursor); } } }