Skip to content

Commit

Permalink
push all/pop all render states + clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Daivuk committed Feb 27, 2023
1 parent 1b42da0 commit 90f0697
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 1 deletion.
16 changes: 16 additions & 0 deletions include/onut/Maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ namespace onut
{
return std::min<Tsize>(a, min(b, args...));
}

inline Vector2 min(const Vector2& a, const Vector2& b)
{
return {
min(a.x, b.x),
min(a.y, b.y)
};
}

inline Vector2 max(const Vector2& a, const Vector2& b)
{
return {
max(a.x, b.x),
max(a.y, b.y)
};
}
}

inline Vector4 ORectLocalToWorld(const Vector4& local, const Vector4& parent)
Expand Down
8 changes: 8 additions & 0 deletions include/onut/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ namespace onut
*this = value;
}

void push()
{
m_stack.push_back(m_value);
}

void pop()
{
if (!m_stack.empty())
Expand Down Expand Up @@ -122,6 +127,9 @@ namespace onut

void reset();

void pushAll();
void popAll();

RenderState<OTextureRef> textures[MAX_TEXTURES];
RenderState<BlendMode> blendMode;
RenderState<sample::Filtering> sampleFiltering;
Expand Down
2 changes: 2 additions & 0 deletions include/onut/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace onut
virtual void setCaption(const std::string& newName) = 0;
virtual void setFullscreen(bool isFullscreen) {}
virtual bool pollEvents() = 0;
virtual void setClipboard(const std::string& text) = 0;
virtual std::string getClipboard() = 0;

std::function<void(char)> onWrite;
std::function<void(WriteFunc)> onWriteFunc;
Expand Down
50 changes: 50 additions & 0 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,56 @@ namespace onut
}
clearColor.reset();
}

void RenderStates::pushAll()
{
for (int i = 0; i < MAX_TEXTURES; ++i) textures[i].push();
blendMode.push();
sampleFiltering.push();
sampleAddressMode.push();
viewport.push();
scissor.push();
projection.push();
view.push();
world.push();
wireframe.push();
depthEnabled.push();
depthWrite.push();
backFaceCull.push();
scissorEnabled.push();
primitiveMode.push();
vertexShader.push();
pixelShader.push();
vertexBuffer.push();
indexBuffer.push();
for (int i = 0; i < MAX_RENDER_TARGETS; ++i) renderTargets[i].push();
clearColor.push();
}

void RenderStates::popAll()
{
for (int i = 0; i < MAX_TEXTURES; ++i) textures[i].pop();
blendMode.pop();
sampleFiltering.pop();
sampleAddressMode.pop();
viewport.pop();
scissor.pop();
projection.pop();
view.pop();
world.pop();
wireframe.pop();
depthEnabled.pop();
depthWrite.pop();
backFaceCull.pop();
scissorEnabled.pop();
primitiveMode.pop();
vertexShader.pop();
pixelShader.pop();
vertexBuffer.pop();
indexBuffer.pop();
for (int i = 0; i < MAX_RENDER_TARGETS; ++i) renderTargets[i].pop();
clearColor.pop();
}

Renderer::Renderer()
{
Expand Down
14 changes: 14 additions & 0 deletions src/WindowSDL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,18 @@ namespace onut

return true;
}

void WindowSDL2::setClipboard(const std::string& text)
{
SDL_SetClipboardText(text.c_str());
}

std::string WindowSDL2::getClipboard()
{
auto clipboard = SDL_GetClipboardText();
if (!clipboard) return "";
std::string ret = clipboard;
SDL_free(clipboard);
return ret;
}
}
2 changes: 2 additions & 0 deletions src/WindowSDL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace onut

void setCaption(const std::string& newName) override;
bool pollEvents() override;
void setClipboard(const std::string& text) override;
std::string getClipboard() override;

SDL_Window* getSDLWindow() const;

Expand Down
42 changes: 42 additions & 0 deletions src/WindowWIN32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,46 @@ namespace onut
}
return true;
}

void WindowWIN32::setClipboard(const std::string& text)
{
if (OpenClipboard(m_handle))
{
EmptyClipboard();
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE, text.size() + 1);
if (hClipboardData)
{
char* pchData;
pchData = (char*)GlobalLock(hClipboardData);
if (pchData)
{
strcpy(pchData, text.data());
GlobalUnlock(hClipboardData);
SetClipboardData(CF_TEXT, hClipboardData);
}
}
CloseClipboard();
}
}

std::string WindowWIN32::getClipboard()
{
std::string ret;
if (OpenClipboard(m_handle))
{
HANDLE hClipboardData = GetClipboardData(CF_TEXT);
if (hClipboardData)
{
char* pchData = (char*)GlobalLock(hClipboardData);
if (pchData)
{
ret = pchData;
GlobalUnlock(hClipboardData);
}
}
CloseClipboard();
}
return ret;
}
}
2 changes: 2 additions & 0 deletions src/WindowWIN32.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace onut
void setCaption(const std::string& newName) override;
void setFullscreen(bool isFullscreen) override;
bool pollEvents() override;
void setClipboard(const std::string& text) override;
std::string getClipboard() override;

private:
HWND m_handle;
Expand Down
4 changes: 3 additions & 1 deletion uieditor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "onut/UIPanel.h"
#include "onut/Window.h"

#include <imgui/imgui.h>

DocumentView* g_pDocument = nullptr;
OUIContextRef g_pUIContext = nullptr;
OUIControlRef g_pUIScreen = nullptr;
Expand All @@ -26,7 +28,7 @@ void initSettings()
oSettings->setUserSettingDefault("width", std::to_string(screenW));
oSettings->setUserSettingDefault("height", std::to_string(screenH));
oSettings->setResolution({std::stoi(oSettings->getUserSetting("width")),
std::stoi(oSettings->getUserSetting("height"))});
std::stoi(oSettings->getUserSetting("height"))});
oSettings->setGameName("Oak Nut UI Editor");
oSettings->setIsResizableWindow(true);
oSettings->setIsEditorMode(true);
Expand Down

0 comments on commit 90f0697

Please sign in to comment.