Skip to content

Commit

Permalink
Split windowing into its own classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed May 1, 2024
1 parent 10c2d54 commit 2d6be88
Show file tree
Hide file tree
Showing 20 changed files with 913 additions and 402 deletions.
3 changes: 2 additions & 1 deletion cmake/yup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ function (yup_standalone_app)
$<$<CONFIG:DEBUG>:-gsource-map>
-sWASM=1 -sASSERTIONS=1 -sUSE_GLFW=3 -sERROR_ON_UNDEFINED_SYMBOLS=1
-sDEMANGLE_SUPPORT=1 -sSTACK_OVERFLOW_CHECK=2 -sFORCE_FILESYSTEM=1
-sNODERAWFS=0 -sMAX_WEBGL_VERSION=2 -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='$dynCall'
-sALLOW_MEMORY_GROWTH=1 -sNODERAWFS=0 -sMAX_WEBGL_VERSION=2
-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='$dynCall'
${YUP_ARG_LINK_OPTIONS})

endif()
Expand Down
35 changes: 17 additions & 18 deletions examples/graphics/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
{
auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
float dpiScale = fiddleContext->dpiScale (getNativeHandle());
x *= dpiScale;
y *= dpiScale;

Expand Down Expand Up @@ -108,7 +108,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
{
auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
float dpiScale = fiddleContext->dpiScale (getNativeHandle());
x *= dpiScale;
y *= dpiScale;

Expand All @@ -129,7 +129,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
switch (keys.getKey())
{
case juce::KeyPress::escapeKey:
close();
userTriedToCloseWindow();
break;

case juce::KeyPress::textAKey:
Expand Down Expand Up @@ -190,7 +190,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
{
float oldScale = scale;
scale *= 1.25;
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (nativeHandle());
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (getNativeHandle());
translate = cursorPos + (translate - cursorPos) * scale / oldScale;
break;
}
Expand All @@ -199,7 +199,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
{
float oldScale = scale;
scale /= 1.25;
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (nativeHandle());
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (getNativeHandle());
translate = cursorPos + (translate - cursorPos) * scale / oldScale;
break;
}
Expand All @@ -220,36 +220,35 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

title << " | " << width << " x " << height;

setWindowTitle (title);
setTitle (title);
}

private:
void timerCallback() override
void userTriedToCloseWindow() override
{
if (shouldClose())
{
stopTimer();
stopTimer();

juce::MessageManager::callAsync ([this] { juce::JUCEApplication::getInstance()->systemRequestedQuit(); });
return;
}
juce::MessageManager::callAsync ([this] { juce::JUCEApplication::getInstance()->systemRequestedQuit(); });
}

private:
void timerCallback() override
{
mainLoop (juce::Time::getMillisecondCounterHiRes() / 1000.0);

fiddleContext->tick();
}

void mainLoop (double time)
{
auto [width, height] = getSize();
auto [width, height] = getContentSize();
if (lastWidth != width || lastHeight != height)
{
DBG ("size changed to " << width << "x" << height << "\n");

lastWidth = width;
lastHeight = height;

fiddleContext->onSizeChanged (nativeHandle(), width, height, 0);
fiddleContext->onSizeChanged (getNativeHandle(), width, height, 0);
renderer = fiddleContext->makeRenderer (width, height);

needsTitleUpdate = true;
Expand Down Expand Up @@ -319,7 +318,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

renderer->drawPath (pointPath.get(), pointPaint.get());

fiddleContext->end (nativeHandle());
fiddleContext->end (getNativeHandle());

updateFrameTime (time, width, height);
}
Expand Down Expand Up @@ -412,7 +411,7 @@ struct Application : juce::JUCEApplication
juce::Logger::outputDebugString ("Starting app " + commandLineParameters);

window = std::make_unique<CustomWindow>();
window->setSize (800, 800);
window->setSize ({ 800, 800 });
window->setVisible (true);
}

Expand Down
39 changes: 19 additions & 20 deletions examples/render/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
float dpiScale = fiddleContext->dpiScale (getNativeHandle());
x *= dpiScale;
y *= dpiScale;

Expand All @@ -121,7 +121,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
float dpiScale = fiddleContext->dpiScale (getNativeHandle());
x *= dpiScale;
y *= dpiScale;

Expand All @@ -137,7 +137,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
float dpiScale = fiddleContext->dpiScale (getNativeHandle());
x *= dpiScale;
y *= dpiScale;

Expand All @@ -153,7 +153,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
float dpiScale = fiddleContext->dpiScale (getNativeHandle());
x *= dpiScale;
y *= dpiScale;

Expand All @@ -169,7 +169,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
switch (keys.getKey())
{
case juce::KeyPress::escapeKey:
close();
userTriedToCloseWindow();
break;

case juce::KeyPress::textAKey:
Expand Down Expand Up @@ -218,7 +218,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
{
float oldScale = scale;
scale *= 1.25;
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (nativeHandle());
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (getNativeHandle());
translate = cursorPos + (translate - cursorPos) * scale / oldScale;
break;
}
Expand All @@ -227,7 +227,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
{
float oldScale = scale;
scale /= 1.25;
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (nativeHandle());
rive::float2 cursorPos = rive::float2 { (float)x, (float)y } * fiddleContext->dpiScale (getNativeHandle());
translate = cursorPos + (translate - cursorPos) * scale / oldScale;
break;
}
Expand All @@ -251,20 +251,19 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

title << " | " << width << " x " << height;

setWindowTitle (title);
setTitle (title);
}

private:
void timerCallback() override
void userTriedToCloseWindow() override
{
if (shouldClose())
{
stopTimer();
stopTimer();

juce::MessageManager::callAsync ([this] { juce::JUCEApplication::getInstance()->systemRequestedQuit(); });
return;
}
juce::MessageManager::callAsync ([this] { juce::JUCEApplication::getInstance()->systemRequestedQuit(); });
}

private:
void timerCallback() override
{
mainLoop (juce::Time::getMillisecondCounterHiRes() / 1000.0);

fiddleContext->tick();
Expand Down Expand Up @@ -314,15 +313,15 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer

void mainLoop (double time)
{
auto [width, height] = getSize();
auto [width, height] = getContentSize();
if (lastWidth != width || lastHeight != height)
{
DBG ("size changed to " << width << "x" << height << "\n");

lastWidth = width;
lastHeight = height;

fiddleContext->onSizeChanged (nativeHandle(), width, height, 0);
fiddleContext->onSizeChanged (getNativeHandle(), width, height, 0);
renderer = fiddleContext->makeRenderer (width, height);

needsTitleUpdate = true;
Expand Down Expand Up @@ -392,7 +391,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
renderer->restore();
}

fiddleContext->end (nativeHandle());
fiddleContext->end (getNativeHandle());

updateFrameTime (time, width, height);
}
Expand Down Expand Up @@ -479,7 +478,7 @@ struct Application : juce::JUCEApplication
juce::Logger::outputDebugString ("Starting app " + commandLineParameters);

window = std::make_unique<CustomWindow>();
window->setSize (1280, 866);
window->setSize ({ 1280, 866 });
window->setVisible (true);
}

Expand Down
8 changes: 4 additions & 4 deletions modules/yup_graphics/graphics/yup_Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class JUCE_API Graphics
public:
Graphics() noexcept = default;

constexpr Graphics (const Graphics& other) noexcept = delete;
constexpr Graphics (Graphics&& other) noexcept = default;
constexpr Graphics& operator=(const Graphics& other) noexcept = delete;
constexpr Graphics& operator=(Graphics&& other) noexcept = default;
Graphics (const Graphics& other) noexcept = delete;
Graphics (Graphics&& other) noexcept = default;
Graphics& operator=(const Graphics& other) noexcept = delete;
Graphics& operator=(Graphics&& other) noexcept = default;

private:
};
Expand Down
6 changes: 6 additions & 0 deletions modules/yup_graphics/primitives/yup_Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class JUCE_API Point
return { x + delta.x, y + delta.y };
}

