Created
April 3, 2020 11:55
-
-
Save Spartan322/b73e2ecf7474064a4eb5e67e9a726b7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Godot; | |
using SpartansLib; | |
using SpartansLib.Extensions; | |
namespace MSG.Source.Script.UI.Base | |
{ | |
[Tool] | |
public class TopWindowDecoration : HBoxContainer | |
{ | |
public enum StandardButton | |
{ | |
Minimize, | |
Maximize, | |
Exit, | |
Max | |
} | |
public class ButtonArgs : EventArgs { public BaseButton Button; } | |
public event EventHandler<TopWindowDecoration, ButtonArgs> OnButtonPressed; | |
[Export] | |
public string WindowName | |
{ | |
get => windowName; | |
set | |
{ | |
windowName = value; | |
if (Label != null) | |
{ | |
Label.Text = windowName; | |
Label.Update(); | |
Update(); | |
} | |
} | |
} | |
private string windowName; | |
[Export] | |
public NodePath LabelPath; | |
public Label Label; | |
[Export] | |
public NodePath ButtonListPath; | |
public HBoxContainer ButtonList; | |
public readonly Button[] StandardButtons = new Button[3]; | |
[Export] | |
public bool CanMinimize | |
{ | |
get => canMinimize; | |
set | |
{ | |
canMinimize = value; | |
if (MinimizeButton != null) | |
{ | |
MinimizeButton.Visible = canMinimize; | |
MinimizeButton.Update(); | |
Update(); | |
} | |
} | |
} | |
public Button MinimizeButton => StandardButtons[(int)StandardButton.Minimize]; | |
private bool canMinimize; | |
[Export] | |
public bool CanMaximize | |
{ | |
get => canMaximize; | |
set | |
{ | |
canMaximize = value; | |
if (MaximizeButton != null) | |
{ | |
MaximizeButton.Visible = canMaximize; | |
MaximizeButton.Update(); | |
Update(); | |
} | |
} | |
} | |
public Button MaximizeButton => StandardButtons[(int)StandardButton.Maximize]; | |
private bool canMaximize; | |
[Export] | |
public bool CanExit | |
{ | |
get => canExit; | |
set | |
{ | |
canExit = value; | |
if(ExitButton != null) | |
{ | |
ExitButton.Visible = canExit; | |
ExitButton.Update(); | |
Update(); | |
} | |
} | |
} | |
public Button ExitButton => StandardButtons[(int)StandardButton.Exit]; | |
private bool canExit; | |
public override void _Ready() | |
{ | |
Label = GetNode<Label>(LabelPath); | |
ButtonList = GetNode<HBoxContainer>(ButtonListPath); | |
foreach (var b in ButtonList.GetChildren().Cast<Button>()) | |
{ | |
int enumVal = -1; | |
switch (b.Text) | |
{ | |
case "-": enumVal = (int)StandardButton.Minimize; break; | |
case "+": enumVal = (int)StandardButton.Maximize; break; | |
case "x": enumVal = (int)StandardButton.Exit; break; | |
} | |
StandardButtons[enumVal] = b; | |
b.GetSignal(Signal.VisibilityChanged).Connect(this, nameof(OnButtonVisibilityChanged), enumVal); | |
if (!Engine.EditorHint) | |
b.GetSignal(Signal.Pressed).Connect(this, nameof(OnButtonPressed), b); | |
} | |
WindowName = WindowName ?? Label.Text; | |
CanMinimize = CanMinimize; | |
CanMaximize = CanMaximize; | |
CanExit = CanExit; | |
} | |
public void AddButton(BaseButton button, int? index = null) | |
{ | |
ButtonList?.AddChild(button); | |
if (index != null) | |
ButtonList?.MoveChild(button, index.Value); | |
if (!Engine.EditorHint) | |
button.Connect(nameof(Signal.Pressed), this, nameof(OnButtonPressedSignal), new Godot.Collections.Array { button }); | |
} | |
public void RemoveButton(BaseButton button) => ButtonList?.RemoveChild(button); | |
public void RemoveButton(int index) => ButtonList?.GetChild(index).RemoveAndSkip(); | |
private void OnButtonPressedSignal(BaseButton button) | |
=> OnButtonPressed?.Invoke(this, new ButtonArgs { Button = button }); | |
private void OnButtonVisibilityChanged(int val) | |
{ | |
switch((StandardButton)val) | |
{ | |
case StandardButton.Minimize: CanMinimize = MinimizeButton.Visible; break; | |
case StandardButton.Maximize: CanMaximize = MaximizeButton.Visible; break; | |
case StandardButton.Exit: CanExit = ExitButton.Visible; break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment