Howdy, Stranger!

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

[CLOSED] My Alert class does not work - Why??

AnatolyAnatoly Mod
edited November 2015 in Questions Posts: 876

Hi, I am making great work.
My Alert Buttons {OK, Dismiss} are based on a text and a hit function. My Dismiss Button doesn't work. Why?
The other thing what I want to know is, how to create that if there are more then xxx symbols in the alert information there will be a new line. Is there anyone going to help me?
Main class:

displayMode(OVERLAY)
displayMode(FULLSCREEN)
function setup()
    al = Alert(500, 200, nil)
end
function draw()
    background(0, 0, 0, 255)
    al:draw()
    font("AmericanTypewriter")
    fontSize(25)
    if al.alertdraw == false then
        text("Touch to see the alert", WIDTH/2, HEIGHT/2)
    end
end
function touched(t)
    al:touched(t)
    al.alertdraw = true
end

Alert class:

Alert = class()

function Alert:init(x, y, action, title, txt)
    if action == nil then
        action = function() print("Action!") end
    end
    if title == nil then
        title = "Alert"
    end
    if txt == nil then
        txt = "The Alert has no content"
    end

    self.x = x
    self.y = y
    self.alertdraw = false
    self.action = action
    self.headtext = title
    self.informationtext = txt
end

function Alert:draw()
    if self.alertdraw == true then
        -- Alert Box
        fill(255, 255, 255, 255)
        rectMode(CORNER)
        rect(self.x, self.y, 350, 200)

        -- Alert Title Text
        font("AmericanTypewriter-Bold")
        fontSize(25)
        fill(42, 64, 74, 255)
        text(self.headtext, self.x + 175, self.y + 175)

        -- Alert Information Text
        font("AmericanTypewriter")
        fontSize(15)
        fill(0, 0, 0, 255)
        text(self.informationtext, self.x + 175, self.y + 150)

        -- Buttons
        font("AmericanTypewriter")
        fontSize(25)
        fill(255, 0, 0, 255)
        text("Dismiss", self.x + 280, self.y + 25)
        fill(0, 0, 255, 255)
        text("OK", self.x + 35, self.y + 25)
    end
end

function Alert:touched(t)
    if t.state == ENDED and self:OKhit(vec2(t.x,t.y)) then
        if self.action then
            self.action()
        end
    end

    if t.state == ENDED and self:Dismiss(vec2(t.x,t.y)) then
        self.alertdraw = false
        print("No Alert Draw")
    end
end

function Alert:OKhit(p)
    local OKT = self.y + 50
    local OKB = self.y
    local OKR = self.x + 50
    local OKL = self.x
    if p.x > OKL and p.x < OKR and p.y > OKB and p.y < OKT then
        return true
    end
    return false
end

function Alert:Dismiss(d)
    local t = self.y - 50
    local b = self.y + 50
    local r = self.x + 350
    local l = self.x + 250
    if d.x > l and d.x < r and d.y > b and d.y < t then
        return true
    end
    return false
end

