TouchyTickets/Android/Activity1.cs

82 lines
3.5 KiB
C#
Raw Normal View History

2020-06-10 23:03:48 +02:00
using System;
2020-05-30 19:41:49 +02:00
using Android.App;
2020-06-09 19:03:55 +02:00
using Android.Content;
2020-05-30 19:41:49 +02:00
using Android.Content.PM;
2020-06-10 23:03:48 +02:00
using Android.Gms.Ads;
2020-05-30 19:41:49 +02:00
using Android.OS;
using Android.Views;
2020-06-10 23:03:48 +02:00
using Android.Widget;
2020-05-30 19:41:49 +02:00
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Misc;
2020-06-01 17:39:57 +02:00
using TouchyTickets;
using static Android.Views.SystemUiFlags;
2020-06-10 23:03:48 +02:00
using static Android.Views.ViewGroup.LayoutParams;
using Uri = Android.Net.Uri;
2020-05-30 19:41:49 +02:00
namespace Android {
[Activity(
Label = "@string/app_name",
MainLauncher = true,
Icon = "@drawable/icon",
AlwaysRetainTaskState = true,
LaunchMode = LaunchMode.SingleInstance,
ScreenOrientation = ScreenOrientation.UserPortrait,
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize
)]
public class Activity1 : AndroidGameActivity {
private GameImpl game;
2020-06-10 23:03:48 +02:00
private LinearLayout mainView;
2020-05-30 19:41:49 +02:00
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
// ad layout
var adLayout = new LinearLayout(this) {Orientation = Orientation.Vertical};
adLayout.SetGravity(GravityFlags.Bottom);
2020-06-10 23:03:48 +02:00
// set up the game
2020-05-30 19:41:49 +02:00
TextInputWrapper.Current = new TextInputWrapper.Mobile();
this.game = new GameImpl(new AndroidPlatform(this, adLayout));
2020-05-30 19:41:49 +02:00
this.game.GraphicsDeviceManager.ResetWidthAndHeight(this.game.Window);
2020-06-10 23:03:48 +02:00
this.game.GraphicsDeviceManager.IsFullScreen = true;
2020-06-09 19:03:55 +02:00
this.game.OnLoadContent += game => {
game.InputHandler.HandleMouse = false;
game.UiSystem.LinkBehavior = l => this.StartActivity(new Intent(Intent.ActionView, Uri.Parse(l.Match.Groups[1].Value)));
};
2020-05-30 19:41:49 +02:00
2020-06-10 23:03:48 +02:00
var gameView = this.game.Services.GetService(typeof(View)) as View;
gameView.LayoutChange += (o, args) => {
// force the game size to update when the ad size changes
this.game.GraphicsDeviceManager.PreferredBackBufferWidth = args.Right - args.Left;
this.game.GraphicsDeviceManager.PreferredBackBufferHeight = args.Bottom - args.Top;
this.game.GraphicsDeviceManager.ApplyChanges();
};
// don't render under notches
if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
this.Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.Never;
2020-06-10 23:03:48 +02:00
// total layout that is displayed
this.mainView = new LinearLayout(this) {Orientation = Orientation.Vertical};
this.mainView.LayoutParameters = new LinearLayout.LayoutParams(MatchParent, MatchParent);
this.mainView.AddView(gameView);
// height of 0 but high weight causes this element so scale based on the ad's height
gameView.LayoutParameters = new LinearLayout.LayoutParams(MatchParent, 0, 1);
this.mainView.AddView(adLayout);
adLayout.LayoutParameters = new LinearLayout.LayoutParams(MatchParent, WrapContent);
this.SetContentView(this.mainView);
2020-05-30 19:41:49 +02:00
this.game.Run();
}
public override void OnWindowFocusChanged(bool hasFocus) {
2020-06-10 23:03:48 +02:00
base.OnWindowFocusChanged(hasFocus);
// hide the status bar
if (hasFocus)
this.Window.DecorView.SystemUiVisibility = (StatusBarVisibility) (ImmersiveSticky | LayoutStable | LayoutHideNavigation | LayoutFullscreen | HideNavigation | Fullscreen);
2020-05-30 19:41:49 +02:00
}
}
}