Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
Signed-off-by: Thxios <[email protected]>
  • Loading branch information
Thxios committed Mar 29, 2020
1 parent d1b3af7 commit 585c8d5
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 90 deletions.
Binary file added newDa/brushImg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/brushes/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/brushes/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/brushes/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/brushes/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/brushes/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/brushsection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 56 additions & 67 deletions src/Brush.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from src.lib import *
from src.Layer import Layer
from src.Interaction import Interaction


_pencil = 0
Expand Down Expand Up @@ -30,47 +31,49 @@ def SetCurrentColor(self, color):


class _PencilBrush(_Brush):
def __init__(self, brush):
self.brush = brush

# ----- for test -----
brush = [
np.array([
[1],
], dtype=np.uint32).T,
np.array([
[1, 1],
[1, 1],
], dtype=np.uint32).T,
np.array([
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
], dtype=np.uint32).T,
np.array([
[0, 1, 1, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[0, 1, 1, 0],
], dtype=np.uint32).T,
np.array([
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
], dtype=np.uint32).T,
]

brushSize = 4
def SetBrush(self, brush):
self.brush = brush

def OnMouseDown(self, clickedPixel, layer: Layer):
# layer.SetPixel(*clickedPixel, self.currentColor)
layer.BrushDown(*clickedPixel, self.brush[self.brushSize], self.currentColor)
layer.BrushDown(*clickedPixel, self.brush, self.currentColor)


class _EraserBrush(_Brush):
def __init__(self, brush):
self.brush = brush

# ----- for test -----
brush = [
def SetBrush(self, brush):
self.brush = brush

def OnMouseDown(self, clickedPixel, layer: Layer):
layer.BrushDown(*clickedPixel, self.brush, self.currentColor)


class _FloodBrush(_Brush):
def OnMouseDown(self, clickedPixel, layer: Layer):
pass


class _PickerBrush(_Brush):
def OnMouseDown(self, clickedPixel, layer: Layer) -> (int, int, int, int):
self.currentColor = layer.GetPixel(*clickedPixel)
return self.currentColor


class Brush:
_layer: Layer

_currentBrush: _Brush
currentBrushIdx: int

_maxThickness = 5
_brushThickness = 5
brushArray = [
None,
np.array([
[1],
], dtype=np.uint32).T,
Expand Down Expand Up @@ -98,38 +101,11 @@ class _EraserBrush(_Brush):
], dtype=np.uint32).T,
]

brushSize = 4


def OnMouseDown(self, clickedPixel, layer: Layer):
layer.BrushDown(*clickedPixel, self.brush[self.brushSize], self.currentColor)

def OnMouseDrag(self, clickedPixel, layer: Layer):
layer.SetPixel(*clickedPixel, self.currentColor)


class _FloodBrush(_Brush):
def OnMouseDown(self, clickedPixel, layer: Layer):
pass


class _PickerBrush(_Brush):
def OnMouseDown(self, clickedPixel, layer: Layer) -> (int, int, int, int):
self.currentColor = layer.GetPixel(*clickedPixel)
return self.currentColor


class Brush:
_layer: Layer

pencil = _PencilBrush()
eraser = _EraserBrush()
pencil = _PencilBrush(brushArray[_brushThickness])
eraser = _EraserBrush(brushArray[_brushThickness])
flood = _FloodBrush()
picker = _PickerBrush()

_currentBrush: _Brush
currentBrushIdx: int

def __init__(self):
self.SetBrush('Pencil')

