News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Pages (2): « First [1] 2 Next > Last »
Using callbacks to move objects on the screen
Author Message
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #1
Using callbacks to move objects on the screen

I am brand new to lua and new to fltk/fluid, so expect dumb questions from me. I find this stuff highly interesting and full of promises ;-). So, I decided to try and learn it to see what is possible. I would be interested in using murgaLua mostly to write mini educational games.

I can draw an interface, play it, modify and get some basic stuff done. However, I can't find the way to move an object on the screen dynamically, from within the lua script.

That's the onscreen object I want to move:

Code:
moving_point = fltk:Fl_Button(135, 35, 15, 15);
moving_point:color(70);


That's the dial I use to try and manipulate the x position of the object:

Code:
point_dial_x = fltk:Fl_Dial(26, 30, 25, 25);
point_dial_x:color(70);
point_dial_x:value(135)
point_dial_x:range(40, 200)
point_dial_x:step(1)

point_dial_x:callback(
  function(point_dial_x)
    -- fltk.fl_message(point_dial_x:x())  --> works, gives me the x value
    -- fltk.fl_message(point_dial_x:value()) --> works, gives me the value on the dial
    moving_point:set_x(point_dial_x:value());  --> causes a crash.
    moving_point:redraw();
end)


I tried: moving_point:x(int), moving_point:set_x(int), moving_point:move_x(int), none works. I can find mention of a o->position(x,y) construct on the fltk pages but don't know how to transfer this to murgaLua.

05-26-2007 03:45 AM
Visit this user's website Find all posts by this user Quote this message in a reply
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #2
RE: Using callbacks to move objects on the screen

Reading tobi's post that mentions a change from "->" to ":" that isn't applied, I gave a try to

Code:
green_left:position(green_left_x:value(),35);

Works... except that I know have multiple versions of the object on screen. Okay, changing

Code:
moving_point:redraw()  -- old
window:redraw(); -- new

does the trick nicely.

This post was last modified: 05-26-2007 04:08 AM by widged.

05-26-2007 04:07 AM
Visit this user's website Find all posts by this user Quote this message in a reply
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #3
RE: Using callbacks to move objects on the screen

Is it possible to have a drag and drop functionality? I can see an example at fltk cheat sheets - Draggable boxes

How to detect and react to these events from within murgaLua?

Code:
case FL_PUSH:
    offset[0] = x() - Fl::event_x();    // save where user clicked for dragging
    offset[1] = y() - Fl::event_y();
    return(1);
case FL_RELEASE:
    return(1);
case FL_DRAG:
    position(offset[0]+Fl::event_x(), offset[1]+Fl::event_y());     // handle dragging
    G_win->redraw();
    return(1);

This post was last modified: 05-26-2007 04:24 AM by widged.

05-26-2007 04:20 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #4
RE: Using callbacks to move objects on the screen

I'll look into this tonight ...

05-30-2007 06:13 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #5
RE: Using callbacks to move objects on the screen

OK, I'll have to think about how to implement the "handle" functionality.

Not as easy as I hoped ... I'll keep you posted.

Cheers
JohnM

05-30-2007 11:09 PM
Visit this user's website Find all posts by this user Quote this message in a reply
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #6
RE: Using callbacks to move objects on the screen

We discovered mikshaws "positionerTest.lua" in the murgaLua examples folder.
In just a few minutes Marielle modified it into this great drag and drop example.

Here's a movie preview
File maintained at the Widged murgaLua Gallery

This post was last modified: 06-03-2007 09:02 AM by iGame3D.

06-02-2007 03:30 AM
Visit this user's website Find all posts by this user Quote this message in a reply
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #7
RE: Using callbacks to move objects on the screen

Thanks Bill, that's spiffy stuff! Bill provided me with the code (from mikshaw, I assume), I only had to modify a few lines.

It can be useful to know that thing = the crosshair widget.

To see it, change 50 to another color in thing:selection_color(50).

It's a hacked drag and drop, so it doesn't really work like a standard drag and drop. What you need to do is first click on the object you want to drag. Then you have to click again on the object to move it around (to the user, it looks like they drag the object when what they do, in reality, is drag the center of the crosshair, with a callback that resets the position of the square).

Note that the maths are slightly inexact and don't take into account the left and right margins (causing the square to appear slightly offset when on the extreme left or right sides of the screen).

Feel free to suggest ways to improve the code. In particular, there shouldn't be the need for two square_cb functions... I don't have time to do better today.

This post was last modified: 06-02-2007 03:45 AM by widged.

