Mouse Point on Landscape Mode - windows-phone-7

I need an image that can follow my finger (mouse) on the screen... the following code works fine on Portrait Mode, but it get's completely mess up on Landscape Mode, has anybody been across to this?
<Image Height="68" HorizontalAlignment="Left" Margin="872,388,0,0" Name="imgStarPoint" Stretch="Fill" VerticalAlignment="Top" Width="54" Source="/GetMousePoint;component/StarT.png" ManipulationCompleted="imgStarPoint_ManipulationCompleted">
<i:Interaction.Behaviors>
<el:MouseDragElementBehavior x:Name="imgStar"/>
</i:Interaction.Behaviors>
</Image>
and code behind:
void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
var PrimaryPoint = e.GetPrimaryTouchPoint(null);
imgStar.X = PrimaryPoint.Position.X;
imgStar.Y = PrimaryPoint.Position.Y;
txt1.Text = PrimaryPoint.Position.X + "." + PrimaryPoint.Position.Y;
}
Does anybody has a way to set the image on my finger tip on Landscape Mode?

EDIT:
Ok, for some reason I though you were using WP Toolkit's Gesture listener which will report the correct X any Y in each orientation mode. In your case you need to detect which orientation mode you are in and do the necessary adjustments.
It seems like when orientation is in landscape the axis are switched. When in landscape-left mode the X axis is inverted and in landscape-right mode the Y axis is inverted. Following code should fix your problem:
bool _switchAxis;
bool _invertX ;
bool _invertY;
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e) {
_switchAxis = (e.Orientation | PageOrientation.LandscapeLeft | PageOrientation.LandscapeRight) == (PageOrientation.LandscapeLeft | PageOrientation.LandscapeRight);
_invertX = e.Orientation == PageOrientation.LandscapeLeft;
_invertY = e.Orientation == PageOrientation.LandscapeRight;
}
private void Touch_FrameReported(object sender, System.Windows.Input.TouchFrameEventArgs e) {
var width = Application.Current.Host.Content.ActualWidth;
var height = Application.Current.Host.Content.ActualHeight;
var primaryPoint = e.GetPrimaryTouchPoint(null);
if (_switchAxis) {
if (_invertY) imgStar.X = height - primaryPoint.Position.Y; else imgStar.X = primaryPoint.Position.Y;
if (_invertX) imgStar.Y = width - primaryPoint.Position.X; else imgStar.Y = primaryPoint.Position.X;
} else {
imgStar.X = primaryPoint.Position.X;
imgStar.Y = primaryPoint.Position.Y;
}
}
You need to add OrientationChanged event to your page xaml:
<phone:PhoneApplicationPage
<!-- ... -->
OrientationChanged="PhoneApplicationPage_OrientationChanged"
/>
In Landscape mode, a visible application bar and system tray will mess up X for you.
If you have an application bar, set its mode to minimized
ApplicationBar.Mode = ApplicationBarMode.Minimized
You also need to hide the system tray to avoid manual adjustments on X. Do that on the page Loaded event
Xaml:
<phone:PhoneApplicationPage
<!-- stuff -->
Loaded="PhoneApplicationPage_Loaded"
/>
Code behind:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
SystemTray.IsVisible = false;
}
Also you would want to deduct image.Width/2 from X and image.Height/2 from Y to make it exactly at the center of your finger tip.
imgStar.X = PrimaryPoint.Position.X - (img.Width/2);
imgStar.Y = PrimaryPoint.Position.Y - (img.Height/2);
That should fix the problem.

Related

SwapChainBackgroundPanel letterboxing Monogame Windows Store App

I am porting my space shooter game from Windows Phone to Windows Store App. In WP it always play in full portrait orientation.
For the Windows Store app though while in landscape mode, I want to center the game screen with letterboxing on the left and right. The problem is I can't adjust the margin property of SwapChainBackgroundPanel so the game always aligned to the left and the black screen is on the right.
Here's my code
public Game1()
{
graphics = new GraphicsDeviceManager(this);
GamePage.Current.SizeChanged += OnWindowSizeChanged;
Content.RootDirectory = "Content";
}
private void OnWindowSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e)
{
var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value;
double width = e.NewSize.Width;
double height = e.NewSize.Height;
// using Windows.Graphics.Display;
ResolutionScale resolutionScale = DisplayProperties.ResolutionScale;
string orientation = null;
if (ApplicationView.Value == ApplicationViewState.FullScreenLandscape)
{
orientation = "FullScreenLandscape";
//Does not work because it's start on the center of the screen
//Black screen is on the left and place the game screen on the right
GamePage.Current.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
//Gives error - WinRT information: Setting 'Margin' property is
//not supported on SwapChainBackgroundPanel.
GamePage.Current.Margin = new Thickness(centerMargin, 0, 0, 0);
}
else if (ApplicationView.Value == ApplicationViewState.FullScreenPortrait)
{
orientation = "FullScreenPortrait";
}
else if (ApplicationView.Value == ApplicationViewState.Filled)
{
orientation = "Filled";
}
else if (ApplicationView.Value == ApplicationViewState.Snapped)
{
orientation = "Snapped";
}
Debug.WriteLine("{0} x {1}. Scale: {2}. Orientation: {3}",
width.ToString(), height.ToString(), resolutionScale.ToString(),
orientation);
}
The GamePage.xaml is the default
<SwapChainBackgroundPanel
x:Class="SpaceShooterXW8.GamePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SpaceShooterXW8"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</SwapChainBackgroundPanel>
After some researched I think I've figured it out thanks to this blog post. To those who are in a similar situation, here's what I did.
The beauty of the solution is that the letterboxing is automatically managed by the Resolution class. All I have to do is update the batch.begin() lines in my code to something like
batch.Begin(SpriteSortMode.Deferred,
null, SamplerState.LinearClamp,
null,
null,
null,
Resolution.getTransformationMatrix());
To handle resolution changes as the orientation changed I use this in my Game1.cs
public Game1()
{
graphics = new GraphicsDeviceManager(this);
GamePage.Current.SizeChanged += OnWindowSizeChanged;
Content.RootDirectory = "Content";
Resolution.Init(ref graphics);
Resolution.SetVirtualResolution(480, 800);
}
private void OnWindowSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e)
{
var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value;
App.AppWidth = (int)e.NewSize.Width;
App.AppHeight = (int)e.NewSize.Height;
Resolution.SetResolution(App.AppWidth, App.AppHeight, true);
}
The initial values of App.AppWidth and App.AppHeight is set in the GamePage.xaml.cs.
public GamePage(string launchArguments)
{
this.InitializeComponent();
App.AppWidth = (int)Window.Current.Bounds.Width;
App.AppHeight = (int)Window.Current.Bounds.Height;
Current = this;
// Create the game.
_game = XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, this);
}
Both are global static property created in the App.xaml.cs
public static int AppWidth { get; set; }
public static int AppHeight { get; set; }
The only problem I've encountered so far, the mouse input does not scale to the screen resolution change. I do not have a touch screen to test unfortunately but I think touch input should scale. If anyone tested touch, please share your findings. Thanks.
Update
I've managed to scale the Mouse input using the following
public static Vector2 ScaleGesture(Vector2 position)
{
int x = (int)(position.X / (float)App.AppWidth * (float)Screen.ScreenWidth);
int y = (int)(position.Y / (float)App.AppHeight * (float)Screen.ScreenHeight);
var scaledPosition = new Vector2(x, y);
return scaledPosition;
}

Windows phone - Avoid scrolling of Web browser control placed inside scroll viewer

I have to show a web browser inside a scroll viewer in windows phone application, with these requirements :
Web browser height should be adjusted based on its content.
Web browser scrolling should be disabled, ( when user scrolls within web browser, scrolling of scroll viewer should take place )
Web browser can do pinch-zoom and navigate to links inside its content.
How can I implement that? Any links or samples is greatly appreciated.
I'm using code like this. Attach events to the Border element in the Browser control tree (I'm using Linq to Visual Tree - http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/).
Browser.Loaded +=
(s,e)=>
{
var border = Browser.Descendants<Border>().Last() as Border;
if (border != null)
{
border.ManipulationDelta += BorderManipulationDelta;
border.ManipulationCompleted += BorderManipulationCompleted;
border.DoubleTap += BorderDoubleTap;
}
};
Further more the implementation I'm using is to prevent pinch and zoom, something you want to have working. Though this should help you in the right direction.
private void BorderDoubleTap(object sender, GestureEventArgs e)
{
e.Handled = true;
}
private void BorderManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
// suppress zoom
if (Math.Abs(e.DeltaManipulation.Scale.X) > 0.0||
Math.Abs(e.DeltaManipulation.Scale.Y) > 0.0)
e.Handled = true;
}
private void BorderManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
// suppress zoom
if (Math.Abs(e.FinalVelocities.ExpansionVelocity.X) > 0.0 ||
Math.Abs(e.FinalVelocities.ExpansionVelocity.Y) > 0.0)
e.Handled = true;
}
On Mark's direction, I used
private void Border_ManipulationDelta(object sender,
System.Windows.Input.ManipulationDeltaEventArgs e)
{
e.Complete();
_browser.IsHitTestVisible = false;
}

How to play a video (wmv file) in wp7 silverlight

i want to play video in my app. I see around but nothing helped me. I ask the user if he wants to listen to his phone music. If clicks ok the music continue playing, if not then the music stop. That is ok from now. Now, my problem is: I create a grid to use it like popup with width and height and so on.. When this popup appears the music stops. This is why can not certify my app in marketplace.
Here is a little code: i believe is easy to understand... Please help!
public void new_grid(int v)
{
Grid gr = new Grid();
gr.Background = new SolidColorBrush(Colors.Cyan);
gr.Opacity = 1.0;
gr.Width = 400;
gr.Height = 600;
// Create a white border.
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.White);
border.BorderThickness = new Thickness(7.0);
MediaElement video_ship = new MediaElement();
//video_ship.AutoPlay = false;
video_ship.Width = 400;
video_ship.Height = 600;
video_ship.VerticalAlignment = VerticalAlignment.Top;
video_ship.HorizontalAlignment = HorizontalAlignment.Left;
if (v == 1)
{
video_ship.Source = new Uri("videos/Lost_Ship.wmv", UriKind.RelativeOrAbsolute);
//video_ship.Play();
gr.Children.Add(video_ship);
}
else if (v == 2)
{
//video_ship.Source = "videos/Lost_Ship.wmv";
video_ship.Source = new Uri("videos/you_are_on_fire.wmv", UriKind.RelativeOrAbsolute);
//video_ship.Name = "fire";
//video_ship.Play();
gr.Children.Add(video_ship);
}
else if (v == 3)
{
//video_ship.SourceName = "videos/Lost_Ship.wmv";
video_ship.Source = new Uri("videos/EscapeShip.wmv", UriKind.RelativeOrAbsolute);
//video_ship.Name = "escape";
//video_ship.Play();
gr.Children.Add(video_ship);
}
I send a variable v to select one of my videos..I set the videos to Build Action Content
Any ideas what to do? or something different than this?
I want only to play the video ..Video does not have music ..or sound effect..
I tested it and it works. You want to popup video when you call event. so you shoud put grid inside file xaml code and it is hidden.
This is my test
on xaml
<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Source="BackgroundMain3.jpg" Stretch="UniformToFill"/>
<Grid Height="400" Width="600" x:Name="playvideo" Visibility="Collapsed">
<MediaElement x:Name="element"/>
</Grid>
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="129,684,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
</Grid>
on xaml.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
poupup(3);
playvideo.Visibility = Visibility.Visible;
playvideo.Opacity = 0.8;
element.MediaOpened += new RoutedEventHandler(element_MediaOpened);
}
void element_MediaOpened(object sender, RoutedEventArgs e)
{
element.Play();
}
public void poupup(int v)
{
if (v == 1)
{
element.Source = new Uri("videos/Lost_Ship.wmv", UriKind.RelativeOrAbsolute);
}
else if (v == 2)
{
element.Source = new Uri("videos/you_are_on_fire.wmv", UriKind.RelativeOrAbsolute);
}
else if (v == 3)
{
element.Source = new Uri("YouTube.mp4", UriKind.RelativeOrAbsolute);
}
}

WP7 allow orientation change but not the background image

My apps is planned to allow orientation change, however, I have background images (in pivot control that allow user to change the background images) that does not want to change the orientation. How should i implement that?
thanks for advice.
You might be able to add a Rotation Transform dynamically to offset the rotation of the background image when the orientation changes, as below :-
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (e.Orientation == PageOrientation.PortraitUp)
{
PivotBackground.RelativeTransform = null;
}
else
{
RotateTransform aRotateTransform = new RotateTransform();
aRotateTransform.CenterX = 0.5;
aRotateTransform.CenterY = 0.5;
aRotateTransform.Angle = 90;
PivotBackground.RelativeTransform = aRotateTransform;
}
}
The XAML is defined as :-
<controls:Pivot Title="MY APPLICATION">
<controls:Pivot.Background>
<ImageBrush ImageSource="/Back.jpg"
x:Name="PivotBackground">
</ImageBrush>
</controls:Pivot.Background>
</controls:Pivot>
Hope this helps.
Paul Diston

how to set ScrollViewer's scroller at certain point in wp7?

I have a large canvas. In the casvas I'm adding textblock in code behind on drage event. Each time I add a textblock the Scrollviewer set/shows the first textbloc. Thus I have to manually scroll up to last text block. So I want to problematically set the Scrollviewer at last element of the canvas after a new textblock added. XAML:-
<ScrollViewer x:Name="sv" HorizontalScrollBarVisibility="Auto" Margin="6,6,-835,66">
<Canvas x:Name="canvas" Height="450" Width="12000" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
</Canvas>
</ScrollViewer>
Code behind C#:-
private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
{
if (tb_conter > 24)
return;
TextBlock[] tb = new TextBlock[elemet];
for (int i = 0; i < elemet; i++)
tb[i] = new TextBlock();
currentPoint = e.GetPosition(this.ContentPanel);
double x = currentPoint.X - oldPoint.X;
if (x >= 100)
{
tb[tb_conter].SetValue(Canvas.LeftProperty, tb_canvasLeft);
tb[tb_conter].SetValue(Canvas.TopProperty, tb_canvasTop);
tb[tb_conter].Text = time_scale.ToString();
canvas.Children.Add(tb[tb_conter]);
time_scale++;
tb_conter++;
tb_canvasLeft += tb_canvasTop;
}
else
Debug.WriteLine(x.ToString());
}
You want the ScrollViewer.ScrollToVerticalOffset method.
Better still would be adding items to an ObservableCollection<string> in a view model and binding that to a ListBox on the page.

Resources