speedometer-icon

Technorati Tags: Speedometer,Winodws Phone 7,development

Creating a speedometer ,largely depends whether you want to display a numerical readout, or as a rotating needle.

First, to get the actual speed value, as mentioned in another answer, you can simply use the rigid body’s velocity. However, Unity’s default scale is one unit = one meter, so reading rigidbody.velocity.magnitude will give you the speed in meters per second.

To convert to MPH, you can use:

Convert to KPH:

Next to display it.

To show the speed as a numerical readout is comparitively simple:

1) Create a GUIText gameobject (from the gameobject menu).

2) In your car script, create a var which will store a reference to this GUIText GameObject:

 

3) Select your car, and drag a reference from the new GUIText GameObject in the hierarchy into this variable slot in car script, in the inspector.

4) Now, in your car script, you can add the lines in your Update() function to calculate the MPH, and update the text displayed:


That should get you working with a numerical readout.

 

To display as a rotating needle requires some trickier coordination. There’s no simple way to rotate a GUI Texture, or a texture drawn using the OnGUI method, so I’ve written a small general-purpose script which you can place on a gameobject to create a rotatable GUI Texture. You can control it by setting the ‘angle’ variable from other scripts.

So:

1) Create a new C# script. Name it “RotatableGuiItem”, and paste in this script:





























 

3) Create a new empty GameObject. Name it “mph needle”. Add the RotatableGuiItem script to it.

4) Assign your speedo needle “Texture” variable. You probably want to use a texture with a transparent alpha background for this, and bear in mind that it will rotate around the centre of the image. Adjust the “size” values to match the size of your texture.

5) Adjust the position of the texture using the X and Y position values of the GameObject in the inspector, so that it is your desired position. (probably in the centre of a normal static GUI Texture showing the mph dial face).

6) In your car script, create a var which will store a reference to this rotatable GUI item:

7) Select your car, and drag a reference from the “mph needle” GameObject in the hierarchy into this variable slot in car script, in the inspector.

8) Now, in your car script, you can add the lines in your Update() function to calculate the MPH, and update the needle’s angle:


You will probably need to adjust how far the needle turns in relation to the mph, and at what angle it starts, so you may end up with a line which looks more like this:

 

Which means the needle will be rotated by 20 degrees when the mph is zero, and will rotate 1.4 degrees for every 1 mph.

(If you want to control this script from a Javascript script, you’ll have to move the C# RotatableGuiItem into a folder called PlugIns in your assets.)

Hopefully this should get you to the stage where you have a working speedometer with a rotating needle!

 

Please comment, I have worked hours solving this, if you comment, it will give me some support!Laughing out loud

I saw a question on stackoverflow.com about theme management in a Silverlight application. As I stated in my answer, there’s no specific API to manage a theme like you know it in Windows. However you have the concept of ResourceDictionnary. Yesterday I posted a link to a video from Microsoft showing different applications with very different “themes”. So you have some liberties from the minimalist UI of Windows Phone 7. But how can you change the default color while still following the guidelines of Microsoft? You can modify the styles provided in the SDK. In the folder “C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Design” you will find several XAML files:

ThemeResource.xaml contains the basic Color, SolidColorBrush and TextBlock resources. You can copy/paste this file in your application and reference it in the App.xaml to override specific styles.

System.Windows.xaml has the styles for controls like Button, CheckBox, etc. Again you can use it to override the look and feel of the widgets.

Finally, there is one folder for each of the 10 “accent” colors (blue is the default) for both the light and dark themes (the operator can provide a 11th color).

 

This is a sample with the default dark theme :







In the Styles.xaml file you have a copy of the content of ThemeResource.xaml file from the SDK :









 

And now the look and feel of your application is customized.

One important notice: even though if you can override all the controls’ styles you should keep an eye on the implicit contract imposed by the platform i.e. the “Metro” UI guidelines. For example, it’s probably a bad idea to change the appearance of the default Button because your users will look after a particular widget in the page to save, confirm, send, etc.

Please Feelo Free To contact me and please do comment, I need them for sure.

Creating File Uploader For Window Phone 7

This is very basic level small user control, in which we can discuss how to create web user control.

To create user controls follow the steps:

  1. Right click on project
  2. Click on Add
  3. Select New Item
  4. Select Web User Control
  5. Specify some Name

I have given name to this file as “uploader.ascx” and kept this file under “userControl” for simplicity purpose.

On this page I am having following controls:

 







































































 

The above function takes care of following things before uploading file to the folder

  1. Invalid file name supplied.
  2. If file not exists or content length 0.
  3. Folder name exists.

 

Error Handling

 

While uploading done with UnauthorizedAccessException and returned with the message

On upload button click I am having following code

 






















 

i.e. I have set up path of folder to upload image

To access control in project, I have added page called as “uploadTester.aspx” to the project in which I have added following line:

<%@ Register TagPrefix=”img” TagName=”Uploader” src=”userControl/uploader.ascx”%>

Which says that this control is register to this page with specified source.

And in HTML code I have added following code inside form tag:

<img:Uploader runat=”server” id=”Uploader1″></img:Uploader>

That’s all about

 

General:

To upload any of the file in respective folder user need to have permission for writing to the folder so please follow the following steps to prevent from the error.

Set permission to virtual directory by following steps in IIS

  • Right Click on virtual directory which you have created for this project. Under directory Tab you will find
    1)Read
    2)Log Visits
    3)Index this resources
    Are marked as checked (enables) in addition to this make
    4)Write access enabled or checked
  • Click on apply
  • Click on ok

