1 #pragma once
2 
3 #ifndef TIMAGESTYLES_H
4 #define TIMAGESTYLES_H
5 
6 // TnzCore includes
7 #include "tsimplecolorstyles.h"
8 #include "tlevel.h"
9 #include "traster.h"
10 #include "tstrokeoutline.h"
11 
12 // Qt includes
13 #include <QCoreApplication>
14 
15 #undef DVAPI
16 #undef DVVAR
17 
18 #ifdef TOONZLIB_EXPORTS
19 #define DVAPI DV_EXPORT_API
20 #define DVVAR DV_EXPORT_VAR
21 #else
22 #define DVAPI DV_IMPORT_API
23 #define DVVAR DV_IMPORT_VAR
24 #endif
25 
26 class ToonzScene;
27 
28 //**********************************************************************************
29 //    TTextureStyle  declaration
30 //**********************************************************************************
31 class TTextureParams {
32 public:
33   enum TYPE {
34     FIXED = 0,  // texture is applied fixed respect the image raster borders.
35     AUTOMATIC,  // texture is centered on the centroid of the filled area to
36                 // texture
37     RANDOM
38   };
39 
40   TYPE m_type;
41   double m_scale;
42   double m_rotation;
43   TPointD m_displacement;
44   double m_contrast;
45   bool m_isPattern;
46   TPixel32 m_patternColor;
47 
TTextureParams()48   TTextureParams()
49       : m_type(AUTOMATIC)
50       , m_scale(1.0)
51       , m_rotation(0.0)
52       , m_displacement()
53       , m_contrast(1.0)
54       , m_isPattern(false)
55       , m_patternColor(TPixel::White) {}
56 
57   bool operator==(const TTextureParams &p) const {
58     if (m_type != p.m_type) return false;
59     if (m_scale != p.m_scale) return false;
60     if (m_rotation != p.m_rotation) return false;
61     if (m_displacement != p.m_displacement) return false;
62     if (m_contrast != p.m_contrast) return false;
63     if (m_isPattern != p.m_isPattern) return false;
64     if (m_patternColor != p.m_patternColor) return false;
65     return true;
66   }
67 };
68 
69 class DVAPI TImageStyle {
70 protected:
71   static TFilePath m_libraryDir;
72   static ToonzScene *m_currentScene;
73 
74 public:
TImageStyle()75   TImageStyle(){};
setLibraryDir(const TFilePath & fp)76   static void setLibraryDir(const TFilePath &fp) { m_libraryDir = fp; }
setCurrentScene(ToonzScene * currentScene)77   static void setCurrentScene(ToonzScene *currentScene) {
78     m_currentScene = currentScene;
79   }
80 };
81 
82 //--------------------------------------------------------------------------------------------------------
83 
84 class DVAPI TTextureStyle final : public TOutlineStyle,
85                                   TRasterStyleFx,
86                                   TImageStyle {
87 public:
88 private:
89   TTextureParams m_params;
90   TRaster32P m_texture;
91   TFilePath m_texturePath, m_texturePathLoaded;
92 
93   TTessellator *m_tessellator;
94   TPixel32 m_averageColor;
95 
96 protected:
97   void makeIcon(const TDimension &d) override;
98 
99   void loadData(TInputStreamInterface &) override;
100   void saveData(TOutputStreamInterface &) const override;
101 
102 public:
103   // TTextureStyle();
104   // TTextureStyle(const TFilePath &path);
105   TTextureStyle(const TRasterP &ras, const TFilePath &m_texturePath);
106   TTextureStyle(const TTextureStyle &);
107 
108   ~TTextureStyle();
109 
110   int getParamCount() const override;
111   TColorStyle::ParamType getParamType(int index) const override;
112   QString getParamNames(int index) const override;
113   void getParamRange(int index, double &min, double &max) const override;
114   double getParamValue(TColorStyle::double_tag, int index) const override;
115 
116   void getParamRange(int index, QStringList &enumItems) const override;
117   bool getParamValue(TColorStyle::bool_tag, int index) const override;
118   int getParamValue(TColorStyle::int_tag, int index) const override;
119   TFilePath getParamValue(TColorStyle::TFilePath_tag, int index) const override;
isPattern()120   bool isPattern() const { return m_params.m_isPattern; }
121   static void fillCustomTextureIcon(const TRaster32P &r);
122 
123   //-------------------------------------------------------------------
124 
125   void setParamValue(int index, const TFilePath &value) override;
126   void setParamValue(int index, double value) override;
127   void setParamValue(int index, bool value) override;
128   void setParamValue(int index, int value) override;
129 
130   int getColorParamCount() const override;
131   TPixel32 getColorParamValue(int index) const override;
132   void setColorParamValue(int index, const TPixel32 &color) override;
133 
134   TColorStyle *clone() const override;
135   QString getDescription() const override;
136 
hasMainColor()137   bool hasMainColor() const override { return true; }
getMainColor()138   TPixel32 getMainColor() const override { return m_averageColor; }
setMainColor(const TPixel32 & color)139   void setMainColor(const TPixel32 &color) override {
140     m_params.m_patternColor = color;
141   }
142 
143   // void draw(const TVectorRenderData &rd,  TStrokeOutline* outline) const;
144   void drawRegion(const TColorFunction *cf, const bool antiAliasing,
145                   TRegionOutline &outline) const override;
146   // void drawRegion( const TVectorRenderData &rd, TRegionOutline &boundary )
147   // const;
148   void drawStroke(const TColorFunction *cf, TStrokeOutline *outline,
149                   const TStroke *stroke) const override;
150 
getRasterStyleFx()151   TRasterStyleFx *getRasterStyleFx() override { return this; }
152 
isRasterStyle()153   bool isRasterStyle() const override { return true; }
154 
155   void computeOutline(const TStroke *stroke, TStrokeOutline &outline,
156                       TOutlineUtil::OutlineParameter param) const override;
157 
158   void setTexture(const TRasterP &color);
159   TRasterP getTexture() const;
160 
161   TPixel32 getAverageColor() const override;
162 
163   int getTagId() const override;
164 
165   // static TRaster32P loadTexture(const TFilePath &path);
166 
isPaintStyle()167   bool isPaintStyle() const override { return true; }
isInkStyle()168   bool isInkStyle() const override { return true; }
inkFxNeedRGBMRaster()169   bool inkFxNeedRGBMRaster() const override { return true; }
170 
compute(const Params & params)171   bool compute(const Params &params) const override {
172     return doCompute(params);
173   }  // faccio questo per compilare su mac! le virtuali pubbliche devono essere
174      // inline
175 
176 private:
177   bool doCompute(const Params &params) const;
178   void setAverageColor();
179   bool loadTextureRaster();
180   TRaster32P loadTextureRasterWithFrame(int frame) const;
181   // Not assignable
182   TTextureStyle &operator=(const TTextureStyle &);
183 };
184 
185 #endif
186