//----------------------------------------------------------------------------------------------- /// <summary> /// AddContent - Add or replace a vwml item in the widget space. Use this method to add /// content you might want to change dynamically /// </summary> //----------------------------------------------------------------------------------------------- public void AddContent(string name, Stream vwmlStream, object bindingContext) { var layout = VarmintWidget.PreloadFromVwml(vwmlStream, name); if (layout.VwmlTag == "Style") { var rootStyle = (VarmintWidgetStyle)VarmintWidget.HydrateLayout(_injector, layout, _controlLibrary); foreach (var style in rootStyle.FindWidgetsByType <VarmintWidgetStyle>()) { if (_styleLibrary.ContainsKey(style.Name)) { Debug.WriteLine("Warning: Added duplicate style: " + style.Name); } _styleLibrary[style.Name] = style; } } else if (layout.VwmlTag == "Control") { _controlLibrary.Add(layout.Name, layout); } else { _screenLibrary.Add(layout.Name, layout); } }
//-------------------------------------------------------------------------------------- /// <summary> /// Render /// </summary> //-------------------------------------------------------------------------------------- void Render(GameTime gameTime, VarmintWidget widget) { var textToDisplay = (Content == null) ? "" : Content.ToString(); Renderer.DrawBox(AbsoluteOffset, Size, RenderBackgroundColor); Vector2 alignedOffset = AbsoluteOffset; var margin = 0f; if (WrapContent) { margin = Size.X; } Vector2 textSize = Renderer.MeasureText(textToDisplay, FontName, FontSize, margin); switch (HorizontalContentAlignment) { case HorizontalContentAlignment.Left: break; case HorizontalContentAlignment.Center: alignedOffset.X += (Size.X - textSize.X) / 2; break; case HorizontalContentAlignment.Right: alignedOffset.X += (Size.X - textSize.X); break; } switch (VerticalContentAlignment) { case VerticalContentAlignment.Top: break; case VerticalContentAlignment.Center: alignedOffset.Y += (Size.Y - textSize.Y) / 2; break; case VerticalContentAlignment.Bottom: alignedOffset.Y += (Size.Y - textSize.Y); break; } Renderer.DrawText(textToDisplay, FontName, FontSize, alignedOffset, RenderForegroundColor, margin); }
//-------------------------------------------------------------------------------------- /// <summary> /// Render /// </summary> //-------------------------------------------------------------------------------------- private void Render(GameTime gameTime, VarmintWidget widget) { if (Content == null) { return; } Renderer.DrawSprite(Content.ToString(), Frame, AbsoluteOffset, Size, RenderGraphicColor); }
//-------------------------------------------------------------------------------------- /// <summary> /// RemoveChild /// </summary> //-------------------------------------------------------------------------------------- public virtual void RemoveChild(VarmintWidget childToRemove, bool suppressChildUpdate = false) { children.Remove(childToRemove); if (ChildrenAffectFormatting && !suppressChildUpdate) { UpdateChildFormatting(); } }
//-------------------------------------------------------------------------------------- /// <summary> /// AddChild /// </summary> //-------------------------------------------------------------------------------------- public virtual void AddChild(VarmintWidget widget, bool suppressChildUpdate = false) { children.Add(widget); widget.Parent = this; if (ChildrenAffectFormatting && !suppressChildUpdate) { UpdateChildFormatting(); } }
//-------------------------------------------------------------------------------------- /// <summary> /// Thumb_OnDrag - Change the Value based on the drag /// </summary> //-------------------------------------------------------------------------------------- private EventHandledState Thumb_OnDrag(VarmintWidget sender, Vector2 start, Vector2 end) { var slideWidth = Size.X - Thumb.Size.X; var xMove = (start.X - end.X) / slideWidth; Value += xMove; return(EventHandledState.Handled); }
//-------------------------------------------------------------------------------------- /// <summary> /// Focus - set input focus on this control /// </summary> //-------------------------------------------------------------------------------------- public void Focus() { if (_focusedContol != null) { _focusedContol.HasFocus = false; } _focusedContol = this; _focusedContol.HasFocus = true; }
//-------------------------------------------------------------------------------------- /// <summary> /// Finish - call this to force an animation to complete /// </summary> //-------------------------------------------------------------------------------------- internal void Finish(VarmintWidget widget) { if (_animationDurationSeconds > 0) { Update(new GameTime(TimeSpan.MaxValue, TimeSpan.MaxValue)); } else { OnComplete?.Invoke(); IsComplete = true; } }
//-------------------------------------------------------------------------------------- /// <summary> /// VarmintWidgetTextInput_OnTap /// </summary> //-------------------------------------------------------------------------------------- private EventHandledState VarmintWidgetTextInput_OnTap(VarmintWidget widget, Vector2 location) { var startOffset = GetTextStartOffset(); var text = GetTextToDisplay(); for (int i = 0; i < text.Length; i++) { startOffset += Renderer.MeasureText(text[i].ToString(), FontName, FontSize); if (location.X < startOffset.X) { CursorSpot = i; break; } } if (location.X >= startOffset.X) { CursorSpot = text.Length; } return(EventHandledState.Handled); }
//-------------------------------------------------------------------------------------- /// <summary> /// ctor /// </summary> //-------------------------------------------------------------------------------------- public VarmintWidgetSpace(IVarmintWidgetInjector injector) { _injector = injector; var assembly = injector.GetType().GetTypeInfo().Assembly; VarmintWidget.DeclareAssembly(assembly); // automatically add embedded layout by preloading the raw layout without hydrating it foreach (var resourceName in assembly.GetManifestResourceNames()) { if (resourceName.ToLower().EndsWith(".vwml")) { Debug.WriteLine("---- Injesting " + resourceName); var nameParts = resourceName.Split('.'); var defaultName = nameParts[nameParts.Length - 2]; AddContent(defaultName, assembly.GetManifestResourceStream(resourceName)); } } }
//-------------------------------------------------------------------------------------- /// <summary> /// ctor /// </summary> //-------------------------------------------------------------------------------------- public VarmintWidgetSpace(IVarmintWidgetInjector injector, object bindingContext = null) { _injector = injector; var assembly = injector.GetType().GetTypeInfo().Assembly; VarmintWidget.DeclareAssembly(assembly); // automatically add embedded resources foreach (var resourceName in assembly.GetManifestResourceNames()) { if (resourceName.ToLower().EndsWith(".vwml")) { System.Diagnostics.Debug.WriteLine("---- " + resourceName); var nameParts = resourceName.Split('.'); var defaultName = nameParts[nameParts.Length - 2]; AddContent(defaultName, assembly.GetManifestResourceStream(resourceName), bindingContext); } } }
//-------------------------------------------------------------------------------------- /// <summary> /// Render /// </summary> //-------------------------------------------------------------------------------------- void Render(GameTime gameTime, VarmintWidget widget) { var textToDisplay = GetTextToDisplay(); Renderer.DrawBox(AbsoluteOffset, Size, RenderBackgroundColor); Vector2 alignedOffset = AbsoluteOffset + GetTextStartOffset(); var leftOfCursor = textToDisplay.Substring(0, CursorSpot); var rightOfCusor = textToDisplay.Substring(CursorSpot); var leftSize = Renderer.MeasureText(leftOfCursor, FontName, FontSize); Renderer.DrawText(leftOfCursor, FontName, FontSize, alignedOffset, RenderForegroundColor, _margin); Renderer.DrawText(rightOfCusor, FontName, FontSize, alignedOffset + new Vector2(leftSize.X, 0), RenderForegroundColor, _margin); if ((gameTime.TotalGameTime.TotalSeconds * 2) % 2.0 > 1.0 && HasFocus) { Renderer.DrawLine( alignedOffset + new Vector2(leftSize.X, 0), alignedOffset + leftSize, FontSize * .2f, RenderForegroundColor); } }
//----------------------------------------------------------------------------------------------- /// <summary> /// GetScreen /// </summary> //----------------------------------------------------------------------------------------------- internal VarmintWidget GetScreen(string screenName, object bindingContext) { if (!_screensByName.ContainsKey(screenName)) { if (_screenLibrary.ContainsKey(screenName)) { var screen = VarmintWidget.HydrateLayout(_injector, _screenLibrary[screenName], _controlLibrary); if (bindingContext != null) { screen.BindingContext = bindingContext; } _screensByName[screen.Name] = screen; } else { throw new ApplicationException("Unknown screen: " + screenName); } } var returnMe = _screensByName[screenName]; returnMe.Prepare(_styleLibrary); return(returnMe); }
//-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- private EventHandledState VarmintWidgetScrollView_OnDrag(VarmintWidget source, Vector2 location, Vector2 delta) { ScrollBy(delta); return(EventHandledState.Handled); }
//-------------------------------------------------------------------------------------- /// <summary> /// VarmintWidgetTextInput_OnTouchDown /// </summary> //-------------------------------------------------------------------------------------- private EventHandledState VarmintWidgetTextInput_OnTouchDown(VarmintWidget arg1, Microsoft.Xna.Framework.Input.Touch.TouchLocation arg2) { return(EventHandledState.Handled); }
//-------------------------------------------------------------------------------------- /// <summary> /// AddPreviousWidget - remember this as a recent widget /// </summary> //-------------------------------------------------------------------------------------- internal void AddTouchedWidget(VarmintWidget widget) { _previousWidgets.Add(widget); }
//-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- private EventHandledState VarmintWidgetScrollView_OnFlick(VarmintWidget widget, Vector2 location, Vector2 delta) { _momentum = delta * .2f; return(EventHandledState.Handled); }
//-------------------------------------------------------------------------------------- /// <summary> /// RemoveChild /// </summary> //-------------------------------------------------------------------------------------- public virtual void RemoveChild(VarmintWidget childToRemove) { _children.Remove(childToRemove); childToRemove.Parent = null; }
//-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- public override void RemoveChild(VarmintWidget childToRemove, bool suppressChildUpdate = false) { _innerContent.RemoveChild(childToRemove, suppressChildUpdate); }
//-------------------------------------------------------------------------------------- /// <summary> /// AddChild /// </summary> //-------------------------------------------------------------------------------------- public virtual void AddChild(VarmintWidget widget) { _children.Add(widget); widget.Parent = this; }
//-------------------------------------------------------------------------------------- /// <summary> /// HydrateLayout - Take a layout item and turn it into a VarmintWidget /// </summary> //-------------------------------------------------------------------------------------- public static VarmintWidget HydrateLayout(IVarmintWidgetInjector injector, LayoutItem widgetLayout, Dictionary <string, LayoutItem> controlLibrary) { VarmintWidget output = null; Action <LayoutItem> applyLayout = (layout) => { foreach (var name in layout.Settings.Keys) { switch (name) { case "Style": case "ApplyTo": output.SetValue(name, layout.Settings[name], true); break; default: output.AddSetting(name, layout.Settings[name]); break; } } foreach (var childItem in layout.Children) { if (childItem.VwmlTag.Contains(".")) { var parts = childItem.VwmlTag.Split('.'); if (parts.Length > 2) { throw new ApplicationException("Property setter specification is too deep. Only one dot allowed! (" + childItem.VwmlTag + ")"); } var propertyType = output.GetType().GetProperty(parts[1]); if (childItem.Children.Count == 1) // Only add items with content { var hydratedLayout = HydrateLayout(injector, childItem.Children[0], controlLibrary); if (!propertyType.PropertyType.IsAssignableFrom(hydratedLayout.GetType())) { throw new ApplicationException("Property " + childItem.VwmlTag + " cannot be assigned Type " + hydratedLayout.GetType().Name); } propertyType.SetValue(output, hydratedLayout); } else if (childItem.Children.Count > 1) { throw new ApplicationException("Too many child nodes on a property setter. You only get one! (" + childItem.VwmlTag + ")"); } } else { output.AddChild(HydrateLayout(injector, childItem, controlLibrary), true); } } }; if (controlLibrary.ContainsKey(widgetLayout.VwmlTag)) { var controlLayout = controlLibrary[widgetLayout.VwmlTag]; if (controlLayout.Settings.ContainsKey("Class")) { var controlType = GetWidgetType(controlLayout.Settings["Class"]); if (!controlType.IsSubclassOf(typeof(VarmintWidgetControl))) { throw new ApplicationException("The Class attribute must point to a VarmintWidgetControl"); } output = (VarmintWidget)Activator.CreateInstance(controlType); output.EventBindingContext = output; } else { output = new VarmintWidgetControl(); } // Controls will get properties from the control layout, but these can // be overridden later by the local instace of the control applyLayout(controlLayout); } else { var nodeType = GetWidgetType(widgetLayout.VwmlTag); output = (VarmintWidget)Activator.CreateInstance(nodeType); } if (widgetLayout.Name != null) { output.Name = widgetLayout.Name; } foreach (var propertyType in output.GetType().GetProperties()) { var injectAttribute = (VarmintWidgetInjectAttribute)propertyType.GetCustomAttribute(typeof(VarmintWidgetInjectAttribute)); if (injectAttribute != null) { propertyType.SetValue(output, injector.GetInjectedValue(injectAttribute, propertyType)); } } applyLayout(widgetLayout); return(output); }
//-------------------------------------------------------------------------------------- /// <summary> /// InsertChild /// </summary> //-------------------------------------------------------------------------------------- public virtual void InsertChild(VarmintWidget widget) { _children.Insert(0, widget); widget.Parent = this; }
//-------------------------------------------------------------------------------------- /// <summary> /// AddStartWidget - remember this as an initially touched widget /// </summary> //-------------------------------------------------------------------------------------- internal void AddStartWidget(VarmintWidget widget) { _startWidgets.Add(widget); }
public override void AddChild(VarmintWidget widget, bool suppressChildUpdate = false) { base.AddChild(widget, suppressChildUpdate); RecalculateExtremes(); }
public override void RemoveChild(VarmintWidget childToRemove, bool suppressChildUpdate = false) { base.RemoveChild(childToRemove, suppressChildUpdate); RecalculateExtremes(); }
//-------------------------------------------------------------------------------------- /// <summary> /// Render /// </summary> //-------------------------------------------------------------------------------------- void Render(GameTime gameTime, VarmintWidget widget) { Renderer.DrawBox(AbsoluteOffset, Size, RenderBackgroundColor); }
//-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- public override void AddChild(VarmintWidget widget, bool suppressChildUpdate = false) { _innerContent.AddChild(widget, suppressChildUpdate); }
//-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- public void SetScreen(VarmintWidget screen) { _visualTree = screen; screen.Prepare(_widgetSpace.StyleLibrary); }
//-------------------------------------------------------------------------------------- /// <summary> /// Update - calculates the animation delta and calls the attached animation code /// </summary> //-------------------------------------------------------------------------------------- internal void Update(VarmintWidget widget, GameTime gameTime) { _widget = widget; Update(gameTime); }
//----------------------------------------------------------------------------------------------- /// <summary> /// Prepare - prepare the visual elements in this widget according to sytles /// </summary> //----------------------------------------------------------------------------------------------- internal void Prepare(VarmintWidget overlay) { overlay.Prepare(_widgetSpace.StyleLibrary); }