Skip to content

Commit e611124

Browse files
committed
NSImageContext
1 parent 13904fc commit e611124

File tree

4 files changed

+393
-1
lines changed

4 files changed

+393
-1
lines changed

Sources/MacOSAppSupport/MAS_CocoaAppUtils.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ NSPoint CreatePoint (const NSView* view, const NUIE::Point& point);
1919
NSPoint CreateScreenPoint (const NSView* view, const NUIE::Point& point);
2020
NSRect CreateRect (const NSView* view, const NUIE::Rect& rect);
2121
NSColor* CreateColor (const NUIE::Color& color);
22+
NSImage* FlipImageVertically (const NSImage* image);
23+
NSImage* FlipImageHorizontally (const NSImage* image);
2224

2325
NUIE::MenuCommandPtr SelectCommandFromContextMenu (const NSView* nsView, const NUIE::Point& position, const NUIE::MenuCommandStructure& commands);
2426

Sources/MacOSAppSupport/MAS_CocoaAppUtils.mm

+35-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,41 @@ NSRect CreateRect (const NSView* view, const NUIE::Rect& rect)
9595
{
9696
return [NSColor colorWithRed:color.GetR () / 255.0f green:color.GetG () / 255.0f blue:color.GetB () / 255.0f alpha:1.0f];
9797
}
98-
98+
99+
NSImage* FlipImageVertically (const NSImage* image)
100+
{
101+
NSImage *tmpImage;
102+
NSAffineTransform *transform = [NSAffineTransform transform];
103+
104+
NSSize dimensions = [image size];
105+
NSAffineTransformStruct flip = {1.0, 0.0, 0.0, -1.0, 0.0, dimensions.height};
106+
tmpImage = [[NSImage alloc] initWithSize:dimensions];
107+
[tmpImage lockFocus];
108+
[transform setTransformStruct:flip];
109+
[transform concat];
110+
[image drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0, dimensions.width, dimensions.height) operation:NSCompositingOperationCopy fraction:1.0];
111+
[tmpImage unlockFocus];
112+
113+
return [tmpImage autorelease];
114+
}
115+
116+
NSImage* FlipImageHorizontally (const NSImage* image)
117+
{
118+
NSImage *tmpImage;
119+
NSAffineTransform *transform = [NSAffineTransform transform];
120+
121+
NSSize dimensions = [image size];
122+
NSAffineTransformStruct flip = {-1.0, 0.0, 0.0, 1.0, 0.0, dimensions.width};
123+
tmpImage = [[NSImage alloc] initWithSize:dimensions];
124+
[tmpImage lockFocus];
125+
[transform setTransformStruct:flip];
126+
[transform concat];
127+
[image drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0, dimensions.width, dimensions.height) operation:NSCompositingOperationCopy fraction:1.0];
128+
[tmpImage unlockFocus];
129+
130+
return [tmpImage autorelease];
131+
}
132+
99133
static void AddCommandToMenu (const NUIE::MenuCommandPtr& command, std::unordered_map<int, NUIE::MenuCommandPtr>& commandTable, ContextMenu* originalMenu, ContextMenu* currentMenu, int& currentCommandId)
100134
{
101135
if (command->HasChildCommands ()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#ifndef MAS_NSIMAGECONTEXT_HPP
2+
#define MAS_NSIMAGECONTEXT_HPP
3+
4+
#include "NUIE_DrawingContext.hpp"
5+
#include "NUIE_Drawing.hpp"
6+
#include "NUIE_DrawingCacheKeys.hpp"
7+
#include "MAS_NSImageLoader.hpp"
8+
9+
#include <unordered_map>
10+
11+
#ifdef __cplusplus
12+
#ifdef __OBJC__
13+
@class NSView;
14+
@class NSFont;
15+
@class NSImage;
16+
#else
17+
struct NSView;
18+
struct NSFont;
19+
struct NSImage;
20+
#endif
21+
#endif
22+
23+
namespace MAS
24+
{
25+
26+
class NSImageContext : public NUIE::NativeDrawingContext
27+
{
28+
public:
29+
NSImageContext ();
30+
NSImageContext (const NSImageLoaderPtr& imageLoader);
31+
NSImageContext (const NSImageContext& rhs) = delete;
32+
virtual ~NSImageContext ();
33+
34+
virtual void Init (void* nativeHandle) override;
35+
virtual void BlitToWindow (void* nativeHandle) override;
36+
virtual void BlitToContext (void* nativeContext) override;
37+
38+
virtual void Resize (int newWidth, int newHeight) override;
39+
40+
virtual int GetWidth () const override;
41+
virtual int GetHeight () const override;
42+
43+
virtual void BeginDraw () override;
44+
virtual void EndDraw () override;
45+
46+
virtual bool NeedToDraw (ItemPreviewMode mode) override;
47+
48+
virtual void DrawLine (const NUIE::Point& beg, const NUIE::Point& end, const NUIE::Pen& pen) override;
49+
virtual void DrawBezier (const NUIE::Point& p1, const NUIE::Point& p2, const NUIE::Point& p3, const NUIE::Point& p4, const NUIE::Pen& pen) override;
50+
51+
virtual void DrawRect (const NUIE::Rect& rect, const NUIE::Pen& pen) override;
52+
virtual void FillRect (const NUIE::Rect& rect, const NUIE::Color& color) override;
53+
54+
virtual void DrawEllipse (const NUIE::Rect& rect, const NUIE::Pen& pen) override;
55+
virtual void FillEllipse (const NUIE::Rect& rect, const NUIE::Color& color) override;
56+
57+
virtual void DrawFormattedText (const NUIE::Rect& rect, const NUIE::Font& font, const std::wstring& text, NUIE::HorizontalAnchor hAnchor, NUIE::VerticalAnchor vAnchor, const NUIE::Color& textColor) override;
58+
virtual NUIE::Size MeasureText (const NUIE::Font& font, const std::wstring& text) override;
59+
60+
virtual bool CanDrawIcon () override;
61+
virtual void DrawIcon (const NUIE::Rect& rect, const NUIE::IconId& iconId) override;
62+
63+
private:
64+
NSFont* GetFont (const NUIE::Font& font);
65+
66+
int width;
67+
int height;
68+
NSView* nsView;
69+
NSImageLoaderPtr imageLoader;
70+
NSImage* image;
71+
72+
std::unordered_map<NUIE::FontCacheKey, NSFont*> fontCache;
73+
};
74+
75+
}
76+
77+
#endif

0 commit comments

Comments
 (0)