using System; using System.Collections.Generic; using System.Linq; using System.Text; using VBXSE; namespace WorldOfMinimalism { public class Window { public string id = "window"; //To prevent opening multiple of the same type public bool allowMultiple = false; //True to allow opening multiple instances of this window public int x = 0; public int y = 0; public int width = 0; public int height = 0; public int innerX = 1; //Where the frame ends and the window starts public int innerY = 15; public int innerWidth = 0; //Size of inner window (i.e. non-frame) public int innerHeight = 0; public Sprite frame = null; public bool readyForClose = false; //For holding, register the cursor's last position protected int lastX = 0; protected int lastY = 0; protected int lastInnerX = 0; protected int lastInnerY = 0; public bool CheckForClick(int x, int y) { //Check if it's inside window if (IsInsideWindow(x, y)) { //Then check if it's on inner window int offsetX = GetInnerOffsetX(x); int offsetY = GetInnerOffsetY(y); if (IsInInnerWindow(offsetX, offsetY)) { OnClick(offsetX, offsetY); } else OnFrameClick(x - this.x, y - this.y); //Store information for hold lastX = x; lastY = y; lastInnerX = offsetX; lastInnerY = offsetY; //Regardless of where it clicked, it was part of the window - return true to stop further checking return true; } else return false; } public bool CheckForHold(int x, int y) { //Check if it's outside of window if (!IsInsideWindow(x, y)) return false; //Then check if it's on inner window int offsetX = GetInnerOffsetX(x); int offsetY = GetInnerOffsetY(y); if (IsInInnerWindow(offsetX, offsetY)) { OnHold(offsetX, offsetY); } else { int changeX = x - lastX; int changeY = y - lastY; this.Move(changeX, changeY); } //Store information for hold lastX = x; lastY = y; lastInnerX = offsetX; lastInnerY = offsetY; return true; } public bool CheckForRightClick(int x, int y) { if (IsInsideWindow(x, y)) { CloseWindow(); return true; } return false; } public bool IsInsideWindow(int x, int y) { if (x < this.x) return false; else if (y < this.y) return false; else if (x > this.x + this.width) return false; else if (y > this.y + this.height) return false; else return true; } public int GetInnerOffsetX(int x) { return x - (this.x + this.innerX); } public int GetInnerOffsetY(int y) { return y - (this.y + this.innerY); } public bool IsInInnerWindow(int offsetX, int offsetY) { return (offsetX >= 0 && offsetX < innerWidth && offsetY >= 0 && offsetY < innerHeight); } //Handles left click inside the inner window public virtual void OnClick(int x, int y) { //Inherit this } //Handles left click outside of the inner window public virtual bool OnFrameClick(int x, int y) { return false; } //Handles holding LMB inside the inner window public virtual void OnHold(int x, int y) { //Inherit this } //Move the window and all sprites within it. Don't forget to inherit! public virtual void Move(int diffX, int diffY) { this.x += diffX; this.y += diffY; this.frame.SetPosition(this.frame.position.X + diffX, this.frame.position.Y + diffY); } //Code to execute when the window is closed - inherit to also clean up custom stuff public virtual void OnClose() { SpriteEngine.EraseGObject(frame); } public void CloseWindow() { OnClose(); this.readyForClose = true; } } }