Browse Source

Merge pull request #17 from Ethosa/nightly

Nightly v0.1.1
Ethosa 3 năm trước cách đây
mục cha
commit
cbeaf4a830
4 tập tin đã thay đổi với 35 bổ sung8 xóa
  1. 1 1
      README.md
  2. 1 1
      nodesnim.nimble
  3. 22 5
      src/nodesnim/nodes/animation_player.nim
  4. 11 1
      tests/test43.nim

+ 1 - 1
README.md

@@ -7,7 +7,7 @@
 [![time tracker](https://wakatime.com/badge/github/Ethosa/nodesnim.svg)](https://wakatime.com/badge/github/Ethosa/nodesnim)
 [![test](https://github.com/Ethosa/nodesnim/workflows/test/badge.svg)](https://github.com/Ethosa/nodesnim/actions)
 
-<h4>Stable version - 0.1.0</h4>
+<h4>Stable version - 0.1.1</h4>
 </div>
 
 ## Install

+ 1 - 1
nodesnim.nimble

@@ -1,7 +1,7 @@
 [Package]
 name = "nodesnim"
 author = "Ethosa"
-version = "0.1.0"
+version = "0.1.1"
 description = "The Nim GUI/2D framework based on OpenGL and SDL2."
 license = "MIT"
 srcDir = "src"

+ 22 - 5
src/nodesnim/nodes/animation_player.nim

@@ -5,7 +5,11 @@ import
   ../core/enums,
   ../thirdparty/opengl
 
+
 type
+  AnimationMode* {.pure, size: sizeof(int8).} = enum
+    ANIMATION_NORMAL,
+    ANIMATION_EASE
   AnimationObject* = object
     states: seq[tuple[tick: int, value: float]]
     obj: ptr float
@@ -15,6 +19,7 @@ type
     tick*: int64
     is_played*: bool
     loop*: bool
+    mode*: AnimationMode
   AnimationPlayerRef* = ref AnimationPlayerObj
 
 
@@ -29,6 +34,16 @@ proc AnimationPlayer*(name: string = "AnimationPlayer"): AnimationPlayerRef =
   result.loop = true
   result.kind = ANIMATION_PLAYER_NODE
   result.type_of_node = NODE_TYPE_DEFAULT
+  result.mode = ANIMATION_NORMAL
+
+
+proc ease(self: AnimationPlayerRef, states: seq[tuple[tick: int, value: float]]): float =
+    var time = (self.tick - states[0].tick).float / ((states[1].tick - states[0].tick).float / 2.0)
+    let diff = states[1].value - states[0].value
+    if time < 1:
+        return diff / 2 * time * time + states[0].value
+    time -= 1
+    return -diff / 2 * (time * (time - 2) - 1) + states[0].value
 
 
 method addState*(self: AnimationPlayerRef, obj: ptr float, states: seq[tuple[tick: int, value: float]]) {.base.} =
@@ -63,11 +78,13 @@ method draw*(self: AnimationPlayerRef, w: GLfloat, h: GLfloat) =
             break
     
       if current_states.len() == 2:
-        let
-          diff_time: float = (current_states[1].tick - current_states[0].tick).float
-          diff: float = current_states[1].value - current_states[0].value
-        current[] = current_states[0].value + (self.tick - current_states[0].tick).float/diff_time * diff
-        echo current[]
+        if self.mode == ANIMATION_NORMAL:
+          let
+            diff_time: float = (current_states[1].tick - current_states[0].tick).float
+            diff: float = current_states[1].value - current_states[0].value
+          current[] = current_states[0].value + (self.tick - current_states[0].tick).float/diff_time * diff
+        else:
+          current[] = self.ease(current_states)
       current_states = @[]
 
     self.tick += 1

+ 11 - 1
tests/test43.nim

@@ -8,11 +8,21 @@ build:
     - ColorRect rect:
       color: Color(0, 0, 0)
       call resize(100, 100)
+    - ColorRect rect1:
+      color: Color(0, 0, 0)
+      call resize(100, 100)
+      call move(0, 200)
     - AnimationPlayer animation:
       call addState(rect.color.r.addr, @[(tick: 0, value: 0.0), (tick: 200, value: 1.0)])
-      call addState(rect.position.x.addr, @[(tick: 0, value: 0.0), (tick: 50, value: 50.0), (tick: 200, value: 100.0)])
+      call addState(rect.position.x.addr, @[(tick: 0, value: 0.0), (tick: 100, value: 250.0)])
+      call setDuration(200)
+      call play()
+      mode: ANIMATION_NORMAL  # Default animation mode.
+    - AnimationPlayer animation1:
+      call addState(rect1.position.x.addr, @[(tick: 0, value: 0.0), (tick: 100, value: 250.0)])
       call setDuration(200)
       call play()
+      mode: ANIMATION_EASE
 
 addMainScene(scene)
 windowLaunch()