FBA The Creator

The True Multiplatform PC / PocketPC Opensource Development Tool - Read Only Mode - Kept only for historical purposes - Thank you all ;)
It is currently Thu Jun 04, 2026 2:57 am

All times are UTC




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Jun 24, 2008 7:32 pm 
Offline
Tutorials Contributor
Tutorials Contributor
User avatar

Joined: Tue Jun 17, 2008 3:39 am
Posts: 59
Location: Midwest, US
Because keeping track of large amounts of "if mousein..." statements can be tedious and prone to error, I've been trying to find a way of getting the information I need without using them. To that end, I poked around the Lua Wiki and even though no one had what I was looking for, I figured out a way to do it using tables.

Let me preface the example by saying that, yes, keeping long tables of information can also be tedious and prone to error, but it does keep your functions clean and manageable and also allows for greater reuse of code. For instance, I can (and probably will from now on) use my "boundedIn" function without changing it for multiple projects.

Here is the example:
Code:
Look to the next post for the updated code.


There are a couple things to note, here. Because this is looping through the table to find a match, this probably isn't as efficient as "if mousein..." for huge amounts of things to check, but I don't know either way since I haven't done any benchmarking. I did allow for this to some extent by having the function instantly return when a match is found instead of it continuing the loop. But since Lua (and therefore presumably FBA) doesn't necessarily keep table items in the order they are added, it probably won't do too much good to order your tables by what will be clicked the most often (but it couldn't hurt).

The other thing to consider is also a factor with mousein, and should be treated in much the same way. If you have two area defined where one is inside of the other, you'll have to do two checks to find out which one is actually being clicked.

I hope this helps someone else.

_________________
Don't look at me. I'm new here.


Last edited by phrebh on Tue Jun 24, 2008 9:05 pm, edited 1 time in total.

Top
   
PostPosted: Tue Jun 24, 2008 9:04 pm 
Offline
Tutorials Contributor
Tutorials Contributor
User avatar

Joined: Tue Jun 17, 2008 3:39 am
Posts: 59
Location: Midwest, US
I realized that the boundedIn function wasn't generalized for the table variable, so here is an update:
Code:
table={
  ['Left']={
    ['bounds']={
      ['x1']=0,
      ['y1']=0,
      ['x2']=20,
      ['y2']=20
    }
  },
  ['Right']={
    ['bounds']={
      ['x1']=220,
      ['y1']=0,
      ['x2']=240,
      ['y2']=20
    }
  }
}
printIt=false

function boundedIn(x,y,t)
  for key,value in pairs(t) do
    if x>=value['bounds']['x1'] and x<=value['bounds']['x2'] and y>=value['bounds']['y1'] and y<=value['bounds']['y2'] then return key end
  end
  return false
end

function onmainloop()
  clear(0,0,0)
  fillrect(table['Left']['bounds']['x1'],table['Left']['bounds']['y1'],table['Left']['bounds']['x2'],table['Left']['bounds']['y2'],255,0,0)
  fillrect(table['Right']['bounds']['x1'],table['Right']['bounds']['y1'],table['Right']['bounds']['x2'],table['Right']['bounds']['y2'],0,0,255)
  if printIt~=false then print(100,100,printIt,255,255,0) end
end

function onstylusup(x,y)
  printIt=boundedIn(x,y,table)
end

_________________
Don't look at me. I'm new here.


Top
   
PostPosted: Thu Jul 10, 2008 7:41 am 
Offline
FBA Startup-Logo Creator
FBA Startup-Logo Creator
User avatar

Joined: Wed Jun 18, 2008 11:40 am
Posts: 100
Location: Brighton, UK
I do a similar thing when creating my "select level" buttons. There are say potentially 30 levels (mazes) but if they have only got to say the 3rd one then I only want to show 3 buttons...

Code:
   START_LEVEL = 3
   local x = 25
   local y = 65
   local width = 70
   local height = 20
   local col = 0
   local row = 0

   for i=1, START_LEVEL do
      level_btns[i] = {}
      level_btns[i]['x'] = (col * width) + x
      level_btns[i]['y'] = (row * height) + y
      col = col + 1
      if col > 2 then
         col = 0 
         row = row + 1
      end
   end


That bit of code seeds the button placement table. Then I check for the mouse event...

Code:
for i=1, START_LEVEL do
           
               if mousein(level_btns[i]['x'], level_btns[i]['y'], level_btns[i]['x'] + 48, level_btns[i]['y'] + 16) then
                  BLOCK_OFFSET = BLOCK_SIZE / 2
                  CURRENT_STATE = STATE_MENU_FADEOUT
                  GAME_STATE = GAME_STATE_LOAD               
                  level = i - 1
                  debug(i)
               end
            end


Hope that helps someone, oh and the place the buttons...

Code:
local x = 25
         local y = 65
         local width = 70
         local height = 20
         local rows = math.floor(START_LEVEL / 4) + 1
         local col = 0
         local row = 0
                 
         if BUILD_TYPE == "demo" then
            if START_LEVEL > MAX_LEVELS then             
               START_LEVEL = MAX_LEVELS
            end
         end
                 
         for i=1, START_LEVEL do
         
            drawimage(btn_small_blank, (col * width) + x, (row * height) + y)
            drawgfxtext(tahoma_font, (col * width) + x+20, (row * height) + y+3, i)
            col = col + 1
            if col > 2 then
               col = 0 
               row = row + 1
            else
               
            end
         end

_________________
http://www.boiledsweets.com


Top
   
PostPosted: Fri Jul 11, 2008 1:46 am 
Offline

Joined: Tue Apr 01, 2008 4:23 am
Posts: 36
Why not do something like a class: viewtopic.php?f=5&t=91

Although there is a lot of code defining the classes, but these will ultimately be static and reside in a different file and can be reused, and what ultimately goes in your main code will be greatly simplified.

For example if you have a lot of areas to check for, following my example Kbutton could represent an area to check. You could put several Kbuttons in a Kform, and several other Kbuttons in another Kform, and if you need to switch between the areas to check you could set the Kform to be visible/invisible, or even set visibility at the button level.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Limited