From f0f7b716581495268bbb272975af9789ae045e0d Mon Sep 17 00:00:00 2001 From: Martin Corino Date: Wed, 3 Apr 2024 16:45:00 +0200 Subject: [PATCH 1/2] add 2D graphics geometry classes + doc --- lib/wx/core/geometry.rb | 112 +++++++++++++++++++ lib/wx/doc/geometry.rb | 125 +++++++++++++++++++++ rakelib/lib/director/geometry.rb | 142 ++++++++++++++++++++++++ rakelib/lib/director/graphics_object.rb | 25 +---- rakelib/lib/specs/interfaces.rb | 1 + 5 files changed, 382 insertions(+), 23 deletions(-) create mode 100644 lib/wx/core/geometry.rb create mode 100644 lib/wx/doc/geometry.rb create mode 100644 rakelib/lib/director/geometry.rb diff --git a/lib/wx/core/geometry.rb b/lib/wx/core/geometry.rb new file mode 100644 index 00000000..35fe6673 --- /dev/null +++ b/lib/wx/core/geometry.rb @@ -0,0 +1,112 @@ +# Copyright (c) 2023 M.J.N. Corino, The Netherlands +# +# This software is released under the MIT license. + +module Wx + class Point2DInt + + alias :x :get_x + alias :x= :set_x + alias :y :get_y + alias :y= :set_y + + # make wrappers private + private :add, :sub, :mul, :div + + wx_assign = instance_method :assign + define_method :assign do |pt| + wx_assign.bind(self).call(pt) + self + end + + def add!(pt) + add(pt) + self + end + + def sub!(pt) + sub(pt) + self + end + + def mul!(v) + mul(v) + self + end + + def div!(v) + div(v) + self + end + + def +(pt) + Point2DInt.new(self).add!(pt) + end + + def -(pt) + Point2DInt.new(self).sub!(pt) + end + + def *(v) + Point2DInt.new(self).mul!(v) + end + + def /(v) + Point2DInt.new(self).div!(v) + end + + end + class Point2DDouble + + alias :x :get_x + alias :x= :set_x + alias :y :get_y + alias :y= :set_y + + # make wrappers private + private :add, :sub, :mul, :div + + wx_assign = instance_method :assign + define_method :assign do |pt| + wx_assign.bind(self).call(pt) + self + end + + def add!(pt) + add(pt) + self + end + + def sub!(pt) + sub(pt) + self + end + + def mul!(v) + mul(v) + self + end + + def div!(v) + div(v) + self + end + + def +(pt) + Point2DDouble.new(self).add!(pt) + end + + def -(pt) + Point2DDouble.new(self).sub!(pt) + end + + def *(v) + Point2DDouble.new(self).mul!(v) + end + + def /(v) + Point2DDouble.new(self).div!(v) + end + + end +end diff --git a/lib/wx/doc/geometry.rb b/lib/wx/doc/geometry.rb new file mode 100644 index 00000000..b0de3591 --- /dev/null +++ b/lib/wx/doc/geometry.rb @@ -0,0 +1,125 @@ +# :stopdoc: +# Copyright (c) 2023 M.J.N. Corino, The Netherlands +# +# This software is released under the MIT license. + + +### +# wxRuby3 2D geometry classes +### +# :startdoc: + + +module Wx + class Point2DInt + + # @return [Integer] + def get_x; end + alias :x :get_x + # @param [Integer] v + # @return [Integer] + def set_x(v) end + alias :x= :set_x + + # @return [Integer] + def get_y; end + alias :y :get_y + # @param [Integer] v + # @return [Integer] + def set_y(v) end + alias :y= :set_y + + # @param [Wx::Point2DInt] pt + # @return [self] + def assign(pt) end + + # @param [Wx::Point2DInt] pt + # @return [self] + def add!(pt) end + + # @param [Wx::Point2DInt] pt + # @return [self] + def sub!(pt) end + + # @param [Wx::Point2DInt,Integer,Float] v + # @return [self] + def mul!(v) end + + # @param [Wx::Point2DInt,Integer,Float] v + # @return [self] + def div!(v) end + + # @param [Wx::Point2DInt] pt + # @return [Wx::Point2DInt] + def +(pt) end + + # @param [Wx::Point2DInt] pt + # @return [Wx::Point2DInt] + def -(pt) end + + # @param [Wx::Point2DInt,Integer,Float] v + # @return [Wx::Point2DInt] + def *(v) end + + # @param [Wx::Point2DInt,Integer,Float] v + # @return [Wx::Point2DInt] + def /(v) end + + end + + class Point2DDouble + + # @return [Integer] + def get_x; end + alias :x :get_x + # @param [Integer] v + # @return [Integer] + def set_x(v) end + alias :x= :set_x + + # @return [Integer] + def get_y; end + alias :y :get_y + # @param [Integer] v + # @return [Integer] + def set_y(v) end + alias :y= :set_y + + # @param [Wx::Point2DInt] pt + # @return [self] + def assign(pt) end + + # @param [Wx::Point2DDouble] pt + # @return [self] + def add!(pt) end + + # @param [Wx::Point2DDouble] pt + # @return [self] + def sub!(pt) end + + # @param [Wx::Point2DDouble,Integer,Float] v + # @return [self] + def mul!(v) end + + # @param [Wx::Point2DDouble,Integer,Float] v + # @return [self] + def div!(v) end + + # @param [Wx::Point2DDouble] pt + # @return [Wx::Point2DDouble] + def +(pt) end + + # @param [Wx::Point2DDouble] pt + # @return [Wx::Point2DDouble] + def -(pt) end + + # @param [Wx::Point2DDouble,Integer,Float] v + # @return [Wx::Point2DDouble] + def *(v) end + + # @param [Wx::Point2DDouble,Integer,Float] v + # @return [Wx::Point2DDouble] + def /(v) end + + end +end diff --git a/rakelib/lib/director/geometry.rb b/rakelib/lib/director/geometry.rb new file mode 100644 index 00000000..07e5fc2b --- /dev/null +++ b/rakelib/lib/director/geometry.rb @@ -0,0 +1,142 @@ +# Copyright (c) 2023 M.J.N. Corino, The Netherlands +# +# This software is released under the MIT license. + +### +# wxRuby3 wxWidgets interface director +### + +module WXRuby3 + + class Director + + class Geometry < Director + + def setup + spec.items.replace %w{wxPoint2DInt wxPoint2DDouble} + + spec.ignore 'wxPoint2DInt::m_x', 'wxPoint2DInt::m_y', + 'wxPoint2DDouble::m_x', 'wxPoint2DDouble::m_y' + + spec.add_extend_code 'wxPoint2DInt', <<~__HEREDOC + wxInt32 get_x() + { + return $self->m_x; + } + wxInt32 set_x(wxInt32 v) + { + return ($self->m_x = v); + } + wxInt32 get_y() + { + return $self->m_y; + } + wxInt32 set_y(wxInt32 v) + { + return ($self->m_y = v); + } + void assign(const wxPoint2DInt& pt) + { + (*$self) = pt; + } + void add(const wxPoint2DInt& pt) + { + (*$self) += pt; + } + void sub(const wxPoint2DInt& pt) + { + (*$self) -= pt; + } + void mul(const wxPoint2DInt& pt) + { + (*$self) *= pt; + } + void mul(wxDouble v) + { + $self->m_x *= v; $self->m_y *= v; + } + void mul(wxInt32 v) + { + $self->m_x *= v; $self->m_y *= v; + } + void div(const wxPoint2DInt& pt) + { + (*$self) /= pt; + } + void div(wxDouble v) + { + $self->m_x /= v; $self->m_y /= v; + } + void div(wxInt32 v) + { + $self->m_x /= v; $self->m_y /= v; + } + __HEREDOC + + spec.add_extend_code 'wxPoint2DDouble', <<~__HEREDOC + wxDouble get_x() + { + return $self->m_x; + } + wxDouble set_x(wxDouble v) + { + return ($self->m_x = v); + } + wxDouble get_y() + { + return $self->m_y; + } + wxDouble set_y(wxDouble v) + { + return ($self->m_y = v); + } + void assign(const wxPoint2DDouble& pt) + { + (*$self) = pt; + } + void add(const wxPoint2DDouble& pt) + { + (*$self) += pt; + } + void sub(const wxPoint2DDouble& pt) + { + (*$self) -= pt; + } + void mul(const wxPoint2DDouble& pt) + { + (*$self) *= pt; + } + void mul(wxDouble v) + { + $self->m_x *= v; $self->m_y *= v; + } + void mul(wxInt32 v) + { + $self->m_x *= v; $self->m_y *= v; + } + void div(const wxPoint2DDouble& pt) + { + (*$self) /= pt; + } + void div(wxDouble v) + { + $self->m_x /= v; $self->m_y /= v; + } + void div(wxInt32 v) + { + $self->m_x /= v; $self->m_y /= v; + } + __HEREDOC + + spec.map_apply 'int * OUTPUT' => 'wxInt32 *' + + # ignore all friend operators + spec.do_not_generate :functions + + end + + end + + end + +end diff --git a/rakelib/lib/director/graphics_object.rb b/rakelib/lib/director/graphics_object.rb index eb462afb..0930ec80 100644 --- a/rakelib/lib/director/graphics_object.rb +++ b/rakelib/lib/director/graphics_object.rb @@ -22,34 +22,13 @@ def setup spec.ignore 'wxGraphicsMatrix::GetNativeMatrix' spec.ignore 'wxGraphicsBitmap::GetNativeBitmap' spec.ignore 'wxGraphicsPath::GetNativePath', - 'wxGraphicsPath::UnGetNativePath', - 'wxGraphicsPath::AddCurveToPoint(const wxPoint2DDouble &, const wxPoint2DDouble &, const wxPoint2DDouble &)', - 'wxGraphicsPath::AddLineToPoint(const wxPoint2DDouble &)', - 'wxGraphicsPath::Contains(const wxPoint2DDouble &, wxPolygonFillMode) const', - 'wxGraphicsPath::GetCurrentPoint', - 'wxGraphicsPath::MoveToPoint(const wxPoint2DDouble &)', - 'wxGraphicsPath::AddArc(const wxPoint2DDouble &, wxDouble, wxDouble, wxDouble, bool)' + 'wxGraphicsPath::UnGetNativePath' # Deal with GraphicsMatrix#get method spec.map_apply 'double *OUTPUT' => [ 'wxDouble *a', 'wxDouble *b', 'wxDouble *c', 'wxDouble *d', 'wxDouble *tx' , 'wxDouble *ty' ] - # type mapping for GraphicsPath wxPoint2DDouble args - spec.map 'const wxPoint2DDouble&' => 'Array' do - map_in temp: 'wxPoint2DDouble tmp_pt', code: <<~__CODE - if (TYPE($input) == T_ARRAY && RARRAY_LEN($input) == 2) - { - tmp_pt = wxPoint2DDouble(NUM2DBL(rb_ary_entry($input, 0)), - NUM2DBL(rb_ary_entry($input, 1))); - $1 = &tmp_pt; - } - else - { - rb_raise(rb_eTypeError, "Wrong type for %i", $argnum-1); - } - __CODE - end spec.ignore 'wxGraphicsPath::GetBox() const', - 'wxGraphicsPath::GetCurrentPoint() const' + 'wxGraphicsPath::GetCurrentPoint(wxDouble*,wxDouble*) const' spec.map_apply 'double * OUTPUT' => 'wxDouble *' if Config.platform == :mingw # it seems for WXMSW there is a problem cleaning up GraphicsObjects in GC after diff --git a/rakelib/lib/specs/interfaces.rb b/rakelib/lib/specs/interfaces.rb index fac2ef7b..a5291188 100644 --- a/rakelib/lib/specs/interfaces.rb +++ b/rakelib/lib/specs/interfaces.rb @@ -235,6 +235,7 @@ module WXRuby3 Director.Spec(pkg, 'wxPersistentWindow', requirements: %w[USE_CONFIG]) Director.Spec(pkg, 'wxSecretStore', requirements: %w[USE_SECRETSTORE]) Director.Spec(pkg, 'wxDCOverlay', director: Director::DerivedDC) + Director.Spec(pkg, 'wxGeometry', requirements: %w[USE_GEOMETRY]) } Director.Package('Wx::PRT', 'USE_PRINTING_ARCHITECTURE') do |pkg| From a9a0127370713215128595a41d3dbb57bef7e14f Mon Sep 17 00:00:00 2001 From: Martin Corino Date: Wed, 3 Apr 2024 20:15:09 +0200 Subject: [PATCH 2/2] add 2D affine transformation support --- rakelib/lib/director/affine_matrix.rb | 51 +++++++++++++++++++++++++++ rakelib/lib/director/dc.rb | 8 +---- rakelib/lib/specs/interfaces.rb | 1 + 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 rakelib/lib/director/affine_matrix.rb diff --git a/rakelib/lib/director/affine_matrix.rb b/rakelib/lib/director/affine_matrix.rb new file mode 100644 index 00000000..07c81536 --- /dev/null +++ b/rakelib/lib/director/affine_matrix.rb @@ -0,0 +1,51 @@ +# Copyright (c) 2023 M.J.N. Corino, The Netherlands +# +# This software is released under the MIT license. + +### +# wxRuby3 wxWidgets interface director +### + +module WXRuby3 + + class Director + + class AffineMatrix2D < Director + + def setup + spec.items.unshift('wxAffineMatrix2DBase') << 'wxMatrix2D' + + spec.make_abstract 'wxAffineMatrix2DBase' + spec.disable_proxies + + spec.map_apply 'int * OUTPUT' => ['wxDouble *'] + spec.map 'wxPoint2DDouble *' => 'Wx::Point2DDouble' do + map_in ignore: true, temp: 'wxPoint2DDouble tmp', code: '$1 = &tmp;' + + map_argout code: <<~__CODE + $result = SWIG_Ruby_AppendOutput($result, SWIG_NewPointerObj(new wxPoint2DDouble(tmp$argnum), SWIGTYPE_p_wxPoint2DDouble, SWIG_POINTER_OWN)); + __CODE + end + spec.map 'wxMatrix2D *' => 'Wx::Matrix2D' do + map_in ignore: true, temp: 'wxMatrix2D tmp', code: '$1 = &tmp;' + + map_argout code: <<~__CODE + $result = SWIG_Ruby_AppendOutput($result, SWIG_NewPointerObj(new wxMatrix2D(tmp$argnum), SWIGTYPE_p_wxPoint2DDouble, SWIG_POINTER_OWN)); + __CODE + end + + spec.ignore 'wxAffineMatrix2D::Mirror', + 'wxAffineMatrix2D::TransformPoint', + 'wxAffineMatrix2D::TransformDistance', + 'wxAffineMatrix2D::IsEqual' + + spec.regard 'wxMatrix2D::m_11', 'wxMatrix2D::m_12', + 'wxMatrix2D::m_21', 'wxMatrix2D::m_22' + + end + + end + + end + +end diff --git a/rakelib/lib/director/dc.rb b/rakelib/lib/director/dc.rb index 7ad61b16..f8e03334 100644 --- a/rakelib/lib/director/dc.rb +++ b/rakelib/lib/director/dc.rb @@ -36,13 +36,7 @@ def setup 'wxDC::GetLogicalOrigin(wxCoord *,wxCoord *) const', 'wxDC::GetHandle' ] - # ignore Matrix Transformation methods until someone asks for them - # TODO : possibly wrap at a later time - spec.ignore 'wxDC::SetTransformMatrix', - 'wxDC::GetTransformMatrix', - 'wxDC::ResetTransformMatrix', - 'wxDC::CanUseTransformMatrix' - spec.disable_proxies + spec.disable_proxies spec.rename_for_ruby({ 'GetDimensions' => 'wxDC::GetSize(wxCoord *, wxCoord *) const', 'GetDimensionsMM' => 'wxDC::GetSizeMM(wxCoord *, wxCoord *) const', diff --git a/rakelib/lib/specs/interfaces.rb b/rakelib/lib/specs/interfaces.rb index a5291188..2dad2ab5 100644 --- a/rakelib/lib/specs/interfaces.rb +++ b/rakelib/lib/specs/interfaces.rb @@ -236,6 +236,7 @@ module WXRuby3 Director.Spec(pkg, 'wxSecretStore', requirements: %w[USE_SECRETSTORE]) Director.Spec(pkg, 'wxDCOverlay', director: Director::DerivedDC) Director.Spec(pkg, 'wxGeometry', requirements: %w[USE_GEOMETRY]) + Director.Spec(pkg, 'wxAffineMatrix2D', requirements: %w[USE_GEOMETRY]) } Director.Package('Wx::PRT', 'USE_PRINTING_ARCHITECTURE') do |pkg|