Skip to content

Commit 047f619

Browse files
committed
Added and implemented some actions, so now you can really do something!
1 parent 3040454 commit 047f619

14 files changed

+363
-58
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ if(GLFW3_FOUND)
1818
brushes.cpp
1919
glarraybuffer.cpp
2020
glprogram.cpp
21+
state.h
2122
)
2223

2324
set(imaditor_HDRS
@@ -39,6 +40,10 @@ if(GLFW3_FOUND)
3940
actions/floodfillaction.h
4041
actions/brushaction.cpp
4142
actions/brushaction.h
43+
actions/dropperaction.cpp
44+
actions/dropperaction.h
45+
actions/eraseaction.cpp
46+
actions/eraseaction.h
4247
)
4348

4449
set(imgui_SRCS

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
An image editor with ImGui interface.
44

5-
## Screenshot
5+
## Screenshots
66

77
![Screenshot](screenshot.png)
8+
9+
![Screenshot 2](screenshot2.png)

actions/brushaction.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "brushaction.h"
2+
#include "../state.h"
23
#include "../image.h"
34

45
BrushAction::BrushAction() { }
56

6-
BrushActionFactory::BrushActionFactory() { }
7+
BrushActionFactory::BrushActionFactory() : _isPainting(false) { }
78

89
BrushActionFactory* BrushActionFactory::Instance()
910
{
@@ -12,10 +13,35 @@ BrushActionFactory* BrushActionFactory::Instance()
1213
return &instance;
1314
}
1415

16+
void BrushActionFactory::paint(Image* image, float color[], const glm::vec2& p)
17+
{
18+
if (_isPainting)
19+
{
20+
auto layer = image->_layers[image->_selectedLayer];
21+
22+
auto p = (_lastPosition[1] * layer->_size[1] + _lastPosition[0]);
23+
for (int b = 0; b < layer->_bpp; b++)
24+
{
25+
layer->_data[p * layer->_bpp + b] = color[b] * 255;
26+
}
27+
28+
layer->setDirty();
29+
}
30+
}
31+
1532
void BrushActionFactory::MouseMove(Image* image, int x, int y)
1633
{
1734
_lastPosition[0] = x;
1835
_lastPosition[1] = y;
36+
37+
for (int i = 0; i < 2; i++)
38+
if (_lastPosition[i] < 0 || _lastPosition[1] >= image->_size[1])
39+
return;
40+
41+
if (_isPainting)
42+
{
43+
paint(image, foreColor, glm::vec2(_lastPosition[0], _lastPosition[1]));
44+
}
1945
}
2046

2147
void BrushActionFactory::PrimaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
@@ -24,11 +50,14 @@ void BrushActionFactory::PrimaryMouseButtonDown(Image* image, bool shift, bool c
2450
if (_lastPosition[i] < 0 || _lastPosition[1] >= image->_size[1])
2551
return;
2652

27-
// todo apply brush texture onto selected layer @ _lastPosition
53+
_isPainting = true;
54+
paint(image, foreColor, glm::vec2(_lastPosition[0], _lastPosition[1]));
2855
}
2956

3057
void BrushActionFactory::PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
31-
{ }
58+
{
59+
_isPainting = false;
60+
}
3261

3362
void BrushActionFactory::SecondaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
3463
{ }

actions/brushaction.h

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define BRUSHACTION_H
33

44
#include "baseaction.h"
5+
#include <glm/glm.hpp>
56

67
class BrushAction : public BaseAction
78
{
@@ -11,8 +12,10 @@ class BrushAction : public BaseAction
1112

1213
class BrushActionFactory : public BaseActionFactory
1314
{
15+
void paint(Image* image, float color[], const glm::vec2& p);
1416
float _size;
1517
int _lastPosition[2];
18+
bool _isPainting;
1619
BrushActionFactory();
1720
public:
1821
static BrushActionFactory* Instance();

actions/dropperaction.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "dropperaction.h"
2+
#include "../state.h"
3+
#include "../image.h"
4+
5+
DropperAction::DropperAction() { }
6+
7+
DropperActionFactory::DropperActionFactory() : _isPainting(false) { }
8+
9+
DropperActionFactory* DropperActionFactory::Instance()
10+
{
11+
static DropperActionFactory instance;
12+
13+
return &instance;
14+
}
15+
16+
void DropperActionFactory::getColor(Image* image, float color[], const glm::vec2& p)
17+
{
18+
if (_isPainting)
19+
{
20+
auto layer = image->_layers[image->_selectedLayer];
21+
22+
auto p = (_lastPosition[1] * layer->_size[1] + _lastPosition[0]);
23+
for (int b = 0; b < layer->_bpp; b++)
24+
{
25+
color[b] = layer->_data[p * layer->_bpp + b] / 255.0f;
26+
}
27+
28+
layer->setDirty();
29+
}
30+
}
31+
32+
void DropperActionFactory::MouseMove(Image* image, int x, int y)
33+
{
34+
_lastPosition[0] = x;
35+
_lastPosition[1] = y;
36+
37+
for (int i = 0; i < 2; i++)
38+
if (_lastPosition[i] < 0 || _lastPosition[1] >= image->_size[1])
39+
return;
40+
41+
getColor(image, foreColor, glm::vec2(_lastPosition[0], _lastPosition[1]));
42+
}
43+
44+
void DropperActionFactory::PrimaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
45+
{
46+
for (int i = 0; i < 2; i++)
47+
if (_lastPosition[i] < 0 || _lastPosition[1] >= image->_size[1])
48+
return;
49+
50+
_isPainting = true;
51+
getColor(image, foreColor, glm::vec2(_lastPosition[0], _lastPosition[1]));
52+
}
53+
54+
void DropperActionFactory::PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
55+
{
56+
_isPainting = false;
57+
}
58+
59+
void DropperActionFactory::SecondaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
60+
{ }
61+
62+
void DropperActionFactory::SecondaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
63+
{ }
64+
65+
GLuint DropperActionFactory::ToolHelperImage()
66+
{
67+
return 0;
68+
}

actions/dropperaction.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef DROPPERACTION_H
2+
#define DROPPERACTION_H
3+
4+
#include "baseaction.h"
5+
#include <glm/glm.hpp>
6+
7+
class DropperAction
8+
{
9+
public:
10+
DropperAction();
11+
};
12+
13+
class DropperActionFactory : public BaseActionFactory
14+
{
15+
void getColor(Image* image, float color[], const glm::vec2& p);
16+
float _size;
17+
int _lastPosition[2];
18+
bool _isPainting;
19+
DropperActionFactory();
20+
public:
21+
static DropperActionFactory* Instance();
22+
23+
virtual void MouseMove(Image* image, int x, int y);
24+
virtual void PrimaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super);
25+
virtual void PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super);
26+
virtual void SecondaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super);
27+
virtual void SecondaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super);
28+
29+
virtual GLuint ToolHelperImage();
30+
};
31+
32+
#endif // DROPPERACTION_H

