Forráskód Böngészése

small refactoring and optimize `Color`

Ethosa 3 éve
szülő
commit
3a79c5d336

+ 1 - 1
README.md

@@ -81,7 +81,7 @@ This section contains links to documentation for all nodes.
 |[SceneBuilder][]|                     |[TextureButton][]     |                    |                    |                    |
 |[StyleSheet][]  |                     |[TextureProgressBar][]|                    |                    |                    |
 |[TileSet][]     |                     |[Counter][]           |                    |                    |                    |
-|[Scripts][]     |                     |[Switch][]            |                    |                    |                    |
+|                |                     |[Switch][]            |                    |                    |                    |
 |                |                     |[SubWindow][]         |                    |                    |                    |
 |                |                     |[CheckBox][]          |                    |                    |                    |
 |                |                     |[ToolTip][]           |                    |                    |                    |

+ 16 - 8
src/nodesnim/core/color.nim

@@ -49,11 +49,12 @@ proc Color*(src: uint32): ColorRef =
     echo clr3
     echo clr4
 
+  let clr = src.int
   ColorRef(
-    r: ((src shr 24) and 255).int / 255,
-    g: ((src shr 16) and 255).int / 255,
-    b: ((src shr 8) and 255).int / 255,
-    a: (src and 255).int / 255
+    r: ((clr shr 24) and 255) / 255,
+    g: ((clr shr 16) and 255) / 255,
+    b: ((clr shr 8) and 255) / 255,
+    a: (clr and 255) / 255
   )
 
 proc Color*(src: string): ColorRef =
@@ -73,16 +74,23 @@ proc Color*(src: string): ColorRef =
     target = src
     matched: array[20, string]
 
-  # #FFFFFFFF, #FFF, #FFFFFF, etc
+  # #FFFFFFFF, #FFF, #FFFFFF, #FFFF, etc
   if target.startsWith('#') or target.startsWith("0x") or target.startsWith("0X"):
     target = target[1..^1]
-    if target[0] == 'x' or target[0] == 'X':
+    if target[0] in {'x', 'X'}:
       target = target[1..^1]
 
-    if target.len() == 3:  # #fff -> #ffffffff
+    let length = target.len
+    case length
+    of 3:
       target = target[0] & target[0] & target[1] & target[1] & target[2] & target[2] & "ff"
-    elif target.len() == 6:  # #ffffff -> #ffffffff
+    of 4:  # #1234 -> #11223344
+      target = target[0] & target[0] & target[1] & target[1] & target[2] & target[2] & target[3] & target[3]
+    of 6:  # #ffffff -> #ffffffff
       target &= "ff"
+    else:
+      discard
+
     return Color(parseHexInt(target).uint32)
 
   # rgba(255, 255, 255, 1.0)

+ 22 - 0
src/nodesnim/core/enums.nim

@@ -61,3 +61,25 @@ type
     VISIBLE,
     INVISIBLE,
     GONE
+  ProgressBarType* {.pure, size: sizeof(int8).} = enum
+    PROGRESS_BAR_HORIZONTAL,
+    PROGRESS_BAR_VERTICAL,
+    PROGRESS_BAR_CIRCLE
+  SliderType* {.pure, size: sizeof(int8).} = enum
+    SLIDER_HORIZONTAL,
+    SLIDER_VERTICAL
+  GeometryType* {.pure, size: sizeof(int8).} = enum
+    GEOMETRY_CUBE,
+    GEOMETRY_CYLINDER,
+    GEOMETRY_SPHERE
+  TileMapMode* {.pure, size: sizeof(int8).} = enum
+    TILEMAP_2D,            ## Default 2D mode.
+    TILEMAP_ISOMETRIC      ## Isometric mode.
+  CollisionShape2DType* {.size: sizeof(int8), pure.} = enum
+    COLLISION_SHAPE_2D_RECTANGLE,
+    COLLISION_SHAPE_2D_CIRCLE,
+    COLLISION_SHAPE_2D_POLYGON
+  AnimationMode* {.pure, size: sizeof(int8).} = enum
+    ANIMATION_NORMAL,
+    ANIMATION_EASE,
+    ANIMATION_BEZIER

+ 0 - 4
src/nodesnim/nodes/animation_player.nim

@@ -8,10 +8,6 @@ import
 
 
 type
-  AnimationMode* {.pure, size: sizeof(int8).} = enum
-    ANIMATION_NORMAL,
-    ANIMATION_EASE,
-    ANIMATION_BEZIER
   AnimationObject* = object
     states: seq[tuple[tick: int, value: float]]
     obj: ptr float

+ 0 - 4
src/nodesnim/nodes2d/collision_shape2d.nim

@@ -22,10 +22,6 @@ when defined(debug):
 
 
 type
-  CollisionShape2DType* {.size: sizeof(int8), pure.} = enum
-    COLLISION_SHAPE_2D_RECTANGLE,
-    COLLISION_SHAPE_2D_CIRCLE,
-    COLLISION_SHAPE_2D_POLYGON
   CollisionShape2DObj* = object of Node2DObj
     disable*: bool
     x1*, y1*, radius*: float

+ 0 - 3
src/nodesnim/nodes2d/tilemap.nim

@@ -12,9 +12,6 @@ import
 
 
 type
-  TileMapMode* {.pure, size: sizeof(int8).} = enum
-    TILEMAP_2D,            ## Default 2D mode.
-    TILEMAP_ISOMETRIC      ## Isometric mode.
   TileMapObj* = object of Node2DObj
     mode*: TileMapMode
     map_size*: tuple[x, y, z: int]

