StatusBar - FPS,Clock and Memory -Auto Installs

Simple Status Bar - https://gist.github.com/briarfox/5578242

I thought I’d share this. It was inspired from @Jvm38 and his Window library. Most of the code is his, I just edited it for my needs. This places a simple bar that can be dragged around the screen. It shows basic info that I like to see when I’m working on a project. The last location is saved and it can be toggled on and off.

I would also like feedback on the installer. Simply copy into main, set the name of the project you created and run it twice. All the code will be there. You can delete the json file when done, I haven’t added to code to remove it yet.

Code if you prefer not to copy off gist


 
--StatusBar Single Install
--Installer created by @acp edited by @Briarfox
--- This will pull the StatusBar project into Codea for you
-- Instructions:
-- * Create a new project in Codea named StatusBar If you chose another name please change the variable Below
--This is case sensitive
ProjectName = "StatusBar"
-- * Paste this into the Main (not from the raw view, as iSafari will escape special characters)
-- * Run, run again, 
-- If all went well, you should have a StatusBar project now
 
 
 
function getJsonLib()
    local tabs = listProjectTabs()
    if #tabs == 1 then
        print("Pulling JSON library into project...")
        local handleSuccess = function(data)
            saveProjectTab("json", data)
            sound(SOUND_POWERUP, 42179)
            print("Success! Please relaunch this project")
        end
        http.request("https://dl.dropboxusercontent.com/s/9e4nvqeu4hsux2q/Json.lua?token_hash=AAFyMB98j4bnt_1gawf9wSke52hsoC7hsIvARcTuZNeOEw&dl=1", handleSuccess)
        return false
    else
        return true
    end
end
 
function setup()
    local handleSuccess = function(data,i,j)
        if listProjectTabs(ProjectName) == nil then
            sound(SOUND_EXPLODE, 32351)
            error("AutoGist project must exist first")
        end
        local gist = json.decode(data)
        local projName = ProjectName
        for k,v in pairs(gist.files) do
            local name = projName .. ":" .. string.gsub(k,".lua","")
            saveProjectTab(name, v.content)
        end
        sound(SOUND_PICKUP, 11797)
        print("Success!")
    end
    local handleFailure = function(data)
        sound(SOUND_EXPLODE, 32351)
        print(data)
    end
    if getJsonLib() then
    http.request("https://api.github.com/gists/5578103",handleSuccess, handleFailure)
    end
end

Very cool ^:)^

So the installer worked :slight_smile:

Yep, for this statusbar. I like it, thanks. (Now for the gist)

Worked for me.Awesome tool!

so simple to use, so effective. why things arent that obvous to me?)))))) hands down, thank you)

Wow! The installer was super smooth! Any thoughts on making a tutorial? It would definately take the codea community to the next level! =)

Glad you guys like it. Again, this was inspired and a lot of the code was from @Jvm38. I simply hacked it out of his awesome windows library.

@presidenten What is it you’d like the tutorial on?

If I want to share code like this. How do I set it up to be able to share with your installer =)

Look at Gister or AutoGist

So all this talk of installers inspired me to finally get off my butt and semi-formally share a pre-release version of a my module system that I’ve been using for quite awhile now. While cmodule itself is not directly related to this thread, I actually spent some time last week exploring specifically how it might be used to help create Codea application/template/library installers easily, and I have found it to be quite useful for exactly that purpose (@Briarfox it seems like you and I have some overlapping interests in our Codea usage :smiley: ). In fact, this weekend I’ll release a sample project for creating installers using cmodule to give an example of it’s usefulness.

See this thread for more info about cmodule if you are interested:
http://twolivesleft.com/Codea/Talk/discussion/2794/cmodule%3A-sneak-peak-at-a-dynamic-module-loading-system-designed-for-codea#Item_1

@toadkick that is awesome! I’ll be playing with it when I get off of work.

as I said above - superb. the only problem I noticed was, that “Mem” raised each 6 seconds by 50kb, which can be considered a bug or memory leak))) I included collectgarbage() to have always clean messurement. I also cleaned up the code a bit. here it is:

--# Main
-- StatusBar

function setup()
    statusBar = StatusBar()
end

function draw()
    background(229, 229, 229, 255)
    statusBar:draw()
end

function touched(touch)
    statusBar:touched(touch)
end
--# Memory
--Original Code from Jvm38
-----Memory Class
Memory = class()

function Memory:init(x, y)
    -- Accept and set parameters here.
    self.x = x
    self.y = y
end

function Memory:draw()
    self:update()
    
    pushStyle()
    
    strokeWidth(0)
    fill(102, 108, 104, 193)
    rectMode(CENTER)
    rect(self.x, self.y, 110, 30)
    
    font("Optima-Regular")
    fontSize(16)
    fill(213, 215, 219, 255)
    text("Mem: "..self.txt, self.x, self.y)
    
    popStyle()
end

function Memory:update()
    local kb = math.floor(gcinfo() / 10) * 10
    self.txt = tostring(kb).." kb"
    collectgarbage()
end
--# FPS
--Original Code from Jvm38
-------------------FPS CLASS
Fps = class()

function Fps:init(x, y)
    -- Accept and set parameters here.
    self.x = x
    self.y = y
    
    self.curr = 60
    self.currTime = ElapsedTime
    self.min = 60
    self.minTime = ElapsedTime
    
    self.frac = .05 --framerate smoothing
end

function Fps:draw()
    self:update()
    
    pushStyle()
    
    strokeWidth(0)
    fill(102, 108, 104, 193)
    rectMode(CENTER)
    rect(self.x, self.y, 110, 30)
    
    font("Optima-Regular")
    fontSize(16)
    fill(213, 215, 219, 255)
    text("FPS: "..self.txt, self.x, self.y)
    
    popStyle()
end

function Fps:update()
    local old = self.curr
    local new = 1 / DeltaTime or old
    
    if self.min > new then
        self.min = new
        self.minTime = ElapsedTime + 1
    end
    if self.minTime < ElapsedTime then
        self.min = 60
    end
    
    self.curr = old * (1 - self.frac) + new * self.frac
    
    self.txt = tostring(math.floor(self.curr)).." (> "..tostring(math.floor(self.min))..")"
end
--# Clock
--Original Code from Jvm38
------CLOCK CLASS

Clock = class()

function Clock:init(x, y)
    -- Accept and set parameters here.
    self.x = x
    self.y = y
end

function Clock:draw()
    self:update()
    
    pushStyle()
    
    strokeWidth(0)
    fill(102, 108, 104, 193)
    rectMode(CENTER)
    rect(self.x,self.y,110,30)
    
    font("Optima-Regular")
    fontSize(16)
    fill(213, 215, 219, 255)
    text(self.time, self.x, self.y)
    
    popStyle()
end

function Clock:update()
    local t = os.date("*t")
    self.time = tostring(t.hour)..":"
    
    if t.min < 10 then
        self.time = self.time.."0"..tostring(t.min)
    else
        self.time = self.time..tostring(t.min)
    end
end
--# StatusBar
--Created by Briarfox 
StatusBar = class()

function StatusBar:init()
    -- Accept and set parameters here.
    parameter.boolean("ShowStatusBar", self:readState(), self.saveState)
    --parameter.action("ResetStatusBar", self.reset)
    
    self.x = readProjectData("statusBarX") or WIDTH/2
    self.y = readProjectData("statusBarY") or HEIGHT/2
    
    self.fps = Fps(-110,0)
    self.clock = Clock(0,0)
    self.mem = Memory(110,0)
end

function StatusBar:readState()
    local state = readProjectData("StatusBarState")
    if state == nil then state = true end
    return state
end

function StatusBar:saveState()
    saveProjectData("StatusBarState", ShowStatusBar)
end

function StatusBar:reset()
    clearProjectData()
    print("ResetStatusBar takes effect after restart.")
end

function StatusBar:draw()
    if ShowStatusBar then 
        pushMatrix()
        translate(self.x, self.y)
        
        self.fps:draw()
        self.clock:draw()
        self.mem:draw()
        
        popMatrix()
    end
end

function StatusBar:touched(touch)
    if ShowStatusBar then
        if touch.state == BEGAN and touch.x < (self.x + 165) and touch.x > (self.x - 165) then
            if touch.y > (self.y - 15) and touch.y < (self.y + 15) then
                self.moving = true
            end
        end
        
        if touch.state == MOVING and self.moving then
            if touch.y + 15 < HEIGHT and touch.y - 15 > 0 and touch.x + 165 < WIDTH and touch.x - 165 > 0 then
                self.x = touch.x
                self.y = touch.y
            end
        end
        
        if touch.state == ENDED then
            if self.moving then self.moving = false end
            saveProjectData("statusBarX",self.x)
            saveProjectData("statusBarY",self.y) 
        end
    end
end

Thanks @se24vad! I really havent messed with garbage collection in code. Gist is updated.

How can i use this in my own project?
If I use StatusBar() then I only see the statusbar on/off button.
The statusbar program on its own works fine.

@Dreamdancer make sure you are not calling the draw beind the background.

function setup()
statusBar = StatusBar()
end

function draw()
background(0,0,0,255)
your code here
statusBar:draw()
end

function touched(touch)
statusBar:touched(touch)
end

@Briarfox Thanks it works now. I did forgot the line statusBar= StatusBar().
But it did nt help me much. I have a strange fault with my project, I thought it was a memory problem but it is not I only use 150 kb. Are there more debug tools available?
Codea stops and I have startup Codea 2 times to get it running again. It is a simple boardgame
I am working on.

I’m working on some more debug tools. I’m not entirely convinced the memory status is accurate. I just used jvm’s code, I’ll be looking it over shortly.