faça uma forma mais fácil de eu mover os textos: using System.Collections.Generic; using…
- User
faça uma forma mais fácil de eu mover os textos: using System.Collections.Generic; using Oxide.Core.Plugins; using Oxide.Game.Rust.Cui; using UnityEngine;
namespace Oxide.Plugins { [Info("QuarryComputerUi", "Marte6", "1.0.0")] public class QuarryComputerUi : RustPlugin { [PluginReference] Plugin ImageLibrary;
private static readonly HashSet<BasePlayer> playersWithPopup = new(); #region Initialization private void Init() { DestroyAllUi(); } #endregion #region Commands [ChatCommand("ui")] private void CmdOpenUi(BasePlayer player) { DestroyUi(player); CreateUi(player); } [ConsoleCommand("quarrycompui.close")] private void CmdClose(ConsoleSystem.Arg arg) { var player = arg.Player(); if (player != null) { DestroyUi(player); PrintToChat(player, "Close button clicked."); } } [ConsoleCommand("quarrycompui.remove")] private void CmdRemoveQuarry(ConsoleSystem.Arg arg) { var player = arg.Player(); TogglePopup(player, "Remove the Quarry?", "quarrycompui.confirmremove", "quarrycompui.cancelremove"); } [ConsoleCommand("quarrycompui.confirmremove")] private void CmdConfirmRemove(ConsoleSystem.Arg arg) { var player = arg.Player(); if (player != null) { ClosePopupAndNotify(player, "Quarry removed."); } } [ConsoleCommand("quarrycompui.cancelremove")] private void CmdCancelRemove(ConsoleSystem.Arg arg) { var player = arg.Player(); if (player != null) { ClosePopupAndNotify(player, "Removal cancelled."); } } [ConsoleCommand("quarrycompui.upgrade")] private void CmdUpgradeQuarry(ConsoleSystem.Arg arg) { var player = arg.Player(); TogglePopup(player, "Upgrade the Quarry?", "quarrycompui.confirmupgrade", "quarrycompui.cancelupgrade"); } [ConsoleCommand("quarrycompui.confirmupgrade")] private void CmdConfirmUpgrade(ConsoleSystem.Arg arg) { var player = arg.Player(); if (player != null) { ClosePopupAndNotify(player, "Quarry upgraded."); } } [ConsoleCommand("quarrycompui.cancelupgrade")] private void CmdCancelUpgrade(ConsoleSystem.Arg arg) { var player = arg.Player(); if (player != null) { ClosePopupAndNotify(player, "Upgrade cancelled."); } } #endregion private void CreateUi(BasePlayer player) { var container = new CuiElementContainer(); AddPanel(container, "0 0", "1 1", "QuarryCompUi_Background", "Overlay", "0 0 0 0.9", true); AddPanel(container, "0.235 0.18", "0.765 0.784", "QuarryCompUi_MainPanel", "QuarryCompUi_Background", "0.6 0.6 0.6 0.95", true); AddInnerSquare(container, "QuarryCompUi_MainPanel", "0 0.9570", "1 1", "0.39 0.39 0.39 1"); AddText(container, "QuarryCompUi_MainPanel", "0.02 0.96", "1 1", "Extraction Interval: 1 sec | Fuel per extraction: 1 Low Grade Fuel", 13, TextAnchor.MiddleLeft); AddCloseButton(container, "QuarryCompUi_MainPanel"); AddImage(container, "QuarryCompUi_Background", "crude.oil", "0.1 0.1", "0.2 0.2", "ResImg"); AddPanel(container, "0.0 0", "0.5 0.94", "QuarryCompUi_Full_Left", "QuarryCompUi_MainPanel", "0 0 0 0"); AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.10 0.05", "0.95 0.90", "0 0 0 0.97"); AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1"); AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.06 0.108", "0.89 0.94", "0 0 1 1"); AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.06 0.865", "0.89 0.870", "0.8 0.8 0.8 1"); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.84", "0.9 0.98", "Quarry Level 1", 15, TextAnchor.MiddleCenter); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.78", "0.9 0.82", "Storage:", 12, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.74", "0.9 0.78", "Fuel: 3 slots", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.7", "0.9 0.74", "Resource: 6 slots", 10, TextAnchor.MiddleLeft); AddInnerSquare(container, "QuarryCompUi_Full_Left", "0.06 0.65", "0.89 0.655", "0.8 0.8 0.8 1"); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.6", "0.9 0.65", "Extraction:", 12, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.56", "0.9 0.6", "Crude Oil: 10", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.52", "0.9 0.56", "HQM Ore: 60", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.3", "0.9 0.34", "Metal Ore: 100", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.26", "0.9 0.3", "Stones: 250", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Left", "0.1 0.22", "0.9 0.26", "Sulfur Ore: 160", 10, TextAnchor.MiddleLeft); AddButton(container, "QuarryCompUi_Full_Left", "Remove Quarry", "quarrycompui.remove", "0.28", "0.11", "0.70", "0.16", "0.6 0.6 0.6 0.95", "1 0 0 1"); AddPanel(container, "0.5 0", "1 0.94", "QuarryCompUi_Full_Right", "QuarryCompUi_MainPanel", "0 0 0 0"); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.10 0.05", "0.95 0.90", "0 0 0 0.97"); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.054 0.1033", "0.895 0.945", "0.8 0.8 0.8 1"); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.108", "0.89 0.94", "0.55 0 0 1"); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.865", "0.89 0.870", "0.8 0.8 0.8 1"); AddText(container, "QuarryCompUi_Full_Right", "0.05 0.84", "0.9 0.98", "Upgrade to Level 2", 15, TextAnchor.MiddleCenter); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.82", "0.89 0.825", "0.8 0.8 0.8 1"); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.78", "0.9 0.82", "Requirements:", 12, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.74", "0.9 0.78", "Scrap: 5000", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.7", "0.9 0.74", "Bone Fragments: 10000", 10, TextAnchor.MiddleLeft); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.65", "0.89 0.655", "0.8 0.8 0.8 1"); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.6", "0.9 0.65", "Storage:", 12, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.56", "0.9 0.6", "Fuel: 6 slots", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.52", "0.9 0.56", "Resource: 12 slots", 10, TextAnchor.MiddleLeft); AddInnerSquare(container, "QuarryCompUi_Full_Right", "0.06 0.47", "0.89 0.475", "0.8 0.8 0.8 1"); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.42", "0.9 0.47", "Crude Oil: 20", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.38", "0.9 0.42", "HQM Ore: 120", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.34", "0.9 0.38", "Metal Ore: 150", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.3", "0.9 0.34", "Stones: 350", 10, TextAnchor.MiddleLeft); AddText(container, "QuarryCompUi_Full_Right", "0.1 0.26", "0.9 0.3", "Sulfur Ore: 200", 10, TextAnchor.MiddleLeft); AddButton(container, "QuarryCompUi_Full_Right", "Upgrade", "quarrycompui.upgrade", "0.35", "0.11", "0.65", "0.16", "0.6 0.6 0.6 0.95", "0 1 0 1"); CuiHelper.AddUi(player, container); } private void AddImage(CuiElementContainer container, string parent, string resourceName, string anchorMin, string anchorMax, string imageName) { string imageUrl = ImageLibrary?.Call("GetImage", resourceName) as string; if (string.IsNullOrEmpty(imageUrl)) { PrintWarning($"Failed to get image for item: {resourceName}"); return; } container.Add( new CuiElement { Name = imageName, Parent = parent, Components = { new CuiRawImageComponent { Png = imageUrl }, new CuiRectTransformComponent { AnchorMin = anchorMin, AnchorMax = anchorMax }, }, } ); } private void AddCloseButton(CuiElementContainer container, string parent) { container.Add( new CuiButton { Button = { Color = "1 0 0 1", Command = "quarrycompui.close", Close = "QuarryCompUi_Background", }, RectTransform = { AnchorMin = "0.965 0.957", AnchorMax = "1 1" }, Text = { Text = "✖", FontSize = 13, Align = TextAnchor.MiddleCenter, Color = "1 1 1 0.5", }, }, parent ); } private void DestroyUi(BasePlayer player) { if (player == null) return; var componentNames = new[] { "QuarryCompUi_Background", "QuarryCompUi_MainPanel", "Overlay_Block", "Popup_Shadow", "Popup_Border", "Popup_Background", "QuarryCompUi_Full_Left", "QuarryCompUi_Full_Right", }; foreach (var name in componentNames) { CuiHelper.DestroyUi(player, name); } playersWithPopup.Remove(player); } private void DestroyAllUi() { foreach (var player in BasePlayer.activePlayerList) { DestroyUi(player); timer.Once(1f, () => CreateUi(player)); } } private void TogglePopup(BasePlayer player, string message, string confirmCmd, string cancelCmd) { if (player != null && !playersWithPopup.Contains(player)) { playersWithPopup.Add(player); ShowPopup(player, message, confirmCmd, cancelCmd); } } private void ShowPopup(BasePlayer player, string message, string confirmCmd, string cancelCmd) { var container = new CuiElementContainer(); AddPanel(container, "0 0", "1 1", "Overlay_Block", "Overlay", "0 0 0 0.9", true); AddPanel(container, "0.4 0.4", "0.6 0.6", "Popup_Shadow", "Overlay_Block", "0 0 0 0.85"); AddPanel(container, "0.0 0.09", "0.95 1.0", "Popup_Border", "Popup_Shadow", "1 1 1 1"); AddPanel(container, "0.01 0.01", "0.991 0.985", "Popup_Background", "Popup_Border", "0 0 0 0.97"); AddText(container, "Popup_Background", "0.1 0.6", "0.9 0.9", message, 16, TextAnchor.MiddleCenter); AddButton(container, "Popup_Background", "Yes", confirmCmd, "0.25", "0.05", "0.45", "0.15", "0.6 0.6 0.6 0.95", "0 1 0 1"); AddButton(container, "Popup_Background", "No", cancelCmd, "0.55", "0.05", "0.75", "0.15", "0.6 0.6 0.6 0.95", "1 0 0 1"); CuiHelper.AddUi(player, container); } private void ClosePopupAndNotify(BasePlayer player, string message) { if (player != null) { foreach (var elem in new[] { "Overlay_Block", "Popup_Shadow", "Popup_Border", "Popup_Background" }) { CuiHelper.DestroyUi(player, elem); } playersWithPopup.Remove(player); PrintToChat(player, message); } } #region UI Helpers private void AddPanel(CuiElementContainer container, string anchorMin, string anchorMax, string panelName, string parent, string color, bool isCursorEnabled = false) { var panel = new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax }, CursorEnabled = isCursorEnabled, }; container.Add(panel, parent, panelName); } private void AddInnerSquare(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string color) { container.Add(new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax } }, parent); } private void AddText(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string text, int fontSize, TextAnchor alignment) { container.Add( new CuiLabel { Text = { Text = text, FontSize = fontSize, Font = "DroidSansMono.ttf", Align = alignment, Color = "1 1 1 1", }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax }, }, parent ); } private void AddButton( CuiElementContainer container, string parent, string text, string command, string anchorMinX, string anchorMinY, string anchorMaxX, string anchorMaxY, string bgColor, string textColor ) { container.Add( new CuiButton { Button = { Color = bgColor, Command = command, Close = "", }, RectTransform = { AnchorMin = $"{anchorMinX} {anchorMinY}", AnchorMax = $"{anchorMaxX} {anchorMaxY}" }, Text = { Text = text, FontSize = 14, Font = "DroidSansMono.ttf", Align = TextAnchor.MiddleCenter, Color = textColor, }, }, parent ); } #endregion } - Assistant
Para facilitar a movimentação de textos no seu código, você pode criar métodos auxiliares que encapsulam a adição de elementos de texto ao
CuiElementContainer. Isso melhora a legibilidade e a modularidade, permitindo que você mova ou ajuste rapidamente blocos de texto sem duplicar constantemente o mesmo código.Segue um exemplo de como isso pode ser implementado:
- Criação de Métodos Auxiliares para Texto: Crie métodos dedicados para adicionar texto ao UI, que aceitam parâmetros como pai, posição, tamanho do texto, alinhamento, etc.
- User
não, quero dizer, de eu mover os textos para cima, para colocar um array de textos por exemplo, e só definir o espacamento
- Assistant