Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor disappears when filling the input box #27

Closed
ghost opened this issue Feb 5, 2016 · 2 comments
Closed

Cursor disappears when filling the input box #27

ghost opened this issue Feb 5, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Feb 5, 2016

When typing a text longer than the input box, when typing the last character, typically the cursor disappears, and one character is shown partially.

This is caused by the fact that the control box width is greater than the edit box width, and the former is used for text width calculations. The latter is adjusted in https://github.com/vrld/SUIT/blob/master/theme.lua#L105 to be 6 pixels less than the control's bounding box.

It would be trivial to fix it this way, but that would not cover the case where the theme is adjusted to set a different margin for the edit box:

diff --git a/input.lua b/input.lua
index e5861bd..0751f40 100644
--- a/input.lua
+++ b/input.lua
@@ -15,7 +15,7 @@ return function(core, input, ...)
    opt.font = opt.font or love.graphics.getFont()

    local text_width = opt.font:getWidth(input.text)
-   w = w or text_width + 4
+   w = w or text_width + 6
    h = h or opt.font:getHeight() + 4

    input.text = input.text or ""
@@ -29,23 +29,24 @@ return function(core, input, ...)
    -- get size of text and cursor position
    opt.cursor_pos = 0
    if input.cursor > 1 then
-       local s = input.text:sub(0, utf8.offset(input.text, input.cursor)-1)
+       local s = input.text:sub(1, utf8.offset(input.text, input.cursor)-1)
        opt.cursor_pos = opt.font:getWidth(s)
    end

    -- compute drawing offset
+   local wm = w - 6 -- consider margin
    input.text_draw_offset = input.text_draw_offset or 0
    if opt.cursor_pos - input.text_draw_offset < 0 then
        -- cursor left of input box
        input.text_draw_offset = opt.cursor_pos
    end
-   if opt.cursor_pos - input.text_draw_offset > w then
+   if opt.cursor_pos - input.text_draw_offset > wm then
        -- cursor right of input box
-       input.text_draw_offset = opt.cursor_pos - w
+       input.text_draw_offset = opt.cursor_pos - wm
    end
-   if text_width - input.text_draw_offset < w and text_width > w then
+   if text_width - input.text_draw_offset < wm and text_width > wm then
        -- text bigger than input box, but does not fill it
-       input.text_draw_offset = text_width - w
+       input.text_draw_offset = text_width - wm
    end

    -- user interaction
@vrld
Copy link
Owner

vrld commented Mar 13, 2016

Thanks for the fix. Do you have any idea how to decouple computation of the cursor from the theme?

@ghost
Copy link
Author

ghost commented Mar 13, 2016

The question would be how to obtain that information from the theme. I guess it could be some kind of standard setting in the theme itself, that theme makers would have to set. Basically you have two nested boxes: the control container box and the clipped text input box. Let theme designers be aware of this, and let them adjust the margin for the contained box. Then make input.lua read the theme's setting to adjust the margins accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant