From 8ad5cc77c33c782516b2d8573ccef884874e48ba Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sat, 27 Sep 2025 08:35:52 +0100 Subject: [PATCH] change colour of text in search bar as well --- .../text-builder/normal-mode-text-builder.sml | 4 +- fcore/text-builder/search-bar.sml | 6 +- temp.txt | 173 +++++++++++++++++- 3 files changed, 177 insertions(+), 6 deletions(-) diff --git a/fcore/text-builder/normal-mode-text-builder.sml b/fcore/text-builder/normal-mode-text-builder.sml index 66b4f08..0c69a50 100644 --- a/fcore/text-builder/normal-mode-text-builder.sml +++ b/fcore/text-builder/normal-mode-text-builder.sml @@ -29,8 +29,8 @@ struct } = buffer val env = Utils.initEnv - ( 5 - , 5 + ( 0 + , 0 , windowWidth , windowHeight , floatWindowWidth diff --git a/fcore/text-builder/search-bar.sml b/fcore/text-builder/search-bar.sml index 3bb4f2b..e69d1c8 100644 --- a/fcore/text-builder/search-bar.sml +++ b/fcore/text-builder/search-bar.sml @@ -12,9 +12,9 @@ struct else let val chr = String.sub (str, pos) - val r: Real32.real = 0.67 - val g: Real32.real = 0.51 - val b: Real32.real = 0.83 + val r: Real32.real = 0.01 + val g: Real32.real = 0.01 + val b: Real32.real = 0.01 val fPosX = Real32.fromInt posX val fPosY = Real32.fromInt posY val z: Real32.real = 0.1 diff --git a/temp.txt b/temp.txt index 6ab201c..72bd141 100644 --- a/temp.txt +++ b/temp.txt @@ -1 +1,172 @@ -hello wor!d again +structure GlDraw = +struct + open CML + open DrawMsg + + (* The name doesn't make it clear, but this structure + * couples GLFW and OpenGL. + * I'm not sure if I will use native windowing systems + * or other graphics APIs at a later stage, + * but the current priority is GLFW + OpenGL. + * *) + type t = + { textVertexBuffer: Word32.word + , textProgram: Word32.word + , textDrawLength: int + , window: MLton.Pointer.t + } + + fun createShader (shaderType, shaderString) = + let + val shader = Gles3.createShader shaderType + val _ = Gles3.shaderSource (shader, shaderString) + val _ = Gles3.compileShader shader + in + shader + end + + fun createProgram (vertexShader, fragmentShader) = + let + val program = Gles3.createProgram () + val _ = Gles3.attachShader (program, vertexShader) + val _ = Gles3.attachShader (program, fragmentShader) + val _ = Gles3.linkProgram program + in + program + end + + fun create window = + let + (* create vertex buffer, program, etc. for text. *) + val textVertexBuffer = Gles3.createBuffer () + val xyzRgbVertexShader = createShader + (Gles3.VERTEX_SHADER, GlShaders.xyzRgbVertexShaderString) + + val rgbFragmentShader = createShader + (Gles3.FRAGMENT_SHADER, GlShaders.rgbFragmentShaderString) + + val textProgram = createProgram (xyzRgbVertexShader, rgbFragmentShader) + + (* clean up shaders which are no longer needed once progran is linked. *) + val _ = Gles3.deleteShader xyzRgbVertexShader + val _ = Gles3.deleteShader rgbFragmentShader + + (* because we only have a single vertex buffer, + * we only need to bind and set attributes once. *) + val _ = Gles3.bindBuffer textVertexBuffer + + (* enable xyz component from uploaded array *) + val _ = Gles3.vertexAttribPointer (0, 3, 6, 0) + val _ = Gles3.enableVertexAttribArray 0 + (* enable rgb component from uploaded array *) + val _ = Gles3.vertexAttribPointer (1, 3, 6, 12) + val _ = Gles3.enableVertexAttribArray 1 + + val _ = Gles3.useProgram textProgram + in + { textVertexBuffer = textVertexBuffer + , textProgram = textProgram + , textDrawLength = 0 + , window = window + } + end + + fun uploadText (shellState: t, vec) = + let + val {textVertexBuffer, textProgram, textDrawLength = _, window} = + shellState + + val _ = Gles3.bufferData (vec, Vector.length vec, Gles3.STATIC_DRAW) + val newTextDrawLength = Vector.length vec div 6 + in + { textVertexBuffer = textVertexBuffer + , textProgram = textProgram + , textDrawLength = newTextDrawLength + , window = window + } + end + + fun draw (drawObject: t) = + let + val {textVertexBuffer, textDrawLength, textProgram, window = _} = + drawObject + in + Gles3.drawArrays (Gles3.TRIANGLES, 0, textDrawLength) + end + + fun yank (shellState: t, str) = + let + (* print when text is yanked *) + val msg = "|" ^ String.toCString str ^ "|\n" + val () = print msg + val () = Glfw.setClipboardString (#window shellState, str) + in + shellState + end + + fun consumeDrawEvent (shellState, msg) = + let + val {textVertexBuffer, textProgram, window, textDrawLength = _, ...} = + shellState + in + case msg of + DRAW_TEXT textVec => uploadText (shellState, textVec) + | YANK str => yank (shellState, str) + end + + local + fun loop (pos, msgVec, shellState) = + if pos = Vector.length msgVec then + shellState + else + let + val msg = Vector.sub (msgVec, pos) + val shellState = consumeDrawEvent (shellState, msg) + in + loop (pos + 1, msgVec, shellState) + end + in + fun consumeDrawEvents shellState = + loop (0, DrawMailbox.getMessagesAndClear (), shellState) + end + + local + fun updateLoop (pos, msgVec, app) = + if pos = Vector.length msgVec then + app + else + let + val msg = Vector.sub (msgVec, pos) + val app = Updater.update (app, msg) + in + updateLoop (pos + 1, msgVec, app) + end + in + fun update app = + updateLoop (0, InputMailbox.getMessagesAndClear (), app) + end + + fun helpLoop (app, shellState as {window, ...}: t) = + case Glfw.windowShouldClose window of + false => + let + val shellState = consumeDrawEvents shellState + + val _ = Gles3.clearColor (0.97, 0.95, 0.937, 1.0) + val _ = Gles3.clear () + + val app = update app + val _ = draw shellState + + val _ = Glfw.swapBuffers window + val _ = Glfw.waitEvents () + in + helpLoop (app, shellState) + end + | true => Glfw.terminate () + + fun loop (app, window) = + let val shellState = create window + in helpLoop (app, shellState) + end +end