hecrj / coffee

An opinionated 2D game engine for Rust
https://docs.rs/coffee
MIT License
1.08k stars 55 forks source link

Scrollable widget support #47

Open hecrj opened 5 years ago

hecrj commented 5 years ago

Scrolling is quite a basic feature for a UI toolkit. We need it!

I see two different approaches to tackle this:

  1. Draw the scrollable contents into a Canvas. The issue is that we would need to recreate it once the scrollable changes dimensions. We could circumvent this initially by forcing users to set a fixed width and height for scrollables. However, this destroys the purpose of a responsive UI. Another approach could be simply avoid resizing the Canvas every frame while resizes happen, and only do it at a specific rate. Although this would cause the UI to not resize smoothly, it could be good enough for now.
  2. Implement scissor test support for Target. This sounds way more elegant, but it entails more work as we will have to add scissor support for all the current pipelines (quads and font rendering). It should be doable, but it needs to be done carefully.

In both scenarios, Widget::draw definition will probably need to change to improve composability and recursive draws (scrollables inside scrollables). Although I think a tree-like data structure stored in Renderer could work too.

Any other ideas?

jakubDoka commented 3 years ago

Triangles can be cropped to fit the rectangle. If you are collecting all vertices into batch and drawing them then you can just pass them trough cropping algorithm that will calculate new vertex and texture positions based of view rectangle. Assuming all objects inside ui can be represented by rectangles (AABB) when being drawn.