Last active
July 14, 2020 04:39
-
-
Save piedoom/7c49097b1bf0eb59b1c4fc8bcfd4a7dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Microsoft.Xna.Framework; | |
using Nez; | |
using Nez.UI; | |
namespace Vapor.Code.Utility.UI | |
{ | |
/// <summary> | |
/// Set up a general purpose canvas and table to draw stuff with | |
/// </summary> | |
class BaseUI : Component | |
{ | |
private int _renderLayer; | |
protected Skin _skin; | |
protected UICanvas ui; | |
protected Table table; | |
/// <summary> | |
/// All we need when setting up is the renderlayer. This assumes that you've already set up a ScreenSpaceRenderer and set its renderLayer. | |
/// </summary> | |
/// <param name="renderLayer">The renderLayer of your ScreenSpaceRenderer</param> | |
/// <param name="skin">The skin to store in our class</param> | |
public BaseUI(int renderLayer, Skin skin = null) | |
{ | |
_renderLayer = renderLayer; | |
// if we didn't pass in a skin, we'll just use a default one | |
// we don't actually use this anywhere in the base class but it can be useful to store it here | |
if (skin == null) | |
_skin = Skin.createDefaultSkin(); | |
} | |
/// <summary> | |
/// Do our setup here. We'll add our UICanvas and a table for starters. | |
/// </summary> | |
public override void onAddedToEntity() | |
{ | |
base.onAddedToEntity(); | |
// add the canvas to our component | |
ui = entity.addComponent(new UICanvas()); | |
ui.renderLayer = _renderLayer; | |
// add a new table layout | |
table = ui.stage.addElement(new Table()); | |
// after our canvas is prepped, create our UI elements | |
Setup(); | |
// when we're done setting up, position our elements for the first time | |
PositionElements(); | |
// add events to listen for screen-size changing and scene changes | |
Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset, PositionElements); | |
Core.emitter.addObserver(CoreEvents.SceneChanged, OnSceneChanged); | |
} | |
/// <summary> | |
/// Sets the background color | |
/// </summary> | |
/// <param name="color">The color to make our background</param> | |
protected void SetBackgroundColor(Color color) | |
{ | |
if (table != null) | |
table.setBackground(new PrimitiveDrawable(color)); | |
} | |
/// <summary> | |
/// Override this method to add elements to your table property | |
/// </summary> | |
protected virtual void Setup() | |
{ | |
} | |
/// <summary> | |
/// Override this method to resize your UI when the screen size changes and when we setup intially | |
/// </summary> | |
protected virtual void PositionElements() | |
{ | |
} | |
/// <summary> | |
/// Override this method to do special stuff when the scene changes. You probably don't need this. | |
/// </summary> | |
protected virtual void OnSceneChanged() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a good piece of code that'd I'd like to use in my project and extend upon. Do you have any problems with that? There isn't a license attached so just want to double check all that.