How Can I Improve The Look Of This Transition? - c#

I am trying to translate two buttons down and off-screen after one is clicked. I have it working, but it is very choppy. How can I make this transition more fluid?
-I am using Microsoft Visual Studio 2015
-The language I am using is c#
Here is my transition code:
private void timer1_Tick(object sender, EventArgs e)
{
int speed_increase = 100;
if(sleep_button.Top <= 800 || calibrate_button.Top <= 800)
{
sleep_button.Top += speed_increase;
calibrate_button.Top += speed_increase;
}
}
I have my timer interval set to 1ms too, just because I am aware the larger interval the choppier it becomes. I have been looking all day for a button transition tutorial and found none. And if this isn't possible can somebody sent me a tutorial for button fade outs? Thanks everyone

Your timing choice is off. 1ms is too granular. Try something around the 20ms mark, this will cause your code to run 50 times per second, which is rather closely aligned with common refresh rates of around 50hz.
How far do the buttons need to scroll? How long should it take?
Assume X is the distance to scroll, and Y is the time in seconds. The number of iterations is Y * 50 (50 times per second), and the distance per step is X / Y.
X = 200 pixels.
Y = 2 seconds.
Distance per step is:
X / (Y * 50)
200 / (2 * 50)
200 / 100
== 2
If you want to scroll 200 pixels over 2 seconds, you want to add 2 to the Y position of each button on each iteration.
I hope that makes sense.

How about hiding it altogether?
private void timer1_Tick(object sender, EventArgs e)
{
sleep_button.Visible = false;
calibrate_button.Visible = false;
}

Related

C# WPF MVVM Controlling mediaplayer position with slider makes audio stutter

For starters, as for now, everything else works perfectly. So I have a project what includes a music player part (a mediaplayer). Position is visualised by text and with a slider, and that's where my problem is. When I make the slider "draggable" (so I can set the position, not just get it) the media (.mp3) starts to stutter randomly. Here is the part of the code:
public double ElapsedSeconds
{
get
{
return mediaPlayer.Position.TotalSeconds;
}
set
{
//mediaPlayer.Position = TimeSpan.FromSeconds(value);
PropertyChanged(this, new PropertyChangedEventArgs(nameof(ElapsedSeconds)));
}
}
private void Timer_Tick(object sender, EventArgs e)
{
ElapsedSeconds = mediaPlayer.Position.TotalSeconds;
TextContent = String.Format("{0} - {1} - {2:mm\\:ss}/{3:mm\\:ss}", playedArtist, playedTitle, TimeSpan.FromSeconds(ElapsedSeconds), TimeSpan.FromSeconds(TotalSeconds));
}
ElapsedSeconds is binded to the value of the slider.
I can only drag the slider (and create the problem) when I uncomment that 1 line
And here is what I already tried:
- I used private field to store data
- Tried to use miliseconds and ticks but it got worse
- Tried to change the dispatchertimers tick frequency (currently 100 ms) what refreshes the current value
Is there any way to make the stuttering go away? (and if yes, what is it)
p.s.: If anyone interested, here is the whole .cs file: GitHub Link
After not thinking on the problem for almost 2 weeks I found a solution. Just need a condition to when to set the Position (so I changed the commented line to these):
if (mediaPlayer.Position > TimeSpan.FromSeconds(value + 1) || mediaPlayer.Position < TimeSpan.FromSeconds(value - 1))
mediaPlayer.Position = TimeSpan.FromSeconds(value);
Although at least 1 second of change is required, this way the program ignores the milisecond rollbacks what cause stuttering, while the Slider is still draggable.

Looking for ways to optimize multi poly-lines on canvas in wpf

I'm working on program in WPF that need to plot on one of her screens 50 poly-lines that describes movement of some energy in space. One more demand from the program is to move two lines and a dot to indicate the mouse location when its relevant.
The problem is that when the poly-lines cover a high percent of the canvas and the mouse is fly over the canvas then all start to run really slowly.
I succeeded to improve this a little but its still not good enough so I'm asking for your help please.
So because this canvas related to another one, I spited the work for two - the handler and the movement function:
private void rayTraceCanvas_MouseMove(object sender, MouseEventArgs e)
{
System.Windows.Point pos = e.GetPosition(rayTraceCanvas);
mouseMove(pos);
if (m_MouseMoveCallback != null)
m_MouseMoveCallback(m_CurrentDepth);
}
And then the movement function:
private void mouseMove(Point currentPos)
{
m_CurrentDepth = Math.Round((currentPos.Y) / (m_PixelPerDepthUnit));
m_CurrentRange = Math.Round((currentPos.X) / (m_PixelPerRangeUnit));
DepthPos.Text = "D: " + m_CurrentDepth.ToString();
RangePos.Text = "R: " + m_CurrentRange.ToString();
rayTraceWidthLine.Y1 = currentPos.Y;
rayTraceWidthLine.Y2 = currentPos.Y;
rayTraceHeightLine.X1 = currentPos.X;
rayTraceHeightLine.X2 = currentPos.X;
Canvas.SetLeft(rayTraceDotOnGraph, currentPos.X - (rayTraceDotOnGraph.Width / 2));
Canvas.SetTop(rayTraceDotOnGraph, currentPos.Y - (rayTraceDotOnGraph.Height / 2));
}
I tried this without the deleget from the handler function and its work the same so the problem isn't there.

Dynamic DSP.setParameter with FMOD

