yglukhov / nimx

GUI library
MIT License
1.08k stars 76 forks source link

How to draw images on collectionView elements (rects) ? #500

Open sgmihai opened 1 year ago

sgmihai commented 1 year ago

I am trying to build a very basic gui mockup for my project, and I need a grid of images displayed (prefferably auto resizing to fit the collectionView, which should auto resize with the main window). I tried modifying the https://github.com/yglukhov/nimx/blob/master/test/sample07_collections.nim code with what I found in https://github.com/yglukhov/nimx/blob/master/test/sample03_image.nim but no success. Code I tried is here: https://play.nim-lang.org/#ix=480e Basically I put

let c = currentContext()
c.drawImage(v.posters[i],result.frame)

in the last line of collectionView.viewForItem I am not sure if it's the proper place or way to do this, in this context. Result is this: https://i.imgur.com/eGEFGi4.png

I have no experience whatsoever with guis, and already spent half a day trying to figure it out, and another full day researching alternatives. In the end I think nimx is the shortest path to my goal. If only it had more complete and comprehensive docs.

yglukhov commented 1 year ago

CollectionViews are a bit outdated, as nimx is in the process of migration to new layout system. But what you're doing wrong is actually drawing the image in viewForItem while you should instead just return the view that will draw said image in its draw method. Smth along the lines of:

let imgView = newImageView(...)
# ... setup imgView
result.addSubview(imgView)

I couldn't play with your example as I could not find imdb dependency.

sgmihai commented 1 year ago

Complete code if you have time for it is here: https://drive.google.com/file/d/1Qq3DtlkfMkmMFSemHgRaiSRk6JDwnGw6/view?usp=sharing compile gui_main.nim, as it was taken from your examples with the registerSample -> tabs. first tab is gui_image.nim which has working loading of images (but no auto arranging) gui_collections.nim is where I tried to achieve the results, the code above, and failed.

I see there is no draw method in that example, and not sure how to implement it, as I have no knowledge on the inner working of nimx, and the documentation is not helping there.