guiMaterialCtrl.cpp
Engine/source/gui/controls/guiMaterialCtrl.cpp
Public Functions
ConsoleDocClass(GuiMaterialCtrl , "@brief Container <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiMaterialPreview\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
DefineEngineMethod(GuiMaterialCtrl , setMaterial , bool , (const char *materialName) , "( string materialName )" "Set the material <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be displayed in the control." )
Detailed Description
Public Functions
ConsoleDocClass(GuiMaterialCtrl , "@brief Container <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiMaterialPreview\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
DefineEngineMethod(GuiMaterialCtrl , setMaterial , bool , (const char *materialName) , "( string materialName )" "Set the material <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be displayed in the control." )
IMPLEMENT_CONOBJECT(GuiMaterialCtrl )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "gui/controls/guiMaterialCtrl.h" 26 27#include "materials/baseMatInstance.h" 28#include "materials/materialManager.h" 29#include "materials/sceneData.h" 30#include "core/util/safeDelete.h" 31#include "console/console.h" 32#include "console/consoleTypes.h" 33#include "console/engineAPI.h" 34#include "gfx/gfxDevice.h" 35#include "math/util/matrixSet.h" 36#include "scene/sceneRenderState.h" 37 38IMPLEMENT_CONOBJECT( GuiMaterialCtrl ); 39 40ConsoleDocClass( GuiMaterialCtrl, 41 "@brief Container for GuiMaterialPreview\n\n" 42 "Editor use only.\n\n" 43 "@internal" 44); 45 46GuiMaterialCtrl::GuiMaterialCtrl() 47 : mMaterialInst( NULL ) 48{ 49} 50 51void GuiMaterialCtrl::initPersistFields() 52{ 53 addGroup( "Material" ); 54 addProtectedField( "materialName", TypeStringFilename, Offset( mMaterialName, GuiMaterialCtrl ), &GuiMaterialCtrl::_setMaterial, &defaultProtectedGetFn, "" ); 55 endGroup( "Material" ); 56 57 Parent::initPersistFields(); 58} 59 60bool GuiMaterialCtrl::onWake() 61{ 62 if ( !Parent::onWake() ) 63 return false; 64 65 setActive( true ); 66 setMaterial( mMaterialName ); 67 68 return true; 69} 70 71void GuiMaterialCtrl::onSleep() 72{ 73 SAFE_DELETE( mMaterialInst ); 74 75 Parent::onSleep(); 76} 77 78bool GuiMaterialCtrl::_setMaterial( void *object, const char *index, const char *data ) 79{ 80 static_cast<GuiMaterialCtrl *>( object )->setMaterial( data ); 81 82 // Return false to keep the caller from setting the field. 83 return false; 84} 85 86bool GuiMaterialCtrl::setMaterial( const String &materialName ) 87{ 88 SAFE_DELETE( mMaterialInst ); 89 mMaterialName = materialName; 90 91 if ( mMaterialName.isNotEmpty() && isAwake() ) 92 mMaterialInst = MATMGR->createMatInstance( mMaterialName, getGFXVertexFormat<GFXVertexPCT>() ); 93 94 return true; 95} 96 97void GuiMaterialCtrl::inspectPostApply() 98{ 99 Parent::inspectPostApply(); 100} 101 102void GuiMaterialCtrl::onRender( Point2I offset, const RectI &updateRect ) 103{ 104 Parent::onRender( offset, updateRect ); 105 106 if ( !mMaterialInst ) 107 return; 108 109 // Draw a quad with the material assigned 110 GFXVertexBufferHandle<GFXVertexPCT> verts( GFX, 4, GFXBufferTypeVolatile ); 111 verts.lock(); 112 113 F32 screenLeft = updateRect.point.x; 114 F32 screenRight = (updateRect.point.x + updateRect.extent.x); 115 F32 screenTop = updateRect.point.y; 116 F32 screenBottom = (updateRect.point.y + updateRect.extent.y); 117 118 const F32 fillConv = GFX->getFillConventionOffset(); 119 verts[0].point.set( screenLeft - fillConv, screenTop - fillConv, 0.f ); 120 verts[1].point.set( screenRight - fillConv, screenTop - fillConv, 0.f ); 121 verts[2].point.set( screenLeft - fillConv, screenBottom - fillConv, 0.f ); 122 verts[3].point.set( screenRight - fillConv, screenBottom - fillConv, 0.f ); 123 124 verts[0].color = verts[1].color = verts[2].color = verts[3].color = ColorI( 255, 255, 255, 255 ); 125 126 verts[0].texCoord.set( 0.0f, 0.0f ); 127 verts[1].texCoord.set( 1.0f, 0.0f ); 128 verts[2].texCoord.set( 0.0f, 1.0f ); 129 verts[3].texCoord.set( 1.0f, 1.0f ); 130 131 verts.unlock(); 132 133 GFX->setVertexBuffer( verts ); 134 135 MatrixSet matSet; 136 matSet.setWorld(GFX->getWorldMatrix()); 137 matSet.setView(GFX->getViewMatrix()); 138 matSet.setProjection(GFX->getProjectionMatrix()); 139 140 MatrixF cameraMatrix( true ); 141 F32 left, right, top, bottom, nearPlane, farPlane; 142 bool isOrtho; 143 GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho ); 144 Frustum frust( isOrtho, left, right, top, bottom, nearPlane, farPlane, cameraMatrix ); 145 146 SceneRenderState state 147 ( 148 gClientSceneGraph, 149 SPT_Diffuse, 150 SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ), 151 gClientSceneGraph->getDefaultRenderPass(), 152 false 153 ); 154 155 SceneData sgData; 156 sgData.init( &state ); 157 sgData.wireframe = false; // Don't wireframe this. 158 159 while( mMaterialInst->setupPass( &state, sgData ) ) 160 { 161 mMaterialInst->setSceneInfo( &state, sgData ); 162 mMaterialInst->setTransforms( matSet, &state ); 163 GFX->drawPrimitive( GFXTriangleStrip, 0, 2 ); 164 } 165 166 // Clean up 167 GFX->setShader( NULL ); 168 GFX->setTexture( 0, NULL ); 169} 170 171DefineEngineMethod( GuiMaterialCtrl, setMaterial, bool, ( const char * materialName ), , "( string materialName )" 172 "Set the material to be displayed in the control." ) 173{ 174 return object->setMaterial( materialName ); 175} 176