06-02-2007 03:44 AM
Visit this user's website Find all posts by this user Quote this message in a reply
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #8
RE: Using callbacks to move objects on the screen

widged Wrote:
There shouldn't be the need for two square_cb functions...


Learning the basics of Lua.

Code:
square2:callback(square2_cb)


is in fact equivalent to

Code:
square2.callback(square2, square2_cb)


which means that no more than one square_cb function is needed:

Code:
function square_cb(self)
  square = self
  thing:show()
  w:redraw()
end

...

square2:callback(square_cb)

This post was last modified: 06-03-2007 04:27 AM by widged.

06-03-2007 03:33 AM
Visit this user's website Find all posts by this user Quote this message in a reply
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #9
RE: Using callbacks to move objects on the screen

which allows to rewrite the code to be more generic:

Code:
------------
ww=350 --window (width and height)
wh=400
fw = ww-20 -- frame
fh = ww-20
dw = fw-10 -- drag and drop area
dh = fw-10
sw = 10 -- square width
------------
square = {}
square_selected = {}

-- :Square object
-- Constucts a little square on the screen that can be drag and dropped within a given display area.
-- @param x the x position of the square
-- @param y the y position of the square
-- @param c the color of the square
function square_create(x,y,c)
  sq = fltk:Fl_Button(x,y,sw,sw);
  sq:box(fltk.FL_BORDER_BOX)
  sq:color(c);
  sq:callback(square_cb)
  return sq
end

-- Callbacks
function crosshair_cb()
  x = crosshair:xvalue()
  y = crosshair:yvalue()
  -- The crosshair area needs to be larger than the drag area, otherwise, a box that is on the far
  -- right or far bottom of the screen cannot be moved anymore.
  xpos=math.floor(x*dw*(dw/fw))
  ypos=math.floor(y*dh*(dh/fh))
  square_selected:position(xpos+15,ypos+15)
  out:value("X: ".. xpos .."  Y: ".. ypos)
  out2:value("X: ".. string.format("%.2f",x) .."  Y: ".. string.format("%.2f",y))
  crosshair:hide()
  -- out:value("X: "..crosshair:xvalue() * ww .."  Y: "..crosshair:yvalue() * wh)
  w:redraw()
end

function square_cb(self)
  square_selected = self
  crosshair:show()
  w:redraw()
end

-- GUI / Interface

w=fltk:Fl_Window(ww,wh,"murgaLua Drag and Drop Demo")

frame=fltk:Fl_Box(10,10,fw,fh);
frame:box(fltk.FL_BORDER_BOX)
frame:color(36);

display = fltk:Fl_Group(10,10,fw,fh)

  square[1] = square_create(15,15,80)
  square[2] = square_create(200,200,60)

  crosshair=fltk:Fl_Positioner(10,10,fw,fh);
  crosshair:box(fltk.FL_NO_BOX)
  crosshair:selection_color(36) -- same color as frame background
  -- crosshair:selection_color(72)
  crosshair:callback(crosshair_cb)
fltk:Fl_End()

-- outputs, display the x and y values during drag and drop
out=fltk:Fl_Output(5,wh-30,ww/2-10,25)
out2=fltk:Fl_Output(ww/2+5,wh-30,ww/2-10,25)

-- required as button 1 is active by default and clicking anywhere on the screen
-- other than one of the two buttons crashes the app.
square_selected = square[1]

w:show()
Fl:run()

This post was last modified: 06-03-2007 04:35 AM by widged.

06-03-2007 04:26 AM
Visit this user's website Find all posts by this user Quote this message in a reply
widged
Junior Member
**


Posts: 35
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #10
RE: Using callbacks to move objects on the screen

JohnMurga Wrote:
OK, I'll have to think about how to implement the "handle" functionality.


A quick hack, this will react to the button being pressed down as well as up. These fltk constants don't seem to be understood by murgalua, so the number between brackets need to be used.

Code:
but = fltk:Fl_Button(10,wh-30,80,22,"a button");
but:when(1);
--[[
    FL_WHEN_NEVER             {0;}
    FL_WHEN_CHANGED           {1;}
    FL_WHEN_RELEASE           {4;}
    FL_WHEN_RELEASE_ALWAYS    {6;}
    FL_WHEN_ENTER_KEY         {8;}
    FL_WHEN_ENTER_KEY_ALWAYS  {10;}
    FL_WHEN_ENTER_KEY_CHANGED {11;}
    FL_WHEN_NOT_CHANGED       {2;}
]]
but:callback(function(self)
  fltk.fl_message("changed")
end)

06-05-2007 11:42 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Pages (2): « First [1] 2 Next > Last »
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: