1 /* 2 Drawpile - a collaborative drawing program. 3 4 Copyright (C) 2018 Calle Laakkonen 5 6 Drawpile is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 Drawpile is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with Drawpile. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 #ifndef BRUSHES_PIXELBRUSHSTATE_H 20 #define BRUSHES_PIXELBRUSHSTATE_H 21 22 #include "brush.h" 23 #include "brushstate.h" 24 #include "core/point.h" 25 #include "../libshared/net/brushes.h" 26 27 namespace paintcore { 28 class LayerStack; 29 class Layer; 30 struct BrushStamp; 31 } 32 33 namespace brushes { 34 35 /** 36 * @brief Pixel brush engine 37 * 38 * This class keeps track of a brush stroke's state and generates 39 * DrawDabs commands. 40 */ 41 class PixelBrushState : public BrushState { 42 public: 43 PixelBrushState(); 44 45 /** 46 * @brief Set the context (user) ID 47 * @param id 48 */ setContextId(int id)49 void setContextId(int id) { m_contextId = id; } 50 51 /** 52 * @brief Set the brush parameters 53 */ 54 void setBrush(const ClassicBrush &brush); 55 56 /** 57 * @brief Set the target layer 58 * @param id layer ID 59 */ setLayer(int id)60 void setLayer(int id) { m_layerId = id; } 61 62 /** 63 * @brief Start or continue a stroke 64 * @param sourceLayer unused 65 */ 66 void strokeTo(const paintcore::Point &p, const paintcore::Layer *sourceLayer) override; 67 68 /** 69 * @brief End the active stroke 70 */ 71 void endStroke() override; 72 73 /** 74 * @brief Take the current DrawDab commands 75 * 76 * This clears the dab buffer but does not end the 77 * stroke. 78 * 79 * @return list of DrawDab commands generated so far 80 */ takeDabs()81 protocol::MessageList takeDabs() override { 82 auto dabs = m_dabs; 83 m_dabs = protocol::MessageList(); 84 m_lastDab = nullptr; 85 return dabs; 86 } 87 88 void addOffset(int x, int y) override; 89 90 private: 91 void addDab(int x, int y, qreal pressure, const paintcore::Layer *sourceLayer); 92 93 ClassicBrush m_brush; // the current brush 94 int m_contextId; // user context ID 95 int m_layerId; // target layer ID 96 qreal m_length; // current length of active brush stroke 97 int m_smudgeDistance; // dabs since last smudge color sampling 98 QColor m_smudgedColor; // effective color (nonzero alpha indicates indirect drawing mode) 99 bool m_pendown; // brush stroke in progress? 100 paintcore::Point m_lastPoint; 101 102 protocol::MessageList m_dabs; 103 protocol::DrawDabsPixel *m_lastDab; 104 int m_lastDabX; 105 int m_lastDabY; 106 }; 107 108 } 109 110 #endif 111