Expand All @@ -141,10 +117,11 @@ def OnMouseDrag(self, clickedPixel, previousPixel=None):
_pixelX, _pixelY = clickedPixel
_prePixelX, _prePixelY = previousPixel
if self.currentBrushIdx == 0 or self.currentBrushIdx == 1:
if abs(_prePixelX - _pixelX) > 1 or abs(_prePixelY - _pixelY) > 1:
self._DrawLine(*clickedPixel, *previousPixel)
else:
self._currentBrush.OnMouseDown(clickedPixel, self._layer)
if self._layer.visible:
if abs(_prePixelX - _pixelX) > 1 or abs(_prePixelY - _pixelY) > 1:
self._DrawLine(*clickedPixel, *previousPixel)
else:
self._currentBrush.OnMouseDown(clickedPixel, self._layer)
else:
self._currentBrush.OnMouseDrag(clickedPixel, self._layer)

Expand Down Expand Up @@ -178,9 +155,21 @@ def SetCurrentColor(self, color):
self.pencil.SetCurrentColor((_r, _g, _b, _a))
self.flood.SetCurrentColor((_r, _g, _b, _a))

def GetCurrentBrushIndex(self):
def GetCurrentBrushIndex(self) -> int:
return self.currentBrushIdx

def GetBrushThickness(self) -> int:
return self._brushThickness

def BrushMagnify(self, value):
if 0 < self._brushThickness + value <= self._maxThickness:
self._brushThickness += value
self.pencil.SetBrush(self.brushArray[self._brushThickness])
self.eraser.SetBrush(self.brushArray[self._brushThickness])

# ----- for test -----
Interaction.brushSection.Changed()

# ----- for test -----
def _DrawLine(self, x0, y0, x1, y1):
dx = x1 - x0
Expand Down
24 changes: 20 additions & 4 deletions src/Section/_BrushSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,37 @@ class BrushSection(Section):

currentBrush = Brush.GetCurrentBrushIndex()

brushThicknessBG = pg.transform.scale(pg.image.load('data/brushes/brushBG.png'), (60, 60))
brushThicknessImage = [None] + [
pg.transform.scale(pg.image.load('data/brushes/' + str(i) + '.png'), (60, 60)) for i in range(1, 6)
]

def Setup(self, x, y, w, h):
super().Setup(x, y, w, h)
for _brush in range(self.brushCount):
self.buttonRect.append(
pg.Rect(6, 6 + (self.buttonSize + self.buttonTerm) * _brush, self.buttonSize, self.buttonSize)
pg.Rect(self.w - 36,
6 + (self.buttonSize + self.buttonTerm) * _brush,
self.buttonSize,
self.buttonSize)
)
# print('brushSection', self.x, self.y, self.w, self.h)

def Update(self):
self.surface.fill(self.bgColor)
for _brush in range(self.brushCount):
if _brush == self.currentBrush:
self.surface.blit(self.buttonSelectedImage, (6, 6 + (self.buttonSize + self.buttonTerm) * _brush))
# self.surface.blit(self.buttonSelectedImage, (6, 6 + (self.buttonSize + self.buttonTerm) * _brush))
self.surface.blit(self.buttonSelectedImage,
(self.w - 36, 6 + (self.buttonSize + self.buttonTerm) * _brush))
else:
self.surface.blit(self.buttonImage, (6, 6 + (self.buttonSize + self.buttonTerm) * _brush))
self.surface.blit(self.buttonIconImage, (0, 0))
# self.surface.blit(self.buttonImage, (6, 6 + (self.buttonSize + self.buttonTerm) * _brush))
self.surface.blit(self.buttonImage,
(self.w - 36, 6 + (self.buttonSize + self.buttonTerm) * _brush))
self.surface.blit(self.buttonIconImage, (self.w - 42, 0))

self.surface.blit(self.brushThicknessBG, (20, 160))
self.surface.blit(self.brushThicknessImage[Brush.GetBrushThickness()], (20, 160))

def OnMouseDown(self, button, x, y):
x, y = self.LocalPosition((x, y))
Expand Down
11 changes: 5 additions & 6 deletions src/Section/_CanvasSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class CanvasSection(Section):
# ----- for test -----
limit = 20
cursorColor = (60, 63, 65)
brushSize = 5

