diff options
author | crupest <crupest@outlook.com> | 2021-10-02 16:44:32 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-10-02 16:44:32 +0800 |
commit | 24a636954a25e938c079d88f48ee01143d667cfb (patch) | |
tree | 5901c9a6eb41907976d38401f3617fc30de62052 /src/osx | |
parent | f246877698e5b9aa98e99f3fd3d2247998008695 (diff) | |
download | cru-24a636954a25e938c079d88f48ee01143d667cfb.tar.gz cru-24a636954a25e938c079d88f48ee01143d667cfb.tar.bz2 cru-24a636954a25e938c079d88f48ee01143d667cfb.zip |
...
Diffstat (limited to 'src/osx')
-rw-r--r-- | src/osx/graphics/quartz/Convert.cpp | 11 | ||||
-rw-r--r-- | src/osx/graphics/quartz/Painter.cpp | 45 | ||||
-rw-r--r-- | src/osx/gui/Window.mm | 14 |
3 files changed, 65 insertions, 5 deletions
diff --git a/src/osx/graphics/quartz/Convert.cpp b/src/osx/graphics/quartz/Convert.cpp index 9dcb29db..a6892da9 100644 --- a/src/osx/graphics/quartz/Convert.cpp +++ b/src/osx/graphics/quartz/Convert.cpp @@ -13,15 +13,20 @@ String Convert(CFStringRef string) { kCFStringEncodingUTF16, 0); auto l = CFDataGetLength(d); - auto s = String::FromUtf16( - reinterpret_cast<const std::uint16_t*>(CFDataGetBytePtr(d)), - CFDataGetLength(d) / 2); + auto s = String(reinterpret_cast<const std::uint16_t*>(CFDataGetBytePtr(d)), + CFDataGetLength(d) / 2); CFRelease(d); return s; } +CGPoint Convert(const Point& point) { return CGPoint{point.x, point.y}; } +Point Convert(const CGPoint& point) { return Point(point.x, point.y); } + +CGSize Convert(const Size& size) { return CGSize{size.width, size.height}; } +Size Convert(const CGSize& size) { return Size(size.width, size.height); } + CGAffineTransform Convert(const Matrix& matrix) { return CGAffineTransformMake(matrix.m11, matrix.m12, matrix.m21, matrix.m22, matrix.m31, matrix.m32); diff --git a/src/osx/graphics/quartz/Painter.cpp b/src/osx/graphics/quartz/Painter.cpp index be29310c..6effe9f9 100644 --- a/src/osx/graphics/quartz/Painter.cpp +++ b/src/osx/graphics/quartz/Painter.cpp @@ -4,6 +4,7 @@ #include "cru/osx/graphics/quartz/Convert.hpp" #include "cru/osx/graphics/quartz/Geometry.hpp" #include "cru/platform/Check.hpp" +#include "cru/platform/Exception.hpp" namespace cru::platform::graphics::osx::quartz { Matrix QuartzCGContextPainter::GetTransform() { @@ -18,11 +19,21 @@ void QuartzCGContextPainter::SetTransform(const Matrix& matrix) { } void QuartzCGContextPainter::Clear(const Color& color) { - // TODO: + Validate(); + + CGColorRef c = + CGColorCreateGenericRGB(color.GetFloatRed(), color.GetFloatGreen(), + color.GetFloatBlue(), color.GetFloatAlpha()); + CGContextSetFillColorWithColor(cg_context_, c); + CGColorRelease(c); + + CGContextFillRect(cg_context_, Convert(Rect{Point{}, size_})); } void QuartzCGContextPainter::DrawLine(const Point& start, const Point& end, IBrush* brush, float width) { + Validate(); + CGContextBeginPath(cg_context_); CGContextMoveToPoint(cg_context_, start.x, start.y); CGContextAddLineToPoint(cg_context_, end.x, end.y); @@ -35,6 +46,8 @@ void QuartzCGContextPainter::DrawLine(const Point& start, const Point& end, void QuartzCGContextPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush, float width) { + Validate(); + QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId()); b->Select(cg_context_); CGContextStrokeRectWithWidth(cg_context_, Convert(rectangle), width); @@ -42,6 +55,8 @@ void QuartzCGContextPainter::StrokeRectangle(const Rect& rectangle, void QuartzCGContextPainter::FillRectangle(const Rect& rectangle, IBrush* brush) { + Validate(); + QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId()); b->Select(cg_context_); CGContextFillRect(cg_context_, Convert(rectangle)); @@ -49,6 +64,8 @@ void QuartzCGContextPainter::FillRectangle(const Rect& rectangle, void QuartzCGContextPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush, float width) { + Validate(); + QuartzGeometry* g = CheckPlatform<QuartzGeometry>(geometry, GetPlatformId()); QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId()); @@ -60,6 +77,8 @@ void QuartzCGContextPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush, } void QuartzCGContextPainter::FillGeometry(IGeometry* geometry, IBrush* brush) { + Validate(); + QuartzGeometry* g = CheckPlatform<QuartzGeometry>(geometry, GetPlatformId()); QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId()); @@ -68,4 +87,28 @@ void QuartzCGContextPainter::FillGeometry(IGeometry* geometry, IBrush* brush) { CGContextAddPath(cg_context_, g->GetCGPath()); CGContextFillPath(cg_context_); } + +void QuartzCGContextPainter::DrawText(const Point& offset, + ITextLayout* text_layout, IBrush* brush) { + // TODO: Implement this. +} + +void QuartzCGContextPainter::PushLayer(const Rect& bounds) { + // TODO: Implement this. +} + +void QuartzCGContextPainter::PopLayer() { + // TODO: Implement this. +} + +void QuartzCGContextPainter::EndDraw() { + CGContextRelease(cg_context_); + CGContextFlush(cg_context_); + cg_context_ = nullptr; +} + +void QuartzCGContextPainter::Validate() { + if (cg_context_ == nullptr) + throw ReuseException(u"QuartzCGContextPainter has already be released."); +} } // namespace cru::platform::graphics::osx::quartz diff --git a/src/osx/gui/Window.mm b/src/osx/gui/Window.mm index 52cc3934..239e175a 100644 --- a/src/osx/gui/Window.mm +++ b/src/osx/gui/Window.mm @@ -1,9 +1,11 @@ #include "cru/osx/gui/Window.hpp" +#include "cru/osx/graphics/quartz/Painter.hpp" #include "cru/osx/gui/UiApplication.hpp" -#include "cru/platform/gui/Base.hpp" +#include <AppKit/NSGraphicsContext.h> #include <AppKit/NSWindow.h> +#include <memory> namespace cru::platform::gui::osx { namespace details { @@ -112,6 +114,16 @@ void OsxWindow::SetWindowRect(const Rect& rect) { } } +std::unique_ptr<graphics::IPainter> OsxWindow::BeginPaint() { + NSGraphicsContext* ns_graphics_context = + [NSGraphicsContext graphicsContextWithWindow:p_->window_]; + + CGContextRef cg_context = [ns_graphics_context CGContext]; + + return std::make_unique<cru::platform::graphics::osx::quartz::QuartzCGContextPainter>( + GetUiApplication()->GetGraphicsFactory(), cg_context, true, GetClientSize()); +} + void OsxWindow::CreateWindow() { NSRect content_rect{p_->content_rect_.left, p_->content_rect_.top, p_->content_rect_.width, p_->content_rect_.height}; |