actions/eraseaction.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "eraseaction.h"
2+
#include "../state.h"
3+
#include "../image.h"
4+
5+
EraseAction::EraseAction() { }
6+
7+
EraseActionFactory::EraseActionFactory() : _isErasing(false) { }
8+
9+
EraseActionFactory* EraseActionFactory::Instance()
10+
{
11+
static EraseActionFactory instance;
12+
13+
return &instance;
14+
}
15+
16+
void EraseActionFactory::erase(Image* image, float color[], const glm::vec2& p)
17+
{
18+
if (_isErasing)
19+
{
20+
auto layer = image->_layers[image->_selectedLayer];
21+
22+
auto p = (_lastPosition[1] * layer->_size[1] + _lastPosition[0]);
23+
for (int b = 0; b < layer->_bpp; b++)
24+
{
25+
layer->_data[p * layer->_bpp + b] = 0.0f;
26+
}
27+
28+
layer->setDirty();
29+
}
30+
}
31+
32+
void EraseActionFactory::MouseMove(Image* image, int x, int y)
33+
{
34+
_lastPosition[0] = x;
35+
_lastPosition[1] = y;
36+
37+
for (int i = 0; i < 2; i++)
38+
if (_lastPosition[i] < 0 || _lastPosition[1] >= image->_size[1])
39+
return;
40+
41+
erase(image, foreColor, glm::vec2(_lastPosition[0], _lastPosition[1]));
42+
}
43+
44+
void EraseActionFactory::PrimaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
45+
{
46+
for (int i = 0; i < 2; i++)
47+
if (_lastPosition[i] < 0 || _lastPosition[1] >= image->_size[1])
48+
return;
49+
50+
_isErasing = true;
51+
erase(image, foreColor, glm::vec2(_lastPosition[0], _lastPosition[1]));
52+
}
53+
54+
void EraseActionFactory::PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
55+
{
56+
_isErasing = false;
57+
}
58+
59+
void EraseActionFactory::SecondaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
60+
{ }
61+
62+
void EraseActionFactory::SecondaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
63+
{ }
64+
65+
GLuint EraseActionFactory::ToolHelperImage()
66+
{
67+
return 0;
68+
}