Comments

  • dave1707dave1707 Mod
    edited November 2015 Posts: 7,907

    Try this. You're also always setting al.alertdraw to true in the touched() function.

    function Alert:Dismiss(d)
        local t = self.y + 50
        local b = self.y
        local r = self.x + 350
        local l = self.x + 250
        if d.x > l and d.x < r and d.y > b and d.y < t then
            return true
        end
        return false
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    Try this

    function draw()
        background(0, 0, 0, 255)
        font("AmericanTypewriter")
        fontSize(25)
        if al.alertdraw == false then
            text("Touch to see the alert", WIDTH/2, HEIGHT/2)
        else
            al:draw()
        end
    end
    
  • AnatolyAnatoly Mod
    edited November 2015 Posts: 876

    This content was deleted. Reason: OutDated

  • AnatolyAnatoly Mod
    Posts: 876

    @Ignatz. So?

  • AnatolyAnatoly Mod
    Posts: 876

    @Ignatz - offline?

  • AnatolyAnatoly Mod
    Posts: 876

    @yojimbo2000, can you help me plz

    Thank you very much!

  • IgnatzIgnatz Mod
    Posts: 5,396

    @TokOut - please don't nag, we are not here just to answer your questions

  • dave1707dave1707 Mod
    Posts: 7,907

    @TokOut I sent you a correction 6 hours ago, did you try it. Did it work or is there still a problem. You constantly ask for help, but you don't take the time to respond if what was sent to you worked or not.

  • Posts: 2,020

    Yup, I know I've answered him before about the text wrap thing and was just met with rudeness.

  • AnatolyAnatoly Mod
    Posts: 876

    @dave1707 - You mean the Main class correction or the hit correction? —> I used them both

  • dave1707dave1707 Mod
    Posts: 7,907

    @TokOut The correction was for the dismiss button and to close the alert.

  • AnatolyAnatoly Mod
    Posts: 876

    Really? Maybe I done something wrong? My Alert didn't work –> I gave up! So you can close the thread

  • Thank you for your help Dave, Yojimbo, and Ignatz. I read your posts going back to 2010. Codea is a great tool for prototyping and exploring, and this forum adds a lot of value. I wish there was a way to track patterns instead of questions.

    I've marked this thread as a save. The idea of passing an action function to the alert extends the state machine pattern.

  • AnatolyAnatoly Mod
    Posts: 876

    And I am back [Don't worry, I am fast explaining]
    A cool tool for making buttons. The best button I ever made

    Main class as overview:

    -- setup function
    button = Button("displayName", x, z, function() action() end)
    -- draw function
    background(0)
    button:draw()
    -- touch clause with coordinates t
    button:touched(t)
    -- action function <     action()     >
    

    Button class:

    Button = class()
    
    function Button:init(name, x, y, action)
        if x == nil or y == nil then
            x = 0
            y = 0
        end
    
        if name == nil then
            name = "Nil"
        end
    
        if action == nil then
            action = function() print("Using a Non-Function Button") end
        end
    
        self.x = x
        self.y = y
        self.name = name
        self.action = action
        self.color = color(255, 255, 255, 255)
    end
    
    function Button:draw()
        fill(self.color)
        rect(self.x, self.y, 75, 75)
        fill(0, 0, 0, 255)
        font("AmericanTypewriter")
        fontSize(25)
        textMode(CENTER)
        text(self.name, self.x + 35, self.y + 35)
    end
    
    function Button:touched(t)
        if t.state == BEGAN and self:hit(vec2(t.y,t.x)) then
            self.color = color(187, 187, 187, 255)
            self.action()
        else
            self.color = color(255, 255, 255, 255)
        end
    
        if t.state == ENDED then
            self.color = color(255, 255, 255, 255)
        end
    end
    
    function Button:hit(p)
        local t = self.x + 75
        local b = self.x - 1
        local l = self.y - 1
        local r = self.y + 75
        if p.x > l and p.x < r and p.y > b and p.y < t then
            return true
        end
            return false
    end
    
  • dave1707dave1707 Mod
    Posts: 7,907

    @TokOut You seem to start a lot of things, ask for help, and then don't finish. You're just wasting everyone's time when you do that. Above you said you gave up on your Alert code. Using the change I gave you and a few minor changes to your code, your Alert code works the way it's supposed to. After the Alert shows, tapping OK executes whatever function you assign to it. Tapping Dismiss closes the Alert box with no action taken. Instead of writing code you don't understand or asking for help and not using or understanding what's given to you, start reading some books or tutorials and write small programs to try things. It takes time to learn a language and how to use it to write programs. When I started 3 years ago, I didn't know anything about Codea, and how it ran was totally different from the way I was used to writing code. There's still a lot I don't know about Codea, but that's what keeps me interested. I write small programs using functions I'm not familiar with and try different things with it so I can see what I can and can't do with it. What I don't know, others here do. We learn from each other. Constantly writing code you can't or won't finish isn't teaching you what you need to know.

  • AnatolyAnatoly Mod
    edited November 2015 Posts: 876

    4th TEST:

    pre
    lang="lua"
    
  • IgnatzIgnatz Mod
    edited November 2015 Posts: 5,396

    What are you doing?

    To post code, put three ~~~ on an empty line at the top of your code, and another three underneath your code.

    See how I changed your test above.

  • AnatolyAnatoly Mod
    edited November 2015 Posts: 876

    @Ignatz, I don't know how to create colored code.
    I don't know where to place the

    pre
    

    tag and use the

    lang="lua"
    

    attribute. Where to place it?

    Test because nobody answers....

    pre lang="lua"
    -- test
    
  • Posts: 2,020

    Read the faq

  • AnatolyAnatoly Mod
    Posts: 876

    I read the FAQ 5 times and still don't understand how to create this. Can you edit my post plz and show me?

  • dave1707dave1707 Mod
    Posts: 7,907

    @TokOut Use what I show here. Don't put any spaces after the < or before the > characters. I have spaces here so it doesn't do any formatting.

    < pre lang="lua" >

    Code goes here

    < /pre >

  • AnatolyAnatoly Mod
    edited December 2015 Posts: 876
    function setup()
    test = true
    end
    -- test
    
  • AnatolyAnatoly Mod
    Posts: 876

    Doesn't work? @dave1707

  • dave1707dave1707 Mod
    Posts: 7,907

    Why doesn't it work. What are you expecting it to do.

  • AnatolyAnatoly Mod
    Posts: 876

    It didn't worked

  • dave1707dave1707 Mod
    Posts: 7,907

    It didn't work doesn't tell me anything. What are you expecting it to do.

  • AnatolyAnatoly Mod
    edited December 2015 Posts: 876

    ~~~
    displayMode(OVERLAY)
    displayMode(FULLSCREEN)
    supportedOrientations(WIDTH)

    textpanel = ""
    

    function setup()
    -- TextBoxButton("value", x, y, function() k("value") end)
    parameter.watch("textpanel")
    A = TextBoxButton("a", 75, 225, function() k("a") end)
    B = TextBoxButton("b", 150, 225, function() k("b") end)
    C = TextBoxButton("c", 225, 225, function() k("c") end)
    D = TextBoxButton("d", 300, 225, function() k("d") end)
    E = TextBoxButton("e", 375, 225, function() k("e") end)
    F = TextBoxButton("f", 450, 225, function() k("f") end)
    G = TextBoxButton("g", 525, 225, function() k("g") end)
    H = TextBoxButton("h", 600, 225, function() k("h") end)
    I = TextBoxButton("i", 675, 225, function() k("i") end)
    J = TextBoxButton("j", 750, 225, function() k("j") end)
    K = TextBoxButton("k", 825, 225, function() k("k") end)
    L = TextBoxButton("l", 900, 225, function() k("l") end)
    M = TextBoxButton("m", 75, 150, function() k("m") end)
    N = TextBoxButton("n", 150, 150, function() k("n") end)
    O = TextBoxButton("o", 225, 150, function() k("o") end)
    P = TextBoxButton("p", 300, 150, function() k("p") end)
    Q = TextBoxButton("q", 375, 150, function() k("q") end)
    R = TextBoxButton("r", 450, 150, function() k("r") end)
    S = TextBoxButton("s", 525, 150, function() k("s") end)
    T = TextBoxButton("t", 600, 150, function() k("t") end)
    U = TextBoxButton("u", 675, 150, function() k("u") end)
    V = TextBoxButton("v", 750, 150, function() k("v") end)
    W = TextBoxButton("w", 825, 150, function() k("w") end)
    X = TextBoxButton("x", 900, 150, function() k("x") end)
    Y = TextBoxButton("y", 75, 75, function() k("y") end)
    Z = TextBoxButton("z", 150, 75, function() k("z") end)
    dot = TextBoxButton(".", 450, 75, function() k(".") end)
    comma = TextBoxButton(",", 525, 75, function() k(",") end)
    space = TextBoxButton("", 225, 75, function() k(" ") end, 225)
    backspace = TextBoxButton("<—", 825, 75, function() k("back") end)
    doubledot = TextBoxButton(":", 675, 75, function() k(":") end)
    wm = TextBoxButton("!", 750, 75, function() k("!") end)
    say = TextBoxButton("Say!", 900, 75, function() k("say") end)
    qm = TextBoxButton("?", 600, 75, function() k("?") end)
    cl1 = TextBoxButton("(", 75, 300, function() k("(") end)
    cl2 = TextBoxButton(")", 150, 300, function() k(")") end)
    cs1 = TextBoxButton("[", 225, 300, function() k("[") end)
    cs2 = TextBoxButton("]", 300, 300, function() k("]") end)
    l = TextBoxButton("-", 375, 300, function() k("-") end)
    one = TextBoxButton("1", 450, 300, function() k("1") end)
    two = TextBoxButton("2", 525, 300, function() k("2") end)
    three = TextBoxButton("3", 600, 300, function() k("3") end)
    pi = TextBoxButton("PI", 675, 300, function() k("pi") end)
    dollar = TextBoxButton("$", 750, 300, function() k("$") end)
    prozent = TextBoxButton("%", 825, 300, function() k("%") end)
    slash = TextBoxButton("~", 900, 300, function() k("~") end)
    rr = TextBoxButton("<", 75, 375, function() k("<") end)
    lr = TextBoxButton(">", 150, 375, function() k(">") end)
    rct = TextBoxButton("#", 225, 375, function() k("#") end)
    star = TextBoxButton("*", 300, 375, function() k("⭐️") end)
    AND = TextBoxButton("&", 375, 375, function() k(" and ") end)
    pr = TextBoxButton('"', 450, 375, function() k('"') end)
    apostroph = TextBoxButton("'", 525, 375, function() k("'") end)
    is = TextBoxButton("=", 600, 375, function() k("=") end)
    euro = TextBoxButton("€", 675, 375, function() k("€") end)
    at = TextBoxButton("@", 750, 375, function() k("@") end)
    wc = TextBoxButton("WC", 825, 375, function() k("

  • dave1707dave1707 Mod
    edited December 2015 Posts: 7,907

    What are you trying to do with the pre statements that isn't working.

  • dave1707dave1707 Mod
    Posts: 7,907

    @TokOut The pre=lua type tags are just for coloring code that's shown on the forum. It's not used to color code in a running program. See this link for an example of colored code shown on the forum. It just colors certain words, numbers, and special characters.

    http://codea.io/talk/discussion/3064/drawing-a-hex-grid
    
Sign In or Register to comment.