-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.cpccDrawingToolsMacOnFocused.h
More file actions
211 lines (148 loc) · 8.23 KB
/
gui.cpccDrawingToolsMacOnFocused.h
File metadata and controls
211 lines (148 loc) · 8.23 KB
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
/* *****************************************
* File: gui.cpccDrawingToolsMacOnFocused.h
* Purpose: Portable (cross-platform), light-weight, graphic drawing tools
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2013 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* Download: https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#include "cpccColor.h"
#include "gui.cpccDrawingToolsAbstract.h"
#include "gui.cpccTextStylesMac.h"
// ////////////////////////////////////////////
// cpccDrawingToolsMacOnFocused
// Asume the proper destination has already a lockFocus
// ////////////////////////////////////////////
class cpccDrawingToolsMacOnFocused: public cpcciDrawingTools
{
public: // constructor
virtual ~cpccDrawingToolsMacOnFocused() {}
public: // data
public: // functions
virtual cpccColor getPixel(const int x, const int y) const override
{
/*
NSReadPixel(NSPoint passedPoint)
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Miscellaneous/AppKit_Functions/Reference/reference.html#//apple_ref/c/func/NSReadPixel
Because the passedPoint parameter is relative to the current coordinate system,
if you wish to read a pixel from a specific view, you must convert points in the view’s
coordinate system to the current coordinate system before calling this function.
Alternatively, you can lock focus on the view and then specify the pixel coordinate in the view’s coordinate system.
*/
// NSReadPixel pulls data out of the current focused graphics context,
NSColor * pixelColor = NSReadPixel(NSMakePoint(x, y));
cpccColor c;
// std::cout << "getPixel result:" << (int) c.getBrightness() << "\n";
c.fromNSColor(pixelColor);
return c;
}
virtual void setPixel(const int x, const int y, const cpccColor &aColor) override
{
// in OSX there is no pair to NSReadPixel, like the window's setPixel(x,y, color)
// I use a fillRect with size of 1x1 pixels
NSColor *drawColor = aColor.asNSColor();
[drawColor set];
NSRectFill(NSMakeRect(x, y, 1.0, 1.0));
}
void fillEllipseWithColor(const int left, const int top, const int right, const int bottom, const cpccColor& c) override
{
NSColor *fillColor = c.asNSColor();
[fillColor setFill];
NSRect ellipseRect = NSMakeRect(left, top, right-left, bottom-top);
[[NSBezierPath bezierPathWithOvalInRect:ellipseRect] fill];
}
virtual void fillRectWithColor(const cpccRecti &r, const cpccColor& aColor) override
{
[aColor.asNSColor() setFill];
NSRectFill(r.asNSRect());
}
virtual void fillRectWithGradientColorV(const cpccRecti &r, const cpccColor& aTopColor, const cpccColor& aBottomColor) override
{
NSGradient* theGradient = [[[NSGradient alloc]
initWithStartingColor:aTopColor.asNSColor()
endingColor:aBottomColor.asNSColor()] autorelease];
[theGradient drawInRect:r.asNSRect() angle:90.0];
}
void drawLine(const float x1, const float y1, const float x2, const float y2, const float width, const cpccColor &c) override
{
NSBezierPath * path = [NSBezierPath bezierPath];
[path setLineWidth: width];
NSPoint startPoint = { x1, y1 };
NSPoint endPoint = { x2, y2 };
[path moveToPoint: startPoint];
[path lineToPoint:endPoint];
[c.asNSColor() set];
[path stroke];
}
virtual void drawLine(const int x1, const int y1, const int x2, const int y2, const int width, const cpccColor &c) override
{
drawLine(static_cast<float>(x1), static_cast<float>(y1), static_cast<float>(x2), static_cast<float>(y2),
static_cast<float>(width), c);
//drawLine(static_cast<CGFloat>(x1), static_cast<CGFloat>(y1), static_cast<CGFloat>(x2), static_cast<CGFloat>(y2),
// static_cast<CGFloat>(width), c);
}
void drawText(int x, int y, const cpcc_char *text, const cpccTextParams& params) const override
{
// https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101
// std::cout << "textOut(" << x << " , " << y << " , "<< text << ")";
// Note: Stroke and fill colors do not affect the appearance of text.
// To apply color to text, you must change the attributes associated with the text
// https://developer.apple.com/library/mac/documentation/cocoa/conceptual/CocoaDrawingGuide/Color/Color.html
NSString *macString = [[[NSString alloc] initWithUTF8String:text] autorelease];
// the following works with ascii text (not greek text)
//NSString *macString = [[[NSString alloc] initWithCString:text encoding:NSASCIIStringEncoding] autorelease];
NSMutableDictionary *textAttrib = [[[NSMutableDictionary alloc] init] autorelease];
cpccTextStylesMac styleHelper;
if (params.kerning)
styleHelper.setKerning(textAttrib, *params.kerning);
if ((params.fontName) || (params.fontSize))
styleHelper.setFont(textAttrib, (params.fontName)? params.fontName : "Arial", (params.fontSize)? *params.fontSize : 10.0f);
styleHelper.setColor(textAttrib, (params.color)? params.color : &cpccBurlyWood );
int align = (params.textAlign) ? *params.textAlign : -1;
styleHelper.setParagraph(textAttrib, align);
NSRect rect;
rect.size = [macString sizeWithAttributes: textAttrib];
rect.origin.y = y;
rect.origin.x = x;
if (align==0) // center
rect.origin.x -= rect.size.width/2;
if (align==1) // right
rect.origin.x -= rect.size.width;
bool prevState = [[NSGraphicsContext currentContext] shouldAntialias];
if ((params.fontQuality==fqNonAntiAliased) && (prevState==YES))
[[NSGraphicsContext currentContext] setShouldAntialias: NO];
if (!params.ignoreRetina)
[macString drawInRect:rect /* respectFlipped:YES */ withAttributes:textAttrib];
else
{
NSAffineTransform *trans = [[[NSAffineTransform alloc] init] autorelease];
[trans set];
[macString drawInRect:rect /* respectFlipped:YES */ withAttributes:textAttrib];
}
if ((params.fontQuality==fqNonAntiAliased) && (prevState==YES))
[[NSGraphicsContext currentContext] setShouldAntialias: YES];
}
virtual void getTextSize(const cpcc_char *txt, const cpccTextParams& params, int *width, int *height) const override
{
// If you give -[NSString initWithUTF8String:] a C-string that's not valid UTF-8, it returns nil.
// NSString *macString = [[[NSString alloc] initWithUTF8String:txt] autorelease];
NSString *macString = [[[NSString alloc] initWithCString:txt encoding:NSASCIIStringEncoding] autorelease];
NSMutableDictionary *textAttrib = [[[NSMutableDictionary alloc] init] autorelease];
cpccTextStylesMac styleHelper;
if (params.kerning)
styleHelper.setKerning(textAttrib, *params.kerning);
if ((params.fontName) || (params.fontSize))
styleHelper.setFont(textAttrib, (params.fontName)? params.fontName : "Arial", (params.fontSize)? *params.fontSize : 10.0f);
NSSize tmpSize = [macString sizeWithAttributes: textAttrib];
if (width)
*width =tmpSize.width;
if (height)
*height =tmpSize.height;
}
};