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 16, 2020
1 parent 03b8e20 commit c607ada
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 86 deletions.
8 changes: 4 additions & 4 deletions img2.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from PIL import Image

img = Image.open('newDa/hue4.png')
img = Image.open('newDa/button_only.png')
img = img.convert('RGBA')
pixel = img.load()
w, h = img.size
for x in range(w):
for y in range(h):
if pixel[x, y] != (0, 0, 0, 0):
pixel[x, y] = (0, 0, 0, 255)
if pixel[x, y] == (0, 255, 0, 255):
pixel[x, y] = (0, 0, 0, 0)

img.save('newDa/value_wheel.png')
img.save('newDa/button_only.png')
Binary file added newDa/BrushButton.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/BrushButton_selected.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/BrushSnew.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/button_only.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 47 additions & 26 deletions src/Brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
from src.Layer import Layer


_pencil = 0
_eraser = 1
_flood = 2
_picker = 3


class _Brush:
currentColor = (0, 0, 0, 0)

Expand Down Expand Up @@ -59,78 +65,93 @@ class Brush:
picker = _PickerBrush()

_currentBrush: _Brush
currentBrushIdx: int

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

def OnMouseDown(self, clickedPixel):
self._currentBrush.OnMouseDown(clickedPixel, self._layer)

def OnMouseDrag(self, clickedPixel):
self._currentBrush.OnMouseDrag(clickedPixel, self._layer)
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)
else:
self._currentBrush.OnMouseDrag(clickedPixel, self._layer)

def OnMouseUp(self, clickedPixel):
self._currentBrush.OnMouseUp(clickedPixel, self._layer)

def SetBrush(self, brush):
if brush == 'Pencil' or brush == 0:
if brush == 'Pencil' or brush == _pencil:
self._currentBrush = self.pencil
elif brush == 'Eraser' or brush == 1:
self.currentBrushIdx = 0
elif brush == 'Eraser' or brush == _eraser:
self._currentBrush = self.eraser
elif brush == 'Flood' or brush == 2:
self.currentBrushIdx = 1
elif brush == 'Flood' or brush == _flood:
self._currentBrush = self.flood
elif brush == 'Picker' or brush == 3:
self.currentBrushIdx = 2
elif brush == 'Picker' or brush == _picker:
self._currentBrush = self.picker
self.currentBrushIdx = 3

def SetCurrentLayer(self, layer: Layer):
self._layer = layer

def SetCurrentColor(self, color):
if len(color) == 3:
_r, _g, _b = color
_a = 255
else:
_r, _g, _b, _a = color

self.pencil.SetCurrentColor((_r, _g, _b, _a))
self.flood.SetCurrentColor((_r, _g, _b, _a))

def GetCurrentBrushIndex(self):
return self.currentBrushIdx

# ----- for test -----
def DrawLine(self, x0, y0, x1, y1):
# print('draw', (x0, y0), (x1, y1))
def _DrawLine(self, x0, y0, x1, y1):
dx = x1 - x0
dy = y1 - y0
ab = 1 if dy > 0 else -1
if dx == 0:
for y in range(y0, y1 + ab, ab):
self.pencil.OnMouseDown((x0, y), self._layer)
self._currentBrush.OnMouseDown((x0, y), self._layer)
return
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
dy = -dy
ab = -ab
# print(' ')
# print((x0, y0), (x1, y1))
slope = abs(dy / dx)
# print(dx, dy)
# print(slope)
error = 0
if slope < 1:
y = y0
for x in range(x0, x1 + 1):
# print((x, y), error)
self.pencil.OnMouseDown((x, y), self._layer)
self._currentBrush.OnMouseDown((x, y), self._layer)
error += slope
if error > 0.5:
error -= 1
self.pencil.OnMouseDown((x, y), self._layer)
# print((x, y), error)
self._currentBrush.OnMouseDown((x, y), self._layer)
y += ab
else:
slope = 1 / slope
x = x0
for y in range(y0, y1 + ab, ab):
self.pencil.OnMouseDown((x, y), self._layer)
# print((x, y), error)
self._currentBrush.OnMouseDown((x, y), self._layer)
error += slope
if error > 0.5:
error -= 1
# print((x, y), error)
self.pencil.OnMouseDown((x, y), self._layer)
self._currentBrush.OnMouseDown((x, y), self._layer)
x += 1
# if error > 0.5:
# for _ in [0] * int(error - 0.5):
# self.pencil.OnMouseDown((x, y), self._layer)
# y += ab
# error -= int(error - 0.5)


Brush = Brush()
2 changes: 1 addition & 1 deletion src/Layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def CropLayer(self, xRange, yRange):
return _layer

def GetSurface(self) -> pg.Surface:
return self._surface
return self._surface.copy()

def GetArray(self) -> np.ndarray:
return self._pixel
Expand Down
43 changes: 43 additions & 0 deletions src/Section/BrushSection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from src.lib import *
from src.Section._Section import Section
from src.Brush import Brush


class BrushSection(Section):
bgColor = (43, 43, 43)

buttonImage = pg.image.load('data/BrushButton.png')
buttonSelectedImage = pg.image.load('data/BrushButton_selected.png')
buttonIconImage = pg.image.load('data/ButtonIcon.png')
buttonSize = buttonImage.get_width()
buttonTerm = 3
buttonRect: List[pg.Rect] = []

brushCount = 7

currentBrush = Brush.GetCurrentBrushIndex()

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)
)

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))
else:
self.surface.blit(self.buttonImage, (6, 6 + (self.buttonSize + self.buttonTerm) * _brush))
self.surface.blit(self.buttonIconImage, (0, 0))

def OnMouseDown(self, button, x, y):
x, y = self.LocalPosition((x, y))
for _brush, _button in enumerate(self.buttonRect):
if _button.collidepoint(x, y):
self.currentBrush = _brush
Brush.SetBrush(_brush)
self.Changed()
break
31 changes: 19 additions & 12 deletions src/Section/CanvasSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class CanvasSection(Section):
_gray1.fill((127, 127, 127, 255))
_gray2.fill((192, 192, 192, 255))

# ----- for test -----
limit = 20

def Setup(self, x, y, w, h):
super().Setup(x, y, w, h)
self.SetupCanvas()
Expand All @@ -49,13 +52,20 @@ def SetupCanvas(self):
self.background = pg.transform.scale(self.backgroundOriginal, self.canvas.size)

def SetCanvasPosition(self, x, y):
_x = utility.Clamp(x, self.w - self.canvas.w, 0)
_y = utility.Clamp(y, self.h - self.canvas.h, 0)
_x = utility.Clamp(x, self.w - self.limit, -self.canvas.w + self.limit)
_y = utility.Clamp(y, self.h - self.limit, -self.canvas.h + self.limit)
self.canvas.x = _x
self.canvas.y = _y

def MoveCanvas(self, dx, dy):
self.canvas.move_ip(dx, dy)
if self.canvas.x > self.w - self.limit or self.canvas.x < -self.canvas.w + self.limit:
self.canvas.x = utility.Clamp(self.canvas.x,
self.w - self.limit, -self.canvas.w + self.limit)
if self.canvas.y > self.h - self.limit or self.canvas.y < -self.canvas.h + self.limit:
self.canvas.y = utility.Clamp(self.canvas.y,
self.h - self.limit, -self.canvas.h + self.limit)
# print(self.canvas.topleft)
self.Changed()

def Magnify(self, mag, pivot):
Expand Down Expand Up @@ -94,22 +104,22 @@ def Update(self):
self.surface.blit(self.background, self.canvas.topleft)
self.surface.blit(pg.transform.scale(Sprite.GetSurface(), self.canvas.size), self.canvas.topleft)
# ----- for test -----
# TimerStart()
# utility.TimerStart()
# for _ in range(100):
# _toScale = Sprite.GetSurface()
# TimerEnd()
# TimerStart()
# utility.TimerEnd()
# utility.TimerStart()
# for _ in range(100):
# _toBlit = pg.transform.scale(_toScale, (self.canvas.w, self.canvas.h)) # 0.3578s
# TimerEnd()
# utility.TimerEnd()
# TimerStart()
# for _ in range(100):
# _temp = pg.transform.rotozoom(_toScale, 0, self.magnification)
# TimerEnd()
# TimerStart()
# utility.TimerStart()
# for _ in range(100):
# self.surface.blit(_toBlit, self.canvas.topleft) # 0.0713s
# TimerEnd()
# utility.TimerEnd()
# pg.quit()
# quit()