actions/eraseaction.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef ERASEACTION_H
2+
#define ERASEACTION_H
3+
4+
#include "baseaction.h"
5+
#include <glm/glm.hpp>
6+
7+
class EraseAction
8+
{
9+
public:
10+
EraseAction();
11+
};
12+
13+
class EraseActionFactory : public BaseActionFactory
14+
{
15+
void erase(Image* image, float color[], const glm::vec2& p);
16+
float _size;
17+
int _lastPosition[2];
18+
bool _isErasing;
19+
EraseActionFactory();
20+
public:
21+
static EraseActionFactory* Instance();
22+
23+
virtual void MouseMove(Image* image, int x, int y);
24+
virtual void PrimaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super);
25+
virtual void PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super);
26+
virtual void SecondaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super);
27+
virtual void SecondaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super);
28+
29+
virtual GLuint ToolHelperImage();
30+
};
31+
32+
#endif // ERASEACTION_H

actions/floodfillaction.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "floodfillaction.h"
2+
#include "../state.h"
23
#include "../image.h"
34

45
FloodFillAction::FloodFillAction()
@@ -20,29 +21,37 @@ void FloodFillActionFactory::MouseMove(Image* image, int x, int y)
2021
void FloodFillActionFactory::PrimaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
2122
{ }
2223

23-
void FloodFillActionFactory::PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
24+
void FloodFillActionFactory::floodFill(class Layer* layer, float color[])
2425
{
25-
static float foreColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
26-
auto layer = image->_layers[image->_selectedLayer];
2726
for (int y = 0; y < layer->_size[1]; ++y)
2827
{
2928
for (int x = 0; x < layer->_size[0]; ++x)
3029
{
3130
auto p = (x * layer->_size[1] + y);
3231
for (int b = 0; b < layer->_bpp; b++)
3332
{
34-
layer->_data[p * layer->_bpp + b] = foreColor[b] * 255;
33+
layer->_data[p * layer->_bpp + b] = color[b] * 255;
3534
}
3635
}
3736
}
37+
}
38+
39+
void FloodFillActionFactory::PrimaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
40+
{
41+
auto layer = image->_layers[image->_selectedLayer];
42+
floodFill(layer, foreColor);
3843
layer->setDirty();
3944
}
4045

4146
void FloodFillActionFactory::SecondaryMouseButtonDown(Image* image, bool shift, bool ctrl, bool alt, bool super)
4247
{ }
4348

4449
void FloodFillActionFactory::SecondaryMouseButtonUp(Image* image, bool shift, bool ctrl, bool alt, bool super)
45-
{ }
50+
{
51+
auto layer = image->_layers[image->_selectedLayer];
52+
floodFill(layer, backColor);
53+
layer->setDirty();
54+
}
4655

4756
GLuint FloodFillActionFactory::ToolHelperImage()
4857
{

actions/floodfillaction.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class FloodFillAction : public BaseAction
1111

1212
class FloodFillActionFactory : public BaseActionFactory
1313
{
14+
void floodFill(class Layer* layer, float color[]);
1415
FloodFillActionFactory();
1516
public:
1617
static FloodFillActionFactory* Instance();

0 commit comments

Comments
 (0)