Skip to content

Commit

Permalink
working on adding serialization (RonenNess#85) + fixed bug (RonenNess#86
Browse files Browse the repository at this point in the history
)
  • Loading branch information
RonenNess committed Jul 14, 2018
1 parent eba9cdb commit a623eca
Show file tree
Hide file tree
Showing 28 changed files with 767 additions and 84 deletions.
1 change: 1 addition & 0 deletions GeonBit.UI/GeonBit.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="Source\Resources.cs" />
<Compile Include="Source\UserInterface.cs" />
<Compile Include="Source\Utils\MessageBox.cs" />
<Compile Include="Source\Utils\SerializedDictionary.cs" />
<Compile Include="Source\Utils\SimpleFileMenu.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions GeonBit.UI/Source/Entities/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ public enum ButtonSkin
/// <summary>
/// A clickable button with label on it.
/// </summary>
[System.Serializable]
public class Button : Entity
{
/// <summary>
/// Static ctor.
/// </summary>
static Button()
{
Entity.MakeSerializable(typeof(Button));
}

// button skin
ButtonSkin _skin;

Expand Down Expand Up @@ -91,6 +100,13 @@ public Button(string text, ButtonSkin skin = ButtonSkin.Default, Anchor anchor =
AddChild(ButtonParagraph, true);
}

/// <summary>
/// Create button with default params and without text.
/// </summary>
public Button() : this(string.Empty)
{
}

/// <summary>
/// Override the default theme textures and set a custom skin for this specific button.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions GeonBit.UI/Source/Entities/CheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ namespace GeonBit.UI.Entities
/// A checkbox entity, eg a label with a square you can mark as checked or uncheck.
/// Holds a boolean value.
/// </summary>
[System.Serializable]
public class CheckBox : Entity
{
/// <summary>
/// Static ctor.
/// </summary>
static CheckBox()
{
Entity.MakeSerializable(typeof(CheckBox));
}

/// <summary>CheckBox label. Use this if you want to change the checkbox text or font style.</summary>
public Paragraph TextParagraph;

Expand Down Expand Up @@ -70,6 +79,13 @@ public CheckBox(string text, Anchor anchor = Anchor.Auto, Vector2? size = null,
Checked = isChecked;
}

/// <summary>
/// Create checkbox without text.
/// </summary>
public CheckBox() : this(string.Empty)
{
}

/// <summary>
/// Is the checkbox a natrually-interactable entity.
/// </summary>
Expand Down
20 changes: 18 additions & 2 deletions GeonBit.UI/Source/Entities/ColoredRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ namespace GeonBit.UI.Entities
/// <summary>
/// A colored rectangle with outline.
/// </summary>
[System.Serializable]
public class ColoredRectangle : Entity
{
/// <summary>
/// Static ctor.
/// </summary>
static ColoredRectangle()
{
Entity.MakeSerializable(typeof(ColoredRectangle));
}

/// <summary>
/// Default rectangle styling. Override this dictionary to change the way default rectangles appear.
/// </summary>
Expand All @@ -35,12 +44,12 @@ public class ColoredRectangle : Entity
/// <param name="size">Rectangle size in pixels.</param>
/// <param name="anchor">Position anchor.</param>
/// <param name="offset">Offset from position anchor.</param>
public ColoredRectangle(Color fillColor, Color outlineColor, int outlineWidth, Vector2 size, Anchor anchor = Anchor.Auto, Vector2? offset = null) :
public ColoredRectangle(Color fillColor, Color? outlineColor = null, int outlineWidth = 1, Vector2? size = null, Anchor anchor = Anchor.Auto, Vector2? offset = null) :
base(size, anchor, offset)
{
UpdateStyle(DefaultStyle);
FillColor = fillColor;
OutlineColor = outlineColor;
OutlineColor = outlineColor ?? Color.Black;
OutlineWidth = outlineWidth;
}

Expand Down Expand Up @@ -75,6 +84,13 @@ public ColoredRectangle(Color fillColor, Vector2 size, Anchor anchor = Anchor.Au
OutlineWidth = 0;
}

/// <summary>
/// Create default colored rectangle.
/// </summary>
public ColoredRectangle() : this(Color.White)
{
}

/// <summary>
/// Create the rectangle with default styling.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions GeonBit.UI/Source/Entities/DropDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ namespace GeonBit.UI.Entities
/// DropDown is just like a list, but it only shows the currently selected value unless clicked on (the list is
/// only revealed while interacted with).
/// </summary>
[System.Serializable]
public class DropDown : Entity
{
/// <summary>
/// Static ctor.
/// </summary>
static DropDown()
{
Entity.MakeSerializable(typeof(DropDown));
}

/// <summary>Default text to show when no value is selected from the list.</summary>
public string DefaultText
{
Expand Down Expand Up @@ -113,6 +122,7 @@ public Image ArrowDownImage
public static int ArrowSize = 30;

/// <summary>Special callback to execute when list size changes.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnListChange = null;

/// <summary>
Expand Down Expand Up @@ -200,6 +210,13 @@ public DropDown(Vector2 size, Anchor anchor = Anchor.Auto, Vector2? offset = nul
_selectedTextPanel.PropagateEventsTo(this);
}

/// <summary>
/// Create default dropdown.
/// </summary>
public DropDown() : this(new Vector2(0, 200))
{
}

/// <summary>
/// Is the DropDown list currentle opened (visible).
/// </summary>
Expand Down
108 changes: 88 additions & 20 deletions GeonBit.UI/Source/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,51 @@ public enum EntityState
/// Basic UI entity.
/// All entities inherit from this class and share this API.
/// </summary>
public class Entity
[System.Serializable]
public abstract class Entity
{
/// <summary>
/// Static ctor.
/// </summary>
static Entity()
{
Entity.MakeSerializable(typeof(Entity));
}

// list of child elements
private List<Entity> _children = new List<Entity>();

/// <summary>
/// Get / set children list.
/// </summary>
public List<Entity> Children
{
get
{
return _children;
}
set
{
ClearChildren();
foreach (var child in value) AddChild(child);
}
}

// list of sorted children
private List<Entity> _sortedChildren;

// all child types
internal static List<System.Type> _serializableTypes = new List<System.Type>();

/// <summary>
/// Make an entity type serializable.
/// </summary>
/// <param name="type">Entity type to make serializable.</param>
public static void MakeSerializable(System.Type type)
{
_serializableTypes.Add(type);
}

// do we need to update sorted children list?
internal bool _needToSortChildren = true;

Expand Down Expand Up @@ -257,6 +294,7 @@ public class Entity
private uint _parentLastDestRectVersion = 0;

/// <summary>Optional data you can attach to this entity and retrieve later (for example when handling events).</summary>
[System.Xml.Serialization.XmlIgnore]
public object AttachedData = null;

/// <summary>
Expand All @@ -279,69 +317,91 @@ public class Entity
public static StyleSheet DefaultStyle = new StyleSheet();

/// <summary>Callback to execute when mouse button is pressed over this entity (called once when button is pressed).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnMouseDown = null;

/// <summary>Callback to execute when right mouse button is pressed over this entity (called once when button is pressed).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnRightMouseDown = null;

/// <summary>Callback to execute when mouse button is released over this entity (called once when button is released).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnMouseReleased = null;

/// <summary>Callback to execute every frame while mouse button is pressed over the entity.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback WhileMouseDown = null;

/// <summary>Callback to execute every frame while right mouse button is pressed over the entity.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback WhileRightMouseDown = null;

/// <summary>Callback to execute every frame while mouse is hovering over the entity (not called while mouse button is down).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback WhileMouseHover = null;

/// <summary>Callback to execute every frame while mouse is hovering over the entity, even if mouse is down.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback WhileMouseHoverOrDown = null;

/// <summary>Callback to execute when user clicks on this entity (eg release mouse over it).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnClick = null;

/// <summary>Callback to execute when user clicks on this entity with right mouse button (eg release mouse over it).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnRightClick = null;

/// <summary>Callback to execute when entity value changes (relevant only for entities with value).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnValueChange = null;

/// <summary>Callback to execute when mouse start hovering over this entity (eg enters its region).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnMouseEnter = null;

/// <summary>Callback to execute when mouse stop hovering over this entity (eg leaves its region).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnMouseLeave = null;

/// <summary>Callback to execute when mouse wheel scrolls and this entity is the active entity.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnMouseWheelScroll = null;

/// <summary>Called when entity starts getting dragged (only if draggable).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnStartDrag = null;

/// <summary>Called when entity stop getting dragged (only if draggable).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnStopDrag = null;

/// <summary>Called every frame while the entity is being dragged.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback WhileDragging = null;

/// <summary>Callback to execute every frame before this entity is rendered.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback BeforeDraw = null;

/// <summary>Callback to execute every frame after this entity is rendered.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback AfterDraw = null;

/// <summary>Callback to execute every frame before this entity updates.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback BeforeUpdate = null;

/// <summary>Callback to execute every frame after this entity updates.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback AfterUpdate = null;

/// <summary>Callback to execute every time the visibility of this entity changes (also invokes when parent becomes invisible / visible again).</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnVisiblityChange = null;

/// <summary>Callback to execute every time this entity focus / unfocus.</summary>
[System.Xml.Serialization.XmlIgnore]
public EventCallback OnFocusChange = null;

/// <summary>
Expand Down Expand Up @@ -648,15 +708,6 @@ public EntityState State
set { _entityState = value; }
}

/// <summary>
/// Return all children of this entity.
/// </summary>
/// <returns>List with all children in entity.</returns>
public List<Entity> GetChildren()
{
return _children;
}

/// <summary>
/// Find and return first occurance of a child entity with a given identifier and specific type.
/// </summary>
Expand Down Expand Up @@ -974,16 +1025,9 @@ public void SetAnchor(Anchor anchor)
/// <param name="offset">New offset to set.</param>
public void SetOffset(Vector2 offset)
{
// if currently dragged:
if (_isBeingDragged)
if (_offset != offset || _dragOffset != offset)
{
_dragOffset = offset;
MarkAsDirty();
}
// if not dragged, set regular offset
else if (_offset != offset)
{
_offset = offset;
_dragOffset = _offset = offset;
MarkAsDirty();
}
}
Expand Down Expand Up @@ -1112,6 +1156,30 @@ protected virtual void DrawChildren(SpriteBatch spriteBatch)
AfterDrawChildren(spriteBatch);
}

/// <summary>
/// Special init after deserializing entity from file.
/// </summary>
public void InitAfterDeserialize()
{
// fix children parent
var temp = _children;
_children = new List<Entity>();
foreach (var child in temp)
{
child._parent = null;
AddChild(child);
}

// mark as dirty
MarkAsDirty();

// update all children
foreach (var child in _children)
{
child.InitAfterDeserialize();
}
}

/// <summary>
/// Called before drawing child entities of this entity.
/// </summary>
Expand Down Expand Up @@ -1698,7 +1766,7 @@ protected Entity GetPreviousEntity(bool skipInvisibles = false)
if (_parent == null) { return null; }

// get siblings and iterate them
List<Entity> siblings = _parent.GetChildren();
List<Entity> siblings = _parent.Children;
Entity prev = null;
foreach (Entity sibling in siblings)
{
Expand Down
Loading

0 comments on commit a623eca

Please sign in to comment.