Last active
August 16, 2016 13:43
-
-
Save voidproc/cbd3ebf226a3cca73d17e9417bac36fc to your computer and use it in GitHub Desktop.
Siv3D custom titlebar & close button
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
#include <Siv3D.hpp> | |
class Titlebar | |
{ | |
public: | |
Titlebar( | |
const int height = 24, | |
const int margin = 2, | |
const Color color = Palette::Mediumorchid, | |
const Color fontColor = Palette::White, | |
const Color shadowColor = Palette::Black) | |
: height_(height), margin_(margin), color_(color), fontColor_(fontColor), shadowColor_(shadowColor), grab_(), | |
font_(11, Typeface::Heavy, FontStyle::Bold) | |
{ | |
} | |
// * call every frame * | |
void update() | |
{ | |
const RectF bar = region(); | |
// d&d move window | |
if (bar.leftPressed && !grab_) { | |
grab_ = Mouse::Pos(); | |
} | |
if (grab_) { | |
if (!Input::MouseL.pressed) { | |
grab_ = none; | |
} | |
else { | |
Window::SetPos(Mouse::ScreenPos().movedBy(-grab_.value())); | |
} | |
} | |
} | |
void draw() | |
{ | |
const RectF bar = region(); | |
bar.draw(color_); | |
// highlight | |
if (grab_) { | |
bar.draw({ 0U,0U,0U,32U }); | |
} | |
else if (bar.mouseOver) { | |
bar.draw({ 255U,255U,255U,32U }); | |
} | |
font_(Window::GetTitle()).drawCenter(bar.center.movedBy(2, 2), shadowColor_); | |
font_(Window::GetTitle()).drawCenter(bar.center, fontColor_); | |
} | |
const RectF region() | |
{ | |
return RectF(Window::Size().x - margin_ * 2, height_).movedBy(margin_, margin_); | |
} | |
private: | |
int height_; | |
int margin_; | |
Color color_; | |
Color fontColor_; | |
Color shadowColor_; | |
Optional<Point> grab_; | |
Font font_; | |
}; | |
class CloseButton | |
{ | |
public: | |
CloseButton(const Vec2 pos, const int buttonSize = 24, const double crossSize = 8.0, const double crossWidth = 3.0) | |
: pos_(pos), buttonSize_(buttonSize), crossSize_(crossSize), crossWidth_(crossWidth) | |
{ | |
} | |
const bool clicked() | |
{ | |
return region().leftClicked; | |
} | |
void draw() | |
{ | |
const RectF button = region(); | |
Polygon polyCross = Geometry2D::CreatePlus(8, 3.0).rotated(45_deg); | |
polyCross.movedBy(button.center).draw(Palette::Indigo); | |
// highlight | |
if (button.mouseOver) { | |
button.draw({ 255U,255U,255U, 64U + 32U * ((System::FrameCount() / 4) % 2) }); | |
} | |
} | |
const RectF region() | |
{ | |
return RectF(buttonSize_).movedBy(pos_); | |
} | |
private: | |
Vec2 pos_; | |
int buttonSize_; | |
double crossSize_; | |
double crossWidth_; | |
}; | |
void Main() | |
{ | |
Window::SetStyle(WindowStyle::NonFrame); | |
Window::Resize(640, 480); | |
const int titlebarHeight = 24; | |
const int titlebarMargin = 2; | |
Titlebar titlebar(titlebarHeight, titlebarMargin); | |
const int closebuttonSize = titlebarHeight; | |
CloseButton closebutton(titlebar.region().tr.movedBy(-closebuttonSize, 0), closebuttonSize); | |
while (System::Update()) | |
{ | |
if (closebutton.clicked()) { | |
// quit app | |
break; | |
} | |
titlebar.update(); | |
RectF(Window::Size()).draw(Palette::Black); // draw bg | |
titlebar.draw(); | |
closebutton.draw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment