add picturedrawable and testcase

This commit is contained in:
侯歌 2021-08-30 17:36:53 +08:00
parent 6cbbbf64aa
commit e573a28e7a
4 changed files with 95 additions and 0 deletions

1
src/gui/drawables.h Normal file → Executable file
View File

@ -5,6 +5,7 @@
#include <drawables/colordrawable.h>
#include <drawables/bitmapdrawable.h>
#include <drawables/ninepatchdrawable.h>
#include <drawables/picturedrawable.h>
#include <drawables/gradientdrawable.h>
#include <drawables/transitiondrawable.h>
#include <drawables/drawablewrapper.h>

View File

@ -0,0 +1,48 @@
#include <drawables/picturedrawable.h>
namespace cdroid{
PictureDrawable::PictureDrawable(Picture picture){
mPicture=picture;
}
Picture PictureDrawable::getPicture(){
return mPicture;
}
void PictureDrawable::setPicture(Picture picture){
mPicture=picture;
}
void PictureDrawable::draw(Canvas& canvas){
if (mPicture) {
Rect bounds = getBounds();
canvas.save();
canvas.set_source(mPicture,bounds.x, bounds.y);
canvas.rectangle(bounds.x, bounds.y,bounds.width, bounds.height);
canvas.clip();
canvas.paint();
canvas.restore();
}
}
int PictureDrawable::getIntrinsicWidth()const{
return mPicture ? mPicture->ink_extents().width : -1;
}
int PictureDrawable::getIntrinsicHeight()const{
return mPicture ? mPicture->ink_extents().height: -1;
}
int PictureDrawable::getOpacity(){
// not sure, so be safe
return TRANSLUCENT;
}
void PictureDrawable::setColorFilter(ColorFilter* colorFilter) {
}
void PictureDrawable::setAlpha(int alpha){
}
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <drawables/drawable.h>
namespace cdroid{
typedef RefPtr<RecordingSurface>Picture;
class PictureDrawable:public Drawable{
private:
Picture mPicture;
public:
PictureDrawable(Picture picture);
Picture getPicture();
void setPicture(Picture picture);
void draw(Canvas& canvas);
int getIntrinsicWidth()const override;
int getIntrinsicHeight()const override;
int getOpacity()override;
void setColorFilter(ColorFilter* colorFilter);
void setAlpha(int alpha)override;
};
}

View File

@ -116,6 +116,28 @@ TEST_F(DRAWABLE,ninepatch2){
delete d;
}
TEST_F(DRAWABLE,picture){
Cairo::Rectangle rc={0,0,400,50};
RefPtr<RecordingSurface>picture= RecordingSurface::create(rc);
RefPtr<Cairo::Context>ctxpic=Cairo::Context::create(picture);
ctxpic->set_source_rgba(1,1,1,1);
ctxpic->rectangle(0,0,400,50);
ctxpic->fill();
ctxpic->set_source_rgba(1,0,0,1);
ctxpic->move_to(50,20);
ctxpic->set_font_size(32);
ctxpic->show_text("PictureDrawable");
ctxpic->fill();
ctxpic->set_source_rgba(0,1,0,.5);
ctxpic->arc(200,25,50,0,M_PI*2.f);
ctxpic->fill();
PictureDrawable*pd=new PictureDrawable(picture);
pd->setBounds(100,100,400,50);
pd->draw(*ctx);
delete pd;
}
TEST_F(DRAWABLE,transition){
std::vector<Drawable*>ds;
ds.push_back(new ColorDrawable(0x80FF0000));