Information project pt:2

Hi, I just found out how to hide buttons (thanks to @luatee), but when I start the program, the buttons just hide! This is not what I want though. I would like to hide all buttons when one is pressed, and have a back button.

#main


-- whoo
supportedOrientations(LANDSCAPE_RIGHT)
-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    button = Button("Japanese")
    button.action = function() buttonPressed() end
    button2 = Button("Polish")
    button2.action = function() button2Pressed() end
    button3 = Button("American")
    button3.action = function() button3Pressed() end
    button4 = Button("Indian")
    button4.action = function() button4Pressed() end
    button5 = Button("Chinese")
    button5.action = function() button5Pressed() end
    saveLocalData( "info.japan", "History... the history of Japan is very unique. History will cover The Emperor and the Samurai. First, the samurai. The first samurai claimed a majority of Japan in 1192." )
    saveLocalData( "info.poland", "History..." )
    saveLocalData( "info.america", "History..." )
    saveLocalData( "info.india", "History..." )
    saveLocalData( "info.china", "History..." )
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(91, 163, 92, 118)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    drawButton()
end

function drawButton()
    button.pos = vec2(WIDTH/2 - 150, HEIGHT/2)
    button:draw()
    button2.pos = vec2(WIDTH/2 - 150, HEIGHT/4)
    button2:draw()
    button3.pos = vec2(WIDTH/2 - 150, HEIGHT/2.6)
    button3:draw()
    button4.pos = vec2(WIDTH/2 - 150, HEIGHT/6.9)
    button4:draw()
    button5.pos = vec2 (WIDTH/2 - 150, HEIGHT/1.5)
    button5:draw()
end

function buttonPressed()
    print("Japan")
    print(readLocalData("info.japan"))
end

function button2Pressed()
    print("Poland")
end

function button3Pressed()
    print("America")
end

function button4Pressed()
    print("India")
end

function button5Pressed()
    print("China")
end

function touched(touch)
    button:touched(touch)
    button2:touched(touch)
    button3:touched(touch)
    button4:touched(touch)
    button5:touched(touch)
end

the Button function
#Button


Button = class()

function Button:init(displayName)
    -- you can accept and set parameters here
    self.displayName = displayName
    
    self.pos = vec2(0,0)
    self.size = vec2(0,0)
    self.action = nil
    self.color = color(0, 70, 255, 184)
end

function Button:draw()
    -- Codea does not automatically call this method
    pushStyle()
    fill(self.color)
    font("MarkerFelt-Thin")
    fontSize(30)
    
    -- use longest sound name for size
    local w,h = textSize(self.displayName)
    w = w + 20
    h = h + 30
    
    roundRect(self.pos.x - w/2,
              self.pos.y - h/2,
              w,h,30)
            
    self.size = vec2(w,h)
            
    textMode(CENTER)
    fill(54, 65, 96, 255)
    text(self.displayName,self.pos.x+2,self.pos.y-2)
    fill(0, 255, 3, 255)
    text(self.displayName,self.pos.x,self.pos.y)
    
    popStyle()
end

function Button:hit(p)
    local l = self.pos.x - self.size.x/2
    local r = self.pos.x + self.size.x/2
    local t = self.pos.y + self.size.y/2
    local b = self.pos.y - self.size.y/2
    if p.x > l and p.x < r and
       p.y > b and p.y < t then
        return true
    end
    
    return false
end

function Button:touched(touch)
    -- Codea does not automatically call this method
    if touch.state == ENDED and
       self:hit(vec2(touch.x,touch.y)) then
        if self.action then
            self.action()
        end
    end
end

roundRect function
#roundRect


function roundRect(x,y,w,h,r)
    pushStyle()
    
    insetPos = vec2(x+r,y+r)
    insetSize = vec2(w-2*r,h-2*r)
    
    --Copy fill into stroke
    local red,green,blue,a = fill()
    stroke(red,green,blue,a)
    
    noSmooth()
    rectMode(CORNER)
    rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
    
    if r > 0 then
        smooth()
        lineCapMode(ROUND)
        strokeWidth(r*2)

        line(insetPos.x, insetPos.y, 
             insetPos.x + insetSize.x, insetPos.y)
        line(insetPos.x, insetPos.y,
             insetPos.x, insetPos.y + insetSize.y)
        line(insetPos.x, insetPos.y + insetSize.y,
             insetPos.x + insetSize.x, insetPos.y + insetSize.y)
        line(insetPos.x + insetSize.x, insetPos.y,
             insetPos.x + insetSize.x, insetPos.y + insetSize.y)            
    end
    popStyle()
end

Thank you in advance.

RandD

@RandD - can I suggest you keep all your queries for this project, in a single thread, to avoid cluttering the forum? Then you can post in it as much as you like.

Also, you will tend to find the same people helping you, so it’s convenient if you stick to the same thread.

PS You can change the topic name at any time by just going back to the very first post and editing it.

@RandD Ignatz has good advice for keeping it all organised in one thread as all of us here tend to keep everything related in one place no matter how old the thread is. Anyway the self.visible can easily cover what you need. If you would like to have dynamic buttons that can have there inner states varied by calling functions created in the class then the best approach is internal variables. Even if you create a controller class that loops through all buttons to make them invisible (a for loop for instance, pairs or not, look it up if you need to) will handle all of your buttons and inside you can decide whether, or whether not you hide the button and keep all other visible etc

Ok, but I do need code… :stuck_out_tongue: please post it on the old thread and I will delete this one.
RandD