2014-03-29 06:44:15 +08:00
|
|
|
|
#if DEBUG
|
|
|
|
|
#define DISPLAY_MEMORY
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Navigation;
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Animation;
|
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
using Microsoft.Phone.Controls;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Phone.Shell;
|
|
|
|
|
using Windows.UI.Input;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
using Microsoft.Phone.Info;
|
2014-05-22 21:58:05 +08:00
|
|
|
|
using Windows.Graphics.Display;
|
2014-09-22 20:47:58 +08:00
|
|
|
|
using Microsoft.Phone.Tasks;
|
2014-03-29 06:44:15 +08:00
|
|
|
|
|
2014-10-23 22:18:49 +08:00
|
|
|
|
namespace cocos2d
|
2014-03-29 06:44:15 +08:00
|
|
|
|
{
|
|
|
|
|
public partial class MainPage : PhoneApplicationPage
|
|
|
|
|
{
|
|
|
|
|
private Direct3DInterop m_d3dInterop = null;
|
|
|
|
|
private DispatcherTimer m_timer;
|
|
|
|
|
|
|
|
|
|
// event handler for CCEditBox
|
|
|
|
|
private event EventHandler<String> m_receiveHandler;
|
|
|
|
|
|
|
|
|
|
// invisible XAML TextBox for Cocos2d-x keyboard input
|
|
|
|
|
TextBox m_textBox = null;
|
|
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
|
public MainPage()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
#if DISPLAY_MEMORY
|
|
|
|
|
StartTimer();
|
|
|
|
|
#else
|
|
|
|
|
MemoryDisplay.Visibility = Visibility.Collapsed;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:16:34 +08:00
|
|
|
|
override protected void OnOrientationChanged(OrientationChangedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
base.OnOrientationChanged(args);
|
|
|
|
|
if (m_d3dInterop != null)
|
|
|
|
|
{
|
|
|
|
|
DisplayOrientations orientation = ConvertToNativeOrientation(args.Orientation);
|
|
|
|
|
m_d3dInterop.OnOrientationChanged(orientation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static DisplayOrientations ConvertToNativeOrientation(PageOrientation xamlOrientation)
|
|
|
|
|
{
|
|
|
|
|
switch (xamlOrientation)
|
|
|
|
|
{
|
|
|
|
|
case PageOrientation.Portrait:
|
|
|
|
|
case PageOrientation.PortraitUp:
|
|
|
|
|
return DisplayOrientations.Portrait;
|
|
|
|
|
case PageOrientation.PortraitDown:
|
|
|
|
|
return DisplayOrientations.PortraitFlipped;
|
|
|
|
|
case PageOrientation.Landscape:
|
|
|
|
|
case PageOrientation.LandscapeLeft:
|
|
|
|
|
return DisplayOrientations.Landscape;
|
|
|
|
|
case PageOrientation.LandscapeRight:
|
|
|
|
|
return DisplayOrientations.LandscapeFlipped;
|
|
|
|
|
default:
|
|
|
|
|
return DisplayOrientations.Landscape;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-29 06:44:15 +08:00
|
|
|
|
private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (m_d3dInterop == null)
|
|
|
|
|
{
|
2014-05-22 21:58:05 +08:00
|
|
|
|
PageOrientation pageOrientation = (PageOrientation)GetValue(OrientationProperty);
|
2014-09-26 16:16:34 +08:00
|
|
|
|
DisplayOrientations displayOrientation = ConvertToNativeOrientation(pageOrientation);
|
2014-05-22 21:58:05 +08:00
|
|
|
|
|
|
|
|
|
m_d3dInterop = new Direct3DInterop(displayOrientation);
|
2014-03-29 06:44:15 +08:00
|
|
|
|
|
|
|
|
|
// Set WindowBounds to size of DrawingSurface
|
|
|
|
|
m_d3dInterop.WindowBounds = new Windows.Foundation.Size(
|
|
|
|
|
(float)Application.Current.Host.Content.ActualWidth,
|
|
|
|
|
(float)Application.Current.Host.Content.ActualHeight
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Hook-up native component to DrawingSurfaceBackgroundGrid
|
|
|
|
|
DrawingSurfaceBackground.SetBackgroundContentProvider(m_d3dInterop.CreateContentProvider());
|
|
|
|
|
DrawingSurfaceBackground.SetBackgroundManipulationHandler(m_d3dInterop);
|
|
|
|
|
|
|
|
|
|
// Hook-up Cocos2d-x delegates
|
|
|
|
|
m_d3dInterop.SetCocos2dEventDelegate(OnCocos2dEvent);
|
|
|
|
|
m_d3dInterop.SetCocos2dMessageBoxDelegate(OnCocos2dMessageBoxEvent);
|
|
|
|
|
m_d3dInterop.SetCocos2dEditBoxDelegate(OpenEditBox);
|
2014-09-22 20:47:58 +08:00
|
|
|
|
m_d3dInterop.SetCocos2dOpenURLDelegate(OpenURL);
|
2014-03-29 06:44:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// called when the user presses the back button on the device
|
|
|
|
|
protected override void OnBackKeyPress(CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
m_d3dInterop.OnBackKeyPress();
|
|
|
|
|
// cocos2d-x will async send Cocos2dEvent.TerminateApp event if it is time to exit app.
|
|
|
|
|
// We do not want to exit now, so we set e.Cancel to true.
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnKeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ModifierKeys modifiers = Keyboard.Modifiers;
|
|
|
|
|
|
|
|
|
|
switch (e.Key)
|
|
|
|
|
{
|
|
|
|
|
case Key.Escape:
|
|
|
|
|
m_d3dInterop.OnCocos2dKeyEvent(Cocos2dKeyEvent.Escape);
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Back:
|
|
|
|
|
m_d3dInterop.OnCocos2dKeyEvent(Cocos2dKeyEvent.Back);
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Enter:
|
|
|
|
|
m_d3dInterop.OnCocos2dKeyEvent(Cocos2dKeyEvent.Enter);
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnKeyUp(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-25 07:41:51 +08:00
|
|
|
|
public void OnTextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
{
|
2014-10-23 22:18:49 +08:00
|
|
|
|
if (m_textBox.Text.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
m_d3dInterop.OnCocos2dKeyEvent(Cocos2dKeyEvent.Text, m_textBox.Text);
|
|
|
|
|
m_textBox.Text = "";
|
|
|
|
|
}
|
2014-09-25 07:41:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-29 06:44:15 +08:00
|
|
|
|
// Called by the Cocos2d-x C++ engine to display a MessageBox
|
|
|
|
|
public void OnCocos2dMessageBoxEvent(String title, String text)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.BeginInvoke(() =>
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(text, title, MessageBoxButton.OK);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// events called by the Cocos2d-x C++ engine to be handled by C#
|
2014-09-25 07:41:51 +08:00
|
|
|
|
public void OnCocos2dEvent(Cocos2dEvent theEvent, String text)
|
2014-03-29 06:44:15 +08:00
|
|
|
|
{
|
|
|
|
|
Dispatcher.BeginInvoke(() =>
|
|
|
|
|
{
|
|
|
|
|
switch (theEvent)
|
|
|
|
|
{
|
|
|
|
|
case Cocos2dEvent.TerminateApp:
|
|
|
|
|
Application.Current.Terminate();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Cocos2dEvent.ShowKeyboard:
|
|
|
|
|
if (m_textBox == null)
|
|
|
|
|
{
|
|
|
|
|
m_textBox = new TextBox();
|
|
|
|
|
m_textBox.Opacity = 0.0;
|
|
|
|
|
m_textBox.Width = 1;
|
|
|
|
|
m_textBox.Height = 1;
|
|
|
|
|
m_textBox.KeyDown += OnKeyDown;
|
2014-09-25 07:41:51 +08:00
|
|
|
|
m_textBox.TextChanged += OnTextChanged;
|
2014-03-29 06:44:15 +08:00
|
|
|
|
DrawingSurfaceBackground.Children.Add(m_textBox);
|
|
|
|
|
}
|
2014-09-25 07:41:51 +08:00
|
|
|
|
m_textBox.SelectionLength = 0;
|
|
|
|
|
m_textBox.SelectionStart = int.MaxValue;
|
2014-03-29 06:44:15 +08:00
|
|
|
|
m_textBox.Focus();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Cocos2dEvent.HideKeyboard:
|
|
|
|
|
if (m_textBox != null)
|
|
|
|
|
{
|
|
|
|
|
DrawingSurfaceBackground.Children.Remove(m_textBox);
|
|
|
|
|
}
|
|
|
|
|
m_textBox = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called by the Cocos2d-x C++ engine to display a CCEditBox
|
|
|
|
|
public void OpenEditBox(String strPlaceHolder, string strText, int maxLength, int inputMode, int inputFlag, EventHandler<String> receiveHandler)
|
|
|
|
|
{
|
|
|
|
|
m_receiveHandler = receiveHandler;
|
|
|
|
|
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
|
|
|
|
{
|
|
|
|
|
EditBox editbox = new EditBox(this, strPlaceHolder, strText, maxLength, inputMode, inputFlag);
|
|
|
|
|
DrawingSurfaceBackground.Children.Add(editbox);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnSelectText(object sender, String str)
|
|
|
|
|
{
|
|
|
|
|
if (m_d3dInterop != null && m_receiveHandler != null)
|
|
|
|
|
{
|
|
|
|
|
m_d3dInterop.OnCocos2dEditboxEvent(sender, str, m_receiveHandler);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-22 20:47:58 +08:00
|
|
|
|
public void OpenURL(String url)
|
|
|
|
|
{
|
|
|
|
|
WebBrowserTask webBrowserTask = new WebBrowserTask();
|
|
|
|
|
webBrowserTask.Uri = new Uri(url, UriKind.Absolute);
|
|
|
|
|
webBrowserTask.Show();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-29 06:44:15 +08:00
|
|
|
|
private void StartTimer()
|
|
|
|
|
{
|
|
|
|
|
m_timer = new DispatcherTimer();
|
|
|
|
|
m_timer.Interval = new TimeSpan(0, 0, 1);
|
|
|
|
|
m_timer.Tick += new EventHandler(TimerTick);
|
|
|
|
|
m_timer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerTick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// These are TextBlock controls that are created in the page’s XAML file.
|
|
|
|
|
float value = DeviceStatus.ApplicationCurrentMemoryUsage / (1024.0f * 1024.0f);
|
|
|
|
|
MemoryTextBlock.Text = value.ToString();
|
|
|
|
|
value = DeviceStatus.ApplicationPeakMemoryUsage / (1024.0f * 1024.0f);
|
|
|
|
|
PeakMemoryTextBlock.Text = value.ToString();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MemoryTextBlock.Text = ex.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|