public override void MouseMove(MouseMoveEventArgs e) { if (disposing || !IsVisible()) { return; } if (dragging) { Position = new Vector2i((int)e.X - (int)draggingOffset.X, (int)e.Y - (int)draggingOffset.Y); } base.MouseMove(e); return; }
public DebugConsole(string uniqueName, Vector2i size, IResourceCache resourceCache) : base(uniqueName, size, resourceCache) { input = new Textbox(size.X, resourceCache) { ClearFocusOnSubmit = false, drawColor = new Color4(64, 64, 64, 100), textColor = new Color4(255, 250, 240, 255) }; input.OnSubmit += input_OnSubmit; this.BackgroundColor = new Color4(64, 64, 64, 200); this.DrawBackground = true; this.DrawBorder = true; InitializeCommands(); }
public override bool Update(Vector2i mouseS, IMapManager currentMap) { if (currentMap == null) { return(false); } mouseScreen = mouseS; mouseWorld = CluwneLib.ScreenToWorld(mouseScreen); if (pManager.CurrentPermission.IsTile) { return(false); } currentTile = currentMap.GetDefaultGrid().GetTile(mouseWorld); if (!RangeCheck()) { return(false); } var nodes = new List <Vector2>(); if (pManager.CurrentPrototype.MountingPoints != null) { nodes.AddRange( pManager.CurrentPrototype.MountingPoints.Select( current => new Vector2(mouseWorld.X, currentTile.Y + current))); } else { nodes.Add(new Vector2(mouseWorld.X, currentTile.Y + 0.5f)); nodes.Add(new Vector2(mouseWorld.X, currentTile.Y + 1.0f)); nodes.Add(new Vector2(mouseWorld.X, currentTile.Y + 1.5f)); } Vector2 closestNode = (from Vector2 node in nodes orderby(node - mouseWorld).LengthSquared ascending select node).First(); mouseWorld = closestNode + new Vector2(pManager.CurrentPrototype.PlacementOffset.X, pManager.CurrentPrototype.PlacementOffset.Y); mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld); return(true); }
public virtual bool WasClicked(Vector2 worldPos) { if (currentBaseSprite == null || !visible) { return(false); } Sprite spriteToCheck = GetActiveDirectionalSprite(); var bounds = spriteToCheck.GetLocalBounds(); var AABB = Box2.FromDimensions( Owner.GetComponent <ITransformComponent>().Position.X - (bounds.Width / 2), Owner.GetComponent <ITransformComponent>().Position.Y - (bounds.Height / 2), bounds.Width, bounds.Height); if (!AABB.Contains(new Vector2(worldPos.X, worldPos.Y))) { return(false); } // Get the sprite's position within the texture var texRect = spriteToCheck.TextureRect; // Get the clicked position relative to the texture var spritePosition = new Vector2i((int)(worldPos.X - AABB.Left + texRect.Left), (int)(worldPos.Y - AABB.Top + texRect.Top)); if (spritePosition.X < 0 || spritePosition.Y < 0) { return(false); } IResourceCache resCache = IoCManager.Resolve <IResourceCache>(); Dictionary <Texture, string> tmp = resCache.TextureToKey; if (!tmp.ContainsKey(spriteToCheck.Texture)) { return(false); } //if it doesn't exist, something's f****d string textureKey = tmp[spriteToCheck.Texture]; var opacityMap = TextureCache.Textures[textureKey].Image; //get our clickthrough 'map' // Check if the clicked pixel is opaque return(opacityMap.GetPixel((uint)spritePosition.X, (uint)spritePosition.Y).A <= Limits.ClickthroughLimit); }
//The game states have to feed the UI Input!!! This is to allow more flexibility. //Maybe this should be handled in the main methods for input. But then the state wouldnt have power over //this and things like the chat might get difficult. //Other Notes: When a component returns true to an input event the loop stops and so only that control recieves the input. // That way we don't have all components reacting to one event. /// <summary> /// Handles MouseDown event. Returns true if a component accepted and handled the event. /// </summary> public virtual bool MouseDown(MouseButtonEventArgs e) { if (_console.IsVisible()) { if (_console.MouseDown(e)) { return(true); } } if (moveMode) { foreach (IGuiComponent comp in _components) { if (comp.ClientArea.Contains(e.X, e.Y)) { movingComp = comp; dragOffset = (new Vector2i(e.X, e.Y)) - new Vector2i(comp.ClientArea.Left, comp.ClientArea.Top); break; } } return(true); } else { IOrderedEnumerable <IGuiComponent> inputList = from IGuiComponent comp in _components where comp.ReceiveInput orderby comp.ZDepth ascending orderby comp.IsVisible() descending //Invisible controls still recieve input but after everyone else. This is mostly for the inventory and other toggleable components. orderby comp.Focus descending select comp; foreach (IGuiComponent current in inputList) { if (current.MouseDown(e)) { SetFocus(current); return(true); } } return(false); } }
public PropEditWindow(Vector2i size, IResourceCache resourceCache, Object obj) : base("Object Properties : " + obj, size, resourceCache) { Position = new Vector2i((int)(CluwneLib.CurrentRenderTarget.Size.X / 2f) - (int)(ClientArea.Width / 2f), (int)(CluwneLib.CurrentRenderTarget.Size.Y / 2f) - (int)(ClientArea.Height / 2f)); search = new Textbox(150, resourceCache); search.Position = new Vector2i(5, 5); search.OnSubmit += search_OnSubmit; search.ClearOnSubmit = true; search.ClearFocusOnSubmit = false; components.Add(search); assigned = obj; BuildPropList(); Update(0); }
/// <summary> /// Handles MouseMove event. Sent to all visible components. /// </summary> public virtual void MouseMove(MouseMoveEventArgs e) { MousePos = new Vector2i(e.X, e.Y); if (_console.IsVisible()) { _console.MouseMove(e); } IOrderedEnumerable <IGuiComponent> inputList = from IGuiComponent comp in _components where comp.ReceiveInput orderby comp.ZDepth ascending select comp; foreach (IGuiComponent current in inputList) { current.MouseMove(e); } }
public ScrollableContainer(string uniqueName, Vector2i size, IResourceCache resourceCache) { _resourceCache = resourceCache; name = uniqueName; Size = size; clippingRI = new RenderImage(uniqueName, (uint)Size.X, (uint)Size.Y); clippingRI.BlendSettings.ColorSrcFactor = BlendMode.Factor.SrcAlpha; clippingRI.BlendSettings.ColorDstFactor = BlendMode.Factor.OneMinusSrcAlpha; clippingRI.BlendSettings.AlphaSrcFactor = BlendMode.Factor.SrcAlpha; clippingRI.BlendSettings.AlphaDstFactor = BlendMode.Factor.OneMinusSrcAlpha; scrollbarH = new Scrollbar(true, _resourceCache); scrollbarV = new Scrollbar(false, _resourceCache); scrollbarV.size = Size.Y; scrollbarH.Update(0); scrollbarV.Update(0); Update(0); }
public ExamineWindow(Vector2i size, IEntity entity, IResourceCache resourceCache) : base(entity.Name, size, resourceCache) { _entityDescription = new Label(entity.GetDescriptionString(), "CALIBRI", _resourceCache); components.Add(_entityDescription); SetVisible(true); if (entity.TryGetComponent <ISpriteRenderableComponent>(out var component)) { _entitySprite = component.GetCurrentSprite(); _entityDescription.Position = new Vector2i(10, (int)_entitySprite.GetLocalBounds().Height + _entityDescription.ClientArea.Height + 10); } else { _entityDescription.Position = new Vector2i(10, 10); } }
public override bool Update(Vector2i mouseS, IMapManager currentMap) { if (currentMap == null) { return(false); } mouseScreen = mouseS; mouseWorld = CluwneLib.ScreenToWorld(mouseScreen); currentTile = currentMap.GetDefaultGrid().GetTile(mouseWorld); if (pManager.CurrentPermission.IsTile) { return(false); } if (!RangeCheck()) { return(false); } return(true); }
public MenuWindow() : base("Menu", new Vector2i(140, 130), IoCManager.Resolve <IResourceCache>()) { Position = new Vector2i((int)(CluwneLib.CurrentRenderTarget.Size.X / 2f) - (int)(ClientArea.Width / 2f), (int)(CluwneLib.CurrentRenderTarget.Size.Y / 2f) - (int)(ClientArea.Height / 2f)); button_entity = new Button("Spawn Entities", _resMgr); button_entity.Clicked += button_entity_Clicked; button_entity.Position = new Vector2i(5, 5); button_entity.Update(0); components.Add(button_entity); button_tile = new Button("Spawn Tiles", _resMgr); button_tile.Clicked += button_tile_Clicked; button_tile.Position = new Vector2i(5, button_entity.ClientArea.Bottom + 5); button_tile.Update(0); components.Add(button_tile); button_quit = new Button("Quit", _resMgr); button_quit.Clicked += button_quit_Clicked; button_quit.Position = new Vector2i(5, button_tile.ClientArea.Bottom + 20); button_quit.Update(0); components.Add(button_quit); }
public override bool Update(Vector2i mouseS, IMapManager currentMap) { if (currentMap == null) { return(false); } mouseScreen = mouseS; mouseWorld = CluwneLib.ScreenToWorld(mouseScreen); currentTile = currentMap.GetDefaultGrid().GetTile(mouseWorld); if (!RangeCheck()) { return(false); } if (pManager.CurrentPermission.IsTile) { mouseWorld = new Vector2(currentTile.X + 0.5f, currentTile.Y + 0.5f); mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld); } else { mouseWorld = new Vector2(currentTile.X + 0.5f + pManager.CurrentPrototype.PlacementOffset.X, currentTile.Y + 0.5f + pManager.CurrentPrototype.PlacementOffset.Y); mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld); if (CheckCollision()) { return(false); } } return(true); }
//The game states have to feed the UI Input!!! This is to allow more flexibility. //Maybe this should be handled in the main methods for input. But then the state wouldnt have power over //this and things like the chat might get difficult. //Other Notes: When a component returns true to an input event the loop stops and so only that control recieves the input. // That way we don't have all Components reacting to one event. /// <summary> /// Handles MouseDown event. Returns true if a component accepted and handled the event. /// </summary> public virtual bool MouseDown(MouseButtonEventArgs e) { if (_console.Visible && _console.MouseDown(e)) { return(true); } if (moveMode) { foreach (var comp in _components) { if (comp.ClientArea.Translated(comp.Position).Contains(e.X, e.Y)) { movingComp = comp; dragOffset = new Vector2i(e.X, e.Y) - new Vector2i(comp.ClientArea.Left, comp.ClientArea.Top); break; } } return(true); } return(_components.Any(current => current.MouseDown(e))); }
public static float Length(this Vector2i vec) => (float)Math.Sqrt(LengthSquared(vec));
public override void Update(float frameTime) { SpriteLocation = new Vector2(SpriteLocation.X + (Velocity.X * frameTime), SpriteLocation.Y + (Velocity.Y * frameTime)); spriteRotation += RotationSpeed * frameTime; if (BounceRotate && Math.Abs(spriteRotation) > BounceRotateAngle) { RotationSpeed = -RotationSpeed; } var bounds = DrawSprite.GetLocalBounds(); ClientArea = Box2i.FromDimensions((int)SpriteLocation.X, (int)SpriteLocation.Y, (int)bounds.Width, (int)bounds.Height); //Outside screen. Does not respect rotation. FIX. if (ClientArea.Left > CluwneLib.Window.Viewport.Size.X) { SpriteLocation = new Vector2((0 - bounds.Width), SpriteLocation.Y); } else if (ClientArea.Left < (0 - bounds.Width)) { SpriteLocation = new Vector2(CluwneLib.Window.Viewport.Size.X, SpriteLocation.Y); } if (ClientArea.Top > CluwneLib.Window.Viewport.Size.Y) { SpriteLocation = new Vector2(SpriteLocation.X, (0 - bounds.Height)); } else if (ClientArea.Top < (0 - bounds.Height)) { SpriteLocation = new Vector2(SpriteLocation.X, CluwneLib.Window.Viewport.Size.Y); } if (MouseParallax) { float ParX = 0; float ParY = 0; if (MouseParallaxHorizontal) { ParX = Math.Abs(_uiMgr.MousePos.X - (CluwneLib.Window.Viewport.Size.X)); ParX *= ParallaxScale; } if (MouseParallaxVertical) { ParY = Math.Abs(_uiMgr.MousePos.Y - ((CluwneLib.Window.Viewport.Size.Y))); ParY *= ParallaxScale; } ParallaxOffset = new Vector2(ParX, ParY); } else { ParallaxOffset = new Vector2(); } Position = new Vector2i((int)SpriteLocation.X, (int)SpriteLocation.Y); }
/// <summary> /// Check if the world position is inside of the sprite texture. This checks both sprite bounds and transparency. /// </summary> /// <param name="worldPos">World position to check.</param> /// <returns>Is the world position inside of the sprite?</returns> public virtual bool WasClicked(LocalCoordinates worldPos) { var spriteToCheck = GetActiveDirectionalSprite(); if (spriteToCheck == null || !visible) { return(false); } var screenScale = CluwneLib.Camera.PixelsPerMeter; // local screen bounds var localBounds = spriteToCheck.LocalBounds; // local world bounds var worldBounds = localBounds.Scale(1.0f / screenScale); // move the origin from bottom right to center worldBounds = worldBounds.Translated(new Vector2(-worldBounds.Width / 2, -worldBounds.Height / 2)); // absolute world bounds worldBounds = worldBounds.Translated(transform.WorldPosition); // check if clicked inside of the rectangle if (!worldBounds.Contains(worldPos.ToWorld().Position)) { return(false); } // Get the sprite's position within the texture var texRect = spriteToCheck.TextureRect; // Get the clicked position relative to the texture (World to Texture) var pixelPos = new Vector2i((int)((worldPos.X - worldBounds.Left) * screenScale), (int)((worldPos.Y - worldBounds.Top) * screenScale)); // offset pos by texture sub-rectangle pixelPos = pixelPos + new Vector2i(texRect.Left, texRect.Top); // make sure the position is actually inside the texture if (!texRect.Contains(pixelPos.X, pixelPos.Y)) { throw new InvalidOperationException("The click was inside the sprite bounds, but not inside the texture bounds? Check yo math."); } // fetch texture key of the sprite var resCache = IoCManager.Resolve <IResourceCache>(); if (!resCache.TextureToKey.TryGetValue(spriteToCheck.Texture, out string textureKey)) { throw new InvalidOperationException("Trying to look up a texture that does not exist in the ResourceCache."); } // use the texture key to fetch the Image of the sprite if (!TextureCache.Textures.TryGetValue(textureKey, out TextureInfo texInfo)) { throw new InvalidOperationException("The texture exists in the ResourceCache, but not in the CluwneLib TextureCache?"); } // Check if the clicked pixel is transparent enough in the Image return(texInfo.Image[(uint)pixelPos.X, (uint)pixelPos.Y].AByte >= Limits.ClickthroughLimit); }
public override bool Update(Vector2i mouseS, IMapManager currentMap) { if (currentMap == null) { return(false); } mouseScreen = mouseS; mouseWorld = CluwneLib.ScreenToWorld(mouseScreen); if (pManager.CurrentPermission.IsTile) { return(false); } currentTile = currentMap.GetDefaultGrid().GetTile(mouseWorld); if (!RangeCheck()) { return(false); } var manager = IoCManager.Resolve <IClientEntityManager>(); IOrderedEnumerable <IEntity> snapToEntities = from IEntity entity in manager.GetEntitiesInRange(mouseWorld, snapToRange) where entity.Prototype == pManager.CurrentPrototype orderby (entity.GetComponent <ITransformComponent>( ).Position - mouseWorld).LengthSquared ascending select entity; if (snapToEntities.Any()) { IEntity closestEntity = snapToEntities.First(); if (closestEntity.TryGetComponent <ISpriteRenderableComponent>(out var component)) { var closestSprite = component.GetCurrentSprite(); var closestBounds = closestSprite.GetLocalBounds(); var closestRect = Box2.FromDimensions( closestEntity.GetComponent <ITransformComponent>().Position.X - closestBounds.Width / 2f, closestEntity.GetComponent <ITransformComponent>().Position.Y - closestBounds.Height / 2f, closestBounds.Width, closestBounds.Height); var sides = new Vector2[] { new Vector2(closestRect.Left + (closestRect.Width / 2f), closestRect.Top - closestBounds.Height / 2f), new Vector2(closestRect.Left + (closestRect.Width / 2f), closestRect.Bottom + closestBounds.Height / 2f), new Vector2(closestRect.Left - closestBounds.Width / 2f, closestRect.Top + (closestRect.Height / 2f)), new Vector2(closestRect.Right + closestBounds.Width / 2f, closestRect.Top + (closestRect.Height / 2f)) }; Vector2 closestSide = (from Vector2 side in sides orderby(side - mouseWorld).LengthSquared ascending select side).First(); mouseWorld = closestSide; mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld); } } if (CheckCollision()) { return(false); } return(true); }
// Vector2i public static int LengthSquared(this Vector2i vec) => vec.X * vec.X + vec.Y * vec.Y;
/// <summary> /// Transforms a point from the screen (pixel) space, to world (tile) space. /// </summary> public static Vector2 ScreenToWorld(Vector2i point) { return(((Vector2)point - ScreenViewportSize / 2) / TileSize + WorldCenter); }
/// <summary> /// Transforms a point from the screen (pixel) space, to world (tile) space. /// </summary> public static Vector2 ScreenToWorld(Vector2i point) { return(((Vector2)point - Window.Viewport.Size / 2) / Window.Camera.PixelsPerMeter + Window.Camera.Position); }
public static Box2i FromDimensions(Vector2i position, Vector2i size) { return(FromDimensions(position.X, position.Y, size.X, size.Y)); }
public static Vector2f ToFloat(this Vector2i vec) => new Vector2f(vec.X, vec.Y);
public static SFML.System.Vector2i Convert(this Vector2i vector) { return(new SFML.System.Vector2i(vector.X, vector.Y)); }
public bool Contains(Vector2i point) { return(Contains(point, true)); }