GIF Reader

Post your nifty GP projects here!

Moderator: MSandro

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

GIF Reader

Post by SimpleSi » Oct 26th, '17, 13:03

Some of my Coding Clubbers are using Scratch to design backgrounds and then exporting them as gifs

I've written this extension so we can load them into current GP costume

Please try it out on any gifs you have lying around to see if it works for you
Attachments
readGIF12.gpp
(34.43 KiB) Downloaded 78 times

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: GIF Reader

Post by JohnM » Oct 27th, '17, 16:19

Cool! I'll give it a try!

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: GIF Reader

Post by SimpleSi » Oct 28th, '17, 12:35

Latest version readGIF24

Changelog

Data is read into binarydata and not arrays in order to save on memory requirements

2 extra blocks - one to save the gif into the images tab (or mutiple images if animated gif)

Other block plays the gif if its an animated one
EDIT _ file attached this time :)
Attachments
readGIF24.gpp
(20.46 KiB) Downloaded 65 times

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: GIF Reader

Post by SimpleSi » Oct 28th, '17, 12:57

Bug fix - forgot to remove a debugging say block :)
Attachments
readGIF25.gpp
(20.13 KiB) Downloaded 72 times

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: GIF Reader

Post by JohnM » Oct 28th, '17, 17:51

Since you're starting to write larger GP extensions, let me suggest an alternative work flow that may scale better, both from the standpoint of navigating through larger amounts of code and also from the standpoint of some of GP's current limitations when dealing with large numbers of blocks.

The idea is to use the "Class Browser" in developer mode to edit one method at a time:

Screen Shot 2017-10-28 at 1.18.36 PM.png

The names of the methods appear in the lower left pane of the browser. Selecting a method shows it's code in the right pane. You can edit the code for a method, then select "save changes" in the browser's scripting pane right-click menu to save your changes or "revert" to undo them:

Screen Shot 2017-10-28 at 1.22.30 PM.png

You can test things as you go by running scripts in the normal scripting pane, and you can see the values of your instance variable in the "Variables" pane. You can use the "make a block" menu command in the lower left pane of the Class Browser to add new methods. These methods appear in the "My Blocks" palette and can be called just as if they were defined in scripting area, but they don't appear in the scripting area, leaving the scripting area free for blocks you are using for testing and debugging.

In developer mode, you can open a class browser on a class by using the "browse this class" command in the right-click menu on the class name in the "Classes" pane. You can close the Class Browser window when you don't need it.

To save your work, you can save the entire project, as usual. You can also use the "export this class" menu command to save your class and you can use "import a class" to read the class into a new project. This saves all the methods and scripts of the class in a textual form. Exporting a class doesn't save the current values of instance variables. That doesn't matter for a utility like your GIF reader, since the instance variables get initialized when you use it to read a GIF file. Exporting a class saves only that one class, not other classes or shared variables in your project. Thus, if you want to share a collection of classes, possible that use some shared variables between them, it's better to save the entire project. However, for a single class, using import and export class has some advantages: it's fast, the resulting code file is small, and you can examine your code using a text editor. You can even edit it in the text editor if you're willing to deal with potential syntax errors such as mismatched parenthesis and spelling errors. (Sometimes making major structural changes, such as changing all variables from shared to instance variables, is quicker to do using find-replace in a text editor.)

When working in this style (and, actually, a good practice in general) it's better to use instance and local variables rather than shared variables. Doing this allows multiple instances of your class to run, possible in parallel, without interfering with each other's variables. It also avoids cluttering up the shared variables with variables that are internal to your class.

As an experiment, I converted an earlier version of your GIF reader (v12) to replace shared variables with instance variables. I see that you posted a newer version this morning after I'd started the conversion process in v12, but this will at least let you see how it feels to view and edit code with the class browser.

Note: I just realized that I didn't convert calls to shared blocks into calls to methods, so it definitely won't work. I'll do that conversion now... Okay, done!

To my amazement, I apparently didn't break anything in the conversion process -- the converted version actually works!
Attachments
gifReader12.gp
(7.25 KiB) Downloaded 76 times

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: GIF Reader

Post by JohnM » Oct 28th, '17, 18:15

If you'd like to work in this style and you'd like me to convert your current code to use instance variables and methods, post your latest version and let me know. It took me a couple of hours to do it the first time, but I think I can do it faster now that I've been through the process once...

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: GIF Reader

Post by SimpleSi » Oct 28th, '17, 20:39

If you'd like to work in this style and you'd like me to convert your current code to use instance variables and methods,
DIDN@T SEE 1st Post - so this is only in reply to 2nd one
I actually did it myself at one point but then switched back to shared vars and methods

I know this might sound daft or irrelevant but I REALLY don't like seeing "this" on blocks when its not needed

I have the exactly the same problem with "self" in Python classes

I feel that unless this or self is required in a class method - it should be optional and not used (in my opinion that is)

I'm going to really struggle to explain to Scratchers why they need "this" just to have a non-global method

Sorry
Last edited by SimpleSi on Oct 28th, '17, 20:42, edited 1 time in total.

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: GIF Reader

Post by SimpleSi » Oct 28th, '17, 20:41

I just wrote that last post without realising you'd written two in sequence - sorry

I'll have to wait till my brain is fully in gear to fully digest the first one and get back to you :)

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: GIF Reader

Post by JohnM » Oct 28th, '17, 20:54

Yes, I totally agree, it's hard to explain "this" and you don't actually want to see it either when defining a method or calling that method from within it's own class. But never fear, I've got a plan for a way to hide the "this" parameter when you don't need it. I just need a week or so to implement it and work through and unanticipated complications.

For now, I'd encourage you to try working with instance variables and methods. When I've implemented my scheme, your code render without all the "this" parameters, making it easier to read and edit.

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: GIF Reader

Post by SimpleSi » Oct 28th, '17, 21:12

But never fear, I've got a plan for a way to hide the "this" parameter when you don't need it
YIPPEE!!!!!!!

Do that and I'll take the vow never to use shared methods :)

Post Reply