Page 1 of 1

CELX Mouse buttons/position. + Showing pictures

Posted: 16.11.2010, 09:47
by roy-t
I've been reading about CELX scripts a lot the last few days. And I've tested almost all the functionality that I would like in my own script file. However there are still 2 things that elude me, and that I think are possible.

1.

There is a nice callback/event-like mechanism for keyboard input, but how about the mouse buttons? And how do I get the mouse cursor position? All queries involving mouse have turned up empty so far.

2.

I think I saw a few scripts display pictures on screen, however I couldn't find any documentation on this. How would I display a .png file (for example). And is it also possible to show movies?

I hope someone can point me too the right direction. I've also got the Celestia source code, so if somebody has some tips that can only be done using the source code, feel free to post. I do prefer CELX solutions, because they are slightly easier to manage (and way easier to create, without further studying the source code).

Sincerely,

Roy T.

Edit:

Ok I've got the mouse buttons figured out (strange how this approach is similar but not analogue to keyboard input, that's confusing)

Code: Select all

-- This allows me to detect e "button pressed event" , fires only once, not as long as the button is pressed
--Mouse input
md = false
function mousedown(m)
   if(md ~= true) then
      md = true
      celestia:print("Mouse pressed", 8, 0, 0, -20, 0)      
   end
end

function mouseup(m)
   md = false
end

--register mouse input
celestia:registereventhandler("mousedown", mousedown)
celestia:registereventhandler("mouseup", mouseup)


However I'm still not sure what "m" (n both methods) is for kind of object in this scenario. I've found this page: http://en.wikibooks.org/wiki/Celestia/C ... enthandler however there is no information on the object given there.

More googling led me to http://www.lns.cornell.edu/~seb/celesti ... notes.html but that simply states "There are also mouseup, mousedown, and tick events. I'll post more information about them soon." So no joy there either.

I did find http://celestia.sourcearchive.com/docum ... ource.html If you take a look at the "bool LuaState::handleMouseButtonEvent(float x, float y, int button, bool down)" method, then so far as I can see "m" is comprised of 3 values.

a field "button" with an integer for the mouse
And fields X and Y.

After some more experiments it seems that this is the mapping:
"button == 1" -> left mouse button
"button == 2" -> scroll wheel clicked
"button ==4" -> right mouse button
(note all measured on a simple mouse that has no more buttons than that).

m.x and m.y indeed give coordinates. where 0,0 is the top left, and screenresolution.x and y are the bottom right.

I posted all this info here, in the hope that someone doesn't have to look for it as long as I have (trough trial and error and C++ source code).

For me the question how to properly show a picture is still open. :)

Re: CELX Mouse buttons/position. + Showing pictures

