Skip to content

Commit

Permalink
LibGfx: Templatize Point, Size, and Rect
Browse files Browse the repository at this point in the history
  • Loading branch information
mattco98 authored and awesomekling committed Jul 26, 2020
1 parent 7a1c328 commit 335916d
Show file tree
Hide file tree
Showing 33 changed files with 402 additions and 833 deletions.
4 changes: 2 additions & 2 deletions Applications/PixelPaint/EllipseTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ void EllipseTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)

GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
auto preview_start = m_editor->layer_position_to_editor_position(layer, m_ellipse_start_position).to_int_point();
auto preview_end = m_editor->layer_position_to_editor_position(layer, m_ellipse_end_position).to_int_point();
auto preview_start = m_editor->layer_position_to_editor_position(layer, m_ellipse_start_position).to_type<int>();
auto preview_end = m_editor->layer_position_to_editor_position(layer, m_ellipse_end_position).to_type<int>();
draw_using(painter, Gfx::IntRect::from_two_points(preview_start, preview_end));
}

Expand Down
2 changes: 1 addition & 1 deletion Applications/PixelPaint/ImageEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "Layer.h"
#include "Tool.h"
#include <LibGUI/Painter.h>
#include <LibGfx/FloatRect.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Palette.h>

namespace PixelPaint {
Expand Down
2 changes: 1 addition & 1 deletion Applications/PixelPaint/ImageEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "Image.h"
#include <LibGUI/Frame.h>
#include <LibGfx/FloatPoint.h>
#include <LibGfx/Point.h>

namespace PixelPaint {

Expand Down
4 changes: 2 additions & 2 deletions Applications/PixelPaint/LineTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ void LineTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)

GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
auto preview_start = m_editor->layer_position_to_editor_position(layer, m_line_start_position).to_int_point();
auto preview_end = m_editor->layer_position_to_editor_position(layer, m_line_end_position).to_int_point();
auto preview_start = m_editor->layer_position_to_editor_position(layer, m_line_start_position).to_type<int>();
auto preview_end = m_editor->layer_position_to_editor_position(layer, m_line_end_position).to_type<int>();
painter.draw_line(preview_start, preview_end, m_editor->color_for(m_drawing_button), m_thickness);
}

Expand Down
4 changes: 3 additions & 1 deletion Applications/PixelPaint/RectangleTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ void RectangleTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)

GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
auto rect = Gfx::IntRect::from_two_points(m_editor->layer_position_to_editor_position(layer, m_rectangle_start_position).to_int_point(), m_editor->layer_position_to_editor_position(layer, m_rectangle_end_position).to_int_point());
auto rect = Gfx::IntRect::from_two_points(
m_editor->layer_position_to_editor_position(layer, m_rectangle_start_position).to_type<int>(),
m_editor->layer_position_to_editor_position(layer, m_rectangle_end_position).to_type<int>());
draw_using(painter, rect);
}

Expand Down
2 changes: 1 addition & 1 deletion Applications/QuickShow/QSWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <LibGUI/Frame.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/FloatPoint.h>
#include <LibGfx/Point.h>

class QSLabel;

Expand Down
23 changes: 14 additions & 9 deletions Libraries/LibGfx/AffineTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <AK/LogStream.h>
#include <AK/Optional.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/FloatRect.h>
#include <LibGfx/Rect.h>

namespace Gfx {
Expand Down Expand Up @@ -97,37 +96,36 @@ void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, f
mapped_y = (m_values[1] * unmapped_x + m_values[3] * unmapped_y + m_values[5]);
}

template<>
IntPoint AffineTransform::map(const IntPoint& point) const
{
float mapped_x;
float mapped_y;
map(point.x(), point.y(), mapped_x, mapped_y);
return IntPoint(roundf(mapped_x), roundf(mapped_y));
return { roundf(mapped_x), roundf(mapped_y) };
}

template<>
FloatPoint AffineTransform::map(const FloatPoint& point) const
{
float mapped_x;
float mapped_y;
map(point.x(), point.y(), mapped_x, mapped_y);
return FloatPoint(mapped_x, mapped_y);
return { mapped_x, mapped_y };
}

template<>
IntSize AffineTransform::map(const IntSize& size) const
{
return IntSize(roundf(size.width() * x_scale()), roundf(y_scale()));
return { roundf(size.width() * x_scale()), roundf(size.height() * y_scale()) };
}

template<>
FloatSize AffineTransform::map(const FloatSize& size) const
{
return { size.width() * x_scale(), size.height() * y_scale() };
}

IntRect AffineTransform::map(const IntRect& rect) const
{
return enclosing_int_rect(map(FloatRect(rect)));
}

template<typename T>
static T smallest_of(T p1, T p2, T p3, T p4)
{
Expand All @@ -140,6 +138,7 @@ static T largest_of(T p1, T p2, T p3, T p4)
return max(max(p1, p2), max(p3, p4));
}

template<>
FloatRect AffineTransform::map(const FloatRect& rect) const
{
FloatPoint p1 = map(rect.top_left());
Expand All @@ -153,6 +152,12 @@ FloatRect AffineTransform::map(const FloatRect& rect) const
return { left, top, right - left, bottom - top };
}

template<>
IntRect AffineTransform::map(const IntRect& rect) const
{
return enclosing_int_rect(map(FloatRect(rect)));
}

const LogStream& operator<<(const LogStream& stream, const AffineTransform& value)
{
if (value.is_identity())
Expand Down
12 changes: 6 additions & 6 deletions Libraries/LibGfx/AffineTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ class AffineTransform {

void map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const;

IntPoint map(const IntPoint&) const;
FloatPoint map(const FloatPoint&) const;
template<typename T>
Point<T> map(const Point<T>&) const;

IntSize map(const IntSize&) const;
FloatSize map(const FloatSize&) const;
template<typename T>
Size<T> map(const Size<T>&) const;

IntRect map(const IntRect&) const;
FloatRect map(const FloatRect&) const;
template<typename T>
Rect<T> map(const Rect<T>&) const;

float a() const { return m_values[0]; }
float b() const { return m_values[1]; }
Expand Down
1 change: 0 additions & 1 deletion Libraries/LibGfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set(SOURCES
Color.cpp
DisjointRectSet.cpp
Emoji.cpp
FloatRect.cpp
Font.cpp
GIFLoader.cpp
ICOLoader.cpp
Expand Down
135 changes: 0 additions & 135 deletions Libraries/LibGfx/FloatRect.cpp

This file was deleted.

Loading

0 comments on commit 335916d

Please sign in to comment.