forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.h
217 lines (176 loc) · 6.23 KB
/
graphics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Aseprite UI Library
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef UI_GRAPHICS_H_INCLUDED
#define UI_GRAPHICS_H_INCLUDED
#pragma once
#include "base/disable_copying.h"
#include "base/string.h"
#include "gfx/color.h"
#include "gfx/point.h"
#include "gfx/rect.h"
#include "gfx/size.h"
#include "os/paint.h"
#include <memory>
#include <string>
namespace gfx {
class Matrix;
class Path;
class Region;
}
namespace os {
class DrawTextDelegate;
class Font;
class Surface;
}
namespace ui {
using os::Paint;
// Class to render a widget in the screen.
class Graphics {
public:
enum class DrawMode {
Solid,
Xor,
Checked,
};
Graphics(os::Surface* surface, int dx, int dy);
~Graphics();
int width() const;
int height() const;
os::Surface* getInternalSurface() { return m_surface; }
int getInternalDeltaX() { return m_dx; }
int getInternalDeltaY() { return m_dy; }
int getSaveCount() const;
gfx::Rect getClipBounds() const;
void saveClip();
void restoreClip();
bool clipRect(const gfx::Rect& rc);
void save();
void concat(const gfx::Matrix& matrix);
void setMatrix(const gfx::Matrix& matrix);
void resetMatrix();
void restore();
gfx::Matrix matrix() const;
void setDrawMode(DrawMode mode, int param = 0,
const gfx::Color a = gfx::ColorNone,
const gfx::Color b = gfx::ColorNone);
gfx::Color getPixel(int x, int y);
void putPixel(gfx::Color color, int x, int y);
void drawHLine(gfx::Color color, int x, int y, int w);
void drawVLine(gfx::Color color, int x, int y, int h);
void drawLine(gfx::Color color, const gfx::Point& a, const gfx::Point& b);
void drawPath(gfx::Path& path, const Paint& paint);
void drawRect(gfx::Color color, const gfx::Rect& rc);
void fillRect(gfx::Color color, const gfx::Rect& rc);
void fillRegion(gfx::Color color, const gfx::Region& rgn);
void fillAreaBetweenRects(gfx::Color color,
const gfx::Rect& outer, const gfx::Rect& inner);
void drawSurface(os::Surface* surface, int x, int y);
void drawSurface(os::Surface* surface,
const gfx::Rect& srcRect,
const gfx::Rect& dstRect);
void drawRgbaSurface(os::Surface* surface, int x, int y);
void drawRgbaSurface(os::Surface* surface, int srcx, int srcy, int dstx, int dsty, int w, int h);
void drawRgbaSurface(os::Surface* surface,
const gfx::Rect& srcRect,
const gfx::Rect& dstRect);
void drawColoredRgbaSurface(os::Surface* surface, gfx::Color color, int x, int y);
void drawColoredRgbaSurface(os::Surface* surface, gfx::Color color, int srcx, int srcy, int dstx, int dsty, int w, int h);
void drawSurfaceNine(os::Surface* surface,
const gfx::Rect& src,
const gfx::Rect& center,
const gfx::Rect& dst,
const Paint* paint = nullptr);
void blit(os::Surface* src, int srcx, int srcy, int dstx, int dsty, int w, int h);
// ======================================================================
// FONT & TEXT
// ======================================================================
os::Font* font() { return m_font; }
void setFont(os::Font* font);
void drawText(base::utf8_const_iterator it,
const base::utf8_const_iterator& end,
gfx::Color fg, gfx::Color bg, const gfx::Point& pt,
os::DrawTextDelegate* delegate);
void drawText(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Point& pt);
void drawUIText(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Point& pt, const int mnemonic);
void drawAlignedUIText(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Rect& rc, const int align);
gfx::Size measureUIText(const std::string& str);
static int measureUITextLength(const std::string& str, os::Font* font);
gfx::Size fitString(const std::string& str, int maxWidth, int align);
private:
gfx::Size doUIStringAlgorithm(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Rect& rc, int align, bool draw);
void dirty(const gfx::Rect& bounds);
os::Surface* m_surface;
int m_dx;
int m_dy;
gfx::Rect m_clipBounds;
os::Font* m_font;
gfx::Rect m_dirtyBounds;
};
// Class to draw directly in the screen.
class ScreenGraphics : public Graphics {
public:
ScreenGraphics();
virtual ~ScreenGraphics();
};
// Class to temporary set the Graphics' clip region to the full
// extend.
class SetClip {
public:
SetClip(Graphics* g)
: m_graphics(g)
, m_oldClip(g->getClipBounds())
, m_oldCount(g->getSaveCount()) {
if (m_oldCount > 1)
m_graphics->restoreClip();
}
~SetClip() {
if (m_oldCount > 1) {
m_graphics->saveClip();
m_graphics->clipRect(m_oldClip);
}
}
private:
Graphics* m_graphics;
gfx::Rect m_oldClip;
int m_oldCount;
DISABLE_COPYING(SetClip);
};
// Class to temporary set the Graphics' clip region to a sub-rectangle
// (in the life-time of the IntersectClip instance).
class IntersectClip {
public:
IntersectClip(Graphics* g, const gfx::Rect& rc)
: m_graphics(g) {
m_graphics->saveClip();
m_notEmpty = m_graphics->clipRect(rc);
}
~IntersectClip() {
m_graphics->restoreClip();
}
operator bool() const { return m_notEmpty; }
private:
Graphics* m_graphics;
bool m_notEmpty;
DISABLE_COPYING(IntersectClip);
};
class CheckedDrawMode {
public:
CheckedDrawMode(Graphics* g, int param,
const gfx::Color a,
const gfx::Color b) : m_graphics(g) {
m_graphics->setDrawMode(Graphics::DrawMode::Checked, param, a, b);
}
~CheckedDrawMode() {
m_graphics->setDrawMode(Graphics::DrawMode::Solid);
}
private:
Graphics* m_graphics;
DISABLE_COPYING(CheckedDrawMode);
};
typedef std::shared_ptr<Graphics> GraphicsPtr;
} // namespace ui
#endif