template <class T>
constexpr Point<T> to() const noexcept
{
return { static_cast<T> (x), static_cast<T> (y) };
}

constexpr bool operator== (const Point& other) const noexcept
{
return x == other.x && y == other.y;
Expand Down
25 changes: 24 additions & 1 deletion modules/yup_graphics/primitives/yup_Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class JUCE_API Rectangle
public:
constexpr Rectangle() noexcept = default;

constexpr Rectangle (const Point<ValueType>& xy, const Size<ValueType>& size) noexcept
: xy (xy)
, size (size)
{
}

constexpr Rectangle (const Rectangle& other) noexcept = default;
constexpr Rectangle (Rectangle&& other) noexcept = default;
constexpr Rectangle& operator=(const Rectangle& other) noexcept = default;
Expand Down Expand Up @@ -63,6 +69,17 @@ class JUCE_API Rectangle
return size;
}

constexpr Rectangle withSize (const Size<ValueType>& newSize) noexcept
{
return { xy, newSize };
}

template <class T>
constexpr Rectangle withSize (const Size<T>& newSize) noexcept
{
return { xy, newSize };
}

constexpr Rectangle& translate (ValueType deltaX, ValueType deltaY) noexcept
{
xy.translate (deltaX, deltaY);
Expand All @@ -75,11 +92,17 @@ class JUCE_API Rectangle
return *this;
}

constexpr Point<ValueType> translated (Point<ValueType> delta) const noexcept
constexpr Rectangle<ValueType> translated (Point<ValueType> delta) const noexcept
{
return { xy.translated (delta), size };
}

template <class T>
constexpr Rectangle<T> to() const noexcept
{
return { xy.template to<T>(), size.template to<T>() };
}

constexpr bool operator== (const Rectangle& other) const noexcept
{
return xy == other.xy && size == other.size;
Expand Down
6 changes: 6 additions & 0 deletions modules/yup_graphics/primitives/yup_Size.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class JUCE_API Size
return { width, newHeight };
}

template <class T>
constexpr Size<T> to() const noexcept
{
return { static_cast<T> (width), static_cast<T> (height) };
}

constexpr bool operator== (const Size& other) const noexcept
{
return width == other.width && height == other.height;
Expand Down
10 changes: 10 additions & 0 deletions modules/yup_gui/application/yup_Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ namespace juce

//==============================================================================

JUCEApplication::JUCEApplication()
{
staticInitialisation();
}

JUCEApplication::~JUCEApplication()
{
staticFinalisation();
}

bool JUCEApplication::moreThanOneInstanceAllowed()
{
return true;
Expand Down
6 changes: 4 additions & 2 deletions modules/yup_gui/application/yup_Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace juce
class JUCE_API JUCEApplication : public JUCEApplicationBase
{
public:
JUCEApplication() = default;
~JUCEApplication() = default;
JUCEApplication();
~JUCEApplication() override;

bool moreThanOneInstanceAllowed() override;

Expand All @@ -45,6 +45,8 @@ class JUCE_API JUCEApplication : public JUCEApplicationBase
int lineNumber) override;

private:
static void staticInitialisation();
static void staticFinalisation();

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JUCEApplication)
};
Expand Down
Loading

0 comments on commit 2d6be88

Please sign in to comment.