CapsAdmin

Untitled

Dec 27th, 2011
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.22 KB | None | 0 0
  1. #include "StdAfx.h"
  2.  
  3. #include "Gwen/Gwen.h"
  4.  
  5. #include "Gwen/Skins/TexturedBase.h"
  6. #include "Gwen/Controls/Canvas.h"
  7.  
  8. #include "gwen_unittest/UnitTest.h"
  9. #include "Gwen/BaseRender.h"
  10.  
  11. #include "IHardwareMouse.h"
  12. #include "Game.h"
  13. #include "IUIDraw.h"
  14.  
  15. #include "Cry_Math.h"
  16. #include <strsafe.h>
  17.  
  18. #include <d3dx9.h>
  19. #include <d3d9.h>
  20.  
  21. #pragma comment(lib, "d3dx9d.lib")
  22.  
  23. namespace gui
  24. {      
  25.     const wchar_t *CharToWide(const char *_from)
  26.     {
  27.         wstring to;
  28.         string from(_from);
  29.  
  30.         size_t len = from.length();
  31.         wchar_t* buff = new wchar_t[len + 1];
  32.         buff[len] = '\0';
  33.         mbstowcs(buff, from.c_str(), len);
  34.         to = buff;
  35.         delete[] buff;
  36.  
  37.         return to.c_str();
  38.     }
  39.  
  40.     const char *WideToChar(const wchar_t *_from)
  41.     {
  42.         string to;
  43.         wstring from(_from);
  44.  
  45.         size_t len = from.length();
  46.         char* buff = new char[len + 1];
  47.         buff[len] = '\0';
  48.         size_t res = wcstombs(buff, from.c_str(), len);
  49.         to = buff;
  50.         delete[] buff;
  51.  
  52.         return to.c_str();
  53.     }
  54.  
  55.     Gwen::Controls::Canvas *canvas;
  56.  
  57.     static int white;
  58.  
  59.     class GwenRenderer : public Gwen::Renderer::Base
  60.     {
  61.         public:
  62.             #define uidraw gEnv->pGame->GetIGameFramework()->GetIUIDraw()
  63.             #define render gEnv->pRenderer
  64.        
  65.             GwenRenderer()
  66.             {
  67.             }
  68.             ~GwenRenderer()
  69.             {
  70.             }
  71.  
  72.             void dxLoadTexture( Gwen::Texture* pTexture )
  73.             {
  74.                 if (gEnv->pRenderer->GetRenderType() == eRT_DX9)
  75.                 {
  76.                     auto pD3DDevice = static_cast<IDirect3DDevice9*>(gEnv->pRenderer->EF_Query(EFQ_D3DDevice));
  77.  
  78.                     if (pD3DDevice)
  79.                     {
  80.  
  81.                         IDirect3DTexture9* ptr = NULL;
  82.                         D3DXIMAGE_INFO ImageInfo;
  83.                         auto path = pTexture->name.GetUnicode().c_str();
  84.  
  85.                         HRESULT hr = D3DXCreateTextureFromFileExW( pD3DDevice, path, 0, 0, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &ImageInfo, NULL, &ptr );
  86.                         if ( hr != S_OK )
  87.                         {
  88.                             return;
  89.                         }
  90.  
  91.                         pTexture->data = ptr;
  92.                         pTexture->width = ImageInfo.Width;
  93.                         pTexture->height = ImageInfo.Height;
  94.                     }
  95.                 }
  96.             }
  97.             Gwen::Color dxPixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color& col_default )
  98.             {
  99.                 IDirect3DTexture9* pImage = (IDirect3DTexture9*) pTexture->data;
  100.                 if ( !pImage ) return col_default;
  101.  
  102.                 IDirect3DSurface9* pSurface = NULL;
  103.  
  104.                 if ( pImage->GetSurfaceLevel( 0, &pSurface ) != S_OK ) return col_default;
  105.                 if ( !pSurface ) return col_default;
  106.  
  107.                 D3DLOCKED_RECT lockedRect;
  108.                 pSurface->LockRect( &lockedRect, NULL, D3DLOCK_READONLY );
  109.                 DWORD* pixels = (DWORD*)lockedRect.pBits;
  110.                 D3DXCOLOR color = pixels[lockedRect.Pitch / sizeof(DWORD) * y + x];
  111.                 pSurface->UnlockRect();
  112.  
  113.                 return Gwen::Color( color.r*255, color.g*255, color.b*255, color.a*255 );
  114.             }
  115.  
  116.             void Begin()
  117.             {
  118.                 uidraw->PreRender();
  119.             }
  120.             void End()
  121.             {
  122.                 uidraw->PostRender();
  123.             }
  124.        
  125.             void SetDrawColor(Gwen::Color color)
  126.             {
  127.                 draw_color.r = (float)color.r / 255.00f;
  128.                 draw_color.g = (float)color.g / 255.00f;
  129.                 draw_color.b = (float)color.b / 255.00f;
  130.                 draw_color.a = (float)color.a / 255.00f;
  131.             }
  132.  
  133.             void DrawFilledRect(Gwen::Rect rect)
  134.             {
  135.                 Translate(rect);
  136.  
  137.                 white = white ? white : uidraw->CreateTexture("textures/defaults/white.dds", true);
  138.  
  139.                 /*uidraw->DrawQuadSimple(
  140.                     rect.x / render->ScaleCoordX(1),
  141.                     rect.y / render->ScaleCoordY(1),
  142.                     rect.w / render->ScaleCoordX(1),
  143.                     rect.h / render->ScaleCoordY(1),
  144.                    
  145.                     draw_color.pack_argb8888(),
  146.                     white
  147.                 );*/
  148.                 render->DrawImage(rect.x, rect.y, rect.w, rect.h, white, 0, 0, 1, 1, draw_color.r, draw_color.g, draw_color.b, draw_color.a, false);
  149.             }
  150.  
  151.             void LoadFont(Gwen::Font* pFont)
  152.             {
  153.                 pFont->realsize = floor(pFont->size * Scale() + 2);
  154.  
  155.                 auto path = WideToChar(pFont->facename.c_str());
  156.                 auto font = gEnv->pSystem->GetICryFont()->NewFont(path);
  157.                 auto success = false;
  158.  
  159.                 #define flags TTFFLAG_SMOOTH_BLUR | TTFLAG_SMOOTH_AMOUNT_2X | FONTRF_VCENTERED | TTFFLAG_CREATE(4, 2)
  160.                 #define scale 1024
  161.  
  162.                 if (font)
  163.                 {
  164.                     success = font->Load(path, scale, scale, flags);
  165.  
  166.                     if (!success)
  167.                     {
  168.                         CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "gwen cannot load the font called '%s', reverting to 'OpenSans.ttf'", name);
  169.  
  170.                         success = font->Load("fonts/OpenSans.ttf", scale, scale, flags);   
  171.                     }
  172.  
  173.                     if (success)
  174.                     {
  175.                         pFont->data = font;
  176.                     }
  177.                 }
  178.  
  179.                 if (!success)
  180.                 {  
  181.                     CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "gwen cannot load the font called 'OpenSans.ttf', reverting to cryengine's 'default' font", name);
  182.  
  183.                     font = gEnv->pSystem->GetICryFont()->GetFont("default");
  184.                     pFont->data = font;
  185.                 }
  186.             }
  187.             void FreeFont(Gwen::Font* pFont)
  188.             {
  189.                 if (!pFont->data) return;
  190.  
  191.                 IFFont *font = (IFFont *)pFont->data;
  192.  
  193.                 if (!font) return;
  194.                    
  195.                 font->Free();
  196.                 pFont->data = NULL;
  197.             }
  198.             IFFont *GetCryFont(Gwen::Font *pFont)
  199.             {  
  200.                 auto font = (IFFont*)pFont->data;
  201.  
  202.                 if(!font || fabs( pFont->realsize - pFont->size * Scale() ) > 2)
  203.                 {
  204.                     FreeFont(pFont);
  205.                     LoadFont(pFont);
  206.  
  207.                     if (pFont)
  208.                     {
  209.                         auto font = (IFFont*)pFont->data;
  210.                         if (font)
  211.                             return font;
  212.                     }
  213.  
  214.                     return NULL;
  215.                 }
  216.  
  217.                 return font;
  218.             }
  219.             void RenderText(Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text)
  220.             {
  221.                 auto font = GetCryFont(pFont);
  222.  
  223.                 if (!font) return;
  224.  
  225.                 Translate(pos.x, pos.y);
  226.                 const Gwen::Rect& rect = ClipRegion();
  227.  
  228.                
  229.                
  230.                 /*uidraw->DrawTextW(
  231.                     font,
  232.                     pos.x / render->ScaleCoordX(1), pos.y / render->ScaleCoordY(1),
  233.                     pFont->realsize, pFont->realsize,
  234.                     text.c_str(),
  235.                     draw_color.a, draw_color.r, draw_color.g, draw_color.b,
  236.                     UIDRAWHORIZONTAL_LEFT, UIDRAWVERTICAL_TOP, UIDRAWHORIZONTAL_LEFT, UIDRAWVERTICAL_CENTER
  237.                 );*/
  238.  
  239.                 STextDrawContext params;
  240.                     params.SetSize(Vec2(1,1) * pFont->realsize);
  241.  
  242.                     params.SetColor(draw_color);
  243.                     params.EnableClipping(true);
  244.                     params.SetClippingRect(rect.x, rect.y, rect.w, rect.h);
  245.  
  246.                 font->DrawStringW(
  247.                     pos.x / render->ScaleCoordX(1),
  248.                     (pos.y / render->ScaleCoordY(1)),
  249.                     text.c_str(),
  250.                     false,
  251.                     params
  252.                 );
  253.             }
  254.             Gwen::Point MeasureText(Gwen::Font* pFont, const Gwen::UnicodeString& text)
  255.             {
  256.                 auto font = GetCryFont(pFont);
  257.  
  258.                 if (!font) return Gwen::Point(0,0);
  259.  
  260.                 auto size = Vec2(0, 0);
  261.                 auto point = Gwen::Point(0,0);
  262.  
  263.                 STextDrawContext params;
  264.                 params.SetSize(Vec2(1,1) * pFont->realsize);
  265.                
  266.                 if (text.empty())
  267.                 {
  268.                     size = font->GetTextSizeW(L"W", false, params);
  269.  
  270.                     point.y = size.y;
  271.                    
  272.                     /*float x = 0;
  273.                     float y = 0;
  274.  
  275.                     uidraw->GetTextDimW(font, &x, &y, pFont->realsize, pFont->realsize, L"W");
  276.  
  277.                     point.x = (int)x;*/
  278.                 }
  279.                 else
  280.                 {  
  281.                     auto str = text.c_str();
  282.                     size = font->GetTextSizeW(str, false, params);
  283.                    
  284.                     point.x = size.x;
  285.                     point.y = size.y;
  286.  
  287.                     /*float x = 0;
  288.                     float y = 0;
  289.                    
  290.                     uidraw->GetTextDimW(font, &x, &y, pFont->realsize, pFont->realsize, str);
  291.  
  292.                     point.x = (int)x;
  293.                     point.y = (int)y;*/
  294.                 }
  295.                                                        
  296.                 return point;
  297.             }
  298.  
  299.             void StartClip()
  300.             {
  301.                 const Gwen::Rect& rect = ClipRegion();
  302.  
  303.                 //render->
  304.  
  305.                 render->SetScissor(rect.x, rect.y, rect.w, rect.h);
  306.             }
  307.             void EndClip()
  308.             {
  309.                 render->SetScissor();
  310.             }
  311.          
  312.             void DrawTexturedRect(Gwen::Texture* tex, Gwen::Rect rect, float u1=0.0f, float v1=0.0f, float u2=1.0f, float v2=1.0f)
  313.             {
  314.                 auto crytex = (ITexture*)tex->data;
  315.  
  316.                 u1 = u1 + 0.001;
  317.                 v1 = v1 + 0.001;
  318.                 u2 = u2 + 0.001;
  319.                 v2 = v2 + 0.001;
  320.  
  321.                 #define res 1000
  322.  
  323.                 u1 = floor(u1*res) / res;
  324.                 u2 = floor(u2*res) / res;
  325.  
  326.                 v1 = floor(v1*res) / res;
  327.                 v2 = floor(v2*res) / res;
  328.                                
  329.                 Translate(rect);
  330.  
  331.                 /*uidraw->DrawImage(
  332.                     crytex->GetTextureID(),
  333.                     rect.x,
  334.                     rect.y,
  335.                     rect.w,
  336.                     rect.h,
  337.                     0,
  338.                     draw_color.r,
  339.                     draw_color.g,
  340.                     draw_color.b,
  341.                     draw_color.a,
  342.                     u1,v1,
  343.                     u2,v2
  344.                 );*/
  345.  
  346.                 /*draw_color.Set(1,1,1,1);
  347.  
  348.                 uidraw->DrawQuad(
  349.                     rect.x / render->ScaleCoordX(1),
  350.                     rect.y / render->ScaleCoordY(1),
  351.                     rect.w / render->ScaleCoordX(1),
  352.                     rect.h / render->ScaleCoordY(1),
  353.  
  354.                     draw_color.pack_argb8888(),
  355.                    
  356.                     1,1,1,1,
  357.  
  358.                     crytex->GetTextureID(),
  359.  
  360.                     u1,v1,
  361.                     u2,v1,
  362.  
  363.                     u1,v2,
  364.                     u2,v2
  365.  
  366.                 );*/
  367.                  
  368.                
  369.                 //this method "works", but it has render issues when using it with a 3d scene
  370.  
  371.                 static float* s = new float[4];
  372.                 static float* t = new float[4];
  373.                
  374.                 s[0] = u1; t[0] = v1;
  375.                 s[1] = u2; t[1] = v1;
  376.                 s[2] = u2; t[2] = v2;
  377.                 s[3] = u1; t[3] = v2;
  378.  
  379.                 render->DrawImageWithUV(   
  380.                         rect.x,
  381.                         rect.y,
  382.  
  383.                         1.0f,
  384.  
  385.                         rect.w,
  386.                         rect.h,
  387.  
  388.                         crytex->GetTextureID(),
  389.  
  390.                         s,
  391.                         t,
  392.  
  393.                         draw_color.r,
  394.                         draw_color.g,
  395.                         draw_color.b,
  396.                         draw_color.a,
  397.                         false
  398.                 );
  399.             }
  400.             void LoadTexture(Gwen::Texture* pTexture)
  401.             {  
  402.                 static string tex_path = "";
  403.  
  404.                 tex_path.Format("textures/%s", pTexture->name.Get().c_str());
  405.                
  406.                 auto tex = gEnv->pRenderer->EF_LoadTexture(tex_path.c_str());
  407.  
  408.                 pTexture->data = tex;
  409.                 pTexture->width = tex->GetWidth();
  410.                 pTexture->height = tex->GetHeight();
  411.             }
  412.             void FreeTexture(Gwen::Texture* pTexture)
  413.             {
  414.                 ITexture *tex = (ITexture*)pTexture->data;
  415.                
  416.                 if (tex)
  417.                     tex->Release();
  418.             }
  419.             Gwen::Color PixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color& col_default )
  420.             {
  421.                 auto cry_tex = (ITexture*)pTexture->data;
  422.                
  423.                 Gwen::Texture gl_tex;
  424.                     gl_tex.name = Gwen::TextObject((string)"!/" + cry_tex->GetName());
  425.                
  426.                 dxLoadTexture(&gl_tex);
  427.  
  428.                 return dxPixelColour(&gl_tex, x, y, col_default);
  429.             }
  430.  
  431.         protected:
  432.             ColorF draw_color;
  433.             ITexture *color_capture;
  434.     };
  435.     class GwenInput: public IHardwareMouseEventListener, IInputEventListener
  436.     {
  437.     public:
  438.  
  439.         GwenInput()
  440.         {
  441.             gEnv->pHardwareMouse->AddListener(this);
  442.             gEnv->pInput->SetExclusiveListener(this);
  443.  
  444.             m_MouseX = 0;
  445.             m_MouseY = 0;
  446.         }
  447.         ~GwenInput()
  448.         {
  449.             gEnv->pHardwareMouse->RemoveListener(this);
  450.             gEnv->pInput->RemoveEventListener(this);
  451.         }
  452.  
  453.         void OnHardwareMouseEvent(int x, int y, EHARDWAREMOUSEEVENT eHardwareMouseEvent, int delta)
  454.         {
  455.             if (!my || !canvas) return;
  456.  
  457.             int dx = x - m_MouseX;
  458.             int dy = y - m_MouseY;
  459.  
  460.             m_MouseX = x;
  461.             m_MouseY = y;
  462.                
  463.             canvas->InputMouseMoved(m_MouseX, m_MouseY, dx, dy);
  464.         }
  465.         virtual bool OnInputEvent(const SInputEvent &data)
  466.         {
  467.             if (!my || !canvas) return false;
  468.  
  469.             if (!(data.state == eIS_Pressed || data.state == eIS_Released))        
  470.                 return false;
  471.  
  472.             bool pressed = data.state == eIS_Pressed;
  473.  
  474.             using namespace Gwen::Key;             
  475.  
  476.             if (data.keyId == eKI_Mouse1)           return canvas->InputMouseButton(0, pressed);
  477.             if (data.keyId == eKI_Mouse2)           return canvas->InputMouseButton(1, pressed);
  478.             if (data.keyId == eKI_Mouse3)           return canvas->InputMouseButton(2, pressed);
  479.             if (data.keyId == eKI_Mouse4)           return canvas->InputMouseButton(3, pressed);
  480.             if (data.keyId == eKI_Mouse5)           return canvas->InputMouseButton(4, pressed);
  481.             if (data.keyId == eKI_Mouse6)           return canvas->InputMouseButton(5, pressed);
  482.  
  483.             if (data.keyId == eKI_MouseWheelUp)     return canvas->InputMouseWheel(data.value * 100);
  484.             if (data.keyId == eKI_MouseWheelDown)   return canvas->InputMouseWheel(-data.value * 100);
  485.  
  486.             int key = -1;
  487.  
  488.             if (data.keyId == eKI_Enter)            key = Return;
  489.             if (data.keyId == eKI_Backspace)        key = Backspace;
  490.             if (data.keyId == eKI_Delete)           key = Delete;
  491.             if (data.keyId == eKI_Left)             key = Left;
  492.             if (data.keyId == eKI_Right)            key = Right;
  493.             if (data.keyId == eKI_LShift)           key = Shift;
  494.             if (data.keyId == eKI_RShift)           key = Shift;
  495.             if (data.keyId == eKI_Tab)              key = Tab;
  496.             if (data.keyId == eKI_Space)            key = Space;
  497.             if (data.keyId == eKI_Home)             key = Home;
  498.             if (data.keyId == eKI_End)              key = End;
  499.             if (data.keyId == eKI_LCtrl)            key = Control;
  500.             if (data.keyId == eKI_RCtrl)            key = Control;
  501.             if (data.keyId == eKI_Up)               key = Up;
  502.             if (data.keyId == eKI_Down)             key = Down;
  503.             if (data.keyId == eKI_Escape)           key = Gwen::Key::Escape; // ugh
  504.             if (data.keyId == eKI_LAlt)             key = Alt;
  505.             if (data.keyId == eKI_RAlt)             key = Alt;
  506.            
  507.             if (key != -1)
  508.                 canvas->InputKey(key, pressed);
  509.  
  510.             if (pressed)
  511.             {
  512.                 auto str = CharToWide(gEnv->pInput->GetKeyName(data, true));
  513.                
  514.                 return canvas->InputCharacter(str[0]);
  515.             }
  516.  
  517.             return false;
  518.         }
  519.  
  520.         protected:
  521.             int m_MouseX;
  522.             int m_MouseY;
  523.     };
  524.  
  525.     GwenInput *gwen_input;
  526.     static bool ready = false;
  527.  
  528.     bool IsReady()
  529.     {
  530.         return ready;
  531.     }
  532.  
  533.     Gwen::Skin::Base *GetSkin()
  534.     {
  535.         return canvas->GetSkin();
  536.     }
  537.  
  538.     Gwen::Controls::Canvas *GetCanvas()
  539.     {
  540.         return canvas;
  541.     }
  542.  
  543.     GwenRenderer *GetRenderer()
  544.     {
  545.         return canvas->GetSkin() ? (GwenRenderer *)canvas->GetSkin()->GetRender() : NULL;
  546.     }
  547.  
  548.     Gwen::Font *GetFont(const char *facename)
  549.     {
  550.         if (GetRenderer())
  551.         {
  552.             Gwen::Font *font = new Gwen::Font();
  553.             font->facename = CharToWide(facename);
  554.             GetRenderer()->LoadFont(font);
  555.  
  556.             return font;
  557.         }
  558.  
  559.         return NULL;
  560.     }
  561.  
  562.     void Open()
  563.     {
  564.         if (ready) return;
  565.  
  566.         auto renderer = new GwenRenderer();
  567.  
  568.         auto skin = new Gwen::Skin::TexturedBase();
  569.             skin->SetRender(renderer);
  570.             skin->Init("gwen/defaultskin.dds");
  571.             skin->SetDefaultFont(L"fonts/OpenSans.ttf");
  572.  
  573.         canvas = new Gwen::Controls::Canvas(skin);
  574.             canvas->SetSize(gEnv->pRenderer->GetWidth(), gEnv->pRenderer->GetHeight());
  575.             canvas->SetDrawBackground(true);
  576.             canvas->SetBackgroundColor( Gwen::Color( 150, 170, 170, 255 ) );
  577.  
  578.         gwen_input = new GwenInput();
  579.  
  580.         gEnv->pHardwareMouse->Hide(false);
  581.  
  582.         ready = true;
  583.     }
  584.  
  585.     void Close()
  586.     {
  587.         #define remove(n) if (n) delete n; n = NULL
  588.        
  589.         remove(canvas);
  590.         remove(gwen_input);
  591.  
  592.         ready = false;
  593.     }
  594.  
  595.     void Draw()
  596.     {
  597.         if (!IsReady()) return;
  598.  
  599.         if (gwen_input && (GwenInput*)gEnv->pInput->GetExclusiveListener() != gwen_input)
  600.         {
  601.             gEnv->pInput->SetExclusiveListener((IInputEventListener*)gwen_input);
  602.         }
  603.  
  604.         if (canvas)
  605.         {
  606.             canvas->SetSize(gEnv->pRenderer->GetWidth(), gEnv->pRenderer->GetHeight());
  607.             canvas->RenderCanvas();
  608.         }
  609.     }
  610. }
Advertisement
Add Comment
Please, Sign In to add comment