Version 2.1 of Fancy Beetle

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Version 2.1 of Fancy Beetle

Post by alberto_lara »

Fancy Beetle it's a cute and small debugger based on the original Bettle: :cool:

Code: Select all

--[[
Fancy fancy 2.1 based on:
Beetle Debug Library V 0.0.2
License : Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
http://creativecommons.org/licenses/by-nc-sa/3.0/
Programmed by substitute541.
]]--

fancy = {}
fancy.__index=fancy
local dbg_initialized=false

function fancy:new(font)
   local b = {}
   b.y = 15
   b.x = 15
   b.names = {}
   b.value = {}
   b.colors={}-- For the level of priority on the tag.
   b.font = font or love.graphics.newFont(12)
   b.font_color={255,255,255}-- White for the font.
   b.fh=b.font:getHeight()
   b.show = true
   b.key = "d"
   b.title = "    Fancy Bettle    "
   b.w_rec  = b.font:getWidth(b.title)-- Width of the window.
   b.pressed = false
   b.minimized = false
   b.COLOR_TAGS={{255,0,0,192},{200,200,0,192},{0,192,63,192}}
   b.rb=b.fh*.65
   b.lw=b.rb/3-- Line width of the '+' '-'.
   b.rdbs=b.fh*0.5-- Radio of dragging button size.
   b.order="n"
   dbg_initialized=true
   return setmetatable(b,fancy)
end

function fancy:key(key)
   if key == self.key and self.show then
      self:hide()
   elseif key == self.key and self.show == false then
      self:show()
   end
end

function fancy:setKey(key)
   self.key = key
end

function fancy:show()
   self.show = true
end

function fancy:hide()
   self.show = false
end

function fancy:getState()
   return self.show
end

function fancy:update(position, name, value, prior)
   if self.value[position]==nil then
      table.insert(self.names, tostring(name) .. ": ")
      table.insert(self.value, tostring(contents))
      if prior then
         table.insert(self.colors, self.COLOR_TAGS[prior])
      end
   else
      self.value[position] = tostring(value)
      self.colors[position]= self.COLOR_TAGS[prior]
   end
end

