Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Storage Device [My first code!]

edited January 2015 in Code Sharing Posts: 100

This is my first program written with Codea! I'm really excited to show it to you guys, you've been a great help so far.

So basically, how it works, is that you type a password and some text to assign with it, then enter it in. Or type in a password and retrieve the data your signed to it.

Switching between "store" and "read" modes is done with a simple tap on the screen. :D

-- Storage
-- Use this function to perform your initial setup
function setup()
    passon = true
    method = "store"
    store = ""
    pass = ""
    key = ""
    returnStorage = ""
end

-- This function gets called once every frame
function draw()
    fontSize(17)

    -- This sets a white background color
    background(255, 255, 255, 255)
    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    text("(To change mode, please tap screen)", WIDTH/2, HEIGHT/3)
    text(pass, WIDTH/2, HEIGHT - 10)
    text(store, WIDTH/2, HEIGHT - 25)
    text(key, WIDTH/2, HEIGHT - 10)

    if method == "store" then
        text("Password:", 50, HEIGHT - 10)
        text("StoreValue:", 54, HEIGHT - 25)
    elseif method == "read" then
        text("Password:", 50, HEIGHT - 10)
        text("Result:", 37, HEIGHT - 25)
    end
    if returnStorage ~= "" then
        text(returnStorage, WIDTH/2, HEIGHT-25)
    end

    fontSize(30)
    text(method, WIDTH/2, HEIGHT/2)

    --Keyboard
    if isKeyboardShowing() == false then
        showKeyboard()
    end
end
function keyboard(k)
    if k == RETURN then
        if method == "store" then
            if passon then
                passon = false
            else
                if store == "" then 
                    saveLocalData(pass, nil)
                else
                    saveLocalData(pass, store)
                end
                store = ""
                pass = ""
                passon = true
            end
        elseif method == "read" then
            print(readLocalData(key))
            returnStorage = readLocalData(key)
            key = ""
        end
        store = ""
    elseif k == BACKSPACE then
        if method == "store" then
            if passon then
                pass = store:sub(1,#store-1)
            else
                store = store:sub(1,#store-1)
            end
        elseif method == "read" then
            key = key:sub(1,#key-1)
        end
    else
        if method == "store" then
            if passon then
                pass = pass .. k
            else 
                store = store .. k
            end
        elseif method == "read" then
            key = key .. k 
            returnStorage = ""
        end
    end
end

function touched(t)
    if t.state == BEGAN then 
        if method == "store" then
            print("ReadMode")
            key = ""
            pass = ""
            store = ""
            method = "read"
            returnStorage = ""
        elseif method == "read" then
            print("StoreMode")
            pass = ""
            store = ""
            returnStorage = ""
            key = ""
            passon = true
            method = "store"
        end
    end
end

displayMode(FULLSCREEN)
showKeyboard()

Any thoughts or suggestions on the program are greatly appreciated, although the main purpose of the thread is to show you what I've accomplished so far with Codea :D

Comments

  • dave1707dave1707 Mod
    Posts: 7,605

    When you switch from read to store, you need to clear the screen. The previous store value is still on the screen and shows as double text when you enter another store value. Also, you need something that indicates that you should enter a password or store value.

  • Posts: 136

    @warspyking nice job on your first program! =D>

  • @jr Thank you. @dave1707 There's text fields :P

  • Posts: 835

    Good job @warspyking =D>

  • @dave1707 I did however, fix the blank out glitch with this new code:

    -- Storage
    -- Use this function to perform your initial setup
    function setup()
        passon = true
        method = "store"
        store = ""
        pass = ""
        key = ""
        returnStorage = ""
    end
    
    -- This function gets called once every frame
    function draw()
        fontSize(17)
    
        -- This sets a white background color
        background(255, 255, 255, 255)
        -- This sets the line thickness
        strokeWidth(5)
    
        -- Do your drawing here
        text("(To change mode, please tap screen)", WIDTH/2, HEIGHT/3)
        text(pass, WIDTH/2, HEIGHT - 10)
        text(store, WIDTH/2, HEIGHT - 25)
        text(key, WIDTH/2, HEIGHT - 10)
    
        if method == "store" then
            text("Password:", 50, HEIGHT - 10)
            text("StoreValue:", 54, HEIGHT - 25)
        elseif method == "read" then
            text("Password:", 50, HEIGHT - 10)
            text("Result:", 37, HEIGHT - 25)
        end
        if returnStorage ~= "" then
            text(returnStorage, WIDTH/2, HEIGHT-25)
        end
    
        fontSize(30)
        text(method, WIDTH/2, HEIGHT/2)
    
        --Keyboard
        if isKeyboardShowing() == false then
            showKeyboard()
        end
    end
    function keyboard(k)
        if k == RETURN then
            if method == "store" then
                if passon then
                    passon = false
                else
                    if store == "" then 
                        saveLocalData(pass, nil)
                    else
                        saveLocalData(pass, store)
                    end
                    store = ""
                    pass = ""
                    passon = true
                end
            elseif method == "read" then
                print(readLocalData(key))
                returnStorage = readLocalData(key)
                key = ""
            end
            store = ""
        elseif k == BACKSPACE then
            if method == "store" then
                if passon then
                    pass = store:sub(1,#store-1)
                else
                    store = store:sub(1,#store-1)
                end
            elseif method == "read" then
                key = key:sub(1,#key-1)
            end
        else
            if method == "store" then
                if passon then
                    pass = pass .. k
                else 
                    store = store .. k
                end
            elseif method == "read" then
                key = key .. k 
                returnStorage = ""
            end
        end
    end
    
    function touched(t)
        if t.state == BEGAN then 
            if method == "store" then
                print("ReadMode")
                key = ""
                pass = ""
                store = ""
                method = "read"
                returnStorage = ""
            elseif method == "read" then
                print("StoreMode")
                pass = ""
                store = ""
                returnStorage = ""
                key = ""
                passon = true
                method = "store"
            end
        end
    end
    
    displayMode(FULLSCREEN)
    showKeyboard()
    
    

    Thanks for pointing that out.

  • Posts: 835

    @warspyking, just replace your code in the first post by hitting the "edit post" hyperlink at the top right of the post

Sign In or Register to comment.