// In OnInitialize, we place various UIElements onto our UIState (this class). // UIState classes have width and height equal to the full screen, because of this, usually we first define a UIElement that will act as the container for our UI. // We then place various other UIElement onto that container UIElement positioned relative to the container UIElement. public override void OnInitialize() { // Here we define our container UIElement. In DragableUIPanel.cs, you can see that DragableUIPanel is a UIPanel with a couple added features. coinCounterPanel = new DragableUIPanel(); coinCounterPanel.SetPadding(0); // We need to place this UIElement in relation to its Parent. Later we will be calling `base.Append(coinCounterPanel);`. // This means that this class, ExampleUI, will be our Parent. Since ExampleUI is a UIState, the Left and Top are relative to the top left of the screen. coinCounterPanel.Left.Set(400f, 0f); coinCounterPanel.Top.Set(100f, 0f); coinCounterPanel.Width.Set(170f, 0f); coinCounterPanel.Height.Set(70f, 0f); coinCounterPanel.BackgroundColor = new Color(73, 94, 171); // Next, we create another UIElement that we will place. Since we will be calling `coinCounterPanel.Append(playButton);`, Left and Top are relative to the top left of the coinCounterPanel UIElement. // By properly nesting UIElements, we can position things relatively to each other easily. Texture2D buttonPlayTexture = ModContent.GetTexture("Terraria/UI/ButtonPlay"); UIHoverImageButton playButton = new UIHoverImageButton(buttonPlayTexture, "Reset Coins Per Minute Counter"); playButton.Left.Set(110, 0f); playButton.Top.Set(10, 0f); playButton.Width.Set(22, 0f); playButton.Height.Set(22, 0f); // UIHoverImageButton doesn't do anything when Clicked. Here we assign a method that we'd like to be called when the button is clicked. playButton.OnClick += new MouseEvent(PlayButtonClicked); coinCounterPanel.Append(playButton); Texture2D buttonDeleteTexture = ModContent.GetTexture("Terraria/UI/ButtonDelete"); UIHoverImageButton closeButton = new UIHoverImageButton(buttonDeleteTexture, Language.GetTextValue("LegacyInterface.52")); // Localized text for "Close" closeButton.Left.Set(140, 0f); closeButton.Top.Set(10, 0f); closeButton.Width.Set(22, 0f); closeButton.Height.Set(22, 0f); closeButton.OnClick += new MouseEvent(CloseButtonClicked); coinCounterPanel.Append(closeButton); // UIMoneyDisplay is a fairly complicated custom UIElement. UIMoneyDisplay handles drawing some text and coin textures. // Organization is key to managing UI design. Making a contained UIElement like UIMoneyDisplay will make many things easier. moneyDiplay = new UIMoneyDisplay(); moneyDiplay.Left.Set(15, 0f); moneyDiplay.Top.Set(20, 0f); moneyDiplay.Width.Set(100f, 0f); moneyDiplay.Height.Set(0, 1f); coinCounterPanel.Append(moneyDiplay); base.Append(coinCounterPanel); // As a recap, ExampleUI is a UIState, meaning it covers the whole screen. We attach coinCounterPanel to ExampleUI some distance from the top left corner. // We then place playButton, closeButton, and moneyDiplay onto coinCounterPanel so we can easily place these UIElements relative to coinCounterPanel. // Since coinCounterPanel will move, this proper organization will move playButton, closeButton, and moneyDiplay properly when coinCounterPanel moves. }
public override void OnInitialize() { coinCounterPanel = new UIPanel(); coinCounterPanel.SetPadding(0); coinCounterPanel.Left.Set(400f, 0f); coinCounterPanel.Top.Set(100f, 0f); coinCounterPanel.Width.Set(170f, 0f); coinCounterPanel.Height.Set(70f, 0f); coinCounterPanel.BackgroundColor = new Color(73, 94, 171); coinCounterPanel.OnMouseDown += new UIElement.MouseEvent(DragStart); coinCounterPanel.OnMouseUp += new UIElement.MouseEvent(DragEnd); Texture2D buttonPlayTexture = ModLoader.GetTexture("Terraria/UI/ButtonPlay"); UIImageButton playButton = new UIImageButton(buttonPlayTexture); playButton.Left.Set(110, 0f); playButton.Top.Set(10, 0f); playButton.Width.Set(22, 0f); playButton.Height.Set(22, 0f); playButton.OnClick += new MouseEvent(PlayButtonClicked); coinCounterPanel.Append(playButton); Texture2D buttonDeleteTexture = ModLoader.GetTexture("Terraria/UI/ButtonDelete"); UIImageButton closeButton = new UIImageButton(buttonDeleteTexture); closeButton.Left.Set(140, 0f); closeButton.Top.Set(10, 0f); closeButton.Width.Set(22, 0f); closeButton.Height.Set(22, 0f); closeButton.OnClick += new MouseEvent(CloseButtonClicked); coinCounterPanel.Append(closeButton); moneyDiplay = new UIMoneyDisplay(); moneyDiplay.Left.Set(15, 0f); moneyDiplay.Top.Set(20, 0f); moneyDiplay.Width.Set(100f, 0f); moneyDiplay.Height.Set(0, 1f); coinCounterPanel.Append(moneyDiplay); base.Append(coinCounterPanel); }