简体   繁体   中英

How to move an image in Lua?

I am new to Lua programming and I am having problems while trying to move an image from a set of coordinates to another.

What I am trying to create is to be used with the X-Plane flight simulator. There is a library called SASL (www.1-sim.com), that was created to make plugins (for X-Plane) creation easir, since the default language is C++ and many people find it difficult.

In general, SASL works as a bridge between Lua and X-Plane, in general lines, the scripts you write reads some data straight from X-Plane (DataRefs) while it is running and depending on the code you wrote its execute commands or many other things that it is possible.

So, when using SASL to create cockpit/panel gauges it uses a base file, named 'avionics.lua' that works as a class and loads all gauges you create for the specific aircraft you are working on. For example my avionics.lua file looks like this:

size = { 2048, 2048 }

components = {
flaps {};
};

where, 'size' is the size that will be used for things to be drawn and components is an array of gauges, in this case the flaps gauge.

The rest of the work is to create the gauge functionality and this is done in a separate file, in my case, called 'flaps.lua'.

Within flaps.lua, is where I need to code the flaps indicator functionality which is to load 2 images: one for the back ground and the second one for the flaps indicator.

The first image is a fixed image. The second one will move throught the 'y' axis based on the flaps indicator DataRef (flapsDegree property below).

The code below when X-Plane is running displays the background image and the flaps indicator on its first stage which is 0 as you can see on the image.

size = {78,100}

local flapsDegree = globalPropertyf("sim/cockpit2/controls/flap_ratio")
local background = loadImage("gfx/Flaps.png")
local indicator = loadImage("gfx/Flaps_Indicator.png")
local flaps = get(flapsPosition)

components = {
texture { position = {945, 1011, 60, 100}, image = background},
texture { position = {959, 1097, 30, 9},   image = indicator},
}

Image 在此处输入图片说明

Now, the problem comes when I need to implement the logic for moving the 'indicator' image through the 'y' axis.

I have tried this code without success:

if flaps == 0.333 then
indicator.position = {959, 1075, 30, 9}
end

So how could I accomplish that?

I am not familiar with the SALS library. I just had a quick look into it.

Everything you need to know is in the manuals on the website you linked. In particular http://www.1-sim.com/files/SASL300.pdf

Everytime your screen is updated each components draw() function will be called.

So if you want to change something dynamically you have to put that into the component's draw function.

If you open the SALS sources you'll find basic components which show you how to use that stuff. One of them is needle.lua:

-- default angle
defineProperty("angle", 0)

-- no image
defineProperty("image")


function draw(self)
    local w, h = getTextureSize(get(image))

    local max = w
    if h > max then
        max = h
    end

    local rw = (w / max) * 100
    local rh = (h / max) * 100
    drawRotatedTexture(get(image), get(angle), 
        (100 - rw) / 2, (100 - rh) / 2, rw, rh)
end

If you check the manual you'll find that there is not only a drawRotatedTexture function but many other functions for drawing stuff. Just play around. Try drawTexture for your purpose.

If you don't know what you have to program, open every single lua file in the library and read it together with the Lua reference manual and the SALS documentation until you understand what is going on. Once you understand the existing code you can extend it with ease.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM