فهرست منبع

update shadow rendering! :eyes:

Ethosa 3 سال پیش
والد
کامیت
4b790edd03
4فایلهای تغییر یافته به همراه73 افزوده شده و 44 حذف شده
  1. 66 32
      src/nodesnim/graphics/drawable.nim
  2. 3 10
      src/nodesnim/graphics/gradient_drawable.nim
  3. 3 1
      tests/test42.nim
  4. 1 1
      tests/test44.nim

+ 66 - 32
src/nodesnim/graphics/drawable.nim

@@ -54,29 +54,71 @@ proc Drawable*: DrawableRef =
 let shadow_color: ColorRef = Color(0f, 0f, 0f, 0.5f)
 
 
-template vd* = discard
+template vd* =
+  ## void template
+  discard
 
-template recalc* =
+template recalc*(shadow: bool = false) =
   ## Calculates vertex positions.
-  # left top
-  for i in bezier_iter(1f/self.border_detail_lefttop.float, Vector2(0, -self.border_radius_lefttop),
-                       Vector2(0, 0), Vector2(self.border_radius_lefttop, 0)):
-    vertex.add(Vector2(x+i.x, y+i.y))
-
-  # right top
-  for i in bezier_iter(1f/self.border_detail_righttop.float, Vector2(-self.border_radius_righttop, 0),
-                       Vector2(0, 0), Vector2(0, -self.border_radius_righttop)):
-    vertex.add(Vector2(x+width+i.x, y+i.y))
-
-  # right bottom
-  for i in bezier_iter(1f/self.border_detail_rightbottom.float, Vector2(0, -self.border_radius_rightbottom),
-                       Vector2(0, 0), Vector2(-self.border_radius_rightbottom, 0)):
-    vertex.add(Vector2(x+width+i.x, y-height-i.y))
-
-  # left bottom
-  for i in bezier_iter(1f/self.border_detail_leftbottom.float, Vector2(self.border_radius_leftbottom, 0),
-                       Vector2(0, 0), Vector2(0, self.border_radius_leftbottom)):
-    vertex.add(Vector2(x+i.x, y-height+i.y))
+  when not shadow:
+    # left top
+    for i in bezier_iter(1f/self.border_detail_lefttop.float, Vector2(0, -self.border_radius_lefttop),
+                         Vector2(0, 0), Vector2(self.border_radius_lefttop, 0)):
+      vertex.add(Vector2(x+i.x, y+i.y))
+
+    # right top
+    for i in bezier_iter(1f/self.border_detail_righttop.float, Vector2(-self.border_radius_righttop, 0),
+                         Vector2(0, 0), Vector2(0, -self.border_radius_righttop)):
+      vertex.add(Vector2(x+width+i.x, y+i.y))
+
+    # right bottom
+    for i in bezier_iter(1f/self.border_detail_rightbottom.float, Vector2(0, -self.border_radius_rightbottom),
+                         Vector2(0, 0), Vector2(-self.border_radius_rightbottom, 0)):
+      vertex.add(Vector2(x+width+i.x, y-height-i.y))
+
+    # left bottom
+    for i in bezier_iter(1f/self.border_detail_leftbottom.float, Vector2(self.border_radius_leftbottom, 0),
+                         Vector2(0, 0), Vector2(0, self.border_radius_leftbottom)):
+      vertex.add(Vector2(x+i.x, y-height+i.y))
+  else:
+    glBegin(GL_QUAD_STRIP)
+    # left top
+    for i in bezier_iter(1f/self.border_detail_lefttop.float, Vector2(0, -self.border_radius_lefttop),
+                         Vector2(0, 0), Vector2(self.border_radius_lefttop, 0)):
+      glColor4f(0, 0, 0, 0)
+      glVertex2f(x+i.x+self.shadow_offset.x, y+i.y-self.shadow_offset.y)
+      glColor4f(shadow_color.r, shadow_color.g, shadow_color.b, shadow_color.a)
+      glVertex2f(x+i.x, y+i.y)
+
+    # right top
+    for i in bezier_iter(1f/self.border_detail_righttop.float, Vector2(-self.border_radius_righttop, 0),
+                         Vector2(0, 0), Vector2(0, -self.border_radius_righttop)):
+      glColor4f(0, 0, 0, 0)
+      glVertex2f(x+width+i.x+self.shadow_offset.x, y+i.y-self.shadow_offset.y)
+      glColor4f(shadow_color.r, shadow_color.g, shadow_color.b, shadow_color.a)
+      glVertex2f(x+width+i.x, y+i.y)
+
+    # right bottom
+    for i in bezier_iter(1f/self.border_detail_rightbottom.float, Vector2(0, -self.border_radius_rightbottom),
+                         Vector2(0, 0), Vector2(-self.border_radius_rightbottom, 0)):
+      glColor4f(0, 0, 0, 0)
+      glVertex2f(x+width+i.x+self.shadow_offset.x, y-height-i.y-self.shadow_offset.y)
+      glColor4f(shadow_color.r, shadow_color.g, shadow_color.b, shadow_color.a)
+      glVertex2f(x+width+i.x, y-height-i.y)
+
+    # left bottom
+    for i in bezier_iter(1f/self.border_detail_leftbottom.float, Vector2(self.border_radius_leftbottom, 0),
+                         Vector2(0, 0), Vector2(0, self.border_radius_leftbottom)):
+      glColor4f(0, 0, 0, 0)
+      glVertex2f(x+i.x+self.shadow_offset.x, y-height+i.y-self.shadow_offset.y)
+      glColor4f(shadow_color.r, shadow_color.g, shadow_color.b, shadow_color.a)
+      glVertex2f(x+i.x, y-height+i.y)
+
+    glColor4f(0, 0, 0, 0)
+    glVertex2f(x+self.shadow_offset.x, y-self.border_radius_lefttop-self.shadow_offset.y)
+    glColor4f(shadow_color.r, shadow_color.g, shadow_color.b, shadow_color.a)
+    glVertex2f(x, y-self.border_radius_lefttop)
+    glEnd()
 
 
 template draw_template*(drawtype, color, function, secondfunc: untyped): untyped =
@@ -139,19 +181,11 @@ method enableShadow*(self: DrawableRef, val: bool) {.base.} =
 method draw*(self: DrawableRef, x1, y1, width, height: float) {.base.} =
   var
     vertex: seq[Vector2Obj] = @[]
-    x = x1 + self.shadow_offset.x
-    y = y1 - self.shadow_offset.y
+    x = x1
+    y = y1
 
   if self.shadow:
-    recalc()
-    if self.texture.texture > 0'u32:
-      draw_texture_template(GL_POLYGON, shadow_color, vd(), vd())
-    else:
-      draw_template(GL_POLYGON, shadow_color, vd(), vd())
-
-  vertex = @[]
-  x = x1
-  y = y1
+    recalc(true)
   recalc()
 
   if self.texture.texture > 0'u32:

+ 3 - 10
src/nodesnim/graphics/gradient_drawable.nim

@@ -61,19 +61,12 @@ template draw_template*(drawtype, color, function, secondfunc: untyped, is_gradi
 method draw*(self: GradientDrawableRef, x1, y1, width, height: float) =
   var
     vertex: seq[Vector2Obj] = @[]
-    x = x1 + self.shadow_offset.x
-    y = y1 - self.shadow_offset.y
+    x = x1
+    y = y1
 
   if self.shadow:
-    recalc()
-    if self.texture.texture > 0'u32:
-      draw_texture_template(GL_POLYGON, shadow_color, vd(), vd())
-    else:
-      draw_template(GL_POLYGON, shadow_color, vd(), vd(), false)
+    recalc(true)
 
-  vertex = @[]
-  x = x1
-  y = y1
   recalc()
 
   if self.texture.texture > 0'u32:

+ 3 - 1
tests/test42.nim

@@ -14,6 +14,8 @@ build:
 ctrl1.background.setTexture(load("assets/sharp.jpg"))
 ctrl1.background.setCornerRadius(25)
 ctrl1.background.setCornerDetail(8)
+ctrl1.background.enableShadow(true)
+ctrl1.background.setShadowOffset(Vector2(0, 8))
 
 
 ctrl.resize(256, 96)
@@ -25,7 +27,7 @@ ctrl.setStyle(style(
     border-width: 1,
     border-color: rgb(0, 0, 0),
     shadow: true,
-    shadow-offset: 3,
+    shadow-offset: 8,
     size-anchor: 0.5 0.7
   }
 ))

+ 1 - 1
tests/test44.nim

@@ -7,7 +7,7 @@ Window("drawable oops")
 build:
   - Scene scene:
     - Control ctrl:
-      call resize(100, 150)
+      call resize(256, 256)
       call move(150, 50)
 
 var gradient = GradientDrawable()