1 #pragma once 2 3 #ifndef TCOLORSTYLES_H 4 #define TCOLORSTYLES_H 5 6 // TnzCore includes 7 #include "tfilepath.h" 8 #include "traster.h" 9 10 // Qt includes 11 #include <QString> 12 13 #undef DVAPI 14 #undef DVVAR 15 16 #ifdef TVRENDER_EXPORTS 17 #define DVAPI DV_EXPORT_API 18 #define DVVAR DV_EXPORT_VAR 19 #else 20 #define DVAPI DV_IMPORT_API 21 #define DVVAR DV_IMPORT_VAR 22 #endif 23 24 //================================================= 25 26 // Forward declarations 27 28 class TStroke; 29 class TRegion; 30 class TStrokeProp; 31 class TRegionProp; 32 class TVectorRenderData; 33 class TInputStreamInterface; 34 class TOutputStreamInterface; 35 class TRasterStyleFx; 36 class QStringList; 37 38 //================================================= 39 40 //************************************************************************ 41 // TRasterStyleFx definition 42 //************************************************************************ 43 44 class DVAPI TRasterStyleFx { 45 public: 46 struct Params { 47 TRasterP m_r; 48 TPoint m_p; 49 TRasterP m_rOrig; 50 int m_colorIndex; 51 int m_frame; 52 53 public: ParamsParams54 Params(const TRasterP &r, const TPoint &p, const TRasterP &rOrig, int index, 55 int frame) 56 : m_r(r), m_p(p), m_rOrig(rOrig), m_colorIndex(index), m_frame(frame) { 57 assert(m_r); 58 } 59 }; 60 61 public: ~TRasterStyleFx()62 virtual ~TRasterStyleFx() {} 63 64 virtual bool isInkStyle() const = 0; 65 virtual bool isPaintStyle() const = 0; 66 inkFxNeedRGBMRaster()67 virtual bool inkFxNeedRGBMRaster() const { return false; } 68 69 virtual bool compute(const Params ¶ms) const = 0; 70 getEnlargement(int & borderIn,int & borderOut)71 virtual void getEnlargement(int &borderIn, int &borderOut) const { 72 borderIn = borderOut = 0; 73 } 74 }; 75 76 //************************************************************************ 77 // TColorStyle definition 78 //************************************************************************ 79 80 /*! 81 \brief Abstract class representing a color style in a Toonz palette. 82 */ 83 84 class DVAPI TColorStyle : public TSmartObject { 85 public: 86 //! Helper class to declare color styles 87 class Declaration { 88 public: Declaration(TColorStyle * style)89 Declaration(TColorStyle *style) { declare(style); } 90 }; 91 92 enum ParamType { 93 BOOL, //!< Boolean parameter type. 94 INT, //!< Integer parameter type. 95 ENUM, //!< Enum parameter type (maps to integer values). 96 DOUBLE, //!< Double parameter type (getParamType() default). 97 FILEPATH //!< TFilePath parameter type. 98 }; 99 100 struct double_tag {}; 101 struct bool_tag {}; 102 struct int_tag {}; 103 struct TFilePath_tag {}; 104 105 struct PickedPosition { 106 TPoint pos = TPoint(); 107 int frame = 0; 108 PickedPosition(TPoint _pos = TPoint(), int _frame = 0) posPickedPosition109 : pos(_pos), frame(_frame) {} 110 inline bool operator==(const PickedPosition &p) const { 111 return (this->pos == p.pos) && (this->frame == p.frame); 112 } 113 inline bool operator!=(const PickedPosition &p) const { 114 return (this->pos != p.pos) || (this->frame != p.frame); 115 } 116 }; 117 118 private: 119 std::wstring m_name; //!< User-define style name. 120 std::wstring m_globalName; //!< User-define style \a global name. 121 std::wstring m_originalName; //!< If the style is copied from studio palette, 122 //! its original name is stored 123 124 int m_versionNumber; //!< Style's version number. 125 126 unsigned int m_flags; //!< Style attributes. 127 bool m_enabled; //!< Style's \a enabled status. 128 129 bool m_isEditedFromOriginal; //<! If the style is copied from studio palette, 130 // This flag will be set when the 131 //! style is edited from the original one. 132 PickedPosition 133 m_pickedPosition; // picked position from color model by using style 134 // picker tool with "organize palette" option. 135 136 protected: 137 TRaster32P m_icon; //!< Icon shown on TPalette viewers. 138 bool m_validIcon; //!< Icon's validity status. 139 140 public: 141 static int m_currentFrame; //!< Time instant in the palette's timeline. 142 //! \deprecated Should be done better 143 public: 144 TColorStyle(); 145 TColorStyle( 146 const TColorStyle &other); //!< Copies another style \a except its icon 147 virtual ~TColorStyle(); 148 149 virtual TColorStyle *clone() const = 0; //!< Polymorphic clone of the style. copy(const TColorStyle & other)150 virtual TColorStyle ©( 151 const TColorStyle &other) //!< Polymorphic copy of the style. 152 { 153 assignBlend(other, other, 0.0); 154 return *this; 155 } 156 157 bool operator==(const TColorStyle &cs) const; 158 bool operator!=(const TColorStyle &cs) { return !operator==(cs); } 159 160 // Renderizer-related objects 161 162 virtual TStrokeProp *makeStrokeProp( 163 const TStroke 164 *stroke) = 0; //!< Allocates a \a new derived TStrokeProp instance 165 //! used to draw the style on the specified stroke object. 166 167 virtual TRegionProp *makeRegionProp( 168 const TRegion 169 *region) = 0; //!< Allocates a \a new derived TRegionProp instance 170 //! used to draw the style on the specified region object. 171 isRasterStyle()172 virtual bool isRasterStyle() const { 173 return false; 174 } //!< Returns whether the style is of the raster kind. getRasterStyleFx()175 virtual TRasterStyleFx *getRasterStyleFx() { 176 return 0; 177 } //!< If the style contains raster effects it must return it, 178 //! else returns 0. 179 180 virtual bool isRegionStyle() 181 const = 0; //!< Returns whether the style applies on regions. 182 virtual bool isStrokeStyle() 183 const = 0; //!< Returns whether the style applies on strokes. 184 185 // General functions 186 setName(std::wstring name)187 void setName(std::wstring name) { 188 m_name = name; 189 } //!< Sets the style's user-defined name. getName()190 std::wstring getName() const { 191 return m_name; 192 } //!< Returns the style's user-defined name. 193 194 /*! \detail 195 The \a global name contains information about palette id. 196 */ setGlobalName(std::wstring name)197 void setGlobalName(std::wstring name) { 198 m_globalName = name; 199 } //!< Sets the global name of the style. getGlobalName()200 std::wstring getGlobalName() const { 201 return m_globalName; 202 } //!< Returns the global name of the style. 203 setOriginalName(std::wstring name)204 void setOriginalName(std::wstring name) { 205 m_originalName = name; 206 } //!< If the style is originally copied from studio palette, set this. getOriginalName()207 std::wstring getOriginalName() const { 208 return m_originalName; 209 } //!< Returns the original name of the style. 210 setIsEditedFlag(bool edited)211 void setIsEditedFlag(bool edited) { 212 m_isEditedFromOriginal = edited; 213 } //<! If the style is copied from studio palette, This flag will be set when 214 // the 215 //! style is edited from the original one. getIsEditedFlag()216 bool getIsEditedFlag() const { 217 return m_isEditedFromOriginal; 218 } //!< Returns whether the style is edited from the original from studio 219 //! palette 220 221 void assignNames( 222 const TColorStyle *src); //!< Copies the style names from src. 223 224 //! \sa tcolorflags.h getFlags()225 unsigned int getFlags() const { return m_flags; } //!< Sets color attributes. setFlags(unsigned int flags)226 void setFlags(unsigned int flags) { 227 m_flags = flags; 228 } //!< Returns color attributes. 229 230 void setPickedPosition(const TPoint &pos, const int index = 0) { 231 m_pickedPosition.pos = pos; 232 m_pickedPosition.frame = index; 233 } setPickedPosition(const PickedPosition & pos)234 void setPickedPosition(const PickedPosition &pos) { m_pickedPosition = pos; } getPickedPosition()235 PickedPosition getPickedPosition() const { return m_pickedPosition; } 236 237 // Color-related functions 238 239 /*! \detail 240 Raster or vector pattern image styles, for example, return false. 241 */ hasMainColor()242 virtual bool hasMainColor() const { 243 return false; 244 } //!< Returns whether the style has a reference \a main color. getMainColor()245 virtual TPixel32 getMainColor() const { 246 return TPixel32::Black; 247 } //!< Returns the style's main color, or black if none. setMainColor(const TPixel32 & color)248 virtual void setMainColor(const TPixel32 &color) { 249 } //!< Sets the main color, if it has one. 250 251 /*! \detail 252 For example a bubble styles has two colors. 253 By default it returns \p 1 if the style has only a main color else 254 returns \p 0 if the style has no color. 255 */ getColorParamCount()256 virtual int getColorParamCount() const { 257 return hasMainColor() ? 1 : 0; 258 } //!< Returns the number of colors (if any) of the style. 259 260 /*! \detail 261 Returns the main color by default. 262 */ getColorParamValue(int index)263 virtual TPixel32 getColorParamValue(int index) const { 264 return getMainColor(); 265 } //!< Returns the value of the color of index \e index. setColorParamValue(int index,const TPixel32 & color)266 virtual void setColorParamValue(int index, const TPixel32 &color) { 267 setMainColor(color); 268 } //!< Sets the \a index-th color value. 269 getAverageColor()270 virtual TPixel32 getAverageColor() const { 271 return getMainColor(); 272 } //!< Returns a color representing the average of all the style's colors 273 274 // Parameters-related functions 275 276 /*! \detail 277 For example: a the general cleanup style has two parameters, brightness and 278 contrast and 279 a black cleanup style has in addition a black and white threshold, therefore 280 4 parameters. 281 */ getParamCount()282 virtual int getParamCount() const { 283 return 0; 284 } //!< Returns the number of parameters necessary 285 //! to describe the style. 286 getParamType(int paramIdx)287 virtual ParamType getParamType(int paramIdx) const { 288 assert(0 <= paramIdx && paramIdx < getParamCount()); 289 return DOUBLE; 290 } //!< Returns the type of the specified parameter. 291 hasParamDefault(int index)292 virtual bool hasParamDefault(int index) const { 293 return false; 294 } //!< Value of parameter can be reset to default. 295 setParamDefault(int index)296 virtual void setParamDefault(int index) { 297 assert(false); 298 } //!< Reset value of parameter to default. 299 isParamDefault(int index)300 virtual bool isParamDefault(int index) const { 301 return false; 302 } //!< Check if current value of parameter equals to default 303 setParamValue(int index,bool value)304 virtual void setParamValue(int index, bool value) { 305 assert(false); 306 } //!< Assigns a value to the specified \p bool parameter. getParamValue(bool_tag,int index)307 virtual bool getParamValue(bool_tag, int index) const { 308 assert(false); 309 return bool(); 310 } //!< Retrieves the specified \p bool parameter value. 311 setParamValue(int index,int value)312 virtual void setParamValue(int index, int value) { 313 assert(false); 314 } //!< Assigns a value to the specified \p int parameter. getParamValue(int_tag,int index)315 virtual int getParamValue(int_tag, int index) const { 316 assert(false); 317 return int(); 318 } //!< Retrieves the specified \p int parameter value. getParamRange(int index,int & min,int & max)319 virtual void getParamRange(int index, int &min, int &max) const { 320 assert(false); 321 } //!< Retrieves the specified \p int parameter range. 322 getParamRange(int pIndex,QStringList & stringItems)323 virtual void getParamRange(int pIndex, QStringList &stringItems) const { 324 assert(false); 325 } //!< Returns a description of each supported enum or file 326 //! extension value in text form. setParamValue(int index,double value)327 virtual void setParamValue(int index, double value) { 328 assert(false); 329 } //!< Assigns a value to the specified \p double parameter. getParamValue(double_tag,int index)330 virtual double getParamValue(double_tag, int index) const { 331 assert(false); 332 return double(); 333 } //!< Retrieves the specified \p double parameter value. getParamRange(int index,double & min,double & max)334 virtual void getParamRange(int index, double &min, double &max) const { 335 assert(false); 336 } //!< Retrieves the specified \p double parameter range. 337 setParamValue(int index,const TFilePath & fp)338 virtual void setParamValue(int index, const TFilePath &fp) { 339 assert(false); 340 } //!< Assigns a value to the specified \p TFilePath parameter. getParamValue(TFilePath_tag,int index)341 virtual TFilePath getParamValue(TFilePath_tag, int index) const { 342 assert(false); 343 return TFilePath(); 344 } //!< Retrieves the specified \p TFilePath parameter value. 345 346 /*! \detail 347 This function is provided to animate a palette style. Given two <I>keyframe 348 styles</I> \p a and \p b, it calculates their linear interpolation at parameter 349 \p t and assigns the result to this style instance. 350 351 \param a Style value at parameter <TT>t = 0</TT>. 352 \param b Style value at parameter <TT>t = 1</TT>. 353 \param t Interpolation parameter. 354 */ 355 void assignBlend(const TColorStyle &a, const TColorStyle &b, 356 double t); //!< Assigns the linear interpolation between the 357 //! supplied styles. 358 359 // Description-related functions 360 361 /*! \detail 362 For example for a stroke style we have "Constant", "Chain", "Rope", "Tulle", 363 etc... 364 */ 365 virtual QString getDescription() 366 const = 0; //!< Return a brief description of the style. 367 virtual QString getParamNames(int index) 368 const; //!< Return the string that identifies the \a index-th parameter. 369 370 // I/O-related functions 371 372 virtual int getTagId() 373 const = 0; //!< Returns an unique number representing the style. getObsoleteTagIds(std::vector<int> &)374 virtual void getObsoleteTagIds(std::vector<int> &) const {}; //!< \deprecated 375 376 void save(TOutputStreamInterface &) const; //!< Calls the local 377 //! implementation of saveData() 378 //! passing it also the name and 379 //! the tagId of the style. 380 static TColorStyle *load(TInputStreamInterface &); //!< Loads the style from 381 //! disk. Calls the local 382 //! implementation of 383 //! loadData(). 384 385 static TColorStyle *create( 386 int tagId); //!< Creates a new style with identifier equal to \a tagId. 387 388 static void declare( 389 TColorStyle *style); //!< Puts the style in the table of actual styles. 390 static void getAllTags(std::vector<int> &tags); 391 392 // Misc functions 393 394 /*! \detail 395 It is used when updates must be done after changes or creation of new styles. 396 */ invalidateIcon()397 void invalidateIcon() { 398 m_validIcon = false; 399 } //!< Sets a flag that defines the style's icon validity. 400 virtual const TRaster32P &getIcon( 401 const TDimension &d); //!< Returns an image representing the style. 402 isEnabled()403 bool isEnabled() const { 404 return m_enabled; 405 } //!< Returns whether the style can be used. enable(bool on)406 void enable(bool on) { 407 m_enabled = on; 408 } //!< Sets the style's \a enabled status 409 getVersionNumber()410 int getVersionNumber() const { 411 return m_versionNumber; 412 } //!< Returns the version number of the style. 413 414 protected: 415 virtual void makeIcon(const TDimension &d); 416 417 virtual void loadData(TInputStreamInterface &) = 0; 418 virtual void saveData(TOutputStreamInterface &) const = 0; 419 loadData(int,TInputStreamInterface &)420 virtual void loadData(int, TInputStreamInterface &) { 421 } //!< \deprecated Backward compatibility 422 423 void updateVersionNumber(); 424 425 protected: 426 TColorStyle &operator=( 427 const TColorStyle &other); //!< Copies another style \a except its icon 428 }; 429 430 //-------------------------------------------------------------------------- 431 432 #ifdef _WIN32 433 template class DVAPI TSmartPointerT<TColorStyle>; 434 #endif 435 typedef TSmartPointerT<TColorStyle> TColorStyleP; 436 437 #endif // TCOLORSTYLES_H 438