-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d7b526
commit fafa338
Showing
9 changed files
with
240 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
#pragma once | ||
|
||
#include "context.h" | ||
|
||
namespace Karm::Gfx { | ||
|
||
struct DropShadow { | ||
Paint fill = Gfx::BLACK; | ||
f64 radius{8}; | ||
Math::Vec2i offset{}; | ||
|
||
static DropShadow elevated(f64 v) { | ||
return { | ||
Gfx::BLACK.withOpacity(0.7), | ||
v * 2, | ||
{0, (int)v}, | ||
}; | ||
} | ||
|
||
auto &withFill(Paint p) { | ||
fill = p; | ||
return *this; | ||
} | ||
|
||
auto &withRadius(f64 r) { | ||
radius = r; | ||
return *this; | ||
} | ||
|
||
auto &withOffset(Math::Vec2i o) { | ||
offset = o; | ||
return *this; | ||
} | ||
|
||
void paint(Gfx::Context &g) { | ||
g.layer(offset, [&](Context &ctx) { | ||
ctx.fill(fill); | ||
ctx.apply(BlurFilter{(isize)radius}); | ||
}); | ||
} | ||
}; | ||
|
||
inline DropShadow dropShadow(auto... args) { | ||
return DropShadow(args...); | ||
} | ||
|
||
struct BoxShadow { | ||
Gfx::Color fill; | ||
f64 blur; | ||
f64 spread; | ||
Math::Vec2i offset; | ||
|
||
static BoxShadow elevated(f64 v) { | ||
return { | ||
Gfx::BLACK.withOpacity(0.5), | ||
v * 2, | ||
-v, | ||
{0, (int)v}, | ||
}; | ||
} | ||
|
||
auto &withFill(Gfx::Color p) { | ||
fill = p; | ||
return *this; | ||
} | ||
|
||
auto &withBlur(f64 r) { | ||
blur = r; | ||
return *this; | ||
} | ||
|
||
auto &withSpread(f64 r) { | ||
spread = r; | ||
return *this; | ||
} | ||
|
||
auto &withOffset(Math::Vec2i o) { | ||
offset = o; | ||
return *this; | ||
} | ||
|
||
void paint(Gfx::Context &g, Math::Recti bound) const { | ||
/// 1 / sqrt(2) | ||
static constexpr f64 IS2 = 0.7071067811865475; | ||
|
||
// 1 - (1 / sqrt(2)) | ||
static constexpr f64 IS2M = 1 - IS2; | ||
|
||
bound = bound.grow(spread); | ||
bound = bound.offset(offset); | ||
|
||
auto grad = Gfx::Gradient::linear().withColors(fill, fill.withOpacity(0)).bake(); | ||
|
||
auto topStart = Math::Recti::fromTwoPoint( | ||
bound.topStart(), | ||
bound.topStart() - Math::Vec2i{(int)blur, (int)blur} | ||
); | ||
|
||
auto topEnd = Math::Recti::fromTwoPoint( | ||
bound.topEnd(), | ||
bound.topEnd() + Math::Vec2i{(int)blur, -(int)blur} | ||
); | ||
|
||
auto bottomStart = Math::Recti::fromTwoPoint( | ||
bound.bottomStart(), | ||
bound.bottomStart() + Math::Vec2i{-(int)blur, (int)blur} | ||
); | ||
|
||
auto bottomEnd = Math::Recti::fromTwoPoint( | ||
bound.bottomEnd(), | ||
bound.bottomEnd() + Math::Vec2i{(int)blur, (int)blur} | ||
); | ||
|
||
auto top = Math::Recti::fromTwoPoint( | ||
bound.topStart() - Math::Vec2i{0, (int)blur}, | ||
bound.topEnd() | ||
); | ||
|
||
auto bottom = Math::Recti::fromTwoPoint( | ||
bound.bottomStart(), | ||
bound.bottomEnd() + Math::Vec2i{0, (int)blur} | ||
); | ||
|
||
auto start = Math::Recti::fromTwoPoint( | ||
bound.topStart() - Math::Vec2i{(int)blur, 0}, | ||
bound.bottomStart() | ||
); | ||
|
||
auto end = Math::Recti::fromTwoPoint( | ||
bound.topEnd(), | ||
bound.bottomEnd() + Math::Vec2i{(int)blur, 0} | ||
); | ||
|
||
grad.withType(Gradient::RADIAL); | ||
g.fillStyle(grad.withPoints({1, 1}, {IS2M, IS2M})); | ||
g.fill(topStart); | ||
|
||
g.fillStyle(grad.withPoints({0, 1}, {IS2, IS2M})); | ||
g.fill(topEnd); | ||
|
||
g.fillStyle(grad.withPoints({1, 0}, {IS2M, IS2})); | ||
g.fill(bottomStart); | ||
|
||
g.fillStyle(grad.withPoints({0, 0}, {IS2, IS2})); | ||
g.fill(bottomEnd); | ||
|
||
grad.withType(Gradient::LINEAR); | ||
g.fillStyle(grad.withPoints({0, 1}, {0, 0})); | ||
g.fill(top); | ||
|
||
g.fillStyle(grad.withPoints({0, 0}, {0, 1})); | ||
g.fill(bottom); | ||
|
||
g.fillStyle(grad.withPoints({1, 0}, {0, 0})); | ||
g.fill(start); | ||
|
||
g.fillStyle(grad.withPoints({0, 0}, {1, 0})); | ||
g.fill(end); | ||
|
||
g.fillStyle(fill); | ||
g.fill(bound); | ||
|
||
// Debug overlay | ||
return; | ||
g.plot(topStart, Gfx::GREEN); | ||
g.plot(topEnd, Gfx::RED); | ||
g.plot(bottomStart, Gfx::BLUE); | ||
g.plot(bottomEnd, Gfx::YELLOW); | ||
|
||
g.plot(top, Gfx::AMBER); | ||
g.plot(bottom, Gfx::INDIGO); | ||
g.plot(start, Gfx::PINK); | ||
g.plot(end, Gfx::CYAN); | ||
} | ||
}; | ||
|
||
Gfx::BoxShadow boxShadow(auto... args) { | ||
return Gfx::BoxShadow(args...); | ||
} | ||
|
||
} // namespace Karm::Gfx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.