|
| 1 | +/********************************************************************************* |
| 2 | +* MIT License |
| 3 | +* |
| 4 | +* Copyright (c) 2023 Strata Source Contributors |
| 5 | +* |
| 6 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +* of this software and associated documentation files (the "Software"), to deal |
| 8 | +* in the Software without restriction, including without limitation the rights |
| 9 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +* copies of the Software, and to permit persons to whom the Software is |
| 11 | +* furnished to do so, subject to the following conditions: |
| 12 | +* |
| 13 | +* The above copyright notice and this permission notice shall be included in all |
| 14 | +* copies or substantial portions of the Software. |
| 15 | +* |
| 16 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +* SOFTWARE. |
| 23 | +*********************************************************************************/ |
| 24 | +#include "imgui_impl_source.h" |
| 25 | + |
| 26 | +#include "KeyValues.h" |
| 27 | +#include "materialsystem/imesh.h" |
| 28 | +#include "materialsystem/itexture.h" |
| 29 | +#include "imgui/imgui.h" |
| 30 | +#include "imgui/imgui_internal.h" |
| 31 | +#include "pixelwriter.h" |
| 32 | +#include "filesystem.h" |
| 33 | + |
| 34 | +#include "vgui/ISystem.h" |
| 35 | +#include "vgui_controls/Controls.h" |
| 36 | + |
| 37 | +#include "tier0/memdbgon.h" |
| 38 | + |
| 39 | +static IMaterial *g_pFontMat = nullptr; |
| 40 | + |
| 41 | +class CDearImGuiFontTextureRegenerator : public ITextureRegenerator |
| 42 | +{ |
| 43 | +public: |
| 44 | + CDearImGuiFontTextureRegenerator() = default; |
| 45 | + |
| 46 | + // Inherited from ITextureRegenerator |
| 47 | + void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect ) override |
| 48 | + { |
| 49 | + ImGuiIO &io = ImGui::GetIO(); |
| 50 | + unsigned char *pixels; |
| 51 | + int width, height; |
| 52 | + io.Fonts->GetTexDataAsRGBA32( &pixels, &width, &height ); |
| 53 | + |
| 54 | + Assert( pVTFTexture->Width() == width ); |
| 55 | + Assert( pVTFTexture->Height() == height ); |
| 56 | + // if we ever use freetype for font loading, this should do format conversion instead |
| 57 | + memcpy( pVTFTexture->ImageData(), pixels, 4ULL * width * height ); |
| 58 | + } |
| 59 | + |
| 60 | + void Release() override |
| 61 | + { |
| 62 | + delete this; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +void ImGui_ImplSource_SetupRenderState( IMatRenderContext *ctx, ImDrawData *draw_data ) |
| 67 | +{ |
| 68 | + // Apply imgui's display dimensions |
| 69 | + ctx->Viewport( draw_data->DisplayPos.x, draw_data->DisplayPos.y, draw_data->DisplaySize.x, draw_data->DisplaySize.y ); |
| 70 | + |
| 71 | + // Setup orthographic projection matrix |
| 72 | + // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. |
| 73 | + ctx->MatrixMode( MATERIAL_PROJECTION ); |
| 74 | + ctx->LoadIdentity(); |
| 75 | + ctx->Scale( 1, -1, 1 ); |
| 76 | + |
| 77 | + float L = draw_data->DisplayPos.x; |
| 78 | + float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; |
| 79 | + float T = draw_data->DisplayPos.y; |
| 80 | + float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; |
| 81 | + ctx->Ortho( L, T, R, B, 0.f, 1.f ); |
| 82 | + |
| 83 | + ctx->MatrixMode( MATERIAL_VIEW ); |
| 84 | + ctx->LoadIdentity(); |
| 85 | +} |
| 86 | + |
| 87 | +void ImGui_ImplSource_RenderDrawData( ImDrawData *draw_data ) |
| 88 | +{ |
| 89 | + // Avoid rendering when minimized |
| 90 | + if ( draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f ) |
| 91 | + return; |
| 92 | + |
| 93 | + CMatRenderContextPtr ctx( materials ); |
| 94 | + |
| 95 | + ctx->MatrixMode( MATERIAL_VIEW ); |
| 96 | + ctx->PushMatrix(); |
| 97 | + ctx->MatrixMode( MATERIAL_PROJECTION ); |
| 98 | + ctx->PushMatrix(); |
| 99 | + |
| 100 | + ImGui_ImplSource_SetupRenderState( ctx, draw_data ); |
| 101 | + |
| 102 | + // Render command lists |
| 103 | + ImVec2 clip_off = draw_data->DisplayPos; |
| 104 | + for ( int n = 0; n < draw_data->CmdListsCount; n++ ) |
| 105 | + { |
| 106 | + const ImDrawList *cmd_list = draw_data->CmdLists[n]; |
| 107 | + const ImDrawIdx *idx_buffer = cmd_list->IdxBuffer.Data; |
| 108 | + |
| 109 | + // Draw the mesh |
| 110 | + for ( int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++ ) |
| 111 | + { |
| 112 | + const ImDrawCmd *pcmd = &cmd_list->CmdBuffer[cmd_i]; |
| 113 | + if ( pcmd->UserCallback != nullptr ) |
| 114 | + { |
| 115 | + // User callback, registered via ImDrawList::AddCallback() |
| 116 | + // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) |
| 117 | + if ( pcmd->UserCallback == ImDrawCallback_ResetRenderState ) |
| 118 | + ImGui_ImplSource_SetupRenderState( ctx, draw_data ); |
| 119 | + else |
| 120 | + pcmd->UserCallback( cmd_list, pcmd ); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + if ( pcmd->GetTexID() ) |
| 125 | + { |
| 126 | + Vector2D clipmin = { pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y }; |
| 127 | + Vector2D clipmax = { pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y }; |
| 128 | + |
| 129 | + // Avoid rendering completely clipped draws |
| 130 | + if ( clipmax.x <= clipmin.x || clipmax.y <= clipmin.y ) |
| 131 | + continue; |
| 132 | + |
| 133 | + ctx->SetScissorRect( clipmin.x, clipmin.y, clipmax.x, clipmax.y, true ); |
| 134 | + IMesh *mesh = ctx->GetDynamicMesh( false, nullptr, nullptr, static_cast<IMaterial*>( pcmd->GetTexID() ) ); |
| 135 | + CMeshBuilder mb; |
| 136 | + mb.Begin( mesh, MATERIAL_TRIANGLES, cmd_list->VtxBuffer.Size, pcmd->ElemCount ); |
| 137 | + |
| 138 | + const ImDrawVert *vtx_src = cmd_list->VtxBuffer.Data; |
| 139 | + for ( int i = 0; i < cmd_list->VtxBuffer.Size; i++ ) |
| 140 | + { |
| 141 | + mb.Position3f( Vector2DExpand( vtx_src->pos ), 0 ); |
| 142 | + mb.Color4ubv( reinterpret_cast<const unsigned char*>( &vtx_src->col ) ); |
| 143 | + mb.TexCoord2fv( 0, &vtx_src->uv.x ); |
| 144 | + mb.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 1>(); |
| 145 | + vtx_src++; |
| 146 | + } |
| 147 | + |
| 148 | + static_cast<CIndexBuilder &>( mb ).FastIndexList( idx_buffer + pcmd->IdxOffset, 0, pcmd->ElemCount ); |
| 149 | + mb.End( false, true ); |
| 150 | + ctx->SetScissorRect( clipmin.x, clipmin.y, clipmax.x, clipmax.y, false ); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + ctx->MatrixMode( MATERIAL_PROJECTION ); |
| 157 | + ctx->PopMatrix(); |
| 158 | + ctx->MatrixMode( MATERIAL_VIEW ); |
| 159 | + ctx->PopMatrix(); |
| 160 | +} |
| 161 | + |
| 162 | +bool ImGui_ImplSource_Init() |
| 163 | +{ |
| 164 | + // Setup backend capabilities flags |
| 165 | + ImGuiIO &io = ImGui::GetIO(); |
| 166 | + io.BackendPlatformName = "source"; |
| 167 | + io.BackendRendererName = "imgui_impl_source"; |
| 168 | + io.BackendFlags = ImGuiBackendFlags_None; |
| 169 | + io.SetClipboardTextFn = []( void *, const char *c ) |
| 170 | + { |
| 171 | + vgui::system()->SetClipboardText( c, V_strlen( c ) ); |
| 172 | + }; |
| 173 | + io.GetClipboardTextFn = []( void *ctx ) -> const char * |
| 174 | + { |
| 175 | + auto &g = *static_cast<ImGuiContext *>( ctx ); |
| 176 | + g.ClipboardHandlerData.clear(); |
| 177 | + auto len = vgui::system()->GetClipboardTextCount(); |
| 178 | + if ( !len ) |
| 179 | + return nullptr; |
| 180 | + g.ClipboardHandlerData.resize( len ); |
| 181 | + vgui::system()->GetClipboardText( 0, g.ClipboardHandlerData.Data, g.ClipboardHandlerData.Size ); |
| 182 | + return g.ClipboardHandlerData.Data; |
| 183 | + }; |
| 184 | + |
| 185 | + ImGui_ImplSource_CreateDeviceObjects(); |
| 186 | + return true; |
| 187 | +} |
| 188 | + |
| 189 | +void ImGui_ImplSource_Shutdown() |
| 190 | +{ |
| 191 | + ImGui_ImplSource_InvalidateDeviceObjects(); |
| 192 | +} |
| 193 | + |
| 194 | +static bool ImGui_ImplSource_CreateFontsTexture() |
| 195 | +{ |
| 196 | + if ( g_pFontMat ) |
| 197 | + return true; |
| 198 | + |
| 199 | + // Build texture atlas |
| 200 | + ImGuiIO &io = ImGui::GetIO(); |
| 201 | + int width, height; |
| 202 | + unsigned char* pixels; |
| 203 | + io.Fonts->GetTexDataAsRGBA32( &pixels, &width, &height ); |
| 204 | + |
| 205 | + // Create a material for the texture |
| 206 | + ITexture *fonttex = g_pMaterialSystem->CreateProceduralTexture( "imgui_font", TEXTURE_GROUP_OTHER, width, height, IMAGE_FORMAT_RGBA8888, TEXTUREFLAGS_NOMIP | TEXTUREFLAGS_POINTSAMPLE | TEXTUREFLAGS_PROCEDURAL | TEXTUREFLAGS_SINGLECOPY | TEXTUREFLAGS_NOLOD ); |
| 207 | + fonttex->SetTextureRegenerator( new CDearImGuiFontTextureRegenerator ); |
| 208 | + |
| 209 | + KeyValues *vmt = new KeyValues( "UnlitGeneric" ); |
| 210 | + vmt->SetString( "$basetexture", "imgui_font" ); |
| 211 | + vmt->SetInt( "$nocull", 1 ); |
| 212 | + vmt->SetInt( "$vertexcolor", 1 ); |
| 213 | + vmt->SetInt( "$vertexalpha", 1 ); |
| 214 | + vmt->SetInt( "$translucent", 1 ); |
| 215 | + g_pFontMat = materials->CreateMaterial( "imgui_font_mat", vmt ); |
| 216 | + g_pFontMat->AddRef(); |
| 217 | + |
| 218 | + // Store our identifier |
| 219 | + io.Fonts->SetTexID( g_pFontMat ); |
| 220 | + |
| 221 | + return true; |
| 222 | +} |
| 223 | + |
| 224 | +bool ImGui_ImplSource_CreateDeviceObjects() |
| 225 | +{ |
| 226 | + return ImGui_ImplSource_CreateFontsTexture(); |
| 227 | +} |
| 228 | + |
| 229 | +void ImGui_ImplSource_InvalidateDeviceObjects() |
| 230 | +{ |
| 231 | + if ( g_pFontMat ) |
| 232 | + { |
| 233 | + g_pFontMat->DecrementReferenceCount(); |
| 234 | + g_pFontMat = nullptr; |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +// The following functions are declared in imconfig_source.h and must not be renamed |
| 239 | + |
| 240 | +ImFileHandle ImFileOpen( const char *filename, const char *mode ) |
| 241 | +{ |
| 242 | + Assert( g_pFullFileSystem ); |
| 243 | + return g_pFullFileSystem->Open( filename, mode ); |
| 244 | +} |
| 245 | + |
| 246 | +bool ImFileClose( ImFileHandle f ) |
| 247 | +{ |
| 248 | + if ( f == nullptr ) |
| 249 | + return false; |
| 250 | + |
| 251 | + Assert( g_pFullFileSystem ); |
| 252 | + g_pFullFileSystem->Close( f ); |
| 253 | + return true; |
| 254 | +} |
| 255 | + |
| 256 | +uint64 ImFileGetSize( ImFileHandle f ) |
| 257 | +{ |
| 258 | + Assert( g_pFullFileSystem && f ); |
| 259 | + |
| 260 | + return g_pFullFileSystem->Size( f ); |
| 261 | +} |
| 262 | + |
| 263 | +uint64 ImFileRead( void *data, uint64 sz, uint64 count, ImFileHandle f ) |
| 264 | +{ |
| 265 | + Assert( g_pFullFileSystem && f ); |
| 266 | + |
| 267 | + return g_pFullFileSystem->Read( data, sz * count, f ); |
| 268 | +} |
| 269 | + |
| 270 | +uint64 ImFileWrite( const void *data, uint64 sz, uint64 count, ImFileHandle f ) |
| 271 | +{ |
| 272 | + Assert( g_pFullFileSystem && f ); |
| 273 | + |
| 274 | + return g_pFullFileSystem->Write( data, sz * count, f ); |
| 275 | +} |
0 commit comments