XNA Touch Location - xna

I am making a bubble wrap game. The kind where you press the bubbles to pop them and they unpop after a little bit. it all works fine, the bubbles pop and unpop as planned. There is one issue though. Whenever I click and drag the bubbles still pop. Is there a way to get them not to pop when i click and drag? Here is my code for the touch location.
foreach (TouchLocation tl in touches)
{
if (tl.State == TouchLocationState.Pressed)
{
if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
{
popLocationX = (int)tl.Position.X;
popLocationY = (int)tl.Position.Y;
}
}
}

Here is what I do, I use this on my main menu but should be able to transfer over.
Have these added in:
MouseState mouse;
public float mouseDelay = 0.01f;
public float mouseTime = 0.0f;
public bool mouseActive = true;
public bool mouseUsed = false;
public string mouseOn = "None";
public Vector2 mouseLocation;
mouse helps find the location of the mouse.
mouseDelay will stop the the repeating when you have the mouse in the pressed position.
mouseTime will say hey, if I am greater than mouseDelay mouse can be used again.
mouseActive will say if it is pressed or not. If mouse is pressed mouseActive is false.
mouseUsed will see if it is ever used. If the mouse is not clicking on anything but the background then it will be false. If true that means it was used to do something.
mouseOn is used in my main menu to say you pressing on options? Make mouseOn = "Options";and go to it.
The mouseLocation holds where the mouse is.
All this in mind putting this all together will help you with your goal of not having the mouse used when it is already pressed.
In update:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
mouse = Mouse.GetState();
if (this.IsMouseVisible == true)
{
MouseActive(gameTime);
}
base.Update(gameTime);
}
I have if mouse is visible to stop the process from repeating when the mouse is not there (less processing power).
Remember this was made for a main menu if you want to make this for a game you need to mess around with it a little.
void MouseActive(GameTime gameTime)
{
mouseTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
mouse = Mouse.GetState();
mouseLocation = new Vector2(mouse.X, mouse.Y);
switch (gameState)
{
case GameStates.TitleScreen:
break;
case GameStates.Options:
break;
case GameStates.Credits:
break;
}
if (mouseOn != "None")
{
mouseUsed = true;
switch (gameState)
{
case GameStates.TitleScreen:
if (mouseOn == "Play")
{
}
if (mouseOn == "Quit")
this.Exit();
if (mouseOn == "Options")
gameState = GameStates.Options;
if (mouseOn == "Title")
{
}
break;
case GameStates.Options:
break;
case GameStates.Credits:
break;
}
mouseOn = "None";
}
if (mouse.LeftButton == ButtonState.Pressed)
{
mouseTime = 0.0f;
mouseActive = false;
}
if (mouse.LeftButton == ButtonState.Released && mouseTime > mouseDelay)
{
mouseActive = true;
mouseUsed = false;
}
}
All the code from above is going to be in the main class
Here is a class named TitleScreen.cs were I use the images on the TitlesScreen and the mouse from the main class:
You first need to Initialize the thing so add this method in TitleScreen:
Game1 game1;
public void Initialize(Game1 game1)
{
this.game1 = game1;
}
You need to add in an Initializer to the Main class(Game1.cs)
TitleScreen titlescreen;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
titlescreen = new TitleScreen();
}
protected override void Initialize()
{
titlescreen.Initialize(this);
base.Initialize();
}
Then for the final step you add Update to the TitleScreen:
public void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (mouse.LeftButton == ButtonState.Pressed && game1.mouseUsed == false && game1.mouseActive == true)
{
if (play.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Play";
game1.mouseUsed = true;
}
else if (title.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Title";
game1.mouseUsed = true;
}
else if (options.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Options";
game1.mouseUsed = true;
}
else if (quit.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Quit";
game1.mouseUsed = true;
}
}
}
You also need to add this to the Update in the main class
titlescreen.Update(gameTime);

I've not worked with touch yet, but the logic should be similar across all input methods.
I would create a variable to store the old TouchLocationState. That way, you can check if the user is clicking and dragging. For example:
//TODO: instantiate this
TouchLocationState oldTLState;
...
// This condition will only be true when the user clicks once, dragging will return false as the oldTLSTate is not released
if (tl.State == TouchLocationState.Pressed && oldTLState.State == TouchLocationState.Released)
{
if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
{
popLocationX = (int)tl.Position.X;
popLocationY = (int)tl.Position.Y;
}
}
//At the end of the Update() method, update oldTLState with the current state
oldTLState = tl;

