Image rotate

From Sim Innovations Wiki
Jump to navigation Jump to search

Creating an instrument with rotating images (steam gauge)

Now we will start to do something with the data that is coming from the simulator. If you have not read the previous lessons, then please do so to fully understand this lesson. You might have already downloaded the images set that is being used in this lesson, if not then you can download it here.

This page is part of the Instrument Creation Tutorial.

Adding the images to the code

Open Air Manager, go to the Create/Edit tab and create a new instrument which has a width and height of 500 pixels, or select the empty instrument you've already made. See lesson one on how to create a new instrument. Don't forget to add the images you have downloaded into the resources folder. Once you have created and/or selected the new instrument, open the logic script by pressing the Logic Script button. This will open your code editor. If you have not already set a default code editor for Lua files, then first do so in your operating system.

You should see your code editor and no code. As you have learned in the images lesson, the images are displayed in a Z-order. That means that the image you add first, comes in the back, the next one comes on top of that, etc... So we will need to add the background first, and this image is conveniently named background.png. To add the images, we use the img_add or img_add_fullscreen function. In this case we have created an instrument which is 500 x 500 pixels, and our background image is also 500 x 500 pixels, so we can use the img_add_fullscreen function. Lets add the image:

-- Comments to your code can be added by using the minus sign two times
-- Add images --
img_add_fullscreen("background.png")

That's all to add your first image. Save your code, go to Air Manager and press Run, you will see the airspeed indicator background. If all you see is a white square, then you've probably forgotten to add the images to the resources folder or you might have misspelled the image name. Next is the airspeed indicator needle, which will sit on top of the background. We will add that to the code you already had. But hang on, the needle needs to rotate, and Air Manager needs to know which images you want to rotate, so we will have to gives this image a name. We will name it img_needle. We can also use img_add_fullscreen because this image is also 500 x 500 pixels.

-- Comments to your code can be added by using the minus sign two times
-- Add images --
img_add_fullscreen("background.png")
img_needle = img_add_fullscreen("needle.png")

If you press Run again, as you did before, you will see the needle on top of the background image. Now we are going to add a data source, which is called a dataref for X-Plane and a variable for FSX and P3D. In the previous lesson you saw how these data sources work, and how you need to subscribe to the data. In this example I will use both X-Plane data and FSX/P3D data. I will not go into detail on this in this lesson, because this was all explained in the previous lesson. We'll add the data subscribe function to the code.

-- Comments to your code can be added by using the minus sign two times
-- Add images --
img_add_fullscreen("background.png")
img_needle = img_add_fullscreen("needle.png")

-- Data subscribe --
xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/airspeed_kts_pilot", "FLOAT", callback_speed)
fsx_variable_subscribe("AIRSPEED INDICATED", "Knots" , callback_speed)

When airspeed data comes from the simulator, this subscribe function will 'call' the function which will add now and say: Hey you! callback_speed, I have new data for you, do with it what you want The callback_speed function receives this data and does something with it, this something is whatever you ask it to do. We're going to rotate an image, so we will let callback_speed handover this airspeed data to a rotate function. Whenever you are sending simulator data from a subscribe function to a callback function, you put function in front of this callback function, then the callback function name, and after that between brackets you add a so called variable. The data for this callback function will be placed in this variable, and this variable is what you'll use later on in your code. This all might sound confusing, but just have a look at the code and you'll understand. First we will setup the callback function:

-- Comments to your code can be added by using the minus sign two times
-- Add images --
img_add_fullscreen("background.png")
img_needle = img_add_fullscreen("needle.png")

-- Functions --
function callback_speed(speed)

end

-- Data subscribe --
xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/airspeed_kts_pilot", "FLOAT", callback_speed)
fsx_variable_subscribe("AIRSPEED INDICATED", "Knots" , callback_speed)

Now we have the callback function, we have the variable speed which within this callback function contains the airspeed in knots. The function is still empty, we will need to add some code to it. Our airspeed dial goes from 0 to 140 knots. But to tell you a secret, the indicator lines go from 0 to 300 degrees. That means that every knot is ~2.14 degrees (300 / 140), and that for every knot the needle has to rotate 2.14 degrees. To let it rotate, we need to multiply this 2.14 with the current airspeed. We will add this calculation to a rotate function. In the beginning of this lesson we have given a name to the needle (img_needle), this is the name we are going to use to let Air Manager know it needs to rotate the needle.

-- Comments to your code can be added by using the minus sign two times
-- Add images --
img_add_fullscreen("background.png")
img_needle = img_add_fullscreen("needle.png")

-- Functions --
function callback_speed(speed)

    rotate(img_needle, (300 / 140) * speed)

end

-- Data subscribe --
xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/airspeed_kts_pilot", "FLOAT", callback_speed)
fsx_variable_subscribe("AIRSPEED INDICATED", "Knots" , callback_speed)

If you run your instrument, fill in a random airspeed and press send, you will see the needle move, and hopefully to the correct value. Notice that if you send a value that is over 140 knots, or under 0 knots, the needle will rotate out of its scale. We need to prevent the value from becoming more than 140 and less than 0. This is very easy with the var_cap function. With this function you set the minimum value, and the maximum value for this variable. So if the data from the simulator becomes more than 140, or less then 0, the variable will never go further than the limits you have set. Be sure to add this function before the rotate function, because Air Manager reads the script from the top first, all the way to the bottom. If you add the function beneath the rotate function, your needle will still go to far. Let's do it:

-- Comments to your code can be added by using the minus sign two times
-- Add images --
img_add_fullscreen("background.png")
img_needle = img_add_fullscreen("needle.png")

-- Functions --
function callback_speed(speed)

    speed = var_cap(speed, 0, 140)
    rotate(img_needle, (300 / 140) * speed)

end

-- Data subscribe --
xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/airspeed_kts_pilot", "FLOAT", callback_speed)
fsx_variable_subscribe("AIRSPEED INDICATED", "Knots" , callback_speed)

If you run your instrument now and send a value that is more than 140 or less than 0, you will notice that it will stay at 140 or 0.

Summary

That's all there's to it to create a rotating image. Problems that you'll run into when recreating for example a 'real life' airspeed indicator is that the scale is probably not linear, in the next lesson I will learn you how to solve this. After that we are going to add text to the instrument.


This page is part of the Instrument Creation Tutorial.