1 // Aseprite
2 // Copyright (C) 2017  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_SPRITE_JOB_H_INCLUDED
8 #define APP_SPRITE_JOB_H_INCLUDED
9 #pragma once
10 
11 #include "app/context.h"
12 #include "app/context_access.h"
13 #include "app/job.h"
14 #include "app/transaction.h"
15 #include "render/task_delegate.h"
16 
17 #include <functional>
18 
19 namespace app {
20 
21 class SpriteJob : public Job,
22                   public render::TaskDelegate {
23 public:
24   SpriteJob(const ContextReader& reader, const char* jobName);
25   ~SpriteJob();
26 
writer()27   ContextWriter& writer() { return m_writer; }
document()28   Doc* document() const { return m_document; }
sprite()29   Sprite* sprite() const { return m_sprite; }
transaction()30   Transaction& transaction() { return m_transaction; }
31 
32   template<typename T>
startJobWithCallback(T && callback)33   void startJobWithCallback(T&& callback) {
34     m_callback = std::move(callback);
35     Job::startJob();
36   }
37 
38 private:
39   // Job impl
40   void onJob() override;
41 
42   // render::TaskDelegate impl just in case you need to use this
43   // Job as a delegate in render::create_palette_from_sprite()
44   bool continueTask() override;
45   void notifyTaskProgress(double progress) override;
46 
47   ContextWriter m_writer;
48   Doc* m_document;
49   Sprite* m_sprite;
50   Transaction m_transaction;
51 
52   // Default implementation calls the given function in
53   // startJob(). Anyway you can just extended the SpriteJob and
54   // override onJob().
55   std::function<void()> m_callback;
56 };
57 
58 } // namespace app
59 
60 #endif // APP_SPRITE_JOB_H_INCLUDED
61