Related

Get scroll position of ViewCell inside ListView Xamarin Form

I would like to know the scroll position of ViewCell inside the ListView.
Tried with various ways but always that gives me 0 value.
My intension is to get ViewCell's position in screen. In order to resolve this problem trying to get it's scroll position and then i will add this value to the Y value of ListView object.
Can anybody please help me in this case?
you have to make custom renderer of the ViewCell its kinda tricky to send the positions to pcl then we subscribe to the event in the view here's my code
PCL
public class SAChatViewCell : ViewCell
{
public delegate int[] IntEventHandler(object sender, float[] postion);
public event IntEventHandler OnCellItemLongClicked;
public event EventHandler OnCellItemTouched;
public void InvokeOnCellItemLongClicked(object sender, float[] e)
{
//send the current grid
OnCellItemLongClicked?.Invoke(sender, e);
}
public void InvokeOnCellItemTouched(object sender, EventArgs e)
{
//send the current grid
OnCellItemTouched?.Invoke(sender, e);
}
}
Android Renderer
class SAChatViewCellRenderer : ViewCellRenderer
{
private bool selected;
ClickListener handler = new ClickListener();
static Android.Widget.ListView listView;
Xamarin.Forms.ListView listviewforms;
static SAChatViewCell cellElement;
Android.Views.View cellControl;
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
{
try
{
if (cellControl == null)
{
cellControl = base.GetCellCore(item, convertView, parent, context);
}
cellElement = item as SAChatViewCell;
selected = false;
listviewforms = cellElement.View.Parent.Parent as Xamarin.Forms.ListView;
if (listviewforms == null)
{
return null;
}
if (listviewforms.BackgroundColor.ToAndroid() == Color.Transparent.ToAndroid())
{
cellControl.SetBackgroundColor(Color.White.ToAndroid());
}
else
{
cellControl.SetBackgroundColor(listviewforms.BackgroundColor.ToAndroid());
}
cellControl.SetOnLongClickListener(handler);
cellControl.SetOnTouchListener(handler);
return cellControl;
}
catch (Exception ex)
{
return null;
}
}
protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnCellPropertyChanged(sender, args);
if (args.PropertyName == "IsSelected")
{
// I had to create a property to track the selection because cellCore.Selected is always false.
// Toggle selection
selected = !selected;
var selectedBackground = cellElement.SelectedBackgroundColor.ToAndroid();
if (selected)
{
if (selectedBackground == Color.Transparent.ToAndroid())
{
cellControl.SetBackgroundColor(Color.White.ToAndroid());
return;
}
cellControl.SetBackgroundColor(selectedBackground);
}
else
{
if (listviewforms.BackgroundColor.ToAndroid() == Color.Transparent.ToAndroid())
{
cellControl.SetBackgroundColor(Color.White.ToAndroid());
}
else
{
cellControl.SetBackgroundColor(listviewforms.BackgroundColor.ToAndroid());
}
}
}
}
internal class ClickListener : Java.Lang.Object, IOnLongClickListener, IOnTouchListener
{
//event priority Touch - LongClick - Click
//NOTE: return true to indicate that we have handled the event and it should stop here;
public bool OnLongClick(Android.Views.View sender)
{
var cellItem = sender as INativeElementView;
var viewCell = sender as Android.Views.View;
float[] location = new float[] { 0, 0 };
Android.Views.View parentRow = (Android.Views.View)viewCell.Parent;
listView = (Android.Widget.ListView)parentRow.Parent;
int position = listView.GetPositionForView(parentRow);
var x = parentRow.Right;
var y = (parentRow.Top - listView.DividerHeight) <= 0 ? parentRow.Bottom : parentRow.Top;
int view_height = parentRow.Height;
location[0] = (x / MainActivity.Current.Resources.DisplayMetrics.Density);
location[1] = y / MainActivity.Current.Resources.DisplayMetrics.Density;
//send current cell
cellElement.InvokeOnCellItemLongClicked((cellItem.Element as ViewCell).View, location);
listView.Scroll += ListView_Scroll;
return true;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (listView != null)
{
listView.Scroll -= ListView_Scroll;
}
}
private void ListView_Scroll(object sender, Android.Widget.AbsListView.ScrollEventArgs e)
{
cellElement.InvokeOnCellItemTouched(cellElement.View, EventArgs.Empty);
}
//return false if you have not handled it and/or the event should continue to any other on-click listeners.
public bool OnTouch(Android.Views.View v, MotionEvent e)
{
if (e.Action == MotionEventActions.Down)
{
cellElement.InvokeOnCellItemTouched(cellElement.View, EventArgs.Empty);
//cellCore.SetOnTouchListener(this);
}
return false;
}
}
}
}
iOS Renderer
class SAUITableViewCell : UITableViewCell
{
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
}
}
//When you scroll, your cells are created in real time. cells aren't created from scratch, instead iOS just takes a cell that has just left the screen and sends it through
class SAChatViewCellRenderer : ViewCellRenderer, IUIGestureRecognizerDelegate
{
UITableView TV;
SAChatViewCell cellElement;
public IntPtr Handle => new IntPtr();
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
try
{
UITableViewCell cell = base.GetCell(item, reusableCell, tv);
TV = tv;
var uiTapGestureRecognize = new UITapGestureRecognizer(OnClick);
var uiLongPressGestureRecognizer = new UILongPressGestureRecognizer(OnLongClick);
uiLongPressGestureRecognizer.MinimumPressDuration = 0.5;
cell.AddGestureRecognizer(uiTapGestureRecognize);
cell.AddGestureRecognizer(uiLongPressGestureRecognizer);
cellElement = item as SAChatViewCell;
cell.BackgroundColor = UIColor.Clear;
if (cellElement.SelectedBackgroundColor == Color.Transparent)
{
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
}
else
{
cell.SelectedBackgroundView = new UIView
{
BackgroundColor = cellElement.SelectedBackgroundColor.ToUIColor() ?? default(UIColor)
};
}
return cell;
}
catch (Exception ex)
{
throw ex;
}
}
private void OnLongClick(UILongPressGestureRecognizer arg)
{
//get the current touch coords based on listview
CGPoint coords = arg.LocationInView(TV);
//current cell
if (arg.State != UIGestureRecognizerState.Began)
{
var indexPath = TV.IndexPathForRowAtPoint(coords);
CGRect Rect = TV.RectForRowAtIndexPath(indexPath);
//delete the listview offset
Rect.Offset(-TV.ContentOffset.X, -TV.ContentOffset.Y);
var CurrentViewCell = (arg.View as UIKit.UITableViewCell).Superview;
//Note : xamarin forms cell element MonoTouch creates it's own internal delegate type for UIScrollView so we either override the uiviewtable or trigger the ondisappear event
var cellItem = arg.View as INativeElementView;
(((cellItem.Element as ViewCell).Parent) as ListView).ItemDisappearing += (s, o) =>
{
cellElement.InvokeOnCellItemTouched(cellElement.View, EventArgs.Empty);
};
float[] location = new float[] { 0, 0 };
location[0] = (float)Rect.X;
var Y = Rect.Top <= 0 ? Rect.Bottom : Rect.Top;
location[1] = (float)Y;
cellElement.InvokeOnCellItemLongClicked((cellItem.Element as ViewCell).View, location);
}
}
private void OnClick()
{
cellElement.InvokeOnCellItemTouched(cellElement.View, EventArgs.Empty);
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}
I have found a solution,
Problem :
My intension is to get ViewCell's position in screen
Solution :
Step 1 : Keep scrollview inside the Relative layout.
Step 2 : When user click on scroll view's ViewCell, save touch point (X, Y) of relative layout. In Y co-ordinate, add top position of relative layout so you will get touch point relative to whole screen.
Step 3 : When user click on scroll view's ViewCell, call XYZ() method.
Step 4 : Inside XYZ() method, do whatever functionality which required on (X, Y) co-ordinate. (Note : put 300ms delay in doing functionality in XYZ() method, as step-2 required some time in saving
touch points.)

