Skip to content

Commit

Permalink
[HaCreator] Implement basic XNA UI button framework (mouse hover, cli…
Browse files Browse the repository at this point in the history
…ck, button animation)

minimap buttons in the simulator as a sample
  • Loading branch information
lastbattle committed Dec 26, 2020
1 parent 3d5c44a commit afc011d
Show file tree
Hide file tree
Showing 14 changed files with 694 additions and 76 deletions.
59 changes: 49 additions & 10 deletions HaCreator/MapSimulator/MapObjects/FieldObject/BackgroundItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class BackgroundItem : BaseDXDrawableItem
{
private readonly int rx;
private readonly int ry;
private readonly int cx;
private readonly int cy;
private BackgroundType type;
private int cx;
private int cy;
private readonly BackgroundType type;
private readonly int a;
private Color color;
private readonly bool front;
Expand All @@ -25,7 +25,7 @@ public class BackgroundItem : BaseDXDrawableItem
private double bgMoveShiftY = 0;

// Custom property
private bool disabledBackground; // disabled background for images that are removed from Map.wz/bg, but entry still presist in maps
private readonly bool disabledBackground; // disabled background for images that are removed from Map.wz/bg, but entry still presist in maps

/// <summary>
///
Expand All @@ -43,8 +43,8 @@ public class BackgroundItem : BaseDXDrawableItem
public BackgroundItem(int cx, int cy, int rx, int ry, BackgroundType type, int a, bool front, List<IDXObject> frames, bool flip, int screenMode)
: base(frames, flip)
{
LastShiftIncreaseX = Environment.TickCount;
LastShiftIncreaseY = Environment.TickCount;
this.LastShiftIncreaseX = Environment.TickCount;
this.LastShiftIncreaseY = Environment.TickCount;
this.rx = rx;
this.cx = cx;
this.ry = ry;
Expand All @@ -57,6 +57,8 @@ public BackgroundItem(int cx, int cy, int rx, int ry, BackgroundType type, int a
color = new Color(0xFF, 0xFF, 0xFF, a);

this.disabledBackground = false;

CheckBGData();
}

/// <summary>
Expand All @@ -74,8 +76,8 @@ public BackgroundItem(int cx, int cy, int rx, int ry, BackgroundType type, int a
public BackgroundItem(int cx, int cy, int rx, int ry, BackgroundType type, int a, bool front, IDXObject frame0, bool flip, int screenMode)
: base(frame0, flip)
{
LastShiftIncreaseX = Environment.TickCount;
LastShiftIncreaseY = Environment.TickCount;
this.LastShiftIncreaseX = Environment.TickCount;
this.LastShiftIncreaseY = Environment.TickCount;
this.rx = rx;
this.cx = cx;
this.ry = ry;
Expand All @@ -91,6 +93,22 @@ public BackgroundItem(int cx, int cy, int rx, int ry, BackgroundType type, int a
this.disabledBackground = true; // removed from Map.wz/bg, but entry still presist in maps
else
this.disabledBackground = false;

CheckBGData();
}

/// <summary>
/// Input validation for the background data.
/// </summary>
private void CheckBGData()
{
if (type != BackgroundType.Regular)
{
if (cx < 0)
this.cx = 0;
if (cy < 0)
this.cy = 0;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -195,6 +213,7 @@ public override void Draw(SpriteBatch sprite, SkeletonMeshRenderer skeletonMeshR
switch (type)
{
default:
break;
case BackgroundType.Regular:
Draw2D(sprite, skeletonMeshRenderer, gameTime, X, Y, frame);
break;
Expand Down Expand Up @@ -226,20 +245,40 @@ public override void Draw(SpriteBatch sprite, SkeletonMeshRenderer skeletonMeshR
}
}

/// <summary>
/// draw_layer(int a1, int punk, IUnknown *a3, int a4, int a5, int a6)
/// </summary>
/// <param name="frame"></param>
/// <param name="mapShiftX"></param>
/// <param name="centerX"></param>
/// <param name="RenderWidth"></param>
/// <param name="RenderObjectScaling"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CalculateBackgroundPosX(IDXObject frame, int mapShiftX, int centerX, int RenderWidth, float RenderObjectScaling)
{
int width = (int) ((RenderWidth / 2) / RenderObjectScaling);
//int width = RenderWidth / 2;

return (rx * (mapShiftX - centerX + width) / 100) + frame.X + width;
return (rx * (mapShiftX - centerX + width) / 100) + frame.X + width;
}

/// <summary>
/// draw_layer(int a1, int punk, IUnknown *a3, int a4, int a5, int a6)
/// </summary>
/// <param name="frame"></param>
/// <param name="mapShiftY"></param>
/// <param name="centerY"></param>
/// <param name="RenderHeight"></param>
/// <param name="RenderObjectScaling"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CalculateBackgroundPosY(IDXObject frame, int mapShiftY, int centerY, int RenderHeight, float RenderObjectScaling)
{
int height = (int)((RenderHeight / 2) / RenderObjectScaling);
//int height = RenderHeight / 2;

return (ry * (mapShiftY - centerY + height) / 100) + frame.Y + height + 95/*hack job for now*/;
return (ry * (mapShiftY - centerY + height) / 100) + frame.Y + height;
}

public Color Color
Expand Down
14 changes: 14 additions & 0 deletions HaCreator/MapSimulator/MapObjects/UIObject/IUIObjectEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HaCreator.MapSimulator.MapObjects.UIObject
{
public interface IUIObjectEvents
{
void CheckMouseEvent(int shiftCenteredX, int shiftCenteredY, MouseState mouseState);
}
}
47 changes: 45 additions & 2 deletions HaCreator/MapSimulator/MapObjects/UIObject/MinimapItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using HaSharedLibrary.Render.DX;
using HaCreator.MapSimulator.MapObjects.UIObject;
using HaSharedLibrary.Render.DX;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Spine;
using System;
using System.Collections.Generic;
Expand All @@ -13,9 +15,10 @@ namespace HaCreator.MapSimulator.Objects.UIObject
/// <summary>
/// Mini map window item
/// </summary>
public class MinimapItem : BaseDXDrawableItem
public class MinimapItem : BaseDXDrawableItem, IUIObjectEvents
{
private readonly BaseDXDrawableItem item_pixelDot;
private readonly List<MapObjects.UIObject.UIObject> uiButtons = new List<MapObjects.UIObject.UIObject>();

public MinimapItem(IDXObject frames, BaseDXDrawableItem item_pixelDot)
: base(frames, false)
Expand All @@ -25,6 +28,15 @@ public MinimapItem(IDXObject frames, BaseDXDrawableItem item_pixelDot)
this.Position = new Point(10, 10); // starting position
}

/// <summary>
/// Add UI buttons to be rendered
/// </summary>
/// <param name="baseClickableUIObject"></param>
public void AddUIButtons(MapObjects.UIObject.UIObject baseClickableUIObject)
{
uiButtons.Add(baseClickableUIObject);
}

public override void Draw(SpriteBatch sprite, SkeletonMeshRenderer skeletonMeshRenderer, GameTime gameTime,
int mapShiftX, int mapShiftY, int centerX, int centerY,
int RenderWidth, int RenderHeight, float RenderObjectScaling, RenderResolution mapRenderResolution,
Expand All @@ -46,6 +58,37 @@ public override void Draw(SpriteBatch sprite, SkeletonMeshRenderer skeletonMeshR
-Position.X, -Position.Y, minimapPosX, minimapPosY,
RenderWidth, RenderHeight, RenderObjectScaling, mapRenderResolution,
TickCount);

//IDXObject lastFrameDrawn = base.LastFrameDrawn;
//int minimapMainFrameWidth = lastFrameDrawn.Width;
//int minimapMainFrameHeight = lastFrameDrawn.Height;

// draw minimap buttons
foreach (MapObjects.UIObject.UIObject uiBtn in uiButtons)
{
BaseDXDrawableItem buttonToDraw = uiBtn.GetBaseDXDrawableItemByState();

// Position drawn is relative to the MinimapItem
int drawRelativeX = -(this.Position.X) - uiBtn.X; // Left to right
int drawRelativeY = -(this.Position.Y) - uiBtn.Y; // Top to bottom

buttonToDraw.Draw(sprite, skeletonMeshRenderer,
gameTime,
drawRelativeX,
drawRelativeY,
centerX, centerY,
RenderWidth, RenderHeight, RenderObjectScaling, mapRenderResolution, TickCount);
}
}

#region IClickableUIObject
public void CheckMouseEvent(int shiftCenteredX, int shiftCenteredY, MouseState mouseState)
{
foreach (MapObjects.UIObject.UIObject uiBtn in uiButtons)
{
uiBtn.CheckMouseEvent(shiftCenteredX, shiftCenteredY, this.Position.X, this.Position.Y, mouseState);
}
}
#endregion
}
}
Loading

1 comment on commit afc011d

@lastbattle
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gif_output

cool? 🤔🤔

Please sign in to comment.