CODE:
#pragma once
namespace fw{
class graphic{
#ifdef FW_GRAPHIC_DRAG_
static bool OutputFlag;
#endif
int myhandle;
int myblendmode;
int blendpal;
int mywidth, myheight;
int myx, myy;
bool mytrans;
int myx2, myy2;
double myextrate;
double myradian;
int mycx, mycy;
int myx3, myy3;
int myx4, myy4;
int myredbright, mygreenbright, mybluebright;
uint Drawmode;
fw::figure Figure;
std::string OutPut;
mutable fw::mouse m;
bool Center;
void init(){
noblend();
bright(255,255,255);
trans(true);
normal();
}
void SetPreDraw() const {
SetDrawBlendMode(blendmode(), blend() );
SetDrawBright(redbright(), greenbright(), bluebright() );
}
struct GraphicHandleData{
int handle;
GraphicHandleData(){}
GraphicHandleData(int h){ handle = h; }
};
void loaded(){
GetGraphSize(handle(), &mywidth,&myheight);
if(figure().size()==0){
figure() = fw::quad(fw::coor(0,0), fw::coor(width(),height() ) );
}
}
public:
// fixed number
static const uint Normal=0, Turn=1, Extend=2, Extrate=3, Radian=4, Radiate=5, Modi=6;
// initialize
GraphicHandleData gradata() const { return GraphicHandleData(handle() ); }
graphic & gradata(const GraphicHandleData ghdata){
myhandle = ghdata.handle;
loaded();
return *this;
}
graphic & set(const graphic & gra){
myhandle = gra.handle();
myblendmode = gra.blendmode();
blendpal = gra.blend();
mywidth = gra.width();
myheight = gra.height();
myx = gra.x();
myy = gra.y();
mytrans = gra.trans();
myx2 = gra.x2();
myy2 = gra.y2();
myextrate = gra.extrate();
myradian = gra.radian();
mycx = gra.cx();
mycy = gra.cy();
myx3 = gra.x3();
myy3 = gra.y3();
myx4 = gra.x4();
myy4 = gra.y4();
myredbright = gra.redbright();
mygreenbright = gra.greenbright();
mybluebright = gra.bluebright();
Drawmode = drawmode();
return *this;
}
graphic & load(const std::string & path){
myhandle = fw::LoadGraph(path);
loaded();
return *this;
}
graphic & load(const std::string & path, int red,int green,int blue){
myhandle = fw::LoadGraph(path, red,green,blue);
loaded();
return *this;
}
// constructor
graphic(){ init(); }
graphic(const GraphicHandleData ghdata){
init();
gradata(ghdata);
}
graphic(const graphic & gra){
init();
set(gra);
}
graphic(const std::string & path){
init();
load(path);
}
graphic(const std::string & path, int red,int green,int blue){
init();
load(path, red,green,blue);
}
//operator=
graphic & operator= (const GraphicHandleData ghdata){
gradata(ghdata);
return *this;
}
graphic & operator= (const graphic & gra){
set(gra);
return *this;
}
graphic & operator= (const std::string & path){
load(path);
return *this;
}
// property
int width() const { return mywidth; }
int height() const { return myheight; }
int x() const { return myx; }
graphic & x(int setx){
myx = setx;
return *this;
}
int y() const { return myy; }
graphic & y(int sety){
myy = sety;
return *this;
}
bool trans() const { return mytrans; }
graphic & trans(bool settrans){
mytrans = settrans;
return *this;
}
int x2() const { return myx2; }
graphic & x2(int setx2){
myx2 = setx2;
return *this;
}
int y2() const { return myy2; }
graphic & y2(int sety2){
myy2 = sety2;
return *this;
}
double extrate() const { return myextrate; }
graphic & extrate(double setextrate){
myextrate = setextrate;
return *this;
}
double radian() const { return myradian; }
graphic & radian(double setradian){
myradian = setradian;
return *this;
}
int cx() const { return mycx; }
graphic & cx(int setcx){
mycx = setcx;
return *this;
}
int cy() const { return mycy; }
graphic & cy(int setcy){
mycy = setcy;
return *this;
}
int x3() const { return myx3; }
graphic & x3(int setx3){
myx3 = setx3;
return *this;
}
int y3() const { return myy3; }
graphic & y3(int sety3){
myy3 = sety3;
return *this;
}
int x4() const { return myx4; }
graphic & x4(int setx4){
myx4 = setx4;
return *this;
}
int y4() const { return myy4; }
graphic & y4(int sety4){
myy4 = sety4;
return *this;
}
int blend() const { return blendpal; }
graphic & blend(int palameter){
blendpal = palameter;
return *this;
}
int blendmode() const { return myblendmode; }
bool ifnoblend() const { return blendmode()==DX_BLENDMODE_NOBLEND; }
graphic & noblend(){
myblendmode = DX_BLENDMODE_NOBLEND;
return *this;
}
bool ifalphablend() const { return blendmode()==DX_BLENDMODE_ALPHA; }
graphic & alphablend(){
myblendmode = DX_BLENDMODE_ALPHA;
return *this;
}
graphic & alphablend(int palameter){
alphablend();
blend(palameter);
return *this;
}
bool ifaddblend() const { return blendmode()==DX_BLENDMODE_ADD; }
graphic & addblend(){
myblendmode = DX_BLENDMODE_ADD;
return *this;
}
graphic & addblend(int palameter){
addblend();
blend(palameter);
return *this;
}
bool ifsubblend() const { return blendmode()==DX_BLENDMODE_SUB; }
graphic & subblend(){
myblendmode = DX_BLENDMODE_SUB;
return *this;
}
graphic & subblend(int palameter){
subblend();
blend(palameter);
return *this;
}
bool ifmulablend() const { return blendmode()==DX_BLENDMODE_MULA; }
graphic & mulablend(){
myblendmode = DX_BLENDMODE_MULA;
return *this;
}
graphic & mulablend(int palameter){
mulablend();
blend(palameter);
return *this;
}
bool ifinvblend() const { return blendmode()==DX_BLENDMODE_INVSRC; }
graphic & invblend(){
myblendmode = DX_BLENDMODE_INVSRC;
return *this;
}
graphic & invblend(int palameter){
invblend();
blend(palameter);
return *this;
}
int redbright() const { return myredbright; }
graphic & redbright(int red){
myredbright = red;
return *this;
}
int greenbright() const { return mygreenbright; }
graphic & greenbright(int green){
mygreenbright = green;
return *this;
}
int bluebright() const { return mybluebright; }
graphic & bluebright(int blue){
mybluebright = blue;
return *this;
}
graphic & bright(int red, int green, int blue){
redbright(red);
greenbright(green);
bluebright(blue);
return *this;
}
graphic & highbright(){
bright(255,255,255);
return *this;
}
int handle() const { return myhandle; }
graphic & handle(int sethandle){
myhandle = sethandle;
GetGraphSize(handle(), &mywidth,&myheight);
return *this;
}
uint drawmode() const { return Drawmode; }
graphic & drawmode(const uint mode){
Drawmode = mode;
return *this;
}
bool ifnormal() const { return drawmode()==graphic::Normal; }
bool ifturn() const { return drawmode()==graphic::Turn; }
bool ifextend() const { return drawmode()==graphic::Extend; }
bool ifextrate() const { return drawmode()==graphic::Extrate; }
bool ifradian() const { return drawmode()==graphic::Radian; }
bool ifradiate() const { return drawmode()==graphic::Radiate; }
bool ifmodi() const { return drawmode()==graphic::Modi; }
const fw::figure & figure() const { return Figure; }
fw::figure & figure(){ return Figure; }
std::string output() const { return OutPut; }
graphic & output(const std::string & path){
OutPut = path;
return *this;
}
graphic & input(const std::string & path){
fw::bfile data(path);
data >> myx;
data >> myy;
}
bool center() const { return Center; }
graphic & center(bool accept){
Center = accept;
return *this;
}
// draw
graphic & normal(){
drawmode(graphic::Normal);
return *this;
}
graphic & turn(){
drawmode(graphic::Turn);
return *this;
}
graphic & extend(){
drawmode(graphic::Extend);
return *this;
}
graphic & extrate(){
drawmode(graphic::Extrate);
return *this;
}
graphic & radian(){
drawmode(graphic::Radian);
return *this;
}
graphic & radiate(){
drawmode(graphic::Radiate);
return *this;
}
graphic & modi(){
drawmode(graphic::Modi);
return *this;
}
const graphic & draw() const {
#ifdef FW_GRAPHIC_DRAG_
#ifdef FW_EACHFRAME_POP_UP_
if(fw::eachframe.notcalled() ){
fw::popup("fw::eachframe()が一度も呼ばれていません", "fw::eachframeエラー");
throw std::invalid_argument("fw_eachframe_error");
}
#endif
graphic & g = const_cast(*this);
g.x(x()+m.dragx() );
g.y(y()+m.dragy() );
if(output().empty() ){
#ifdef FW_GRAPHIC_POP_UP_
fw::popup("出力先が設定されていません", "fw::graphicエラー");
#endif
throw std::invalid_argument("fw_graphic_output_error");
}
if(OutputFlag){ fw::bfile(output() ).clear() buttons;
static uint ChildID;
WNDCLASSEX wc;
HWND mainWH;
static bool notmine(const uint childID){ return childID=ChildID; }
static LRESULT CALLBACK WindowProc(HWND hwnd,UINT uiMsg,WPARAM wParam,LPARAM lParam){
static HBRUSH aquaBrsh = NULL;
uint childID = static_cast(GetWindowLongPtr(reinterpret_cast(lParam), GWLP_ID) );
switch(uiMsg){
case WM_COMMAND:
if(notmine(childID) ) break;
buttons[childID-100] = true;
break;
case WM_CTLCOLORSTATIC:
if(notmine(childID) ) break;
if(childID%2){
// 背景色を白色に設定
SetBkColor(reinterpret_cast(wParam), 0xffffff);
return reinterpret_cast(GetStockObject(WHITE_BRUSH) );
}
else{
// 背景色を水色に設定
SetBkColor(reinterpret_cast(wParam), RGB(200,255,255) );
if(aquaBrsh==NULL) aquaBrsh = CreateSolidBrush(RGB(200,255,255) );
return reinterpret_cast(aquaBrsh);
}
case WM_DESTROY:
if(aquaBrsh!=NULL){
DeleteObject(aquaBrsh);
aquaBrsh = NULL;
}
}
return DefWindowProc(hwnd, uiMsg, wParam, lParam);
}
static void InitIndex(uint index){ buttons[index] = false; }
static bool clicked(uint index){
bool res = buttons[index];
InitIndex(index);
return res;
}
static HMENU childID(){ return reinterpret_cast(ChildID++); }
public:
class text{
HWND handle;
public:
text(){ handle = NULL; }
text & create(const newwin & w, const std::string & str, int x,int y, int width,int height){
if(handle!=NULL) del();
handle = CreateWindow(
"STATIC",
str.c_str(),
WS_CHILD|WS_VISIBLE,
x,y,
width,height,
w.handle(),
newwin::childID(),
GetModuleHandle(NULL),
NULL
);
return *this;
}
text(const newwin & w, const std::string & str, int x,int y, int width,int height){ create(w, str, x,y, width,height); }
std::string gets(){
char content[257];
GetWindowText(handle, content, 256);
return content;
}
text & sets(const std::string & output){
SetWindowText(handle, output.c_str() );
return *this;
}
text & del(){
DestroyWindow(handle);
return *this;
}
~text(){ del(); }
};
class button{
HWND handle;
uint index;
public:
button(){ handle = NULL; }
button & create(const newwin & w, const std::string & str, int x,int y, int width,int height, bool defbutton = false){
if(handle!=NULL) del();
int attribute = WS_CHILD|WS_VISIBLE;
if(defbutton) attribute = attribute|BS_DEFPUSHBUTTON;
else attribute = attribute|BS_PUSHBUTTON;
HMENU id = newwin::childID();
index = reinterpret_cast(id);
index -= 100;
newwin::InitIndex(index);
handle = CreateWindow(
"BUTTON",
str.c_str(),
attribute,
x,y,
width,height,
w.handle(),
id,
GetModuleHandle(NULL),
NULL
);
return *this;
}
button(const newwin & w, const std::string & str, int x,int y, int width,int height, bool defbutton = false){ create(w, str, x,y, width,height, defbutton); }
bool clicked() const { return newwin::clicked(index); }
button & del(){
DestroyWindow(handle);
return *this;
}
~button(){ del(); }
};
newwin(){}
void init(const std::string &title, int width=CW_USEDEFAULT, int height=CW_USEDEFAULT){
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = reinterpret_cast(COLOR_APPWORKSPACE+1);
wc.hCursor = LoadCursor(GetModuleHandle(NULL),IDC_ARROW);
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = "myClass";
wc.lpszMenuName = NULL;
wc.style = CS_DBLCLKS;
RegisterClassEx(&wc);
mainWH = CreateWindowEx(
WS_EX_TOOLWINDOW,
"myClass",
title.c_str(),
WS_BORDER|WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT,
width,height,
NULL,NULL,GetModuleHandle(NULL),NULL
);
ShowWindow(mainWH,SW_SHOWDEFAULT);
UpdateWindow(mainWH);
}
newwin(const std::string &title, int width=CW_USEDEFAULT, int height=CW_USEDEFAULT){ init(title, width, height); }
HWND handle() const { return mainWH; }
newwin & del(){
for(uint i=minID;i(i) );
DestroyWindow(mainWH);
return *this;
}
~newwin(){ del(); }
};
#ifdef FW_WIN_MAIN_
std::map newwin::buttons;
uint newwin::ChildID = minID;
#endif
}