Close the keyboard once button pressed in Xamarin

In a view controller I have a 2 text boxes (UITextField) and a submit button. The text boxes pop up the ASCII keyboard. The Submit button takes the values from the text boxes and does something with them.
When the keyboard is open, how do I kill it once the submit button is pressed
The keyboard has a Next button, how do I get it to go to the next field.
Using Xamarin Studio 4.0.12
Thank you!
You need to do what incmiko suggested. Here's the code in C#
Part 1.
txtUsername.ShouldReturn = TextFieldShouldReturn;
txtPassword.ShouldReturn = TextFieldShouldReturn;
create a function in your view
private bool TextFieldShouldReturn(UITextField tf)
{
//change the code below as per your validation
if (tf == _txtUsername)
{
_txtPassword.BecomeFirstResponder();
return true;
}
if(tf == _txtPassword)
{
// validate field inputs as per your requirement
tf.ResignFirstResponder();
return true;
}
return true;
}
Just have a try with this ,
It's just a sample,
NSObject keyboardShowObserver;
NSObject keyboardHideObserver;
public override void ViewWillAppear(bool animated) {
base.ViewWillAppear(animated);
keyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, (notification) => {
NSValue nsKeyboardBounds = (NSValue)notification.UserInfo.ObjectForKey(UIKeyboard.BoundsUserInfoKey);
RectangleF keyboardBounds = nsKeyboardBounds.RectangleFValue;
float height = View.Bounds.Height - keyboardBounds.Height;
if (NavigationController != null && NavigationController.TabBarController != null && NavigationController.TabBarController.TabBar != null) {
// Re-add tab bar height since it is hidden under keyboard but still excluded from View.Bounds.Height.
height += NavigationController.TabBarController.TabBar.Frame.Height;
}
someScrollView.Frame = new RectangleF(someScrollView.Frame.Location, new SizeF(View.Bounds.Width, height));
});
keyboardHideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, (notification) => {
UIApplication.EnsureUIThread();
someScrollView.Frame = new RectangleF(someScrollView.Frame.Location, View.Bounds.Size);
});
}
public override void ViewDidDisappear(bool animated) {
base.ViewDidDisappear(animated);
if (keyboardShowObserver != null) {
NSNotificationCenter.DefaultCenter.RemoveObserver(keyboardShowObserver);
}
if (keyboardHideObserver != null) {
NSNotificationCenter.DefaultCenter.RemoveObserver(keyboardHideObserver);
}
}

How to display buttons with background in whole width and height blackberry?

I am displaying buttons dynamically in my app. When I am determining the number of buttons to be displayed it works fine. However, now I want to change the background color of the button on click and I am able to do that too, but only the label portion changed its color and the rest all is still the same.
So, how can I solve this problem? Here is the code I have so far:
btn = new ButtonField[Global.vec_locdisablecityname.size()];
for(int i=0;i<Global.vec_locdisablecityname.size();i++){
btn[i] = new ButtonField("",ButtonField.CONSUME_CLICK |ButtonField.FOCUSABLE);
btn[i].setLabel((String)Global.vec_locdisablecityname.elementAt(i));
if(Global.flag == true){
Global.flag = false;
Bitmap bmp_backbtnclick=Bitmap.getBitmapResource("bg-btn-location.png");
Background bg_backbtnclick=BackgroundFactory.createBitmapBackground(bmp_backbtnclick);
btn[i].setBackground(bg_backbtnclick);
}
else {
if(Global.selectedbutton == i){
Bitmap bmp_backbtnclick=Bitmap.getBitmapResource("bg-btn-location.png");
Background bg_backbtnclick=BackgroundFactory.createBitmapBackground(bmp_backbtnclick);
btn[i].setBackground(bg_backbtnclick);
}
else {
btn[i].setBackground(null);
}
}
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if ( field instanceof ButtonField ) {
for ( int i = 0; i <= 1; i++ ) {
if ( field == btn[i] ) {
System.out.println("CLicked::::---->>>" +i);
btn[i].setBackground(null);
Global.selectedbutton = i;
selectedcityid = Global.vec_locdisablecitycode.elementAt(i).toString();
System.out.println("value of selected city id:::-->>" +selectedcityid);
selectedcitycodeparse(selectedcityid);
selectedstartloading();
break;
}
else {
btn[i].setBackground(null);
}
}
}
}
};
btn[i].setChangeListener(listener);
hfmbuttons.add(btn[i]);
}

is it possible to move the map on touch in blackberry by using MapField.

