Jump to content

Creating an edit box with dX functions


Bacchus

Recommended Posts

This can be a little tricky, as there's a few different ways to accomplish this. I've been thinking of re-writing my login interface and make it publicly available so people can learn from it and customize it the way they want. But I've been lazy lately :P

Basically, first of all you will need to use these events;

onClientClick -- Detect whenever an "editbox" is clicked. 
onClientKey   -- Detect whenever the player tries to type something into the "editbox" 

1. The player clicks the editbox, this sets the status to active/true (onClientClick)

2. While an editbox is active, it will accept input (onClientKey)

3. Update the text whenever a letter or number is entered. Look up concatenation if you don't know how to do this.

4. Detect if backspace or delete is pressed (onClientKey). Use string.sub to remove letters accordingly.

5. You can count the letters in the string to calculate the position of the caret.

The list goes on. Basically when you write interface behaviour like this you should always assume that the user will mess something up. So be ready to capture any possible mistakes and notify the user. For example if the text is too long, contains invalid characters and so on.

Link to comment

I know this doesn't help you nor answer your question but, why don't you just use the CEGUI one? It's efficient and bug-free. It doesn't have to be inside a window, just create it in the right position to suit the rest of your dx user interface.

Link to comment

Ok it's working, thank you.

But, is there a way to hide (with setGuiAlpha) an edit box but display the characters that have been entered into this edit box? Because when I set the alpha of the edit box to 0, I can't see the characters typed.

Thanks,

Link to comment

There's no way to do that that, not that I know of. I've tried to do it before as a part of a dxEditBox class.

I ended up treating the caret as the dxEditBox's caret as well. So basically, I positioned the dx text so that the edit box's caret would also be the dxEditBox's caret.

Link to comment
local keys = {} 
  
function isMouseInPosition ( x, y, width, height ) 
    if ( not isCursorShowing ( ) ) then 
        return false 
    end 
  
    local sx, sy = guiGetScreenSize ( ) 
    local cx, cy = getCursorPosition ( ) 
    local cx, cy = ( cx * sx ), ( cy * sy ) 
    if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then 
        return true 
    else 
        return false 
    end 
end 
  
addEventHandler ("onClientResourceStart", getRootElement(), 
    function () 
        showCursor (true) 
    end 
) 
  
function isClickingOnRectangle (button, state) 
    local position = isMouseInPosition (350, 350, 150, 200) 
    if position and (state == "down") then 
        addEventHandler ("onClientCharacter", getRootElement(), insertCharsInTable) 
        bindKey ("backspace", "down", removeLastChar) 
    else 
        removeEventHandler ("onClientRender", getRootElement(), insertCharsInTable) 
        unbindKey ("backspace", "down", removeLastChar) 
    end 
end 
addEventHandler ("onClientClick", getRootElement(), isClickingOnRectangle) 
  
function insertCharsInTable (charac) 
    table.insert (keys, charac) 
end 
  
function removeLastChar () 
    local lastNum = table.maxn (keys) 
    table.remove (keys, lastNum) 
end 
  
function drawIt () 
    local sentence = table.concat (keys, "") 
    dxDrawRectangle (350, 350, 150, 35, tocolor (255, 255, 255, 150)) 
    if sentence then 
        dxDrawText (sentence, 400, 354, x, y, tocolor (255, 255, 255), 1.2, "default-bold") 
    end 
end 
addEventHandler ("onClientRender", getRootElement (), drawIt) 
  
function mouseDrawingLine () 
    dxDrawRectangle (355, 353, 2, 30, tocolor (0, 0, 0, 100))  
end 
addEventHandler ("onClientRender", getRootElement(), mouseDrawingLine) 

Have a look at this, it's a way how edit works. It was made so fast but you can look at the example, and make your own. Make sure you have to click on the rectangle to be able to write on it. Then the way to get the text from there is by table.concating the whole table keys

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...