BGradient: add Flatten, Unflatten methods
Change-Id: I861bab8ae19628d34297c294fd6ce3e5b6c82b34 Reviewed-on: https://review.haiku-os.org/c/haiku/+/2827 Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <List.h>
|
||||
|
||||
|
||||
class BDataIO;
|
||||
class BMessage;
|
||||
class BRect;
|
||||
|
||||
@@ -86,6 +87,9 @@ public:
|
||||
|
||||
void MakeEmpty();
|
||||
|
||||
status_t Flatten(BDataIO* stream) const;
|
||||
static status_t Unflatten(BGradient *&output, BDataIO* stream);
|
||||
|
||||
private:
|
||||
friend class BGradientLinear;
|
||||
friend class BGradientRadial;
|
||||
|
||||
@@ -9,6 +9,11 @@ USES_BE_API on <libbe_build>interface_kit.o = true ;
|
||||
BuildPlatformMergeObjectPIC <libbe_build>interface_kit.o :
|
||||
Bitmap.cpp
|
||||
Gradient.cpp
|
||||
GradientLinear.cpp
|
||||
GradientRadial.cpp
|
||||
GradientRadialFocus.cpp
|
||||
GradientDiamond.cpp
|
||||
GradientConic.cpp
|
||||
GraphicsDefs.cpp
|
||||
Point.cpp
|
||||
Rect.cpp
|
||||
|
||||
@@ -13,8 +13,16 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <DataIO.h>
|
||||
#include <Message.h>
|
||||
|
||||
#include <AutoDeleter.h>
|
||||
#include <GradientLinear.h>
|
||||
#include <GradientRadial.h>
|
||||
#include <GradientRadialFocus.h>
|
||||
#include <GradientDiamond.h>
|
||||
#include <GradientConic.h>
|
||||
|
||||
|
||||
// constructor
|
||||
BGradient::ColorStop::ColorStop(const rgb_color c, float o)
|
||||
@@ -447,3 +455,139 @@ BGradient::MakeEmpty()
|
||||
delete ColorStopAtFast(i);
|
||||
fColorStops.MakeEmpty();
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BGradient::Flatten(BDataIO* stream) const
|
||||
{
|
||||
int32 stopCount = CountColorStops();
|
||||
stream->Write(&fType, sizeof(Type));
|
||||
stream->Write(&stopCount, sizeof(int32));
|
||||
if (stopCount > 0) {
|
||||
for (int i = 0; i < stopCount; i++) {
|
||||
stream->Write(ColorStopAtFast(i),
|
||||
sizeof(ColorStop));
|
||||
}
|
||||
}
|
||||
|
||||
switch (fType) {
|
||||
case TYPE_LINEAR:
|
||||
stream->Write(&fData.linear.x1, sizeof(float));
|
||||
stream->Write(&fData.linear.y1, sizeof(float));
|
||||
stream->Write(&fData.linear.x2, sizeof(float));
|
||||
stream->Write(&fData.linear.y2, sizeof(float));
|
||||
break;
|
||||
case TYPE_RADIAL:
|
||||
stream->Write(&fData.radial.cx, sizeof(float));
|
||||
stream->Write(&fData.radial.cy, sizeof(float));
|
||||
stream->Write(&fData.radial.radius, sizeof(float));
|
||||
break;
|
||||
case TYPE_RADIAL_FOCUS:
|
||||
stream->Write(&fData.radial_focus.cx, sizeof(float));
|
||||
stream->Write(&fData.radial_focus.cy, sizeof(float));
|
||||
stream->Write(&fData.radial_focus.fx, sizeof(float));
|
||||
stream->Write(&fData.radial_focus.fy, sizeof(float));
|
||||
stream->Write(&fData.radial_focus.radius, sizeof(float));
|
||||
break;
|
||||
case TYPE_DIAMOND:
|
||||
stream->Write(&fData.diamond.cx, sizeof(float));
|
||||
stream->Write(&fData.diamond.cy, sizeof(float));
|
||||
break;
|
||||
case TYPE_CONIC:
|
||||
stream->Write(&fData.conic.cx, sizeof(float));
|
||||
stream->Write(&fData.conic.cy, sizeof(float));
|
||||
stream->Write(&fData.conic.angle, sizeof(float));
|
||||
break;
|
||||
case TYPE_NONE:
|
||||
break;
|
||||
}
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
static BGradient*
|
||||
gradient_for_type(BGradient::Type type)
|
||||
{
|
||||
switch (type) {
|
||||
case BGradient::TYPE_LINEAR:
|
||||
return new (std::nothrow) BGradientLinear();
|
||||
case BGradient::TYPE_RADIAL:
|
||||
return new (std::nothrow) BGradientRadial();
|
||||
case BGradient::TYPE_RADIAL_FOCUS:
|
||||
return new (std::nothrow) BGradientRadialFocus();
|
||||
case BGradient::TYPE_DIAMOND:
|
||||
return new (std::nothrow) BGradientDiamond();
|
||||
case BGradient::TYPE_CONIC:
|
||||
return new (std::nothrow) BGradientConic();
|
||||
case BGradient::TYPE_NONE:
|
||||
return new (std::nothrow) BGradient();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BGradient::Unflatten(BGradient *&output, BDataIO* stream)
|
||||
{
|
||||
output = NULL;
|
||||
Type gradientType;
|
||||
int32 colorsCount;
|
||||
stream->Read(&gradientType, sizeof(Type));
|
||||
status_t status = stream->Read(&colorsCount, sizeof(int32));
|
||||
if (status < B_OK)
|
||||
return status;
|
||||
|
||||
ObjectDeleter<BGradient> gradient(gradient_for_type(gradientType));
|
||||
if (gradient.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
if (colorsCount > 0) {
|
||||
ColorStop stop;
|
||||
for (int i = 0; i < colorsCount; i++) {
|
||||
if ((status = stream->Read(&stop, sizeof(ColorStop))) < B_OK)
|
||||
return status;
|
||||
if (!gradient->AddColorStop(stop, i))
|
||||
return B_NO_MEMORY;
|
||||
}
|
||||
}
|
||||
|
||||
switch (gradientType) {
|
||||
case TYPE_LINEAR:
|
||||
stream->Read(&gradient->fData.linear.x1, sizeof(float));
|
||||
stream->Read(&gradient->fData.linear.y1, sizeof(float));
|
||||
stream->Read(&gradient->fData.linear.x2, sizeof(float));
|
||||
if ((status = stream->Read(&gradient->fData.linear.y2, sizeof(float))) < B_OK)
|
||||
return status;
|
||||
break;
|
||||
case TYPE_RADIAL:
|
||||
stream->Read(&gradient->fData.radial.cx, sizeof(float));
|
||||
stream->Read(&gradient->fData.radial.cy, sizeof(float));
|
||||
if ((stream->Read(&gradient->fData.radial.radius, sizeof(float))) < B_OK)
|
||||
return status;
|
||||
break;
|
||||
case TYPE_RADIAL_FOCUS:
|
||||
stream->Read(&gradient->fData.radial_focus.cx, sizeof(float));
|
||||
stream->Read(&gradient->fData.radial_focus.cy, sizeof(float));
|
||||
stream->Read(&gradient->fData.radial_focus.fx, sizeof(float));
|
||||
stream->Read(&gradient->fData.radial_focus.fy, sizeof(float));
|
||||
if ((stream->Read(&gradient->fData.radial_focus.radius, sizeof(float))) < B_OK)
|
||||
return status;
|
||||
break;
|
||||
case TYPE_DIAMOND:
|
||||
stream->Read(&gradient->fData.diamond.cx, sizeof(float));
|
||||
if ((stream->Read(&gradient->fData.diamond.cy, sizeof(float))) < B_OK)
|
||||
return status;
|
||||
break;
|
||||
case TYPE_CONIC:
|
||||
stream->Read(&gradient->fData.conic.cx, sizeof(float));
|
||||
stream->Read(&gradient->fData.conic.cy, sizeof(float));
|
||||
if ((stream->Read(&gradient->fData.conic.angle, sizeof(float))) < B_OK)
|
||||
return status;
|
||||
break;
|
||||
case TYPE_NONE:
|
||||
break;
|
||||
}
|
||||
|
||||
output = gradient.Detach();
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user