I am using mapField to create a custom map.I am using the code in this link.
How to show more than one location in Blackberry MapField?.
But the map position is fixed. i am not able to drag the map as we can do in google maps or when we invoke the maps like
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments());
}
Here's some code that should get you going on the correct path. I've taken it from a project of mine that had some special requirements, so there could be some remnants of that left in there inadvertently. There will be some undefined variables in there -- they're member variables that are declared in the class and should all start with an underscore. This is also part of a class that extends MapField, so you would have to create a custom map class and then use that rather than the default.
protected boolean touchEvent(TouchEvent message) {
boolean ret = super.touchEvent(message);
//mark that we're starting to interact
if(message.getEvent() == TouchEvent.DOWN) {
_startTouchTracking = true;
_clicking = true;
_touchX = message.getX(1);
_touchY = message.getY(1);
}
//user is wanting to move the map
else if(message.getEvent() == TouchEvent.MOVE) {
int dx = _touchX - message.getX(1);
int dy = _touchY - message.getY(1);
_clicking = false;
_touchX = message.getX(1);
_touchY = message.getY(1);
//perform checks to make sure we don't move outside of the map's range
int lat = getLatitude() - dy*(int)MathUtilities.pow(2, (double)getZoom());
if(lat < -9000000) {
lat = -9000000;
}
else if (lat > 9000000) {
lat = 9000000;
}
int lon = getLongitude() + dx*(int)MathUtilities.pow(2, (double)getZoom());
if(lon < -18000000) {
lon = -18000000;
}
else if (lon > 18000000) {
lon = 18000000;
}
moveTo(lat, lon);
}
//if the person just touches and releases, we want to move to that spot
else if (message.getEvent() == TouchEvent.UNCLICK && _clicking) {
int dx = message.getX(1) - getWidth()/2;
int dy = message.getY(1) - getHeight()/2;
move(dx, dy);
_clicking = false;
}
//touch has been released
else if (message.getEvent() == TouchEvent.UP) {
_startTouchTracking = false;
}
//we handled the click
return true;
}
As said, this might need tweaking for your use, but in general should get you started. The MathUtilities.pow() calls were my way of coming up with an appropriate amount of motion depending on the zoom level.
Edit for Comments
Letting a Bitmap move with the map:
protected Coordinates _bitmapCoordinates;
protected Bitmap _bitmap;
public YourMapField() {
//we're going to put the bitmap at -38.43, 20.32
_bitmapCoordinates = new Coordinates(-38.43, 20.32, 0.0);
_bitmap = YOUR_CODE_TO_GET_THE_BITMAP;
}
protected void paint(Graphics g) {
super.paint(g);
XYPoint placeToPaintBitmap = new XYPoint();
convertWorldToField(_bitmapCoordinates, placeToPaintBitmap);
//perform a check here to make sure that field will be seen. This code would depend
//on how you're painting the image. Just check the placeToPaintBitmap.x and placeToPaintBitmap.y
//against 0 and the map's width and height, along with some adjustment for how you paint
if(bitmap will be visible on the screen) {
//The code I have here is drawing the bitmap from the top left of the image, but if
//you need to draw from some other place you may have to offset the x and y
g.drawBitmap(placeToPaintBitmap.x, placeToPaintBitmap.y, _bitmap.getWidth(), _bitmap.getHeight(), 0, 0);
}
}
I didn't test any of that code, so it might be buggy but should give you the general idea.

Blackberry: How can I make a buttonfield act like toggle button?

I have a single button which I want to use as Start/Stop button. How can I make the buttonfield work as toggle button?
Please help.
Just change button label on fieldChange or navigationClick or touchEvent, don't forget to save toggle state in class member:
class ToggleButtonField extends ButtonField {
int mToggleState = -1;
String[] mLabels = {};
public ToggleButtonField(String[] labels) {
super(CONSUME_CLICK);
if(labels != null && labels.length > 0)
{
mLabels = labels;
mToggleState = 0;
updateLabel();
}
}
private void updateLabel() {
setLabel(mLabels[mToggleState]);
}
protected void fieldChangeNotify(int context) {
mToggleState = getNextToggleState(mToggleState);
updateLabel();
super.fieldChangeNotify(context);
}
private int getNextToggleState(int state) {
int result = mToggleState+1;
if(result >= mLabels.length)
result = 0;
return result;
}
}

Resources