public override void Draw(AdvancedDrawBatch drawBatch, DrawButtonInfo info) { Update(info.EllapsedTime, info.ButtonState); ButtonState state = info.ButtonState; Color color = ColorFromState * info.Opacity * Opacity; NinePatchImage image = _imageReleased; switch (state & ButtonState.Mask) { case ButtonState.Disabled: if (_imageDisabled != null) { image = _imageDisabled; } break; case ButtonState.Pushed: if (_imagePushed != null) { image = _imagePushed; } break; } float scale = _scaleByUnit ? (float)UiUnit.Unit : 1; Rectangle target = _margin.ComputeRect(info.Target); drawBatch.DrawNinePatchRect(image, target, color, scale * _scale); }
protected override void Init(UiController controller, object binding, DefinitionFile definition) { base.Init(controller, binding, definition); DefinitionFileWithStyle file = new DefinitionFileWithStyle(definition, typeof(NinePatchBackground)); _imageDisabled = DefinitionResolver.Get <NinePatchImage>(controller, binding, file["ImageDisabled"], null); _imageReleased = DefinitionResolver.Get <NinePatchImage>(controller, binding, file["ImageReleased"], null); _imagePushed = DefinitionResolver.Get <NinePatchImage>(controller, binding, file["ImagePushed"], null); _scaleByUnit = DefinitionResolver.Get <bool>(controller, binding, file["ScaleByUnit"], false); _scale = (float)DefinitionResolver.Get <double>(controller, binding, file["Scale"], 1); }
bool IDefinitionClass.Init(UiController controller, object binding, DefinitionFile definition) { DefinitionFileWithStyle file = new DefinitionFileWithStyle(definition, typeof(NinePatchBackground)); _image = DefinitionResolver.Get <NinePatchImage>(controller, binding, file["Image"], null); _scaleByUnit = DefinitionResolver.Get <bool>(controller, binding, file["ScaleByUnit"], false); _scale = (float)DefinitionResolver.Get <double>(controller, binding, file["Scale"], 1); _color = DefinitionResolver.GetColorWrapper(controller, binding, file["Color"]) ?? new ColorWrapper(); _margin = DefinitionResolver.Get <Margin>(controller, binding, file["Margin"], Margin.None); return(true); }
public void DrawNinePatchRect(NinePatchImage image, Rectangle target, Color color, float scale) { if (image != null) { Texture = image.Texture; SpriteBatchIsNeeded(); image.Draw(_spriteBatch, target, new Vector2(scale), color); } else { DrawRectangle(target, color); } }
protected override bool Init(object controller, object binding, DefinitionFile definition) { if (!base.Init(controller, binding, definition)) { return(false); } DefinitionFileWithStyle file = new DefinitionFileWithStyle(definition, typeof(UiRectangle)); _image = DefinitionResolver.Get <NinePatchImage>(Controller, Binding, file["Image"], null); _scaleByUnit = DefinitionResolver.Get(Controller, Binding, file["ScaleByUnit"], false); _scale = (float)DefinitionResolver.Get(Controller, Binding, file["Scale"], 1.0); _color = DefinitionResolver.GetColorWrapper(Controller, Binding, file["Color"]); return(true); }
public void transform_images(string file, double scale) { if (!Directory.Exists("TestResult/NinePatch")) { Directory.CreateDirectory("TestResult/NinePatch"); } var img = new NinePatchImage(File.OpenRead(file), scale); var name = $"TestResult/NinePatch/{ Path.GetFileNameWithoutExtension(file) }x{ scale } " + $"({ img.Left }, { img.Top }, { img.Right }, { img.Bottom })-" + $"({ img.PaddingLeft }, { img.PaddingTop }, { img.PaddingRight }, { img.PaddingBottom }).png"; if (file == "TestImages/NinePatch.png" && scale == 1) { Assert.Equal(43, img.Left); Assert.Equal(53, img.Right); Assert.Equal(46, img.Top); Assert.Equal(44, img.Bottom); Assert.Equal(17, img.PaddingLeft); Assert.Equal(29, img.PaddingRight); Assert.Equal(21, img.PaddingTop); Assert.Equal(19, img.PaddingBottom); } using (var output = File.OpenWrite(name)) { img.SaveAsPng(output); } for (int i = 0; i < img.Patches.Count; i++) { var patch = $"TestResult/NinePatch/{ Path.GetFileNameWithoutExtension(file) }x{ scale }-{ i }.png"; using (var output = File.OpenWrite(patch)) { img.Patches[i].SaveAsPng(output); } } }
public static Widget CreateFromPrototype(ContentManager content, WidgetPrototype prototype, ref Dictionary <string, WeakReference <Widget> > widgetIdDict) { Widget widget = null; AbstractValue width = ValueFromString(prototype.Width); AbstractValue height = ValueFromString(prototype.Height); AbstractValue horizontal = ValueFromString(prototype.Horizontal); AbstractValue vertical = ValueFromString(prototype.Vertical); HorizontalAlignment halign; switch (prototype.Halign.ToLower()[0]) { case 'r': halign = HorizontalAlignment.Right; break; case 'l': halign = HorizontalAlignment.Left; break; case 'c': default: halign = HorizontalAlignment.Center; break; } VerticalAlignment valign; switch (prototype.Valign.ToLower()[0]) { case 't': valign = VerticalAlignment.Top; break; case 'b': valign = VerticalAlignment.Bottom; break; case 'c': default: valign = VerticalAlignment.Center; break; } /** WIDGET SPECIALIZATIONS **/ if (prototype is LabelWidgetPrototype) { FieldFont font = content.Load <FieldFont>(((LabelWidgetPrototype)prototype).Font); string labelContent = ((LabelWidgetPrototype)prototype).Content; widget = new Label(font, labelContent, halign, horizontal, valign, vertical, width, height); } if (prototype is ImageWidgetPrototype) { TextureRegion2D texture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((ImageWidgetPrototype)prototype).Image)); widget = new Image(texture, halign, horizontal, valign, vertical, width, height); } if (prototype is NinePatchImageWidgetPrototype) { TextureRegion2D texture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((NinePatchImageWidgetPrototype)prototype).Image)); string[] rawThickness = ((NinePatchImageWidgetPrototype)prototype).Thickness.Trim().Split(','); if (rawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } widget = new NinePatchImage(new NinePatchRegion2D(texture, int.Parse(rawThickness[0]), int.Parse(rawThickness[1]), int.Parse(rawThickness[2]), int.Parse(rawThickness[3])), halign, horizontal, valign, vertical, width, height); } if (prototype is PanelWidgetPrototype || prototype is ExternalWidgetPrototype) { widget = new Panel(halign, horizontal, valign, vertical, width, height); } if (prototype is ButtonWidgetPrototype) { TextureRegion2D releasedTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((ButtonWidgetPrototype)prototype).ReleasedImage)); string[] releasedRawThickness = ((ButtonWidgetPrototype)prototype).ReleasedThickness.Trim().Split(','); if (releasedRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } TextureRegion2D hoverTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((ButtonWidgetPrototype)prototype).HoverImage)); string[] hoverRawThickness = ((ButtonWidgetPrototype)prototype).HoverThickness.Trim().Split(','); if (hoverRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } TextureRegion2D pressedTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((ButtonWidgetPrototype)prototype).PressedImage)); string[] pressedRawThickness = ((ButtonWidgetPrototype)prototype).PressedThickness.Trim().Split(','); if (pressedRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } widget = new Button(new NinePatchRegion2D(releasedTexture, int.Parse(releasedRawThickness[0]), int.Parse(releasedRawThickness[1]), int.Parse(releasedRawThickness[2]), int.Parse(releasedRawThickness[3])), new NinePatchRegion2D(hoverTexture, int.Parse(hoverRawThickness[0]), int.Parse(hoverRawThickness[1]), int.Parse(hoverRawThickness[2]), int.Parse(hoverRawThickness[3])), new NinePatchRegion2D(pressedTexture, int.Parse(pressedRawThickness[0]), int.Parse(pressedRawThickness[1]), int.Parse(pressedRawThickness[2]), int.Parse(pressedRawThickness[3])), halign, horizontal, valign, vertical, width, height); if (((ButtonWidgetPrototype)prototype).OnClick.Length > 0) { string[] onclickParts = ((ButtonWidgetPrototype)prototype).OnClick.Trim().Split(':'); switch (onclickParts[0].ToLower()) { case "queue": ((Button)widget).Action = () => { string assemblyQualifiedName = string.Format("GameJam.Events.{0}, GameJam", onclickParts[1]); IEvent evt = (IEvent)Activator.CreateInstance(Type.GetType(assemblyQualifiedName)); EventManager.Instance.QueueEvent(evt); }; break; default: throw new Exception(string.Format("Unkown action type: `{0}`", onclickParts[0])); } } ((Button)widget).rightID = ((ButtonWidgetPrototype)prototype).RightID; ((Button)widget).leftID = ((ButtonWidgetPrototype)prototype).LeftID; ((Button)widget).aboveID = ((ButtonWidgetPrototype)prototype).AboveID; ((Button)widget).belowID = ((ButtonWidgetPrototype)prototype).BelowID; ((Button)widget).isSelected = ((ButtonWidgetPrototype)prototype).IsSelected; } if (prototype is SliderWidgetPrototype) { TextureRegion2D releasedTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((SliderWidgetPrototype)prototype).ReleasedImage)); string[] releasedRawThickness = ((SliderWidgetPrototype)prototype).ReleasedThickness.Trim().Split(','); if (releasedRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } TextureRegion2D hoverTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((SliderWidgetPrototype)prototype).HoverImage)); string[] hoverRawThickness = ((SliderWidgetPrototype)prototype).HoverThickness.Trim().Split(','); if (hoverRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } TextureRegion2D pressedTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((SliderWidgetPrototype)prototype).PressedImage)); string[] pressedRawThickness = ((SliderWidgetPrototype)prototype).PressedThickness.Trim().Split(','); if (pressedRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } widget = new Slider(new NinePatchRegion2D(releasedTexture, int.Parse(releasedRawThickness[0]), int.Parse(releasedRawThickness[1]), int.Parse(releasedRawThickness[2]), int.Parse(releasedRawThickness[3])), new NinePatchRegion2D(hoverTexture, int.Parse(hoverRawThickness[0]), int.Parse(hoverRawThickness[1]), int.Parse(hoverRawThickness[2]), int.Parse(hoverRawThickness[3])), new NinePatchRegion2D(pressedTexture, int.Parse(pressedRawThickness[0]), int.Parse(pressedRawThickness[1]), int.Parse(pressedRawThickness[2]), int.Parse(pressedRawThickness[3])), halign, horizontal, valign, vertical, width, height, ((SliderWidgetPrototype)prototype).isVertical, ((SliderWidgetPrototype)prototype).isHorizontal, ((SliderWidgetPrototype)prototype).divisions, ((SliderWidgetPrototype)prototype).cvar); ((Slider)widget).rightID = ((SliderWidgetPrototype)prototype).RightID; ((Slider)widget).leftID = ((SliderWidgetPrototype)prototype).LeftID; ((Slider)widget).aboveID = ((SliderWidgetPrototype)prototype).AboveID; ((Slider)widget).belowID = ((SliderWidgetPrototype)prototype).BelowID; ((Slider)widget).isSelected = ((SliderWidgetPrototype)prototype).IsSelected; } if (prototype is DropDownPanelWidgetPrototype) { TextureRegion2D releasedTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((DropDownPanelWidgetPrototype)prototype).ReleasedImage)); string[] releasedRawThickness = ((DropDownPanelWidgetPrototype)prototype).ReleasedThickness.Trim().Split(','); if (releasedRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } TextureRegion2D hoverTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((DropDownPanelWidgetPrototype)prototype).HoverImage)); string[] hoverRawThickness = ((DropDownPanelWidgetPrototype)prototype).HoverThickness.Trim().Split(','); if (hoverRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } TextureRegion2D pressedTexture = content.Load <TextureAtlas>("complete_texture_atlas") .GetRegion((((DropDownPanelWidgetPrototype)prototype).PressedImage)); string[] pressedRawThickness = ((DropDownPanelWidgetPrototype)prototype).PressedThickness.Trim().Split(','); if (pressedRawThickness.Length != 4) { throw new Exception("NinePatchImage thickness must be integers in the for `left,top,right,bottom`."); } AbstractValue contentsWidth = ValueFromString(((DropDownPanelWidgetPrototype)prototype).ContentsInfo.Width); AbstractValue contentsHeight = ValueFromString(((DropDownPanelWidgetPrototype)prototype).ContentsInfo.Height); string[] rawEventTypes = ((DropDownPanelWidgetPrototype)prototype).CloseOn.Split(','); Type[] eventTypes = new Type[rawEventTypes.Length]; for (int i = 0; i < rawEventTypes.Length; i++) { string rawEventType = rawEventTypes[i]; string assemblyQualifiedName = string.Format("GameJam.Events.{0}, GameJam", rawEventType); eventTypes[i] = Type.GetType(assemblyQualifiedName); } widget = new DropDownPanel(new NinePatchRegion2D(releasedTexture, int.Parse(releasedRawThickness[0]), int.Parse(releasedRawThickness[1]), int.Parse(releasedRawThickness[2]), int.Parse(releasedRawThickness[3])), new NinePatchRegion2D(hoverTexture, int.Parse(hoverRawThickness[0]), int.Parse(hoverRawThickness[1]), int.Parse(hoverRawThickness[2]), int.Parse(hoverRawThickness[3])), new NinePatchRegion2D(pressedTexture, int.Parse(pressedRawThickness[0]), int.Parse(pressedRawThickness[1]), int.Parse(pressedRawThickness[2]), int.Parse(pressedRawThickness[3])), halign, horizontal, valign, vertical, width, height, contentsWidth, contentsHeight, eventTypes); ((DropDownPanel)widget).rightID = ((DropDownPanelWidgetPrototype)prototype).RightID; ((DropDownPanel)widget).leftID = ((DropDownPanelWidgetPrototype)prototype).LeftID; ((DropDownPanel)widget).aboveID = ((DropDownPanelWidgetPrototype)prototype).AboveID; ((DropDownPanel)widget).belowID = ((DropDownPanelWidgetPrototype)prototype).BelowID; ((DropDownPanel)widget).isSelected = ((DropDownPanelWidgetPrototype)prototype).IsSelected; foreach (WidgetPrototype childPrototype in ((DropDownPanelWidgetPrototype)prototype).ContentsInfo.Children) { ((DropDownPanel)widget).AddContent(CreateFromPrototype(content, childPrototype, ref widgetIdDict)); } } if (widget is IParentWidget) { List <WidgetPrototype> children = prototype.Children; if (prototype is ExternalWidgetPrototype) { string src = ((ExternalWidgetPrototype)prototype).Source; children = content.Load <List <WidgetPrototype> >(src); } foreach (WidgetPrototype childPrototype in children) { ((IParentWidget)widget).Add(CreateFromPrototype(content, childPrototype, ref widgetIdDict)); } } widget.Hidden = prototype.Hidden; widget.Alpha = prototype.Alpha; if (prototype.AspectRatio.Length > 0) { string[] aspectRatioParts = prototype.AspectRatio.Split(new[] { ':', '/' }); float aspectRatio = 0; if (aspectRatioParts.Length == 1) { aspectRatio = float.Parse(aspectRatioParts[0]); } if (aspectRatioParts.Length == 2) { aspectRatio = float.Parse(aspectRatioParts[0]) / float.Parse(aspectRatioParts[1]); } widget.AspectRatio = aspectRatio; widget.MaintainAspectRatio = true; } if (prototype.ID.Trim().Length > 0) { if (widgetIdDict.ContainsKey(prototype.ID)) { throw new Exception(string.Format("Duplicate Widget ID: '{0}'", prototype.ID)); } widgetIdDict.Add(prototype.ID, new WeakReference <Widget>(widget)); } if (prototype.Class.Trim().Length > 0) { string[] classes = prototype.Class.Trim().ToLower().Split(','); widget.Classes.AddRange(classes); } return(widget); }
public void DrawNinePatchRect(NinePatchImage image, Rectangle target, Color color) { DrawNinePatchRect(image, target, color, 1); }
public static void Draw(this SpriteBatch spriteBatch, NinePatchImage ninePatch, Rectangle rectangle, Color color) { ninePatch.Draw(null, spriteBatch, rectangle, color); }