Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
global refactoring 👀
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Nov 21, 2021
1 parent 29b693c commit 89e2254
Show file tree
Hide file tree
Showing 44 changed files with 525 additions and 446 deletions.
147 changes: 8 additions & 139 deletions src/nodesnim/graphics/drawable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import
../core/vector2,
../core/tools,

../private/templates,

strutils,
re

Expand All @@ -27,145 +29,12 @@ type
DrawableRef* = ref DrawableObj


let standard_shadow_color: ColorRef = Color(0f, 0f, 0f, 0.5f)

template drawablepattern*(`type`: untyped): untyped =
result = `type`(
texture: GlTextureObj(), border_width: 0,
border_detail: [8, 8, 8, 8],
border_radius: [0.float, 0, 0, 0],
border_color: Color(0, 0, 0, 0),
background_color: Color(0, 0, 0, 0),
shadow_offset: Vector2(0, 0), shadow: false,
shadow_color: standard_shadow_color
)
let standard_shadow_color*: ColorRef = Color(0f, 0f, 0f, 0.5f)

proc Drawable*: DrawableRef =
drawablepattern(DrawableRef)


template vd* =
## void template
discard

template recalc*(shadow: bool = false) =
## Calculates vertex positions.
let (xw, yh) = (x + width, y - height)
when not shadow:
# left top
for i in bezier_iter(1f/self.border_detail[0].float, Vector2(0, -self.border_radius[0]),
Vector2(0, 0), Vector2(self.border_radius[0], 0)):
vertex.add(Vector2(x + i.x, y + i.y))

# right top
for i in bezier_iter(1f/self.border_detail[1].float, Vector2(-self.border_radius[01], 0),
Vector2(0, 0), Vector2(0, -self.border_radius[1])):
vertex.add(Vector2(xw + i.x, y + i.y))

# right bottom
for i in bezier_iter(1f/self.border_detail[2].float, Vector2(0, -self.border_radius[2]),
Vector2(0, 0), Vector2(-self.border_radius[2], 0)):
vertex.add(Vector2(xw + i.x, yh - i.y))

# left bottom
for i in bezier_iter(1f/self.border_detail[3].float, Vector2(self.border_radius[3], 0),
Vector2(0, 0), Vector2(0, self.border_radius[3])):
vertex.add(Vector2(x + i.x, yh + i.y))
else:
glBegin(GL_QUAD_STRIP)
# left top
for i in bezier_iter(1f/self.border_detail[0].float, Vector2(0, -self.border_radius[0]),
Vector2(0, 0), Vector2(self.border_radius[0], 0)):
glColor4f(0, 0, 0, 0)
glVertex2f(x + i.x + self.shadow_offset.x, y + i.y - self.shadow_offset.y)
glColor(self.shadow_color)
glVertex2f(x + i.x, y + i.y)

# right top
for i in bezier_iter(1f/self.border_detail[1].float, Vector2(-self.border_radius[1], 0),
Vector2(0, 0), Vector2(0, -self.border_radius[1])):
glColor4f(0, 0, 0, 0)
glVertex2f(xw + i.x + self.shadow_offset.x, y + i.y - self.shadow_offset.y)
glColor(self.shadow_color)
glVertex2f(xw + i.x, y + i.y)

# right bottom
for i in bezier_iter(1f/self.border_detail[2].float, Vector2(0, -self.border_radius[2]),
Vector2(0, 0), Vector2(-self.border_radius[2], 0)):
glColor4f(0, 0, 0, 0)
glVertex2f(xw + i.x + self.shadow_offset.x, yh - i.y - self.shadow_offset.y)
glColor(self.shadow_color)
glVertex2f(xw + i.x, yh - i.y)

# left bottom
for i in bezier_iter(1f/self.border_detail[3].float, Vector2(self.border_radius[3], 0),
Vector2(0, 0), Vector2(0, self.border_radius[3])):
glColor4f(0, 0, 0, 0)
glVertex2f(x + i.x + self.shadow_offset.x, yh + i.y - self.shadow_offset.y)
glColor(self.shadow_color)
glVertex2f(x + i.x, yh + i.y)

glColor4f(0, 0, 0, 0)
glVertex2f(x + self.shadow_offset.x, y - self.border_radius[0] - self.shadow_offset.y)
glColor(self.shadow_color)
glVertex2f(x, y - self.border_radius[0])
glEnd()


template draw_template*(drawtype, color, function, secondfunc: untyped): untyped =
## Draws colorized vertexes
##
## Arguments:
## - `drawtype` - draw type, like `GL_POLYGON`
## - `color` - color for border drawing.
## - `function` - function called before `glBegin`
## - `secondfunc` - function called after `glEnd`
glColor4f(`color`.r, `color`.g, `color`.b, `color`.a)
`function`
glBegin(`drawtype`)

for i in vertex:
glVertex2f(i.x, i.y)

glEnd()
`secondfunc`

template draw_texture_template*(drawtype, color, function, secondfunc: untyped): untyped =
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture.texture)
glColor4f(`color`.r, `color`.g, `color`.b, `color`.a)
`function`
glBegin(`drawtype`)
var
texture_size = self.texture.size
h = height
w = width
if texture_size.x < width:
let q = width / texture_size.x
texture_size.x *= q
texture_size.y *= q

if texture_size.y < height:
let q = height / texture_size.y
texture_size.x *= q
texture_size.y *= q

# crop .. :eyes:
let q = width / texture_size.x
texture_size.x *= q
texture_size.y *= q
h /= height/width

for i in vertex:
glTexCoord2f((-x + i.x - w + texture_size.x) / texture_size.x,
(y - i.y - h + texture_size.y) / texture_size.y)
glVertex2f(i.x, i.y)

glEnd()
`secondfunc`
glDisable(GL_TEXTURE_2D)


method enableShadow*(self: DrawableRef, val: bool = true) {.base.} =
## Enables shadow, when `val` is true.
self.shadow = val
Expand All @@ -177,15 +46,15 @@ method draw*(self: DrawableRef, x1, y1, width, height: float) {.base.} =
y = y1

if self.shadow:
recalc(true)
recalc()
calculateDrawableCorners(true)
calculateDrawableCorners()

if self.texture.texture > 0'u32:
draw_texture_template(GL_POLYGON, self.background_color, vd(), vd())
drawTextureTemplate(GL_POLYGON, self.background_color, voidTemplate(), voidTemplate())
else:
draw_template(GL_POLYGON, self.background_color, vd(), vd())
drawTemplate(GL_POLYGON, self.background_color, voidTemplate(), voidTemplate())
if self.border_width > 0f:
draw_template(GL_LINE_LOOP, self.border_color, glLineWidth(self.border_width), glLineWidth(1))
drawTemplate(GL_LINE_LOOP, self.border_color, glLineWidth(self.border_width), glLineWidth(1))


method getColor*(self: DrawableRef): ColorRef {.base.} =
Expand Down
47 changes: 8 additions & 39 deletions src/nodesnim/graphics/gradient_drawable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import
../core/stylesheet,
../core/image,
../core/vector2,
../core/tools,

../private/templates,

drawable,
strutils
Expand All @@ -23,56 +26,22 @@ proc GradientDrawable*: GradientDrawableRef =
Color(1f, 1f, 1f, 1.0),
Color(1f, 1f, 1f, 1.0)]


template draw_template*(drawtype, color, function, secondfunc: untyped, is_gradient: bool = true): untyped =
## Draws colorized vertexes
##
## Arguments:
## - `drawtype` - draw type, like `GL_POLYGON`
## - `color` - color for border drawing.
## - `function` - function called before `glBegin`
## - `secondfunc` - function called after `glEnd`
## - `is_gradient` - true when drawtype is `GL_POLYGON`.
glColor4f(`color`.r, `color`.g, `color`.b, `color`.a)
`function`
glBegin(`drawtype`)

if is_gradient:
for i in 0..vertex.high:
let tmp = i/vertex.len()
if tmp < 0.25:
glColor(self.corners[0])
elif tmp < 0.5:
glColor(self.corners[1])
elif tmp < 0.75:
glColor(self.corners[2])
else:
glColor(self.corners[3])
glVertex2f(vertex[i].x, vertex[i].y)
else:
for i in vertex:
glVertex2f(i.x, i.y)

glEnd()
`secondfunc`

method draw*(self: GradientDrawableRef, x1, y1, width, height: float) =
var
vertex: seq[Vector2Obj] = @[]
x = x1
y = y1

if self.shadow:
recalc(true)

recalc()
calculateDrawableCorners(true)
calculateDrawableCorners()

if self.texture.texture > 0'u32:
draw_texture_template(GL_POLYGON, self.background_color, vd(), vd())
drawTextureTemplate(GL_POLYGON, self.background_color, voidTemplate(), voidTemplate())
else:
draw_template(GL_POLYGON, self.background_color, vd(), vd())
drawTemplate(GL_POLYGON, self.background_color, voidTemplate(), voidTemplate(), true)
if self.border_width > 0f:
draw_template(GL_LINE_LOOP, self.border_color, glLineWidth(self.border_width), glLineWidth(1), false)
drawTemplate(GL_LINE_LOOP, self.border_color, glLineWidth(self.border_width), glLineWidth(1), false)

method setCornerColors*(self: GradientDrawableRef, c0, c1, c2, c3: ColorRef) {.base.} =
## Changes corners colors
Expand Down
5 changes: 3 additions & 2 deletions src/nodesnim/nodes/animation_player.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# author: Ethosa
## Provides animation in most nodes
import
node,
../thirdparty/opengl,
../core/enums,
../core/tools,
../thirdparty/opengl
../private/templates,
node


type
Expand Down
5 changes: 3 additions & 2 deletions src/nodesnim/nodes/audio_stream_player.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
##
## AudioStream is responsible for audio. You can play multiple audio recordings at once.
import
node,
../thirdparty/sdl2/mixer,
../core/enums,
../core/audio_stream
../core/audio_stream,
../private/templates,
node


type
Expand Down
Loading

0 comments on commit 89e2254

Please sign in to comment.