Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "Gwen/Gwen.h"
- #include "Gwen/Skins/TexturedBase.h"
- #include "Gwen/Controls/Canvas.h"
- #include "gwen_unittest/UnitTest.h"
- #include "Gwen/BaseRender.h"
- #include "IHardwareMouse.h"
- #include "Game.h"
- #include "IUIDraw.h"
- #include "Cry_Math.h"
- #include <strsafe.h>
- #include <d3dx9.h>
- #include <d3d9.h>
- #pragma comment(lib, "d3dx9d.lib")
- namespace gui
- {
- const wchar_t *CharToWide(const char *_from)
- {
- wstring to;
- string from(_from);
- size_t len = from.length();
- wchar_t* buff = new wchar_t[len + 1];
- buff[len] = '\0';
- mbstowcs(buff, from.c_str(), len);
- to = buff;
- delete[] buff;
- return to.c_str();
- }
- const char *WideToChar(const wchar_t *_from)
- {
- string to;
- wstring from(_from);
- size_t len = from.length();
- char* buff = new char[len + 1];
- buff[len] = '\0';
- size_t res = wcstombs(buff, from.c_str(), len);
- to = buff;
- delete[] buff;
- return to.c_str();
- }
- Gwen::Controls::Canvas *canvas;
- static int white;
- class GwenRenderer : public Gwen::Renderer::Base
- {
- public:
- #define uidraw gEnv->pGame->GetIGameFramework()->GetIUIDraw()
- #define render gEnv->pRenderer
- GwenRenderer()
- {
- }
- ~GwenRenderer()
- {
- }
- void dxLoadTexture( Gwen::Texture* pTexture )
- {
- if (gEnv->pRenderer->GetRenderType() == eRT_DX9)
- {
- auto pD3DDevice = static_cast<IDirect3DDevice9*>(gEnv->pRenderer->EF_Query(EFQ_D3DDevice));
- if (pD3DDevice)
- {
- IDirect3DTexture9* ptr = NULL;
- D3DXIMAGE_INFO ImageInfo;
- auto path = pTexture->name.GetUnicode().c_str();
- HRESULT hr = D3DXCreateTextureFromFileExW( pD3DDevice, path, 0, 0, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &ImageInfo, NULL, &ptr );
- if ( hr != S_OK )
- {
- return;
- }
- pTexture->data = ptr;
- pTexture->width = ImageInfo.Width;
- pTexture->height = ImageInfo.Height;
- }
- }
- }
- Gwen::Color dxPixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color& col_default )
- {
- IDirect3DTexture9* pImage = (IDirect3DTexture9*) pTexture->data;
- if ( !pImage ) return col_default;
- IDirect3DSurface9* pSurface = NULL;
- if ( pImage->GetSurfaceLevel( 0, &pSurface ) != S_OK ) return col_default;
- if ( !pSurface ) return col_default;
- D3DLOCKED_RECT lockedRect;
- pSurface->LockRect( &lockedRect, NULL, D3DLOCK_READONLY );
- DWORD* pixels = (DWORD*)lockedRect.pBits;
- D3DXCOLOR color = pixels[lockedRect.Pitch / sizeof(DWORD) * y + x];
- pSurface->UnlockRect();
- return Gwen::Color( color.r*255, color.g*255, color.b*255, color.a*255 );
- }
- void Begin()
- {
- uidraw->PreRender();
- }
- void End()
- {
- uidraw->PostRender();
- }
- void SetDrawColor(Gwen::Color color)
- {
- draw_color.r = (float)color.r / 255.00f;
- draw_color.g = (float)color.g / 255.00f;
- draw_color.b = (float)color.b / 255.00f;
- draw_color.a = (float)color.a / 255.00f;
- }
- void DrawFilledRect(Gwen::Rect rect)
- {
- Translate(rect);
- white = white ? white : uidraw->CreateTexture("textures/defaults/white.dds", true);
- /*uidraw->DrawQuadSimple(
- rect.x / render->ScaleCoordX(1),
- rect.y / render->ScaleCoordY(1),
- rect.w / render->ScaleCoordX(1),
- rect.h / render->ScaleCoordY(1),
- draw_color.pack_argb8888(),
- white
- );*/
- 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);
- }
- void LoadFont(Gwen::Font* pFont)
- {
- pFont->realsize = floor(pFont->size * Scale() + 2);
- auto path = WideToChar(pFont->facename.c_str());
- auto font = gEnv->pSystem->GetICryFont()->NewFont(path);
- auto success = false;
- #define flags TTFFLAG_SMOOTH_BLUR | TTFLAG_SMOOTH_AMOUNT_2X | FONTRF_VCENTERED | TTFFLAG_CREATE(4, 2)
- #define scale 1024
- if (font)
- {
- success = font->Load(path, scale, scale, flags);
- if (!success)
- {
- CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "gwen cannot load the font called '%s', reverting to 'OpenSans.ttf'", name);
- success = font->Load("fonts/OpenSans.ttf", scale, scale, flags);
- }
- if (success)
- {
- pFont->data = font;
- }
- }
- if (!success)
- {
- CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "gwen cannot load the font called 'OpenSans.ttf', reverting to cryengine's 'default' font", name);
- font = gEnv->pSystem->GetICryFont()->GetFont("default");
- pFont->data = font;
- }
- }
- void FreeFont(Gwen::Font* pFont)
- {
- if (!pFont->data) return;
- IFFont *font = (IFFont *)pFont->data;
- if (!font) return;
- font->Free();
- pFont->data = NULL;
- }
- IFFont *GetCryFont(Gwen::Font *pFont)
- {
- auto font = (IFFont*)pFont->data;
- if(!font || fabs( pFont->realsize - pFont->size * Scale() ) > 2)
- {
- FreeFont(pFont);
- LoadFont(pFont);
- if (pFont)
- {
- auto font = (IFFont*)pFont->data;
- if (font)
- return font;
- }
- return NULL;
- }
- return font;
- }
- void RenderText(Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text)
- {
- auto font = GetCryFont(pFont);
- if (!font) return;
- Translate(pos.x, pos.y);
- const Gwen::Rect& rect = ClipRegion();
- /*uidraw->DrawTextW(
- font,
- pos.x / render->ScaleCoordX(1), pos.y / render->ScaleCoordY(1),
- pFont->realsize, pFont->realsize,
- text.c_str(),
- draw_color.a, draw_color.r, draw_color.g, draw_color.b,
- UIDRAWHORIZONTAL_LEFT, UIDRAWVERTICAL_TOP, UIDRAWHORIZONTAL_LEFT, UIDRAWVERTICAL_CENTER
- );*/
- STextDrawContext params;
- params.SetSize(Vec2(1,1) * pFont->realsize);
- params.SetColor(draw_color);
- params.EnableClipping(true);
- params.SetClippingRect(rect.x, rect.y, rect.w, rect.h);
- font->DrawStringW(
- pos.x / render->ScaleCoordX(1),
- (pos.y / render->ScaleCoordY(1)),
- text.c_str(),
- false,
- params
- );
- }
- Gwen::Point MeasureText(Gwen::Font* pFont, const Gwen::UnicodeString& text)
- {
- auto font = GetCryFont(pFont);
- if (!font) return Gwen::Point(0,0);
- auto size = Vec2(0, 0);
- auto point = Gwen::Point(0,0);
- STextDrawContext params;
- params.SetSize(Vec2(1,1) * pFont->realsize);
- if (text.empty())
- {
- size = font->GetTextSizeW(L"W", false, params);
- point.y = size.y;
- /*float x = 0;
- float y = 0;
- uidraw->GetTextDimW(font, &x, &y, pFont->realsize, pFont->realsize, L"W");
- point.x = (int)x;*/
- }
- else
- {
- auto str = text.c_str();
- size = font->GetTextSizeW(str, false, params);
- point.x = size.x;
- point.y = size.y;
- /*float x = 0;
- float y = 0;
- uidraw->GetTextDimW(font, &x, &y, pFont->realsize, pFont->realsize, str);
- point.x = (int)x;
- point.y = (int)y;*/
- }
- return point;
- }
- void StartClip()
- {
- const Gwen::Rect& rect = ClipRegion();
- //render->
- render->SetScissor(rect.x, rect.y, rect.w, rect.h);
- }
- void EndClip()
- {
- render->SetScissor();
- }
- void DrawTexturedRect(Gwen::Texture* tex, Gwen::Rect rect, float u1=0.0f, float v1=0.0f, float u2=1.0f, float v2=1.0f)
- {
- auto crytex = (ITexture*)tex->data;
- u1 = u1 + 0.001;
- v1 = v1 + 0.001;
- u2 = u2 + 0.001;
- v2 = v2 + 0.001;
- #define res 1000
- u1 = floor(u1*res) / res;
- u2 = floor(u2*res) / res;
- v1 = floor(v1*res) / res;
- v2 = floor(v2*res) / res;
- Translate(rect);
- /*uidraw->DrawImage(
- crytex->GetTextureID(),
- rect.x,
- rect.y,
- rect.w,
- rect.h,
- 0,
- draw_color.r,
- draw_color.g,
- draw_color.b,
- draw_color.a,
- u1,v1,
- u2,v2
- );*/
- /*draw_color.Set(1,1,1,1);
- uidraw->DrawQuad(
- rect.x / render->ScaleCoordX(1),
- rect.y / render->ScaleCoordY(1),
- rect.w / render->ScaleCoordX(1),
- rect.h / render->ScaleCoordY(1),
- draw_color.pack_argb8888(),
- 1,1,1,1,
- crytex->GetTextureID(),
- u1,v1,
- u2,v1,
- u1,v2,
- u2,v2
- );*/
- //this method "works", but it has render issues when using it with a 3d scene
- static float* s = new float[4];
- static float* t = new float[4];
- s[0] = u1; t[0] = v1;
- s[1] = u2; t[1] = v1;
- s[2] = u2; t[2] = v2;
- s[3] = u1; t[3] = v2;
- render->DrawImageWithUV(
- rect.x,
- rect.y,
- 1.0f,
- rect.w,
- rect.h,
- crytex->GetTextureID(),
- s,
- t,
- draw_color.r,
- draw_color.g,
- draw_color.b,
- draw_color.a,
- false
- );
- }
- void LoadTexture(Gwen::Texture* pTexture)
- {
- static string tex_path = "";
- tex_path.Format("textures/%s", pTexture->name.Get().c_str());
- auto tex = gEnv->pRenderer->EF_LoadTexture(tex_path.c_str());
- pTexture->data = tex;
- pTexture->width = tex->GetWidth();
- pTexture->height = tex->GetHeight();
- }
- void FreeTexture(Gwen::Texture* pTexture)
- {
- ITexture *tex = (ITexture*)pTexture->data;
- if (tex)
- tex->Release();
- }
- Gwen::Color PixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color& col_default )
- {
- auto cry_tex = (ITexture*)pTexture->data;
- Gwen::Texture gl_tex;
- gl_tex.name = Gwen::TextObject((string)"!/" + cry_tex->GetName());
- dxLoadTexture(&gl_tex);
- return dxPixelColour(&gl_tex, x, y, col_default);
- }
- protected:
- ColorF draw_color;
- ITexture *color_capture;
- };
- class GwenInput: public IHardwareMouseEventListener, IInputEventListener
- {
- public:
- GwenInput()
- {
- gEnv->pHardwareMouse->AddListener(this);
- gEnv->pInput->SetExclusiveListener(this);
- m_MouseX = 0;
- m_MouseY = 0;
- }
- ~GwenInput()
- {
- gEnv->pHardwareMouse->RemoveListener(this);
- gEnv->pInput->RemoveEventListener(this);
- }
- void OnHardwareMouseEvent(int x, int y, EHARDWAREMOUSEEVENT eHardwareMouseEvent, int delta)
- {
- if (!my || !canvas) return;
- int dx = x - m_MouseX;
- int dy = y - m_MouseY;
- m_MouseX = x;
- m_MouseY = y;
- canvas->InputMouseMoved(m_MouseX, m_MouseY, dx, dy);
- }
- virtual bool OnInputEvent(const SInputEvent &data)
- {
- if (!my || !canvas) return false;
- if (!(data.state == eIS_Pressed || data.state == eIS_Released))
- return false;
- bool pressed = data.state == eIS_Pressed;
- using namespace Gwen::Key;
- if (data.keyId == eKI_Mouse1) return canvas->InputMouseButton(0, pressed);
- if (data.keyId == eKI_Mouse2) return canvas->InputMouseButton(1, pressed);
- if (data.keyId == eKI_Mouse3) return canvas->InputMouseButton(2, pressed);
- if (data.keyId == eKI_Mouse4) return canvas->InputMouseButton(3, pressed);
- if (data.keyId == eKI_Mouse5) return canvas->InputMouseButton(4, pressed);
- if (data.keyId == eKI_Mouse6) return canvas->InputMouseButton(5, pressed);
- if (data.keyId == eKI_MouseWheelUp) return canvas->InputMouseWheel(data.value * 100);
- if (data.keyId == eKI_MouseWheelDown) return canvas->InputMouseWheel(-data.value * 100);
- int key = -1;
- if (data.keyId == eKI_Enter) key = Return;
- if (data.keyId == eKI_Backspace) key = Backspace;
- if (data.keyId == eKI_Delete) key = Delete;
- if (data.keyId == eKI_Left) key = Left;
- if (data.keyId == eKI_Right) key = Right;
- if (data.keyId == eKI_LShift) key = Shift;
- if (data.keyId == eKI_RShift) key = Shift;
- if (data.keyId == eKI_Tab) key = Tab;
- if (data.keyId == eKI_Space) key = Space;
- if (data.keyId == eKI_Home) key = Home;
- if (data.keyId == eKI_End) key = End;
- if (data.keyId == eKI_LCtrl) key = Control;
- if (data.keyId == eKI_RCtrl) key = Control;
- if (data.keyId == eKI_Up) key = Up;
- if (data.keyId == eKI_Down) key = Down;
- if (data.keyId == eKI_Escape) key = Gwen::Key::Escape; // ugh
- if (data.keyId == eKI_LAlt) key = Alt;
- if (data.keyId == eKI_RAlt) key = Alt;
- if (key != -1)
- canvas->InputKey(key, pressed);
- if (pressed)
- {
- auto str = CharToWide(gEnv->pInput->GetKeyName(data, true));
- return canvas->InputCharacter(str[0]);
- }
- return false;
- }
- protected:
- int m_MouseX;
- int m_MouseY;
- };
- GwenInput *gwen_input;
- static bool ready = false;
- bool IsReady()
- {
- return ready;
- }
- Gwen::Skin::Base *GetSkin()
- {
- return canvas->GetSkin();
- }
- Gwen::Controls::Canvas *GetCanvas()
- {
- return canvas;
- }
- GwenRenderer *GetRenderer()
- {
- return canvas->GetSkin() ? (GwenRenderer *)canvas->GetSkin()->GetRender() : NULL;
- }
- Gwen::Font *GetFont(const char *facename)
- {
- if (GetRenderer())
- {
- Gwen::Font *font = new Gwen::Font();
- font->facename = CharToWide(facename);
- GetRenderer()->LoadFont(font);
- return font;
- }
- return NULL;
- }
- void Open()
- {
- if (ready) return;
- auto renderer = new GwenRenderer();
- auto skin = new Gwen::Skin::TexturedBase();
- skin->SetRender(renderer);
- skin->Init("gwen/defaultskin.dds");
- skin->SetDefaultFont(L"fonts/OpenSans.ttf");
- canvas = new Gwen::Controls::Canvas(skin);
- canvas->SetSize(gEnv->pRenderer->GetWidth(), gEnv->pRenderer->GetHeight());
- canvas->SetDrawBackground(true);
- canvas->SetBackgroundColor( Gwen::Color( 150, 170, 170, 255 ) );
- gwen_input = new GwenInput();
- gEnv->pHardwareMouse->Hide(false);
- ready = true;
- }
- void Close()
- {
- #define remove(n) if (n) delete n; n = NULL
- remove(canvas);
- remove(gwen_input);
- ready = false;
- }
- void Draw()
- {
- if (!IsReady()) return;
- if (gwen_input && (GwenInput*)gEnv->pInput->GetExclusiveListener() != gwen_input)
- {
- gEnv->pInput->SetExclusiveListener((IInputEventListener*)gwen_input);
- }
- if (canvas)
- {
- canvas->SetSize(gEnv->pRenderer->GetWidth(), gEnv->pRenderer->GetHeight());
- canvas->RenderCanvas();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment