Skip to content

Commit

Permalink
Merge pull request #16 from manups4e/master
Browse files Browse the repository at this point in the history
Colors and Styles!
  • Loading branch information
manups4e authored Oct 28, 2019
2 parents a736d7d + 8ad4262 commit ce0b1f4
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 86 deletions.
18 changes: 17 additions & 1 deletion MenuExample/MenuExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MenuExample : BaseScript

public void AddMenuKetchup(UIMenu menu)
{
var newitem = new UIMenuCheckboxItem("Add ketchup?", ketchup, "Do you wish to add ketchup?");
var newitem = new UIMenuCheckboxItem("Add ketchup?", UIMenuCheckboxStyle.Cross, ketchup, "Do you wish to add ketchup?");
menu.AddItem(newitem);
menu.OnCheckboxChange += (sender, item, checked_) =>
{
Expand Down Expand Up @@ -110,9 +110,25 @@ public void AddMenuCook(UIMenu menu)
newitem.SetLeftBadge(UIMenuItem.BadgeStyle.None);
};

var colorItem = new UIMenuItem("UIMenuItem with Colors", "~b~Look!!~r~I can be colored ~y~too!!~w~", Color.FromArgb(150, 185, 230, 185), Color.FromArgb(170, 174, 219, 242));
menu.AddItem(colorItem);

var foods = new List<dynamic>
{
"Banana",
"Apple",
"Pizza",
"Quartilicious",
0xF00D, // Dynamic!
};

var BlankItem = new UIMenuSeparatorItem();
menu.AddItem(BlankItem);

var colorListItem = new UIMenuListItem("Colored ListItem.. Really?", foods, 0, "~b~Look!!~r~I can be colored ~y~too!!~w~", Color.FromArgb(150, 185, 230, 185), Color.FromArgb(170, 174, 219, 242));
menu.AddItem(colorListItem);


var SliderProgress = new UIMenuSliderProgressItem("Slider Progress Item", 10, 0);
menu.AddItem(SliderProgress);

Expand Down
85 changes: 54 additions & 31 deletions NativeUI/Items/UIMenuCheckboxItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

namespace NativeUI
{
public class UIMenuCheckboxItem : UIMenuItem
public enum UIMenuCheckboxStyle
{
Cross,
Tick
}

public class UIMenuCheckboxItem : UIMenuItem
{
protected Sprite _checkedSprite;

Expand All @@ -13,13 +19,14 @@ public class UIMenuCheckboxItem : UIMenuItem
/// </summary>
public event ItemCheckboxEvent CheckboxEvent;

/// <summary>
/// Checkbox item with a toggleable checkbox.
/// </summary>
/// <param name="text">Item label.</param>
/// <param name="check">Boolean value whether the checkbox is checked.</param>
public UIMenuCheckboxItem(string text, bool check)
: this(text, check, "")
public UIMenuCheckboxStyle Style { get; set; }

/// <summary>
/// Checkbox item with a toggleable checkbox.
/// </summary>
/// <param name="text">Item label.</param>
/// <param name="check">Boolean value whether the checkbox is checked.</param>
public UIMenuCheckboxItem(string text, bool check) : this(text, check, "")
{
}

Expand All @@ -29,26 +36,49 @@ public UIMenuCheckboxItem(string text, bool check)
/// <param name="text">Item label.</param>
/// <param name="check">Boolean value whether the checkbox is checked.</param>
/// <param name="description">Description for this item.</param>
public UIMenuCheckboxItem(string text, bool check, string description)
: base(text, description)
public UIMenuCheckboxItem(string text, bool check, string description) : this(text, UIMenuCheckboxStyle.Tick, check, description, Color.Transparent, Color.FromArgb(255, 255, 255, 255))
{
const int y = 0;
_checkedSprite = new Sprite("commonmenu", "shop_box_blank", new PointF(410, y + 95), new SizeF(50, 50));
Checked = check;
}

/// <summary>
/// Checkbox item with a toggleable checkbox.
/// </summary>
/// <param name="text">Item label.</param>
/// <param name="style">CheckBox style (Tick or Cross).</param>
/// <param name="check">Boolean value whether the checkbox is checked.</param>
/// <param name="description">Description for this item.</param>
public UIMenuCheckboxItem(string text, UIMenuCheckboxStyle style, bool check, string description) : this(text, style, check, description, Color.Transparent, Color.FromArgb(255, 255, 255, 255))
{
}

/// <summary>
/// Change or get whether the checkbox is checked.
/// </summary>
public bool Checked { get; set; }
/// <summary>
/// Checkbox item with a toggleable checkbox.
/// </summary>
/// <param name="text">Item label.</param>
/// <param name="style">CheckBox style (Tick or Cross).</param>
/// <param name="check">Boolean value whether the checkbox is checked.</param>
/// <param name="description">Description for this item.</param>
/// <param name="mainColor">Main item color.</param>
/// <param name="highlightColor">Highlight item color.</param>
public UIMenuCheckboxItem(string text, UIMenuCheckboxStyle style, bool check, string description, Color mainColor, Color highlightColor) : base(text, description, mainColor, highlightColor)
{
const int y = 0;
Style = style;
_checkedSprite = new Sprite("commonmenu", "shop_box_blank", new PointF(410, y + 95), new SizeF(50, 50));
Checked = check;
}


/// <summary>
/// Change item's position.
/// </summary>
/// <param name="y">New Y value.</param>
public override void Position(int y)
/// <summary>
/// Change or get whether the checkbox is checked.
/// </summary>
public bool Checked { get; set; }

/// <summary>
/// Change item's position.
/// </summary>
/// <param name="y">New Y value.</param>
public override void Position(int y)
{
base.Position(y);
_checkedSprite.Position = new PointF(380 + Offset.X + Parent.WidthOffset, y + 138 + Offset.Y);
Expand All @@ -62,15 +92,8 @@ public override async Task Draw()
{
base.Draw();
_checkedSprite.Position = new PointF(380 + Offset.X + Parent.WidthOffset, _checkedSprite.Position.Y);
if (Selected)
{
_checkedSprite.TextureName = Checked ? "shop_box_tickb" : "shop_box_blankb";
}
else
{
_checkedSprite.TextureName = Checked ? "shop_box_tick" : "shop_box_blank";
}
_checkedSprite.Draw();
_checkedSprite.TextureName = Selected ? (Checked ? (Style == UIMenuCheckboxStyle.Tick ? "shop_box_tickb" : "shop_box_crossb") : "shop_box_blankb") : Checked ? (Style == UIMenuCheckboxStyle.Tick ? "shop_box_tick" : "shop_box_cross") : "shop_box_blank";
_checkedSprite.Draw();
}

public void CheckboxEventTrigger()
Expand Down
4 changes: 3 additions & 1 deletion NativeUI/Items/UIMenuColoredItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using CitizenFX.Core.UI;
using System;
using System.Drawing;
using System.Threading.Tasks;

namespace NativeUI
{
[Obsolete("UIMenuColoredItem is deprecated: use UIMenuItem(text, description, mainColor, hilightColor) instead", true)]
public class UIMenuColoredItem : UIMenuItem
{
public Color MainColor { get; set; }
Expand All @@ -12,7 +14,7 @@ public class UIMenuColoredItem : UIMenuItem
public Color TextColor { get; set; }
public Color HighlightedTextColor { get; set; }

public UIMenuColoredItem(string label, Color color, Color highlightColor) : base(label)
public UIMenuColoredItem(string label, Color color, Color highlightColor) : base(label, "")
{
MainColor = color;
HighlightColor = highlightColor;
Expand Down
103 changes: 64 additions & 39 deletions NativeUI/Items/UIMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,67 @@ public class UIMenuItem

protected UIResText _labelText;

private readonly Color _disabledColor = Color.FromArgb(163, 159, 148); // Why allocating memory for same color every time?

/// <summary>
/// Called when user selects the current item.
/// </summary>
public event ItemActivatedEvent Activated;
public Color MainColor { get; set; }
public Color HighlightColor { get; set; }

public Color TextColor { get; set; }
public Color HighlightedTextColor { get; set; }

/// <summary>
/// Basic menu button.
/// </summary>
/// <param name="text">Button label.</param>
public UIMenuItem(string text) : this(text, "")
{
}
private readonly Color _defaultColor = Color.FromArgb(20, 255, 255, 255);
private readonly Color _disabledColor = Color.FromArgb(163, 159, 148); // Why allocating memory for same color every time?

/// <summary>
/// Basic menu button.
/// Called when user selects the current item.
/// </summary>
/// <param name="text">Button label.</param>
/// <param name="description">Description.</param>
public UIMenuItem(string text, string description)
{
Enabled = true;

_rectangle = new UIResRectangle(new PointF(0, 0), new SizeF(431, 38), Color.FromArgb(20, 255, 255, 255)); // Color.FromArgb(150, 0, 0, 0)
_text = new UIResText(text, new PointF(8, 0), 0.33f, Colors.WhiteSmoke, CitizenFX.Core.UI.Font.ChaletLondon, Alignment.Left);
Description = description;
_selectedSprite = new Sprite("commonmenu", "gradient_nav", new PointF(0, 0), new SizeF(431, 38));
public event ItemActivatedEvent Activated;

_badgeLeft = new Sprite("commonmenu", "", new PointF(0, 0), new SizeF(40, 40));
_badgeRight = new Sprite("commonmenu", "", new PointF(0, 0), new SizeF(40, 40));

_labelText = new UIResText("", new PointF(0, 0), 0.35f) { TextAlignment = Alignment.Right };
}
/// <summary>
/// Basic menu button.
/// </summary>
/// <param name="text">Button label.</param>
public UIMenuItem(string text) : this(text, "", Color.Transparent, Color.FromArgb(255, 255, 255, 255)){}

/// <summary>
/// Basic menu button with description.
/// </summary>
/// <param name="text">Button label.</param>
/// <param name="description">Description.</param>
public UIMenuItem(string text, string description) : this(text, description, Color.Transparent, Color.FromArgb(255, 255, 255, 255)){}

/// <summary>
/// Basic menu button with description and colors.
/// </summary>
/// <param name="text">Button label.</param>
/// <param name="description">Button label.</param>
/// <param name="description">Button label.</param>
/// <param name="description">Button label.</param>
public UIMenuItem(string text, string description, Color color, Color highlightColor)
{
Enabled = true;

MainColor = color;
HighlightColor = highlightColor;

TextColor = Colors.White;
HighlightedTextColor = Colors.Black;

_rectangle = new UIResRectangle(new Point(0, 0), new Size(431, 38), _defaultColor); // Color.FromArgb(150, 0, 0, 0)
_text = new UIResText(text, new Point(8, 0), 0.33f, Colors.WhiteSmoke, CitizenFX.Core.UI.Font.ChaletLondon, Alignment.Left);
Description = description;
_selectedSprite = new Sprite("commonmenu", "gradient_nav", new Point(0, 0), new Size(431, 38));

_badgeLeft = new Sprite("commonmenu", "", new Point(0, 0), new Size(40, 40));
_badgeRight = new Sprite("commonmenu", "", new Point(0, 0), new Size(40, 40));

_labelText = new UIResText("", new Point(0, 0), 0.35f) { TextAlignment = Alignment.Right };
}


/// <summary>
/// Whether this item is currently selected.
/// </summary>
public virtual bool Selected { get; set; }
/// <summary>
/// Whether this item is currently selected.
/// </summary>
public virtual bool Selected { get; set; }


/// <summary>
Expand Down Expand Up @@ -111,14 +131,20 @@ public virtual async Task Draw()
//_selectedSprite.Size = new SizeF(431 + Parent.WidthOffset, 38);

if (Hovered && !Selected)
{
//_rectangle.Color = Color.FromArgb(20, 255, 255, 255); // Why setting color every time? (I set it in ctor)
_rectangle.Draw();
}
if (Selected)
_selectedSprite.Draw();
if (Selected)
{
_selectedSprite.Color = HighlightColor;
_selectedSprite.Draw();
}
else
{

_text.Color = Enabled ? (Selected ? Colors.Black : Colors.WhiteSmoke) : _disabledColor; // No alloc anymore there
_selectedSprite.Color = MainColor;
_selectedSprite.Draw();
}

_text.Color = Enabled ? (Selected ? HighlightedTextColor : TextColor) : _disabledColor; // No alloc anymore there

if (LeftBadge == BadgeStyle.None)
{
Expand Down Expand Up @@ -152,7 +178,6 @@ public virtual async Task Draw()
_labelText.Color = _text.Color = Enabled ? (Selected ? Colors.Black : Colors.WhiteSmoke) : _disabledColor; // No alloc anymore there
_labelText.Draw();
}

_text.Draw();
}

Expand Down
32 changes: 18 additions & 14 deletions NativeUI/Items/UIMenuListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,28 @@ public UIMenuListItem(string text, List<dynamic> items, int index) : this(text,
/// <param name="items">List that contains your items.</param>
/// <param name="index">Index in the list. If unsure user 0.</param>
/// <param name="description">Description for this item.</param>
public UIMenuListItem(string text, List<dynamic> items, int index, string description) : base(text, description)
public UIMenuListItem(string text, List<dynamic> items, int index, string description) : this(text, items, index, description, Color.Transparent, Color.FromArgb(255, 255, 255, 255))
{
const int y = 0;
_items = items;
_arrowLeft = new Sprite("commonmenu", "arrowleft", new PointF(110, 105 + y), new SizeF(30, 30));
_arrowRight = new Sprite("commonmenu", "arrowright", new PointF(280, 105 + y), new SizeF(30, 30));
_itemText = new UIResText("", new PointF(290, y + 104), 0.35f, Colors.White, CitizenFX.Core.UI.Font.ChaletLondon,
Alignment.Left)
{ TextAlignment = Alignment.Right };
Index = index;
}

public UIMenuListItem(string text, List<dynamic> items, int index, string description, Color mainColor, Color higlightColor) : base(text, description, mainColor, higlightColor)
{
const int y = 0;
_items = items;
_arrowLeft = new Sprite("commonmenu", "arrowleft", new PointF(110, 105 + y), new SizeF(30, 30));
_arrowRight = new Sprite("commonmenu", "arrowright", new PointF(280, 105 + y), new SizeF(30, 30));
_itemText = new UIResText("", new PointF(290, y + 104), 0.35f, Colors.White, CitizenFX.Core.UI.Font.ChaletLondon,
Alignment.Left)
{ TextAlignment = Alignment.Right };
Index = index;
}

/// <summary>
/// Change item's position.
/// </summary>
/// <param name="y">New Y position.</param>
public override void Position(int y)

/// <summary>
/// Change item's position.
/// </summary>
/// <param name="y">New Y position.</param>
public override void Position(int y)
{
_arrowLeft.Position = new PointF(300 + Offset.X + Parent.WidthOffset, 147 + y + Offset.Y);
_arrowRight.Position = new PointF(400 + Offset.X + Parent.WidthOffset, 147 + y + Offset.Y);
Expand Down

0 comments on commit ce0b1f4

Please sign in to comment.