function fancy:draw()
   love.graphics.setLineWidth(self.fh/15)
   if self.show then
      if self.minimized then
         -- Check if click/touch to restore:
         local rb,x_btn,y_btn=self.rb,self.rb,love.graphics.getHeight()-self.rb
         if love.mouse.isDown("l") and self:overButton(x_btn,y_btn,rb) then
            self.minimized=not self.minimized
         end
         -- Maximize button:
         love.graphics.setColor(255,255,255)
         love.graphics.circle("fill",x_btn,y_btn,rb,50)
         love.graphics.circle("line",x_btn,y_btn,rb,50)
         love.graphics.setColor(0,0,0)
         love.graphics.setLineWidth(self.lw)
         love.graphics.line(
            rb-rb*.75,
            love.graphics.getHeight()-rb,
            rb+rb*.75,
            love.graphics.getHeight()-rb)
         love.graphics.line(
            rb,
            love.graphics.getHeight()-rb-rb*.75,
            rb,
            love.graphics.getHeight()-rb+rb*.75)
      else-- Not minimized:
         -- Check if pressed minimize button:
         local x_btn,y_btn=self.x+self.w_rec,self.y
         if love.mouse.isDown("l") and self:overButton(x_btn,y_btn,self.rb) then
            self.minimized=not self.minimized
         else
            -- Check if pressed over window:
            if love.mouse.isDown("l") then
               if self:overIt() then
                  self.pressed=true
               end
            end
            if not love.mouse.isDown("l") then self.pressed=false end
            -- Update position:
            if self.pressed then
               local tch=love.touch-- on android:
               if tch then
                  local id,x,y=love.touch.getTouch(1)
                  self.x,self.y=
                  x*love.graphics.getWidth()-self.w_rec/2,
                  y*love.graphics.getHeight()-(#self.names+1)*self.fh/2
               else-- on the PC:
                  self.x,self.y=
                     love.mouse.getX()-self.w_rec/2,
                     love.mouse.getY()-(#self.names+1)*self.fh/2
               end
            end
         end
         love.graphics.setFont(self.font)
         -- Update w_rec:
         for i=1,#self.names do
            if self.font:getWidth(self.names[i]..self.value[i])>self.w_rec then
               self.w_rec=self.font:getWidth(self.names[i]..self.value[i].."  ")
            end
         end
         love.graphics.setColor(self.COLOR_TAGS[3])
         love.graphics.rectangle("fill",self.x,self.y,self.w_rec,self.fh)
         love.graphics.setColor(self.font_color)
         love.graphics.print(self.title, self.x+self.w_rec/2-self.font:getWidth(self.title)/2, self.y)
         for i, v in ipairs(self.names) do
            love.graphics.setColor(self.COLOR_TAGS[3])
            if self.colors[i]~=nil then
               love.graphics.setColor(self.colors[i])
            end
            love.graphics.setColor(self.COLOR_TAGS[3])
            if self.colors[i]~=nil then
               love.graphics.setColor(self.colors[i])
            end
            love.graphics.rectangle("fill",self.x,self.y+i*self.fh,self.w_rec,self.fh)
            love.graphics.setColor(self.font_color)
            love.graphics.print(" "..v .. self.value[i], self.x, self.y+self.fh*i)
         end
         -- Minimize button:
         love.graphics.circle("fill",self.x+self.w_rec,self.y,self.rb,50)
         love.graphics.circle("line",self.x+self.w_rec,self.y,self.rb,50)
         love.graphics.setColor(0,0,0)
         love.graphics.setLineWidth(self.lw)
         love.graphics.line(self.x+self.w_rec-self.rb*.75,
            self.y,
            self.x+self.w_rec+self.rb*.75,
            self.y)
      end
   end
   love.graphics.setColor(255, 255, 255, 255)
   --love.graphics.setLineStyle("smooth")
end
function fancy:overIt(...)
   local t={...}
   local x,y=self:getCoords()
   if #t==0 then
      return not
      (
         x<self.x or
         x>self.x+self.w_rec or
         y<self.y or
         y>self.y+(#self.names+1)*self.fh
      )
   else
      return not
      (
         x<t[1] or
         x>t[1]+t[3] or
         y<t[2] or
         y>t[2]+t[4]
      )  
   end
end
function fancy:overButton(x_btn,y_btn,rb)
   local x,y=self:getCoords()
   local hip=math.sqrt(math.pow(x-x_btn,2)+math.pow(y-y_btn,2))
   return hip<rb
end
function fancy:getCoords()
   local id,x,y=0,0,0
   local tch=love.touch
   if tch and tch.getTouchCount()>0 then
      id,x,y=tch.getTouch(1)
      x=x*love.graphics.getWidth()
      y=y*love.graphics.getHeight()
   else
      x,y=love.mouse.getX(),love.mouse.getY()
   end
   return x,y
end
The changes/upgrades are this ones:


1.- We have an elegant and cute window.
-----------------------------------------------------------------------------------------------------------------
2.- The debugger it's ON by default.
-----------------------------------------------------------------------------------------------------------------
3.- We have a minimize/maximize button.
-----------------------------------------------------------------------------------------------------------------
4.- Functions beetle.add() and beetle.remove(), removed. Instead of doing this:

Code: Select all

function love.load()
	beetle.load()
	beetle.add("tag1",value1)
	beetle.add("tag2",value2)
	beetle.add("tag3",value3)
	beetle.add("tag4",value4)
end
function love.update(dt)
	beetle.update(1,value1)
	beetle.update(2,value2)
	beetle.update(3,value3)
	beetle.update(4,value4)
end
function love.draw()
end
You will only use this:

Code: Select all

function love.load()
   dbgr=fancy:new()
end
function love.update(dt)
   dbgr:update(1,"tag1",value1)
   dbgr:update(2,"tag2",value2)
   dbgr:update(3,"tag3",value3)
   dbgr:update(4,"tag4",value4)
end
function love.draw()
end
-----------------------------------------------------------------------------------------------------------------
5.- You can also specify a 4th parameter, the priority of the 'tag' (by default it's normal):
GREEN = normal (number 3)
YELLOW = important (number 2)
RED = critical (number 1)

E. g. calling this (example of my code):

Code: Select all

dbgr:update(1,"FPS",love.timer.getFPS(),3)
dbgr:update(2,"# of candies",#candies,2)
dbgr:update(3,"Candy delay",string.sub(delay_candy,0,5),1)
dbgr:update(4,"abs_w",string.sub(abs_w,1,5))
dbgr:update(5,"abs_h",string.sub(abs_h,1,5))
you'll get this:
Image
-----------------------------------------------------------------------------------------------------------------
6.- We can call beetle.load() or beetle.load(font) with whatever font you want.
-----------------------------------------------------------------------------------------------------------------
7.- We can move the window around.
-----------------------------------------------------------------------------------------------------------------
8.- It works on touchscreens too :) (http://love2d.org/forums/viewtopic.php?f=11&t=76979)
-----------------------------------------------------------------------------------------------------------------

Screenshots:

Fancy Beetle showed up with all normal tags:
Image

Minimized:
Image

Thanks for your attention, not tested at 100%, can have bugs (but the basic works well). :)
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests