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 14, 2020
1 parent a8e5335 commit 03b8e20
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 172 deletions.
Binary file added newDa/AlphaShow.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 newDa/cdrc.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/tk_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 1 addition & 7 deletions src/Brush.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pygame as pg
from src.lib import *
from src.Layer import Layer


Expand Down Expand Up @@ -82,12 +82,6 @@ def SetBrush(self, brush):
def SetCurrentLayer(self, layer: Layer):
self._layer = layer

def LayerChanged(self):
return self._layer.IsChanged()

def LayerApplied(self):
self._layer.Applied()

# ----- for test -----
def DrawLine(self, x0, y0, x1, y1):
# print('draw', (x0, y0), (x1, y1))
Expand Down
14 changes: 14 additions & 0 deletions src/Command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from src.lib import *


class Command:
key: list

def GetInput(self):
self.key = pg.key.get_pressed()

def GetKey(self, key):
return self.key[key]


Command = Command()
110 changes: 0 additions & 110 deletions src/HWwindow.py

This file was deleted.

27 changes: 15 additions & 12 deletions src/Layer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import numpy as np
import pygame as pg
from src.lib import *
from src import utility


class Layer:
def __init__(self, wid, hei, color=None):
def __init__(self, wid, hei, name, color=None):
self.w = wid
self.h = hei
self.resolution = (self.w, self.h)
self._surface = pg.Surface((wid, hei), pg.SRCALPHA, 32)
self._hasChange = True
self.name = name

if color is not None:
self._surface.fill(color)
Expand All @@ -30,6 +30,9 @@ def SetPixel(self, x, y, color):
def GetPixel(self, x, y) -> (int, int, int, int):
return utility.INT2RGBA(self._pixel[x][y])

def SetName(self, name):
self.name = name

def Paste(self, x, y, layer):
if isinstance(layer, Layer):
xs, ys = max(x, 0), max(y, 0)
Expand All @@ -46,7 +49,7 @@ def CropLayer(self, xRange, yRange):
_xs, _xe = max(_xs, 0), min(_xe, self.w)
_ys, _ye = max(_ys, 0), min(_ye, self.h)
_w, _h = _xe - _xs, _ye - _ys
_layer = Layer(_w, _h)
_layer = Layer(_w, _h, '')
_layer._pixel = self._pixel[_xs:_xe, _ys:_ye]
return _layer

Expand All @@ -63,29 +66,29 @@ def Applied(self):
self._hasChange = False

@staticmethod
def FromArray(array: np.ndarray):
def FromArray(array: np.ndarray, name):
try:
_w, _h = array.shape
except ValueError:
return None
_layer = Layer(_w, _h)
_layer = Layer(_w, _h, name)
_layer._surface = pg.surfarray.make_surface(array)
_layer._pixel = pg.surfarray.pixels2d(_layer._surface)
return _layer

@staticmethod
def FromSurface(surface: pg.Surface):
def FromSurface(surface: pg.Surface, name):
_w, _h = surface.get_size()
_layer = Layer(_w, _h)
_layer = Layer(_w, _h, name)
_layer._surface = surface
_layer._pixel = pg.surfarray.pixels2d(surface)
return _layer

@staticmethod
def Empty(wid, hei):
return Layer(wid, hei)
def Empty(wid, hei, name):
return Layer(wid, hei, name)

@staticmethod
def Solid(wid, hei, color):
return Layer(wid, hei, color)
def Solid(wid, hei, color, name):
return Layer(wid, hei, color, name)

31 changes: 18 additions & 13 deletions src/Section/CanvasSection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pygame as pg
from src.lib import *
from src.Section._Section import Section
from src.Brush import Brush
from src.Sprite import Sprite
from src import utility
from math import ceil


Expand All @@ -26,13 +27,17 @@ class CanvasSection(Section):
_gray1.fill((127, 127, 127, 255))
_gray2.fill((192, 192, 192, 255))

def SetupCanvas(self, w, h):
self.sprite = Sprite.Empty(w, h)
self.canvasWidth = w
self.canvasHeight = h
self.canvas = pg.Rect(0, 0, w * self.magnification, h * self.magnification)
def Setup(self, x, y, w, h):
super().Setup(x, y, w, h)
self.SetupCanvas()

def SetupCanvas(self):
# self.sprite = Sprite.Empty(w, h)
self.canvasWidth = Sprite.w
self.canvasHeight = Sprite.h
self.canvas = pg.Rect(0, 0, self.canvasWidth * self.magnification, self.canvasHeight * self.magnification)
self.canvas.center = self.LocalPosition(self.rect.center)
self.canvasSurface = self.sprite.GetSurface()
self.canvasSurface = Sprite.GetSurface()

self.backgroundOriginal = pg.Surface((self.canvasWidth, self.canvasHeight), pg.SRCALPHA, 32)
for _x in range(ceil(self.canvasWidth / self.tileSize)):
Expand All @@ -44,8 +49,8 @@ def SetupCanvas(self, w, h):
self.background = pg.transform.scale(self.backgroundOriginal, self.canvas.size)

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

Expand All @@ -59,8 +64,8 @@ def Magnify(self, mag, pivot):
if self.canvas.collidepoint(*pivot):
p_x, p_y = pivot
else:
p_x = max(min(pivot[0], self.canvas.x + self.canvas.w), self.canvas.x)
p_y = max(min(pivot[1], self.canvas.y + self.canvas.h), self.canvas.y)
p_x = utility.Clamp(pivot[0], self.canvas.x + self.canvas.w, self.canvas.x)
p_y = utility.Clamp(pivot[1], self.canvas.y + self.canvas.h, self.canvas.y)
dx = (self.canvas.x - p_x) / self.magnification
dy = (self.canvas.y - p_y) / self.magnification
self.magnification += mag
Expand All @@ -87,11 +92,11 @@ def Update(self):
self.canvas.h + 2 * self.canvasOutlineWidth),
1)
self.surface.blit(self.background, self.canvas.topleft)
self.surface.blit(pg.transform.scale(self.sprite.GetSurface(), self.canvas.size), self.canvas.topleft)
self.surface.blit(pg.transform.scale(Sprite.GetSurface(), self.canvas.size), self.canvas.topleft)
# ----- for test -----
# TimerStart()
# for _ in range(100):
# _toScale = self.sprite.GetSurface()
# _toScale = Sprite.GetSurface()
# TimerEnd()
# TimerStart()
# for _ in range(100):
Expand Down
44 changes: 42 additions & 2 deletions src/Section/ColorSection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pygame as pg
from src.Section._Section import Section
from src.Brush import Brush
from src.Command import Command
from src import utility
from math import sin, cos, atan2, degrees, radians, sqrt

Expand All @@ -18,6 +19,7 @@ class ColorSection(Section):
colorRGB = (166, 106, 150)
colorHSV = utility.RGB2HSV(colorRGB)
alpha = 255
scrollStep = 5

colorWheelImage = pg.image.load('data/hue4.png')
valueWheelImage = pg.image.load('data/value_wheel.png')
Expand All @@ -36,6 +38,8 @@ class ColorSection(Section):
alphaImage = pg.image.load('data/alpha.png')
valueBarRect: pg.Rect
alphaBarRect: pg.Rect
previewAlphaImage = pg.image.load('data/preview_alpha.png')
preViewSurface = pg.Surface(previewAlphaImage.get_size(), pg.SRCALPHA, 32)

colorChange = [0, 0, 0]

Expand All @@ -62,6 +66,7 @@ def Update(self):
self.surface.fill(self.bgColor)
self.DrawColorWheel()
self.DrawColorBar()
self.DrawPreview()

def SetColorRGB(self, color: (int, int, int)):
if len(color) == 4:
Expand Down Expand Up @@ -113,6 +118,11 @@ def DrawColorBar(self):
self.surface.blit(self.dotImage, (self.alphaBarRect.x + self.alpha / 255 * self.barWidth - self.dotRadius,
self.alphaBarRect.centery - self.dotRadius))

def DrawPreview(self):
self.preViewSurface.fill((*self.colorRGB, self.alpha))
self.surface.blit(self.previewAlphaImage, (20, 310))
self.surface.blit(self.preViewSurface, (20, 310))

def Position2HSV(self, x, y) -> (float, float, float):
_theta = atan2(self.wheelCenterY - y, x - self.wheelCenterX)
_h = 90 - degrees(_theta)
Expand All @@ -123,13 +133,23 @@ def Position2HSV(self, x, y) -> (float, float, float):
return _h, _s, _v

def Position2Value(self, x) -> (float, float, float):
_dx = max(min(x - self.valueBarRect.x, self.barWidth), 0)
_dx = utility.Clamp(x - self.valueBarRect.x, self.barWidth, 0)
_v = _dx / self.barWidth * 100
_h, _s, _ = self.colorHSV
return _h, _s, _v

def DiffHSV(self, dh=0, ds=0, dv=0):
_h = self.colorHSV[H] + dh
if _h >= 360:
_h -= 360
elif _h < 0:
_h += 360
_s = utility.Clamp(self.colorHSV[S] + ds, 100, 0)
_v = utility.Clamp(self.colorHSV[V] + dv, 100, 0)
self.SetColorHSV((_h, _s, _v))

def Position2Alpha(self, x) -> int:
_dx = max(min(x - self.valueBarRect.x, self.barWidth), 0)
_dx = utility.Clamp(x - self.valueBarRect.x, self.barWidth, 0)
return round(_dx / self.barWidth * 255)

def DistToOrigin(self, x, y) -> float:
Expand All @@ -144,6 +164,26 @@ def OnMouseDown(self, button, x, y):
self.colorChange[VALUE] = 1
elif self.alphaBarRect.collidepoint(x, y):
self.colorChange[ALPHA] = 1
elif button == 4:
if self.DistToOrigin(x, y) < self.radius + self.radiusTerm:
if Command.GetKey(pg.K_LCTRL):
self.DiffHSV(dh=self.scrollStep)
else:
self.DiffHSV(ds=self.scrollStep)
elif self.valueBarRect.collidepoint(x, y):
self.DiffHSV(dv=self.scrollStep)
elif self.alphaBarRect.collidepoint(x, y):
self.SetAlpha(utility.Clamp(self.alpha + self.scrollStep, 255, 0))
elif button == 5:
if self.DistToOrigin(x, y) < self.radius + self.radiusTerm:
if Command.GetKey(pg.K_LCTRL):
self.DiffHSV(dh=-self.scrollStep)
else:
self.DiffHSV(ds=-self.scrollStep)
elif self.valueBarRect.collidepoint(x, y):
self.DiffHSV(dv=-self.scrollStep)
elif self.alphaBarRect.collidepoint(x, y):
self.SetAlpha(utility.Clamp(self.alpha - self.scrollStep, 255, 0))

def OnMouseDrag(self, button, x, y, _x, _y):
x, y = self.LocalPosition((x, y))
Expand Down
4 changes: 2 additions & 2 deletions src/Section/FrameSection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pygame as pg
from src.lib import *
from src.Section._Section import Section


class FrameSection(Section):
bgColor = (32, 32, 32)
bgColor = (43, 43, 43)
Loading

0 comments on commit 03b8e20

Please sign in to comment.