Skip to content

Commit

Permalink
NSImageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
kissandras committed Dec 10, 2020
1 parent 13904fc commit e611124
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Sources/MacOSAppSupport/MAS_CocoaAppUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ NSPoint CreatePoint (const NSView* view, const NUIE::Point& point);
NSPoint CreateScreenPoint (const NSView* view, const NUIE::Point& point);
NSRect CreateRect (const NSView* view, const NUIE::Rect& rect);
NSColor* CreateColor (const NUIE::Color& color);
NSImage* FlipImageVertically (const NSImage* image);
NSImage* FlipImageHorizontally (const NSImage* image);

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

Expand Down
36 changes: 35 additions & 1 deletion Sources/MacOSAppSupport/MAS_CocoaAppUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,41 @@ NSRect CreateRect (const NSView* view, const NUIE::Rect& rect)
{
return [NSColor colorWithRed:color.GetR () / 255.0f green:color.GetG () / 255.0f blue:color.GetB () / 255.0f alpha:1.0f];
}


NSImage* FlipImageVertically (const NSImage* image)
{
NSImage *tmpImage;
NSAffineTransform *transform = [NSAffineTransform transform];

NSSize dimensions = [image size];
NSAffineTransformStruct flip = {1.0, 0.0, 0.0, -1.0, 0.0, dimensions.height};
tmpImage = [[NSImage alloc] initWithSize:dimensions];
[tmpImage lockFocus];
[transform setTransformStruct:flip];
[transform concat];
[image drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0, dimensions.width, dimensions.height) operation:NSCompositingOperationCopy fraction:1.0];
[tmpImage unlockFocus];

return [tmpImage autorelease];
}

NSImage* FlipImageHorizontally (const NSImage* image)
{
NSImage *tmpImage;
NSAffineTransform *transform = [NSAffineTransform transform];

NSSize dimensions = [image size];
NSAffineTransformStruct flip = {-1.0, 0.0, 0.0, 1.0, 0.0, dimensions.width};
tmpImage = [[NSImage alloc] initWithSize:dimensions];
[tmpImage lockFocus];
[transform setTransformStruct:flip];
[transform concat];
[image drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0, dimensions.width, dimensions.height) operation:NSCompositingOperationCopy fraction:1.0];
[tmpImage unlockFocus];

return [tmpImage autorelease];
}

static void AddCommandToMenu (const NUIE::MenuCommandPtr& command, std::unordered_map<int, NUIE::MenuCommandPtr>& commandTable, ContextMenu* originalMenu, ContextMenu* currentMenu, int& currentCommandId)
{
if (command->HasChildCommands ()) {
Expand Down
77 changes: 77 additions & 0 deletions Sources/MacOSAppSupport/MAS_NSImageContext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef MAS_NSIMAGECONTEXT_HPP
#define MAS_NSIMAGECONTEXT_HPP

#include "NUIE_DrawingContext.hpp"
#include "NUIE_Drawing.hpp"
#include "NUIE_DrawingCacheKeys.hpp"
#include "MAS_NSImageLoader.hpp"

#include <unordered_map>

#ifdef __cplusplus
#ifdef __OBJC__
@class NSView;
@class NSFont;
@class NSImage;
#else
struct NSView;
struct NSFont;
struct NSImage;
#endif
#endif

namespace MAS
{

class NSImageContext : public NUIE::NativeDrawingContext
{
public:
NSImageContext ();
NSImageContext (const NSImageLoaderPtr& imageLoader);
NSImageContext (const NSImageContext& rhs) = delete;
virtual ~NSImageContext ();

virtual void Init (void* nativeHandle) override;
virtual void BlitToWindow (void* nativeHandle) override;
virtual void BlitToContext (void* nativeContext) override;

virtual void Resize (int newWidth, int newHeight) override;

virtual int GetWidth () const override;
virtual int GetHeight () const override;

virtual void BeginDraw () override;
virtual void EndDraw () override;

virtual bool NeedToDraw (ItemPreviewMode mode) override;

virtual void DrawLine (const NUIE::Point& beg, const NUIE::Point& end, const NUIE::Pen& pen) override;
virtual void DrawBezier (const NUIE::Point& p1, const NUIE::Point& p2, const NUIE::Point& p3, const NUIE::Point& p4, const NUIE::Pen& pen) override;

virtual void DrawRect (const NUIE::Rect& rect, const NUIE::Pen& pen) override;
virtual void FillRect (const NUIE::Rect& rect, const NUIE::Color& color) override;

virtual void DrawEllipse (const NUIE::Rect& rect, const NUIE::Pen& pen) override;
virtual void FillEllipse (const NUIE::Rect& rect, const NUIE::Color& color) override;

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;
virtual NUIE::Size MeasureText (const NUIE::Font& font, const std::wstring& text) override;

virtual bool CanDrawIcon () override;
virtual void DrawIcon (const NUIE::Rect& rect, const NUIE::IconId& iconId) override;

private:
NSFont* GetFont (const NUIE::Font& font);

int width;
int height;
NSView* nsView;
NSImageLoaderPtr imageLoader;
NSImage* image;

std::unordered_map<NUIE::FontCacheKey, NSFont*> fontCache;
};

}

#endif
Loading

0 comments on commit e611124

Please sign in to comment.