Expand Down Expand Up @@ -145,10 +155,7 @@ def OnMouseDrag(self, button, x, y, _x, _y):
_pixelX, _pixelY, _valid = self.PositionToPixel(x, y)
_prePixelX, _prePixelY, _preValid = self.PositionToPixel(_x, _y)
if _valid and _preValid:
if abs(_prePixelX - _pixelX) > 1 or abs(_prePixelY - _pixelY) > 1:
Brush.DrawLine(_prePixelX, _prePixelY, _pixelX, _pixelY)
else:
Brush.OnMouseDown((_pixelX, _pixelY))
Brush.OnMouseDrag((_pixelX, _pixelY), (_prePixelX, _prePixelY))
self.Changed()
elif button == 2:
self.MoveCanvas(x - _x, y - _y)
Expand Down
4 changes: 2 additions & 2 deletions src/Section/ColorSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ def SetColorHSV(self, color: (float, float, float)):
self.colorHSV = color
self.colorRGB = utility.HSV2RGB(color)
self.Changed()
Brush.pencil.SetCurrentColor((*self.colorRGB, self.alpha))
Brush.SetCurrentColor((*self.colorRGB, self.alpha))
if self.colorHSV[V] > 50:
self.dotImage = self.dotDarkImage
else:
self.dotImage = self.dotBrightImage

def SetAlpha(self, alpha):
self.alpha = alpha
Brush.pencil.SetCurrentColor((*self.colorRGB, self.alpha))
Brush.SetCurrentColor((*self.colorRGB, self.alpha))
self.Changed()

def DrawColorWheel(self):
Expand Down
45 changes: 35 additions & 10 deletions src/Section/LayerSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@


class LayerSection(Section):
bgColor = (43, 43, 43)
layerCount = Sprite.LayerCount()

bgColor: (int, int, int) = (43, 43, 43)
layerCount: int = Sprite.LayerCount()
selectedLayer: int = 0
# ----- for test -----
rectTerm = 15
verticalTerm = 7
layerRectHeight = 30
layerRect: [pg.Rect] = []
layerName = Sprite.CurrentFrame().GetLayerName()
rectTerm: int = 10
verticalTerm: int = 3
leftTerm: int = 20
layerRectHeight: int = 30
layerRect: List[pg.Rect] = []
layerName: List[str] = Sprite.CurrentFrame().GetLayerName()

layerColor: (int, int, int) = (60, 63, 65)
selectedColor: (int, int, int) = (75, 110, 175)

def Setup(self, x, y, w, h):
super().Setup(x, y, w, h)
Expand All @@ -22,14 +26,35 @@ def Setup(self, x, y, w, h):
self.MakeRect()

def Update(self):
# sprint('re')
self.surface.fill(self.bgColor)
for i, rect in enumerate(self.layerRect):
pg.draw.rect(self.surface, (255, 255, 255), rect, 2)
pg.draw.rect(self.surface, self.layerColor, rect)
if i == self.selectedLayer:
pg.draw.rect(self.surface,
self.selectedColor,
rect.inflate(-2 * self.verticalTerm, -2 * self.verticalTerm))
Text.LeftAligned(self.layerName[i], self.surface, rect, 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.w - self.rectTerm * 2 - self.leftTerm,
self.layerRectHeight))

def SetLayer(self, idx):
if idx < self.layerCount:
self.selectedLayer = idx
else:
raise IndexError(str(self.layerCount) + ' layer but given ' + str(idx))
self.Changed()

def OnMouseDown(self, button, x, y):
x, y = self.LocalPosition((x, y))
if button == 1:
for i, rect in enumerate(self.layerRect):
if rect.collidepoint(x, y):
Sprite.SetCurrentLayer(i)
self.selectedLayer = i
self.Changed()
2 changes: 2 additions & 0 deletions src/Section/Section.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from src.Section.FrameSection import FrameSection
from src.Section.ColorSection import ColorSection
from src.Section.LayerSection import LayerSection
from src.Section.BrushSection import BrushSection
from src.Section._Section import Section


Expand All @@ -12,4 +13,5 @@
FrameSection = FrameSection()
ColorSection = ColorSection()
LayerSection = LayerSection()
BrushSection = BrushSection()
Empty = Section()
Loading

0 comments on commit c607ada

Please sign in to comment.