I am trying to write some code that will allow the user to draw on the touch screen.
When using either GestureService or ManipulationStarted/Delta, there's a "pause" that occurs when the user starts moving their finger - only when the finger is far enough from the point in which it started, only then do you start getting ManipulationDelta events (and like I said, same deal is true for GestureService).
What can I do to avoid this threshold? It really does not work well with drawing code.
Just blogged about it as I have come across similar questions on AppHub Forum.
https://invokeit.wordpress.com/2012/04/27/high-performance-touch-interface-wpdev-wp7dev/
Manipulation Delta and Gesture services are high level touch interfaces. If you want performance, consider using low level interfaces: Touch and an event called TouchReported. I tend to use them mostly (for drawing / position detection) in many of my projects
The event you want to plug in to detech touch is
Touch.FrameReported += Touch_FrameReported;
You can do this in Loaded event. Here's the implementation of the Touch_FrameReported handler. WorkArea is Canvas in this. I have also used this in conjugation with WritableBitmap
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
try
{
// Determine if finger / mouse is down
point = e.GetPrimaryTouchPoint(this.workArea);
if (point.Position.X < 0 || point.Position.Y < 0)
return;
if (point.Position.X > this.workArea.Width || point.Position.Y > this.workArea.Height)
return;
if (this.lbLetter.SelectedIndex == -1)
return;
switch (point.Action)
{
case TouchAction.Down:
draw = true;
old_point = point;
goto default;
case TouchAction.Up:
draw = false;
break;
default:
Draw();
break;
}
}
catch
{
MessageBox.Show("Application encountered error processing last request.");
}
}
This works lot better than high level interfaces.
Related
I am developing a Java 2D Game, in which I have used a KeyListener, but as you probably can guess, it has focusing issues, mainly when the Player is running and you keep the same key pressed for long, for example pressing "W" to run forward, but after some seconds of keep pressing W, the KeyListener dies and no key works, I want to use KeyBindings, as most people suggest it for game development, but I cannot find any usefull tutorials, most of them use some form of Buttons, and other useless features for my game, so how can i avoid the KeyListener from losing focus, or how can I write a simple KeyBinding code, that only moves the player, and other simple stuff used in a game.
This is the kind of Key Binding that I want, I KNOW IT DOES NOT WORK, it is an example:
component.getInputMap().put(KeyStroke.getKeyStroke(VK_W),
"move forward")
component.getActionMap().put("released",
releasedAction);
if(releasedAction == true){
Player.playerSpeedY = 7;
} else{
Player.playerSpeedY = 0;
}
FWI: this is the current KeyListener code:
if(HUD.PlayerHealth > 0){
if(key == KeyEvent.VK_W) {Player.playerSpeedY = -5; keyDown[0]= true;}
if(key == KeyEvent.VK_S) {Player.playerSpeedY = 5; keyDown [1]= true;}
if(key == KeyEvent.VK_A) {Player.playerSpeedX = -5; keyDown [2]= true;}
if(key == KeyEvent.VK_D) {Player.playerSpeedX = 5; keyDown [3]= true;}
}
I keep looking for Actions and Input maps tutorials, or KeyBinding ones, and I just don't find anything useful, another dough, the component in action and input map, what is it for?, should my entire code be based on that, and is there any way to only make the action map move the player, and only that?
Try something like this:
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right");
this.getActionMap().put("right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//Do Something Here
Player.playerSpeedX = 5;
}
});
How to use KeyBindings
With my team we want to implement area learning for relocalization purposes in our projects.
I added this functionnality and it seems to work well. But when a drift disaster happens (motion tracking lost) and that the main camera is instantaneously projected in "the other side of the universe" the program doesn't succeed in relocalizing it : the camera is 2 meters below, or 3 meters beside than where it should be.
Is it an area description error (because it has got not enough point of interests) ?
Or I still have not understood how to use area learning ?
Thanks a lot.
P.S.:
I use the Unity SDK.
public void Update()
{
TangoPoseData pose = new TangoPoseData ();
TangoCoordinateFramePair pair;
if(poseLocalized)
{
pair.baseFrame = TangoEnums.TangoCoordinateFrameType.TANGO_COORDINATE_FRAME_AREA_DESCRIPTION;
pair.targetFrame = TangoEnums.TangoCoordinateFrameType.TANGO_COORDINATE_FRAME_DEVICE;
}
else
{
pair.baseFrame = TangoEnums.TangoCoordinateFrameType.TANGO_COORDINATE_FRAME_START_OF_SERVICE;
pair.targetFrame = TangoEnums.TangoCoordinateFrameType.TANGO_COORDINATE_FRAME_DEVICE;
}
double timestamp = VideoOverlayProvider.RenderLatestFrame(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR);
PoseProvider.GetPoseAtTime (pose, timestamp, pair);
m_status = pose.status_code;
if (pose.status_code == TangoEnums.TangoPoseStatusType.TANGO_POSE_VALID)
{
// it does not differ with the pair base frame
Matrix4x4 ssTd = UpdateTransform(pose);
m_uwTuc = m_uwTss * ssTd * m_dTuc;
}
}
public void OnTangoPoseAvailable(TangoPoseData pose)
{
if (pose == null)
{
return;
}
// Relocalization signal
if (pose.framePair.baseFrame == TangoEnums.TangoCoordinateFrameType.TANGO_COORDINATE_FRAME_AREA_DESCRIPTION &&
pose.framePair.targetFrame == TangoEnums.TangoCoordinateFrameType.TANGO_COORDINATE_FRAME_START_OF_SERVICE)
{
poseLocalized = true;
}
// If pose status is not valid, nothing is valid
if (!(pose.status_code == TangoEnums.TangoPoseStatusType.TANGO_POSE_VALID))
{
poseLocalized = false;
// Do I forget something here ?
}
}
I've regularly observed that the localization and re-localization of the Area Learning can produce x,y Pose coordinates off by a few meters.
Coordinates can be more accurate if I take more care in recording an area well before moving to a new area.
Upon re-localization the coordinate accuracy is improved if the tablet is able to observe the area using slow, consistent movements before traveling to a new area.
If I learn a new area I always return to a well known area for better accuracy as described by drift correction:
I have two Tango tablets using a Java app that is autonomously navigating an iRobot in my home. I've setup a grid test site using 1 meter tape marks to make the observations.
just want to know if that is a normal behaviour and if there is anything that could prevent it.
I am using OpenGL and TaoClassic (might not be relevant to the issue but well) on a Windows Form.
At the moment, I have a very basic square that moves around the window and when reaching the edges of the window, then the velocity is inverted (x when left/right, y when top/bottom).
So far nothing fancy.
Where it gets fancy is as the object moves along, if I click on the border of the form, then the application stops, new drawing are not happening but the application partially keeps updating. I say partially because from the code below the first two lines keep on running while the "collision" detection is not
transform.position.x += velocity.x * xSign * Time.deltaTime;
transform.position.y += velocity.y * ySign * Time.deltaTime;
if (penguin.transform.position.x + penguin.sprite.texture.width / 2f > Engine.Screen.Width ||
penguin.transform.position.x - penguin.sprite.texture.width / 2f < 0) xSign *= -1;
if (penguin.transform.position.y + penguin.sprite.texture.height / 2f > Engine.Screen.Height ||
penguin.transform.position.y - penguin.sprite.texture.height / 2f < 0) ySign *= -1;
no genius trick. When I release the mouse button, then the object jumps to the position it should be meaning the update kept running on the back. But if I keep pressing long enough, the object gets out of bound but does not bounce. When I release, I get my square out of window and starting to jitter at the borders.
I got this:
protected override void OnClientSizeChanged(EventArgs e)
{
base.OnClientSizeChanged(e);
Engine.Screen.Width = this.ClientSize.Width;
Engine.Screen.Height = this.ClientSize.Height;
// This is just to set the window size and the position of origin
App.SetProjection2D(Engine.Screen.Width, Engine.Screen.Height, App.Projection);
Gl.glViewport(0, 0, Engine.Screen.Width, Engine.Screen.Height);
}
and here is PeekMessage of the application:
private void OnApplicationEnterIdle(object sender, EventArgs e)
{
Message msg;
while (!PeekMessage(out msg, IntPtr.Zero, 0, 0, 0))
{
_timer.SetTime();
Input.Update();
_callback();
}
}
I would guess the problem lies in one of those. The Update and Draw methods are called from the _callback(); method
Just for info, when clicking on the form, it does not stop the application so it has to do with the event based on clicking on borders.
Any idea?
It is normal behaviour. Moving and resizing events are blocking (that's the way default WndProc works) and OnApplicationEnterIdle isn't called.
AFAIK the only way to continue updating when window is resized is to use different thread for rendering. In that case, only main thread will block on move and resize events, while rendering thread will continue updating viewport.
I'm creating a game where the player will use Drag gestures to "slash" an enemy, swiping their finger across the screen to kill them.
I have a feeling this should be easier than it is, but currently the code I have to detect this is:
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
if (gs.GestureType == GestureType.FreeDrag ||
gs.GestureType == GestureType.HorizontalDrag ||
gs.GestureType == GestureType.VerticalDrag)
{
Current_Start = gs.Position;
Current_End = Current_Start + gs.Delta;
}
if (gs.GestureType == GestureType.DragComplete)
{
DragEnded = true;
}
}
This isn't quite working, though. I need the two vectors of:
Where the drag started
Where the drag ended
What is wrong, and how would I get this to work?
I would try using the onmousedown and onmouseup events to get the start and end points and go from there.
I'm writing an application that runs an algorithm, but allows you to 'step through' the algorithm by pressing a button - displaying what's happening at each step.
How do I listen for events while within a method?
eg, look at the code I've got.
static int proceed;
button1Event(GtkWidget *widget)
{
proceed = 0;
int i = 0;
for (i=0; i<15; i++) //this is our example 'algorithm'
{
while (proceed ==0) continue;
printf("the nunmber is %d\n", i);
proceed = 0;
}
}
button2Event(GtkWidget *widget)
{
proceed = 1;
}
This doesn't work because it's required to exit out of the button1 method before it can listen for button2 (or any other events).
I'm thinking something like in that while loop.
while(proceed == 0)
{
listen_for_button_click();
}
What method is that?
The "real" answer here (the one any experienced GTK+ programmer will give you) isn't one you will like perhaps: don't do this, your code is structured the wrong way.
The options include:
recommended: restructure the app to be event-driven instead; probably you need to keep track of your state (either a state machine or just a boolean flag) and ignore whichever button is not currently applicable.
you can run a recursive main loop, as in the other answer with gtk_main_iteration(); however this is quite dangerous because any UI event can happen in that loop, such as windows closing or other totally unrelated stuff. Not workable in most real apps of any size.
move the blocking logic to another thread and communicate via a GAsyncQueue or something along those lines (caution, this is hard-ish to get right and likely to be overkill).
I think you are going wrong here:
while(proceed == 0)
{
listen_for_button_click();
}
You don't want while loops like this; you just want the GTK+ main loop doing your blocking. When you get the button click, in the callback for it, then write whatever the code after this while loop would have been.
You could check for pending events & handle the events in while loop in the clicked callback. Something on these lines:
button1Event(GtkWidget *widget)
{
proceed = 0;
int i = 0;
for (i=0; i<15; i++) //this is our example 'algorithm'
{
while (proceed ==0)
{
/* Check for all pending events */
while(gtk_events_pending())
{
gtk_main_iteration(); /* Handle the events */
}
continue;
}
printf("the nunmber is %d\n", i);
proceed = 0;
}
}
This way when the events related click on the second button is added to the event queue to be handled, the check will see the events as pending and handle them & then proceed. This way your global value changes can be reflected & stepping should be possible.
Hope this helps!
If you want to do it like this, the only way that comes to my mind is to create a separate thread for your algorithm and use some synchronization methods to notify that thread from within button click handlers.
GTK+ (glib, to be more specific) has its own API for threads and synchronization. As far as I know Condition variables are a standard way to implement wait-notify logic.