2019-08-07 22:25:33 +02:00
using System ;
2019-08-11 18:02:21 +02:00
using System.Collections.Generic ;
using Coroutine ;
2019-08-07 22:25:33 +02:00
using Microsoft.Xna.Framework ;
2019-08-09 15:15:22 +02:00
using Microsoft.Xna.Framework.Graphics ;
2019-08-09 19:28:48 +02:00
using MLEM.Font ;
2019-08-07 22:25:33 +02:00
using MLEM.Input ;
2019-08-07 21:26:16 +02:00
using MLEM.Startup ;
2019-08-09 15:15:22 +02:00
using MLEM.Textures ;
2019-08-09 18:26:28 +02:00
using MLEM.Ui ;
using MLEM.Ui.Elements ;
2019-08-10 21:37:10 +02:00
using MLEM.Ui.Style ;
2019-08-07 21:26:16 +02:00
2019-08-12 00:59:55 +02:00
namespace Examples {
2019-08-07 21:26:16 +02:00
public class GameImpl : MlemGame {
2019-08-09 15:15:22 +02:00
private Texture2D testTexture ;
private NinePatch testPatch ;
2019-08-09 18:26:28 +02:00
2019-08-09 22:04:26 +02:00
public GameImpl ( ) {
this . IsMouseVisible = true ;
}
2019-08-09 15:15:22 +02:00
protected override void LoadContent ( ) {
this . testTexture = LoadContent < Texture2D > ( "Textures/Test" ) ;
this . testPatch = new NinePatch ( new TextureRegion ( this . testTexture , 0 , 8 , 24 , 24 ) , 8 ) ;
2019-08-10 21:37:10 +02:00
base . LoadContent ( ) ;
2019-08-09 18:26:28 +02:00
2019-08-12 19:44:16 +02:00
var style = new UntexturedStyle ( this . SpriteBatch ) {
2019-08-10 21:37:10 +02:00
Font = new GenericSpriteFont ( LoadContent < SpriteFont > ( "Fonts/TestFont" ) ) ,
2019-08-11 21:24:09 +02:00
TextScale = 0.2F ,
2019-08-10 21:37:10 +02:00
PanelTexture = this . testPatch ,
ButtonTexture = new NinePatch ( new TextureRegion ( this . testTexture , 24 , 8 , 16 , 16 ) , 4 ) ,
TextFieldTexture = new NinePatch ( new TextureRegion ( this . testTexture , 24 , 8 , 16 , 16 ) , 4 ) ,
ButtonHoveredColor = Color . LightGray ,
TextFieldHoveredColor = Color . LightGray
} ;
var untexturedStyle = this . UiSystem . Style ;
this . UiSystem . Style = style ;
2019-08-11 21:24:09 +02:00
this . UiSystem . GlobalScale = 5 ;
2019-08-09 18:26:28 +02:00
2019-08-12 19:44:16 +02:00
var root = new Panel ( Anchor . Center , new Vector2 ( 100 , 80 ) , Point . Zero , false , true , new Point ( 5 , 10 ) ) ;
2019-08-10 21:37:10 +02:00
this . UiSystem . Add ( "Test" , root ) ;
2019-08-10 18:41:56 +02:00
root . AddChild ( new Paragraph ( Anchor . AutoLeft , 1 , "This is a test text that is hopefully long enough to cover at least a few lines, otherwise it would be very sad." ) ) ;
2019-08-11 21:24:09 +02:00
var image = root . AddChild ( new Image ( Anchor . AutoCenter , new Vector2 ( 20 , 20 ) , new TextureRegion ( this . testTexture , 0 , 0 , 8 , 8 ) ) { IsHidden = true } ) ;
root . AddChild ( new Button ( Anchor . AutoCenter , new Vector2 ( 1 , 15 ) , "Test Button" ) {
2019-08-11 18:02:21 +02:00
OnClicked = ( element , button ) = > {
2019-08-09 22:04:26 +02:00
if ( button = = MouseButton . Left )
2019-08-09 22:23:16 +02:00
image . IsHidden = ! image . IsHidden ;
2019-08-09 22:04:26 +02:00
}
} ) ;
2019-08-11 21:24:09 +02:00
root . AddChild ( new Button ( Anchor . AutoCenter , new Vector2 ( 1 , 15 ) , "Change Style" ) {
2019-08-11 18:02:21 +02:00
OnClicked = ( element , button ) = > {
2019-08-10 21:37:10 +02:00
if ( button = = MouseButton . Left )
2019-08-12 19:44:16 +02:00
this . UiSystem . Style = this . UiSystem . Style = = untexturedStyle ? style : untexturedStyle ;
2019-08-10 21:37:10 +02:00
} ,
HasCustomStyle = true ,
Texture = this . testPatch ,
HoveredColor = Color . LightGray
} ) ;
2019-08-11 21:24:09 +02:00
root . AddChild ( new TextField ( Anchor . AutoLeft , new Vector2 ( 1 , 15 ) ) ) ;
2019-08-11 18:02:21 +02:00
root . AddChild ( new VerticalSpace ( 3 ) ) ;
2019-08-11 21:24:09 +02:00
root . AddChild ( new Button ( Anchor . AutoLeft , new Vector2 ( 15 ) , "+" ) {
2019-08-11 18:02:21 +02:00
OnClicked = ( element , button ) = > {
if ( element . Root . Scale < 2 )
element . Root . Scale + = 0.1F ;
}
} ) ;
2019-08-11 21:24:09 +02:00
root . AddChild ( new Button ( Anchor . AutoInline , new Vector2 ( 15 ) , "-" ) {
2019-08-11 18:02:21 +02:00
OnClicked = ( element , button ) = > {
if ( element . Root . Scale > 0.5F )
element . Root . Scale - = 0.1F ;
}
} ) ;
2019-08-11 21:24:09 +02:00
root . AddChild ( new Button ( Anchor . AutoInline , new Vector2 ( 30 , 15 ) , "Woop" ) {
2019-08-12 19:44:16 +02:00
PositionOffset = new Point ( 2 , 0 ) ,
2019-08-11 21:24:09 +02:00
OnClicked = ( element , button ) = > CoroutineHandler . Start ( Woop ( element ) )
} ) ;
}
private static IEnumerator < Wait > Woop ( Element element ) {
var angle = 0 ;
var startScale = element . Root . Scale ;
while ( angle < 180 ) {
element . Root . Scale = startScale + ( float ) Math . Sin ( MathHelper . ToRadians ( angle ) ) ;
angle + + ;
yield return new WaitSeconds ( 0.01F ) ;
}
2019-08-07 22:25:33 +02:00
}
2019-08-07 21:26:16 +02:00
2019-08-09 15:15:22 +02:00
protected override void Draw ( GameTime gameTime ) {
this . GraphicsDevice . Clear ( Color . Black ) ;
2019-08-09 23:43:50 +02:00
base . Draw ( gameTime ) ;
2019-08-09 15:15:22 +02:00
}
2019-08-07 21:26:16 +02:00
}
}