Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Add animation example #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LilithHafner
Copy link
Contributor

It took me a while to figure out how to make an animation. I think an example and/or an explanation belongs in the docs. I've tested this and it seems to work, but I still don't totally understand how to use the Gtk system. Specifically, the sleep(.0001) calls seem like a hack, and the c.draw(true) call is a bit mysterious to me (what does true mean?), and the while !c.is_sized loop should be a wait call but I don't know what to wait on.

It might also make sense to put a simpler animation-only example first and then add this combined example after if at all.

Perhaps this could be a reasonable animation-only example, but its still pretty complex:
using Gtk, Graphics

# Initialization
c = @GtkCanvas()
win = GtkWindow(c, "Animation")

# Rendering
@guarded draw(c) do widget
    ctx = getgc(c)
    h = height(c)
    w = width(c)

    # Paint background
    rectangle(ctx, 0, 0, w, h)
    set_source_rgb(ctx, 0, 0, 0)
    fill(ctx)
    set_source_rgb(ctx, .4, .4, .4) # color and path can appear in either order
    rectangle(ctx, 0, 0, w, h * (time() % 1))
    fill(ctx)
end

running = Ref(true)
signal_connect(win, :destroy) do widget
    running[] = false
end

# Initialization part 2
show(c)
while !c.is_sized
    sleep(.0001) # Wait for the canvas to initialize before we can call getgc(c)
end

# Main loop
while running[]
    c.draw(true) # Manually repaint the scene (this does not automatically clear the background)
    reveal(c) # Tell Gtk that the canvas needs to be re-drawn
    sleep(.0001) # Hand control back to Gtk to display the redrawn window and handle events.
end

I'd like an example of "animation done well" in the docs, but this likely needs some revision before it can be exemplary.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 2805140701

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-1.8%) to 62.857%

Totals Coverage Status
Change from base Build 2795089809: -1.8%
Covered Lines: 1628
Relevant Lines: 2590

💛 - Coveralls

1 similar comment
@coveralls
Copy link

Pull Request Test Coverage Report for Build 2805140701

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-1.8%) to 62.857%

Totals Coverage Status
Change from base Build 2795089809: -1.8%
Covered Lines: 1628
Relevant Lines: 2590

💛 - Coveralls

@codecov
Copy link

codecov bot commented Aug 5, 2022

Codecov Report

Merging #657 (3fe1be5) into master (ea4e6ab) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #657   +/-   ##
=======================================
  Coverage   65.60%   65.60%           
=======================================
  Files          32       32           
  Lines        2721     2721           
=======================================
  Hits         1785     1785           
  Misses        936      936           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

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

Successfully merging this pull request may close these issues.

2 participants