Howdy, Stranger!

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

In this Discussion

A good looking textparagraph, but ....

edited December 2015 in Questions Posts: 809

This is the textparagraph code, but how to make that a new line will be under it and not in the middle. The code will be in the next message.

The Main class:

something = TextParagraph(x, y, width, height, text value or nil)
something:draw()
something:touched(t)

Comments

  • edited December 2015 Posts: 809

    New code?

  • edited December 2015 Posts: 809

    Thank you for help!

  • Posts: 809

    Why is nobody helping?

  • Posts: 809

    Here is the end code:

    TextParagraph = class()
    
    function TextParagraph:init(x, y, w, h, txt)
        -- nil into number
        if x == nil then x = 0 end
        if w == nil then w = 450 end
        if h == nil then h = 250 end
        if y == nil then y = 0 end
        if txt == nil then txt = "" end
        -- the self values
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.text = txt
        self.placeholder = "This is a TextBox\nMade by TokOut.\nStart to write and\nremove this Add."
        self.enabled = false
    end
    
    function TextParagraph:draw()
        -- Official Look
        fill(255, 255, 255, 255)
        stroke(160, 160, 160, 255)
        strokeWidth(5)
        rect(self.x, self.y, self.w, self.h)
        -- Text Settings
        fontSize(25)
        font("AmericanTypewriter")
        textMode(CORNER)
        textWrapWidth(0)
        -- Text
        fill(0, 0, 0, 255)
        ww,hh = textSize(self.text)
        text(self.text, self.x + 20, self.y + self.h - 20 - hh)
        -- Place holder
        fill(127, 127, 127, 255)
        if string.len(self.text) == 0 then
            text(self.placeholder, self.x + self.w/2, self.y + self.h/2)
        end
    end
    
    function TextParagraph:touched(t)
        if t.state == ENDED and self:hit(vec2(t.x,t.y)) then
            showKeyboard()
            self.enabled = true
        elseif not self:hit(vec2(t.x,t.y)) and t.state == BEGAN then
            hideKeyboard()
            self.enabled = false
        end
    end
    
    function TextParagraph:hit(p)
        local t = self.y + self.h
        local b = self.y
        local r = self.x + self.w
        local l = self.x
        if p.x > l and p.x < r and p.y > b and p.y < t then
            return true
        end
        return false
    end
    
    function TextParagraph:keyboard(key)
        if self.enabled == true then
            if key == BACKSPACE and self.enabled == true then
                if string.len(self.text) > 0 then
                    self.text = string.sub(self.text, 1, string.len(self.text) - 1)
                end
            elseif key == RETURN then
                self.text = self.text .. "\n"
            else
                self.text = self.text .. key
            end
        end
    end
    
Sign In or Register to comment.