浏览代码

small update.

SakiKawasaki 4 年之前
父节点
当前提交
fffc96cf83
共有 4 个文件被更改,包括 112 次插入25 次删除
  1. 19 0
      src/nodesnim/core/color.nim
  2. 82 18
      src/nodesnim/core/color_text.nim
  3. 10 7
      src/nodesnim/core/input.nim
  4. 1 0
      src/nodesnim/core/rect2.nim

+ 19 - 0
src/nodesnim/core/color.nim

@@ -172,8 +172,27 @@ proc lerp*(r1, g1, b1, a1, r2, g2, b2, a2: uint32, lerpv: float): uint32 =
   r or (g shl 8) or (b shl 16) or (a shl 24)
 
 
+# --- Operators --- #
 proc `$`*(color: ColorRef): string =
   "Color(" & $color.r & ", " & $color.g & ", " & $color.b & ", " & $color.a & ")"
 
+proc `+`*(x, y: ColorRef): ColorRef =
+  ColorRef(r: x.r + y.r, g: x.g + y.g, b: x.b + y.b, a: x.a + y.a)
+proc `-`*(x, y: ColorRef): ColorRef =
+  ColorRef(r: x.r - y.r, g: x.g - y.g, b: x.b - y.b, a: x.a - y.a)
+proc `*`*(x, y: ColorRef): ColorRef =
+  ColorRef(r: x.r * y.r, g: x.g * y.g, b: x.b * y.b, a: x.a * y.a)
+proc `/`*(x, y: ColorRef): ColorRef =
+  ColorRef(r: x.r / y.r, g: x.g / y.g, b: x.b / y.b, a: x.a / y.a)
+
+proc `+=`*(x: var ColorRef, y: ColorRef) =
+  x = x + y
+proc `-=`*(x: var ColorRef, y: ColorRef) =
+  x = x - y
+proc `*=`*(x: var ColorRef, y: ColorRef) =
+  x = x * y
+proc `/=`*(x: var ColorRef, y: ColorRef) =
+  x = x / y
+
 proc `==`*(x, y: ColorRef): bool =
   x.r == y.r and x.g == y.g and x.b == y.b and x.a == y.a

+ 82 - 18
src/nodesnim/core/color_text.nim

@@ -6,12 +6,17 @@ type
   ColorCharRef* = ref object
     c*: char
     color*: ColorRef
+    underline*: bool
   ColorTextRef* = ref object
     chars*: seq[ColorCharRef]
 
 
-func clrtext*(text: string, color: ColorRef = Color(1f, 1f, 1f)): ColorTextRef =
+func clrtext*(text: string, color: ColorRef = Color(1f, 1f, 1f), underline: bool = false): ColorTextRef =
   ## Creates a new ColorText ref object.
+  ##
+  ## Arguments:
+  ## - `color` is a text color.
+  ## - `underline` is a text underline.
   runnableExamples:
     import color
     var
@@ -19,21 +24,31 @@ func clrtext*(text: string, color: ColorRef = Color(1f, 1f, 1f)): ColorTextRef =
       text1 = clrtext("hello", Color(1, 0.6, 1))
   var chars: seq[ColorCharRef] = @[]
   for c in text:
-    chars.add(ColorCharRef(c: c, color: color))
+    chars.add(ColorCharRef(c: c, color: color, underline: underline))
   result = ColorTextRef(chars: chars)
 
 
-func clrchar*(c: char, color: ColorRef = Color(1f, 1f, 1f)): ColorCharRef =
+func clrchar*(c: char, color: ColorRef = Color(1f, 1f, 1f), underline: bool = false): ColorCharRef =
   ## Creates a new ColorChar ref object.
+  ##
+  ## Arguments:
+  ## - `color` is a char color.
+  ## - `underline` is a char underline.
   runnableExamples:
     import color
     var
       c = clrchar's'
-      c1 = clrchar('s', Color(1f, 1f, 1f))
-  result = ColorCharRef(c: c, color: color)
+      c1 = clrchar('s', Color(1f, 1f, 1f), underline=true)
+  result = ColorCharRef(c: c, color: color, underline: underline)
 
 
 proc setColor*(self: ColorTextRef, fromc, toc: int, value: ColorRef) =
+  ## Changes text color.
+  ##
+  ## Arguments:
+  ## - `fromc` - from char position.
+  ## - `toc` - to char position.
+  ## - `value` - new color.
   runnableExamples:
     import color
     var
@@ -45,6 +60,10 @@ proc setColor*(self: ColorTextRef, fromc, toc: int, value: ColorRef) =
 
 
 proc setColor*(self: ColorTextRef, value: ColorRef) =
+  ## Changes text color.
+  ##
+  ## Arguments:
+  ## - `value` - new color.
   runnableExamples:
     import color
     var
@@ -56,6 +75,11 @@ proc setColor*(self: ColorTextRef, value: ColorRef) =
 
 
 proc setColor*(self: ColorTextRef, index: int, value: ColorRef) =
