Mercurial > games > semicongine
annotate semiconginev2/old/panel.nim @ 1235:c70fee6568f6
did: improv render tests to run without user input
author | sam <sam@basx.dev> |
---|---|
date | Sat, 20 Jul 2024 15:45:02 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
431 | 1 import std/strformat |
433 | 2 import std/tables |
431 | 3 |
430 | 4 import ./core |
431 | 5 import ./mesh |
433 | 6 import ./material |
7 import ./vulkan/shader | |
434 | 8 import ./events |
430 | 9 |
10 const | |
433 | 11 # font shader |
431 | 12 SHADER_ATTRIB_PREFIX = "semicon_panel_" |
13 MAX_PANEL_MATERIALS = 10 | |
433 | 14 POSITION_ATTRIB = SHADER_ATTRIB_PREFIX & "position" |
15 UV_ATTRIB = SHADER_ATTRIB_PREFIX & "uv" | |
16 PANEL_MATERIAL_TYPE* = MaterialType( | |
17 name: "default-panel-material-type", | |
999
b5be4ea07c3c
fix: errors that have now been discovered thanks to last commit :)
sam <sam@basx.dev>
parents:
998
diff
changeset
|
18 vertexAttributes: {POSITION_ATTRIB: Vec3F32, UV_ATTRIB: Vec2F32}.toTable, |
b5be4ea07c3c
fix: errors that have now been discovered thanks to last commit :)
sam <sam@basx.dev>
parents:
998
diff
changeset
|
19 instanceAttributes: {TRANSFORM_ATTRIB: Mat4F32, MATERIALINDEX_ATTRIBUTE: UInt16}.toTable, |
433 | 20 attributes: {"panelTexture": TextureType, "color": Vec4F32}.toTable, |
21 ) | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
22 PANEL_SHADER* = CreateShaderConfiguration( |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
444
diff
changeset
|
23 name = "panel shader", |
433 | 24 inputs = [ |
1137 | 25 Attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true), |
26 Attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite), | |
27 Attr[Vec2f](UV_ATTRIB, memoryPerformanceHint = PreferFastWrite), | |
28 Attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true), | |
433 | 29 ], |
30 intermediates = [ | |
1137 | 31 Attr[Vec2f]("uvFrag"), |
32 Attr[uint16]("materialIndexOut", noInterpolation = true) | |
433 | 33 ], |
1137 | 34 outputs = [Attr[Vec4f]("color")], |
35 uniforms = [Attr[Vec4f]("color", arrayCount = MAX_PANEL_MATERIALS), Attr[float32](ASPECT_RATIO_ATTRIBUTE)], | |
36 samplers = [Attr[Texture]("panelTexture", arrayCount = MAX_PANEL_MATERIALS)], | |
433 | 37 vertexCode = &""" |
444 | 38 gl_Position = vec4({POSITION_ATTRIB}.x, {POSITION_ATTRIB}.y * Uniforms.{ASPECT_RATIO_ATTRIBUTE}, {POSITION_ATTRIB}.z, 1.0) * {TRANSFORM_ATTRIB}; |
433 | 39 uvFrag = {UV_ATTRIB}; |
40 materialIndexOut = {MATERIALINDEX_ATTRIBUTE}; | |
41 """, | |
42 fragmentCode = &"""color = Uniforms.color[materialIndexOut] * texture(panelTexture[materialIndexOut], uvFrag);""" | |
43 ) | |
430 | 44 |
45 var instanceCounter = 0 | |
46 | |
47 type | |
48 Panel* = object | |
431 | 49 texture: Texture |
430 | 50 horizontalAlignment: HorizontalAlignment = Center |
51 verticalAlignment: VerticalAlignment = Center | |
52 dirty: bool | |
444 | 53 mesh*: Mesh |
434 | 54 # input handling |
55 onMouseDown*: proc(panel: var Panel, buttons: set[MouseButton]) | |
56 onMouseUp*: proc(panel: var Panel, buttons: set[MouseButton]) | |
57 onMouseEnter*: proc(panel: var Panel) | |
58 onMouseMove*: proc(panel: var Panel) | |
59 onMouseLeave*: proc(panel: var Panel) | |
60 hasMouse*: bool | |
430 | 61 |
433 | 62 proc `$`*(panel: Panel): string = |
444 | 63 &"Panel {panel.mesh}" |
433 | 64 |
1138 | 65 proc Refresh*(panel: var Panel) = |
433 | 66 if not panel.dirty: |
67 return | |
68 | |
69 var | |
70 offsetX = case panel.horizontalAlignment | |
444 | 71 of Left: 0.5 |
433 | 72 of Center: 0 |
444 | 73 of Right: -0.5 |
433 | 74 offsetY = case panel.verticalAlignment |
444 | 75 of Top: 0.5 |
433 | 76 of Center: 0 |
444 | 77 of Bottom: -0.5 |
433 | 78 |
1136 | 79 panel.mesh[POSITION_ATTRIB, 0] = NewVec3f(-0.5 + offsetX, -0.5 + offsetY) |
80 panel.mesh[POSITION_ATTRIB, 1] = NewVec3f(+0.5 + offsetX, -0.5 + offsetY) | |
81 panel.mesh[POSITION_ATTRIB, 2] = NewVec3f(+0.5 + offsetX, +0.5 + offsetY) | |
82 panel.mesh[POSITION_ATTRIB, 3] = NewVec3f(-0.5 + offsetX, +0.5 + offsetY) | |
433 | 83 |
84 panel.dirty = false | |
85 | |
1138 | 86 proc InitPanel*( |
444 | 87 transform = Unit4, |
1136 | 88 color = NewVec4f(1, 1, 1, 1), |
434 | 89 texture = EMPTY_TEXTURE, |
90 horizontalAlignment = HorizontalAlignment.Center, | |
91 verticalAlignment = VerticalAlignment.Center, | |
92 onMouseDown: proc(panel: var Panel, buttons: set[MouseButton]) = nil, | |
93 onMouseUp: proc(panel: var Panel, buttons: set[MouseButton]) = nil, | |
94 onMouseEnter: proc(panel: var Panel) = nil, | |
95 onMouseMove: proc(panel: var Panel) = nil, | |
96 onMouseLeave: proc(panel: var Panel) = nil, | |
97 ): Panel = | |
431 | 98 |
434 | 99 result = Panel( |
100 texture: texture, | |
101 horizontalAlignment: horizontalAlignment, | |
102 verticalAlignment: verticalAlignment, | |
103 onMouseDown: onMouseDown, | |
104 onMouseUp: onMouseUp, | |
105 onMouseEnter: onMouseEnter, | |
106 onMouseMove: onMouseMove, | |
107 onMouseLeave: onMouseLeave, | |
444 | 108 dirty: true, |
434 | 109 ) |
431 | 110 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
111 result.mesh = NewMesh( |
444 | 112 name = &"panel-{instanceCounter}", |
433 | 113 positions = newSeq[Vec3f](4), |
431 | 114 indices = @[ |
115 [uint16(0), uint16(1), uint16(2)], | |
116 [uint16(2), uint16(3), uint16(0)], | |
433 | 117 ], |
1136 | 118 uvs = @[NewVec2f(0, 1), NewVec2f(1, 1), NewVec2f(1, 0), NewVec2f(0, 0)], |
444 | 119 transform = transform |
433 | 120 ) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
121 result.mesh[].RenameAttribute("position", POSITION_ATTRIB) |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
122 result.mesh[].RenameAttribute("uv", UV_ATTRIB) |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
123 result.mesh.material = InitMaterialData( |
433 | 124 theType = PANEL_MATERIAL_TYPE, |
125 name = "Panel material", | |
1137 | 126 attributes = {"panelTexture": InitDataList(@[texture]), "color": InitDataList(@[color])}, |
433 | 127 ) |
128 inc instanceCounter | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
129 result.Refresh() |
431 | 130 |
1138 | 131 proc Color*(panel: Panel): Vec4f = |
433 | 132 panel.mesh.material["color", 0, Vec4f] |
133 proc `color=`*(panel: var Panel, value: Vec4f) = | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
134 if value != panel.mesh.material["color", 0, Vec4f]: |
433 | 135 panel.mesh.material["color", 0] = value |
136 | |
1138 | 137 proc HorizontalAlignment*(panel: Panel): HorizontalAlignment = |
430 | 138 panel.horizontalAlignment |
139 proc `horizontalAlignment=`*(panel: var Panel, value: HorizontalAlignment) = | |
140 if value != panel.horizontalAlignment: | |
141 panel.horizontalAlignment = value | |
142 panel.dirty = true | |
143 | |
1138 | 144 proc VerticalAlignment*(panel: Panel): VerticalAlignment = |
430 | 145 panel.verticalAlignment |
146 proc `verticalAlignment=`*(panel: var Panel, value: VerticalAlignment) = | |
147 if value != panel.verticalAlignment: | |
148 panel.verticalAlignment = value | |
149 panel.dirty = true | |
150 | |
1138 | 151 proc Contains*(panel: Panel, p: Vec2f, aspectRatio: float32): bool = |
434 | 152 let |
1136 | 153 cursor = panel.mesh.transform.Inversed * p.ToVec3 |
444 | 154 p1 = panel.mesh[POSITION_ATTRIB, 0, Vec3f] |
155 p2 = panel.mesh[POSITION_ATTRIB, 2, Vec3f] | |
434 | 156 left = min(p1.x, p2.x) |
157 right = max(p1.x, p2.x) | |
444 | 158 top = min(p1.y * aspectRatio, p2.y * aspectRatio) |
159 bottom = max(p1.y * aspectRatio, p2.y * aspectRatio) | |
434 | 160 return left <= cursor.x and cursor.x <= right and top <= cursor.y and cursor.y <= bottom |