This will set right permission to entire virtual directory, this way we can minimize error from the front end for permission / access denied.

Other way to solve permission denied issue is to go to actual folder “images” by using physical path and follow these steps:

  • Right click folder
  • Sharing Tab
  • Enable share this folder radio button
  • Click Apply
  • Click Ok

If u are using this code on 2000 server you should do following:

  • Right click respective folder
  • Go to security tab
  • Select Everyone user
  • Apply full control
  • click on ok

 

Please Do comment!

  • Click on Add
  • Select New Item
  • Select Web User Control
  • Specify some Name

I have given name to this file as “uploader.ascx” and kept this file un

Creating a Simple Download Manager For Window Phone 7

Start by creating a C# Windows Application project in Visual Studio and on the form drag 3 labels, 2 textboxes, 2 buttons and one progressbar.

As you can see in the form above, the first two labels are just for describing the role of the first two textboxes which are named txtUrl and txtPath. They will obviously hold the URL of the file we want to download and the local path (and filename) to where we want to save it. The default values I set in the project will download a short video from Microsoft’s Channel 9 website to C:\Filename.wmv.
The two buttons – btnDownload and btnStop – are used for starting and stopping a download, while the label below them – lblProgress – is used for showing the current progress of the download in bytes and percentage. Finally, the ProgressBar located at the bottom of the form is entitled prgDownload and it display the progress of the download on a scale from 1 to 100.

Now let’s get into coding. Switch to code view, and the first thing we need to do is to add a few using statements for the namespaces we’re going to use. Add the following 3 lines below the already existing using statements:



 

 

System.Net will be used for connecting to the web server, System.IO will be used for saving the file to the hard drive, and System.Threading will be used for the thread in which we’re going to put the downloading process.

Inside the form’s class, right above the constructor, add the following lines:














 

 

As you can see, we are creating a few objects that we’re going to use later in the code. The object that stands out the most is not really an object, but a delegate: UpdateProgressCallback – this delegate will be used to call a method you’ll see later in our code – UpdateProgress – from inside the thread. Because you see, from inside a thread we can’t update the form elements directly (the label and the progress bar), so we need to create a delegate first, that takes the same arguments as the method. In our case the arguments are two Int64 variables that hold the number of bytes we downloaded from the server by now, and the number of total bytes the file has. Int64 is needed because this number can be really big.

Now let’s review the biggest piece of the code. The one where the file actually gets downloaded. We’ll put this code in a method called Download() which we’re going to call in a thread when the download button is pressed. Here goes the method, below it there’s more explanation of the code:











































 

The first line inside the method mentions that inside this method we’ll be using the wcDownload object, which can be disposed after we’re finished. If any error happens within the code, we have a finally block which closes the streams to prevent keeping a connection opened uselessly and to prevent the local file from being locked by the code.

Inside the try block we first retrieve information about the file using HttpWebRequest and HttpWebResponse objects. Note that some servers don’t give information about the size of the file, case in which we can only download blindly. If the web server did not return any information regarding the size of the file, webResponse.ContentLength will return -1.

After we get the size of the file, we define the stream that retrieves the bytes from the server, and the stream that saves the file to the hard drive. Before starting to stream the bytes down the cables, we create a buffer where we store the data that is written to the hard drive file. The buffer is 2048 bytes in size, but you can change it to a different value if you prefer.

In the while loop we loop through the buffer and write the content of the buffer to the file on the local drive. We also use the Invoke method of the form to call UpdateProgressCallback (the delegate of UpdateProgress). In the array we pass two parameters that UpdateProgress accepts: how much we downloaded until now (by measuring the length of the local stream), and how big the total file is. If you don’t have any knowledge of threading in C#, you probably would have guessed that you can update the form elements (labels, progress bars, etc.) directly, but for good enough reasons you can’t.
If we were to call the Download() method directly, then we wouldn’t have to use this.Invoke to call the UpdateProgress method. Speaking of UpdateProgress, let’s see how this method looks like:









 

We do a simple math calculation to get the percentage (0 to 100) and we set it on the ProgressBar to reflect the progress. We also set the label with information on the progress of the download.

We’re done with the methods for this application, now we only need to create the two event handlers for the Download and Stop buttons. Double clicking btnDownload in Visual Studio 2005 will create the Click event handler for you. Use the following code:









 

In the code above we start a new thread, to which we pass the name of the method (without the parenthesis). Then we start the thread. The reason we need to use a thread and we can’t just call the method from inside the Click event is because in that case our application would completely hang while downloading the file. It would become unusable and unresponsive, as if it crashed.

Finally, we have the code for the stop button:












 

To make this a real download manager, we’d have to add resume options, and a download list so that we give the user the option to download multiple files at once, or schedule them. This will be covered in a future tutorial.

Below is the entire source code of Form1.csthat you can also view in the Visual Studio 2005 project files attached to this tutorial.