+  ## Changes text color.
+  ##
+  ## Arguments:
+  ## - `index` - char position.
+  ## - `value` - new color.
   runnableExamples:
     import color
     var
@@ -64,6 +88,59 @@ proc setColor*(self: ColorTextRef, index: int, value: ColorRef) =
     text.setColor(0, clr)
   self.chars[index].color = value
 
+
+proc setUnderline*(self: ColorTextRef, fromc, toc: int, value: bool) =
+  ## Changes text underline.
+  ##
+  ## Arguments:
+  ## - `fromc` - from char position.
+  ## - `toc` - to char position.
+  runnableExamples:
+    var
+      text = clrtext"hello world"
+    text.setUnderline(5, text.chars.len()-1, true)
+  for i in fromc..toc:
+    self.chars[i].underline = value
+
+
+proc setUnderline*(self: ColorTextRef, value: bool) =
+  ## Changes text underline.
+  runnableExamples:
+    var
+      text = clrtext"hello world"
+    text.setUnderline(true)
+  for i in 0..self.chars.high:
+    self.chars[i].underline = value
+
+
+proc setUnderline*(self: ColorTextRef, index: int, value: bool) =
+  ## Changes text underline.
+  ##
+  ## Arguments:
+  ## - `index` - char position.
+  runnableExamples:
+    var
+      text = clrtext"hello world"
+    text.setUnderline(0, true)
+  self.chars[index].underline = value
+
+
+proc len*(x: ColorTextRef): int =
+  x.chars.len()
+
+
+proc splitLines*(x: ColorTextRef): seq[ColorTextRef] =
+  ## Creates a new seq of ColorTextRef.
+  result = @[clrtext("")]
+  for c in x.chars:
+    if c.c.int != 13:
+      result[^1].chars.add(c)
+    else:
+      result.add(clrtext(""))
+  if result[^1].len() == 0:
+    discard result.pop()
+
+
 # --- Operators --- #
 proc `$`*(text: ColorTextRef): string =
   result = ""
@@ -118,19 +195,6 @@ proc contains*(x: ColorTextRef, y: ColorCharRef): bool =
 converter toChar*(x: ColorCharRef): char =
   x.c
 
-proc len*(x: ColorTextRef): int =
-  x.chars.len()
-
-proc splitLines*(x: ColorTextRef): seq[ColorTextRef] =
-  result = @[clrtext("")]
-  for c in x.chars:
-    if c.c.int != 13:
-      result[^1].chars.add(c)
-    else:
-      result.add(clrtext(""))
-  if result[^1].len() == 0:
-    discard result.pop()
-
 proc `[]`*[U, V](self: ColorTextRef, i: HSlice[U, V]): ColorTextRef =
   ColorTextRef(chars: self.chars[i])
 

+ 10 - 7
src/nodesnim/core/input.nim

@@ -8,7 +8,11 @@ import
 
 type
   InputEventType* {.size: sizeof(int8).} = enum
-    MOUSE, TOUCH, MOTION, KEYBOARD, UNKNOWN
+    MOUSE,     ## Mouse button.
+    TOUCH,     ## Touch screen.
+    MOTION,    ## Mouse motion.
+    KEYBOARD,  ## Keyboard input
+    UNKNOWN    ## Unknown event.
 
   InputAction* = object
     kind*: InputEventType
@@ -28,12 +32,6 @@ type
     x*, y*, xrel*, yrel*: float
     key*: string
 
-  InputEventVoid* = distinct int8
-  InputEventMouseButton* = distinct int8
-  InputEventMouseMotion* = distinct int8
-  InputEventTouchScreen* = distinct int8
-  InputEventKeyboard* = distinct int8
-
 
 const
   BUTTON_LEFT* = 0
@@ -99,18 +97,23 @@ var
 
 
 proc isInputEventVoid*(a: InputEvent): bool =
+  ## Returns true, when `a` kind is an UNKNOWN.
   a.kind == UNKNOWN
 
 proc isInputEventMouseButton*(a: InputEvent): bool =
+  ## Returns true, when `a` kind is a MOUSE.
   a.kind == MOUSE
 
 proc isInputEventMouseMotion*(a: InputEvent): bool =
+  ## Returns true, when `a` kind is a MOTION.
   a.kind == MOTION
 
 proc isInputEventTouchScreen*(a: InputEvent): bool =
+  ## Returns true, when `a` kind is a TOUCH.
   a.kind == TOUCH
 
 proc isInputEventKeyboard*(a: InputEvent): bool =
+  ## Returns true, when `a` kind is a KEYBOARD.
   a.kind == KEYBOARD
 
 

+ 1 - 0
src/nodesnim/core/rect2.nim

@@ -40,5 +40,6 @@ proc contains*(self, other: Rect2Ref): bool =
    self.hasPoint(other.x+other.w, other.y+other.h))
 
 
+# --- Operators --- #
 proc `$`*(x: Rect2Ref): string =
   "Rect2(x: " & $x.x & ", y: " & $x.y & ", w: " & $x.w & ", h: " & $x.h & ")"