I want to play sound with effect.
In example, I'm using low and highpass, I want to set the parameters of two effect with the mouse location (x, y).
I used DSP.reset() and DSP.setParameter(), but, there is a problem.
If I moved the mouse faster and faster, the Sound is not smooth.
The original sound is played between time of the code reset() and setParameter() .
Therefore I can hear a sound like spark( 'tick! tick!').
I want to make it smooth.
Is there any way??
private void mouse_effect_move(object sender, MouseEventArgs e)
{
int i;
i = e.Y / 10;
dsplowpass.reset();
dsphighpass.reset();
if (i < 9)
{
dsphighpass.setParameter(0, 6310 - 700 * i);
//dsphighpass.setParameter(1, 1);
}
else
{
dsplowpass.setParameter(0, 22000 - 2200 * (i - 9));
//dsplowpass.setParameter(1, 1);
}
}
You don't need the calls to DSP::reset when using DSP::setParameter. Does the problem go away if you remove them?

How to animate a simple shape moving vertically?

So, I have a rectangle "rectangle1", at 160,160.
I want it to move smoothly to cordinates 160,30, with a duration of about 1 second. (time delay)
I've figured out that some basic code to move the shape is
rectangle1.Location = new Point(160,30);
However, when I tried doing a for loop with
rectangle1.Location = new Point(160, rectangle1.Location.Y - 100);
it just moved there instantly. Which I should have expected really. Same occurred with
int count = 0;
while(count != 300)
{
rectangle1.Location = new Point(160, rectangle1.Location.Y -1);
count += 2;
}
So, I assume I need some sort of clock / timer loop, that moves it by x pixels every x milliseconds. Not sure how to do this, so help would be appreciated.
Also, I'm going to be animating two other rectangles horizontally, which will then move up at the same time/speed as rectangle1. I think I'll have to "delay" rectangle1's movement until they are in position, correct?
Thanks.
PS: I've googled a fair bit, but since I'm not entirely sure what I'm looking for, it wasn't very fruitful.
If you need smooth movements, it's great to use timers, threads, backgroundworkers.
Here is what you need to do. Assuming you have the code that increment/decrement x,y points for the shape.
Steps:
set timer interval to for e.g. 100
set an integer int count=0; *
in timer_tick event do the moving work
private void timer1_Tick(object sender, EventArgs e)
// no need to use your while loop anymore :))
{
If(count< 300) //set to your own criteria
{
//e.g. myrect.location=new point(x,y);
// rectangle1.Location = new Point(160, rectangle1.Location.Y -1);
}
count += 2;
}

when I connect for example 14 points with lines and try to move them its moving very very slow what's making it to move that slow?

This is the paint event:
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
SolidBrush brush;
Pen p=null;
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
brush = new SolidBrush(Color.Red);
p = new Pen(brush);
for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);
}
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
int startIndex = wireObject1._connectionstart[i];
int endIndex = wireObject1._connectionend[i];
connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
moveCounter++;
textBox1.Text = moveCounter.ToString();
}
}
On the loop in the bottom I'm running over the _connectionstart List which is type of int.
I added a int variable I called it moveCounter to see how many time its calling this loop and drawing the lines.
If I'm adding two points over the pictureBox1 connect them with one line then drag one of the points around its moving smooth. Same thing with 3 connected points and with 7 connected points but when I'm getting to 9-10 points connected with many lines and try to move it drag it around or any point that is not connected everything is moving very very slow and as much as I add a new connected line between two points its getting slower.
So the problem can be a bug with my loop's in the paint event or that in the move event its doing so many time pictureBox1.refresh();
So I added a timer enabled it true in the designer and set it to 30 milliseconds. In the move event I raise a flag and in the timer tick event I check if the flag is on I make a pictureBox1.refresh(); and reset back the flag. The idea is that when I move the points or the connected points it will do it each 30 milliseconds.
But it didn't solve the problem. Still when I have 9-10 points connected with many lines between them everything is getting very slow.
This is the picturebox1 mouse move event:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
mouseDrag = true;
Point NewPoint = e.Location;
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
refreshFlag = true;
timer2.Start();
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}
timer2 is another timer I added just to see how many times it moved in 5 seconds. So if timer1 set to 30 milliseconds I should see after 5 seconds something about 150 moves.
So in timer1 tick event I did:
private void timer1_Tick(object sender, EventArgs e)
{
if (refreshFlag == true)
{
pictureBox1.Refresh();
refreshFlag = false;
}
}
I was sure the timer1 idea should solve the problem but it didn't. So I wonder where the problem is maybe i did something wrong with the loop's in the paint event ?
The List _connectionstart contain indexes for example if I have two points connected with one line and I clicked on one point and drag this point around which make the line the be longer or shorter then in _connectionstart for example I have one place/cell [0] which contain index 0
In the List _point_X I have for example now two cells place in [0] 120.0 and in [1] 180.0 which are the points coordinates same for the _point_Y List.
Now the question is where is the problem ? A bug in the paint event ? Something with the move event ? I can't figure out why its getting so slow when it have 9-10 points connecting with a lot of lines between them.
For example I tested now with two points connected with a line I moved one point around which stretch the line longer or shorter and after 5 seconds the result was 160 moves counted then after another 5 seconds it was 323 and so on. So it seems to be working the timer1 and the 30 milliseconds or I'm wrong?
First, a few tweaks for the application:
Did You try to run the logic separated from drawing? => For example, an "updateData()" method and a "draw()" method? Also I would recommend to use the DoubleBuffer.
Second, if the performance is bad for a larger set of data (when You add more points), than Your algorithm is not efficient enough. If drawing a polygon is what You want, first update the vertex coordinates and then call Graphics.DrawPolygon

Categories