Posted: 17.11.2010, 01:37
by Fenerit
Hallo! First, thanks to share the mouse's informations. Second, I've read also your post in the Development forum: from there seems that you have tried to install the luatools 1.2 (you say 1.6) without success. Because you are referring to Celestia 1.6, is unclear whether you use the actual 1.6 version or the 1.61 (SVN). Such fact is important because the LuaTools 1.2 beta 8 works only on 1.61.
- You do not get the pictures, but: is the toolkit displayed (the luatools' panel box)?
- Is the "celestia.cfg" properly set with the insertion of the string: LuaHook "luahookinit.lua"?
- Are the files (whether on Windows) "lua5.1.dll" and "libpng1.dll" inside the main Celestia folder?
- Are you running Celestia under Windows 7 within "\programs" dir?

Re: CELX Mouse buttons/position. + Showing pictures

Posted: 17.11.2010, 09:28
by Vincent
Roy,

The lua script below can be considered as the simpliest
way to display an image within celestia so far.
It doesn't require any lua plugin. It can be used with
Celestia version 5.1 or higher.

Code: Select all

-- Display an image using the Lua Hook process

local beginScreenDraw =
    function()
        gl.MatrixMode(gl.PROJECTION);
        gl.PushMatrix();
        gl.LoadIdentity();
        sx,sy = celestia:getscreendimension();
        gl.Ortho(0, sx, 0, sy, 0, 1);
        gl.MatrixMode(gl.MODELVIEW);
        gl.PushMatrix();
        gl.LoadIdentity();
        gl.Disable(gl.TEXTURE_2D);
        gl.Disable(gl.LIGHTING);
        gl.Enable(gl.TEXTURE_2D);
        gl.Enable(gl.BLEND);
        gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    end;

local endScreenDraw =
    function()
        gl.MatrixMode(gl.PROJECTION);
        gl.PopMatrix();
        gl.MatrixMode(gl.MODELVIEW);
        gl.PopMatrix();
    end;

local drawtexture =
    function(x, y, w, h)
        gl.Enable(gl.TEXTURE_2D);
        gl.Begin(gl.QUADS);
        gl.Color(1, 1, 1, 1);
        gl.TexCoord(0.0, 1.0);
        gl.Vertex(x, y);
        gl.TexCoord(1.0, 1.0);
        gl.Vertex(x + w, y);
        gl.TexCoord(1.0, 0.0);
        gl.Vertex(x + w, y + h);
        gl.TexCoord(0.0, 0.0);
        gl.Vertex(x, y + h);
        gl.End();
        gl.Disable(gl.TEXTURE_2D);
    end;

local drawImage =
    function(im, x, y)
        local iw = im:getwidth();
        local ih = im:getheight();
        im:bind();
        drawtexture(x, y, iw, ih);
    end;

---------------------------------------------
local image_file = "image.png";
local xImage, yImage = 100, 100;
---------------------------------------------

lua_image =
{
    renderoverlay =
        function(self)
            if not image then
                image = celestia:loadtexture(image_file);
            end
            beginScreenDraw();
            drawImage(image, xImage, yImage);
            endScreenDraw();
        end;
};


Since it uses the Lua Hook process, just add:
- a luahookinit.lua file that looks like the following into the celestia base dir:

Code: Select all

-- luahookinit.lua
require "lua_image";
celestia:setluahook(lua_image);

- The following line to the celestia.cfg file:

Code: Select all

Configuration
{

LuaHook "luahookinit.lua" # <-- Line to add


Here's a test package:

Re: CELX Mouse buttons/position. + Showing pictures

Posted: 17.11.2010, 10:36
by Vincent
... And the following one displays the mouse cursor postion on screen.

Code: Select all

-- Display mouse cursor position on screen

local xpos, ypos;

local beginScreenDraw =
    function()
        gl.MatrixMode(gl.PROJECTION);
        gl.PushMatrix();
        gl.LoadIdentity();
        sx,sy = celestia:getscreendimension();
        gl.Ortho(0, sx, 0, sy, 0, 1);
        gl.MatrixMode(gl.MODELVIEW);
        gl.PushMatrix();
        gl.LoadIdentity();
        gl.Disable(gl.TEXTURE_2D);
        gl.Disable(gl.LIGHTING);
        gl.Enable(gl.TEXTURE_2D);
        gl.Enable(gl.BLEND);
        gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    end;

local endScreenDraw =
    function()
        gl.MatrixMode(gl.PROJECTION);
        gl.PopMatrix();
        gl.MatrixMode(gl.MODELVIEW);
        gl.PopMatrix();
    end;


local displayOverlayText =
    function(str, x, y)
        gl.Color(1.0, 0.0, 0.0, 0.7);
        gl.Enable(gl.TEXTURE_2D);
        gl.Enable(gl.BLEND);
        gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
        gl.MatrixMode(gl.PROJECTION);
        gl.PushMatrix();
        gl.LoadIdentity();
        local sx,sy = celestia:getscreendimension();
        gl.Ortho(0, sx+0, 0, sy+0, 0, 1);
        gl.MatrixMode(gl.MODELVIEW);
        gl.PushMatrix();
        gl.LoadIdentity();
        gl.Translate(x + 0.125, y + 0.125);
        local font = celestia:getfont();
        font:bind();
        font:render(str);
        gl.PopMatrix();
        gl.MatrixMode(gl.PROJECTION);
        gl.PopMatrix();
        gl.MatrixMode(gl.MODELVIEW);
    end

lua_cursorpos =
{
    renderoverlay =
        function(self)
            beginScreenDraw();
            displayOverlayText("["..xpos..', '..ypos.."]", xpos + 10, ypos - 20);
            endScreenDraw();
        end;
       
    mousemove =
        function(self,x,y)
            local sx,sy = celestia:getscreendimension();
            xpos, ypos = x, sy - y;
        end;
}


- luahookinit.lua file:

Code: Select all

-- luahookinit.lua
require "lua_cursorpos";
celestia:setluahook(lua_cursorpos);

Re: CELX Mouse buttons/position. + Showing pictures

Posted: 22.11.2010, 08:32
by roy-t
Thanks a lot Vincent and Fenerit,

Sorry for the late reply ( I can only work on this one day in the week right now, I'm not 'on-site' other days to check these forums).

I think with Vincent's code I will be able to get this working, which would be the last critical prototype that I have to complete before we can make the descision between self-built and Celestia.

Edit Fenerit, to answer your questions
-I'm running Celestia's latest SVN version
-I do see the Lua-tools toolbox
-I'm not running from \programs