Howdy, Stranger!

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

In this Discussion

Buttons in the screen

1) Create a blank file
2) Copy there this script:

Button = class()

function Button:init(displayName)
    -- you can accept and set parameters here
    self.displayName = displayName

    self.pos = vec2(0,0)
    self.size = vec2(100,100)
    self.action = nil
    self.color = color(34, 33, 33, 255)
    self.isTextButton = false
end

function Button:draw()
    -- Codea does not automatically call this method
    pushStyle()
    fill(self.color)

    font("AmericanTypewriter-CondensedBold")
    fontSize(22)

    local w,h = textSize(self.displayName)
    if(self.isTextButton) then
        -- use longest sound name for size
        w = w + 20
        h = h + 30
        self.size = vec2(w,h)
    else
        w=self.size.x
        h=self.size.y
    end
    rectBox(self.pos.x - w/2,self.pos.y - h/2,w,h)
    -- rect(self.pos.x,self.pos.y,w,h)

    -- self.size = vec2(w,h)

    textMode(CENTER)
    fill(54, 65, 96, 255)
    text(self.displayName,self.pos.x+2,self.pos.y-2)
    fill(255, 255, 255, 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

    function rectBox(x,y,w,h)
    pushStyle()

    insetPos = vec2(x,y)
    insetSize = vec2(w,h)

    --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)

    popStyle()
    end

3) To place buttons do (in the class, where you want to place it):

function setup()
button1 = Button("The Button value")
button1.action = function() button1f() end
end
-- The button1's function
function button1f()
-- Here goes the action from button1
print("You tipped the button1")
end
function draw()
button1.pos = vec2(x, y) -- You have to set an integer value for x and y
button1.size = vec2(wdt, hgt) -- You have to set an integer value for wdt and hgt (width and height)
button1:draw()
end
function touched(touch)
button1:touched(touch)
end
Tagged:

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    @TokOut - there's no need to make a new thread for every post. Please keep your comments on the same topic, in the same thread where possible.

    So you created all this code yourself in the few minutes since you asked where you could find button code?

    Nope - I can see where you copied this code from. Please don't take credit for other people's work.

Sign In or Register to comment.