def Setup(self, x, y, w, h):
super().Setup(x, y, w, h)
Expand All @@ -42,7 +41,7 @@ def Draw(self, screen):
self.Update()
self._hasChange = False
_surface = self.surface.copy()
self.DrawCursor(_surface, )
self.DrawCursor(_surface)
screen.blit(_surface, (self.x, self.y))
pg.display.update(pg.draw.rect(screen, self._outlineColor, self.rect, 3))

Expand Down Expand Up @@ -104,8 +103,8 @@ def Magnify(self, mag, pivot):
def DrawCursor(self, surface):
_mouseX, _mouseY = pg.mouse.get_pos()
if self.IsClicked((_mouseX, _mouseY)):
pg.draw.circle(surface, self.cursorColor,
self.LocalPosition((_mouseX, _mouseY)), self.magnification * self.brushSize // 2, 1)
pg.draw.circle(surface, self.cursorColor, self.LocalPosition((_mouseX, _mouseY)),
self.magnification * Brush.GetBrushThickness() // 2, 1)

def DisplayArea(self):
_xs, _xe = max(0, self.canvas.x), min(self.w, self.canvas.x + self.canvas.w)
Expand Down Expand Up @@ -164,12 +163,12 @@ def OnMouseDown(self, button, x, y):
# self.Changed()
elif button == 4:
if Command.GetKey(pg.K_LCTRL):
pass
Brush.BrushMagnify(1)
else:
self.Magnify(1, (x, y))
elif button == 5:
if Command.GetKey(pg.K_LCTRL):
pass
Brush.BrushMagnify(-1)
else:
self.Magnify(-1, (x, y))

Expand Down
36 changes: 29 additions & 7 deletions src/Section/_LayerSection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from src.lib import *
from src.Section._Section import Section
from src.Sprite import Sprite
from src.Interaction import Interaction
from src.Text import Text


Expand All @@ -14,6 +15,7 @@ class LayerSection(Section):
leftTerm: int = 20
layerRectHeight: int = 30
layerRect: List[pg.Rect] = []
visibleButtonRect: List[pg.Rect] = []
layerName: List[str] = Sprite.CurrentFrame().GetLayerName()

layerColor: (int, int, int) = (60, 63, 65)
Expand All @@ -38,15 +40,30 @@ def Update(self):
pg.draw.rect(self.surface,
self.selectedColor,
rect.inflate(-2 * self.verticalTerm, -2 * self.verticalTerm))
pg.draw.rect(self.surface, self.buttonColor, (rect.right - 4 - 22, rect.top + 4, 22, 22), 2)
pg.draw.rect(self.surface, self.buttonColor, self.visibleButtonRect[i], 1)
# pg.draw.rect(self.surface, self.buttonColor, (rect.right - 4 - 22, rect.top + 4, 22, 22), 1)
# pg.draw.rect(self.surface, self.buttonColor, (rect.right - 5 - 22, rect.top + 3, 24, 24), 1)
if Sprite.LayerVisible(i):
self.surface.blit(self.visibleIconImage, self.visibleButtonRect[i].topleft)
else:
self.surface.blit(self.invisibleIconImage, self.visibleButtonRect[i].topleft)
Text.LeftAligned(self.layerName[i], self.surface, rect, term=10)

def MakeRect(self):
for i in range(self.layerCount):
self.layerRect.append(pg.Rect(self.rectTerm,
self.rectTerm + (self.layerRectHeight + self.verticalTerm) * i,
self.w - self.rectTerm * 2 - self.leftTerm,
self.layerRectHeight))
_rect = pg.Rect(
self.rectTerm,
self.rectTerm + (self.layerRectHeight + self.verticalTerm) * i,
self.w - self.rectTerm * 2 - self.leftTerm,
self.layerRectHeight
)
self.layerRect.append(_rect)
self.visibleButtonRect.append(pg.Rect(
_rect.right - 4 - 22,
_rect.top + 4,
22,
22
))

def SetLayer(self, idx):
if idx < self.layerCount:
Expand All @@ -60,6 +77,11 @@ def OnMouseDown(self, button, x, y):
if button == 1:
for i, rect in enumerate(self.layerRect):
if rect.collidepoint(x, y):
Sprite.SetCurrentLayer(i)
self.selectedLayer = i
if self.visibleButtonRect[i].collidepoint(x, y):
Sprite.GetLayer(i).visible = not Sprite.GetLayer(i).visible
Sprite.CurrentLayer().Changed()
Interaction.canvasSection.Changed()
else:
Sprite.SetCurrentLayer(i)
self.selectedLayer = i
self.Changed()
8 changes: 7 additions & 1 deletion src/Sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def SetCurrentLayer(self, idx):
self._currentLayer = idx
Brush.SetCurrentLayer(self.CurrentFrame().GetLayer(idx))

def GetLayer(self, idx) -> Layer:
return self._frame[self._currentFrame].GetLayer(idx)

def CurrentFrame(self) -> Frame:
return self._frame[self._currentFrame]

Expand All @@ -100,7 +103,10 @@ def FrameCount(self) -> int:
return self._frameCount

def LayerCount(self) -> int:
return self.CurrentFrame().LayerCount()
return self._frame[self._currentFrame].LayerCount()

def LayerVisible(self, idx) -> bool:
return self._frame[self._currentFrame].GetLayer(idx).visible

def SetFrame(self, idx):
if idx < 0 or idx > self._frameCount:
Expand Down
20 changes: 15 additions & 5 deletions src/Window.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,37 @@ class MainWindow:

_topTerm = 30
_left = 250
_middle = 52
_l1 = 250
_l2 = 400 - _topTerm
_l3 = 250
_r1 = 800
_r2 = 100 - _topTerm
_bs = 52
_middle = 130

# CanvasSection.Setup(_left, _topTerm, w - _left, _r1)
CanvasSection.Setup(_left + _middle, _topTerm, w - _left - _middle, _r1)
# ORIGINAL
# PaletteSection.Setup(0, _topTerm, _left - _bs, _l1)
PaletteSection.Setup(0, _topTerm, _left, _l1)
# CanvasSection.Setup(_left, _topTerm, w - _left, _r1)
# BrushSection.Setup(_left - _bs, _topTerm, _bs, _l1)
BrushSection.Setup(_left, _topTerm, _middle, _l1 + _l2 + _l3)
# FrameSection.Setup(_left, _r1 + _topTerm, w - _left, _r2)

# CanvasSection.Setup(_left + _middle, _topTerm, w - _left - _middle, _r1)
# PaletteSection.Setup(0, _topTerm, _left, _l1)
# BrushSection.Setup(_left, _topTerm, _middle, _l1 + _l2 + _l3)
# FrameSection.Setup(_left + _middle, _r1 + _topTerm, w - _left - _middle, _r2)

PaletteSection.Setup(_left, _topTerm, _middle, _l1 + _l2 + _l3)
CanvasSection.Setup(_left + _middle, _topTerm, w - _left - _middle, _r1)
BrushSection.Setup(0, _topTerm, _left, _l1)
FrameSection.Setup(_left + _middle, _r1 + _topTerm, w - _left - _middle, _r2)

ColorSection.Setup(0, _l1 + _topTerm, _left, _l2)
LayerSection.Setup(0, _l1 + _l2 + _topTerm, _left, _l3)

# ----- for test -----
Interaction.Initialize(
brush=BrushSection,
canvas=CanvasSection,
palette=PaletteSection,
color=ColorSection
)
Expand Down
Binary file added src/data/brushes/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/brushes/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/brushes/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/brushes/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/brushes/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/brushes/brushBG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/data/invisibleIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/data/visibleIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 585c8d5

Please sign in to comment.