Clearing a drawArea in haskell GTK - haskell

I'm making a game using gtk/cairo in haskell, and I'm looking for a way to completely clear the drawing area (a widget of type drawingArea). When a widget resizes for instance, the drawing area is cleared automatically. I'm looking for a way to clear it manually.
Any suggestions?

First get the DrawWindow of the DrawingArea using the function widgetGetDrawWindow, then call drawWindowClear with the DrawWindow as the argument.

Related

Make canvas background transparent - python

So I am making a Frogger game, but have run into a problem. For the collision detection, I am using the following to check if one Tkinter canvas object is overlapping another:
canvas.find_overlapping(*canvas.bbox(imageObj))
However, I made the background a canvas object as well:
background = self.canvas.create_image(0, 0, image = self.imageData["Background"], anchor = "nw")
So the program is detecting a collision between the player and an object 24/7. Is there any way around this? I searched SO and tried putting the background in a label, but when I packed the canvas over it the background disappeared (probably because the canvas was covering it).
I can't find a way to make the canvas transparent without making the objects on it transparent as well. I also do not want to calculate the x and y boxes of each object, as that is just cumbersome and unreliable.
If someone could suggest another way, that would be awesome.
The find_overlapping method returns a list of items. Just cycle through the list and ignore the background item.

Getting mouse coordinates in Haskell FLTK

I'm trying to load an image into a fltkhs box widget and do some image processing on it and I'd like to get the coordinates when the mouse click on a point on the image.
I couldn't find any part of fltkhs that can handle mouse events.
Anyone could give any suggestions?
Glancing through the documentation yielded this page:
https://hackage.haskell.org/package/fltkhs-0.5.4.1/docs/Graphics-UI-FLTK-LowLevel-FL.html
It has a function with the following signature:
getMouse :: IO Position
I suppose it's FLTKHS's analogue for get_mouse

Anyone know if its possible draw to erase the picture on the top?

Scratch allows selection of the color using the set pen color. Does anyone know if its possible to program to set the color to transparent so that what ever is drawn can effectively be erased? What would the color number be for that?
Update
The idea was to cover a picture (background) with a colour drawn over the top. Now give the player a little creature that erases the colour on top, they have a certain amount of moves or time to guess what the picture is. The less moves/time they use the more points the player will be awarded.
But the problem seems to be that in the paint a sprite part of scratch erase is an option, but not in the pen programming.
If I cant solve it using erase apprach, my alternative is to make a lot of sprites covering the picture and hide them when the creature touches them. But it seems less fun for the player as the uncovered patterns will be more predictable.
Unfortunately, this isn't really possible. If you have a backdrop that's all one color, you could set the pen color to be the same as the backdrop and color over what you already have (giving the illusion of erasing), but other than that there really isn't a good way.
Your only other option is to use the clear block and then re-draw everything except the piece you want to erase.
If you want to give more context about your specific project, I might be able to help you work out a solution (I've done quite a lot with pen blocks over the years).
Like PullJosh said, it isn't really possible but you can make a sprite called Cover and make it a circle of any color you want. Then, it can clone it's sprite over the sprite you want to hide. Then you can attach a script to the clones:
https://i.stack.imgur.com/S6h4c.png (ctrl + click)

What is the right way to render Cairo updates in Gtk2hs?

I'm writing a Haskell program with click-and-drag functionality, so with each mouse movement event an update gets painted to the window. At the moment I'm using
renderWithDrawable myCanvas update
However this is flickering a lot. My understanding is that I need to create a separate drawable (a "surface"?), render to that, and then blit it onto the screen window in a single operation. However I'm confused as to the right way to do this.
I've found drawWindowBeginPaintRegion, which talks about eliminating flicker.
However it is removed in Gtk3 according to the Haddock docs. So I'm not sure if I should use this, as it seems to be deprecated.
I've also found renderWithSimilarSurface in Cairo, which seems to do something similar.
I'm also not sure how these functions relate to renderWithDrawable: do I have to use them inside that function, or what?
What is the right way to do this?
Edit
This seems to be a known thing in Cairo. I'm trying to figure out how to handle this in Haskell.
Explanation and solution in Python
Description of how to handle this issue in raw C
The right way to do this is to make sure all your drawing comes from within expose events, and operates on the draw window provided by the event. You can mark a region as "dirty" and trigger a synthetic expose event using drawWindowInvalidateRect, drawWindowInvalidateRegion, or widgetQueueDraw.
A quick worked example of setting up the drawing pipeline follows. It is excerpted from a custom Viewport type, which does Google-maps style panning with smooth motions on drag-and-release operations, that I built for a side-project some time ago. To support that, it has to redraw on mouse motion events, so it addresses a similar use case to your described problem. I've elided irrelevant stuff with ... to highlight the important bits. I've uploaded the complete project to github just now, so you can browse the repo to see the full details of Viewport. (It's been years though, so there's probably a fair bit of bitrot -- don't expect the project to just build and run with modern GHCs/packages.)
viewportNew :: Viewport -> IO DrawingArea
viewportNew v = do
da <- drawingAreaNew
-- ...
on da exposeEvent $ exposeViewport posRef (draw v)
-- ...
exposeViewport :: IORef Position -> RegionRenderer -> EventM EExpose Bool
exposeViewport posRef draw = do
dw <- eventWindow
region <- eventRegion >>= liftIO . regionGetRectangles
-- ...
liftIO . renderWithDrawable dw $ do
-- Cairo () action goes here
-- can reference region to decide which things to draw
draw region
return True -- see documentation of exposeEvent for what this means
This template should take advantage of gtk's built-in double-buffering and work with both the gtk and gtk3 packages.

Java 8/JavaFX: Drawing text in a rectangle

Is there a way to bind text to a JavaFX shape (for my purposes, I want to use rectangles) the same way you could using Graphics2D's drawString in previous versions of Java?
You may be able to just use Graphics2D still; I just don't know how to implement it.
Note: What isn't helpful is a way to place a Text object at the same x and y position as the shape on canvas. Rather, is there a way to actually write onto the Rectangle?

Resources