TouchyTickets/Android/Activity1.cs
2020-06-10 23:03:48 +02:00

90 lines
No EOL
3.8 KiB
C#

using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Gms.Ads;
using Android.OS;
using Android.Views;
using Android.Widget;
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Misc;
using TouchyTickets;
using static Android.Views.ViewGroup.LayoutParams;
using Uri = Android.Net.Uri;
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;
private LinearLayout mainView;
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
// set up the game
TextInputWrapper.Current = new TextInputWrapper.Mobile();
this.game = new GameImpl(new AndroidAnalytics(this));
this.game.GraphicsDeviceManager.ResetWidthAndHeight(this.game.Window);
this.game.GraphicsDeviceManager.IsFullScreen = true;
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)));
};
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();
};
// render under notches
if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
this.Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.ShortEdges;
// ad layout
var adLayout = new LinearLayout(this) {Orientation = Orientation.Vertical};
adLayout.SetGravity(GravityFlags.Bottom);
var ad = new AdView(this) {
AdSize = AdSize.SmartBanner,
#if DEBUG
AdUnitId = "ca-app-pub-3940256099942544/6300978111"
#else
AdUnitId = "ca-app-pub-5754829579653773/7841535920"
#endif
};
ad.LoadAd(new AdRequest.Builder().Build());
adLayout.AddView(ad);
// 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);
this.game.Run();
}
public override void OnWindowFocusChanged(bool hasFocus) {
// hide the status bar
this.mainView.SystemUiVisibility = (StatusBarVisibility) (SystemUiFlags.LayoutStable | SystemUiFlags.LayoutHideNavigation | SystemUiFlags.LayoutFullscreen | SystemUiFlags.HideNavigation | SystemUiFlags.Fullscreen | SystemUiFlags.ImmersiveSticky);
base.OnWindowFocusChanged(hasFocus);
}
}
}