android
  #1  
Old 12-30-2010, 06:11 PM
WooDChecker's Avatar
WooDChecker WooDChecker is offline
Junior Member
 
Join Date: Dec 2010
Location: Germany
Posts: 35
Default How to make a Button in LUA?

Hello people.
First ive to say my english is not the best (Im from Germany).
Hope you understand me. About one week ago I bought a ZEN X-fi 2 and Im really interested in programming apps for my ZEN. So I found this Website and downloaded some apps. Very cool thing btw.
Yesterday I looked at the code from: LINK. Now my problem is I dont know how to add a another button to it. I want to add two buttons they control the volume of my app. The functions are finished:

Code:
function volPlus()
    local volOld = audio.volume()
    if(volOld < 25) then
        audio.volume(volOld + 1)
    end
end


function volMinus()
    local volOld = audio.volume()
    if(volOld > 0) then
        audio.volume(volOld - 1)
    end
end
But how can I add these two functions to my two buttons??
I hope you guys can help me with that.

-sincerely Wood
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 12-30-2010, 06:43 PM
jbob182's Avatar
jbob182 jbob182 is offline
X-Fi2 Development Moderator
 
Join Date: Feb 2010
Location: The U.S.A.
Posts: 554
Default

if i am right you want to know how to map another button to that function?

well here is a simple button layout



Code:
function withinRegion(x, y, rx, ry, w, h) 
    if x >= rx and x <= (rx + w) and y >= ry and y <= (ry + h) then
        return true
    else
        return false
    end;
end
Then to use this function do:

Code:
       
 if withinRegion(x, y, 85, 214, 66, 21) then  ---this is where you fill in the coordinates of your button
      --[[this is where you put what the actions are (not in the 

     --]]



  end
is this want you wanted?
Reply With Quote

  #3  
Old 12-30-2010, 07:23 PM
WooDChecker's Avatar
WooDChecker WooDChecker is offline
Junior Member
 
Join Date: Dec 2010
Location: Germany
Posts: 35
Default

Im not sure^^

Like that?
Code:
 if withinRegion(x, y, 85, 214, 66, 21) then
        clickXY3()
  end
..
x (position x)
y (position y)
85 (What is that?)
214 (?)

66 (width)
21 (height)
..

Here is a picture to understand better what I want:
http://img88.imageshack.us/i/thatiswhatiwant.jpg/
Reply With Quote

  #4  
Old 12-30-2010, 09:10 PM
jbob182's Avatar
jbob182 jbob182 is offline
X-Fi2 Development Moderator
 
Join Date: Feb 2010
Location: The U.S.A.
Posts: 554
Default

ok. as for variables here they are

x (telling the function what variable to use for x)
y (telling the function what variable to use for y)
85 (starting point on x coordinate)
214 (starting point on y coordinate)
66 (width)
21 (height)


as for the code it would look something like like

Code:
volint = audio.volume()
function vol()
       if(vol < 0) then
    volint=0
  elseif vol > 25 then
    volint=25
  end


end

if withinRegion(x, y, 85, 214, 66, 21) then
            -- plus button
     volint=volint+1
elseif withinRegion(x, y, 85, 214, 66, 21) then
   --minus button
     volint=volint-1
 end
Reply With Quote

  #5  
Old 12-30-2010, 09:34 PM
WooDChecker's Avatar
WooDChecker WooDChecker is offline
Junior Member
 
Join Date: Dec 2010
Location: Germany
Posts: 35
Default

Thank you so much! That is what I wanted.
Now I can continue my project.

Problem solved.
Reply With Quote

  #6  
Old 01-10-2011, 06:22 PM
WooDChecker's Avatar
WooDChecker WooDChecker is offline
Junior Member
 
Join Date: Dec 2010
Location: Germany
Posts: 35
Default Another question

Now that I can add some buttons to my app, I need to know how to add a rollover image to my button.
Can you help me?

This wont work:
Code:
if touch_x >= 0 and touch_x <= (0 + 90) and touch_y >= 0  and touch_y <= (0 + 90) then
        pad1_h:draw(0,0,106,106) --hover btn
        screen.update()
        else
                pad1_b:draw(0,0,106,106)
                screen.update()
end;
Reply With Quote

  #7  
Old 01-13-2011, 04:22 PM
jbob182's Avatar
jbob182 jbob182 is offline
X-Fi2 Development Moderator
 
Join Date: Feb 2010
Location: The U.S.A.
Posts: 554
Default

Your problem is simple, you redraw the image but only when they click the screen, you need to add a timer in there and delay how long that image is displayed

ie:

Code:
timer=0
if touch_x >= 0 and touch_x <= (0 + 90) and touch_y >= 0  and touch_y <= (0 + 90) then
        timer=40
        active=true         
end

while true do
  if active then
     pad1_h:draw(0,0,106,106) --hover btn     
     timer=timer-1
  else
     pad1_b:draw(0,0,106,106)
 end
  if timer <= 0 then
    active=false
  end
screen.update()
end
Reply With Quote

  #8  
Old 01-14-2011, 12:31 AM
WooDChecker's Avatar
WooDChecker WooDChecker is offline
Junior Member
 
Join Date: Dec 2010
Location: Germany
Posts: 35
Default

Exactly what I wanted. Thanks a lot!
by the way jbob182, you are in the credits of my app : )
__________________
Tell me if you need an unique design for your app ; )
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 11:25 PM.