+ 0 - 4
src/nodesnim/nodes3d/geometry_instance.nim

@@ -15,10 +15,6 @@ import
 
 
 type
-  GeometryType* {.pure.} = enum
-    GEOMETRY_CUBE,
-    GEOMETRY_CYLINDER,
-    GEOMETRY_SPHERE
   GeometryInstanceObj* = object of Node3DObj
     geometry*: GeometryType
     sides*, rings*: int

+ 0 - 4
src/nodesnim/nodescontrol/progress_bar.nim

@@ -20,10 +20,6 @@ import
 const CIRCLE_STEP: float = TAU * 0.01
 
 type
-  ProgressBarType* {.pure.} = enum
-    PROGRESS_BAR_HORIZONTAL,
-    PROGRESS_BAR_VERTICAL,
-    PROGRESS_BAR_CIRCLE
   ProgressBarObj* = object of ControlRef
     max_value*, value*: float
     progress_color*: ColorRef

+ 0 - 10
src/nodesnim/nodescontrol/scroll.nim

@@ -68,16 +68,6 @@ method duplicate*(self: ScrollRef): ScrollRef {.base.} =
   self.deepCopy()
 
 
-method resize*(canvas: ScrollRef, w, h: GLfloat, save_anchor: bool = false) =
-  ## Resizes scroll.
-  ##
-  ## Arguments:
-  ## - `w` is a new width.
-  ## - `h` is a new height.
-  canvas.rect_size.x = w
-  canvas.rect_size.y = h
-
-
 method draw*(self: ScrollRef, w, h: GLfloat) =
   ## This uses in the `window.nim`.
   let

+ 0 - 3
src/nodesnim/nodescontrol/slider.nim

@@ -16,9 +16,6 @@ import
 
 
 type
-  SliderType*{.pure, size: sizeof(int8).} = enum
-    SLIDER_HORIZONTAL,
-    SLIDER_VERTICAL
   SliderObj* = object of ControlRef
     slider_type*: SliderType
     max_value*, value*: uint

+ 1 - 0
src/nodesnim/nodescontrol/tooltip.nim

@@ -43,6 +43,7 @@ proc ToolTip*(name: string = "ToolTip",
 
 
 method postdraw*(self: ToolTipRef, w, h: GLfloat) =
+  {.warning[LockLevel]: off.}
   procCall self.ControlRef.draw(w, h)
   let
     x = -w/2 + self.global_position.x

+ 13 - 11
src/nodesnim/core/scripts.nim → src/nodesnim/runtime/scripts.nim

@@ -8,28 +8,28 @@ import
     condsyms
   ],
 
-  exceptions,
+  ../core/exceptions,
+  ../core/vector2,
+  scripts_impl,
 
   os
 
 
-type
-  CompiledScript = ref object
-    pctx*: PCtx
-    graph*: ModuleGraph
-    module*: PSym
-    filename*: string
-
-
 var
   ident_cache = newIdentCache()
   cfg = newConfigRef()
 
 once:
+  let std = AbsoluteDir(getHomeDir() / ".nimble" / "lib")
   # Search .nimble/lib folder 👀
-  cfg.libpath = AbsoluteDir(getHomeDir() / ".nimble" / "lib")
+  cfg.libpath = std
   cfg.searchPaths.add(cfg.libpath)
-  cfg.searchPaths.add(AbsoluteDir($cfg.libpath / "pure"))
+  cfg.searchPaths.add(AbsoluteDir($std / "pure"))
+  cfg.searchPaths.add(AbsoluteDir($std / "pure" / "collections"))
+  cfg.searchPaths.add(AbsoluteDir($std / "core"))
+  cfg.searchPaths.add(AbsoluteDir($std / "impure"))
+  cfg.searchPaths.add(AbsoluteDir($std / "std"))
+  cfg.implicitIncludes.add(getCurrentDir() / "scripts_api.nim")
 
 
 # --- Convert default Nim types to PNode --- #
@@ -44,10 +44,12 @@ converter toNode*(x: openarray[int|float|string|bool|enum]): PNode =
   result.sons.initialize(x.len)
   for i in x.low..x.high:
     result[i] = x[i].toNode()
+
 converter toNode*(x: tuple | object): PNode =
   result = newTree(nkPar)
   for field in x.fields:
     result.sons.add(field.toNode())
+
 converter toNode*(x: ref tuple | ref object): PNode =
   result = newTree(nkPar)
   if x.isNil():

+ 2 - 0
src/nodesnim/runtime/scripts_api.nim

@@ -0,0 +1,2 @@
+# author: Ethosa
+## Built-ins for writing scripts.

+ 24 - 0
src/nodesnim/runtime/scripts_impl.nim

@@ -0,0 +1,24 @@
+import
+  compiler / [vmdef, modulegraphs, vm, ast]
+
+
+type
+  CompiledScript* = ref object
+    pctx*: PCtx
+    graph*: ModuleGraph
+    module*: PSym
+    filename*: string
+
+
+proc exposeScriptApi* (self: CompiledScript) =
+  template expose (routine, body: untyped) {.dirty.} =
+    self.pctx.registerCallback self.filename & "." & astToStr(routine),
+      proc (a: VmArgs) =
+        body
+
+  expose add:
+    # We need to use procs like getInt to retrieve the argument values from VmArgs
+    # Instead of using the return statement we need to use setResult
+    setResult(a,
+      getInt(a, 0) +
+      getInt(a, 1))