#pragma once
#include "Gui/BasicPainter.h"
#include "WindowGraphics.h"

namespace gui {

	/**
	 * Object responsible for painting window contents. Create a subclass and override
	 * 'render'. Then attach it to a Window.
	 */
	class Painter : public BasicPainter {
		STORM_CLASS;
	public:
		STORM_CTOR Painter();

		virtual ~Painter();

		// Called to render the window. Before this call, the output is filled with `bgColor`.
		// `size` is the drawing size in device independent units.
		// Return `true` to schedule a repaint for the next frame.
		virtual Bool STORM_FN render(Size size, Graphics *graphics);

		// Background color. Updated on next redraw.
		Color bgColor;

		// Render a picture to a Bitmap. This is not necessarily a fast operation and is therefore
		// not suitable for real-time rendering. Rather, it is intended to provide the ability to
		// save a picture of a part of the screen to a file. Note that the painter needs to be
		// attached to a window for this to work.
		Image *STORM_FN captureImage(Size size, Color bgColor);
		using BasicPainter::captureImage;

	protected:
		// Surface type.
		virtual SurfaceType surfaceType() { return surface2D; }

		// Callback for rendering.
		virtual void render(Surface *surface, Bool &more, Bool &ok);

		// Attach, create WindowGraphics.
		virtual void onAttach(Surface *surface);

		// Destroy our WindowGraphics.
		virtual void onDestroy();

		// Called when the underlying surface was resized.
		virtual void onResize();

	private:
		// Graphics object. Associated to the surface.
		WindowGraphics *graphics;

		// Helper to capture an image with a custom background color.
		static Bool captureImageFn(BasicPainter *p, Surface *surface, void *data);
	};

}
