changeset 430:c5999345f42c

add: panels
author Sam <sam@basx.dev>
date Mon, 05 Feb 2024 23:11:22 +0700
parents 740d2f0d1264
children e13b75bff00c
files semicongine/panel.nim tests/test_panel.nim
diffstat 2 files changed, 83 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/semicongine/panel.nim	Mon Feb 05 23:11:22 2024 +0700
@@ -0,0 +1,55 @@
+import ./core
+
+const
+  SHADER_ATTRIB_PREFIX = "semicon_text_"
+  MAX_TEXT_MATERIALS = 10
+
+var instanceCounter = 0
+
+type
+  Panel* = object
+    position: Vec2f
+    size: Vec2f
+    color*: Vec4f
+
+    horizontalAlignment: HorizontalAlignment = Center
+    verticalAlignment: VerticalAlignment = Center
+    aspect_ratio: float32
+    texture: Vec4f
+    dirty: bool
+    mesh: Mesh
+
+proc position*(panel: Panel): Vec2f =
+  panel.position
+proc `position=`*(panel: var Panel, value: Vec2f) =
+  if value != panel.position:
+    panel.position = value
+    panel.dirty = true
+
+proc size*(panel: Panel): Vec2f =
+  panel.size
+proc `size=`*(panel: var Panel, value: Vec2f) =
+  if value != panel.size:
+    panel.size = value
+    panel.dirty = true
+
+proc horizontalAlignment*(panel: Panel): HorizontalAlignment =
+  panel.horizontalAlignment
+proc `horizontalAlignment=`*(panel: var Panel, value: HorizontalAlignment) =
+  if value != panel.horizontalAlignment:
+    panel.horizontalAlignment = value
+    panel.dirty = true
+
+proc verticalAlignment*(panel: Panel): VerticalAlignment =
+  panel.verticalAlignment
+proc `verticalAlignment=`*(panel: var Panel, value: VerticalAlignment) =
+  if value != panel.verticalAlignment:
+    panel.verticalAlignment = value
+    panel.dirty = true
+
+proc aspect_ratio*(panel: Panel): float32 =
+  panel.aspect_ratio
+proc `aspect_ratio=`*(panel: var Panel, value: float32) =
+  if value != panel.aspect_ratio:
+    panel.aspect_ratio = value
+    panel.dirty = true
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_panel.nim	Mon Feb 05 23:11:22 2024 +0700
@@ -0,0 +1,28 @@
+import std/unicode
+
+import semicongine
+
+
+proc main() =
+  # setup engine
+  var engine = initEngine("Test panels")
+  engine.initRenderer([])
+
+  # build scene
+  var scene = Scene(name: "main")
+  var panel = initPanel(position: newVec2f(0, 0), size: newVec2f(0.1, 0.1))
+
+  scene.add panel
+  engine.loadScene(scene)
+
+  while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
+    if engine.windowWasResized():
+      var winSize = engine.getWindow().size
+      panel.aspect_ratio = winSize[0] / winSize[1]
+
+    engine.renderScene(scene)
+  engine.destroy()
+
+
+when isMainModule:
+  main()