Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- namespace rl{
- #include <raylib.h>
- }
- #include <vector>
- constexpr rl::Color COLOR = rl::SKYBLUE;
- constexpr float MOUSE_SCALE_MARK_SIZE = 15;
- class Figure {
- public:
- virtual void Update() = 0;
- virtual void Draw() = 0;
- };
- class Rectangle : public Figure {
- public:
- Rectangle(int x, int y,int width, int height)
- : posX(x), posY(y), width(width), height(height) {};
- bool mouseScaleReady = false;
- bool mouseScaleMode = false;
- virtual void Update(){
- rl::Vector2 mouse = rl::GetMousePosition();
- rl::Rectangle rec = {posX,posY,width,height};
- if (rl::CheckCollisionPointRec(mouse,rec) &&
- rl::CheckCollisionPointRec
- (mouse,
- (rl::Rectangle)
- // WARNING: The following line may screw up Emacs indenting!
- {
- rec.x + rec.width - MOUSE_SCALE_MARK_SIZE,
- rec.y + rec.height - MOUSE_SCALE_MARK_SIZE,
- MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE
- }))
- {
- mouseScaleReady = true;
- if (IsMouseButtonPressed(rl::MOUSE_LEFT_BUTTON)) mouseScaleMode = true;
- }
- else mouseScaleReady = false;
- if (mouseScaleMode)
- {
- mouseScaleReady = true;
- width = (mouse.x - posX);
- height = (mouse.y - posY);
- if (width < MOUSE_SCALE_MARK_SIZE) width = MOUSE_SCALE_MARK_SIZE;
- if (height < MOUSE_SCALE_MARK_SIZE) height = MOUSE_SCALE_MARK_SIZE;
- if (IsMouseButtonReleased(rl::MOUSE_LEFT_BUTTON)) mouseScaleMode = false;
- }
- };
- virtual void Draw(){
- DrawRectangle(posX, posY, width, height, color);
- if (mouseScaleReady){
- rl::Rectangle rec = {posX,posY,width,height};
- rl::DrawRectangleLinesEx(rec,1,rl::BLACK);
- rl::DrawTriangle((rl::Vector2){rec.x + rec.width - MOUSE_SCALE_MARK_SIZE,
- rec.y + rec.height},
- (rl::Vector2){ rec.x + rec.width, rec.y + rec.height },
- (rl::Vector2){ rec.x + rec.width,
- rec.y + rec.height - MOUSE_SCALE_MARK_SIZE },
- rl::BLACK);
- }
- };
- private:
- float posX;
- float posY;
- float width;
- float height;
- rl::Color color = COLOR;
- };
- class Ellipse {
- public:
- virtual void Update(){};
- virtual void Draw()
- {
- puts("Drawing an ellipse");
- };
- };
- class Canvas {
- public:
- void Update();
- void Draw();
- void addFigure(Figure *fig){figures.push_back(fig);};
- private:
- std::vector<Figure*> figures;
- };
- void Canvas::Update(){
- for(auto fig : figures)
- fig->Update();
- }
- void Canvas::Draw(){
- rl::BeginDrawing();
- ClearBackground(rl::RAYWHITE);
- for(auto fig : figures)
- {
- fig->Draw();
- }
- rl::EndDrawing();
- }
- int main(void){
- rl::InitWindow(1280,1024, "Grafity");
- rl::SetTargetFPS(60);
- Canvas canvas;
- Rectangle r1(800,300,50,80);
- Rectangle r2(500,400,90,120);
- Rectangle r3(200,100,100,50);
- canvas.addFigure(&r1);
- canvas.addFigure(&r2);
- canvas.addFigure(&r3);
- while (!rl::WindowShouldClose())
- {
- canvas.Update();
- canvas.Draw();
- }
- rl::CloseWindow();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement