浏览代码

fix Label, RichLabel and EditText.

SakiKawasaki 5 年之前
父节点
当前提交
c39acc0ef1

+ 12 - 10
src/nodesnim/nodescontrol/edittext.nim

@@ -90,16 +90,18 @@ method draw*(self: EditTextPtr, w, h: GLfloat) =
     var tx = x + self.rect_size.x*self.text_align.x1 - tw * self.text_align.x2
     for c in line:
       glColor4f(color.r, color.g, color.b, color.a)
-      glRasterPos2f(tx, ty)  # set char position
-      self.font.glutBitmapCharacter(c.int)  # render char
-      tx += self.font.glutBitmapWidth(c.int).float
-
-      inc char_num
-      if char_num == self.caret_position and self.blit_caret and self.blit_time > 1f:
-        glColor4f(self.caret_color.r, self.caret_color.g, self.caret_color.b, self.caret_color.a)
-        glRectf(tx, ty, tx+2, ty+self.size)
-        if self.blit_time > 2f:
-          self.blit_time = 0f
+      let cw = self.font.glutBitmapWidth(c.int).float
+      if tx >= x and tx < x + self.rect_size.x - cw and ty <= y and ty > y - self.rect_size.y:
+        glRasterPos2f(tx, ty)  # set char position
+        self.font.glutBitmapCharacter(c.int)  # render char
+        tx += cw
+
+        inc char_num
+        if char_num == self.caret_position and self.blit_caret and self.blit_time > 1f:
+          glColor4f(self.caret_color.r, self.caret_color.g, self.caret_color.b, self.caret_color.a)
+          glRectf(tx, ty, tx+2, ty+self.size)
+          if self.blit_time > 2f:
+            self.blit_time = 0f
     inc char_num
     ty -= self.spacing + self.size
 

+ 5 - 3
src/nodesnim/nodescontrol/label.nim

@@ -66,9 +66,11 @@ method draw*(self: LabelPtr, w, h: GLfloat) =
     # Draw text:
     var tx = x + self.rect_size.x*self.text_align.x1 - tw * self.text_align.x2
     for c in line:
-      glRasterPos2f(tx, ty)  # set char position
-      self.font.glutBitmapCharacter(c.int)  # render char
-      tx += self.font.glutBitmapWidth(c.int).float
+      let cw = self.font.glutBitmapWidth(c.int).float
+      if tx >= x and tx < x + self.rect_size.x - cw and ty <= y and ty > y - self.rect_size.y:
+        glRasterPos2f(tx, ty)  # set char position
+        self.font.glutBitmapCharacter(c.int)  # render char
+        tx += cw
     ty -= self.spacing + self.size
 
   # Press

+ 6 - 4
src/nodesnim/nodescontrol/rich_label.nim

@@ -63,10 +63,12 @@ method draw*(self: RichLabelPtr, w, h: GLfloat) =
     # Draw text:
     var tx = x + self.rect_size.x*self.text_align.x1 - tw * self.text_align.x2
     for c in line.chars:
-      glColor4f(c.color.r, c.color.g, c.color.b, c.color.a)
-      glRasterPos2f(tx, ty)  # set char position
-      self.font.glutBitmapCharacter(c.c.int)  # render char
-      tx += self.font.glutBitmapWidth(c.c.int).float
+      let cw = self.font.glutBitmapWidth(c.c.int).float
+      if tx >= x and tx < x + self.rect_size.x - cw and ty <= y and ty > y - self.rect_size.y:
+        glColor4f(c.color.r, c.color.g, c.color.b, c.color.a)
+        glRasterPos2f(tx, ty)  # set char position
+        self.font.glutBitmapCharacter(c.c.int)  # render char
+        tx += cw
     ty -= self.spacing + self.size
 
   # Press

+ 1 - 0
tests/test16.nim

@@ -16,6 +16,7 @@ main.addChild(edittext)
 edittext.color = Color(1f, 1f, 1f)  # default text color.
 edittext.hint_color = Color(1f, 0.6, 1f)
 edittext.resize(512, 256)
+edittext.setBackgroundColor(Color(0x212121ff))
 
 
 addScene(main)