Skip to content
Snippets Groups Projects

Highlight keys when clicking or tapping

Merged Imported Administrator requested to merge feature/highlight into master

This merge request should be considered a draft. It creates a stop-gap solution for #12 (closed) by inverting the background / foreground color of keys upon clicking and tapping.

I would like to use this as a basis for a discussion about the approach taken. My C++ is less than poor and my experience with SDL ... well, what experience? :sweat_smile: So while this does seem to work in my own testing, I'm not really confident in what I'm doing here and parts of my changes are probably utter crap. :see_no_evil:

Implementation-wise, I changed the keyboard textures to have streaming access mode (which is supposedly optimized for frequent changes). When clicking or tapping a key, its area is recreated inside the surface (taking the selection state into account) and then copied into the texture.

Open questions on my end:

  • Is the general approach of updating the surface, then copying it into the texture advisable here? Reading into some of the SDL docs, it seems that textures are optimized for display so maybe we should rather perform all operations on the textures directly (if that's possible by any chance)?
  • Is it really necessary to convert the surface into the window's pixel format before every transfer into the texture? It seems like I could also directly create the surface in the window's pixel format from the start but I wasn't sure what side effects that might have.
  • I'm currently memcpying the whole pixel array into the texture on any touch / mouse down & up event. That's probably not very efficient. Is there an easy way to copy just the key rect?

Update

Answering part of my questions myself after having done more research.

It appears that texture's are stored in VRAM on the GPU whereas surfaces are stored in RAM which should in principle make textures a lot more performant than surfaces. The key drawing seems to be possible on the texture directly, e.g. there is SDL_RenderFillRect which could be used on the texture instead of SDL_FillRect on the surface. The only thing that appears to require surfaces as an intermediary step is the text rendering. To speed that up, we could cache the textures created for each character to be able to quickly draw them upon selection.

Drawing into the texture directly would eliminate having to constantly map all of the surface's pixels into the window format as well as having to manually copy pixels over from the surface into the texture.

Update 2

This fixes #12 (closed) and adds both key highlighting as well as character previews. I'm using the approach suggested by @MartijnBraam in the comments now. All highlighted keys are drawn into a separate texture. Upon rendering of the keyboard, both the normal and highlighted texture are combined to create the highlighting / preview effect.

I'd welcome feedback on the styling. Also, my C++ is still ugly so an opinionated review would be appreciated. :slight_smile:

Some screenshots:

new-1

new-2

new-3

Edited by Administrator

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Administrator marked this merge request as draft · Imported

    marked this merge request as draft

    By Johannes Marbach on 2020-11-28T19:42:24

  • Administrator added 1 commit · Imported

    added 1 commit

    • ed903510 - Highlight keys when clicking or tapping

    Compare with previous version

    By Johannes Marbach on 2020-11-28T19:50:24

  • Administrator mentioned in issue #96 (closed) · Imported

    mentioned in issue #96 (closed)

    By Johannes Marbach on 2020-11-29T19:19:24

  • Administrator changed the description · Imported

    changed the description

    By Johannes Marbach on 2020-11-30T18:33:13

  • Administrator added 6 commits · Imported

    added 6 commits

    Compare with previous version

    By Johannes Marbach on 2020-12-06T12:43:01

  • Administrator changed the description · Imported

    changed the description

    By Johannes Marbach on 2020-12-06T13:38:06

  • Administrator changed the description · Imported

    changed the description

    By Johannes Marbach on 2020-12-06T13:38:25

  • Author Owner

    Looking forward to this one!

    By Noah Andrews on 2020-12-09T05:41:28

    • Author Owner
      Resolved by Administrator

      Seems great. I wonder if it would be possible to just draw the whole keyboard twice at the start, once normal and once with inverted colors and have that in 2 textures. Then when a key is held it can pick out the correct region of the inverted keyboard and draw that over the normal keyboard.

      It would also be quite easy to draw the inverted keyboard with a small vertical offset so thee highlighted keys are slightly above the one pressed, like offset with half a key's height.

      It would solve both the cache part and the memcpy part in one go and since all pixel data is in VRAM it should be very efficient on gpu accelerated hardware.

      By Martijn Braam on 2020-12-09T18:51:03

      Edited by Administrator
    • Author Owner
      Resolved by Administrator

      I'm also pretty sure the color conversions for the surface formats is because the drawing code in SDL supports less pixel formats than what the framebuffers and window managers support, especially the weird formats with R and B swapped on some phones.

      By Martijn Braam on 2020-12-11T19:42:03

      Edited by Ghost User
  • Administrator added 3 commits · Imported

    added 3 commits

    Compare with previous version

    By Johannes Marbach on 2020-12-11T19:30:45

  • Administrator marked this merge request as ready · Imported

    marked this merge request as ready

    By Johannes Marbach on 2020-12-11T19:31:14

  • Administrator marked this merge request as draft · Imported

    marked this merge request as draft

    By Johannes Marbach on 2020-12-11T19:31:56

  • Administrator added 1 commit · Imported

    added 1 commit

    • 5e7072f8 - Highlight keys and show preview when clicking or tapping

    Compare with previous version

    By Johannes Marbach on 2020-12-11T19:33:47

  • Administrator changed the description · Imported

    changed the description

    By Johannes Marbach on 2020-12-11T19:39:05

  • Administrator changed the description · Imported

    changed the description

    By Johannes Marbach on 2020-12-11T19:40:03

  • Administrator resolved all threads · Imported

    resolved all threads

    By Johannes Marbach on 2020-12-11T19:41:09

  • Administrator resolved all threads · Imported

    resolved all threads

    By Johannes Marbach on 2020-12-11T19:42:05

  • Administrator marked this merge request as ready · Imported

    marked this merge request as ready

    By Johannes Marbach on 2020-12-11T19:44:33

  • Author Owner

    Wow, amazing job on this, it works (and looks!) really great! I'll try to get this reviewed ASAP.

    By clayton craft on 2020-12-11T20:23:43

  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
Please register or sign in to reply
Loading